From b63aca899862e7af7c29f2d32006e20b132a98cb Mon Sep 17 00:00:00 2001 From: Nicolas Belouin Date: Wed, 16 Oct 2024 16:06:00 +0200 Subject: [PATCH] Make output archives more reproducible This commit stores the mtime of the go.mod file as the mtime for all files in the produced archive. It also remove the optional timestamp in gz archive. This is enought to get reproducible output for tar.gz, tar.xz and tar.zstd. Signed-off-by: Nicolas Belouin --- go_modules | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/go_modules b/go_modules index 86b8a70..828b9a7 100755 --- a/go_modules +++ b/go_modules @@ -174,7 +174,7 @@ def extract(filename, outdir): os.chdir(outdir) try: - libarchive.extract_file(filename) + libarchive.extract_file(filename, libarchive.extract.EXTRACT_TIME) except libarchive.exception.ArchiveError as archive_error: log.error(archive_error) exit(1) @@ -299,16 +299,21 @@ def main(): os.chdir(go_mod_dir) vendor_dir = "vendor" - kwargs = {} + mtime = os.path.getmtime(go_mod_path) + log.debug(f"Set archive files times to {mtime}") + + options = [] + if archive_args["compression"] == "gzip": + options.append("!timestamp") if archive_args["level"]: - kwargs["options"] = f"compression-level={archive_args['level']}" + options.append(f"compression-level={archive_args['level']}") with libarchive.file_writer( vendor_tarfile, archive_args["format"], archive_args["compression"], - **kwargs, + options=",".join(options), ) as new_archive: - new_archive.add_files(vendor_dir) + new_archive.add_files(vendor_dir, mtime=mtime, ctime=mtime, atime=mtime) os.chdir(cwd)