From dfa7838ffde97babac9348c7f86905e79a513c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Sat, 7 Nov 2026 16:57:46 +0100 Subject: [PATCH 1/3] Unify parsing of changelog entry timestamps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Detection of extended timestamps didn't recognize zero-padded days of month, so add_changelog_entry() would create changelog entries without a item in such a case. Therefore we unify the code to use the same regex for extended_timestamp & sanitize_day_of_month_padding. Signed-off-by: Dan Čermák Assisted-By: Claude Opus 5.5 --- specfile/changelog.py | 83 ++++++++++++++++-------------- tests/integration/test_specfile.py | 33 ++++++++++++ tests/unit/test_changelog.py | 19 +++++++ 3 files changed, 95 insertions(+), 40 deletions(-) diff --git a/specfile/changelog.py b/specfile/changelog.py index 0a984fa..c909c0f 100644 --- a/specfile/changelog.py +++ b/specfile/changelog.py @@ -35,6 +35,35 @@ "Dec", ) +# matches both the basic ("Tue May 4 2021") and the extended +# ("Mon Oct 18 12:34:45 CEST 2021") timestamp format +_TIMESTAMP_RE = re.compile( + rf""" + (?:{'|'.join(WEEKDAYS)}) # weekday + [ ] + (?P{'|'.join(MONTHS)}) # month + [ ] + (?P[ ]*) # optional whitespace padding + (?P0)? # optional zero padding + (?P[12][0-9]|3[01]|[1-9]) # day of month + (?: # optional time and timezone + [ ] + (?P[01][0-9]|2[0-3]) # hour + : + (?P[0-5][0-9]) # minute + : + (?P[0-5][0-9]) # second + [ ] + (?P\S+) # timezone + )? + [ ] + (?P[0-9]{{4}}) # year + (?!\S) + """, + re.VERBOSE, +) + + _OPENSUSE_CHANGELOG_SEPARATOR = 67 * "-" @@ -138,33 +167,22 @@ def evr(self) -> Optional[EVR]: return evr + @property + def _timestamp_line(self) -> str: + """Last line of the entry header without the leading _*_.""" + # the first line of openSUSE-style headers is the separator + line = self.header.split("\n")[-1] + return line[1:].lstrip() if line.startswith("*") else line + + @property + def _timestamp_match(self) -> Optional["re.Match[str]"]: + return _TIMESTAMP_RE.match(self._timestamp_line) + @property def extended_timestamp(self) -> bool: """Whether the timestamp present in the entry header is extended (date and time).""" - weekdays = "|".join(WEEKDAYS) - months = "|".join(MONTHS) - m = re.search( - rf""" - ({weekdays}) # weekday - [ ] - ({months}) # month - [ ]+ - ([12]?\d|3[01]) # day of month - [ ] - ([01]\d|2[0-3]) # hour - : - ([0-5]\d) # minute - : - ([0-5]\d) # second - [ ] - \S+ # timezone - [ ] - \d{{4}} # year - """, - self.header, - re.VERBOSE, - ) - return m is not None + m = self._timestamp_match + return m is not None and m.group("hour") is not None @property def sanitized_day_of_month_padding(self) -> str: @@ -172,22 +190,7 @@ def sanitized_day_of_month_padding(self) -> str: Padding of day of month in the entry header timestamp. Nonsensical padding (i.e. multiple spaces before double-digit day of month) is corrected. """ - weekdays = "|".join(WEEKDAYS) - months = "|".join(MONTHS) - - m = re.search( - rf""" - ({weekdays}) # weekday - [ ] - ({months}) # month - [ ] - (?P[ ]*) # optional whitespace padding - (?P0)? # optional zero-padding - (?P[12]\d|3[01]|[1-9]) # day of month (1-31) - """, - self.header, - re.VERBOSE, - ) + m = self._timestamp_match if not m: return "" # remove extra padding for double-digit day numbers diff --git a/tests/integration/test_specfile.py b/tests/integration/test_specfile.py index f92f751..2b1845d 100644 --- a/tests/integration/test_specfile.py +++ b/tests/integration/test_specfile.py @@ -3,6 +3,7 @@ import copy import datetime +import types import pytest import rpm @@ -227,6 +228,38 @@ def test_add_changelog_entry( assert sections.changelog[: len(result)] == result +def test_add_changelog_entry_keeps_extended_timestamp( + specfile_factory, spec_minimal, monkeypatch +): + class FrozenDatetime(datetime.datetime): + @classmethod + def now(cls, tz=None): + return cls(2021, 10, 5, 12, 34, 56, tzinfo=datetime.timezone.utc) + + def astimezone(self, tz=None): + # make the result independent of the local timezone + return self + + monkeypatch.setattr( + specfile.specfile, + "datetime", + types.SimpleNamespace(datetime=FrozenDatetime, timezone=datetime.timezone), + ) + spec = specfile_factory(spec_minimal) + with spec.sections() as sections: + sections.changelog[:] = [ + "* Mon Oct 04 12:34:45 CEST 2021 Nikola Forró - 0.1-1", + "- first version", + ] + spec.add_changelog_entry("- second version", author="Nikola Forró") + with spec.sections() as sections: + # slicing creates ChangeLog objects :-/ + assert (sections.changelog[0], sections.changelog[1]) == ( + "* Tue Oct 05 12:34:56 UTC 2021 Nikola Forró - 0.1-1", + "- second version", + ) + + def test_add_changelog_entry_ignores_invalid_padding(specfile_factory, spec_minimal): """Test that add_changelog_entry ignores entries with invalid day padding (issue #216).""" spec = specfile_factory(spec_minimal) diff --git a/tests/unit/test_changelog.py b/tests/unit/test_changelog.py index 825c9a8..acb3687 100644 --- a/tests/unit/test_changelog.py +++ b/tests/unit/test_changelog.py @@ -74,6 +74,25 @@ def test_entry_evr(header, evr: Optional[EVR]): "* Mon Oct 18 12:34:45 CEST 2021 Nikola Forró - 0.2-1", True, ), + ( + "* Mon Oct 04 12:34:45 CEST 2021 Nikola Forró - 0.2-1", + True, + ), + ( + "*\tMon Oct 18 12:34:45 CEST 2021 Nikola Forró - 0.2-1", + True, + ), + # RPM rejects anything preceding the timestamp + ( + "* Some Text Mon Oct 18 12:34:45 CEST 2021 Nikola Forró ", + False, + ), + ( + _OPENSUSE_CHANGELOG_SEPARATOR + + "\n" + + "Tue Dec 17 14:21:37 UTC 2024 - Dan Čermák ", + True, + ), ], ) def test_entry_has_extended_timestamp(header, extended): From e190e9ed6049442bcc2467dec11871b4afc45b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Sat, 7 Nov 2026 16:58:40 +0100 Subject: [PATCH 2/3] Add author and timestamp properties to ChangelogEntry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These properties are required to convert Fedora to openSUSE style changelogs for packit. Timezones are resolved the same way RPM resolves them, so that timestamps match RPMTAG_CHANGELOGTIME: names missing in the timezone database (e.g. CEST) mean UTC and DST is not applied Signed-off-by: Dan Čermák Assisted-By: Claude Opus 5.5 --- specfile/changelog.py | 102 +++++++++++++++++++- tests/constants.py | 8 ++ tests/integration/test_specfile.py | 40 ++++++++ tests/unit/test_changelog.py | 143 +++++++++++++++++++++++++++++ 4 files changed, 291 insertions(+), 2 deletions(-) diff --git a/specfile/changelog.py b/specfile/changelog.py index c909c0f..b1ee22e 100644 --- a/specfile/changelog.py +++ b/specfile/changelog.py @@ -64,6 +64,42 @@ ) +def _get_tzinfo(name: str, timestamp: datetime.datetime) -> Optional[datetime.tzinfo]: + """Resolves a timezone name as RPM does it: pass the name to libc, which + looks it up in the system timezone database. Unknown names (e.g. "CEST", + i.e. an abbreviation instead of a timezone identifier) are treated as UTC + and DST is never applied, because RPM calls `mktime()` with `tm_isdst` set + to 0. + + Args: + name: Timezone name + timestamp: Naive timestamp the timezone applies to, the standard offset + of some timezones changed over time. + + Returns: + Matching `tzinfo` object or None if the name can't be resolved reliably. + + """ + try: + from zoneinfo import ZoneInfo + except ImportError: + # Python < 3.9, better no timestamp than a wrong one + if name in ("UTC", "GMT"): + return datetime.timezone(datetime.timedelta(), name) + return None + try: + aware = timestamp.replace(tzinfo=ZoneInfo(name)) + except (KeyError, ValueError, OSError): + # ZoneInfoNotFoundError is a subclass of KeyError, ValueError is raised + # for invalid keys (e.g. containing "..") + return datetime.timezone.utc + # utcoffset() and dst() are Optional as far as type checkers are concerned + offset = (aware.utcoffset() or datetime.timedelta()) - ( + aware.dst() or datetime.timedelta() + ) + return datetime.timezone(offset, name) + + _OPENSUSE_CHANGELOG_SEPARATOR = 67 * "-" @@ -100,7 +136,7 @@ class ChangelogEntry: \d{4} # year \s+ # whitespace .+ # author - \s+ # preceding whitespace + (?P\s+) # preceding whitespace ((?P\[)|(?P\())? # optional opening bracket (?P\S+?) # EVR (?(sb)\]|(?(rb)\))) # matching closing bracket @@ -149,7 +185,12 @@ def __repr__(self) -> str: @property def evr(self) -> Optional[EVR]: - """EVR (epoch, version, release) of the entry.""" + """ + EVR (epoch, version, release) of the entry. + + Parsing is best effort, RPM doesn't impose any format on the text + following the timestamp. + """ m = self._EVR_RE.match(self.header) if not m: @@ -205,6 +246,63 @@ def style(self) -> ChangelogStyle: return ChangelogStyle.openSUSE return ChangelogStyle.standard + @property + def author(self) -> Optional[str]: + """ + Author of the entry, usually including their email. + + Parsing is best effort, see `evr`. The last word of an entry + with neither an email nor an EVR is taken for an EVR. + """ + line = self._timestamp_line + timestamp = _TIMESTAMP_RE.match(line) + if timestamp is None: + return None + evr = self._EVR_RE.match(line) if self.evr is not None else None + # strips the "-" separating the author from the timestamp (openSUSE style) + # or from the EVR + return line[timestamp.end() : evr.start("pre_wsp") if evr else None].strip( + " \t-" + ) + + @property + def timestamp(self) -> Optional[Union[datetime.datetime, datetime.date]]: + """Timestamp of the entry, `date` for the basic format ("Tue May 4 + 2021"), timezone-aware `datetime` for the extended one ("Mon Oct 18 + 12:34:45 CET 2021"). `None` if it can't be parsed. + + Timezones are resolved similarly to RPM: + - names missing in the system timezone database (e.g. "CEST" or "+03") + mean UTC + - DST is never applied + - CET is UTC+1 even in summer + + Unlike RPM, POSIX TZ strings (e.g. "UTC+2") are not supported and mean + UTC as well + + Without `zoneinfo` (Python < 3.9) only "UTC" and "GMT" are resolved! + + """ + m = self._timestamp_match + if m is None: + return None + try: + timestamp = datetime.datetime( + int(m.group("year")), + MONTHS.index(m.group("month")) + 1, + int(m.group("day")), + int(m.group("hour") or 0), + int(m.group("minute") or 0), + int(m.group("second") or 0), + ) + except ValueError: + # non-existent date, e.g. Feb 31 + return None + if m.group("hour") is None: + return timestamp.date() + tzinfo = _get_tzinfo(m.group("timezone"), timestamp) + return timestamp.replace(tzinfo=tzinfo) if tzinfo else None + @classmethod def assemble( cls, diff --git a/tests/constants.py b/tests/constants.py index 47419d0..de2744f 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -1,8 +1,16 @@ # Copyright Contributors to the Packit project. # SPDX-License-Identifier: MIT +import datetime from pathlib import Path +from specfile.changelog import ChangelogEntry + +# requires zoneinfo (Python >= 3.9) and the system timezone database +HAS_TZDB = ChangelogEntry( + "* Mon Jan 01 00:00:00 EST 2024", [] +).timestamp == datetime.datetime(2024, 1, 1, 5, tzinfo=datetime.timezone.utc) + TESTS_DIR = Path(__file__).parent DATA_DIR = TESTS_DIR / "data" SPEC_MINIMAL = DATA_DIR / "spec_minimal" diff --git a/tests/integration/test_specfile.py b/tests/integration/test_specfile.py index 2b1845d..bb46a4e 100644 --- a/tests/integration/test_specfile.py +++ b/tests/integration/test_specfile.py @@ -15,6 +15,7 @@ from specfile.prep import AutopatchMacro, AutosetupMacro, PatchMacro, SetupMacro from specfile.sections import Section from specfile.specfile import Specfile, SpecParser +from tests.constants import HAS_TZDB def test_parse(spec_multiple_sources): @@ -287,6 +288,45 @@ def test_add_changelog_entry_ignores_invalid_padding(specfile_factory, spec_mini assert "Jan 5" not in new_entry +@pytest.mark.skipif(not HAS_TZDB, reason="timezone database not available") +def test_changelog_timestamps_match_rpm(spec_minimal): + # RPM requires the newest entry first + timestamps = [ + "Fri Dec 20 08:15:00 CET 2024", + "Wed Nov 20 23:45:59 BOGUS 2024", + "Mon Oct 21 12:34:45 CEST 2024", + "Wed Sep 18 06:05:04 PST8PDT 2024", + "Thu Aug 15 2024", + "Tue Jul 16 12:00:00 CET 2024", + # printed by `date +%Z` for timezones without an abbreviation + "Fri May 10 09:25:30 +03 2024", + "Wed Jan 03 01:02:03 UTC 2024", + ] + spec = Specfile( + spec_minimal, + macros=[("_changelog_trimage", "0"), ("_changelog_trimtime", "0")], + ) + with spec.sections() as sections: + sections.changelog[:] = [ + line + for t in timestamps + for line in (f"* {t} Nikola Forró ", "- entry", "") + ] + + def to_unix(timestamp): + if not isinstance(timestamp, datetime.datetime): + # RPM represents dates as noon UTC + timestamp = datetime.datetime.combine( + timestamp, datetime.time(12), datetime.timezone.utc + ) + return int(timestamp.timestamp()) + + with spec.changelog() as changelog: + assert [to_unix(e.timestamp) for e in reversed(changelog)] == list( + spec.rpm_spec.sourceHeader[rpm.RPMTAG_CHANGELOGTIME] + ) + + @pytest.mark.parametrize( "version, release", [ diff --git a/tests/unit/test_changelog.py b/tests/unit/test_changelog.py index acb3687..5713a04 100644 --- a/tests/unit/test_changelog.py +++ b/tests/unit/test_changelog.py @@ -15,6 +15,11 @@ ) from specfile.sections import Section from specfile.utils import EVR +from tests.constants import HAS_TZDB + +UTC = datetime.timezone.utc + +needs_tzdb = pytest.mark.skipif(not HAS_TZDB, reason="timezone database not available") @pytest.mark.parametrize( @@ -414,6 +419,144 @@ def test_create_opensuse_changelog_assemble( ) +@pytest.mark.parametrize( + "header, author", + [ + ( + "* Thu Jan 04 2007 Michael Schwendt ", + "Michael Schwendt ", + ), + ( + "* Fri Jul 26 2024 Miroslav Suchý - ss981107-67", + "Miroslav Suchý ", + ), + ( + "* Mon Jul 13 2020 Tom Stellard 4.0-0.4.pre2", + "Tom Stellard ", + ), + ( + "* Mon Jan 03 2022 Fedora Kernel Team " + "[5.16-0.rc8.55]", + "Fedora Kernel Team ", + ), + ( + "* Wed Jan 23 2002 Karsten Hopp (4.6-1)", + "Karsten Hopp ", + ), + ( + "* Thu Apr 9 2015 Jeffrey C. Ollie - 13.3.2-1:", + "Jeffrey C. Ollie ", + ), + ( + "* Thu Feb 04 2016 Marcin Zajaczkowski - 1:0.9.10-6", + "Marcin Zajaczkowski ", + ), + ( + "* Mon Oct 18 12:34:45 CEST 2021 Nikola Forró - 0.2-1", + "Nikola Forró ", + ), + ( + _OPENSUSE_CHANGELOG_SEPARATOR + + "\n" + + "Tue Dec 17 14:21:37 UTC 2024 - Dan Čermák ", + "Dan Čermák ", + ), + ( + _OPENSUSE_CHANGELOG_SEPARATOR + + "\n" + + "Tue Dec 17 14:21:37 UTC 2024 - Dan Čermák", + "Dan Čermák", + ), + ("* Nikola Forró ", None), + ], +) +def test_entry_author(header, author): + assert ChangelogEntry(header, [""]).author == author + + +@pytest.mark.parametrize( + "header, timestamp", + [ + ( + "* Tue May 4 2021 Nikola Forró - 0.1-1", + datetime.date(2021, 5, 4), + ), + ( + "* Mon Oct 18 12:34:45 UTC 2021 Nikola Forró - 0.2-1", + datetime.datetime(2021, 10, 18, 12, 34, 45, tzinfo=UTC), + ), + # not in the timezone database, UTC like in RPM + pytest.param( + "* Mon Oct 18 12:34:45 CEST 2021 Nikola Forró - 0.2-1", + datetime.datetime(2021, 10, 18, 12, 34, 45, tzinfo=UTC), + marks=needs_tzdb, + ), + # DST is never applied, like in RPM + pytest.param( + "* Tue Jul 16 12:00:00 CET 2024 Nikola Forró - 0.3-1", + datetime.datetime(2024, 7, 16, 11, tzinfo=UTC), + marks=needs_tzdb, + ), + pytest.param( + _OPENSUSE_CHANGELOG_SEPARATOR + + "\n" + + "Tue Dec 17 14:21:37 EST 2024 - Dan Čermák ", + datetime.datetime(2024, 12, 17, 19, 21, 37, tzinfo=UTC), + marks=needs_tzdb, + ), + # must not escape the timezone database + pytest.param( + "* Tue Jul 16 12:00:00 ../../etc/passwd 2024 Nikola Forró - 0.3-1", + datetime.datetime(2024, 7, 16, 12, tzinfo=UTC), + marks=needs_tzdb, + ), + # neither the basic nor the extended format, RPM rejects it as well + ("* Mon Oct 18 12:34:45 2021 Nikola Forró - 0.2-1", None), + ("* Wed Feb 31 2021 Nikola Forró - 0.1-1", None), + ("* Nikola Forró ", None), + (_OPENSUSE_CHANGELOG_SEPARATOR, None), + ], +) +def test_entry_timestamp(header, timestamp): + assert ChangelogEntry(header, [""]).timestamp == timestamp + + +@pytest.mark.parametrize( + "header, style", + [ + ( + "* Tue May 4 2021 Nikola Forró - 0.1-1", + ChangelogStyle.standard, + ), + pytest.param( + "* Mon Oct 18 12:34:45 CET 2021 Nikola Forró - 0.2-1", + ChangelogStyle.standard, + marks=needs_tzdb, + ), + ( + _OPENSUSE_CHANGELOG_SEPARATOR + + "\n" + + "Tue Dec 17 14:21:37 UTC 2024 - Dan Čermák ", + ChangelogStyle.openSUSE, + ), + ], +) +def test_entry_assemble_roundtrip(header, style): + entry = ChangelogEntry(header, ["- entry"]) + evr = entry.evr + assert ( + ChangelogEntry.assemble( + entry.timestamp, + entry.author, + entry.content, + str(evr) if evr is not None else None, + day_of_month_padding=entry.sanitized_day_of_month_padding, + style=style, + ).header + == header + ) + + def test_get_raw_section_data(): tzinfo = datetime.timezone(datetime.timedelta(hours=2), name="CEST") changelog = Changelog( From 3f2d3e9fc8b7fe2039dede5a86bd3c6528a1968e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Tue, 10 Nov 2026 12:20:36 +0100 Subject: [PATCH 3/3] Apply suggestions from Nikola MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nikola Forró --- specfile/changelog.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/specfile/changelog.py b/specfile/changelog.py index b1ee22e..e4d33f3 100644 --- a/specfile/changelog.py +++ b/specfile/changelog.py @@ -65,7 +65,8 @@ def _get_tzinfo(name: str, timestamp: datetime.datetime) -> Optional[datetime.tzinfo]: - """Resolves a timezone name as RPM does it: pass the name to libc, which + """ + Resolves a timezone name in a similar way to how RPM does it: pass the name to libc, which looks it up in the system timezone database. Unknown names (e.g. "CEST", i.e. an abbreviation instead of a timezone identifier) are treated as UTC and DST is never applied, because RPM calls `mktime()` with `tm_isdst` set @@ -78,7 +79,6 @@ def _get_tzinfo(name: str, timestamp: datetime.datetime) -> Optional[datetime.tz Returns: Matching `tzinfo` object or None if the name can't be resolved reliably. - """ try: from zoneinfo import ZoneInfo @@ -267,7 +267,8 @@ def author(self) -> Optional[str]: @property def timestamp(self) -> Optional[Union[datetime.datetime, datetime.date]]: - """Timestamp of the entry, `date` for the basic format ("Tue May 4 + """ + Timestamp of the entry, `date` for the basic format ("Tue May 4 2021"), timezone-aware `datetime` for the extended one ("Mon Oct 18 12:34:45 CET 2021"). `None` if it can't be parsed. @@ -278,10 +279,9 @@ def timestamp(self) -> Optional[Union[datetime.datetime, datetime.date]]: - CET is UTC+1 even in summer Unlike RPM, POSIX TZ strings (e.g. "UTC+2") are not supported and mean - UTC as well + UTC as well. Without `zoneinfo` (Python < 3.9) only "UTC" and "GMT" are resolved! - """ m = self._timestamp_match if m is None: