From d7362d9ed8899b52804044bb9ea037714deebf74 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Sat, 2 Nov 2024 17:49:20 +0100 Subject: [PATCH] Use Python f-strings ``` flynt -ll 99 -tc -tj -a pbuilder-dist pm-helper running-autopkgtests ubuntu-build ubuntutools ``` --- pm-helper | 22 ++++++++++----------- ubuntu-build | 2 +- ubuntutools/archive.py | 4 ++-- ubuntutools/builder.py | 10 +++++----- ubuntutools/config.py | 10 +++++----- ubuntutools/lp/lpapicache.py | 2 +- ubuntutools/misc.py | 2 +- ubuntutools/question.py | 8 ++++---- ubuntutools/requestsync/mail.py | 2 +- ubuntutools/sponsor_patch/source_package.py | 2 +- ubuntutools/sponsor_patch/sponsor_patch.py | 4 ++-- ubuntutools/update_maintainer.py | 6 +++--- ubuntutools/utils.py | 8 ++++---- 13 files changed, 41 insertions(+), 41 deletions(-) diff --git a/pm-helper b/pm-helper index dd1b45f..6c1808c 100755 --- a/pm-helper +++ b/pm-helper @@ -37,24 +37,24 @@ def get_proposed_version(excuses, package): def claim_excuses_bug(launchpad, bug, package): - print("LP: #%d: %s" % (bug.id, bug.title)) + print(f"LP: #{bug.id}: {bug.title}") ubuntu = launchpad.distributions["ubuntu"] series = ubuntu.current_series.fullseriesname for task in bug.bug_tasks: # targeting to a series doesn't make the default task disappear, # it just makes it useless - if task.bug_target_name == "%s (%s)" % (package, series): + if task.bug_target_name == f"{package} ({series})": our_task = task break - elif task.bug_target_name == "%s (Ubuntu)" % package: + elif task.bug_target_name == f"{package} (Ubuntu)": our_task = task if our_task.assignee == launchpad.me: print("Bug already assigned to you.") return True elif our_task.assignee: - print("Currently assigned to %s" % our_task.assignee.name) + print(f"Currently assigned to {our_task.assignee.name}") print("""Do you want to claim this bug? [yN] """, end="") sys.stdout.flush() @@ -70,17 +70,17 @@ def claim_excuses_bug(launchpad, bug, package): def create_excuses_bug(launchpad, package, version): print("Will open a new bug") bug = launchpad.bugs.createBug( - title="proposed-migration for %s %s" % (package, version), + title=f"proposed-migration for {package} {version}", tags=("update-excuse"), - target="https://api.launchpad.net/devel/ubuntu/+source/%s" % package, - description="%s %s is stuck in -proposed." % (package, version), + target=f"https://api.launchpad.net/devel/ubuntu/+source/{package}", + description=f"{package} {version} is stuck in -proposed.", ) task = bug.bug_tasks[0] task.assignee = launchpad.me task.lp_save() - print("Opening %s in browser" % bug.web_link) + print(f"Opening {bug.web_link} in browser") webbrowser.open(bug.web_link) return bug @@ -98,9 +98,9 @@ def has_excuses_bugs(launchpad, package): return False if len(bugs) == 1: - print("There is 1 open update-excuse bug against %s" % package) + print(f"There is 1 open update-excuse bug against {package}") else: - print("There are %d open update-excuse bugs against %s" % (len(bugs), package)) + print(f"There are {len(bugs)} open update-excuse bugs against {package}") for bug in bugs: if claim_excuses_bug(launchpad, bug, package): @@ -129,7 +129,7 @@ def main(): if not has_excuses_bugs(args.launchpad, args.package): proposed_version = get_proposed_version(excuses, args.package) if not proposed_version: - print("Package %s not found in -proposed." % args.package) + print(f"Package {args.package} not found in -proposed.") sys.exit(1) create_excuses_bug(args.launchpad, args.package, proposed_version) except ValueError as e: diff --git a/ubuntu-build b/ubuntu-build index 937ec0d..7726666 100755 --- a/ubuntu-build +++ b/ubuntu-build @@ -163,7 +163,7 @@ def main(): # but for the main archive we default to -proposed release = ubuntu.getDevelopmentSeries()[0].name if args.archive == "ubuntu": - release = release + "-proposed" + release = f"{release}-proposed" try: (release, pocket) = split_release_pocket(release) except PocketDoesNotExistError as error: diff --git a/ubuntutools/archive.py b/ubuntutools/archive.py index 538cf43..0a12716 100644 --- a/ubuntutools/archive.py +++ b/ubuntutools/archive.py @@ -543,7 +543,7 @@ class SourcePackage(ABC): Return the debdiff filename. """ cmd = ["debdiff", self.dsc_name, newpkg.dsc_name] - difffn = newpkg.dsc_name[:-3] + "debdiff" + difffn = f"{newpkg.dsc_name[:-3]}debdiff" Logger.debug("%s > %s", " ".join(cmd), difffn) with open(difffn, "w", encoding="utf-8") as f: if subprocess.call(cmd, stdout=f, cwd=str(self.workdir)) > 2: @@ -1342,7 +1342,7 @@ class SnapshotSPPH: self.getComponent(), subdir, name, - name + "_" + pkgversion, + f"{name}_{pkgversion}", "changelog.txt", ) try: diff --git a/ubuntutools/builder.py b/ubuntutools/builder.py index b6ab738..20c6943 100644 --- a/ubuntutools/builder.py +++ b/ubuntutools/builder.py @@ -71,8 +71,8 @@ class Pbuilder(Builder): cmd = [ "sudo", "-E", - "ARCH=" + self.architecture, - "DIST=" + dist, + f"ARCH={self.architecture}", + f"DIST={dist}", self.name, "--build", "--architecture", @@ -91,8 +91,8 @@ class Pbuilder(Builder): cmd = [ "sudo", "-E", - "ARCH=" + self.architecture, - "DIST=" + dist, + f"ARCH={self.architecture}", + f"DIST={dist}", self.name, "--update", "--architecture", @@ -140,7 +140,7 @@ class Sbuild(Builder): workdir = os.getcwd() Logger.debug("cd %s", result_directory) os.chdir(result_directory) - cmd = ["sbuild", "--arch-all", "--dist=" + dist, "--arch=" + self.architecture, dsc_file] + cmd = ["sbuild", "--arch-all", f"--dist={dist}", f"--arch={self.architecture}", dsc_file] Logger.debug(" ".join(cmd)) returncode = subprocess.call(cmd) Logger.debug("cd %s", workdir) diff --git a/ubuntutools/config.py b/ubuntutools/config.py index 99731ca..54d3396 100644 --- a/ubuntutools/config.py +++ b/ubuntutools/config.py @@ -99,9 +99,9 @@ class UDTConfig: if default is None and key in self.defaults: default = self.defaults[key] - keys = [self.prefix + "_" + key] + keys = [f"{self.prefix}_{key}"] if key in self.defaults: - keys.append("UBUNTUTOOLS_" + key) + keys.append(f"UBUNTUTOOLS_{key}") keys += compat_keys for k in keys: @@ -114,9 +114,9 @@ class UDTConfig: else: continue if k in compat_keys: - replacements = self.prefix + "_" + key + replacements = f"{self.prefix}_{key}" if key in self.defaults: - replacements += "or UBUNTUTOOLS_" + key + replacements += f"or UBUNTUTOOLS_{key}" Logger.warning( "Using deprecated configuration variable %s. You should use %s.", k, @@ -180,7 +180,7 @@ def ubu_email(name=None, email=None, export=True): mailname = socket.getfqdn() if os.path.isfile("/etc/mailname"): mailname = open("/etc/mailname", "r", encoding="utf-8").read().strip() - email = pwd.getpwuid(os.getuid()).pw_name + "@" + mailname + email = f"{pwd.getpwuid(os.getuid()).pw_name}@{mailname}" if export: os.environ["DEBFULLNAME"] = name diff --git a/ubuntutools/lp/lpapicache.py b/ubuntutools/lp/lpapicache.py index 1404809..22f5506 100644 --- a/ubuntutools/lp/lpapicache.py +++ b/ubuntutools/lp/lpapicache.py @@ -883,7 +883,7 @@ class SourcePackagePublishingHistory(BaseWrapper): """ release = self.getSeriesName() if self.pocket != "Release": - release += "-" + self.pocket.lower() + release += f"-{self.pocket.lower()}" return release def getArchive(self): diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py index 0b31e42..e310807 100644 --- a/ubuntutools/misc.py +++ b/ubuntutools/misc.py @@ -385,7 +385,7 @@ class _StderrProgressBar: pctstr = f"{pct:>3}%" barlen = self.width * pct // 100 barstr = "=" * barlen - barstr = barstr[:-1] + ">" + barstr = f"{barstr[:-1]}>" barstr = barstr.ljust(self.width) fullstr = f"\r[{barstr}]{pctstr}" sys.stderr.write(fullstr) diff --git a/ubuntutools/question.py b/ubuntutools/question.py index c86f7bd..abf788f 100644 --- a/ubuntutools/question.py +++ b/ubuntutools/question.py @@ -31,9 +31,9 @@ class Question: def get_options(self): if len(self.options) == 2: - options = self.options[0] + " or " + self.options[1] + options = f"{self.options[0]} or {self.options[1]}" else: - options = ", ".join(self.options[:-1]) + ", or " + self.options[-1] + options = f"{', '.join(self.options[:-1])}, or {self.options[-1]}" return options def ask(self, question, default=None): @@ -67,7 +67,7 @@ class Question: if selected == option[0]: selected = option if selected not in self.options: - print("Please answer the question with " + self.get_options() + ".") + print(f"Please answer the question with {self.get_options()}.") return selected @@ -170,7 +170,7 @@ class EditBugReport(EditFile): split_re = re.compile(r"^Summary.*?:\s+(.*?)\s+Description:\s+(.*)$", re.DOTALL | re.UNICODE) def __init__(self, subject, body, placeholders=None): - prefix = os.path.basename(sys.argv[0]) + "_" + prefix = f"{os.path.basename(sys.argv[0])}_" tmpfile = tempfile.NamedTemporaryFile(prefix=prefix, suffix=".txt", delete=False) tmpfile.write((f"Summary (one line):\n{subject}\n\nDescription:\n{body}").encode("utf-8")) tmpfile.close() diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 560253e..97316d6 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -183,7 +183,7 @@ Content-Type: text/plain; charset=UTF-8 backup = tempfile.NamedTemporaryFile( mode="w", delete=False, - prefix="requestsync-" + re.sub(r"[^a-zA-Z0-9_-]", "", bugtitle.replace(" ", "_")), + prefix=f"requestsync-{re.sub('[^a-zA-Z0-9_-]', '', bugtitle.replace(' ', '_'))}", ) with backup: backup.write(mail) diff --git a/ubuntutools/sponsor_patch/source_package.py b/ubuntutools/sponsor_patch/source_package.py index 0589c08..b2d98cc 100644 --- a/ubuntutools/sponsor_patch/source_package.py +++ b/ubuntutools/sponsor_patch/source_package.py @@ -255,7 +255,7 @@ class SourcePackage: def _changes_file(self): """Returns the file name of the .changes file.""" return os.path.join( - self._workdir, f"{self._package}_{ strip_epoch(self._version)}_source.changes" + self._workdir, f"{self._package}_{strip_epoch(self._version)}_source.changes" ) def check_target(self, upload, launchpad): diff --git a/ubuntutools/sponsor_patch/sponsor_patch.py b/ubuntutools/sponsor_patch/sponsor_patch.py index 8a0b7dd..602fd51 100644 --- a/ubuntutools/sponsor_patch/sponsor_patch.py +++ b/ubuntutools/sponsor_patch/sponsor_patch.py @@ -39,7 +39,7 @@ def is_command_available(command, check_sbin=False): "Is command in $PATH?" path = os.environ.get("PATH", "/usr/bin:/bin").split(":") if check_sbin: - path += [directory[:-3] + "sbin" for directory in path if directory.endswith("/bin")] + path += [f"{directory[:-3]}sbin" for directory in path if directory.endswith("/bin")] return any(os.access(os.path.join(directory, command), os.X_OK) for directory in path) @@ -303,7 +303,7 @@ def _download_and_change_into(task, dsc_file, patch, branch): extract_source(dsc_file, Logger.isEnabledFor(logging.DEBUG)) # change directory - directory = task.package + "-" + task.get_version().upstream_version + directory = f"{task.package}-{task.get_version().upstream_version}" Logger.debug("cd %s", directory) os.chdir(directory) diff --git a/ubuntutools/update_maintainer.py b/ubuntutools/update_maintainer.py index f48fd72..66d953f 100644 --- a/ubuntutools/update_maintainer.py +++ b/ubuntutools/update_maintainer.py @@ -72,17 +72,17 @@ class Control: def set_maintainer(self, maintainer): """Sets the value of the Maintainer field.""" pattern = re.compile("^Maintainer: ?.*$", re.MULTILINE) - self._content = pattern.sub("Maintainer: " + maintainer, self._content) + self._content = pattern.sub(f"Maintainer: {maintainer}", self._content) def set_original_maintainer(self, original_maintainer): """Sets the value of the XSBC-Original-Maintainer field.""" - original_maintainer = "XSBC-Original-Maintainer: " + original_maintainer + original_maintainer = f"XSBC-Original-Maintainer: {original_maintainer}" if self.get_original_maintainer(): pattern = re.compile("^(?:[XSBC]*-)?Original-Maintainer:.*$", re.MULTILINE) self._content = pattern.sub(original_maintainer, self._content) else: pattern = re.compile("^(Maintainer:.*)$", re.MULTILINE) - self._content = pattern.sub(r"\1\n" + original_maintainer, self._content) + self._content = pattern.sub(f"\\1\\n{original_maintainer}", self._content) def remove_original_maintainer(self): """Strip out out the XSBC-Original-Maintainer line""" diff --git a/ubuntutools/utils.py b/ubuntutools/utils.py index d9e8657..48112ca 100644 --- a/ubuntutools/utils.py +++ b/ubuntutools/utils.py @@ -40,7 +40,7 @@ def get_url(url, force_cached): m = re.search("ubuntu-archive-team.ubuntu.com/proposed-migration/([^/]*)/([^/]*)", url) if m: cache_dir = get_cache_dir() - cache_file = os.path.join(cache_dir, "%s_%s" % (m.group(1), m.group(2))) + cache_file = os.path.join(cache_dir, f"{m.group(1)}_{m.group(2)}") else: # test logs can be cached, too m = re.search( @@ -51,7 +51,7 @@ def get_url(url, force_cached): if m: cache_dir = get_cache_dir() cache_file = os.path.join( - cache_dir, "%s_%s_%s_%s.gz" % (m.group(1), m.group(2), m.group(3), m.group(4)) + cache_dir, f"{m.group(1)}_{m.group(2)}_{m.group(3)}_{m.group(4)}.gz" ) if cache_file: @@ -69,10 +69,10 @@ def get_url(url, force_cached): if cache_file: remote_ts = dateutil.parser.parse(f.headers["last-modified"]) if remote_ts > prev_timestamp: - with open("%s.new" % cache_file, "wb") as new_cache: + with open(f"{cache_file}.new", "wb") as new_cache: for line in f: new_cache.write(line) - os.rename("%s.new" % cache_file, cache_file) + os.rename(f"{cache_file}.new", cache_file) os.utime(cache_file, times=(new_timestamp, new_timestamp)) f.close() f = open(cache_file, "rb")