From 256d3320d2d49a01baf869d2c08fa39487cb566b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Wed, 25 Oct 2023 17:22:17 +0200 Subject: [PATCH 1/2] Set validity of sourcelist entries based on conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nikola Forró --- specfile/sourcelist.py | 22 ++++++++++++++++++---- specfile/sources.py | 7 +++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/specfile/sourcelist.py b/specfile/sourcelist.py index da70bec..97b8113 100644 --- a/specfile/sourcelist.py +++ b/specfile/sourcelist.py @@ -5,7 +5,9 @@ import copy from typing import TYPE_CHECKING, Any, Dict, List, Optional, overload +from specfile.conditions import process_conditions from specfile.formatter import formatted +from specfile.macro_definitions import MacroDefinitions from specfile.macros import Macros from specfile.sections import Section from specfile.tags import Comments @@ -22,10 +24,15 @@ class SourcelistEntry: Attributes: location: Literal location of the source/patch as stored in the spec file. comments: List of comments associated with the source/patch. + valid: Whether the entry is not located in a false branch of a condition. """ def __init__( - self, location: str, comments: Comments, context: Optional["Specfile"] = None + self, + location: str, + comments: Comments, + valid: bool = True, + context: Optional["Specfile"] = None, ) -> None: """ Constructs a `SourceListEntry` object. @@ -33,6 +40,7 @@ def __init__( Args: location: Literal location of the source/patch as stored in the spec file. comments: List of comments associated with the source/patch. + valid: Whether the entry is not located in a false branch of a condition. context: `Specfile` instance that defines the context for macro expansions. Returns: @@ -40,6 +48,7 @@ def __init__( """ self.location = location self.comments = comments.copy() + self.valid = valid self._context = context def __eq__(self, other: object) -> bool: @@ -50,7 +59,8 @@ def __eq__(self, other: object) -> bool: @formatted def __repr__(self) -> str: return ( - f"SourcelistEntry({self.location!r}, {self.comments!r}, {self._context!r})" + f"SourcelistEntry({self.location!r}, {self.comments!r}, " + f"{self.valid!r}, {self._context!r})" ) def __deepcopy__(self, memo: Dict[int, Any]) -> "SourcelistEntry": @@ -134,11 +144,15 @@ def parse( Returns: Constructed instance of `Sourcelist` class. """ + macro_definitions = MacroDefinitions.parse(list(section)) + lines = process_conditions(list(section), macro_definitions, context) data = [] buffer: List[str] = [] - for line in section: + for line, valid in lines: if line and not line.lstrip().startswith("#"): - data.append(SourcelistEntry(line, Comments.parse(buffer), context)) + data.append( + SourcelistEntry(line, Comments.parse(buffer), valid, context) + ) buffer = [] else: buffer.append(line) diff --git a/specfile/sources.py b/specfile/sources.py index 62986c2..2f3f82c 100644 --- a/specfile/sources.py +++ b/specfile/sources.py @@ -537,12 +537,15 @@ def insert(self, i: int, location: str) -> None: container.insert( index, SourcelistEntry( # type: ignore[arg-type] - location, Comments(), context=self._context + location, + Comments(), + container[index - 1].valid, + context=self._context, ), ) elif self._sourcelists: self._sourcelists[-1].append( - SourcelistEntry(location, Comments(), context=self._context) + SourcelistEntry(location, Comments(), True, context=self._context) ) else: index, name, separator = self._get_initial_tag_setup() From 47c21fa7346da1a51cadd5360e20f5d879f98a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Wed, 25 Oct 2023 17:30:45 +0200 Subject: [PATCH 2/2] Add valid property to sources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nikola Forró --- specfile/sources.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/specfile/sources.py b/specfile/sources.py index 2f3f82c..34b6675 100644 --- a/specfile/sources.py +++ b/specfile/sources.py @@ -66,6 +66,12 @@ def comments(self) -> Comments: """List of comments associated with the source.""" ... + @property + @abstractmethod + def valid(self) -> bool: + """Whether the source is not located in a false branch of a condition.""" + ... + class TagSource(Source): """Class that represents a source backed by a spec file tag.""" @@ -161,6 +167,11 @@ def comments(self) -> Comments: """List of comments associated with the source.""" return self._tag.comments + @property + def valid(self) -> bool: + """Whether the source is not located in a false branch of a condition.""" + return self._tag.valid + class ListSource(Source): """Class that represents a source backed by a line in a %sourcelist section.""" @@ -224,6 +235,11 @@ def comments(self) -> Comments: """List of comments associated with the source.""" return self._source.comments + @property + def valid(self) -> bool: + """Whether the source is not located in a false branch of a condition.""" + return self._source.valid + class Sources(collections.abc.MutableSequence): """Class that represents a sequence of all sources."""