launcher: switch command quoting to shlex.quote

Minor fix, but just in case, provides properly quoted commands for
people to copy & paste.

Change-Id: Ia9fce5c0df9f51cbed9d49861adcf6821251e46f
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/466821
Tested-by: Mike Frysinger <vapier@google.com>
Commit-Queue: Mike Frysinger <vapier@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Mike Frysinger 2025-04-09 19:59:05 -04:00 committed by LUCI
parent daebd6cbc2
commit 0214730c9a
2 changed files with 10 additions and 9 deletions

View File

@ -16,6 +16,7 @@
import os import os
import re import re
import shlex
import subprocess import subprocess
import sys import sys
@ -35,12 +36,7 @@ KEYID_ECC = "E1F9040D7A3F6DAFAC897CD3D3B95DA243E48A39"
def cmdstr(cmd): def cmdstr(cmd):
"""Get a nicely quoted shell command.""" """Get a nicely quoted shell command."""
ret = [] return " ".join(shlex.quote(x) for x in cmd)
for arg in cmd:
if not re.match(r"^[a-zA-Z0-9/_.=-]+$", arg):
arg = f'"{arg}"'
ret.append(arg)
return " ".join(ret)
def run(opts, cmd, check=True, **kwargs): def run(opts, cmd, check=True, **kwargs):

11
repo
View File

@ -57,9 +57,14 @@ class Trace:
trace = Trace() trace = Trace()
def cmdstr(cmd):
"""Get a nicely quoted shell command."""
return " ".join(shlex.quote(x) for x in cmd)
def exec_command(cmd): def exec_command(cmd):
"""Execute |cmd| or return None on failure.""" """Execute |cmd| or return None on failure."""
trace.print(":", " ".join(cmd)) trace.print(":", cmdstr(cmd))
try: try:
if platform.system() == "Windows": if platform.system() == "Windows":
ret = subprocess.call(cmd) ret = subprocess.call(cmd)
@ -506,7 +511,7 @@ def run_command(cmd, **kwargs):
# Run & package the results. # Run & package the results.
proc = subprocess.Popen(cmd, **kwargs) proc = subprocess.Popen(cmd, **kwargs)
(stdout, stderr) = proc.communicate(input=cmd_input) (stdout, stderr) = proc.communicate(input=cmd_input)
dbg = ": " + " ".join(cmd) dbg = ": " + cmdstr(cmd)
if cmd_input is not None: if cmd_input is not None:
dbg += " 0<|" dbg += " 0<|"
if stdout == subprocess.PIPE: if stdout == subprocess.PIPE:
@ -843,7 +848,7 @@ def _GetRepoConfig(name):
return None return None
else: else:
print( print(
f"repo: error: git {' '.join(cmd)} failed:\n{ret.stderr}", f"repo: error: git {cmdstr(cmd)} failed:\n{ret.stderr}",
file=sys.stderr, file=sys.stderr,
) )
# This will raise subprocess.CalledProcessError for us. # This will raise subprocess.CalledProcessError for us.