@@ -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