3131import tarfile
3232import tempfile
3333import textwrap
34- import time
3534import typing
3635import warnings
3736
@@ -867,6 +866,7 @@ def sdist(self, directory: Path) -> pathlib.Path:
867866 meson_dist_name = f'{ self ._meson_name } -{ self ._meson_version } '
868867 meson_dist_path = pathlib .Path (self ._build_dir , 'meson-dist' , f'{ meson_dist_name } .tar.gz' )
869868 sdist_path = pathlib .Path (directory , f'{ dist_name } .tar.gz' )
869+ pyproject_toml_mtime = 0
870870
871871 with tarfile .open (meson_dist_path , 'r:gz' ) as meson_dist , mesonpy ._util .create_targz (sdist_path ) as sdist :
872872 for member in meson_dist .getmembers ():
@@ -877,6 +877,9 @@ def sdist(self, directory: Path) -> pathlib.Path:
877877 stem = member .name .split ('/' , 1 )[1 ]
878878 member .name = '/' .join ((dist_name , stem ))
879879
880+ if stem == 'pyproject.toml' :
881+ pyproject_toml_mtime = member .mtime
882+
880883 # Reset owner and group to root:root. This mimics what
881884 # 'git archive' does and makes the sdist reproducible upon
882885 # being built by different users.
@@ -889,7 +892,23 @@ def sdist(self, directory: Path) -> pathlib.Path:
889892 member = tarfile .TarInfo (f'{ dist_name } /PKG-INFO' )
890893 member .uid = member .gid = 0
891894 member .uname = member .gname = 'root'
892- member .mtime = time .time ()
895+
896+ # Set the 'PKG-INFO' modification time to the modification time of
897+ # 'pyproject.toml' in the archive generated by 'meson dist'. In
898+ # turn this is the last commit time, unless touched by a dist
899+ # script. This makes the sdist reproducible upon being built at
900+ # different times, when dist scripts are not used, which should be
901+ # the majority of cases.
902+ #
903+ # Note that support for dynamic version in project metadata allows
904+ # the version to depend on the build time. Therefore, setting the
905+ # 'PKG-INFO' modification time to the 'pyproject.toml'
906+ # modification time can be seen as not strictly correct. However,
907+ # the sdist standard does not dictate which modification time to
908+ # use for 'PKG-INFO'. This choice allows to make the sdist
909+ # byte-for-byte reproducible in the most common case.
910+ member .mtime = pyproject_toml_mtime
911+
893912 metadata = bytes (self ._metadata .as_rfc822 ())
894913 member .size = len (metadata )
895914 sdist .addfile (member , io .BytesIO (metadata ))
0 commit comments