Skip to content

Commit

Permalink
fix SOURCE_DATE_EPOCH message spam (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering authored Oct 19, 2024
1 parent eac6e2c commit eb68b29
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections import defaultdict
from contextlib import contextmanager
from copy import copy
from functools import cached_property
from gzip import GzipFile
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -68,7 +69,7 @@ def build(

name = distribution_name(self._package.name)
target = target_dir / f"{name}-{self._meta.version}.tar.gz"
gz = GzipFile(target.as_posix(), mode="wb", mtime=self._get_archive_mtime())
gz = GzipFile(target.as_posix(), mode="wb", mtime=self._archive_mtime)
tar = tarfile.TarFile(
target.as_posix(), mode="w", fileobj=gz, format=tarfile.PAX_FORMAT
)
Expand Down Expand Up @@ -414,13 +415,13 @@ def clean_tarinfo(self, tar_info: TarInfo) -> TarInfo:
ti.gid = 0
ti.uname = ""
ti.gname = ""
ti.mtime = self._get_archive_mtime()
ti.mtime = self._archive_mtime
ti.mode = normalize_file_permissions(ti.mode)

return ti

@staticmethod
def _get_archive_mtime() -> int:
@cached_property
def _archive_mtime(self) -> int:
if source_date_epoch := os.getenv("SOURCE_DATE_EPOCH"):
try:
return int(source_date_epoch)
Expand All @@ -430,5 +431,5 @@ def _get_archive_mtime() -> int:
" using mtime=0"
)
return 0
logger.info("SOURCE_DATE_EPOCH environment variable is not set, using mtime=0")
logger.debug("SOURCE_DATE_EPOCH environment variable is not set, using mtime=0")
return 0
11 changes: 6 additions & 5 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import zipfile

from base64 import urlsafe_b64encode
from functools import cached_property
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -449,7 +450,7 @@ def _add_file(
) -> None:
# We always want to have /-separated paths in the zip file and in RECORD
rel_path_name = rel_path.as_posix()
zinfo = zipfile.ZipInfo(rel_path_name, self._get_zipfile_date_time())
zinfo = zipfile.ZipInfo(rel_path_name, self._zipfile_date_time)

# Normalize permission bits to either 755 (executable) or 644
st_mode = full_path.stat().st_mode
Expand Down Expand Up @@ -482,7 +483,7 @@ def _write_to_zip(
sio = StringIO()
yield sio

date_time = self._get_zipfile_date_time()
date_time = self._zipfile_date_time
zi = zipfile.ZipInfo(rel_path, date_time)
zi.external_attr = (0o644 & 0xFFFF) << 16 # Unix attributes
b = sio.getvalue().encode("utf-8")
Expand All @@ -492,8 +493,8 @@ def _write_to_zip(
wheel.writestr(zi, b, compress_type=zipfile.ZIP_DEFLATED)
self._records.append((rel_path, hash_digest, len(b)))

@staticmethod
def _get_zipfile_date_time() -> ZipInfoTimestamp:
@cached_property
def _zipfile_date_time(self) -> ZipInfoTimestamp:
import time

# The default is a fixed timestamp rather than the current time, so
Expand All @@ -510,7 +511,7 @@ def _get_zipfile_date_time() -> ZipInfoTimestamp:
)
return default
except KeyError:
logger.info(
logger.debug(
"SOURCE_DATE_EPOCH environment variable not set,"
" setting zipinfo date to default=%s",
default,
Expand Down
18 changes: 11 additions & 7 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ast
import gzip
import hashlib
import logging
import shutil
import tarfile

Expand Down Expand Up @@ -706,10 +707,9 @@ def test_split_source() -> None:
assert "" in ns["package_dir"] and "module_b" in ns["package_dir"]


def test_sdist_members_mtime_default(caplog: LogCaptureFixture) -> None:
import logging

caplog.set_level(logging.INFO)
@pytest.mark.parametrize("log_level", [logging.INFO, logging.DEBUG])
def test_sdist_members_mtime_default(caplog: LogCaptureFixture, log_level: int) -> None:
caplog.set_level(log_level)
poetry = Factory().create_poetry(project("module1"))

builder = SdistBuilder(poetry)
Expand All @@ -723,9 +723,13 @@ def test_sdist_members_mtime_default(caplog: LogCaptureFixture) -> None:
for tarinfo in tar.getmembers():
assert tarinfo.mtime == 0

assert (
"SOURCE_DATE_EPOCH environment variable is not set," " using mtime=0"
) in caplog.messages
source_data_epoch_message = (
"SOURCE_DATE_EPOCH environment variable is not set, using mtime=0"
)
if log_level == logging.DEBUG:
assert source_data_epoch_message in caplog.messages
else:
assert source_data_epoch_message not in caplog.messages


def test_sdist_mtime_set_from_envvar(monkeypatch: MonkeyPatch) -> None:
Expand Down
18 changes: 12 additions & 6 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import importlib.machinery
import logging
import os
import re
import shutil
Expand Down Expand Up @@ -495,10 +496,11 @@ def test_generated_script_file(tmp_path: Path) -> None:
assert "generated_script_file-0.1.data/scripts/script.sh" in z.namelist()


def test_dist_info_date_time_default_value(caplog: LogCaptureFixture) -> None:
import logging

caplog.set_level(logging.INFO)
@pytest.mark.parametrize("log_level", [logging.INFO, logging.DEBUG])
def test_dist_info_date_time_default_value(
caplog: LogCaptureFixture, log_level: int
) -> None:
caplog.set_level(log_level)
module_path = fixtures_dir / "complete"
WheelBuilder.make(Factory().create_poetry(module_path))

Expand All @@ -511,10 +513,14 @@ def test_dist_info_date_time_default_value(caplog: LogCaptureFixture) -> None:
z.getinfo("my_package-1.2.3.dist-info/WHEEL").date_time == default_date_time
)

assert (
source_data_epoch_message = (
"SOURCE_DATE_EPOCH environment variable not set,"
f" setting zipinfo date to default={default_date_time}"
) in caplog.messages
)
if log_level == logging.DEBUG:
assert source_data_epoch_message in caplog.messages
else:
assert source_data_epoch_message not in caplog.messages


def test_dist_info_date_time_value_from_envvar(monkeypatch: MonkeyPatch) -> None:
Expand Down

0 comments on commit eb68b29

Please sign in to comment.