From a0bfdd59e30a9d6ee2a515ca8df275134d09c830 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Fri, 1 Oct 2021 09:35:23 +0100 Subject: [PATCH] Push wheel timestamps to 1980 if SOURCE_DATE_EPOCH before that --- flit_core/flit_core/tests/test_wheel.py | 9 +++++++++ flit_core/flit_core/wheel.py | 12 +++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/flit_core/flit_core/tests/test_wheel.py b/flit_core/flit_core/tests/test_wheel.py index 2c422b33..7f10e32c 100644 --- a/flit_core/flit_core/tests/test_wheel.py +++ b/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 @@ -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) diff --git a/flit_core/flit_core/wheel.py b/flit_core/flit_core/wheel.py index 0579e03b..5e815996 100644 --- a/flit_core/flit_core/wheel.py +++ b/flit_core/flit_core/wheel.py @@ -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',