Skip to content

Commit 26c181c

Browse files
authored
Fix replay file processing (#74)
- Really skip copying replay file if it doesn't exit - Remove the `_output_dir` from the replay file - Don't save the `_template` in the replay file if it looks local
2 parents f0cd840 + 28d68a3 commit 26c181c

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

cookiecutter/hooks/post_gen_project.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,37 @@ def copy_replay_file() -> None:
141141

142142
if not src.exists():
143143
print(f"WARNING: No replay file found in {src}. Skipping...")
144+
return
144145

145146
try:
146-
_shutil.copyfile(src, dst)
147-
except (OSError, IOError) as error:
147+
with src.open("r", encoding="utf8") as input_file:
148+
replay_data = _json.load(input_file)
149+
150+
# Remove the _output_dir key from the replay data because it is an absolute path
151+
# that depends on the current environment and we don't want to add it as part of
152+
# the generated project.
153+
replay_data["cookiecutter"].pop("_output_dir", None)
154+
155+
if template := replay_data["cookiecutter"].get("_template"):
156+
if not template.startswith(("gh:", "git@", "https://")):
157+
print(
158+
f"WARNING: The replay file's `_template` ({template}) doesn't seem "
159+
"to be a remote repository, it won't be saved to avoid storing a "
160+
"local template in the new repository. If this is incorrect, "
161+
f"please add it back manually to {dst}."
162+
)
163+
replay_data["cookiecutter"].pop("_template", None)
164+
165+
with dst.open("w", encoding="utf8") as output_file:
166+
_json.dump(replay_data, output_file, indent=2)
167+
except KeyError as error:
168+
print(
169+
f"WARNING: Error parsing the replay file {src} -> {dst} ({error}). "
170+
"Skipping..."
171+
)
172+
# We want to catch a broad range of exceptions here because we don't want to fail
173+
# the whole generation process just because the replay file is broken.
174+
except Exception as error: # pylint: disable=broad-except
148175
print(
149176
f"WARNING: Error copying the replay file {src} -> {dst} ({error}). "
150177
"Skipping..."

0 commit comments

Comments
 (0)