From 098a5145999b44eab822aaa1f7ba78d8b57bfa5e Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 26 Apr 2022 17:10:28 -0700 Subject: [PATCH] python: Use Python 3.8 shlex.join function. Signed-off-by: Anders Kaseorg --- .../files/postgresql/pg_backup_and_purge | 2 +- scripts/lib/zulip_tools.py | 28 ++++--------------- tools/diagnose | 2 +- tools/review | 2 +- tools/test-js-with-puppeteer | 2 +- zerver/openapi/markdown_extension.py | 2 +- zerver/views/zephyr.py | 4 +-- 7 files changed, 11 insertions(+), 31 deletions(-) diff --git a/puppet/zulip/files/postgresql/pg_backup_and_purge b/puppet/zulip/files/postgresql/pg_backup_and_purge index 9af5b7b432..c3b4f8857c 100755 --- a/puppet/zulip/files/postgresql/pg_backup_and_purge +++ b/puppet/zulip/files/postgresql/pg_backup_and_purge @@ -18,7 +18,7 @@ logger = logging.getLogger(__name__) def run(args: List[str], dry_run: bool = False) -> str: if dry_run: - print("Would have run: " + " ".join(map(shlex.quote, args))) + print(f"Would have run: {shlex.join(args)}") return "" return subprocess.check_output(args, stdin=subprocess.DEVNULL, text=True) diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index e9ac1046bb..b825cfd93c 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -227,19 +227,14 @@ def release_deployment_lock() -> None: def run(args: Sequence[str], **kwargs: Any) -> None: # Output what we're doing in the `set -x` style - print("+ {}".format(" ".join(map(shlex.quote, args))), flush=True) + print(f"+ {shlex.join(args)}", flush=True) try: subprocess.check_call(args, **kwargs) except subprocess.CalledProcessError: print() print( - WHITEONRED - + "Error running a subcommand of {}: {}".format( - sys.argv[0], - " ".join(map(shlex.quote, args)), - ) - + ENDC + WHITEONRED + f"Error running a subcommand of {sys.argv[0]}: {shlex.join(args)}" + ENDC ) print(WHITEONRED + "Actual error output for the subcommand is just above this." + ENDC) print() @@ -258,7 +253,7 @@ def log_management_command(cmd: Sequence[str], log_path: str) -> None: logger.addHandler(file_handler) logger.setLevel(logging.INFO) - logger.info("Ran %s", " ".join(map(shlex.quote, cmd))) + logger.info("Ran %s", shlex.join(cmd)) def get_environment() -> str: @@ -567,7 +562,7 @@ def get_config_file() -> configparser.RawConfigParser: def get_deploy_options(config_file: configparser.RawConfigParser) -> List[str]: - return shlex.split(get_config(config_file, "deployment", "deploy_options", "").strip()) + return shlex.split(get_config(config_file, "deployment", "deploy_options", "")) def run_psql_as_postgres( @@ -575,20 +570,7 @@ def run_psql_as_postgres( sql_query: str, ) -> None: dbname = get_config(config_file, "postgresql", "database_name", "zulip") - subcmd = " ".join( - map( - shlex.quote, - [ - "psql", - "-v", - "ON_ERROR_STOP=1", - "-d", - dbname, - "-c", - sql_query, - ], - ) - ) + subcmd = shlex.join(["psql", "-v", "ON_ERROR_STOP=1", "-d", dbname, "-c", sql_query]) subprocess.check_call(["su", "postgres", "-c", subcmd]) diff --git a/tools/diagnose b/tools/diagnose index e992db50a2..1b05167c38 100755 --- a/tools/diagnose +++ b/tools/diagnose @@ -25,7 +25,7 @@ def run(check_func: Callable[[], bool]) -> None: def run_command(args: List[str]) -> None: - print(" ".join(map(shlex.quote, args))) + print(shlex.join(args)) subprocess.check_call(args) diff --git a/tools/review b/tools/review index 820c0e37d6..d00c5049b0 100755 --- a/tools/review +++ b/tools/review @@ -12,7 +12,7 @@ def exit(message: str) -> None: def run(command: List[str]) -> None: - print("\n>>> " + " ".join(map(shlex.quote, command))) + print(f"\n>>> {shlex.join(command)}") subprocess.check_call(command) diff --git a/tools/test-js-with-puppeteer b/tools/test-js-with-puppeteer index 46b2f7cb55..1aae323df1 100755 --- a/tools/test-js-with-puppeteer +++ b/tools/test-js-with-puppeteer @@ -73,7 +73,7 @@ def run_single_test(test_file: str, test_number: int, total_tests: int) -> int: ] test_name = os.path.basename(test_file) - cmd_str = " ".join(map(shlex.quote, cmd)) + cmd_str = shlex.join(cmd) print( f"\n\n===================== ({test_number}/{total_tests}) {test_name} =====================\nRunning {cmd_str}\n\n", flush=True, diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.py index f270719785..d120e9f217 100644 --- a/zerver/openapi/markdown_extension.py +++ b/zerver/openapi/markdown_extension.py @@ -300,7 +300,7 @@ def generate_curl_example( example_endpoint = endpoint.format_map(format_dict) curl_first_line_parts = ["curl", *curl_method_arguments(example_endpoint, method, api_url)] - lines.append(" ".join(map(shlex.quote, curl_first_line_parts))) + lines.append(shlex.join(curl_first_line_parts)) insecure_operations = ["/dev_fetch_api_key:post", "/fetch_api_key:post"] if operation_security is None: diff --git a/zerver/views/zephyr.py b/zerver/views/zephyr.py index d8917d3270..a434a7ee93 100644 --- a/zerver/views/zephyr.py +++ b/zerver/views/zephyr.py @@ -67,9 +67,7 @@ def webathena_kerberos_login( api_key, base64.b64encode(ccache).decode(), ] - subprocess.check_call( - ["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--", " ".join(map(shlex.quote, command))] - ) + subprocess.check_call(["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--", shlex.join(command)]) except subprocess.CalledProcessError: logging.exception("Error updating the user's ccache", stack_info=True) raise JsonableError(_("We were unable to set up mirroring for you"))