From abfa0a18c57f37a79dbbf3a07b9609d5b301baa2 Mon Sep 17 00:00:00 2001 From: Olav Vitters Date: Wed, 29 Apr 2020 15:00:01 +0200 Subject: refactor SpecFile.clean_spec, split it into multiple private functions --- mgagnome | 153 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 83 insertions(+), 70 deletions(-) diff --git a/mgagnome b/mgagnome index ef87d7e..a4b5dfa 100755 --- a/mgagnome +++ b/mgagnome @@ -512,7 +512,7 @@ class SpecFile(): re.MULTILINE + re.IGNORECASE) ), ( - 'change find_lang remove duplicate with_gnome', + 'change find_lang remove duplicate --with-gnome', None, re.compile(r'^(?P\%find_lang[^\\\n]+ --with-gnome) --with-gnome(?P[^\\\n]*\n)', re.MULTILINE + re.IGNORECASE) @@ -524,18 +524,6 @@ class SpecFile(): ('use new Python macros', r'%{python3_version}', re.compile(r'%{py3ver}', re.MULTILINE)), ] - re_convert_br = [ - ( - 'remove py_requires', - ('python',), - re.compile(r'^\%(?:py_requires|\{py_requires\})[ \t]*\n', re.MULTILINE) - ), - ( - 'remove py_requires -d', - ('python', 'python-devel'), - re.compile(r'^\%(?:py_requires[ \t]+-d|\{py_requires[ \t]+-d\})[ \t]*\n', re.MULTILINE) - ), - ] made_changes = False with open(self.path, "r", encoding="utf-8") as fp_spec: @@ -560,64 +548,9 @@ class SpecFile(): if should_rebuild: self._should_rebuild = True - # Convert %py_requires and %py_requires -d - # - first figure out how a buildrequire is usually defined - match = self.re_update_br.search(data) - br_pre = match.group('pre') if match and match.group('pre') else "BuildRequires:\t" - - for reason, new_brs, regexp in re_convert_br: - match = regexp.search(data) - if match: - # Don't add multiple buildrequires - change_to = "" - brs_in_file = set() - for match2 in self.re_update_br.finditer(data): - if match2.group('br') in new_brs: - brs_in_file.add(match2.group('br')) - - for buildreq in set(new_brs) - brs_in_file: - change_to += ''.join((br_pre, buildreq, "\n")) - data, subs = regexp.subn(change_to, data) - if subs: - made_changes = True - self._changes['SILENT %s' % reason] = True - - # Convert: - # %define name SOMETHING - # name: %{name} - # Into - # name: SOMETHING - converted_defines = [] - for search_for in ('name', 'version', 'release', 'summary', 'Summary', 'group'): - search_for_ignore_case = ''.join(("[%s%s]" % (letter, letter.swapcase()) for letter in search_for)) - re_spec = re.compile( - r'^(?P' + re.escape(search_for_ignore_case) + r'[ \t]*:[ \t]*)(?:\%' + - re.escape(search_for) + r'|\%\{' + re.escape(search_for) + - r'\})(?P[ \t]*\n)', re.MULTILINE - ) - re_variable = re.compile( - r'^(?P\%define[ \t]+' + re.escape(search_for) + - r'[ \t]+(?P[^\n]+?))(?P[ \t]*\n)', - re.MULTILINE - ) - - match = re_variable.search(data) - if match and match.group('definition') and len(re_variable.findall(data)) == 1: - match2 = re_spec.search(data) - if match2: - data, subs = re_spec.subn( - r'\g' + match.group('definition').replace('\\', '\\\\') + r'\g', - data - ) - if subs: - made_changes = True - data, subs = re_variable.subn('', data) - converted_defines.append(search_for) - - if made_changes and converted_defines: - data = data.lstrip() - self._changes['SILENT remove variable definition(s) %s' % ", ".join(converted_defines)] = True + made_changes, data = self._clean_spec_py_requires(made_changes, data) + made_changes, data = self._clean_spec_defines(made_changes, data) made_changes, data = self._clean_spec_patches(made_changes, data) # Overwrite file with new version number @@ -627,6 +560,86 @@ class SpecFile(): return made_changes + + def _clean_spec_py_requires(self, made_changes, data): + """Convert %py_requires and %py_requires -d""" + + re_convert_py_requires = [ + ( + 'remove py_requires', + ('python',), + re.compile(r'^\%(?:py_requires|\{py_requires\})[ \t]*\n', re.MULTILINE) + ), + ( + 'remove py_requires -d', + ('python', 'python-devel'), + re.compile(r'^\%(?:py_requires[ \t]+-d|\{py_requires[ \t]+-d\})[ \t]*\n', re.MULTILINE) + ), + ] + + # First figure out how a buildrequire is usually defined + match = self.re_update_br.search(data) + br_pre = match.group('pre') if match and match.group('pre') else "BuildRequires:\t" + + for reason, new_brs, regexp in re_convert_py_requires: + match = regexp.search(data) + if match: + # Don't add multiple buildrequires + change_to = "" + brs_in_file = set() + for match2 in self.re_update_br.finditer(data): + if match2.group('br') in new_brs: + brs_in_file.add(match2.group('br')) + + for buildreq in set(new_brs) - brs_in_file: + change_to += ''.join((br_pre, buildreq, "\n")) + data, subs = regexp.subn(change_to, data) + if subs: + made_changes = True + self._changes['SILENT %s' % reason] = True + + return made_changes, data + + + def _clean_spec_defines(self, made_changes, data): + # Convert: + # %define name SOMETHING + # name: %{name} + # Into + # name: SOMETHING + converted_defines = [] + for search_for in ('name', 'version', 'release', 'summary', 'Summary', 'group'): + search_for_ignore_case = ''.join(("[%s%s]" % (letter, letter.swapcase()) for letter in search_for)) + re_spec = re.compile( + r'^(?P' + re.escape(search_for_ignore_case) + r'[ \t]*:[ \t]*)(?:\%' + + re.escape(search_for) + r'|\%\{' + re.escape(search_for) + + r'\})(?P[ \t]*\n)', re.MULTILINE + ) + re_variable = re.compile( + r'^(?P\%define[ \t]+' + re.escape(search_for) + + r'[ \t]+(?P[^\n]+?))(?P[ \t]*\n)', + re.MULTILINE + ) + + match = re_variable.search(data) + if match and match.group('definition') and len(re_variable.findall(data)) == 1: + match2 = re_spec.search(data) + if match2: + data, subs = re_spec.subn( + r'\g' + match.group('definition').replace('\\', '\\\\') + r'\g', + data + ) + if subs: + made_changes = True + data, subs = re_variable.subn('', data) + converted_defines.append(search_for) + + if made_changes and converted_defines: + data = data.lstrip() + self._changes['SILENT remove variable definition(s) %s' % ", ".join(converted_defines)] = True + + return made_changes, data + def _clean_spec_patches(self, made_changes, data): re_autopatch = re.compile(r'^[ \t]*\%autopatch(?:[ \t]+-p(?P[0-9]+))?$', re.MULTILINE) -- cgit v1.2.1