From be0cf8ac8fba9a85f602b4f9d6c6390eb07862a1 Mon Sep 17 00:00:00 2001 From: Lavesh Sharma Date: Thu, 20 Mar 2025 13:36:29 +0530 Subject: [PATCH] Update contribute.py --- contribute.py | 108 ++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 66 deletions(-) diff --git a/contribute.py b/contribute.py index 0bb6202fdd..a0001fcc19 100755 --- a/contribute.py +++ b/contribute.py @@ -1,12 +1,14 @@ #!/usr/bin/env python import argparse import os -from datetime import datetime -from datetime import timedelta +from datetime import datetime, timedelta from random import randint from subprocess import Popen import sys +import logging +# Configure logging +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def main(def_args=sys.argv[1:]): args = arguments(def_args) @@ -15,10 +17,12 @@ def main(def_args=sys.argv[1:]): repository = args.repository user_name = args.user_name user_email = args.user_email + if repository is not None: start = repository.rfind('/') + 1 end = repository.rfind('.') directory = repository[start:end] + no_weekends = args.no_weekends frequency = args.frequency days_before = args.days_before @@ -27,50 +31,47 @@ def main(def_args=sys.argv[1:]): days_after = args.days_after if days_after < 0: sys.exit('days_after must not be negative') - os.mkdir(directory) - os.chdir(directory) - run(['git', 'init', '-b', 'main']) - if user_name is not None: - run(['git', 'config', 'user.name', user_name]) + try: + os.mkdir(directory) + os.chdir(directory) + run(['git', 'init', '-b', 'main']) - if user_email is not None: - run(['git', 'config', 'user.email', user_email]) + if user_name is not None: + run(['git', 'config', 'user.name', user_name]) - start_date = curr_date.replace(hour=20, minute=0) - timedelta(days_before) - for day in (start_date + timedelta(n) for n - in range(days_before + days_after)): - if (not no_weekends or day.weekday() < 5) \ - and randint(0, 100) < frequency: - for commit_time in (day + timedelta(minutes=m) - for m in range(contributions_per_day(args))): - contribute(commit_time) + if user_email is not None: + run(['git', 'config', 'user.email', user_email]) - if repository is not None: - run(['git', 'remote', 'add', 'origin', repository]) - run(['git', 'branch', '-M', 'main']) - run(['git', 'push', '-u', 'origin', 'main']) + start_date = curr_date.replace(hour=20, minute=0) - timedelta(days=days_before) + for day in (start_date + timedelta(n) for n in range(days_before + days_after)): + if (not no_weekends or day.weekday() < 5) and randint(0, 100) < frequency: + for commit_time in (day + timedelta(minutes=m) for m in range(contributions_per_day(args))): + contribute(commit_time) - print('\nRepository generation ' + - '\x1b[6;30;42mcompleted successfully\x1b[0m!') + if repository is not None: + run(['git', 'remote', 'add', 'origin', repository]) + run(['git', 'branch', '-M', 'main']) + run(['git', 'push', '-u', 'origin', 'main']) + logging.info('\nRepository generation \x1b[6;30;42mcompleted successfully\x1b[0m!') + + except Exception as e: + logging.error(f'An error occurred: {e}') + sys.exit(1) def contribute(date): with open(os.path.join(os.getcwd(), 'README.md'), 'a') as file: file.write(message(date) + '\n\n') run(['git', 'add', '.']) - run(['git', 'commit', '-m', '"%s"' % message(date), - '--date', date.strftime('"%Y-%m-%d %H:%M:%S"')]) - + run(['git', 'commit', '-m', f'"{message(date)}"', '--date', date.strftime('"%Y-%m-%d %H:%M:%S"')]) def run(commands): Popen(commands).wait() - def message(date): return date.strftime('Contribution: %Y-%m-%d %H:%M') - def contributions_per_day(args): max_c = args.max_commits if max_c > 20: @@ -79,50 +80,25 @@ def contributions_per_day(args): max_c = 1 return randint(1, max_c) - def arguments(argsval): parser = argparse.ArgumentParser() - parser.add_argument('-nw', '--no_weekends', - required=False, action='store_true', default=False, - help="""do not commit on weekends""") - parser.add_argument('-mc', '--max_commits', type=int, default=10, - required=False, help="""Defines the maximum amount of - commits a day the script can make. Accepts a number - from 1 to 20. If N is specified the script commits - from 1 to N times a day. The exact number of commits - is defined randomly for each day. The default value - is 10.""") - parser.add_argument('-fr', '--frequency', type=int, default=80, - required=False, help="""Percentage of days when the - script performs commits. If N is specified, the script - will commit N%% of days in a year. The default value - is 80.""") + parser.add_argument('-nw', '--no_weekends', required=False, action='store_true', default=False, + help="Do not commit on weekends") + parser.add_argument('-mc', '--max_commits', type=int, default=10, required=False, + help="Defines the maximum amount of commits a day the script can make. Accepts a number from 1 to 20. If N is specified the script commits from 1 to N times a day. The exact number of commits is defined randomly for each day. The default value is 10.") + parser.add_argument('-fr', '--frequency', type=int, default=80, required=False, + help="Percentage of days when the script performs commits. If N is specified, the script will commit N%% of days in a year. The default value is 80.") parser.add_argument('-r', '--repository', type=str, required=False, - help="""A link on an empty non-initialized remote git - repository. If specified, the script pushes the changes - to the repository. The link is accepted in SSH or HTTPS - format. For example: git@github.com:user/repo.git or - https://github.com/user/repo.git""") + help="A link on an empty non-initialized remote git repository. If specified, the script pushes the changes to the repository. The link is accepted in SSH or HTTPS format. For example: git@github.com:user/repo.git or https://github.com/user/repo.git") parser.add_argument('-un', '--user_name', type=str, required=False, - help="""Overrides user.name git config. - If not specified, the global config is used.""") + help="Overrides user.name git config. If not specified, the global config is used.") parser.add_argument('-ue', '--user_email', type=str, required=False, - help="""Overrides user.email git config. - If not specified, the global config is used.""") - parser.add_argument('-db', '--days_before', type=int, default=365, - required=False, help="""Specifies the number of days - before the current date when the script will start - adding commits. For example: if it is set to 30 the - first commit date will be the current date minus 30 - days.""") - parser.add_argument('-da', '--days_after', type=int, default=0, - required=False, help="""Specifies the number of days - after the current date until which the script will be - adding commits. For example: if it is set to 30 the - last commit will be on a future date which is the - current date plus 30 days.""") + help="Overrides user.email git config. If not specified, the global config is used.") + parser.add_argument('-db', '--days_before', type=int, default=365, required=False, + help="Specifies the number of days before the current date when the script will start adding commits. For example: if it is set to 30 the first commit date will be the current date minus 30 days.") + parser.add_argument('-da', '--days_after', type=int, default=0, required=False, + help="Specifies the number of days after the current date until which the script will be adding commits. For example: if it is set to 30 the last commit will be on a future date which is the current date plus 30 days.") return parser.parse_args(argsval) - if __name__ == "__main__": main()