From: Thierry Parmentelat Date: Fri, 24 Jan 2025 15:25:23 +0000 (+0100) Subject: pkgs.py uses more fstrings, and hopefully more helpful error messages X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=6ef90aa3703b16d8dc9bfeef83677d47b855ce07;p=build.git pkgs.py uses more fstrings, and hopefully more helpful error messages --- diff --git a/pkgs.py b/pkgs.py index 1cc078a3..964a0cdb 100755 --- a/pkgs.py +++ b/pkgs.py @@ -108,25 +108,25 @@ class PkgsParser: return self.version <= version else: raise Exception( - 'Internal error - unexpected qualifier {}'.format(qualifier)) + f'Internal error - unexpected qualifier {qualifier}') m_comment = re.compile(r'\A\s*#') m_blank = re.compile(r'\A\s*\Z') m_ident = re.compile(r'\A'+re_ident+r'\Z') re_qualified = r'\s*' - re_qualified += r'(?P[+-]?)' - re_qualified += r'\s*' - re_qualified += r'(?P{re_ident})'.format(re_ident=re_ident) - re_qualified += r'\s*' - re_qualified += r'(?P>=|<=|=)' - re_qualified += r'\s*' - re_qualified += r'(?P{re_ident}[0-9]+)'.format(re_ident=re_ident) - re_qualified += r'\s*' - m_qualified = re.compile(r'\A{re_qualified}\Z'.format(re_qualified=re_qualified)) + re_qualified += rf'(?P[+-]?)' + re_qualified += rf'\s*' + re_qualified += rf'(?P{re_ident})' + re_qualified += rf'\s*' + re_qualified += rf'(?P=|<=|>=)' + re_qualified += rf'\s*' + re_qualified += rf'(?P{re_ident}[0-9]+)' + re_qualified += rf'\s*' + m_qualified = re.compile(rf'\A{re_qualified}\Z') re_old = '[a-z]+[+-][a-z]+[0-9]+' - m_old = re.compile(r'\A{}\Z'.format(re_old)) + m_old = re.compile(rf'\A{re_old}\Z') # returns a tuple (included, excluded) def parse(self, filename): @@ -145,7 +145,7 @@ class PkgsParser: ########## single ident if self.m_ident.match(left): if left not in known_keywords: - raise Exception("Unknown keyword {left}".format(**locals())) + raise Exception(f"Unknown keyword {left}") elif left == self.keyword: included += rights.split() else: @@ -153,9 +153,9 @@ class PkgsParser: if m: (plus_minus, kw, qual, fcdistro) = m.groups() if kw not in known_keywords: - raise Exception("Unknown keyword in {left}".format(**locals())) + raise Exception(f"Unknown keyword in {left}") if fcdistro not in known_fcdistros: - raise Exception('Unknown fcdistro {fcdistro}'.format(**locals())) + raise Exception(f'Unknown fcdistro {fcdistro}') # skip if another keyword if kw != self.keyword: continue # does this fcdistro match ? @@ -166,32 +166,31 @@ class PkgsParser: # skip if the qualifier does not fit if not self.match (qual, version): if self.options.verbose: - print('{filename}:{lineno}:qualifer {left} does not apply' - .format(**locals()), file=stderr) + print(f'{filename}:{lineno}:qualifer {left} does not apply', + file=stderr) continue # we're in, let's add (default) or remove (if plus_minus is minus) if plus_minus == '-': if self.options.verbose: - print('{filename}:{lineno}: from {left}, excluding {rights}' - .format(**locals()), file=stderr) + print(f'{filename}:{lineno}: from {left}, excluding {rights}', + file=stderr) excluded += rights.split() else: if self.options.verbose: - print('{filename}:{lineno}: from {left}, including {rights}'\ - .format(**locals()), file=stderr) + print(f'{filename}:{lineno}: from {left}, including {rights}, + file=stderr) included += rights.split() elif self.m_old.match(left): - raise Exception('Old-fashioned syntax not supported anymore {left}'.\ - format(**locals())) + raise Exception(f'Old-fashioned syntax not supported anymore {left}') else: - raise Exception('error in left expression {left}'.format(**locals())) + raise Exception(f'error in left expression {left}') except Exception as e: ok = False - print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr) + print(f"{filename}:{lineno}:syntax error: {e}", file=stderr) except Exception as exc: ok = False - print('Could not parse file', filename, exc, file=stderr) + print(f'Could not parse file {filename} {exc=}', file=stderr) return (ok, included, excluded) def run (self): @@ -225,7 +224,7 @@ def main (): parser = OptionParser(usage=usage) parser.add_option( '-a', '--arch', dest='arch', action='store', default=default_arch, - help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch)) + help=f'target arch, e.g. i386 or x86_64, default={default_arch}') parser.add_option( '-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro, help='fcdistro, e.g. f12 or centos5')