pyc-timestamp.sh 661 B

12345678910111213141516171819
  1. #!/bin/bash
  2. # Usage: pyc-timestamp.sh "2001-01-01" FILENAMES...
  3. # Overwrite (in place) the timestamp in .pyc Python bytecode files.
  4. #
  5. # http://hg.python.org/cpython/file/2.7/Lib/py_compile.py#l123
  6. # http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html
  7. # http://benno.id.au/blog/2013/01/15/python-determinism
  8. TIMESPEC="$1"
  9. shift
  10. hex=$(printf 0x%08x $(date +%s --date="$TIMESPEC"))
  11. # Write little-endian.
  12. esc=$(printf "\\\\x%02x\\\\x%02x\\\\x%02x\\\\x%02x" $(($hex&0xff)) $((($hex>>8)&0xff)) $((($hex>>16)&0xff)) $((($hex>>24)&0xff)))
  13. for filename in "$@"; do
  14. echo $filename
  15. echo -n -e "$esc" | dd of="$filename" bs=1 seek=4 conv=notrunc
  16. done