aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2024-04-01 17:52:08 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-04-02 00:06:06 +0000
commit67fd41ab7625dc74702f8f6d6e287faea895a16b (patch)
tree5609ae58877c2f6cddb568db73914cc173677500
parent44fc9bbb4a14aece874c35feaabf8a3475e3e1c0 (diff)
downloadtoolchain-utils-67fd41ab7625dc74702f8f6d6e287faea895a16b.tar.gz
replace shlex.quote with shlex.join
shlex.join(list_of_strs) is new in py3.8. It's also a decent bit cleaner than `" ".join(shlex.quote(x) for x in l)`. BUG=None TEST=repo upload Change-Id: Id1f6170bd3364adb6e229bc001ab9c0a182c2123 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5410944 Tested-by: George Burgess <gbiv@chromium.org> Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com> Commit-Queue: Jordan Abrahams-Whitehead <ajordanr@google.com> Auto-Submit: George Burgess <gbiv@chromium.org> Commit-Queue: George Burgess <gbiv@chromium.org>
-rwxr-xr-xpgo_tools/benchmark_pgo_profiles.py6
-rwxr-xr-xpgo_tools/create_chroot_and_generate_pgo_profile.py4
-rw-r--r--pgo_tools/pgo_tools.py2
-rwxr-xr-xtoolchain_utils_githooks/check-presubmit.py33
4 files changed, 17 insertions, 28 deletions
diff --git a/pgo_tools/benchmark_pgo_profiles.py b/pgo_tools/benchmark_pgo_profiles.py
index d6fb4945..121fb774 100755
--- a/pgo_tools/benchmark_pgo_profiles.py
+++ b/pgo_tools/benchmark_pgo_profiles.py
@@ -118,8 +118,8 @@ def construct_hyperfine_cmd(
else:
raise ValueError(f"Unknown profile type: {type(profile)}")
- quickpkg_restore = " ".join(
- shlex.quote(str(x))
+ quickpkg_restore = shlex.join(
+ str(x)
for x in pgo_tools.generate_quickpkg_restoration_command(llvm_binpkg)
)
@@ -231,7 +231,7 @@ def run_benchmark(
logging.info(
"Profile %r: Running %s",
str(profile),
- " ".join(shlex.quote(str(x)) for x in cmd),
+ shlex.join(str(x) for x in cmd),
)
pgo_tools.run(cmd)
diff --git a/pgo_tools/create_chroot_and_generate_pgo_profile.py b/pgo_tools/create_chroot_and_generate_pgo_profile.py
index b9e4c62c..c09cb4d8 100755
--- a/pgo_tools/create_chroot_and_generate_pgo_profile.py
+++ b/pgo_tools/create_chroot_and_generate_pgo_profile.py
@@ -231,9 +231,7 @@ def main(argv: List[str]):
if opts.upload:
pgo_tools.run(upload_command)
else:
- friendly_upload_command = " ".join(
- shlex.quote(str(x)) for x in upload_command
- )
+ friendly_upload_command = shlex.join(str(x) for x in upload_command)
logging.info(
"To upload the profile, run %r in %r",
friendly_upload_command,
diff --git a/pgo_tools/pgo_tools.py b/pgo_tools/pgo_tools.py
index 577fa376..2702d605 100644
--- a/pgo_tools/pgo_tools.py
+++ b/pgo_tools/pgo_tools.py
@@ -35,7 +35,7 @@ def run(
env = None
if logging.getLogger().isEnabledFor(logging.DEBUG):
- c = " ".join(shlex.quote(str(x)) for x in command)
+ c = shlex.join(str(x) for x in command)
dir_extra = f" in {cwd}" if cwd is not None else ""
logging.debug("Running `%s`%s", c, dir_extra)
diff --git a/toolchain_utils_githooks/check-presubmit.py b/toolchain_utils_githooks/check-presubmit.py
index 995773a2..12d278a8 100755
--- a/toolchain_utils_githooks/check-presubmit.py
+++ b/toolchain_utils_githooks/check-presubmit.py
@@ -110,15 +110,6 @@ def has_executable_on_path(exe: str) -> bool:
return shutil.which(exe) is not None
-def escape_command(command: Iterable[str]) -> str:
- """Returns a human-readable and copy-pastable shell command.
-
- Only intended for use in output to users. shell=True is strongly
- discouraged.
- """
- return " ".join(shlex.quote(x) for x in command)
-
-
def remove_deleted_files(files: Iterable[str]) -> List[str]:
return [f for f in files if os.path.exists(f)]
@@ -279,8 +270,8 @@ def check_isort(
if not bad_files:
return CheckResult(
ok=False,
- output="`%s` failed; stdout/stderr:\n%s"
- % (escape_command(command), stdout_and_stderr),
+ output=f"`{shlex.join(command)}` failed; stdout/stderr:\n"
+ f"{stdout_and_stderr}",
autofix_commands=[],
)
@@ -711,8 +702,7 @@ def check_go_format(toolchain_utils_root, _thread_pool, files):
if exit_code:
return CheckResult(
ok=False,
- output="%s failed; stdout/stderr:\n%s"
- % (escape_command(command), output),
+ output=f"{shlex.join(command)} failed; stdout/stderr:\n{output}",
autofix_commands=[],
)
@@ -800,9 +790,10 @@ def process_check_result(
if isinstance(check_results, CheckResult):
ok, output, autofix_commands = check_results
if not ok and autofix_commands:
- recommendation = "Recommended command(s) to fix this: %s" % [
- escape_command(x) for x in autofix_commands
- ]
+ recommendation = (
+ "Recommended command(s) to fix this: "
+ f"{[shlex.join(x) for x in autofix_commands]}"
+ )
if output:
output += "\n" + recommendation
else:
@@ -818,8 +809,8 @@ def process_check_result(
if not ok and autofix:
message.append(
indent_block(
- "Recommended command(s) to fix this: %s"
- % [escape_command(x) for x in autofix]
+ "Recommended command(s) to fix this: "
+ "{[shlex.join(x) for x in autofix]}"
)
)
@@ -870,12 +861,12 @@ def try_autofix(
if exit_code:
print(
- "*** Autofix command `%s` exited with code %d; stdout/stderr:"
- % (escape_command(command), exit_code)
+ f"*** Autofix command `{shlex.join(command)}` exited with "
+ f"code {exit_code}; stdout/stderr:"
)
print(output)
else:
- print("*** Autofix `%s` succeeded" % escape_command(command))
+ print(f"*** Autofix `{shlex.join(command)}` succeeded")
anything_succeeded = True
if anything_succeeded: