Skip to content

Commit fad7fd3

Browse files
Bernhard M. Wiedemannmcepl
Bernhard M. Wiedemann
authored andcommitted
allow for reproducible builds of python packages
See https://reproducible-builds.org/ for why this is good idea 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. Original version which doesn't force hashed *.pyc files Code is originally from gh#python#296 (never released in this form). Patch: 0001-allow-for-reproducible-builds-of-python-packages.patch
1 parent 8238f61 commit fad7fd3

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Lib/py_compile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1):
137137
except FileExistsError:
138138
pass
139139
source_stats = loader.path_stats(file)
140+
sde = os.environ.get('SOURCE_DATE_EPOCH')
141+
if sde and source_stats['mtime'] > int(sde):
142+
source_stats['mtime'] = int(sde)
143+
try:
144+
os.utime(file, (source_stats['mtime'], source_stats['mtime']))
145+
except PermissionError:
146+
pass
140147
bytecode = importlib._bootstrap_external._code_to_bytecode(
141148
code, source_stats['mtime'], source_stats['size'])
142149
mode = importlib._bootstrap_external._calc_mode(file)

0 commit comments

Comments
 (0)