Skip to content

Commit

Permalink
Push wheel timestamps to 1980 if SOURCE_DATE_EPOCH before that
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Oct 1, 2021
1 parent 9335bfb commit a0bfdd5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions flit_core/flit_core/tests/test_wheel.py
@@ -1,4 +1,5 @@
from pathlib import Path
from zipfile import ZipFile

from testpath import assert_isfile

Expand All @@ -10,3 +11,11 @@ def test_licenses_dir(tmp_path):
# Smoketest for https://github.com/takluyver/flit/issues/399
info = make_wheel_in(samples_dir / 'inclusion' / 'pyproject.toml', tmp_path)
assert_isfile(info.file)

def test_zero_timestamp(tmp_path, monkeypatch):
monkeypatch.setenv('SOURCE_DATE_EPOCH', '0')
info = make_wheel_in(samples_dir / 'pep621' / 'pyproject.toml', tmp_path)
assert_isfile(info.file)
# Minimum value for zip timestamps is 1980-1-1
with ZipFile(info.file, 'r') as zf:
assert zf.getinfo('module1a.py').date_time == (1980, 1, 1, 0, 0, 0)
12 changes: 9 additions & 3 deletions flit_core/flit_core/wheel.py
Expand Up @@ -50,13 +50,19 @@ def __init__(self, directory, module, metadata, entrypoints, target_fp):
# If SOURCE_DATE_EPOCH is set (e.g. by Debian), it's used for
# timestamps inside the zip file.
d = datetime.utcfromtimestamp(int(os.environ['SOURCE_DATE_EPOCH']))
log.info("Zip timestamps will be from SOURCE_DATE_EPOCH: %s", d)
# zipfile expects a 6-tuple, not a datetime object
self.source_time_stamp = (d.year, d.month, d.day, d.hour, d.minute, d.second)
except (KeyError, ValueError):
# Otherwise, we'll use the mtime of files, and generated files will
# default to 2016-1-1 00:00:00
self.source_time_stamp = None
else:
if d.year >= 1980:
log.info("Zip timestamps will be from SOURCE_DATE_EPOCH: %s", d)
# zipfile expects a 6-tuple, not a datetime object
self.source_time_stamp = (d.year, d.month, d.day, d.hour, d.minute, d.second)
else:
log.info("SOURCE_DATE_EPOCH is below the minimum for zip file timestamps")
log.info("Zip timestamps will be 1980-01-01 00:00:00")
self.source_time_stamp = (1980, 1, 1, 0, 0, 0)

# Open the zip file ready to write
self.wheel_zip = zipfile.ZipFile(target_fp, 'w',
Expand Down

0 comments on commit a0bfdd5

Please sign in to comment.