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..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.""" @@ -537,12 +553,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()