示例#1
0
    def _parse_list_tags(cls, section: str, section_content: List[str],
                         parsed: List[str], section_index: int,
                         next_source_index: int,
                         next_patch_index: int) -> Tuple[List[Tag], int, int]:
        """Parses all tags in a %sourcelist or %patchlist section.

        Only parses tags that are valid (that is - are in parsed), nothing more can
        consistently be detected.

        Follows how rpm works, the new Source/Patch tags are indexed starting from
        the last parsed Source/Patch tag.

        """
        tag = 'Source' if section == '%sourcelist' else 'Patch'
        result = []
        for i, line in enumerate(section_content):
            expanded = MacroHelper.expand(line)
            is_comment = SpecContent.get_comment_span(line,
                                                      section)[0] != len(line)
            if not expanded or not line or is_comment or not [
                    p for p in parsed if p == expanded.rstrip()
            ]:
                continue
            tag_name, tag_index, next_source_index, next_patch_index = cls._sanitize_tag(
                tag, next_source_index, next_patch_index)
            result.append(
                Tag(section_index, section, i, tag_name, (0, len(line)), True,
                    tag_index))

        return result, next_source_index, next_patch_index
示例#2
0
 def update(self) -> None:
     # explicitly discard old instance to prevent rpm from destroying
     # "sources" and "patches" lua tables after new instance is created
     self.spc = None
     self.spc = RpmHelper.get_rpm_spec(self.path, self.sources_location, self.predefined_macros)
     self.header = RpmHeader(self.spc.sourceHeader)
     self.spec_content = self._read_spec_content()
     self.tags = Tags(self.spec_content, SpecContent(self.spc.parsed))
     self._update_data()
示例#3
0
    def _read_spec_content(self) -> SpecContent:
        """Reads the content of the Spec file.

        Returns:
            The created SpecContent instance.

        Raises:
            RebaseHelperError: If the Spec file cannot be read.

        """
        try:
            with open(self.path) as f:
                content = f.read()
        except IOError as e:
            raise RebaseHelperError("Unable to open and read SPEC file '{}'".format(self.path)) from e
        return SpecContent(content)
示例#4
0
    def __init__(self, path: str, sources_location: str = '', predefined_macros: Optional[Dict[str, str]] = None,
                 lookaside_cache_preset: str = 'fedpkg', keep_comments: bool = False):
        # Initialize attributes
        self.path: str = path
        self.sources_location: str = sources_location
        self.predefined_macros: Dict[str, str] = predefined_macros or {}
        self.lookaside_cache_preset: str = lookaside_cache_preset
        self.keep_comments: bool = keep_comments
        self.prep_section: str = ''
        self.sources: List[str] = []
        self.patches: Dict[str, List[PatchObject]] = {}
        self.removed_patches: List[str] = []
        self.category: Optional[PackageCategory] = None
        self.spc: rpm.spec = RpmHelper.get_rpm_spec(self.path, self.sources_location, self.predefined_macros)
        self.header: RpmHeader = RpmHeader(self.spc.sourceHeader)
        self.spec_content: SpecContent = self._read_spec_content()
        self.tags: Tags = Tags(self.spec_content, SpecContent(self.spc.parsed))

        # Load rpm information
        self._update_data()
示例#5
0
def mocked_spec_object(spec_attributes):
    spec = SpecFile.__new__(SpecFile)
    spec.save = lambda: None
    for attribute, value in spec_attributes.items():
        if attribute == 'macros':
            for macro, properties in value.items():
                rpm.addMacro(macro, properties.get('value', ''))
            macros = MacroHelper.dump()
            for macro, properties in value.items():
                for m in macros:
                    if m['name'] == macro:
                        for prop, v in properties.items():
                            if prop != 'value':
                                m[prop] = v
            value = macros
        if attribute == 'spec_content' and isinstance(value, str):
            value = SpecContent(value)
        setattr(spec, attribute, value)
    if hasattr(spec, 'spec_content') and not hasattr(spec, 'tags'):
        spec.tags = Tags(spec.spec_content, spec.spec_content)
    return spec
示例#6
0
 def tags(self):
     rpm.reloadConfig()
     for macro in self.MACROS:
         rpm.addMacro(*macro)
     return Tags(SpecContent(self.RAW_CONTENT), SpecContent(self.PARSED_CONTENT))
示例#7
0
 def test_contructor(self):
     spec = SpecContent('')
     tags = Tags(spec, spec)
     assert isinstance(tags, Tags)
 def test_get_comment_span(self, line, section, expected):
     assert SpecContent.get_comment_span(line, section) == expected
 def spec_content(self):
     with open(SPEC_FILE, 'r') as infile:
         return SpecContent(infile.read())