Skip to content

Commit

Permalink
Set validity of sourcelist entries based on conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Nikola Forró <[email protected]>
  • Loading branch information
nforro committed Oct 25, 2023
1 parent 4a8d268 commit 256d332
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
22 changes: 18 additions & 4 deletions specfile/sourcelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,24 +24,31 @@ 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.
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:
Constructed instance of `SourceListEntry` class.
"""
self.location = location
self.comments = comments.copy()
self.valid = valid
self._context = context

def __eq__(self, other: object) -> bool:
Expand All @@ -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":
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions specfile/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 256d332

Please sign in to comment.