Skip to content

Commit

Permalink
Respect SOURCE_DATE_EPOCH
Browse files Browse the repository at this point in the history
  • Loading branch information
jvolkman committed Apr 18, 2023
1 parent 12a246b commit 260e04d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/repairwheel/repair.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import argparse
import datetime
import os
import shutil
import sys
import tempfile
Expand Down Expand Up @@ -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 [])]
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions src/repairwheel/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand Down

0 comments on commit 260e04d

Please sign in to comment.