Skip to content

Instantly share code, notes, and snippets.

@agners
Created January 28, 2019 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agners/e8e16af9a3786229595cc6b61475702f to your computer and use it in GitHub Desktop.
Save agners/e8e16af9a3786229595cc6b61475702f to your computer and use it in GitHub Desktop.
Update Python3 bytecode timestamps
#!/usr/bin/python3
# Python scripts which reads all Python bytecode files (pyc) and clears
# the timestamp (set to 0)
# This is useful for OSTree which clears modification time of all source
# files. Processed bytecode files are then not considered stale anymore.
import sys, os
import marshal
import importlib.util
rootdir = str(sys.argv[1])
def set_mtime(name, newtime):
with open(name, 'r+b') as fd:
fd.seek(0)
magic = fd.read(4) # python version specific magic num
#date = fd.read(4) # mtime of py file...
if (magic != importlib.util.MAGIC_NUMBER):
print('bad magic number in {!r}: {!r}'.format(name, magic))
return
# Replace timestamp
fd.seek(4)
fd.write(int.to_bytes(newtime, 4, 'little'))
def main(rootdir):
for root,d_names,f_names in os.walk(rootdir):
for f in f_names:
if f.endswith(".pyc"):
set_mtime(os.path.join(root, f), 0)
if __name__ == '__main__':
main(rootdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment