From 510c97d86119398388569623036f5bef2b9439d2 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 18 Jul 2018 17:26:44 -0400 Subject: [PATCH] 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 --- puppet/zulip/files/postgresql/pg_backup_and_purge | 3 ++- scripts/lib/zulip_tools.py | 6 ++++-- tools/diagnose | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/puppet/zulip/files/postgresql/pg_backup_and_purge b/puppet/zulip/files/postgresql/pg_backup_and_purge index d0f6d6d08d..9ee21a7fa9 100755 --- a/puppet/zulip/files/postgresql/pg_backup_and_purge +++ b/puppet/zulip/files/postgresql/pg_backup_and_purge @@ -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, diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index fd0c594d6e..2f979c022a 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -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) diff --git a/tools/diagnose b/tools/diagnose index e7443b6fd0..8afcdc0ae0 100755 --- a/tools/diagnose +++ b/tools/diagnose @@ -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