Skip to content

Commit

Permalink
Refactor SpecFile._process_patches()
Browse files Browse the repository at this point in the history
Make it public and usable on its own, not only from within
SpecFile.write_updated_patches().

Signed-off-by: Nikola Forró <nforro@redhat.com>
  • Loading branch information
nforro committed Jan 29, 2021
1 parent ad815b7 commit 0cda1a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
58 changes: 34 additions & 24 deletions rebasehelper/specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,19 @@ def get_not_used_patches(self):
"""
return self.patches['not_applied']

def _process_patches(self, comment_out=None, remove_patches=None, disable_inapplicable_patches=None):
"""
Comment out and delete patches from SPEC file
def process_patch_macros(self, comment_out: Optional[List[int]] = None, remove: Optional[List[int]] = None,
annotate: Optional[List[int]] = None, note: Optional[str] = None) -> None:
"""Processes %patch macros in %prep section.
:var comment_out: list with patch numbers to comment out
:var remove_patches: list with patch numbers to delete
:var disable_inapplicable_patches: boolean value deciding if the inapplicable patches should be commented out
Args:
comment_out: List of patch numbers to comment out.
remove: List of patch numbers to remove.
annotate: List of patch numbers to annotate.
note: Message to annotate patches with.
"""
if comment_out is None:
comment_out = []
if remove_patches is None:
remove_patches = []
comment_out = list(comment_out or [])
remove = list(remove or [])
annotate = list(annotate or [])

prep = self.spec_content.section('%prep')
if not prep:
Expand All @@ -389,22 +390,26 @@ def _process_patches(self, comment_out=None, remove_patches=None, disable_inappl
match = patch_re.match(line)
if match:
index = int(match.group('index'))
if note and index in annotate and index not in remove:
prep.insert(i, '# {}'.format(note))
annotate.remove(index)
i += 1
continue
if index in comment_out:
if disable_inapplicable_patches:
prep[i] = '#%{}'.format(line)
removed += 1
prep.insert(i, '# The following patch contains conflicts')
prep[i] = '#%{}'.format(line)
comment_out.remove(index)
i += 1
elif index in remove_patches:
removed += 1
elif index in remove:
del prep[i]
remove_patches.remove(index)
i -= 1
remove.remove(index)
removed += 1
i -= 1
# When combining Patch tags and %patchlist, if a Patch is removed, the indexes
# of %patchlist patches change and %patch macros need to be modified.
elif self.tag('Patch{}'.format(index)).section_name.startswith('%patchlist'):
prep[i] = patch_re.sub(r'%patch{}\2'.format(index - removed), prep[i])
else:
tag = self.tag('Patch{}'.format(index))
if tag and tag.section_name.startswith('%patchlist'):
prep[i] = patch_re.sub(r'%patch{}\2'.format(index - removed), prep[i])
i += 1

@saves
Expand Down Expand Up @@ -455,7 +460,8 @@ def is_empty(line):
if patch_removed:
# remove the line of the patch that was removed
self.removed_patches.append(patch_name)
removed_patches.append(tag.index)
if tag.index:
removed_patches.append(tag.index)
# find associated comments
i = tag.line
if not self.keep_comments:
Expand All @@ -470,20 +476,24 @@ def is_empty(line):
if disable_inapplicable:
# comment out line if the patch was not applied
section[tag.line] = '#' + section[tag.line]
inapplicable_patches.append(tag.index)
if tag.index:
inapplicable_patches.append(tag.index)
if 'modified' in patches:
patch = [x for x in patches['modified'] if patch_name in x]
else:
patch = []
if patch:
name = os.path.join(constants.RESULTS_DIR, constants.REBASED_SOURCES_DIR, patch_name)
self.set_raw_tag_value(tag.name, name)
modified_patches.append(tag.index)
if tag.index:
modified_patches.append(tag.index)
for section_index, remove in remove_lines.items():
content = self.spec_content[section_index]
for span in sorted(remove, key=lambda s: s[0], reverse=True):
del content[slice(*span)]
self._process_patches(inapplicable_patches, removed_patches, disable_inapplicable)
self.process_patch_macros(comment_out=inapplicable_patches if disable_inapplicable else None,
remove=removed_patches, annotate=inapplicable_patches,
note='The following patch contains conflicts')

###################################
# PACKAGE VERSION RELATED METHODS #
Expand Down
4 changes: 4 additions & 0 deletions rebasehelper/tests/public_api/test_specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ def test_save(self, spec_object):

def test_udpate(self, spec_object):
assert spec_object.update() is None

def test_process_patch_macros(self, spec_object):
assert spec_object.process_patch_macros() is None
assert spec_object.process_patch_macros(comment_out=[0], remove=[1], annotate=[2], note='test') is None

0 comments on commit 0cda1a1

Please sign in to comment.