* [gentoo-commits] proj/portage:master commit in: lib/portage/, lib/_emerge/
@ 2022-03-27 23:07 Sam James
0 siblings, 0 replies; 3+ messages in thread
From: Sam James @ 2022-03-27 23:07 UTC (permalink / raw
To: gentoo-commits
commit: 7c8875c38b187fbd2e0fa5fc39bbb38c1588d0fb
Author: Kenneth Raplee <kenrap <AT> kennethraplee <DOT> com>
AuthorDate: Tue Mar 22 07:01:05 2022 +0000
Commit: Sam James <sam <AT> gentoo <DOT> 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 <kenrap <AT> kennethraplee.com>
Signed-off-by: Sam James <sam <AT> 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 "<portage.eclass_cache.hashed_path('%s')>" % (self.location,)
+ return f"<portage.eclass_cache.hashed_path('{self.location}')>"
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)
)
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [gentoo-commits] proj/portage:master commit in: lib/portage/, lib/_emerge/
@ 2022-03-27 23:07 Sam James
0 siblings, 0 replies; 3+ messages in thread
From: Sam James @ 2022-03-27 23:07 UTC (permalink / raw
To: gentoo-commits
commit: 18e5a8170c69aecd10f162918de571d85055ae81
Author: Kenneth Raplee <kenrap <AT> kennethraplee <DOT> com>
AuthorDate: Fri Mar 25 22:06:22 2022 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Mar 27 23:06:48 2022 +0000
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=18e5a817
Miscellaneous refactors and cleanups
Signed-off-by: Kenneth Raplee <kenrap <AT> kennethraplee.com>
Closes: https://github.com/gentoo/portage/pull/796
Signed-off-by: Sam James <sam <AT> gentoo.org>
lib/_emerge/main.py | 10 ++--
lib/portage/__init__.py | 14 ++---
lib/portage/_global_updates.py | 36 ++++++-------
lib/portage/checksum.py | 17 ++----
lib/portage/data.py | 34 +++++-------
lib/portage/dispatch_conf.py | 2 +-
lib/portage/getbinpkg.py | 115 ++++++++++++++++++-----------------------
lib/portage/glsa.py | 4 +-
8 files changed, 91 insertions(+), 141 deletions(-)
diff --git a/lib/_emerge/main.py b/lib/_emerge/main.py
index 01dc1b419..d22da1926 100644
--- a/lib/_emerge/main.py
+++ b/lib/_emerge/main.py
@@ -1235,12 +1235,10 @@ def emerge_main(args=None):
1: dev_null.fileno(),
2: dev_null.fileno(),
}
- if (
- portage.process.spawn_bash(
- "[[ $(< <(echo foo) ) == foo ]]", fd_pipes=fd_pipes
- )
- != 0
- ):
+ exit_code = portage.process.spawn_bash(
+ "[[ $(< <(echo foo) ) == foo ]]", fd_pipes=fd_pipes
+ )
+ if exit_code != 0:
writemsg_level(
"Failed to validate a sane '/dev'.\n"
"bash process substitution doesn't work; this may be an "
diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py
index e6dff28ee..3f1f29f08 100644
--- a/lib/portage/__init__.py
+++ b/lib/portage/__init__.py
@@ -260,7 +260,6 @@ class _unicode_func_wrapper:
self._encoding = encoding
def _process_args(self, args, kwargs):
-
encoding = self._encoding
wrapped_args = [
_unicode_encode(x, encoding=encoding, errors="strict") for x in args
@@ -276,7 +275,6 @@ class _unicode_func_wrapper:
return (wrapped_args, wrapped_kwargs)
def __call__(self, *args, **kwargs):
-
encoding = self._encoding
wrapped_args, wrapped_kwargs = self._process_args(args, kwargs)
@@ -360,7 +358,6 @@ class _eintr_func_wrapper:
self._func = func
def __call__(self, *args, **kwargs):
-
while True:
try:
rval = self._func(*args, **kwargs)
@@ -383,10 +380,7 @@ _os_overrides = {
}
-try:
- _os_overrides[id(_os.mkfifo)] = _os.mkfifo
-except AttributeError:
- pass # Jython
+_os_overrides[id(_os.mkfifo)] = _os.mkfifo
if hasattr(_os, "statvfs"):
_os_overrides[id(_os.statvfs)] = _os.statvfs
@@ -498,9 +492,9 @@ if platform.system() in ("FreeBSD",):
def load_mod(name):
- modname = ".".join(name.split(".")[:-1])
- mod = __import__(modname)
components = name.split(".")
+ modname = ".".join(components[:-1])
+ mod = __import__(modname)
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
@@ -573,7 +567,6 @@ def _eapi_is_deprecated(eapi):
def eapi_is_supported(eapi):
eapi = str(eapi).strip()
-
return eapi in _supported_eapis
@@ -794,7 +787,6 @@ _legacy_global_var_names = (
def _reset_legacy_globals():
-
global _legacy_globals_constructed
_legacy_globals_constructed = set()
for k in _legacy_global_var_names:
diff --git a/lib/portage/_global_updates.py b/lib/portage/_global_updates.py
index d7117d36b..59e0b263e 100644
--- a/lib/portage/_global_updates.py
+++ b/lib/portage/_global_updates.py
@@ -89,7 +89,7 @@ def _do_global_updates(trees, prev_mtimes, quiet=False, if_mtime_changed=True):
if not update_notice_printed:
update_notice_printed = True
writemsg_stdout("\n")
- writemsg_stdout(colorize("GOOD", _("Performing Global Updates\n")))
+ writemsg_stdout(colorize("GOOD", "Performing Global Updates\n"))
writemsg_stdout(
_(
"(Could take a couple of minutes if you have a lot of binary packages.)\n"
@@ -236,38 +236,32 @@ def _do_global_updates(trees, prev_mtimes, quiet=False, if_mtime_changed=True):
# until after _all_ of the above updates have
# been processed because the mtimedb will
# automatically commit when killed by ctrl C.
- for mykey, mtime in timestamps.items():
- prev_mtimes[mykey] = mtime
+ prev_mtimes.update(timestamps)
- do_upgrade_packagesmessage = False
# We gotta do the brute force updates for these now.
- if True:
+ def onUpdate(_maxval, curval):
+ if curval > 0:
+ writemsg_stdout("#")
- def onUpdate(_maxval, curval):
- if curval > 0:
- writemsg_stdout("#")
+ if quiet:
+ onUpdate = None
- if quiet:
- onUpdate = None
- vardb.update_ents(repo_map, onUpdate=onUpdate)
- if bindb:
+ vardb.update_ents(repo_map, onUpdate=onUpdate)
- def onUpdate(_maxval, curval):
- if curval > 0:
- writemsg_stdout("*")
+ if bindb:
- if quiet:
- onUpdate = None
- bindb.update_ents(repo_map, onUpdate=onUpdate)
- else:
- do_upgrade_packagesmessage = 1
+ def onUpdate(_maxval, curval):
+ if curval > 0:
+ writemsg_stdout("*")
+
+ bindb.update_ents(repo_map, onUpdate=onUpdate)
# Update progress above is indicated by characters written to stdout so
# we print a couple new lines here to separate the progress output from
# what follows.
writemsg_stdout("\n\n")
- if do_upgrade_packagesmessage and bindb and bindb.cpv_all():
+ if bindb and bindb.cpv_all():
writemsg_stdout(
_(
" ** Skipping packages. Run 'fixpackages' or set it in FEATURES to fix the tbz2's in the packages directory.\n"
diff --git a/lib/portage/checksum.py b/lib/portage/checksum.py
index 72d20525a..b4644da04 100644
--- a/lib/portage/checksum.py
+++ b/lib/portage/checksum.py
@@ -344,11 +344,8 @@ if os.path.exists(PRELINK_BINARY):
def is_prelinkable_elf(filename):
- f = _open_file(filename)
- try:
+ with _open_file(filename) as f:
magic = f.read(17)
- finally:
- f.close()
return (
len(magic) == 17
and magic.startswith(b"\x7fELF")
@@ -444,11 +441,11 @@ def _apply_hash_filter(digests, hash_filter):
@type hash_filter: callable
"""
- verifiable_hash_types = set(digests).intersection(hashfunc_keys)
- verifiable_hash_types.discard("size")
+ verifiable_hash_types = frozenset(digests).intersection(hashfunc_keys)
modified = False
if len(verifiable_hash_types) > 1:
- for k in list(verifiable_hash_types):
+ verifiable_hash_types.discard("size")
+ for k in verifiable_hash_types:
if not hash_filter(k):
modified = True
verifiable_hash_types.remove(k)
@@ -456,11 +453,7 @@ def _apply_hash_filter(digests, hash_filter):
break
if modified:
- digests = {
- k: v
- for (k, v) in digests.items()
- if k == "size" or k in verifiable_hash_types
- }
+ digests = {k: v for k, v in digests.items() if k in verifiable_hash_types}
return digests
diff --git a/lib/portage/data.py b/lib/portage/data.py
index b73fa8882..c783d76d0 100644
--- a/lib/portage/data.py
+++ b/lib/portage/data.py
@@ -8,6 +8,7 @@ import platform
import pwd
import portage
+from portage.localization import _
portage.proxy.lazyimport.lazyimport(
globals(),
@@ -16,14 +17,11 @@ portage.proxy.lazyimport.lazyimport(
"portage.util.path:first_existing",
"subprocess",
)
-from portage.localization import _
ostype = platform.system()
-userland = None
+userland = "GNU"
if ostype == "DragonFly" or ostype.endswith("BSD"):
userland = "BSD"
-else:
- userland = "GNU"
lchown = getattr(os, "lchown", None)
@@ -140,7 +138,6 @@ def _get_global(k):
return globals()[k]
if k == "secpass":
-
unprivileged = False
if hasattr(portage, "settings"):
unprivileged = "unprivileged" in portage.settings.features
@@ -254,11 +251,10 @@ def _get_global(k):
# Avoid instantiating portage.settings when the desired
# variable is set in os.environ.
elif k in ("_portage_grpname", "_portage_username"):
- v = None
+ v = "portage"
+ env_key = "PORTAGE_USERNAME"
if k == "_portage_grpname":
env_key = "PORTAGE_GRPNAME"
- else:
- env_key = "PORTAGE_USERNAME"
if env_key in os.environ:
v = os.environ[env_key]
@@ -290,9 +286,6 @@ def _get_global(k):
pass
else:
v = pwd_struct.pw_name
-
- if v is None:
- v = "portage"
else:
raise AssertionError("unknown name: %s" % k)
@@ -302,7 +295,6 @@ def _get_global(k):
class _GlobalProxy(portage.proxy.objectproxy.ObjectProxy):
-
__slots__ = ("_name",)
def __init__(self, name):
@@ -340,17 +332,15 @@ def _init(settings):
# from grp.getgrnam() with PyPy
native_string = platform.python_implementation() == "PyPy"
- v = settings.get("PORTAGE_GRPNAME", "portage")
- if native_string:
- v = portage._native_string(v)
- globals()["_portage_grpname"] = v
- _initialized_globals.add("_portage_grpname")
-
- v = settings.get("PORTAGE_USERNAME", "portage")
if native_string:
- v = portage._native_string(v)
- globals()["_portage_username"] = v
- _initialized_globals.add("_portage_username")
+ grpname = settings.get("PORTAGE_GRPNAME", "portage")
+ grpname = portage._native_string(grpname)
+ globals()["_portage_grpname"] = grpname
+ _initialized_globals.add("_portage_grpname")
+ username = settings.get("PORTAGE_USERNAME", "portage")
+ username = portage._native_string(username)
+ globals()["_portage_username"] = username
+ _initialized_globals.add("_portage_username")
if "secpass" not in _initialized_globals:
v = 0
diff --git a/lib/portage/dispatch_conf.py b/lib/portage/dispatch_conf.py
index d682a9ad0..6dbb1885e 100644
--- a/lib/portage/dispatch_conf.py
+++ b/lib/portage/dispatch_conf.py
@@ -40,7 +40,7 @@ def diffstatusoutput(cmd, file1, file2):
args = (portage._unicode_encode(x, errors="strict") for x in args)
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = portage._unicode_decode(proc.communicate()[0])
- if output and output[-1] == "\n":
+ if output and output.endswith("\n"):
# getstatusoutput strips one newline
output = output[:-1]
return (proc.wait(), output)
diff --git a/lib/portage/getbinpkg.py b/lib/portage/getbinpkg.py
index f357a91df..ea9ee1d0a 100644
--- a/lib/portage/getbinpkg.py
+++ b/lib/portage/getbinpkg.py
@@ -21,11 +21,11 @@ import tempfile
import base64
import warnings
-_all_errors = [NotImplementedError, ValueError, socket.error]
-
from html.parser import HTMLParser as html_parser_HTMLParser
from urllib.parse import unquote as urllib_parse_unquote
+_all_errors = [NotImplementedError, ValueError, socket.error]
+
try:
import ftplib
except ImportError as e:
@@ -47,7 +47,6 @@ _all_errors = tuple(_all_errors)
def make_metadata_dict(data):
-
warnings.warn(
"portage.getbinpkg.make_metadata_dict() is deprecated",
DeprecationWarning,
@@ -82,7 +81,6 @@ class ParseLinks(html_parser_HTMLParser):
page and provide suffix and prefix limitors"""
def __init__(self):
-
warnings.warn(
"portage.getbinpkg.ParseLinks is deprecated",
DeprecationWarning,
@@ -96,19 +94,15 @@ class ParseLinks(html_parser_HTMLParser):
return self.PL_anchors
def get_anchors_by_prefix(self, prefix):
- newlist = []
- for x in self.PL_anchors:
- if x.startswith(prefix):
- if x not in newlist:
- newlist.append(x[:])
+ newlist = [
+ x for x in self.PL_anchors if x.startswith(prefix) and x not in newlist
+ ]
return newlist
def get_anchors_by_suffix(self, suffix):
- newlist = []
- for x in self.PL_anchors:
- if x.endswith(suffix):
- if x not in newlist:
- newlist.append(x[:])
+ newlist = [
+ x for x in self.PL_anchors if x.endswith(suffix) and x not in newlist
+ ]
return newlist
def handle_endtag(self, tag):
@@ -140,16 +134,13 @@ def create_conn(baseurl, conn=None):
raise ValueError(
_("Provided URI does not " "contain protocol identifier. '%s'") % baseurl
)
- protocol, url_parts = parts
+ protocol, url = parts
del parts
- url_parts = url_parts.split("/")
- host = url_parts[0]
- if len(url_parts) < 2:
- address = "/"
- else:
- address = "/" + "/".join(url_parts[1:])
- del url_parts
+ url_split = url.split("/", 1)
+ host = url_split[0]
+ address = f"/{url_split[1]}"
+ del url, url_split
userpass_host = host.split("@", 1)
if len(userpass_host) == 1:
@@ -160,13 +151,14 @@ def create_conn(baseurl, conn=None):
userpass = userpass_host[0].split(":")
del userpass_host
- if len(userpass) > 2:
+ userpass_size = len(userpass)
+ if userpass_size > 2:
raise ValueError(_("Unable to interpret username/password provided."))
- elif len(userpass) == 2:
- username = userpass[0]
+
+ username = userpass[0]
+ if userpass_size == 2:
password = userpass[1]
- elif len(userpass) == 1:
- username = userpass[0]
+ elif userpass_size == 1:
password = None
del userpass
@@ -243,7 +235,6 @@ def make_ftp_request(conn, address, rest=None, dest=None):
)
try:
-
if dest:
fstart_pos = dest.tell()
@@ -268,10 +259,9 @@ def make_ftp_request(conn, address, rest=None, dest=None):
else:
break
+ data_size = len(mydata)
if dest:
data_size = fstart_pos - dest.tell()
- else:
- data_size = len(mydata)
mysocket.close()
conn.voidresp()
@@ -296,7 +286,7 @@ def make_http_request(conn, address, _params={}, headers={}, dest=None):
rc = 0
response = None
- while (rc == 0) or (rc == 301) or (rc == 302):
+ while rc in (0, 301, 302):
try:
if rc != 0:
conn = create_conn(address)[0]
@@ -309,9 +299,9 @@ def make_http_request(conn, address, _params={}, headers={}, dest=None):
rc = response.status
# 301 means that the page address is wrong.
- if (rc == 301) or (rc == 302):
- ignored_data = response.read()
- del ignored_data
+ if rc in (301, 302):
+ # This response reading is ignored on purpose.
+ _ = response.read()
for x in str(response.msg).split("\n"):
parts = x.split(": ", 1)
if parts[0] == "Location":
@@ -326,7 +316,7 @@ def make_http_request(conn, address, _params={}, headers={}, dest=None):
address = parts[1]
break
- if (rc != 200) and (rc != 206):
+ if rc not in (200, 206):
return (
None,
rc,
@@ -355,26 +345,28 @@ def match_in_array(array, prefix="", suffix="", match_both=1, allow_overlap=0):
for x in array:
add_p = 0
- if prefix and (len(x) >= len(prefix)) and (x[: len(prefix)] == prefix):
+ x_size = len(x)
+ prefix_size = len(prefix)
+ if prefix and x_size >= prefix_size and x[:prefix_size] == prefix:
add_p = 1
if match_both:
if prefix and not add_p: # Require both, but don't have first one.
continue
- else:
- if add_p: # Only need one, and we have it.
- myarray.append(x[:])
- continue
+ elif add_p: # Only need one, and we have it.
+ myarray.append(x[:])
+ continue
+ suffix_size = len(suffix)
if not allow_overlap: # Not allow to overlap prefix and suffix
- if len(x) >= (len(prefix) + len(suffix)):
+ if x_size >= (prefix_size + suffix_size):
pass
else:
continue # Too short to match.
else:
pass # Do whatever... We're overlapping.
- if suffix and (len(x) >= len(suffix)) and (x[-len(suffix) :] == suffix):
+ if suffix and x_size >= suffix_size and x[-len(suffix) :] == suffix:
myarray.append(x) # It matches
else:
continue # Doesn't match.
@@ -393,9 +385,8 @@ def dir_get_list(baseurl, conn=None):
stacklevel=2,
)
- if not conn:
- keepconnection = 0
- else:
+ keepconnection = 0
+ if conn:
keepconnection = 1
conn, protocol, address, params, headers = create_conn(baseurl, conn)
@@ -419,7 +410,7 @@ def dir_get_list(baseurl, conn=None):
raise portage.exception.PortageException(
_("Unable to get listing: %s %s") % (rc, msg)
)
- elif protocol in ["ftp"]:
+ elif protocol == "ftp":
if address[-1] == "/":
olddir = conn.pwd()
conn.cwd(address)
@@ -450,10 +441,9 @@ def file_get_metadata(baseurl, conn=None, chunk_size=3000):
stacklevel=2,
)
- if not conn:
+ keepconnection = 1
+ if conn:
keepconnection = 0
- else:
- keepconnection = 1
conn, protocol, address, params, headers = create_conn(baseurl, conn)
@@ -532,7 +522,7 @@ def file_get(
variables["URI"] = baseurl
if "FILE" not in variables:
- if filename is None:
+ if not filename:
filename = os.path.basename(variables["URI"])
variables["FILE"] = filename
@@ -565,9 +555,8 @@ def file_get_lib(baseurl, dest, conn=None):
stacklevel=2,
)
- if not conn:
- keepconnection = 0
- else:
+ keepconnection = 0
+ if conn:
keepconnection = 1
conn, protocol, address, params, headers = create_conn(baseurl, conn)
@@ -608,22 +597,20 @@ def file_get_lib(baseurl, dest, conn=None):
def dir_get_metadata(
baseurl, conn=None, chunk_size=3000, verbose=1, usingcache=1, makepickle=None
):
-
warnings.warn(
"portage.getbinpkg.dir_get_metadata() is deprecated",
DeprecationWarning,
stacklevel=2,
)
- if not conn:
+ keepconnection = 1
+ if conn:
keepconnection = 0
- else:
- keepconnection = 1
cache_path = "/var/cache/edb"
metadatafilename = os.path.join(cache_path, "remote_metadata.pickle")
- if makepickle is None:
+ if not makepickle:
makepickle = "/var/cache/edb/metadata.idx.most_recent"
try:
@@ -896,7 +883,7 @@ class PackageIndex:
):
self._pkg_slot_dict = None
- if allowed_pkg_keys is not None:
+ if allowed_pkg_keys:
self._pkg_slot_dict = slot_dict_class(allowed_pkg_keys)
self._default_header_data = default_header_data
@@ -914,11 +901,9 @@ class PackageIndex:
self.modified = True
def _readpkgindex(self, pkgfile, pkg_entry=True):
-
+ d = {}
allowed_keys = None
- if self._pkg_slot_dict is None or not pkg_entry:
- d = {}
- else:
+ if self._pkg_slot_dict and pkg_entry:
d = self._pkg_slot_dict()
allowed_keys = d.allowed_keys
@@ -964,7 +949,7 @@ class PackageIndex:
if self._inherited_keys:
for k in self._inherited_keys:
v = self.header.get(k)
- if v is not None:
+ if v:
d.setdefault(k, v)
self.packages.append(d)
@@ -982,7 +967,7 @@ class PackageIndex:
if self._inherited_keys:
for k in self._inherited_keys:
v = self.header.get(k)
- if v is not None and v == metadata.get(k):
+ if v and v == metadata.get(k):
del metadata[k]
if self._default_pkg_data:
for k, v in self._default_pkg_data.items():
diff --git a/lib/portage/glsa.py b/lib/portage/glsa.py
index 1b68bc0e9..d61ad7e59 100644
--- a/lib/portage/glsa.py
+++ b/lib/portage/glsa.py
@@ -115,12 +115,10 @@ def get_glsa_list(myconfig):
@rtype: List of Strings
@return: a list of GLSA IDs in this repository
"""
- rValue = []
+ repository = os.path.join(myconfig["PORTDIR"], "metadata", "glsa")
if "GLSA_DIR" in myconfig:
repository = myconfig["GLSA_DIR"]
- else:
- repository = os.path.join(myconfig["PORTDIR"], "metadata", "glsa")
if not os.access(repository, os.R_OK):
return []
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [gentoo-commits] proj/portage:master commit in: lib/portage/, lib/_emerge/
@ 2022-12-03 1:38 Sam James
0 siblings, 0 replies; 3+ messages in thread
From: Sam James @ 2022-12-03 1:38 UTC (permalink / raw
To: gentoo-commits
commit: a6737b229d62ad08a2f133c718d22f8701ae7ecb
Author: John Helmert III <ajak <AT> gentoo <DOT> org>
AuthorDate: Thu Dec 1 17:43:19 2022 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Dec 3 01:38:23 2022 +0000
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=a6737b22
Manual string formatting updates
Signed-off-by: John Helmert III <ajak <AT> gentoo.org>
Closes: https://github.com/gentoo/portage/pull/888
Signed-off-by: Sam James <sam <AT> gentoo.org>
lib/_emerge/actions.py | 5 +++--
lib/_emerge/depgraph.py | 13 +++++++------
lib/portage/update.py | 4 ++--
3 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/lib/_emerge/actions.py b/lib/_emerge/actions.py
index e93202b7f..30b687140 100644
--- a/lib/_emerge/actions.py
+++ b/lib/_emerge/actions.py
@@ -1651,8 +1651,9 @@ def _calc_depclean(settings, trees, ldpath_mtimes, myopts, action, args_set, spi
if debug:
writemsg_level(
- "Candidates: [%s]\n"
- % ", ".join("'{}'".format(x) for x in atoms),
+ "Candidates: [{}]\n".format(
+ ", ".join("'{}'".format(x) for x in atoms)
+ ),
noiselevel=-1,
level=logging.DEBUG,
)
diff --git a/lib/_emerge/depgraph.py b/lib/_emerge/depgraph.py
index 3b7c0d431..6b08f9fe2 100644
--- a/lib/_emerge/depgraph.py
+++ b/lib/_emerge/depgraph.py
@@ -2039,12 +2039,13 @@ class depgraph:
"",
"",
"backtracking due to slot conflict:",
- " first package: %s" % existing_node,
- " package(s) to mask: %s" % str(to_be_masked),
- " slot: %s" % slot_atom,
- " parents: %s"
- % ", ".join(
- "({}, '{}')".format(ppkg, atom) for ppkg, atom in all_parents
+ " first package: {}".format(existing_node),
+ " package(s) to mask: {}".format(str(to_be_masked)),
+ " slot: {}".format(slot_atom),
+ " parents: {}".format(
+ ", ".join(
+ "({}, '{}')".format(ppkg, atom) for ppkg, atom in all_parents
+ )
),
"",
]
diff --git a/lib/portage/update.py b/lib/portage/update.py
index fdd0b1948..64dfad7e9 100644
--- a/lib/portage/update.py
+++ b/lib/portage/update.py
@@ -424,8 +424,8 @@ def update_config_files(
if match_callback(repo_name, atom, new_atom):
# add a comment with the update command, so
# the user can clearly see what happened
- contents[pos] = "# %s\n" % " ".join(
- "{}".format(x) for x in update_cmd
+ contents[pos] = "# {}\n".format(
+ " ".join("{}".format(x) for x in update_cmd)
)
contents.insert(
pos + 1,
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-12-03 1:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-27 23:07 [gentoo-commits] proj/portage:master commit in: lib/portage/, lib/_emerge/ Sam James
-- strict thread matches above, loose matches on Subject: below --
2022-03-27 23:07 Sam James
2022-12-03 1:38 Sam James
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox