diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2021-08-30 23:25:11 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-30 23:25:11 (GMT) |
commit | 044e8d866fdde3804bdb2282c7d23a8074de8f6f (patch) | |
tree | ed2fdeb3bacec221e5ee96ad3544667ab55d6376 /Tools/scripts/update_file.py | |
parent | 5246dbc2a12bf8e64e18efee2fdce02a350bbf09 (diff) | |
download | cpython-044e8d866fdde3804bdb2282c7d23a8074de8f6f.zip cpython-044e8d866fdde3804bdb2282c7d23a8074de8f6f.tar.gz cpython-044e8d866fdde3804bdb2282c7d23a8074de8f6f.tar.bz2 |
bpo-45019: Add a tool to generate list of modules to include for frozen modules (gh-27980)
Frozen modules must be added to several files in order to work properly. Before this change this had to be done manually. Here we add a tool to generate the relevant lines in those files instead. This helps us avoid mistakes and omissions.
https://bugs.python.org/issue45019
Diffstat (limited to 'Tools/scripts/update_file.py')
-rw-r--r-- | Tools/scripts/update_file.py | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/Tools/scripts/update_file.py b/Tools/scripts/update_file.py index 224585c..cfc4e2b 100644 --- a/Tools/scripts/update_file.py +++ b/Tools/scripts/update_file.py @@ -6,23 +6,47 @@ This avoids wholesale rebuilds when a code (re)generation phase does not actually change the in-tree generated code. """ +import contextlib import os +import os.path import sys -def main(old_path, new_path): - with open(old_path, 'rb') as f: +@contextlib.contextmanager +def updating_file_with_tmpfile(filename, tmpfile=None): + """A context manager for updating a file via a temp file. + + The context manager provides two open files: the source file open + for reading, and the temp file, open for writing. + + Upon exiting: both files are closed, and the source file is replaced + with the temp file. + """ + # XXX Optionally use tempfile.TemporaryFile? + if not tmpfile: + tmpfile = filename + '.tmp' + elif os.path.isdir(tmpfile): + tmpfile = os.path.join(tmpfile, filename + '.tmp') + + with open(tmpfile, 'w') as outfile: + with open(filename) as infile: + yield infile, outfile + 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(new_path, 'rb') as f: + with open(tmpfile, 'rb') as f: new_contents = f.read() if old_contents != new_contents: - os.replace(new_path, old_path) + os.replace(tmpfile, filename) else: - os.unlink(new_path) + os.unlink(tmpfile) 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) - main(sys.argv[1], sys.argv[2]) + update_file_with_tmpfile(sys.argv[1], sys.argv[2]) |