diff options
| author | Eric Snow <ericsnowcurrently@gmail.com> | 2021-09-24 14:35:47 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-24 14:35:47 -0600 |
| commit | 7c801e0fa603b155eab3fd19698aa90854ac5a7b (patch) | |
| tree | b949ff73ff706412848c8c3a2f67c85fa105716c /Tools/scripts/update_file.py | |
| parent | bfe26bbad787c124f0ce144cff1b513ef9d2dc9c (diff) | |
| download | cpython-git-7c801e0fa603b155eab3fd19698aa90854ac5a7b.tar.gz | |
bpo-45020: Fix some corner cases for frozen module generation. (gh-28538)
This also includes some cleanup in preparation for a PR to make the "make all" output less noisy.
https://bugs.python.org/issue45020
Diffstat (limited to 'Tools/scripts/update_file.py')
| -rw-r--r-- | Tools/scripts/update_file.py | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/Tools/scripts/update_file.py b/Tools/scripts/update_file.py index 5e22486ec0..b4182c1d0c 100644 --- a/Tools/scripts/update_file.py +++ b/Tools/scripts/update_file.py @@ -46,19 +46,47 @@ def updating_file_with_tmpfile(filename, tmpfile=None): update_file_with_tmpfile(filename, tmpfile) -def update_file_with_tmpfile(filename, tmpfile): - with open(filename, 'rb') as f: - old_contents = f.read() - with open(tmpfile, 'rb') as f: - new_contents = f.read() - if old_contents != new_contents: +def update_file_with_tmpfile(filename, tmpfile, *, create=False): + try: + targetfile = open(filename, 'rb') + except FileNotFoundError: + if not create: + raise # re-raise + outcome = 'created' os.replace(tmpfile, filename) else: - os.unlink(tmpfile) + with targetfile: + old_contents = targetfile.read() + with open(tmpfile, 'rb') as f: + new_contents = f.read() + # Now compare! + if old_contents != new_contents: + outcome = 'updated' + os.replace(tmpfile, filename) + else: + outcome = 'same' + os.unlink(tmpfile) + return outcome if __name__ == '__main__': - if len(sys.argv) != 3: - print("Usage: %s <path to be updated> <path with new contents>" % (sys.argv[0],)) - sys.exit(1) - update_file_with_tmpfile(sys.argv[1], sys.argv[2]) + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('--create', action='store_true') + parser.add_argument('--exitcode', action='store_true') + parser.add_argument('filename', help='path to be updated') + parser.add_argument('tmpfile', help='path with new contents') + args = parser.parse_args() + kwargs = vars(args) + setexitcode = kwargs.pop('exitcode') + + outcome = update_file_with_tmpfile(**kwargs) + if setexitcode: + if outcome == 'same': + sys.exit(0) + elif outcome == 'updated': + sys.exit(1) + elif outcome == 'created': + sys.exit(2) + else: + raise NotImplementedError |
