|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from datetime import datetime |
| 4 | +from datetime import timedelta |
| 5 | +from random import randint |
| 6 | +from subprocess import Popen |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +def main(def_args=sys.argv[1:]): |
| 11 | + args = arguments(def_args) |
| 12 | + curr_date = datetime.now() |
| 13 | + directory = 'repository-' + curr_date.strftime('%Y-%m-%d-%H-%M-%S') |
| 14 | + repository = args.repository |
| 15 | + user_name = args.user_name |
| 16 | + user_email = args.user_email |
| 17 | + if repository is not None: |
| 18 | + start = repository.rfind('/') + 1 |
| 19 | + end = repository.rfind('.') |
| 20 | + directory = repository[start:end] |
| 21 | + no_weekends = args.no_weekends |
| 22 | + frequency = args.frequency |
| 23 | + days_before = args.days_before |
| 24 | + if days_before < 0: |
| 25 | + sys.exit('days_before must not be negative') |
| 26 | + days_after = args.days_after |
| 27 | + if days_after < 0: |
| 28 | + sys.exit('days_after must not be negative') |
| 29 | + os.mkdir(directory) |
| 30 | + os.chdir(directory) |
| 31 | + run(['git', 'init', '-b', 'main']) |
| 32 | + |
| 33 | + if user_name is not None: |
| 34 | + run(['git', 'config', 'user.name', user_name]) |
| 35 | + |
| 36 | + if user_email is not None: |
| 37 | + run(['git', 'config', 'user.email', user_email]) |
| 38 | + |
| 39 | + start_date = curr_date.replace(hour=20, minute=0) - timedelta(days_before) |
| 40 | + for day in (start_date + timedelta(n) for n |
| 41 | + in range(days_before + days_after)): |
| 42 | + if (not no_weekends or day.weekday() < 5) \ |
| 43 | + and randint(0, 100) < frequency: |
| 44 | + for commit_time in (day + timedelta(minutes=m) |
| 45 | + for m in range(contributions_per_day(args))): |
| 46 | + contribute(commit_time) |
| 47 | + |
| 48 | + if repository is not None: |
| 49 | + run(['git', 'remote', 'add', 'origin', repository]) |
| 50 | + run(['git', 'branch', '-M', 'main']) |
| 51 | + run(['git', 'push', '-u', 'origin', 'main']) |
| 52 | + |
| 53 | + print('\nRepository generation ' + |
| 54 | + '\x1b[6;30;42mcompleted successfully\x1b[0m!') |
| 55 | + |
| 56 | + |
| 57 | +def contribute(date): |
| 58 | + with open(os.path.join(os.getcwd(), 'README.md'), 'a') as file: |
| 59 | + file.write(message(date) + '\n\n') |
| 60 | + run(['git', 'add', '.']) |
| 61 | + run(['git', 'commit', '-m', '"%s"' % message(date), |
| 62 | + '--date', date.strftime('"%Y-%m-%d %H:%M:%S"')]) |
| 63 | + |
| 64 | + |
| 65 | +def run(commands): |
| 66 | + Popen(commands).wait() |
| 67 | + |
| 68 | + |
| 69 | +def message(date): |
| 70 | + return date.strftime('Contribution: %Y-%m-%d %H:%M') |
| 71 | + |
| 72 | + |
| 73 | +def contributions_per_day(args): |
| 74 | + max_c = args.max_commits |
| 75 | + if max_c > 20: |
| 76 | + max_c = 20 |
| 77 | + if max_c < 1: |
| 78 | + max_c = 1 |
| 79 | + return randint(1, max_c) |
| 80 | + |
| 81 | + |
| 82 | +def arguments(argsval): |
| 83 | + parser = argparse.ArgumentParser() |
| 84 | + parser.add_argument('-nw', '--no_weekends', |
| 85 | + required=False, action='store_true', default=False, |
| 86 | + help="""do not commit on weekends""") |
| 87 | + parser.add_argument('-mc', '--max_commits', type=int, default=10, |
| 88 | + required=False, help="""Defines the maximum amount of |
| 89 | + commits a day the script can make. Accepts a number |
| 90 | + from 1 to 20. If N is specified the script commits |
| 91 | + from 1 to N times a day. The exact number of commits |
| 92 | + is defined randomly for each day. The default value |
| 93 | + is 10.""") |
| 94 | + parser.add_argument('-fr', '--frequency', type=int, default=80, |
| 95 | + required=False, help="""Percentage of days when the |
| 96 | + script performs commits. If N is specified, the script |
| 97 | + will commit N%% of days in a year. The default value |
| 98 | + is 80.""") |
| 99 | + parser.add_argument('-r', '--repository', type=str, required=False, |
| 100 | + help="""A link on an empty non-initialized remote git |
| 101 | + repository. If specified, the script pushes the changes |
| 102 | + to the repository. The link is accepted in SSH or HTTPS |
| 103 | + format. For example: git@github.com:user/repo.git or |
| 104 | + https://github.com/user/repo.git""") |
| 105 | + parser.add_argument('-un', '--user_name', type=str, required=False, |
| 106 | + help="""Overrides user.name git config. |
| 107 | + If not specified, the global config is used.""") |
| 108 | + parser.add_argument('-ue', '--user_email', type=str, required=False, |
| 109 | + help="""Overrides user.email git config. |
| 110 | + If not specified, the global config is used.""") |
| 111 | + parser.add_argument('-db', '--days_before', type=int, default=365, |
| 112 | + required=False, help="""Specifies the number of days |
| 113 | + before the current date when the script will start |
| 114 | + adding commits. For example: if it is set to 30 the |
| 115 | + first commit date will be the current date minus 30 |
| 116 | + days.""") |
| 117 | + parser.add_argument('-da', '--days_after', type=int, default=0, |
| 118 | + required=False, help="""Specifies the number of days |
| 119 | + after the current date until which the script will be |
| 120 | + adding commits. For example: if it is set to 30 the |
| 121 | + last commit will be on a future date which is the |
| 122 | + current date plus 30 days.""") |
| 123 | + return parser.parse_args(argsval) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments