Add a context wrapper for mtime preservation (#21258)
Sometimes we need to patch a file that is a dependency for some other automatically generated file that comes in a release tarball. As a result, make tries to regenerate the dependent file using additional tools (e.g. help2man), which would not be needed otherwise. In some cases, it's preferable to avoid that (e.g. see #21255). A way to do that is to save the modification timestamps before patching and restoring them afterwards. This PR introduces a context wrapper that does that.
This commit is contained in:
@@ -63,7 +63,8 @@
|
||||
'touchp',
|
||||
'traverse_tree',
|
||||
'unset_executable_mode',
|
||||
'working_dir'
|
||||
'working_dir',
|
||||
'keep_modification_time'
|
||||
]
|
||||
|
||||
|
||||
@@ -1819,3 +1820,24 @@ def remove_directory_contents(dir):
|
||||
os.unlink(entry)
|
||||
else:
|
||||
shutil.rmtree(entry)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def keep_modification_time(*filenames):
|
||||
"""
|
||||
Context manager to keep the modification timestamps of the input files.
|
||||
Tolerates and has no effect on non-existent files and files that are
|
||||
deleted by the nested code.
|
||||
|
||||
Parameters:
|
||||
*filenames: one or more files that must have their modification
|
||||
timestamps unchanged
|
||||
"""
|
||||
mtimes = {}
|
||||
for f in filenames:
|
||||
if os.path.exists(f):
|
||||
mtimes[f] = os.path.getmtime(f)
|
||||
yield
|
||||
for f, mtime in mtimes.items():
|
||||
if os.path.exists(f):
|
||||
os.utime(f, (os.path.getatime(f), mtime))
|
||||
|
Reference in New Issue
Block a user