From 702987f546a4e26142c7c34d32376dccd641fa8d Mon Sep 17 00:00:00 2001 From: Zkh-dot Date: Thu, 26 Dec 2024 15:54:31 +0300 Subject: [PATCH 1/3] add reruning script, moved dir creation to separated func --- contribute.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/contribute.py b/contribute.py index 0bb6202fdd..3f15849017 100755 --- a/contribute.py +++ b/contribute.py @@ -8,7 +8,7 @@ import sys -def main(def_args=sys.argv[1:]): +def main(def_args=sys.argv[1:]) -> None: args = arguments(def_args) curr_date = datetime.now() directory = 'repository-' + curr_date.strftime('%Y-%m-%d-%H-%M-%S') @@ -27,9 +27,8 @@ 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']) + + chdir_to_repo(repository, directory) if user_name is not None: run(['git', 'config', 'user.name', user_name]) @@ -55,23 +54,40 @@ def main(def_args=sys.argv[1:]): '\x1b[6;30;42mcompleted successfully\x1b[0m!') -def contribute(date): +def contribute(date) -> None: 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"')]) + + +def chdir_to_repo(repository, directory) -> None: + git_command = ['git', repository, directory] + if ".git" in os.listdir(): + git_command.insert(1, 'submodule') + git_command.insert(2, 'add') + else: + git_command.insert(1, 'clone') + if directory not in os.listdir(): + try: + run(git_command) + except Exception as e: + print(e) + raise Exception('Repository does not exist. Please check if the link is ssh and do your user has access to your ssh keys') + os.chdir(directory) + run(['git', 'pull']) -def run(commands): +def run(commands) -> None: Popen(commands).wait() -def message(date): +def message(date) -> str: return date.strftime('Contribution: %Y-%m-%d %H:%M') -def contributions_per_day(args): +def contributions_per_day(args) -> int: max_c = args.max_commits if max_c > 20: max_c = 20 @@ -80,7 +96,7 @@ def contributions_per_day(args): return randint(1, max_c) -def arguments(argsval): +def arguments(argsval) -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument('-nw', '--no_weekends', required=False, action='store_true', default=False, From 30a4e25a9f23a1000e601605b3bd48c91c70f1f0 Mon Sep 17 00:00:00 2001 From: Zkh-dot Date: Mon, 24 Feb 2025 18:32:41 +0300 Subject: [PATCH 2/3] add chaitu20000 suggested changes --- contribute.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/contribute.py b/contribute.py index 3f15849017..66c8eb18a1 100755 --- a/contribute.py +++ b/contribute.py @@ -7,7 +7,6 @@ from subprocess import Popen import sys - def main(def_args=sys.argv[1:]) -> None: args = arguments(def_args) curr_date = datetime.now() @@ -28,6 +27,7 @@ def main(def_args=sys.argv[1:]) -> None: if days_after < 0: sys.exit('days_after must not be negative') + create_directory(directory) chdir_to_repo(repository, directory) if user_name is not None: @@ -60,21 +60,23 @@ def contribute(date) -> None: run(['git', 'add', '.']) run(['git', 'commit', '-m', '"%s"' % message(date), '--date', date.strftime('"%Y-%m-%d %H:%M:%S"')]) - + def chdir_to_repo(repository, directory) -> None: - git_command = ['git', repository, directory] + if repository is None: + return + if ".git" in os.listdir(): - git_command.insert(1, 'submodule') - git_command.insert(2, 'add') + git_command = ['git', 'submodule', 'add', repository, directory] else: - git_command.insert(1, 'clone') + git_command = ['git', 'clone', repository, directory] + if directory not in os.listdir(): try: run(git_command) except Exception as e: print(e) - raise Exception('Repository does not exist. Please check if the link is ssh and do your user has access to your ssh keys') + raise Exception('Repository does not exist. Please check if the link is SSH and do your user has access to your ssh keys') os.chdir(directory) run(['git', 'pull']) @@ -140,5 +142,11 @@ def arguments(argsval) -> argparse.Namespace: return parser.parse_args(argsval) +def create_directory(directory: str) -> None: + if not os.path.exists(directory): + os.mkdir(directory) + os.chdir(directory) + + if __name__ == "__main__": - main() + main() \ No newline at end of file From a739538d51d11689ed740329f80395cd0d00344c Mon Sep 17 00:00:00 2001 From: Zkh-dot Date: Mon, 24 Feb 2025 18:36:10 +0300 Subject: [PATCH 3/3] add print into subrepo case --- contribute.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/contribute.py b/contribute.py index 66c8eb18a1..ae354ddc41 100755 --- a/contribute.py +++ b/contribute.py @@ -66,10 +66,14 @@ def chdir_to_repo(repository, directory) -> None: if repository is None: return + git_command = ['git', repository, directory] + if ".git" in os.listdir(): - git_command = ['git', 'submodule', 'add', repository, directory] + git_command.insert(1, 'submodule') + git_command.insert(2, 'add') + print("it appears you are running this script in repo. adding new repo as submodule") else: - git_command = ['git', 'clone', repository, directory] + git_command.insert(1, 'clone') if directory not in os.listdir(): try: