From 36374505208a53387729c3102ed165fecb202e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 13 Aug 2018 17:52:53 +0200 Subject: [PATCH] Do not clobber real files when creating a patch When a file unpacked from a crate is edited, it is OK to edit it directly. But when editing something that was already there, we should make a temporary copy. Let's do this copy next to the file being edited, so it's easy for the user to open related files. --- rust2rpm/__main__.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 86acc37..04e9245 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -131,7 +131,7 @@ def toml_from_crate(cratef, crate, version): raise IOError('crate does not contain Cargo.toml file') yield toml -def make_patch(toml, enabled=True): +def make_patch(toml, enabled=True, tmpfile=False): if not enabled: return [] @@ -139,9 +139,21 @@ def make_patch(toml, enabled=True): mtime_before = file_mtime(toml) toml_before = open(toml).readlines() - subprocess.check_call([editor, toml]) + + # When we are editing a git checkout, we should not modify the real file. + # When we are editing an unpacked crate, we are free to edit anything. + # Let's keep the file name as close as possible to make editing easier. + if tmpfile: + tmpfile = tempfile.NamedTemporaryFile('w+t', dir=os.path.dirname(toml), + prefix='Cargo.', suffix='.toml') + tmpfile.writelines(toml_before) + tmpfile.flush() + fname = tmpfile.name + else: + fname = toml + subprocess.check_call([editor, fname]) mtime_after = file_mtime(toml) - toml_after = open(toml).readlines() + toml_after = open(fname).readlines() toml_relpath = '/'.join(toml.split('/')[-2:]) diff = list(difflib.unified_diff(toml_before, toml_after, fromfile=toml_relpath, tofile=toml_relpath, @@ -158,7 +170,7 @@ def make_diff_metadata(crate, version, patch=False): cratef, crate, version = local_crate(crate, version) else: toml, crate, version = local_toml(crate, version) - diff = make_patch(toml, enabled=patch) + diff = make_patch(toml, enabled=patch, tmpfile=True) metadata = Metadata.from_file(toml) return metadata.name, diff, metadata else: