Skip to content

Commit

Permalink
Support for SOURCE_DATE_EPOCH (reproducible builds)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcinl committed Aug 25, 2023
1 parent 1c038e8 commit 7d06045
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions delocate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import stat
import subprocess
import time
import warnings
import zipfile
from datetime import datetime
Expand Down Expand Up @@ -844,12 +845,27 @@ def zip2dir(
os.utime(extracted_path, (modified_time, modified_time))


ZIP_TIMESTAMP_MIN = 315532800 # 1980-01-01 00:00:00 UTC
DateTuple = Tuple[int, int, int, int, int, int]


def get_zip_datetime(
date_time: Optional[DateTuple] = None,
) -> Optional[DateTuple]:
source_date_epoch = os.environ.get("SOURCE_DATE_EPOCH")
if source_date_epoch is not None:
timestamp = max(int(source_date_epoch), ZIP_TIMESTAMP_MIN)
date_time = time.gmtime(timestamp)[0:6]

Check warning on line 858 in delocate/tools.py

View check run for this annotation

Codecov / codecov/patch

delocate/tools.py#L857-L858

Added lines #L857 - L858 were not covered by tests
return date_time


def dir2zip(
in_dir: str | PathLike[str],
zip_fname: str | PathLike[str],
*,
compression: int = zipfile.ZIP_DEFLATED,
compress_level: int = -1,
date_time: Optional[DateTuple] = None,
) -> None:
"""Make a zip file `zip_fname` with contents of directory `in_dir`
Expand All @@ -868,6 +884,7 @@ def dir2zip(
compress_level : int, optional, keyword-only
The compression level used for this archive.
"""
date_time = get_zip_datetime(date_time)
with zipfile.ZipFile(
zip_fname, "w", compression=compression, compresslevel=compress_level
) as zip:
Expand All @@ -881,6 +898,8 @@ def dir2zip(
file_path = Path(root, file)
out_file_path = file_path.relative_to(in_dir)
zip_info = zipfile.ZipInfo.from_file(file_path, out_file_path)
if date_time is not None:
zip_info.date_time = date_time

Check warning on line 902 in delocate/tools.py

View check run for this annotation

Codecov / codecov/patch

delocate/tools.py#L902

Added line #L902 was not covered by tests
zip.writestr(
zip_info,
file_path.read_bytes(),
Expand Down

0 comments on commit 7d06045

Please sign in to comment.