From 9a4317c0580f49f7d07ae7914dd74a32ec1b2c8f Mon Sep 17 00:00:00 2001 From: Josiah Baldwin Date: Mon, 25 Jul 2022 17:10:05 -0700 Subject: [PATCH 1/4] Added argument to remove __main__.py file --- cookiecutter.json | 3 ++- hooks/post_gen_project.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cookiecutter.json b/cookiecutter.json index 4ea59c0..8b9867d 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -1,4 +1,5 @@ { "project_name": "Best Practices", - "repo_name": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}" + "repo_name": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}", + "binary":"y" } diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index afcc8ba..3f60bb7 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -13,6 +13,12 @@ def set_python_version(): with open(file_name, "w") as f: f.write(contents) +def remove_main_if_lib(): + is_lib = '{{ cookiecutter.binary }}' + main_file_path = os.path.join('{{ cookiecutter.repo_name }}', '__main__.py') + if not (is_lib == "y" or is_lib == "Y"): + os.remove(main_file_path) + print(main_file_path) SUCCESS = "\x1b[1;32m" INFO = "\x1b[1;33m" @@ -21,6 +27,7 @@ def set_python_version(): def main(): set_python_version() + remove_main_if_lib() print(SUCCESS + "Project successfully initialized" + TERMINATOR) From 416708105daccc75d1c37787b7af359f11519239 Mon Sep 17 00:00:00 2001 From: Josiah Baldwin Date: Mon, 25 Jul 2022 19:40:51 -0700 Subject: [PATCH 2/4] Remove unneeded print statement in post hook --- hooks/post_gen_project.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 3f60bb7..f92535a 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -18,7 +18,6 @@ def remove_main_if_lib(): main_file_path = os.path.join('{{ cookiecutter.repo_name }}', '__main__.py') if not (is_lib == "y" or is_lib == "Y"): os.remove(main_file_path) - print(main_file_path) SUCCESS = "\x1b[1;32m" INFO = "\x1b[1;33m" From 123c9912d3ddaf0446c2299daca6ff081b927656 Mon Sep 17 00:00:00 2001 From: Josiah Baldwin Date: Mon, 25 Jul 2022 19:51:33 -0700 Subject: [PATCH 3/4] Move repo name to global var --- hooks/post_gen_project.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index f92535a..1a363d8 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -1,6 +1,7 @@ import os import sys +repo_name = '{{cookiecutter.repo_name }}' def set_python_version(): python_version = str(sys.version_info.major) + "." + str(sys.version_info.minor) @@ -15,7 +16,7 @@ def set_python_version(): def remove_main_if_lib(): is_lib = '{{ cookiecutter.binary }}' - main_file_path = os.path.join('{{ cookiecutter.repo_name }}', '__main__.py') + main_file_path = os.path.join(repo_name, '__main__.py') if not (is_lib == "y" or is_lib == "Y"): os.remove(main_file_path) From 8cede4c9c6eff44276d394ebc3ebb4ffb751462d Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Tue, 26 Jul 2022 03:01:00 +0000 Subject: [PATCH 4/4] 'Refactored by Sourcery' --- hooks/post_gen_project.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 1a363d8..058f92f 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -4,7 +4,7 @@ repo_name = '{{cookiecutter.repo_name }}' def set_python_version(): - python_version = str(sys.version_info.major) + "." + str(sys.version_info.minor) + python_version = f"{str(sys.version_info.major)}.{str(sys.version_info.minor)}" file_names = ["Dockerfile", "Pipfile", ".github/workflows/test.yml"] for file_name in file_names: @@ -16,8 +16,8 @@ def set_python_version(): def remove_main_if_lib(): is_lib = '{{ cookiecutter.binary }}' - main_file_path = os.path.join(repo_name, '__main__.py') - if not (is_lib == "y" or is_lib == "Y"): + if is_lib not in {"y", "Y"}: + main_file_path = os.path.join(repo_name, '__main__.py') os.remove(main_file_path) SUCCESS = "\x1b[1;32m" @@ -28,7 +28,7 @@ def remove_main_if_lib(): def main(): set_python_version() remove_main_if_lib() - print(SUCCESS + "Project successfully initialized" + TERMINATOR) + print(f"{SUCCESS}Project successfully initialized{TERMINATOR}") if __name__ == "__main__":