scripts: Use shell quoting when displaying commands to be run.

This way, commands with arguments containing whitespace or
metacharacters are unambiguously readable.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2018-07-18 17:26:44 -04:00 committed by Tim Abbott
parent de5e3d3fbd
commit 510c97d861
3 changed files with 8 additions and 4 deletions

View File

@ -2,6 +2,7 @@
import sys
import glob
import shlex
import subprocess
import sys
import logging
@ -17,7 +18,7 @@ logger = logging.getLogger(__name__)
def run(args, dry_run=False):
# type: (List[str], bool) -> str
if dry_run:
print("Would have run: " + " ".join(args))
print("Would have run: " + " ".join(map(shlex.quote, args)))
return ""
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,

View File

@ -7,6 +7,7 @@ import logging
import os
import pwd
import re
import shlex
import shutil
import subprocess
import sys
@ -153,7 +154,7 @@ def release_deployment_lock():
def run(args, **kwargs):
# type: (Sequence[str], **Any) -> None
# Output what we're doing in the `set -x` style
print("+ %s" % (" ".join(args)))
print("+ %s" % (" ".join(map(shlex.quote, args)),))
if kwargs.get('shell'):
# With shell=True we can only pass string to Popen
@ -163,7 +164,8 @@ def run(args, **kwargs):
subprocess.check_call(args, **kwargs)
except subprocess.CalledProcessError:
print()
print(WHITEONRED + "Error running a subcommand of %s: %s" % (sys.argv[0], " ".join(args)) +
print(WHITEONRED + "Error running a subcommand of %s: %s" %
(sys.argv[0], " ".join(map(shlex.quote, args))) +
ENDC)
print(WHITEONRED + "Actual error output for the subcommand is just above this." +
ENDC)

View File

@ -2,6 +2,7 @@
import os
import platform
import shlex
import sys
import subprocess
@ -26,7 +27,7 @@ def run(check_func):
def run_command(args):
# type: (List[str]) -> None
print(' '.join(args))
print(' '.join(map(shlex.quote, args)))
subprocess.check_call(args)
@run