Skip to content

Commit

Permalink
Convert ChangelogEntry.evr to return an EVR
Browse files Browse the repository at this point in the history
Also, adjust the EVR code to ensure that we don't match emails as EVRs

Co-authored-by: Nikola Forró <[email protected]>
  • Loading branch information
dcermak and nforro committed Jan 7, 2025
1 parent c0a98a8 commit 64b14c8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
64 changes: 40 additions & 24 deletions specfile/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ class ChangelogEntry:
content: List of lines forming the content of the entry.
"""

_EVR_RE = re.compile(
r"""
^.*
\d{4} # year
\s+ # whitespace
.+ # author
\s+ # preceding whitespace
((?P<sb>\[)|(?P<rb>\())? # optional opening bracket
(?P<evr>\S+?) # EVR
(?(sb)\]|(?(rb)\))) # matching closing bracket
:? # optional colon
\s* # optional following whitespace
$
""",
re.VERBOSE,
)

def __init__(
self,
header: str,
Expand Down Expand Up @@ -83,25 +100,24 @@ def __repr__(self) -> str:
return f"ChangelogEntry({self.header!r}, {self.content!r}, {self._following_lines!r})"

@property
def evr(self) -> Optional[str]:
def evr(self) -> Optional[EVR]:
"""EVR (epoch, version, release) of the entry."""
m = re.match(
r"""
^.*
\s+ # preceding whitespace
((?P<sb>\[)|(?P<rb>\())? # optional opening bracket
(?P<evr>(\d+:)?\S+-\S+?) # EVR
(?(sb)\]|(?(rb)\))) # matching closing bracket
:? # optional colon
\s* # optional following whitespace
$
""",
self.header,
re.VERBOSE,
)
m = self._EVR_RE.match(self.header)

if not m:
return None
return m.group("evr")

evr_s = m.group("evr")
try:
evr = EVR.from_string(evr_s)
except SpecfileException:
return None

# looks like an email
if evr_s.startswith("<") and evr_s.endswith(">"):
return None

return evr

@property
def extended_timestamp(self) -> bool:
Expand Down Expand Up @@ -263,7 +279,9 @@ def copy(self) -> "Changelog":
return copy.copy(self)

def filter(
self, since: Optional[str] = None, until: Optional[str] = None
self,
since: Optional[Union[str, EVR]] = None,
until: Optional[Union[str, EVR]] = None,
) -> "Changelog":
"""
Filters changelog entries with EVR between since and until.
Expand All @@ -287,22 +305,20 @@ def parse_evr(s):
if since is None:
start_index = 0
else:
since_evr: EVR = parse_evr(since) if isinstance(since, str) else since
start_index = next(
(
i
for i, e in enumerate(self.data)
if parse_evr(e.evr) >= parse_evr(since)
),
(i for i, entry in enumerate(self.data) if entry.evr >= since_evr),
len(self.data) + 1,
)
if until is None:
end_index = len(self.data) + 1
else:
until_evr = parse_evr(until) if isinstance(until, str) else until
end_index = next(
(
i + 1
for i, e in reversed(list(enumerate(self.data)))
if parse_evr(e.evr) <= parse_evr(until)
for i, entry in reversed(list(enumerate(self.data)))
if entry.evr <= until_evr
),
0,
)
Expand Down
20 changes: 16 additions & 4 deletions tests/unit/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

from specfile.changelog import Changelog, ChangelogEntry
from specfile.sections import Section
from specfile.utils import EVR


@pytest.mark.parametrize(
"header, evr",
"header, evr_str",
[
("* Thu Jan 04 2007 Michael Schwendt <[email protected]>", None),
("* Thu Jan 04 2007 Michael Schwendt <[email protected]>", None),
(
"* Fri Jul 26 2024 Miroslav Suchý <[email protected]> - ss981107-67",
"ss981107-67",
),
(
"* Mon Jul 13 2020 Tom Stellard <[email protected]> 4.0-0.4.pre2",
"4.0-0.4.pre2",
Expand All @@ -39,8 +45,12 @@
),
],
)
def test_entry_evr(header, evr):
assert ChangelogEntry(header, [""]).evr == evr
def test_entry_evr(header, evr_str):
evr = ChangelogEntry(header, [""]).evr
if evr_str:
assert evr == EVR.from_string(evr_str)
else:
assert evr is None


@pytest.mark.parametrize(
Expand Down Expand Up @@ -133,7 +143,9 @@ def test_filter(since, until, evrs):
),
]
)
assert [e.evr for e in changelog.filter(since=since, until=until)] == evrs
assert [e.evr for e in changelog.filter(since=since, until=until)] == [
EVR.from_string(evr) for evr in evrs
]


def test_parse():
Expand Down

0 comments on commit 64b14c8

Please sign in to comment.