Skip to content

Commit

Permalink
fix: pre-commit; hooks style; bandit /tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
christokur committed Feb 3, 2023
1 parent 4883b51 commit 6285196
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Expand Up @@ -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
Expand Down
45 changes: 32 additions & 13 deletions cookiecutter/hooks.py
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions cookiecutter/main.py
Expand Up @@ -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:
Expand Down

0 comments on commit 6285196

Please sign in to comment.