Skip to content

Commit

Permalink
feat: support specifying zipinfo date via env var (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Jan 30, 2023
1 parent 20c5ef4 commit cd1fc91
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- flake8-bugbear

- repo: https://github.com/PyCQA/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
exclude: ^.*/?setup\.py$
Expand Down
4 changes: 4 additions & 0 deletions docs/build_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,9 @@ For example, you can supply these options with [build]:
python -m build --sdist --wheel --outdir dist/ --config-setting="--python-tag=cp37" --config-setting="--plat-name=win_amd64"
```

## Environment variables

- `SOURCE_DATE_EPOCH`: Set the timestamp(seconds) of the zipinfo in the wheel for reproducible builds. The default date is 2016/01/01.

[build]: https://pypi.org/project/build
[pdm]: https://pypi.org/project/pdm
14 changes: 11 additions & 3 deletions src/pdm/backend/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import stat
import sys
import tempfile
import time
import zipfile
from base64 import urlsafe_b64encode
from pathlib import Path
Expand Down Expand Up @@ -43,7 +44,14 @@

PY_LIMITED_API_PATTERN = r"cp3\d{1,2}"
# Fix the date time for reproducible builds
ZIPINFO_DEFAULT_DATE_TIME = (2016, 1, 1, 0, 0, 0)
try:
_env_date = time.gmtime(int(os.environ["SOURCE_DATE_EPOCH"]))[:6]
except (ValueError, KeyError):
ZIPINFO_DATE_TIME = (2016, 1, 1, 0, 0, 0)
else:
if _env_date[0] < 1980:
raise ValueError("zipinfo date can't be earlier than 1980")
ZIPINFO_DATE_TIME = _env_date


def _open_for_write(path: str | Path) -> IO[str]:
Expand Down Expand Up @@ -219,7 +227,7 @@ def _add_file_to_zip(
) -> RecordEntry:

self._show_add_file(rel_path, full_path)
zi = zipfile.ZipInfo(rel_path, ZIPINFO_DEFAULT_DATE_TIME)
zi = zipfile.ZipInfo(rel_path, ZIPINFO_DATE_TIME)
st_mode = os.stat(full_path).st_mode
zi.external_attr = (st_mode & 0xFFFF) << 16 # Unix attributes

Expand All @@ -244,7 +252,7 @@ def _write_zip_info(
return urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")

def _write_record(self, zf: zipfile.ZipFile, records: list[RecordEntry]) -> None:
zi = zipfile.ZipInfo(f"{self.dist_info_name}/RECORD", ZIPINFO_DEFAULT_DATE_TIME)
zi = zipfile.ZipInfo(f"{self.dist_info_name}/RECORD", ZIPINFO_DATE_TIME)
buffer = io.BytesIO()
text_buffer = io.TextIOWrapper(buffer, encoding="utf-8", newline="")

Expand Down

0 comments on commit cd1fc91

Please sign in to comment.