From 6285196adc576da28e81372cce45902bfbc1c980 Mon Sep 17 00:00:00 2001 From: Christo De Lange Date: Fri, 3 Feb 2023 12:02:38 -0500 Subject: [PATCH] fix: pre-commit; hooks style; bandit /tmp --- .pre-commit-config.yaml | 2 +- cookiecutter/hooks.py | 45 +++++++++++++++++++++++++++++------------ cookiecutter/main.py | 1 + 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 96d89ad9a..e5d1e7f52 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: - id: check-xml - id: check-yaml exclude: "not_rendered.yml|invalid-config.yaml" - - repo: https://gitlab.com/pycqa/flake8 + - repo: https://github.com/PyCQA/flake8 rev: 4.0.1 hooks: - id: flake8 diff --git a/cookiecutter/hooks.py b/cookiecutter/hooks.py index 26ae2faa9..0fb60dde3 100644 --- a/cookiecutter/hooks.py +++ b/cookiecutter/hooks.py @@ -105,20 +105,39 @@ def run_script_with_context(script_path, cwd, context): with open(script_path, encoding='utf-8') as file: contents = file.read() - temp_name = None - with tempfile.NamedTemporaryFile(delete=False, mode='wb', suffix=extension) as temp: - env = StrictEnvironment(context=context, keep_trailing_newline=True) - template = env.from_string(contents) - output = template.render(**context) - if os.getenv('COOKIECUTTER_DEBUG_HOOKS', None): - import pathlib - temp = tempfile.NamedTemporaryFile(delete=False, mode='wb', suffix=extension, dir='/tmp', prefix=os.path.basename(_)+'+') - temp = pathlib.Path(temp.name) + temp_name = None # Just to make sure it's defined in this scope. + env = StrictEnvironment(context=context, keep_trailing_newline=True) + template = env.from_string(contents) + output = template.render(**context) + if os.getenv('COOKIECUTTER_DEBUG_HOOKS', "").lower() in ( + "1", + "true", + "yes", + "on", + "enabled", + ): + import pathlib + + with tempfile.NamedTemporaryFile( + delete=False, + mode='wb', + suffix=extension, + dir=tempfile.gettempdir(), + prefix=os.path.basename(_) + '+', + ) as temp: + debug_temp = pathlib.Path(temp.name) temp.unlink() - temp = pathlib.Path(os.path.join(temp.parent, temp.stem.split('+')[0]+temp.suffix)) - temp.write_text(output, encoding='utf-8') - temp_name = str(temp) - else: + debug_temp = pathlib.Path( + os.path.join( + debug_temp.parent, debug_temp.stem.split('+')[0] + debug_temp.suffix + ) + ) + debug_temp.write_text(output, encoding='utf-8') + temp_name = str(debug_temp) + else: + with tempfile.NamedTemporaryFile( + delete=False, mode='wb', suffix=extension + ) as temp: temp.write(output.encode('utf-8')) temp_name = temp.name diff --git a/cookiecutter/main.py b/cookiecutter/main.py index d4c6dbb99..0d733e0ec 100644 --- a/cookiecutter/main.py +++ b/cookiecutter/main.py @@ -118,6 +118,7 @@ def cookiecutter( dump(config_dict['replay_dir'], template_name, context) from cookiecutter import __version__ as cookiecutter__version__ + context['__version__'] = cookiecutter__version__ # Create project from local context and project template. with import_patch: