Skip to content

Commit 7876726

Browse files
Fix EVR regex to not match email addresses with - in them (#445)
Fix EVR regex to not match email addresses with - in them TODO: Write new tests or update the old ones to cover new functionality. Update doc-strings where appropriate. Update or write new documentation in packit/packit.dev. Reviewed-by: Nikola Forró Reviewed-by: Dan Čermák <[email protected]>
2 parents c0a98a8 + 10a7f1e commit 7876726

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

specfile/changelog.py

Lines changed: 43 additions & 23 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.
@@ -278,9 +296,11 @@ def filter(
278296
Filtered changelog.
279297
"""
280298

281-
def parse_evr(s):
299+
def parse_evr(str_or_evr: Union[str, EVR]) -> EVR:
300+
if isinstance(str_or_evr, EVR):
301+
return str_or_evr
282302
try:
283-
return EVR.from_string(s)
303+
return EVR.from_string(str_or_evr)
284304
except SpecfileException:
285305
return EVR(version="0")
286306

@@ -290,8 +310,8 @@ def parse_evr(s):
290310
start_index = next(
291311
(
292312
i
293-
for i, e in enumerate(self.data)
294-
if parse_evr(e.evr) >= parse_evr(since)
313+
for i, entry in enumerate(self.data)
314+
if entry.evr >= parse_evr(since)
295315
),
296316
len(self.data) + 1,
297317
)
@@ -301,8 +321,8 @@ def parse_evr(s):
301321
end_index = next(
302322
(
303323
i + 1
304-
for i, e in reversed(list(enumerate(self.data)))
305-
if parse_evr(e.evr) <= parse_evr(until)
324+
for i, entry in reversed(list(enumerate(self.data)))
325+
if entry.evr <= parse_evr(until)
306326
),
307327
0,
308328
)

tests/unit/test_changelog.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,57 @@
33

44
import copy
55
import datetime
6+
from typing import Optional
67

78
import pytest
89

910
from specfile.changelog import Changelog, ChangelogEntry
1011
from specfile.sections import Section
12+
from specfile.utils import EVR
1113

1214

1315
@pytest.mark.parametrize(
1416
"header, evr",
1517
[
1618
("* Thu Jan 04 2007 Michael Schwendt <[email protected]>", None),
19+
("* Thu Jan 04 2007 Michael Schwendt <[email protected]>", None),
20+
(
21+
"* Fri Jul 26 2024 Miroslav Suchý <[email protected]> - ss981107-67",
22+
EVR(version="ss981107", release="67"),
23+
),
1724
(
1825
"* Mon Jul 13 2020 Tom Stellard <[email protected]> 4.0-0.4.pre2",
19-
"4.0-0.4.pre2",
26+
EVR(version="4.0", release="0.4.pre2"),
27+
),
28+
(
29+
"* Fri Jul 20 2018 Gwyn Ciesla <[email protected]> - 0.52-6",
30+
EVR(version="0.52", release="6"),
2031
),
21-
("* Fri Jul 20 2018 Gwyn Ciesla <[email protected]> - 0.52-6", "0.52-6"),
2232
(
2333
"* Mon Feb 23 2009 Fedora Release Engineering <[email protected]> "
2434
"- 1.23-3.20081106gitbe42b4",
25-
"1.23-3.20081106gitbe42b4",
35+
EVR(version="1.23", release="3.20081106gitbe42b4"),
2636
),
2737
(
2838
"* Thu Feb 04 2016 Marcin Zajaczkowski <mszpak ATT wp DOTT pl> - 1:0.9.10-6",
29-
"1:0.9.10-6",
39+
EVR(epoch=1, version="0.9.10", release="6"),
3040
),
3141
(
3242
"* Mon Jan 03 2022 Fedora Kernel Team <[email protected]> [5.16-0.rc8.55]",
33-
"5.16-0.rc8.55",
43+
EVR(version="5.16", release="0.rc8.55"),
44+
),
45+
(
46+
"* Wed Jan 23 2002 Karsten Hopp <[email protected]> (4.6-1)",
47+
EVR(version="4.6", release="1"),
3448
),
35-
("* Wed Jan 23 2002 Karsten Hopp <[email protected]> (4.6-1)", "4.6-1"),
3649
(
3750
"* Thu Apr 9 2015 Jeffrey C. Ollie <[email protected]> - 13.3.2-1:",
38-
"13.3.2-1",
51+
EVR(version="13.3.2", release="1"),
3952
),
4053
],
4154
)
42-
def test_entry_evr(header, evr):
43-
assert ChangelogEntry(header, [""]).evr == evr
55+
def test_entry_evr(header, evr: Optional[EVR]):
56+
assert evr == ChangelogEntry(header, [""]).evr
4457

4558

4659
@pytest.mark.parametrize(
@@ -133,7 +146,9 @@ def test_filter(since, until, evrs):
133146
),
134147
]
135148
)
136-
assert [e.evr for e in changelog.filter(since=since, until=until)] == evrs
149+
assert [e.evr for e in changelog.filter(since=since, until=until)] == [
150+
EVR.from_string(evr) for evr in evrs
151+
]
137152

138153

139154
def test_parse():

0 commit comments

Comments
 (0)