Skip to content

Commit 64b14c8

Browse files
dcermaknforro
andcommitted
Convert ChangelogEntry.evr to return an EVR
Also, adjust the EVR code to ensure that we don't match emails as EVRs Co-authored-by: Nikola Forró <nforro@redhat.com>
1 parent c0a98a8 commit 64b14c8

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

specfile/changelog.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ class ChangelogEntry:
4646
content: List of lines forming the content of the entry.
4747
"""
4848

49+
_EVR_RE = re.compile(
50+
r"""
51+
^.*
52+
\d{4} # year
53+
\s+ # whitespace
54+
.+ # author
55+
\s+ # preceding whitespace
56+
((?P<sb>\[)|(?P<rb>\())? # optional opening bracket
57+
(?P<evr>\S+?) # EVR
58+
(?(sb)\]|(?(rb)\))) # matching closing bracket
59+
:? # optional colon
60+
\s* # optional following whitespace
61+
$
62+
""",
63+
re.VERBOSE,
64+
)
65+
4966
def __init__(
5067
self,
5168
header: str,
@@ -83,25 +100,24 @@ def __repr__(self) -> str:
83100
return f"ChangelogEntry({self.header!r}, {self.content!r}, {self._following_lines!r})"
84101

85102
@property
86-
def evr(self) -> Optional[str]:
103+
def evr(self) -> Optional[EVR]:
87104
"""EVR (epoch, version, release) of the entry."""
88-
m = re.match(
89-
r"""
90-
^.*
91-
\s+ # preceding whitespace
92-
((?P<sb>\[)|(?P<rb>\())? # optional opening bracket
93-
(?P<evr>(\d+:)?\S+-\S+?) # EVR
94-
(?(sb)\]|(?(rb)\))) # matching closing bracket
95-
:? # optional colon
96-
\s* # optional following whitespace
97-
$
98-
""",
99-
self.header,
100-
re.VERBOSE,
101-
)
105+
m = self._EVR_RE.match(self.header)
106+
102107
if not m:
103108
return None
104-
return m.group("evr")
109+
110+
evr_s = m.group("evr")
111+
try:
112+
evr = EVR.from_string(evr_s)
113+
except SpecfileException:
114+
return None
115+
116+
# looks like an email
117+
if evr_s.startswith("<") and evr_s.endswith(">"):
118+
return None
119+
120+
return evr
105121

106122
@property
107123
def extended_timestamp(self) -> bool:
@@ -263,7 +279,9 @@ def copy(self) -> "Changelog":
263279
return copy.copy(self)
264280

265281
def filter(
266-
self, since: Optional[str] = None, until: Optional[str] = None
282+
self,
283+
since: Optional[Union[str, EVR]] = None,
284+
until: Optional[Union[str, EVR]] = None,
267285
) -> "Changelog":
268286
"""
269287
Filters changelog entries with EVR between since and until.
@@ -287,22 +305,20 @@ def parse_evr(s):
287305
if since is None:
288306
start_index = 0
289307
else:
308+
since_evr: EVR = parse_evr(since) if isinstance(since, str) else since
290309
start_index = next(
291-
(
292-
i
293-
for i, e in enumerate(self.data)
294-
if parse_evr(e.evr) >= parse_evr(since)
295-
),
310+
(i for i, entry in enumerate(self.data) if entry.evr >= since_evr),
296311
len(self.data) + 1,
297312
)
298313
if until is None:
299314
end_index = len(self.data) + 1
300315
else:
316+
until_evr = parse_evr(until) if isinstance(until, str) else until
301317
end_index = next(
302318
(
303319
i + 1
304-
for i, e in reversed(list(enumerate(self.data)))
305-
if parse_evr(e.evr) <= parse_evr(until)
320+
for i, entry in reversed(list(enumerate(self.data)))
321+
if entry.evr <= until_evr
306322
),
307323
0,
308324
)

tests/unit/test_changelog.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88

99
from specfile.changelog import Changelog, ChangelogEntry
1010
from specfile.sections import Section
11+
from specfile.utils import EVR
1112

1213

1314
@pytest.mark.parametrize(
14-
"header, evr",
15+
"header, evr_str",
1516
[
1617
("* Thu Jan 04 2007 Michael Schwendt <mschwendt@fedoraproject.org>", None),
18+
("* Thu Jan 04 2007 Michael Schwendt <mschwendt@fedora-project.org>", None),
19+
(
20+
"* Fri Jul 26 2024 Miroslav Suchý <msuchy@redhat.com> - ss981107-67",
21+
"ss981107-67",
22+
),
1723
(
1824
"* Mon Jul 13 2020 Tom Stellard <tstellar@redhat.com> 4.0-0.4.pre2",
1925
"4.0-0.4.pre2",
@@ -39,8 +45,12 @@
3945
),
4046
],
4147
)
42-
def test_entry_evr(header, evr):
43-
assert ChangelogEntry(header, [""]).evr == evr
48+
def test_entry_evr(header, evr_str):
49+
evr = ChangelogEntry(header, [""]).evr
50+
if evr_str:
51+
assert evr == EVR.from_string(evr_str)
52+
else:
53+
assert evr is None
4454

4555

4656
@pytest.mark.parametrize(
@@ -133,7 +143,9 @@ def test_filter(since, until, evrs):
133143
),
134144
]
135145
)
136-
assert [e.evr for e in changelog.filter(since=since, until=until)] == evrs
146+
assert [e.evr for e in changelog.filter(since=since, until=until)] == [
147+
EVR.from_string(evr) for evr in evrs
148+
]
137149

138150

139151
def test_parse():

0 commit comments

Comments
 (0)