From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 60F1815808B for ; Sun, 27 Mar 2022 23:07:18 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A72E6E0978; Sun, 27 Mar 2022 23:07:15 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id CDF89E0984 for ; Sun, 27 Mar 2022 23:07:13 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id EABE5343691 for ; Sun, 27 Mar 2022 23:07:11 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 36EAE342 for ; Sun, 27 Mar 2022 23:07:10 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1648422395.7c8875c38b187fbd2e0fa5fc39bbb38c1588d0fb.sam@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/, lib/_emerge/ X-VCS-Repository: proj/portage X-VCS-Files: lib/_emerge/main.py lib/portage/__init__.py lib/portage/_global_updates.py lib/portage/checksum.py lib/portage/const.py lib/portage/data.py lib/portage/dispatch_conf.py lib/portage/eclass_cache.py lib/portage/exception.py lib/portage/getbinpkg.py lib/portage/glsa.py lib/portage/gpg.py lib/portage/gpkg.py lib/portage/localization.py lib/portage/mail.py X-VCS-Directories: lib/portage/ lib/_emerge/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 7c8875c38b187fbd2e0fa5fc39bbb38c1588d0fb X-VCS-Branch: master Date: Sun, 27 Mar 2022 23:07:10 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 83fd9922-14b7-4016-8201-fcda3a71f33c X-Archives-Hash: 171f0c0aa53bd74b8c462bb2aa79f03e commit: 7c8875c38b187fbd2e0fa5fc39bbb38c1588d0fb Author: Kenneth Raplee kennethraplee com> AuthorDate: Tue Mar 22 07:01:05 2022 +0000 Commit: Sam James gentoo org> CommitDate: Sun Mar 27 23:06:35 2022 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=7c8875c3 Begin modernizing the string usage These files are updated to use the newer f-string feature (String Interpolation) that has been added since Python 3.6. The benefits to using String Interpolation are: * Avoids incurring bugs from using Old Style formatting. * Makes it easier to understand how the string is being formatted. * Using embedded expressions into strings removes the need for string concatenation and will execute much faster for the same effect. Also, a few global string constants with Old Style formatting had their literals inlined to their respective function arguments while transitioning them be f-strings. When Python searches for variables, it looks them up in the local namespace first, which is the namespace specific a function or class method. If not found, then it searches in the global namespace. Removing the need for global strings will also improve performance a bit along the way. The Portage codebase as a whole still needs this kind of work. Signed-off-by: Kenneth Raplee kennethraplee.com> Signed-off-by: Sam James gentoo.org> lib/_emerge/main.py | 38 +++++------ lib/portage/__init__.py | 35 +++++----- lib/portage/_global_updates.py | 4 +- lib/portage/checksum.py | 15 ++-- lib/portage/const.py | 42 ++++++------ lib/portage/data.py | 2 +- lib/portage/dispatch_conf.py | 81 ++++++++++------------ lib/portage/eclass_cache.py | 4 +- lib/portage/exception.py | 6 +- lib/portage/getbinpkg.py | 89 +++++++++++------------- lib/portage/glsa.py | 151 +++++++++++++++++++---------------------- lib/portage/gpg.py | 4 +- lib/portage/gpkg.py | 18 +++-- lib/portage/localization.py | 8 +-- lib/portage/mail.py | 12 ++-- 15 files changed, 230 insertions(+), 279 deletions(-) diff --git a/lib/_emerge/main.py b/lib/_emerge/main.py index 8928f268d..f3ff02404 100644 --- a/lib/_emerge/main.py +++ b/lib/_emerge/main.py @@ -81,7 +81,7 @@ shortmapping = { COWSAY_MOO = r""" - Larry loves Gentoo (%s) + Larry loves Gentoo ({}) _______________________ < Have you mooed today? > @@ -97,7 +97,7 @@ COWSAY_MOO = r""" def multiple_actions(action1, action2): sys.stderr.write("\n!!! Multiple actions requested... Please choose one only.\n") - sys.stderr.write("!!! '%s' or '%s'\n\n" % (action1, action2)) + sys.stderr.write(f"!!! '{action1}' or '{action2}'\n\n") sys.exit(1) @@ -745,7 +745,7 @@ def parse_opts(tmpcmdline, silent=False): for action_opt in actions: parser.add_argument( - "--" + action_opt, + f"--{action_opt}", action="store_true", dest=action_opt.replace("-", "_"), default=False, @@ -759,7 +759,7 @@ def parse_opts(tmpcmdline, silent=False): ) for shortopt, longopt in shortmapping.items(): parser.add_argument( - "-" + shortopt, + f"-{shortopt}", action="store_true", dest=longopt.lstrip("--").replace("-", "_"), default=False, @@ -832,9 +832,9 @@ def parse_opts(tmpcmdline, silent=False): if myoptions.buildpkg_exclude: bad_atoms = _find_bad_atoms(myoptions.buildpkg_exclude, less_strict=True) if bad_atoms and not silent: + invalid_atoms = ",".join(bad_atoms) parser.error( - "Invalid Atom(s) in --buildpkg-exclude parameter: '%s'\n" - % (",".join(bad_atoms),) + f"Invalid Atom(s) in --buildpkg-exclude parameter: '{invalid_atoms}'\n" ) if myoptions.changed_deps is not None: @@ -1014,7 +1014,7 @@ def parse_opts(tmpcmdline, silent=False): backtrack = None if not silent: parser.error( - "Invalid --backtrack parameter: '%s'\n" % (myoptions.backtrack,) + f"Invalid --backtrack parameter: '{myoptions.backtrack}'\n" ) myoptions.backtrack = backtrack @@ -1032,7 +1032,7 @@ def parse_opts(tmpcmdline, silent=False): if deep is not True and deep < 0: deep = None if not silent: - parser.error("Invalid --deep parameter: '%s'\n" % (myoptions.deep,)) + parser.error(f"Invalid --deep parameter: '{myoptions.deep}'\n") myoptions.deep = deep @@ -1049,7 +1049,7 @@ def parse_opts(tmpcmdline, silent=False): if jobs is not True and jobs < 1: jobs = None if not silent: - parser.error("Invalid --jobs parameter: '%s'\n" % (myoptions.jobs,)) + parser.error(f"Invalid --jobs parameter: '{myoptions.jobs}'\n") myoptions.jobs = jobs @@ -1066,8 +1066,7 @@ def parse_opts(tmpcmdline, silent=False): load_average = None if not silent: parser.error( - "Invalid --load-average parameter: '%s'\n" - % (myoptions.load_average,) + f"Invalid --load-average parameter: '{myoptions.load_average}'\n" ) myoptions.load_average = load_average @@ -1082,8 +1081,7 @@ def parse_opts(tmpcmdline, silent=False): rebuilt_binaries_timestamp = 0 if not silent: parser.error( - "Invalid --rebuilt-binaries-timestamp parameter: '%s'\n" - % (myoptions.rebuilt_binaries_timestamp,) + f"Invalid --rebuilt-binaries-timestamp parameter: '{myoptions.rebuilt_binaries_timestamp}'\n" ) myoptions.rebuilt_binaries_timestamp = rebuilt_binaries_timestamp @@ -1093,14 +1091,12 @@ def parse_opts(tmpcmdline, silent=False): search_similarity = float(myoptions.search_similarity) except ValueError: parser.error( - "Invalid --search-similarity parameter " - "(not a number): '{}'\n".format(myoptions.search_similarity) + f"Invalid --search-similarity parameter (not a number): '{myoptions.search_similarity}'\n" ) if search_similarity < 0 or search_similarity > 100: parser.error( - "Invalid --search-similarity parameter " - "(not between 0 and 100): '{}'\n".format(myoptions.search_similarity) + f"Invalid --search-similarity parameter (not between 0 and 100): '{myoptions.search_similarity}'\n" ) myoptions.search_similarity = search_similarity @@ -1181,7 +1177,7 @@ def profile_check(trees, myaction): "--help, --info, --search, --sync, and --version." ) writemsg_level( - "".join("!!! %s\n" % l for l in textwrap.wrap(msg, 70)), + "".join(f"!!! {l}\n" for l in textwrap.wrap(msg, 70)), level=logging.ERROR, noiselevel=-1, ) @@ -1203,7 +1199,7 @@ def emerge_main(args=None): try: locale.setlocale(locale.LC_ALL, "") except locale.Error as e: - writemsg_level("setlocale: %s\n" % e, level=logging.WARN) + writemsg_level(f"setlocale: {e}\n", level=logging.WARN) # Disable color until we're sure that it should be enabled (after # EMERGE_DEFAULT_OPTS has been parsed). @@ -1234,7 +1230,7 @@ def emerge_main(args=None): emerge_help() return os.EX_OK if myaction == "moo": - print(COWSAY_MOO % platform.system()) + print(COWSAY_MOO.format(platform.system())) return os.EX_OK if myaction == "sync": # need to set this to True now in order for the repository config @@ -1311,7 +1307,7 @@ def emerge_main(args=None): try: locale.setlocale(locale.LC_ALL, "") except locale.Error as e: - writemsg_level("setlocale: %s\n" % e, level=logging.WARN) + writemsg_level(f"setlocale: {e}\n", level=logging.WARN) tmpcmdline = [] if "--ignore-default-opts" not in myopts: diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py index 3042de1aa..82e47d0ff 100644 --- a/lib/portage/__init__.py +++ b/lib/portage/__init__.py @@ -48,7 +48,7 @@ except ImportError as e: sys.stderr.write( "!!! gone wrong. Here is the information we got for this exception:\n" ) - sys.stderr.write(" " + str(e) + "\n\n") + sys.stderr.write(f" {e}\n\n") raise try: @@ -93,7 +93,7 @@ try: + "doebuild_environment,spawn,spawnebuild", "portage.package.ebuild.config:autouse,best_from_dict," + "check_config_instance,config", - "portage.package.ebuild.deprecated_profile_check:" + "deprecated_profile_check", + "portage.package.ebuild.deprecated_profile_check:deprecated_profile_check", "portage.package.ebuild.digestcheck:digestcheck", "portage.package.ebuild.digestgen:digestgen", "portage.package.ebuild.fetch:fetch", @@ -184,7 +184,7 @@ except ImportError as e: "!!! There is a README.RESCUE file that details the steps required to perform\n" ) sys.stderr.write("!!! a recovery of portage.\n") - sys.stderr.write(" " + str(e) + "\n\n") + sys.stderr.write(f" {e}\n\n") raise @@ -409,7 +409,7 @@ try: _selinux_merge = _unicode_module_wrapper(_selinux, encoding=_encodings["merge"]) except (ImportError, OSError) as e: if isinstance(e, OSError): - sys.stderr.write("!!! SELinux not loaded: %s\n" % str(e)) + sys.stderr.write(f"!!! SELinux not loaded: {e}\n") del e _selinux = None selinux = None @@ -482,10 +482,10 @@ def _shell_quote(s): """ if _shell_quote_re.search(s) is None: return s - for letter in '\\"$`': + for letter in r"\"$`": if letter in s: - s = s.replace(letter, "\\" + letter) - return '"%s"' % s + s = s.replace(letter, rf"\{letter}") + return f'"{s}"' bsd_chflags = None @@ -534,7 +534,7 @@ def abssymlink(symlink, target=None): mylink = os.readlink(symlink) if mylink[0] != "/": mydir = os.path.dirname(symlink) - mylink = mydir + "/" + mylink + mylink = f"{mydir}/{mylink}" return os.path.normpath(mylink) @@ -597,7 +597,7 @@ def _parse_eapi_ebuild_head(f): def _movefile(src, dest, **kwargs): """Calls movefile and raises a PortageException if an error occurs.""" if movefile(src, dest, **kwargs) is None: - raise portage.exception.PortageException("mv '%s' '%s'" % (src, dest)) + raise portage.exception.PortageException(f"mv '{src}' '{dest}'") auxdbkeys = ( @@ -726,12 +726,11 @@ if VERSION == "HEAD": BASH_BINARY, "-c", ( - "cd %s ; git describe --match 'portage-*' || exit $? ; " - + 'if [ -n "`git diff-index --name-only --diff-filter=M HEAD`" ] ; ' - + "then echo modified ; git rev-list --format=%%ct -n 1 HEAD ; fi ; " - + "exit 0" - ) - % _shell_quote(PORTAGE_BASE_PATH), + f"cd {_shell_quote(PORTAGE_BASE_PATH)} ; git describe --match 'portage-*' || exit $? ; " + 'if [ -n "`git diff-index --name-only --diff-filter=M HEAD`" ] ; ' + "then echo modified ; git rev-list --format=%%ct -n 1 HEAD ; fi ; " + "exit 0" + ), ] cmd = [ _unicode_encode(x, encoding=encoding, errors="strict") for x in cmd @@ -750,7 +749,7 @@ if VERSION == "HEAD": patchlevel = False if len(version_split) > 2: patchlevel = True - VERSION = "%s_p%s" % (VERSION, version_split[2]) + VERSION = f"{VERSION}_p{version_split[2]}" if len(output_lines) > 1 and output_lines[1] == "modified": head_timestamp = None if len(output_lines) > 3: @@ -765,8 +764,8 @@ if VERSION == "HEAD": ): timestamp = timestamp - head_timestamp if not patchlevel: - VERSION = "%s_p0" % (VERSION,) - VERSION = "%s_p%d" % (VERSION, timestamp) + VERSION = f"{VERSION}_p0" + VERSION = f"{VERSION}_p{timestamp}" return VERSION VERSION = "HEAD" return VERSION diff --git a/lib/portage/_global_updates.py b/lib/portage/_global_updates.py index 80728fb43..a17ee861d 100644 --- a/lib/portage/_global_updates.py +++ b/lib/portage/_global_updates.py @@ -118,14 +118,14 @@ def _do_global_updates(trees, prev_mtimes, quiet=False, if_mtime_changed=True): myupd.extend(valid_updates) if not quiet: writemsg_stdout(bold(mykey)) - writemsg_stdout(len(valid_updates) * "." + "\n") + writemsg_stdout(f"{len(valid_updates) * '.'}\n") if len(errors) == 0: # Update our internal mtime since we # processed all of our directives. timestamps[mykey] = mystat[stat.ST_MTIME] else: for msg in errors: - writemsg("%s\n" % msg, noiselevel=-1) + writemsg(f"{msg}\n", noiselevel=-1) if myupd: retupd = True diff --git a/lib/portage/checksum.py b/lib/portage/checksum.py index c19a50df9..3b5a10d24 100644 --- a/lib/portage/checksum.py +++ b/lib/portage/checksum.py @@ -44,7 +44,7 @@ def _open_file(filename): _unicode_encode(filename, encoding=_encodings["fs"], errors="strict"), "rb" ) except IOError as e: - func_call = "open('%s')" % _unicode_decode(filename) + func_call = f"open('{_unicode_decode(filename)}')" if e.errno == errno.EPERM: raise portage.exception.OperationNotPermitted(func_call) elif e.errno == errno.EACCES: @@ -525,12 +525,11 @@ def verify_all(filename, mydict, calc_prelink=0, strict=0): if mydict[x] != myhash: if strict: raise portage.exception.DigestException( - ("Failed to verify '$(file)s' on " + "checksum type '%(type)s'") - % {"file": filename, "type": x} + f"Failed to verify '{filename}' on checksum type '{x}'" ) else: file_is_ok = False - reason = (("Failed on %s verification" % x), myhash, mydict[x]) + reason = (f"Failed on {x} verification", myhash, mydict[x]) break return file_is_ok, reason @@ -578,8 +577,7 @@ def perform_checksum(filename, hashname="MD5", calc_prelink=0): try: if hashname not in hashfunc_keys: raise portage.exception.DigestException( - hashname - + " hash function not available (needs dev-python/pycrypto)" + f"{hashname} hash function not available (needs dev-python/pycrypto)" ) myhash, mysize = hashfunc_map[hashname].checksum_file(myfilename) except (OSError, IOError) as e: @@ -618,8 +616,7 @@ def perform_multiple_checksums(filename, hashes=["MD5"], calc_prelink=0): for x in hashes: if x not in hashfunc_keys: raise portage.exception.DigestException( - x - + " hash function not available (needs dev-python/pycrypto or >=dev-lang/python-2.5)" + f"{x} hash function not available (needs dev-python/pycrypto)" ) rVal[x] = perform_checksum(filename, x, calc_prelink)[0] return rVal @@ -638,6 +635,6 @@ def checksum_str(data, hashname="MD5"): """ if hashname not in hashfunc_keys: raise portage.exception.DigestException( - hashname + " hash function not available (needs dev-python/pycrypto)" + f"{hashname} hash function not available (needs dev-python/pycrypto)" ) return hashfunc_map[hashname].checksum_str(data) diff --git a/lib/portage/const.py b/lib/portage/const.py index 9e6f9311d..98eb7e4f2 100644 --- a/lib/portage/const.py +++ b/lib/portage/const.py @@ -29,32 +29,32 @@ import sys # variables used with config_root (these need to be relative) USER_CONFIG_PATH = "etc/portage" -BINREPOS_CONF_FILE = USER_CONFIG_PATH + "/binrepos.conf" -MAKE_CONF_FILE = USER_CONFIG_PATH + "/make.conf" -MODULES_FILE_PATH = USER_CONFIG_PATH + "/modules" -CUSTOM_PROFILE_PATH = USER_CONFIG_PATH + "/profile" -USER_VIRTUALS_FILE = USER_CONFIG_PATH + "/virtuals" -EBUILD_SH_ENV_FILE = USER_CONFIG_PATH + "/bashrc" -EBUILD_SH_ENV_DIR = USER_CONFIG_PATH + "/env" -CUSTOM_MIRRORS_FILE = USER_CONFIG_PATH + "/mirrors" -COLOR_MAP_FILE = USER_CONFIG_PATH + "/color.map" -PROFILE_PATH = USER_CONFIG_PATH + "/make.profile" -MAKE_DEFAULTS_FILE = PROFILE_PATH + "/make.defaults" # FIXME: not used -DEPRECATED_PROFILE_FILE = PROFILE_PATH + "/deprecated" +BINREPOS_CONF_FILE = f"{USER_CONFIG_PATH}/binrepos.conf" +MAKE_CONF_FILE = f"{USER_CONFIG_PATH}/make.conf" +MODULES_FILE_PATH = f"{USER_CONFIG_PATH}/modules" +CUSTOM_PROFILE_PATH = f"{USER_CONFIG_PATH}/profile" +USER_VIRTUALS_FILE = f"{USER_CONFIG_PATH}/virtuals" +EBUILD_SH_ENV_FILE = f"{USER_CONFIG_PATH}/bashrc" +EBUILD_SH_ENV_DIR = f"{USER_CONFIG_PATH}/env" +CUSTOM_MIRRORS_FILE = f"{USER_CONFIG_PATH}/mirrors" +COLOR_MAP_FILE = f"{USER_CONFIG_PATH}/color.map" +PROFILE_PATH = f"{USER_CONFIG_PATH}/make.profile" +MAKE_DEFAULTS_FILE = f"{PROFILE_PATH}/make.defaults" # FIXME: not used +DEPRECATED_PROFILE_FILE = f"{PROFILE_PATH}/deprecated" # variables used with targetroot (these need to be absolute, but not # have a leading '/' since they are used directly with os.path.join on EROOT) VDB_PATH = "var/db/pkg" CACHE_PATH = "var/cache/edb" PRIVATE_PATH = "var/lib/portage" -WORLD_FILE = PRIVATE_PATH + "/world" -WORLD_SETS_FILE = PRIVATE_PATH + "/world_sets" -CONFIG_MEMORY_FILE = PRIVATE_PATH + "/config" +WORLD_FILE = f"{PRIVATE_PATH}/world" +WORLD_SETS_FILE = f"{PRIVATE_PATH}/world_sets" +CONFIG_MEMORY_FILE = f"{PRIVATE_PATH}/config" NEWS_LIB_PATH = "var/lib/gentoo" # these variables get EPREFIX prepended automagically when they are # translated into their lowercase variants -DEPCACHE_PATH = "/var/cache/edb/dep" +DEPCACHE_PATH = f"/{CACHE_PATH}/dep" GLOBAL_CONFIG_PATH = "/usr/share/portage/config" # these variables are not used with target_root or config_root @@ -64,11 +64,11 @@ GLOBAL_CONFIG_PATH = "/usr/share/portage/config" # fmt:off PORTAGE_BASE_PATH = os.path.join(os.sep, os.sep.join(os.path.realpath(__file__.rstrip("co")).split(os.sep)[:-3])) # fmt:on -PORTAGE_BIN_PATH = PORTAGE_BASE_PATH + "/bin" +PORTAGE_BIN_PATH = f"{PORTAGE_BASE_PATH}/bin" PORTAGE_PYM_PATH = os.path.realpath(os.path.join(__file__, "../..")) -LOCALE_DATA_PATH = PORTAGE_BASE_PATH + "/locale" # FIXME: not used -EBUILD_SH_BINARY = PORTAGE_BIN_PATH + "/ebuild.sh" -MISC_SH_BINARY = PORTAGE_BIN_PATH + "/misc-functions.sh" +LOCALE_DATA_PATH = f"{PORTAGE_BASE_PATH}/locale" # FIXME: not used +EBUILD_SH_BINARY = f"{PORTAGE_BIN_PATH}/ebuild.sh" +MISC_SH_BINARY = f"{PORTAGE_BIN_PATH}/misc-functions.sh" SANDBOX_BINARY = "/usr/bin/sandbox" FAKEROOT_BINARY = "/usr/bin/fakeroot" BASH_BINARY = "/bin/bash" @@ -78,7 +78,7 @@ PRELINK_BINARY = "/usr/sbin/prelink" INVALID_ENV_FILE = "/etc/spork/is/not/valid/profile.env" MERGING_IDENTIFIER = "-MERGING-" REPO_NAME_FILE = "repo_name" -REPO_NAME_LOC = "profiles" + "/" + REPO_NAME_FILE +REPO_NAME_LOC = f"profiles/{REPO_NAME_FILE}" PORTAGE_PACKAGE_ATOM = "sys-apps/portage" LIBC_PACKAGE_ATOM = "virtual/libc" diff --git a/lib/portage/data.py b/lib/portage/data.py index 09a4dd079..e1457c566 100644 --- a/lib/portage/data.py +++ b/lib/portage/data.py @@ -78,7 +78,7 @@ def _target_root(): # Handle either empty or unset ROOT. root = os.sep root = portage.util.normalize_path(root) - return root.rstrip(os.sep) + os.sep + return f"{root.rstrip(os.sep)}{os.sep}" def portage_group_warning(): diff --git a/lib/portage/dispatch_conf.py b/lib/portage/dispatch_conf.py index fa9780937..6c6036c4e 100644 --- a/lib/portage/dispatch_conf.py +++ b/lib/portage/dispatch_conf.py @@ -25,9 +25,6 @@ RCS_BRANCH = "1.1.1" RCS_LOCK = "rcs -ko -M -l" RCS_PUT = 'ci -t-"Archived config file." -m"dispatch-conf update."' RCS_GET = "co" -RCS_MERGE = "rcsmerge -p -r" + RCS_BRANCH + " '%s' > '%s'" - -DIFF3_MERGE = "diff3 -mE '%s' '%s' '%s' > '%s'" _ARCHIVE_ROTATE_MAX = 9 @@ -75,18 +72,18 @@ def diff_mixed(func, file1, file2): if tempdir is None: tempdir = tempfile.mkdtemp() - diff_files[i] = os.path.join(tempdir, "%d" % i) + diff_files[i] = os.path.join(tempdir, f"{i}") if st is None: content = "/dev/null\n" elif stat.S_ISLNK(st.st_mode): link_dest = os.readlink(files[i]) - content = "SYM: %s -> %s\n" % (file1, link_dest) + content = f"SYM: {file1} -> {link_dest}\n" elif stat.S_ISDIR(st.st_mode): - content = "DIR: %s\n" % (file1,) + content = f"DIR: {file1}\n" elif stat.S_ISFIFO(st.st_mode): - content = "FIF: %s\n" % (file1,) + content = f"FIF: {file1}\n" else: - content = "DEV: %s\n" % (file1,) + content = f"DEV: {file1}\n" with io.open( diff_files[i], mode="w", encoding=_encodings["stdio"] ) as f: @@ -124,10 +121,7 @@ def read_config(mandatory_opts): loader = KeyValuePairFileLoader(config_path, None) opts, _errors = loader.load() if not opts: - print( - _("dispatch-conf: Error reading {}; fatal").format(config_path), - file=sys.stderr, - ) + print(_(f"dispatch-conf: Error reading {config_path}; fatal"), file=sys.stderr) sys.exit(1) # Handle quote removal here, since KeyValuePairFileLoader doesn't do that. @@ -143,9 +137,8 @@ def read_config(mandatory_opts): else: print( _( - 'dispatch-conf: Missing option "%s" in /etc/dispatch-conf.conf; fatal' - ) - % (key,), + f'dispatch-conf: Missing option "{key}" in /etc/dispatch-conf.conf; fatal' + ), file=sys.stderr, ) @@ -160,8 +153,9 @@ def read_config(mandatory_opts): os.chmod(opts["archive-dir"], 0o700) elif not os.path.isdir(opts["archive-dir"]): print( - _("dispatch-conf: Config archive dir [%s] must exist; fatal") - % (opts["archive-dir"],), + _( + rf"""dispatch-conf: Config archive dir [{opts["archive-dir"]}] must exist; fatal""" + ), file=sys.stderr, ) sys.exit(1) @@ -195,11 +189,7 @@ def _archive_copy(src_st, src_path, dest_path): shutil.copy2(src_path, dest_path) except EnvironmentError as e: portage.util.writemsg( - _( - "dispatch-conf: Error copying %(src_path)s to " - "%(dest_path)s: %(reason)s\n" - ) - % {"src_path": src_path, "dest_path": dest_path, "reason": e}, + f"dispatch-conf: Error copying {src_path} to {dest_path}: {e}\n", noiselevel=-1, ) @@ -226,9 +216,9 @@ def rcs_archive(archive, curconf, newconf, mrgconf): ): _archive_copy(curconf_st, curconf, archive) - if os.path.lexists(archive + ",v"): - os.system(RCS_LOCK + " " + archive) - os.system(RCS_PUT + " " + archive) + if os.path.lexists(f"{archive},v"): + os.system(f"{RCS_LOCK} {archive}") + os.system(f"{RCS_PUT} {archive}") ret = 0 mystat = None @@ -241,17 +231,17 @@ def rcs_archive(archive, curconf, newconf, mrgconf): if mystat is not None and ( stat.S_ISREG(mystat.st_mode) or stat.S_ISLNK(mystat.st_mode) ): - os.system(RCS_GET + " -r" + RCS_BRANCH + " " + archive) + os.system(f"{RCS_GET} -r{RCS_BRANCH} {archive}") has_branch = os.path.lexists(archive) if has_branch: - os.rename(archive, archive + ".dist") + os.rename(archive, f"{archive}.dist") _archive_copy(mystat, newconf, archive) if has_branch: if mrgconf and os.path.isfile(archive) and os.path.isfile(mrgconf): # This puts the results of the merge into mrgconf. - ret = os.system(RCS_MERGE % (archive, mrgconf)) + ret = os.system(f"rcsmerge -p -r{RCS_BRANCH} '{archive}' > '{mrgconf}'") os.chmod(mrgconf, mystat.st_mode) os.chown(mrgconf, mystat.st_uid, mystat.st_gid) os.rename(archive, archive + ".dist.new") @@ -273,8 +263,7 @@ def _file_archive_rotate(archive): for max_suf, max_st, max_path in ( (suf, os.lstat(path), path) for suf, path in ( - (suf, "%s.%s" % (archive, suf)) - for suf in range(1, _ARCHIVE_ROTATE_MAX + 1) + (suf, f"{archive}.{suf}") for suf in range(1, _ARCHIVE_ROTATE_MAX + 1) ) ): pass @@ -290,7 +279,7 @@ def _file_archive_rotate(archive): # Removing a directory might destroy something important, # so rename it instead. head, tail = os.path.split(archive) - placeholder = tempfile.NamedTemporaryFile(prefix="%s." % tail, dir=head) + placeholder = tempfile.NamedTemporaryFile(prefix=f"{tail}.", dir=head) placeholder.close() os.rename(max_path, placeholder.name) else: @@ -300,9 +289,9 @@ def _file_archive_rotate(archive): max_suf -= 1 for suf in range(max_suf + 1, 1, -1): - os.rename("%s.%s" % (archive, suf - 1), "%s.%s" % (archive, suf)) + os.rename(f"{archive}.{suf - 1}", f"{archive}.{suf}") - os.rename(archive, "%s.1" % (archive,)) + os.rename(archive, f"{archive}.1") def _file_archive_ensure_dir(parent_dir): @@ -347,7 +336,7 @@ def file_archive(archive, curconf, newconf, mrgconf): # Archive the current config file if it isn't already saved if ( os.path.lexists(archive) - and len(diffstatusoutput_mixed("diff -aq '%s' '%s'", curconf, archive)[1]) != 0 + and len(diffstatusoutput_mixed(f"diff -aq '{curconf}' '{archive}'")[1]) != 0 ): _file_archive_rotate(archive) @@ -372,7 +361,7 @@ def file_archive(archive, curconf, newconf, mrgconf): stat.S_ISREG(mystat.st_mode) or stat.S_ISLNK(mystat.st_mode) ): # Save off new config file in the archive dir with .dist.new suffix - newconf_archive = archive + ".dist.new" + newconf_archive = f"{archive}.dist.new" if os.path.isdir(newconf_archive) and not os.path.islink(newconf_archive): _file_archive_rotate(newconf_archive) _archive_copy(mystat, newconf, newconf_archive) @@ -382,11 +371,11 @@ def file_archive(archive, curconf, newconf, mrgconf): mrgconf and os.path.isfile(curconf) and os.path.isfile(newconf) - and os.path.isfile(archive + ".dist") + and os.path.isfile(f"{archive}.dist") ): # This puts the results of the merge into mrgconf. ret = os.system( - DIFF3_MERGE % (curconf, archive + ".dist", newconf, mrgconf) + f"diff3 -mE '{curconf}' '{archive}.dist' '{newconf}' > '{mrgconf}'" ) os.chmod(mrgconf, mystat.st_mode) os.chown(mrgconf, mystat.st_uid, mystat.st_gid) @@ -397,24 +386,24 @@ def file_archive(archive, curconf, newconf, mrgconf): def rcs_archive_post_process(archive): """Check in the archive file with the .dist.new suffix on the branch and remove the one with the .dist suffix.""" - os.rename(archive + ".dist.new", archive) - if os.path.lexists(archive + ".dist"): + os.rename(f"{archive}.dist.new", archive) + if os.path.lexists(f"{archive}.dist"): # Commit the last-distributed version onto the branch. - os.system(RCS_LOCK + RCS_BRANCH + " " + archive) - os.system(RCS_PUT + " -r" + RCS_BRANCH + " " + archive) - os.unlink(archive + ".dist") + os.system(f"{RCS_LOCK}{RCS_BRANCH} {archive}") + os.system(f"{RCS_PUT} -r{RCS_BRANCH} {archive}") + os.unlink(f"{archive}.dist") else: # Forcefully commit the last-distributed version onto the branch. - os.system(RCS_PUT + " -f -r" + RCS_BRANCH + " " + archive) + os.system(f"{RCS_PUT} -f -r{RCS_BRANCH} {archive}") def file_archive_post_process(archive): """Rename the archive file with the .dist.new suffix to a .dist suffix""" - if os.path.lexists(archive + ".dist.new"): - dest = "%s.dist" % archive + if os.path.lexists(f"{archive}.dist.new"): + dest = f"{archive}.dist" if os.path.isdir(dest) and not os.path.islink(dest): _file_archive_rotate(dest) - os.rename(archive + ".dist.new", dest) + os.rename(f"{archive}.dist.new", dest) def perform_conf_update_hooks(kind, conf): diff --git a/lib/portage/eclass_cache.py b/lib/portage/eclass_cache.py index c89b70922..f1729326d 100644 --- a/lib/portage/eclass_cache.py +++ b/lib/portage/eclass_cache.py @@ -47,7 +47,7 @@ class hashed_path: return val def __repr__(self): - return "" % (self.location,) + return f"" class cache: @@ -98,7 +98,7 @@ class cache: that have the same name. """ if not isinstance(other, self.__class__): - raise TypeError("expected type %s, got %s" % (self.__class__, type(other))) + raise TypeError(f"expected type {self.__class__}, got {type(other)}") self.porttrees = self.porttrees + other.porttrees self.eclasses.update(other.eclasses) self._eclass_locations.update(other._eclass_locations) diff --git a/lib/portage/exception.py b/lib/portage/exception.py index ff40e463b..360febcc8 100644 --- a/lib/portage/exception.py +++ b/lib/portage/exception.py @@ -217,10 +217,10 @@ class UnsupportedAPIException(PortagePackageException): eapi = str(eapi) eapi = eapi.lstrip("-") msg = _( - "Unable to do any operations on '%(cpv)s', since " + f"Unable to do any operations on '{self.cpv}', since " "its EAPI is higher than this portage version's. Please upgrade" - " to a portage version that supports EAPI '%(eapi)s'." - ) % {"cpv": self.cpv, "eapi": eapi} + f" to a portage version that supports EAPI '{eapi}'." + ) return _unicode_decode(msg, encoding=_encodings["content"], errors="replace") diff --git a/lib/portage/getbinpkg.py b/lib/portage/getbinpkg.py index 6aa8f1de1..1e89119fb 100644 --- a/lib/portage/getbinpkg.py +++ b/lib/portage/getbinpkg.py @@ -172,12 +172,10 @@ def create_conn(baseurl, conn=None): except AttributeError: # Python 2 encodebytes = base64.encodestring - http_headers = { - b"Authorization": "Basic %s" - % encodebytes(_unicode_encode("%s:%s" % (username, password))).replace( - b"\012", b"" - ), - } + unicode_bytes = encodebytes(_unicode_encode(f"{username}:{password}")).replace( + b"\012", b"" + ) + http_headers = {b"Authorization": f"Basic {unicode_bytes}"} if not conn: if protocol == "https": @@ -204,9 +202,10 @@ def create_conn(baseurl, conn=None): conn.login(username, password) else: sys.stderr.write( - colorize("WARN", _(" * No password provided for username")) - + " '%s'" % (username,) - + "\n\n" + colorize( + "WARN", + _(f" * No password provided for username '{username}'\n\n"), + ) ) conn.login(username) conn.set_pasv(passive) @@ -245,15 +244,12 @@ def make_ftp_request(conn, address, rest=None, dest=None): conn.voidcmd("TYPE I") fsize = conn.size(address) - if (rest != None) and (rest < 0): + retr_address = f"RETR {address}" + if rest and rest < 0: rest = fsize + int(rest) - if rest < 0: - rest = 0 - - if rest != None: - mysocket = conn.transfercmd("RETR %s" % str(address), rest) + mysocket = conn.transfercmd(retr_address, rest) else: - mysocket = conn.transfercmd("RETR %s" % str(address)) + mysocket = conn.transfercmd(retr_address) mydata = "" while 1: @@ -262,7 +258,7 @@ def make_ftp_request(conn, address, rest=None, dest=None): if dest: dest.write(somedata) else: - mydata = mydata + somedata + mydata = f"{mydata}{somedata}" else: break @@ -302,7 +298,7 @@ def make_http_request(conn, address, _params={}, headers={}, dest=None): except SystemExit as e: raise except Exception as e: - return None, None, "Server request failed: %s" % str(e) + return None, None, f"Server request failed: {e}" response = conn.getresponse() rc = response.status @@ -315,15 +311,11 @@ def make_http_request(conn, address, _params={}, headers={}, dest=None): if parts[0] == "Location": if rc == 301: sys.stderr.write( - colorize("BAD", _("Location has moved: ")) - + str(parts[1]) - + "\n" + f"{colorize('BAD', _('Location has moved: '))}{parts[1]}\n" ) if rc == 302: sys.stderr.write( - colorize("BAD", _("Location has temporarily moved: ")) - + str(parts[1]) - + "\n" + f"{colorize('BAD', _('Location has temporarily moved: '))}{parts[1]}\n" ) address = parts[1] break @@ -332,8 +324,7 @@ def make_http_request(conn, address, _params={}, headers={}, dest=None): return ( None, rc, - "Server did not respond successfully (%s: %s)" - % (str(response.status), str(response.reason)), + f"Server did not respond successfully ({response.status}: {response.reason})", ) if dest: @@ -408,7 +399,7 @@ def dir_get_list(baseurl, conn=None): if not address.endswith("/"): # http servers can return a 400 error here # if the address doesn't end with a slash. - address += "/" + address = f"{address}/" page, rc, msg = make_http_request(conn, address, params, headers) if page: @@ -461,7 +452,7 @@ def file_get_metadata(baseurl, conn=None, chunk_size=3000): conn, protocol, address, params, headers = create_conn(baseurl, conn) if protocol in ["http", "https"]: - headers["Range"] = "bytes=-%s" % str(chunk_size) + headers["Range"] = f"bytes=-{chunk_size}" data, _x, _x = make_http_request(conn, address, params, headers) elif protocol in ["ftp"]: data, _x, _x = make_ftp_request(conn, address, -chunk_size) @@ -473,7 +464,7 @@ def file_get_metadata(baseurl, conn=None, chunk_size=3000): finally: f.close() else: - raise TypeError(_("Unknown protocol. '%s'") % protocol) + raise TypeError(_(f"Unknown protocol. '{protocol}'")) if data: xpaksize = portage.xpak.decodeint(data[-8:-4]) @@ -523,14 +514,14 @@ def file_get( if "DISTDIR" not in variables: if dest is None: raise portage.exception.MissingParameter( - _("%s is missing required '%s' key") % ("fcmd_vars", "DISTDIR") + _("fcmd_vars is missing required 'DISTDIR' key") ) variables["DISTDIR"] = dest if "URI" not in variables: if baseurl is None: raise portage.exception.MissingParameter( - _("%s is missing required '%s' key") % ("fcmd_vars", "URI") + _("fcmd_vars is missing required 'URI' key") ) variables["URI"] = baseurl @@ -576,7 +567,7 @@ def file_get_lib(baseurl, dest, conn=None): conn, protocol, address, params, headers = create_conn(baseurl, conn) - sys.stderr.write("Fetching '" + str(os.path.basename(address)) + "'\n") + sys.stderr.write(f"Fetching '{os.path.basename(address)}'\n") if protocol in ["http", "https"]: data, rc, _msg = make_http_request(conn, address, params, headers, dest=dest) elif protocol in ["ftp"]: @@ -673,7 +664,7 @@ def dir_get_metadata( if not os.access(cache_path, os.W_OK): sys.stderr.write(_("!!! Unable to write binary metadata to disk!\n")) - sys.stderr.write(_("!!! Permission denied: '%s'\n") % cache_path) + sys.stderr.write(_(f"!!! Permission denied: '{cache_path}'\n")) return metadata[baseurl]["data"] import portage.exception @@ -681,10 +672,8 @@ def dir_get_metadata( try: filelist = dir_get_list(baseurl, conn) except portage.exception.PortageException as e: - sys.stderr.write( - _("!!! Error connecting to '%s'.\n") % _hide_url_passwd(baseurl) - ) - sys.stderr.write("!!! %s\n" % str(e)) + sys.stderr.write(_(f"!!! Error connecting to '{_hide_url_passwd(baseurl)}'.\n")) + sys.stderr.write(f"!!! {e}\n") del e return metadata[baseurl]["data"] tbz2list = match_in_array(filelist, suffix=".tbz2") @@ -743,10 +732,8 @@ def dir_get_metadata( except SystemExit as e: raise except Exception as e: - sys.stderr.write( - _("!!! Failed to read data from index: ") + str(mfile) + "\n" - ) - sys.stderr.write("!!! %s" % str(e)) + sys.stderr.write(f"!!! Failed to read data from index: {mfile}\n") + sys.stderr.write(f"!!! {e}") sys.stderr.flush() try: metadatafile = open( @@ -761,7 +748,7 @@ def dir_get_metadata( raise except Exception as e: sys.stderr.write(_("!!! Failed to write binary metadata to disk!\n")) - sys.stderr.write("!!! %s\n" % str(e)) + sys.stderr.write(f"!!! {e}\n") sys.stderr.flush() break # We may have metadata... now we run through the tbz2 list and check. @@ -784,10 +771,14 @@ def dir_get_metadata( def display(self): self.out.write( - "\r" - + colorize("WARN", _("cache miss: '") + str(self.misses) + "'") - + " --- " - + colorize("GOOD", _("cache hit: '") + str(self.hits) + "'") + "".join( + ( + "\r", + colorize("WARN", _(f"cache miss: '{self.misses}'")), + " --- ", + colorize("GOOD", _(f"cache hit: '{self.hits}'")), + ) + ) ) self.out.flush() @@ -829,9 +820,7 @@ def dir_get_metadata( metadata[baseurl]["data"][x] = make_metadata_dict(myid) elif verbose: sys.stderr.write( - colorize("BAD", _("!!! Failed to retrieve metadata on: ")) - + str(x) - + "\n" + f"{colorize('BAD', _('!!! Failed to retrieve metadata on: '))}{x}\n" ) sys.stderr.flush() else: @@ -946,7 +935,7 @@ class PackageIndex: def _writepkgindex(self, pkgfile, items): for k, v in items: - pkgfile.write("%s: %s\n" % (self._write_translation_map.get(k, k), v)) + pkgfile.write(f"{self._write_translation_map.get(k, k)}: {v}\n") pkgfile.write("\n") def read(self, pkgfile): diff --git a/lib/portage/glsa.py b/lib/portage/glsa.py index 19f226db1..05de3ade6 100644 --- a/lib/portage/glsa.py +++ b/lib/portage/glsa.py @@ -76,26 +76,29 @@ def wrap(text, width, caption=""): words = text.split() indentLevel = len(caption) + 1 - for w in words: - if line != "" and line[-1] == "\n": - rValue += line + for word in words: + if line and line[-1] == "\n": + rValue = f"{rValue}{line}" line = " " * indentLevel - if len(line) + len(w.replace(NEWLINE_ESCAPE, "")) + 1 > width: - rValue += line + "\n" - line = " " * indentLevel + w.replace(NEWLINE_ESCAPE, "\n") - elif w.find(NEWLINE_ESCAPE) >= 0: + if len(line) + len(word.replace(NEWLINE_ESCAPE, "")) + 1 > width: + rValue = f"{rValue}{line}\n" + escaped_word = word.replace(NEWLINE_ESCAPE, "\n") + line = f"{' ' * indentLevel}{escaped_word}" + elif word.find(NEWLINE_ESCAPE) >= 0: + escaped_word = word.replace(NEWLINE_ESCAPE, "\n") + whitespace = "" if len(line.strip()) > 0: - rValue += line + " " + w.replace(NEWLINE_ESCAPE, "\n") - else: - rValue += line + w.replace(NEWLINE_ESCAPE, "\n") + whitespace = " " + rValue = f"{rValue}{line}{whitespace}{escaped_word}" line = " " * indentLevel else: + whitespace = "" if len(line.strip()) > 0: - line += " " + w - else: - line += w + whitespace = " " + line = f"{line}{whitespace}{word}" if len(line) > 0: - rValue += line.replace(NEWLINE_ESCAPE, "\n") + escaped_line = line.replace(NEWLINE_ESCAPE, "\n") + rValue = f"{rValue}{escaped_line}" rValue = rValue.replace(SPACE_ESCAPE, " ") return rValue @@ -184,7 +187,7 @@ def getText(node, format, textfd=None): # pylint: disable=redefined-builtin returnNone = True if format in ["strip", "keep"]: if node.nodeName in ["uri", "mail"]: - textfd.write(node.childNodes[0].data + ": " + node.getAttribute("link")) + textfd.write(f"{node.childNodes[0].data}:{node.getAttribute('link')}") else: for subnode in node.childNodes: if subnode.nodeName == "#text": @@ -203,14 +206,12 @@ def getText(node, format, textfd=None): # pylint: disable=redefined-builtin textfd.write(NEWLINE_ESCAPE) elif subnode.nodeName == "ul": for li in getListElements(subnode): - textfd.write("-" + SPACE_ESCAPE + li + NEWLINE_ESCAPE + " ") + textfd.write(f"-{SPACE_ESCAPE}{li}{NEWLINE_ESCAPE} ") elif subnode.nodeName == "ol": i = 0 for li in getListElements(subnode): i = i + 1 - textfd.write( - str(i) + "." + SPACE_ESCAPE + li + NEWLINE_ESCAPE + " " - ) + textfd.write(f"{i}.{SPACE_ESCAPE}{li}{NEWLINE_ESCAPE} ") elif subnode.nodeName == "code": textfd.write( getText(subnode, format="keep") @@ -262,19 +263,16 @@ def makeAtom(pkgname, versionNode): @rtype: String @return: the portage atom """ - rValue = ( - opMapping[versionNode.getAttribute("range")] - + pkgname - + "-" - + getText(versionNode, format="strip") - ) + op = opMapping[versionNode.getAttribute("range")] + version = getText(versionNode, format="strip") + rValue = f"{op}{pkgname}-{version}" try: slot = versionNode.getAttribute("slot").strip() except KeyError: pass else: if slot and slot != "*": - rValue += _slot_separator + slot + rValue = f"{rValue}{_slot_separator}{slot}" return str(rValue) @@ -289,16 +287,16 @@ def makeVersion(versionNode): @rtype: String @return: the version string """ - rValue = opMapping[versionNode.getAttribute("range")] + getText( - versionNode, format="strip" - ) + op = opMapping[versionNode.getAttribute("range")] + version = getText(versionNode, format="strip") + rValue = f"{op}{version}" try: slot = versionNode.getAttribute("slot").strip() except KeyError: pass else: if slot and slot != "*": - rValue += _slot_separator + slot + rValue = f"{rValue}{_slot_separator}{slot}" return rValue @@ -421,9 +419,9 @@ def getMinUpgrade(vulnerableList, unaffectedList, portdbapi, vardbapi, minimize= and portdbapi._pkg_str(c, None).slot == vardbapi._pkg_str(vuln, None).slot ): - update = c_pv[0] + "/" + c_pv[1] + "-" + c_pv[2] + update = f"{c_pv[0]}/{c_pv[1]}-{c_pv[2]}" if c_pv[3] != "r0": # we don't like -r0 for display - update += "-" + c_pv[3] + update = f"{update}-{c_pv[3]}" update = portdbapi._pkg_str(update, None) vuln_update.append([vuln, update]) @@ -466,7 +464,7 @@ def format_date(datestr): class GlsaTypeException(Exception): def __init__(self, doctype): - Exception.__init__(self, "wrong DOCTYPE: %s" % doctype) + Exception.__init__(self, f"wrong DOCTYPE: {doctype}") class GlsaFormatException(Exception): @@ -509,7 +507,7 @@ class Glsa: self.type = "file" else: raise GlsaArgumentException( - _("Given ID %s isn't a valid GLSA ID or filename.") % myid + _(f"Given ID {myid} isn't a valid GLSA ID or filename.") ) self.nr = myid self.config = myconfig @@ -526,13 +524,13 @@ class Glsa: @return: None """ if "GLSA_DIR" in self.config: - repository = "file://" + self.config["GLSA_DIR"] + "/" + repository = f"file://{self.config['GLSA_DIR']}/" else: - repository = "file://" + self.config["PORTDIR"] + "/metadata/glsa/" + repository = f"file://{self.config['PORTDIR']}/metadata/glsa/" if self.type == "file": - myurl = "file://" + self.nr + myurl = f"file://{self.nr}" else: - myurl = repository + "glsa-%s.xml" % str(self.nr) + myurl = f"{repository}glsa-{self.nr}.xml" f = urllib_request_urlopen(myurl) try: @@ -563,10 +561,9 @@ class Glsa: myroot = self.DOM.getElementsByTagName("glsa")[0] if self.type == "id" and myroot.getAttribute("id") != self.nr: raise GlsaFormatException( - _("filename and internal id don't match:") - + myroot.getAttribute("id") - + " != " - + self.nr + _( + f"filename and internal id don't match: {myroot.getAttribute('id')} != {self.nr}" + ) ) # the simple (single, required, top-level, #PCDATA) tags first @@ -585,17 +582,14 @@ class Glsa: count = revisedEl.getAttribute("count") if not count: raise GlsaFormatException( - "Count attribute is missing or blank in GLSA: " - + myroot.getAttribute("id") + f"Count attribute is missing or blank in GLSA: {myroot.getAttribute('id')}" ) try: self.count = int(count) except ValueError: raise GlsaFormatException( - "Revision attribute in GLSA: " - + myroot.getAttribute("id") - + " is not an integer" + f"Revision attribute in GLSA: {myroot.getAttribute('id')} is not an integer" ) self.revised = format_date(self.revised) @@ -645,9 +639,9 @@ class Glsa: try: name = portage.dep.Atom(name) except portage.exception.InvalidAtom: - raise GlsaFormatException(_("invalid package name: %s") % name) + raise GlsaFormatException(_(f"invalid package name: {name}")) if name != name.cp: - raise GlsaFormatException(_("invalid package name: %s") % name) + raise GlsaFormatException(_(f"invalid package name: {name}")) name = name.cp if name not in self.packages: self.packages[name] = [] @@ -683,58 +677,51 @@ class Glsa: outstream = getattr(outstream, "buffer", outstream) outstream = codecs.getwriter(encoding)(outstream) width = 76 - outstream.write(("GLSA %s: \n%s" % (self.nr, self.title)).center(width) + "\n") - outstream.write((width * "=") + "\n") - outstream.write( - wrap(self.synopsis, width, caption=_("Synopsis: ")) + "\n" - ) - outstream.write(_("Announced on: %s\n") % self.announced) - outstream.write( - _("Last revised on: %s : %02d\n\n") % (self.revised, self.count) + buffer = "\n".join( + ( + f"GLSA {self.nr}: ", + f"{self.title}".center(width), + "=" * width, + wrap(self.synopsis, width, caption=_("Synopsis: ")), + _(f"Announced on: {self.announced}"), + _(f"Last revised on: {self.revised} : %{self.count}\n"), + ) ) + outstream.write(buffer) if self.glsatype == "ebuild": for k in self.packages: pkg = self.packages[k] for path in pkg: vul_vers = ", ".join(path["vul_vers"]) unaff_vers = ", ".join(path["unaff_vers"]) - outstream.write(_("Affected package: %s\n") % k) + outstream.write(_(f"Affected package: {k}\n")) outstream.write(_("Affected archs: ")) if path["arch"] == "*": outstream.write(_("All\n")) else: - outstream.write("%s\n" % path["arch"]) - outstream.write(_("Vulnerable: %s\n") % vul_vers) - outstream.write(_("Unaffected: %s\n\n") % unaff_vers) + outstream.write(f"{path['arch']}\n") + outstream.write(_(f"Vulnerable: {vul_vers}\n")) + outstream.write(_(f"Unaffected: {unaff_vers}\n\n")) elif self.glsatype == "infrastructure": pass if len(self.bugs) > 0: - outstream.write(_("\nRelated bugs: ")) - outstream.write(", ".join(self.bugs)) - outstream.write("\n") + outstream.write(_(f"\nRelated bugs: {', '.join(self.bugs)}\n")) if self.background: - outstream.write( - "\n" + wrap(self.background, width, caption=_("Background: ")) - ) - outstream.write( - "\n" + wrap(self.description, width, caption=_("Description: ")) - ) - outstream.write( - "\n" + wrap(self.impact_text, width, caption=_("Impact: ")) - ) - outstream.write( - "\n" + wrap(self.workaround, width, caption=_("Workaround: ")) - ) - outstream.write( - "\n" + wrap(self.resolution, width, caption=_("Resolution: ")) - ) + bg = wrap(self.background, width, caption=_("Background: ")) + outstream.write(f"\n{bg}") myreferences = " ".join( r.replace(" ", SPACE_ESCAPE) + NEWLINE_ESCAPE for r in self.references ) - outstream.write( - "\n" + wrap(myreferences, width, caption=_("References: ")) + buffer = "\n".join( + ( + wrap(self.description, width, caption=_("Description: ")), + wrap(self.impact_text, width, caption=_("Impact: ")), + wrap(self.workaround, width, caption=_("Workaround: ")), + wrap(self.resolution, width, caption=_("Resolution: ")), + wrap(myreferences, width, caption=_("References: ")), + ) ) - outstream.write("\n") + outstream.write(f"\n{buffer}\n") def isVulnerable(self): """ diff --git a/lib/portage/gpg.py b/lib/portage/gpg.py index 57be2ebc0..6238ea6f7 100644 --- a/lib/portage/gpg.py +++ b/lib/portage/gpg.py @@ -58,13 +58,13 @@ class GPG: # When run with no input/output tty, this will fail. # However, if the password is given by command, # GPG does not need to ask password, so can be ignored. - writemsg(colorize("WARN", str(e)) + "\n") + writemsg(f"{colorize('WARN', str(e))}\n") cmd = shlex_split(varexpand(self.GPG_unlock_command, mydict=self.settings)) return_code = subprocess.Popen(cmd).wait() if return_code == os.EX_OK: - writemsg_stdout(colorize("GOOD", "unlocked") + "\n") + writemsg_stdout(f"{colorize('GOOD', 'unlocked')}\n") sys.stdout.flush() else: raise GPGException("GPG unlock failed") diff --git a/lib/portage/gpkg.py b/lib/portage/gpkg.py index b642e74ec..fc111e44a 100644 --- a/lib/portage/gpkg.py +++ b/lib/portage/gpkg.py @@ -427,7 +427,7 @@ class tar_stream_reader: if not self.proc.stderr.closed: stderr = self.proc.stderr.read().decode() if not self.killed: - writemsg(colorize("BAD", "!!!" + "\n" + stderr)) + writemsg(colorize("BAD", f"!!!\n{stderr}")) raise CompressorOperationFailed("decompression failed") finally: self.proc.stdout.close() @@ -610,7 +610,7 @@ class checksum_helper: trust_signature = True if (not good_signature) or (not trust_signature): - writemsg(colorize("BAD", "!!!" + "\n" + self.gpg_result.decode())) + writemsg(colorize("BAD", f"!!!\n{self.gpg_result.decode()}")) raise InvalidSignature("GPG verify failed") def _drop_privileges(self): @@ -673,7 +673,7 @@ class checksum_helper: if self.gpg_operation == checksum_helper.VERIFY: self._check_gpg_status(self.gpg_result.decode()) else: - writemsg(colorize("BAD", "!!!" + "\n" + self.gpg_result.decode())) + writemsg(colorize("BAD", f"!!!\n{self.gpg_result.decode()}")) if self.gpg_operation == checksum_helper.SIGNING: writemsg(colorize("BAD", self.gpg_output.decode())) raise GPGException("GPG signing failed") @@ -913,9 +913,7 @@ class gpkg: container_file.write( urlopen( url, - headers={ - "Range": "bytes=" + str(init_size + 1) + "-" + str(end_size) - }, + headers={"Range": f"bytes={init_size + 1}-{end_size}"}, ).read() ) @@ -1057,7 +1055,7 @@ class gpkg: image_safe = tar_safe_extract(image, "image") image_safe.extractall(decompress_dir) except Exception as ex: - writemsg(colorize("BAD", "!!!" + "Extract failed.")) + writemsg(colorize("BAD", "!!!Extract failed.")) raise finally: image_tar.kill() @@ -1079,7 +1077,7 @@ class gpkg: raise InvalidBinaryPackageFormat("Cannot identify tar format") # container - tmp_gpkg_file_name = self.gpkg_file + "." + str(os.getpid()) + tmp_gpkg_file_name = f"{self.gpkg_file}.{os.getpid()}" with tarfile.TarFile( name=tmp_gpkg_file_name, mode="w", format=container_tar_format ) as container: @@ -1178,7 +1176,7 @@ class gpkg: """ protect_file = io.BytesIO( - b"# empty file because --include-config=n " + b"when `quickpkg` was used\n" + b"# empty file because --include-config=n when `quickpkg` was used\n" ) protect_file.seek(0, io.SEEK_END) protect_file_size = protect_file.tell() @@ -1412,7 +1410,7 @@ class gpkg: raise GPGException("GPG signature is not exists") signature = io.BytesIO(checksum_info.gpg_output) - signature_tarinfo = tarfile.TarInfo(tarinfo.name + ".sig") + signature_tarinfo = tarfile.TarInfo(f"{tarinfo.name}.sig") signature_tarinfo.size = len(signature.getvalue()) signature_tarinfo.mtime = datetime.utcnow().timestamp() container.addfile(signature_tarinfo, signature) diff --git a/lib/portage/localization.py b/lib/portage/localization.py index 9df71d62d..b9c9e90ec 100644 --- a/lib/portage/localization.py +++ b/lib/portage/localization.py @@ -28,10 +28,7 @@ def localization_example(): a_value = "value.of.a" b_value = 123 c_value = [1, 2, 3, 4] - print( - _("A: %(a)s -- B: %(b)s -- C: %(c)s") - % {"a": a_value, "b": b_value, "c": c_value} - ) + print(_(f"A: {a_value} -- B: {b_value} -- C: {c_value}")) def localized_size(num_bytes): @@ -47,4 +44,5 @@ def localized_size(num_bytes): except UnicodeDecodeError: # failure to decode locale data formatted_num = str(num_kib) - return _unicode_decode(formatted_num, encoding=_encodings["stdio"]) + " KiB" + unicode_num = _unicode_decode(formatted_num, encoding=_encodings["stdio"]) + return f"{unicode_num} KiB" diff --git a/lib/portage/mail.py b/lib/portage/mail.py index aa2617b42..7ffb0d269 100644 --- a/lib/portage/mail.py +++ b/lib/portage/mail.py @@ -54,7 +54,7 @@ def create_message(sender, recipient, subject, body, attachments=None): mymessage.attach(TextMessage(x)) else: raise portage.exception.PortageException( - _("Can't handle type of attachment: %s") % type(x) + _(f"Can't handle type of attachment: {type(x)}") ) mymessage.set_unixfrom(sender) @@ -117,14 +117,13 @@ def send_mail(mysettings, message): # user wants to use a sendmail binary instead of smtp if mymailhost[0] == os.sep and os.path.exists(mymailhost): - fd = os.popen(mymailhost + " -f " + myfrom + " " + myrecipient, "w") + fd = os.popen(f"{mymailhost } -f {myfrom} {myrecipient}", "w") fd.write(_force_ascii_if_necessary(message.as_string())) if fd.close() != None: sys.stderr.write( _( - "!!! %s returned with a non-zero exit code. This generally indicates an error.\n" + f"!!! {mymailhost} returned with a non-zero exit code. This generally indicates an error.\n" ) - % mymailhost ) else: try: @@ -149,12 +148,11 @@ def send_mail(mysettings, message): myconn.quit() except smtplib.SMTPException as e: raise portage.exception.PortageException( - _("!!! An error occurred while trying to send logmail:\n") + str(e) + _(f"!!! An error occurred while trying to send logmail:\n{e}") ) except socket.error as e: raise portage.exception.PortageException( _( - "!!! A network error occurred while trying to send logmail:\n%s\nSure you configured PORTAGE_ELOG_MAILURI correctly?" + f"!!! A network error occurred while trying to send logmail:\n{e}\nSure you configured PORTAGE_ELOG_MAILURI correctly?" ) - % str(e) )