diff --git a/src/repairwheel/repair.py b/src/repairwheel/repair.py index 6fb02ff..730e544 100644 --- a/src/repairwheel/repair.py +++ b/src/repairwheel/repair.py @@ -1,4 +1,6 @@ import argparse +import datetime +import os import shutil import sys import tempfile @@ -63,6 +65,15 @@ def main(): parser = make_parser() args = parser.parse_args() + if "SOURCE_DATE_EPOCH" in os.environ: + try: + epoch = int(os.environ["SOURCE_DATE_EPOCH"]) + mtime = datetime.datetime.utcfromtimestamp(epoch) + except ValueError: + fatal(f"SOURCE_DATE_EPOCH value cannot be parsed as a number: {os.environ['SOURCE_DATE_EPOCH']}") + else: + mtime = None + original_wheel: Path = args.wheel.resolve() out_dir: Path = args.output_dir.resolve() lib_path: List[Path] = [lp.resolve() for lp in (args.lib_dir or [])] @@ -92,7 +103,7 @@ def main(): patched_wheel = find_written_wheel(temp_wheel_dir) out_dir.mkdir(parents=True, exist_ok=True) - out_wheel = write_canonical_wheel(original_wheel, patched_wheel, out_dir) + out_wheel = write_canonical_wheel(original_wheel, patched_wheel, out_dir, mtime=mtime) print("Wrote", out_wheel) diff --git a/src/repairwheel/wheel.py b/src/repairwheel/wheel.py index 0ded4c6..ebe3911 100644 --- a/src/repairwheel/wheel.py +++ b/src/repairwheel/wheel.py @@ -7,7 +7,7 @@ from datetime import datetime from io import StringIO from pathlib import Path -from typing import BinaryIO, List, Tuple +from typing import BinaryIO, List, Optional, Tuple from zipfile import ZipFile, ZipInfo from packaging.utils import parse_wheel_filename @@ -68,7 +68,7 @@ def write_canonical_wheel( out_dir: Path, default_file_mode: int = 0o664, default_dir_mode: int = 0o775, - mtime: datetime = DEFAULT_MTIME, + mtime: Optional[datetime] = None, compression: int = zipfile.ZIP_DEFLATED, ) -> Path: """This function rewrites a wheel in a canonical form. @@ -80,6 +80,9 @@ def write_canonical_wheel( Because Windows doesn't support posix file modes, we use corresponding modes in the original file if they exist. Else we use the default mode. """ + if mtime is None: + mtime = DEFAULT_MTIME + out_wheel = out_dir / patched_wheel.name dist_name, dist_version, _, _ = parse_wheel_filename(patched_wheel.name)