Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Lib/distutils/archive_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,25 @@ def _set_uid_gid(tarinfo):
tarinfo.uname = owner
return tarinfo

_filter = _set_uid_gid

# SOURCE_DATE EPOCH is defined there
# https://reproducible-builds.org/specs/source-date-epoch/
# we are at least sure that when it is set no timestamp can be later than
# this.
if (sde:= os.environ.get('SOURCE_DATE_EPOCH')):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (sde:= os.environ.get('SOURCE_DATE_EPOCH')):
if (sde := os.environ.get('SOURCE_DATE_EPOCH')):

timestamp = int(sde)

def _respect_SOURCE_DATE_EPOCH(tarinfo):
tarinfo.mtime = min(tarinfo.mtime, timestamp)
return tarinfo

_filter = lambda x: _respect_SOURCE_DATE_EPOCH(_set_uid_gid(x))

if not dry_run:
tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
try:
tar.add(base_dir, filter=_set_uid_gid)
tar.add(base_dir, filter=_filter)
finally:
tar.close()

Expand Down
18 changes: 18 additions & 0 deletions Lib/distutils/tests/test_archive_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,24 @@ def test_make_archive_tar(self):
self.assertEqual(os.path.basename(res), 'archive.tar')
self.assertEqual(self._tarinfo(res), self._created_files)

def test_make_archive_tar_source_date_epoch(self):
ORIGINAL_SDE = os.environ.get('SOURCE_DATE_EPOCH')
try:
os.environ['SOURCE_DATE_EPOCH'] = '1337'
base_dir = self._create_files()
base_name = os.path.join(self.mkdtemp() , 'archive')
res = make_archive(base_name, 'tar', base_dir, 'dist')

archive = tarfile.open(res,mode='r')
for item in archive:
self.assertLessEqual(item.mtime, 1337)
finally:
archive.close()
if ORIGINAL_SDE is None:
del os.environ['SOURCE_DATE_EPOCH']
else:
os.environ['SOURCE_DATE_EPOCH'] = ORIGINAL_SDE
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could make all this setup/teardown simpler by using one of the helpers in test.support or maybe unittest.mock.patch?


@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
def test_make_archive_gztar(self):
base_dir = self._create_files()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Partial support for ``SOURCE_DATE_EPOCH`` environment variable for sdists
has been added in distutils. When the ``SOURCE_DATE_EPOCH`` environment
variable is set, the ``mtime`` of the files in an sdist tar archive will not
be later than ``SOURCE_DATE_EPOCH``. This is a firs step to simplify getting
byte identical reproducibility of source dists.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Can you explain why this PR is a first step / partial support?

  2. distutils is mostly invisible these days, importated and extended by setuptools. Do the changes impact setuptools sdists?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There a bunch of other things that add variability to the end artifact. For example ZIP using current datetime in the header, which also needs to be fixed, making sure directory/files are traveres in the same order... etc.

I try to keep things minimal as PRs here tends to sits for years, and the bigger the longer.

2. distutils is mostly invisible these days, imported and extended by setuptools. Do the changes impact setuptools sdists?

Yes, it extends and call it, so if the result of this is not reproducible then it needs to be monkeypatch in setuptools (see pypa/setuptools#2136 which I need to finish) Basically 1/2 of the upstream patch is copy-pasting code from distutils to tweak the internal behavior.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds OK then!