Skip to content

Commit 152f67c

Browse files
Take validity into account when accessing tags and macro definitions by name (#328)
Take validity into account when accessing tags and macro definitions by name Fixes #327. RELEASE NOTES BEGIN When accessing tags or macro definitions by name, specfile now takes validity into account when looking for the best match. For example if there are two instances of Version tag, one in the true and one in the false branch of a condition, Specfile.version will always access the one that is in the true branch. RELEASE NOTES END Reviewed-by: Maja Massarini
2 parents c9599ac + 90006aa commit 152f67c

File tree

12 files changed

+178
-5
lines changed

12 files changed

+178
-5
lines changed

specfile/macro_definitions.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,36 @@ def get(self, name: str, position: Optional[int] = None) -> MacroDefinition:
219219
return self.data[self.find(name, position)]
220220

221221
def find(self, name: str, position: Optional[int] = None) -> int:
222+
"""
223+
Finds a macro definition with the specified name. If position is not specified,
224+
returns the first valid matching macro definiton. If there is no such macro
225+
definition, returns the first match, if any. If position is specified and there is
226+
a matching macro definition at that position, it is returned, otherwise
227+
ValueError is raised.
228+
229+
Args:
230+
name: Name of the tag to find.
231+
position: Optional position in the spec file.
232+
233+
Returns:
234+
Index of the matching tag.
235+
236+
Raises:
237+
ValueError if there is no match.
238+
"""
239+
first_match = None
222240
for i, macro_definition in enumerate(self.data):
223241
if macro_definition.name == name:
224-
if position is None or macro_definition.get_position(self) == position:
242+
if position is None:
243+
if first_match is None:
244+
first_match = i
245+
if macro_definition.valid:
246+
return i
247+
elif macro_definition.get_position(self) == position:
225248
return i
226-
raise ValueError
249+
if first_match is None or position is not None:
250+
raise ValueError
251+
return first_match
227252

228253
@classmethod
229254
def _parse(

specfile/tags.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,35 @@ def get(self, name: str, position: Optional[int] = None) -> Tag:
441441
return self.data[self.find(name, position)]
442442

443443
def find(self, name: str, position: Optional[int] = None) -> int:
444+
"""
445+
Finds a tag with the specified name. If position is not specified,
446+
returns the first valid matching tag. If there is no such tag, returns
447+
the first match, if any. If position is specified and there is a matching
448+
tag at that position, it is returned, otherwise ValueError is raised.
449+
450+
Args:
451+
name: Name of the tag to find.
452+
position: Optional position in the spec file.
453+
454+
Returns:
455+
Index of the matching tag.
456+
457+
Raises:
458+
ValueError if there is no match.
459+
"""
460+
first_match = None
444461
for i, tag in enumerate(self.data):
445462
if tag.name.capitalize() == name.capitalize():
446-
if position is None or tag.get_position(self) == position:
463+
if position is None:
464+
if first_match is None:
465+
first_match = i
466+
if tag.valid:
467+
return i
468+
elif tag.get_position(self) == position:
447469
return i
448-
raise ValueError
470+
if first_match is None or position is not None:
471+
raise ValueError
472+
return first_match
449473

450474
def insert(self, i: int, item: Tag) -> None:
451475
if i > len(self.data):

tests/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
SPEC_COMMENTED_PATCHES = DATA_DIR / "spec_commented_patches"
1919
SPEC_SHELL_EXPANSIONS = DATA_DIR / "spec_shell_expansions"
2020
SPEC_CONDITIONALIZED_CHANGELOG = DATA_DIR / "spec_conditionalized_changelog"
21+
SPEC_CONDITIONALIZED_VERSION = DATA_DIR / "spec_conditionalized_version"
2122

2223
SPECFILE = "test.spec"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
From b7af0b9194585c6d208de3a0e9978d5ad9c5d97b Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <[email protected]>
3+
Date: Wed, 16 Mar 2022 10:29:59 +0100
4+
Subject: [PATCH 1/3] patch0
5+
6+
---
7+
test.txt | 1 +
8+
1 file changed, 1 insertion(+)
9+
10+
diff --git a/test.txt b/test.txt
11+
index 9daeafb..dec2cbe 100644
12+
--- a/test.txt
13+
+++ b/test.txt
14+
@@ -1 +1,2 @@
15+
test
16+
+test
17+
--
18+
2.35.1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
From 6d5d1561b3ccf2df9d001a7af011144acc352361 Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <[email protected]>
3+
Date: Wed, 16 Mar 2022 10:30:15 +0100
4+
Subject: [PATCH 2/3] patch1
5+
6+
---
7+
test.txt | 1 +
8+
1 file changed, 1 insertion(+)
9+
10+
diff --git a/test.txt b/test.txt
11+
index dec2cbe..0867e73 100644
12+
--- a/test.txt
13+
+++ b/test.txt
14+
@@ -1,2 +1,3 @@
15+
test
16+
test
17+
+test
18+
--
19+
2.35.1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
From ae1d3bbca0caf1cce1842ceab4c6d7252c0a7bd8 Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <[email protected]>
3+
Date: Wed, 16 Mar 2022 10:30:29 +0100
4+
Subject: [PATCH 3/3] patch2
5+
6+
---
7+
test.txt | 1 +
8+
1 file changed, 1 insertion(+)
9+
10+
diff --git a/test.txt b/test.txt
11+
index 0867e73..d0c7fbe 100644
12+
--- a/test.txt
13+
+++ b/test.txt
14+
@@ -1,3 +1,4 @@
15+
test
16+
test
17+
test
18+
+test
19+
--
20+
2.35.1
204 Bytes
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
%dnl %global commit 202ab7e698a34154129bb9ded589db58996eeb53
2+
%global shortcommit %(c=%{commit}; echo ${c:0:7})
3+
4+
%global upstream_version 0.1.2
5+
6+
Name: test
7+
%if 0%{?commit:1}
8+
Version: %{upstream_version}^git%{shortcommit}
9+
%else
10+
Version: %{upstream_version}
11+
%endif
12+
Release: 1%{?dist}
13+
Summary: Test package
14+
15+
License: MIT
16+
17+
%if 0%{?commit:1}
18+
Source0: https://example.com/archive/%{name}/%{commit}/%{name}-%{shortcommit}.tar.xz
19+
%else
20+
Source0: https://example.com/archive/%{name}/v%{version}/%{name}-%{version}.tar.xz
21+
%endif
22+
Patch0: patch0.patch
23+
Patch1: patch1.patch
24+
Patch2: patch2.patch
25+
26+
27+
%description
28+
Test package
29+
30+
31+
%prep
32+
%if 0%{?commit:1}
33+
%autosetup -p1 -n %{name}-%{shortcommit}
34+
%else
35+
%autosetup -p1 -n %{name}-%{version}
36+
%endif
37+
38+
39+
%changelog
40+
* Thu Jun 07 2018 Nikola Forró <[email protected]> - 0.1.2-1
41+
- first version
208 Bytes
Binary file not shown.
-204 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)