patching: only modify temporary files (and fix patch output)

This commit is contained in:
Fabio Valentini 2023-10-03 22:24:59 +02:00
parent 11554c5053
commit 4a9a7bd4a5
No known key found for this signature in database
GPG key ID: 5AC5F572E5D410AF
3 changed files with 16 additions and 3 deletions

View file

@ -172,7 +172,7 @@ def main():
for fname, diff in zip(patch_files, diffs): for fname, diff in zip(patch_files, diffs):
if fname and diff: if fname and diff:
print(f"# {fname}") print(f"# {fname}")
print("".join(diff), end="") print("\n".join(diff))
else: else:
with open(spec_file, "w") as fobj: with open(spec_file, "w") as fobj:
fobj.write(spec_contents) fobj.write(spec_contents)
@ -180,7 +180,7 @@ def main():
for fname, diff in zip(patch_files, diffs): for fname, diff in zip(patch_files, diffs):
if fname and diff: if fname and diff:
with open(fname, "w") as fobj: with open(fname, "w") as fobj:
fobj.writelines(diff) fobj.write("\n".join(diff))
log.success(f"Generated: {fobj.name}") log.success(f"Generated: {fobj.name}")

View file

@ -149,6 +149,16 @@ def guess_local_project_name_from_path(project: str) -> str:
return name return name
@contextlib.contextmanager
def toml_temp_copy(toml_path: str):
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = f"{temp_dir}/Cargo.toml"
with open(temp_path, "w") as file:
with open(toml_path) as orig:
file.write(orig.read())
yield temp_path
def process_project_local( def process_project_local(
project: str, patch: bool, patch_foreign: bool project: str, patch: bool, patch_foreign: bool
) -> tuple[str, tuple[Optional[list[str]], Optional[list[str]]], Metadata, list[str], list[str]]: ) -> tuple[str, tuple[Optional[list[str]], Optional[list[str]]], Metadata, list[str], list[str]]:
@ -174,7 +184,9 @@ def process_project_local(
package = metadata.packages[0] package = metadata.packages[0]
name = package.name name = package.name
features = package.get_feature_names() features = package.get_feature_names()
diffs = make_patches(name, package.version, patch, patch_foreign, toml_path, features)
with toml_temp_copy(toml_path) as temp_toml:
diffs = make_patches(name, package.version, patch, patch_foreign, temp_toml, features)
# ensure metadata is up-to-date with changes from patches # ensure metadata is up-to-date with changes from patches
metadata = Metadata.from_cargo(toml_path) metadata = Metadata.from_cargo(toml_path)

View file

@ -61,6 +61,7 @@ def make_diff_from_lines(
tofile=diff_path, tofile=diff_path,
fromfiledate=mtime_before, fromfiledate=mtime_before,
tofiledate=mtime_after, tofiledate=mtime_after,
lineterm="",
) )
) )