From 48240def0942091ced78583f2094fa987ed32411 Mon Sep 17 00:00:00 2001 From: David Schischke Date: Thu, 26 Sep 2024 10:30:13 +0200 Subject: [PATCH] fix: improved conditional file handling for pylint/ruff + cicd --- hooks/post_gen_project.py | 60 +++++++++---------- .../.gitlab-ci.yml | 0 .../{ => .conditional_files}/.pylintrc | 0 .../{ => .conditional_files}/ruff.toml | 0 .../.temp_ci_cd/.manifest.yaml | 9 --- 5 files changed, 30 insertions(+), 39 deletions(-) rename {{cookiecutter.repo_name}}/{.temp_ci_cd => .conditional_files}/.gitlab-ci.yml (100%) rename {{cookiecutter.repo_name}}/{ => .conditional_files}/.pylintrc (100%) rename {{cookiecutter.repo_name}}/{ => .conditional_files}/ruff.toml (100%) delete mode 100644 {{cookiecutter.repo_name}}/.temp_ci_cd/.manifest.yaml diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index eaf9834..7504b71 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -5,8 +5,6 @@ import subprocess from abc import ABC, abstractmethod -import yaml - # as cookiecutter is currently (v2.1.1) unable to support local imports in hooks # the environment management code has to be included here @@ -107,34 +105,34 @@ def copy_chosen_files(self) -> None: else: shutil.copy2(src, dst) - def remove_unused_linter_files(self): - with open(f"{self.temp_files_dir}/.manifest.yaml", "r", encoding="utf-8") as f: - manifest = yaml.safe_load(f) - for feature in manifest["features"]: - if not feature["enabled"]: - for resource in feature["resources"]: - os.remove(resource) - -def get_ci_cd_file_manager(ci_cd_options: str) -> ConditionalFileManager: +def get_conditional_file_manager(ci_cd_option: str, linter_option: str) -> ConditionalFileManager: template_root_dir = pathlib.Path.cwd() - temp_files_dir = template_root_dir.joinpath(".temp_ci_cd") - if ci_cd_options == "none": - manager = ConditionalFileManager( - temp_files_dir=temp_files_dir, - template_root_dir=template_root_dir, - relevant_paths_list=[], - ) - elif ci_cd_options == "gitlab": - manager = ConditionalFileManager( - temp_files_dir=temp_files_dir, - template_root_dir=template_root_dir, - relevant_paths_list=[".gitlab-ci.yml"], - ) - else: - raise NotImplementedError( - f"Option {ci_cd_options} is not implemented as ci_cd_file_manager" - ) + temp_files_dir = template_root_dir.joinpath(".conditional_files") + relevant_paths = [] + + match ci_cd_option: + case "gitlab": + relevant_paths.append(".gitlab-ci.yml") + case "none": + pass + case _: + raise NotImplementedError(f"Option {ci_cd_option} is not implemented as CI/CD pipeline") + + match linter_option: + case "pylint": + relevant_paths.append(".pylintrc") + case "ruff": + relevant_paths.append("ruff.toml") + case _: + raise NotImplementedError(f"Option {linter_option} is not implemented as Linters") + + manager = ConditionalFileManager( + temp_files_dir=temp_files_dir, + template_root_dir=template_root_dir, + relevant_paths_list=relevant_paths, + ) + return manager @@ -153,11 +151,13 @@ def get_ci_cd_file_manager(ci_cd_options: str) -> ConditionalFileManager: print("before") # setup ci/cd related files (if any) print("initializing") - CICD_FILE_MANAGER = get_ci_cd_file_manager(ci_cd_options="{{cookiecutter.cicd_configuration}}") + CICD_FILE_MANAGER = get_conditional_file_manager( + ci_cd_option="{{cookiecutter.cicd_configuration}}", + linter_option="{{cookiecutter.linter_name}}", + ) print("copying") CICD_FILE_MANAGER.copy_chosen_files() print("cleaning") - CICD_FILE_MANAGER.remove_unused_linter_files() CICD_FILE_MANAGER.clean_temp_dir() print("after") diff --git a/{{cookiecutter.repo_name}}/.temp_ci_cd/.gitlab-ci.yml b/{{cookiecutter.repo_name}}/.conditional_files/.gitlab-ci.yml similarity index 100% rename from {{cookiecutter.repo_name}}/.temp_ci_cd/.gitlab-ci.yml rename to {{cookiecutter.repo_name}}/.conditional_files/.gitlab-ci.yml diff --git a/{{cookiecutter.repo_name}}/.pylintrc b/{{cookiecutter.repo_name}}/.conditional_files/.pylintrc similarity index 100% rename from {{cookiecutter.repo_name}}/.pylintrc rename to {{cookiecutter.repo_name}}/.conditional_files/.pylintrc diff --git a/{{cookiecutter.repo_name}}/ruff.toml b/{{cookiecutter.repo_name}}/.conditional_files/ruff.toml similarity index 100% rename from {{cookiecutter.repo_name}}/ruff.toml rename to {{cookiecutter.repo_name}}/.conditional_files/ruff.toml diff --git a/{{cookiecutter.repo_name}}/.temp_ci_cd/.manifest.yaml b/{{cookiecutter.repo_name}}/.temp_ci_cd/.manifest.yaml deleted file mode 100644 index b87502a..0000000 --- a/{{cookiecutter.repo_name}}/.temp_ci_cd/.manifest.yaml +++ /dev/null @@ -1,9 +0,0 @@ -features: - - name: pylint - enabled: {{cookiecutter.linter_name == "pylint"}} - resources: - - .pylintrc - - name: ruff - enabled: {{cookiecutter.linter_name == "ruff"}} - resources: - - ruff.toml