From 2bf88394e1e61a0733e40e56027a9e826ec1522e Mon Sep 17 00:00:00 2001 From: Olav Vitters Date: Thu, 23 Apr 2020 22:14:37 +0200 Subject: pylint fixes: variable names --- mgagnome | 178 ++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 86 insertions(+), 92 deletions(-) diff --git a/mgagnome b/mgagnome index 3687211..cb7cd10 100755 --- a/mgagnome +++ b/mgagnome @@ -5,7 +5,7 @@ # https://git.gnome.org/browse/sysadmin-bin/tree/ftpadmin # Written by Olav Vitters -from functools import wraps +from functools import wraps, lru_cache # basic modules: import os @@ -86,14 +86,14 @@ def retry(exceptions, tries=4, delay=3, backoff=2, logger=None): :param logger: logger to use. If None, print :type logger: logging.Logger instance """ - def deco_retry(f): + def deco_retry(func): - @wraps(f) - def f_retry(*args, **kwargs): + @wraps(func) + def func_retry(*args, **kwargs): mtries, mdelay = tries, delay while mtries > 1: try: - return f(*args, **kwargs) + return func(*args, **kwargs) except exceptions as exc: msg = "%s, Retrying in %d seconds..." % (str(exc), mdelay) if logger: @@ -103,9 +103,9 @@ def retry(exceptions, tries=4, delay=3, backoff=2, logger=None): time.sleep(mdelay) mtries -= 1 mdelay *= backoff - return f(*args, **kwargs) + return func(*args, **kwargs) - return f_retry # true decorator + return func_retry # true decorator return deco_retry @@ -440,18 +440,18 @@ class SpecFile(): # Convert %py_requires and %py_requires -d # - first figure out how a buildrequire is usually defined - mo = self.re_update_br.search(data) - br_pre = mo.group('pre') if mo and mo.group('pre') else "BuildRequires:\t" + 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: - mo = regexp.search(data) - if mo: + match = regexp.search(data) + if match: # Don't add multiple buildrequires change_to = "" brs_in_file = set() - for mo2 in self.re_update_br.finditer(data): - if mo2.group('br') in new_brs: - brs_in_file.add(mo2.group('br')) + for match2 in self.re_update_br.finditer(data): + if match2.group('br') in new_brs: + brs_in_file.add(match2.group('br')) for br in set(new_brs) - brs_in_file: change_to += ''.join((br_pre, br, "\n")) @@ -471,11 +471,11 @@ class SpecFile(): 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) - mo = re_variable.search(data) - if mo and mo.group('definition') and len(re_variable.findall(data)) == 1: - mo2 = re_spec.search(data) - if mo2: - data, nr = re_spec.subn(r'\g' + mo.group('definition').replace('\\', '\\\\') + r'\g', data) + 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, nr = re_spec.subn(r'\g' + match.group('definition').replace('\\', '\\\\') + r'\g', data) if nr: made_changes = True data, nr = re_variable.subn('', data) @@ -502,7 +502,7 @@ class SpecFile(): re_patch_valid = re.compile(r'^[ \t+]*\%patch(?P[0-9]*)(?:[ \t]+-p(?P[0-9]+))?(?:[ \t]+-b[ \t]+\S+)?$\n?', re.MULTILINE) re_prep_patches = re.compile(r'^\%setup[^\n]+$(?:' + re_patch_valid.pattern + r'|^#[^%\n]+\n|^[ \t]*(?:%{_bindir}/|%_bindir)?autoreconf[ \t][^\n]+$|\s)+\n\%build', re.MULTILINE) - give_patchnr = lambda mo: (mo.group('nr') if len(mo.group('nr')) == 1 else mo.group('nr').lstrip('0')) if mo.group('nr') else "0" + give_patchnr = lambda match: (match.group('nr') if len(match.group('nr')) == 1 else match.group('nr').lstrip('0')) if match.group('nr') else "0" # Make use of %apply_patches @@ -521,26 +521,21 @@ class SpecFile(): if self.uses_apply_patches: return made_changes, data -# print "WARNING: Patches, no %%apply_patches for %s" % self.module -# print self.module, patches -# print re_patch_header.findall(data) -# print re_patch_valid.findall(data) - # XXX -- apparently this is really inefficient with e.g. apache - mo2 = re_prep_patches.search(data) - patch_nrs_header = set((give_patchnr(mo) for mo in re_patch_header.finditer(data))) - patch_nrs_any = set((give_patchnr(mo) for mo in re_patch_any.finditer(data))) - patch_nrs_valid = set((give_patchnr(mo) for mo in re_patch_valid.finditer(mo2.group(0)))) if mo2 else set() + match2 = re_prep_patches.search(data) + patch_nrs_header = set((give_patchnr(match) for match in re_patch_header.finditer(data))) + patch_nrs_any = set((give_patchnr(match) for match in re_patch_any.finditer(data))) + patch_nrs_valid = set((give_patchnr(match) for match in re_patch_valid.finditer(match2.group(0)))) if match2 else set() if not patch_nrs_header: - # XXX -- werird, self.patches should've returned 0 already + # XXX -- weird, self.patches should've returned 0 already return made_changes, data if not patch_nrs_header == patch_nrs_any == patch_nrs_valid: print("NOTICE: Unable to automatically convert %s patches into %%autopatch (header/patch/valid: %s, %s, %s)" % (self.module, len(patch_nrs_header), len(patch_nrs_any), len(patch_nrs_valid)), file=sys.stderr) return made_changes, data - patch_flags = set([0 if mo.group('strip') is None else mo.group('strip') for mo in re_patch_valid.finditer(mo2.group(0))]) + patch_flags = set([0 if match.group('strip') is None else match.group('strip') for match in re_patch_valid.finditer(match2.group(0))]) if len(patch_flags) != 1: print("NOTICE: Unable to automatically convert patches into as different -p / strip levels used", file=sys.stderr) @@ -548,7 +543,7 @@ class SpecFile(): # Whoot, we can convert!! change_to = "%%autopatch -p%s\n" % list(patch_flags)[0] - prep, n1 = re_patch_valid.subn(change_to.replace('\\', '\\\\'), mo2.group(0), count=1) + prep, n1 = re_patch_valid.subn(change_to.replace('\\', '\\\\'), match2.group(0), count=1) prep, n2 = re_patch_valid.subn('', prep) if len(patch_nrs_valid) != n1 + n2: print("WARNING: Couldn't replace patches?!? Likely error in program logic", file=sys.stderr) @@ -559,13 +554,12 @@ class SpecFile(): return made_changes, data try: - change_to = data.replace(mo2.group(0), prep, 1) + change_to = data.replace(match2.group(0), prep, 1) self._changed_spec_file = True write_file(self.path, change_to) # Validate patches still apply if self.check_and_update_patches(check_only=True): - pass data = change_to self._changes['SILENT use autopatch'] = True made_changes = True @@ -630,7 +624,7 @@ class SpecFile(): with open(self.path, "r", encoding="utf-8") as f: data = f.read() - data, nr = self.re_update_patch.subn(lambda mo: '' if mo.group('nr') in nrs or (mo.group('nr').isdigit() and int(mo.group('nr')) in nrs) else mo.group(0), data) + data, nr = self.re_update_patch.subn(lambda match: '' if match.group('nr') in nrs or (match.group('nr').isdigit() and int(match.group('nr')) in nrs) else match.group(0), data) if not nr: print("ERROR: Could not remove patch nr %s!" % patchnr, file=sys.stderr) @@ -703,12 +697,14 @@ class SpecFile(): if 'FAILED' in line or 'File to patch:' in line: has_failed = True break - elif 'ignored' in line: + + if 'ignored' in line: has_ignored = True elif 'saving rejects to file' in line: has_failed = True break - elif 'Reversed (or previously applied) patch detected' in line: + + if 'Reversed (or previously applied) patch detected' in line: has_reversed = True if has_ignored and has_reversed and not has_failed: @@ -746,7 +742,7 @@ class SpecFile(): return True - def update_br(self, changes, force=False, change_description='update buildrequirements'): + def update_br(self, changes, change_description='update buildrequirements'): """Update buildrequirement""" # XXX - doesn't handle buildrequires with version numbers :-( @@ -756,19 +752,19 @@ class SpecFile(): data = f.read() # Change any "," in buildrequires into multiple lines - data, nr = self.re_update_br_unsplit.subn(lambda mo: ''.join((''.join((mo.group('pre'), mo2.group(0), mo.group('unsplitpost'))) for mo2 in self.re_br_part.finditer(mo.group('unsplit')) if mo.group(0).strip() != '')), data) + data, nr = self.re_update_br_unsplit.subn(lambda match: ''.join((''.join((match.group('pre'), match2.group(0), match.group('unsplitpost'))) for match2 in self.re_br_part.finditer(match.group('unsplit')) if match.group(0).strip() != '')), data) if nr: made_changes = True self._changes['SILENT one line per buildrequirement'] = True # Change =< and => operators into <= and >= - data, nr = self.re_update_br_fix_operator.subn(lambda mo: mo.group(0).replace('=>', '>=').replace('=<', '<') if self.re_update_br.match(mo.group(0).replace('=>', '>=').replace('=<', '<')) else mo.group(0), data) + data, nr = self.re_update_br_fix_operator.subn(lambda match: match.group(0).replace('=>', '>=').replace('=<', '<') if self.re_update_br.match(match.group(0).replace('=>', '>=').replace('=<', '<')) else match.group(0), data) if nr: made_changes = True self._changes['SILENT fix operator in buildrequires'] = True # Now update buildrequires if any - data, nr = self.re_update_br.subn(lambda mo: ''.join((mo.group('pre'), changes[mo.group('br')], mo.group('post'))) if mo.group('br') in changes else mo.group(0), data) + data, nr = self.re_update_br.subn(lambda match: ''.join((match.group('pre'), changes[match.group('br')], match.group('post'))) if match.group('br') in changes else match.group(0), data) if nr: made_changes = True @@ -948,7 +944,6 @@ class Upstream(): URL = "https://download.gnome.org/sources/" limit = None - _cache_versions = {} def __init__(self): urlopen = urllib.request.build_opener() @@ -969,21 +964,18 @@ class Upstream(): self.names = tarballs + @lru_cache(maxsize=4096) @classmethod def versions(cls, module): - # XXX - ugly - if module not in cls._cache_versions: - versions = None + versions = None - url = '%s%s/cache.json' % (cls.URL, module) - r = requests.get(url) - j = r.json() - if j is not None and len(j) > 2 and module in j[2]: - versions = j[2][module] + url = '%s%s/cache.json' % (cls.URL, module) + r = requests.get(url) + j = r.json() + if j is not None and len(j) > 2 and module in j[2]: + versions = j[2][module] - cls._cache_versions[module] = versions - - return cls._cache_versions[module] + return versions class Downstream(): re_file = re.compile(r'^(?P.*)[_-](?:(?P([0-9]+[\.])*[0-9]+)-)?(?P([0-9]+[\.\-])*[0-9]+)\.(?P(?:tar\.|diff\.)?[a-z][a-z0-9]*)$') @@ -1083,7 +1075,10 @@ class Downstream(): for key in provide_has_single: - # XXX - optimize memory usage somewhat + # Ignore some of the provides to optimize memory usage somewhat + # + # WARNING: This might need to be changed depending on how this + # function is used if '(' in key and not ')(' in key: continue @@ -1135,6 +1130,23 @@ class Downstream(): cmd = ['mgarepo', 'ci', '-m', changes] return subprocess.check_call(cmd, cwd=cwd) + @classmethod + def submit(cls, package, cwd=None): + if cwd is None: + cwd = cls.package_path(package) + + cmd = ['mgarepo', 'submit'] + if cls.DISTRO: + cmd.extend(('--define', 'section=core/updates_testing', '-t', cls.DISTRO)) + elif cls.SECTION: + cmd.extend(('--define', 'section={0}'.format(cls.SECTION))) + + # retry submission various times, could be that some dependencies are being built at the same time + @retry(subprocess.CalledProcessError, tries=10, delay=300, backoff=1.5) + def _submit(): + subprocess.check_call(cmd, cwd=cwd) + _submit() + def get_downstream_from_upstream(self, upstream, version): if upstream not in self.tarballs: @@ -1325,8 +1337,8 @@ def cmd_cleanup(options): # Downstream.co(path) continue - c2 = pysvn.Client() - stats = [stat for stat in c2.status(os.path.join(root, path, 'SOURCES'), depth=pysvn.depth.immediates) if stat.text_status == pysvn.wc_status_kind.unversioned and os.path.basename(stat.path) not in binaries] + vcs = pysvn.Client() + stats = [stat for stat in vcs.status(os.path.join(root, path, 'SOURCES'), depth=pysvn.depth.immediates) if stat.text_status == pysvn.wc_status_kind.unversioned and os.path.basename(stat.path) not in binaries] if stats: print(path) @@ -1341,9 +1353,10 @@ def cmd_cleanup(options): def cmd_ls(options): streams = join_streams(show_version=options.show_version, only_diff_version=options.diff) if options.sort: - SORT = dict(list(zip(options.sort.read().splitlines(), itertools.count()))) + # Sort packages on the line number in the file + sort_helper = dict(list(zip(options.sort.read().splitlines(), itertools.count()))) - streams = sorted(streams, key=lambda a: (SORT.get(a[1], 9999), a[0])) + streams = sorted(streams, key=lambda a: (sort_helper.get(a[1], 9999), a[0])) else: streams = sorted(streams) @@ -1434,7 +1447,7 @@ def cmd_clean_spec_multi(args): try: Downstream.co(package) except subprocess.CalledProcessError: - # XXX - ignoring packages which cannot be checked out + print('WARNING: Cannot check package %s. Skipping.' % package, file=sys.stderr) return False print('ERROR: Cannot find spec file for package %s' % package, file=sys.stderr) @@ -1547,10 +1560,10 @@ def cmd_clean_spec_multi(args): # This is only done as a last resort to avoid matching # e.g. gtk+3.0-devel --> pkgconfig(gtk+2.0) for prov in provides_alt: - for mo in re_prov_get_version.finditer(prov): + for match in re_prov_get_version.finditer(prov): if options.debug: - print("NOTICE: Heuristically adding version %s from provide %s" % (mo.group('version'), prov)) - versions.add(mo.group('version')) + print("NOTICE: Heuristically adding version %s from provide %s" % (match.group('version'), prov)) + versions.add(match.group('version')) check_for = keys['extra'](basereqs, versions) @@ -1683,8 +1696,8 @@ def cmd_new_release(options): if options.upstream: try: package = Downstream().get_downstream_from_upstream(pkg, options.version)[0] - except ValueError as e: - print("ERROR: %s" % e, file=sys.stderr) + except ValueError as exc: + print("ERROR: %s" % exc, file=sys.stderr) success = False continue else: @@ -1729,17 +1742,7 @@ def cmd_new_release(options): # Submit is optional if options.submit: - cmd = ['mgarepo', 'submit'] - if Downstream.DISTRO: - cmd.extend(('--define', 'section=core/updates_testing', '-t', Downstream.DISTRO)) - elif Downstream.SECTION: - cmd.extend(('--define', 'section={0}'.format(Downstream.SECTION))) - - # retry submission various times, could be that some dependencies are being built at the same time - @retry(subprocess.CalledProcessError, tries=10, delay=300, backoff=1.5) - def _submit(): - subprocess.check_call(cmd, cwd=cwd) - _submit() + Downstream.submit(package) except subprocess.CalledProcessError: success = False continue @@ -1811,22 +1814,13 @@ def cmd_package_new_version(options): # Submit is optional if options.submit: - cmd = ['mgarepo', 'submit'] - if Downstream.DISTRO: - cmd.extend(('--define', 'section=core/updates_testing', '-t', Downstream.DISTRO)) - elif Downstream.SECTION: - cmd.extend(('--define', 'section={0}'.format(Downstream.SECTION))) - - # retry submission various times, could be that some dependencies are being built at the same time - @retry(subprocess.CalledProcessError, tries=10, delay=300, backoff=1.5) - def _submit(): - subprocess.check_call(cmd, cwd=cwd) - _submit() + Downstream.submit(package) + except subprocess.CalledProcessError: sys.exit(1) def cmd_parse_ftp_release_list(options): - def _send_reply_mail(contents, orig_msg, to, packages=[], error=False): + def _send_reply_mail(contents, orig_msg, to, packages=None, error=False): """Send an reply email""" contents.seek(0) msg = MIMEText(contents.read(), _charset='utf-8') @@ -1876,8 +1870,8 @@ def cmd_parse_ftp_release_list(options): module = msg['X-Module-Name'] version = msg['X-Module-Version'] hexdigest = msg['X-Module-SHA256-tar.xz'] - except KeyError as e: - print("ERROR: %s" % e, file=stderr) + except KeyError as exc: + print("ERROR: %s" % exc, file=stderr) if options.mail: _send_reply_mail(stdout, msg, options.mail, error=True) @@ -1902,9 +1896,9 @@ def cmd_parse_ftp_release_list(options): if options.wait: # wait SLEEP_INITIAL after the message was sent secs = SLEEP_INITIAL - t = email.utils.parsedate_tz(msg['Date']) - if t is not None: - msg_time = email.utils.mktime_tz(t) + parsed_date = email.utils.parsedate_tz(msg['Date']) + if parsed_date is not None: + msg_time = email.utils.mktime_tz(parsed_date) secs = SLEEP_INITIAL - (time.time() - msg_time) if secs > 0: -- cgit v1.2.1