From 0d6dde67a01878b0d708b4216e2b31f1c76544fb Mon Sep 17 00:00:00 2001 From: "Bernhard M. Wiedemann" Date: Sat, 25 Feb 2017 06:42:28 +0100 Subject: [PATCH] allow for reproducible builds of python packages See https://reproducible-builds.org/ for why this is good and https://reproducible-builds.org/specs/source-date-epoch/ for the definition of this variable. Background: In some distributions like openSUSE, binary rpms contain precompiled .pyc files. And packages like amqp or twisted dynamically generate .py files at build time so those have the current time and that timestamp gets embedded into the .pyc file header. When we then adapt file timestamps in rpms to be constant, the timestamp in the .pyc header will no more match the .py timestamp in the filesystem. The software will still work, but it will not use the .pyc file as it should. --- Lib/py_compile.py | 4 ++++ 1 file changed, 4 insertions(+) Index: Python-3.6.15/Lib/py_compile.py =================================================================== --- Python-3.6.15.orig/Lib/py_compile.py +++ Python-3.6.15/Lib/py_compile.py @@ -137,6 +137,13 @@ def compile(file, cfile=None, dfile=None except FileExistsError: pass source_stats = loader.path_stats(file) + sde = os.environ.get('SOURCE_DATE_EPOCH') + if sde and source_stats['mtime'] > int(sde): + source_stats['mtime'] = int(sde) + try: + os.utime(file, (source_stats['mtime'], source_stats['mtime'])) + except PermissionError: + pass bytecode = importlib._bootstrap_external._code_to_bytecode( code, source_stats['mtime'], source_stats['size']) mode = importlib._bootstrap_external._calc_mode(file)