python: Use Python 3.8 shlex.join function.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-04-26 17:10:28 -07:00 committed by Tim Abbott
parent 71390fb635
commit 098a514599
7 changed files with 11 additions and 31 deletions

View File

@ -18,7 +18,7 @@ logger = logging.getLogger(__name__)
def run(args: List[str], dry_run: bool = False) -> str: def run(args: List[str], dry_run: bool = False) -> str:
if dry_run: if dry_run:
print("Would have run: " + " ".join(map(shlex.quote, args))) print(f"Would have run: {shlex.join(args)}")
return "" return ""
return subprocess.check_output(args, stdin=subprocess.DEVNULL, text=True) return subprocess.check_output(args, stdin=subprocess.DEVNULL, text=True)

View File

@ -227,19 +227,14 @@ def release_deployment_lock() -> None:
def run(args: Sequence[str], **kwargs: Any) -> None: def run(args: Sequence[str], **kwargs: Any) -> None:
# Output what we're doing in the `set -x` style # 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: try:
subprocess.check_call(args, **kwargs) subprocess.check_call(args, **kwargs)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print() print()
print( print(
WHITEONRED WHITEONRED + f"Error running a subcommand of {sys.argv[0]}: {shlex.join(args)}" + ENDC
+ "Error running a subcommand of {}: {}".format(
sys.argv[0],
" ".join(map(shlex.quote, args)),
)
+ ENDC
) )
print(WHITEONRED + "Actual error output for the subcommand is just above this." + ENDC) print(WHITEONRED + "Actual error output for the subcommand is just above this." + ENDC)
print() print()
@ -258,7 +253,7 @@ def log_management_command(cmd: Sequence[str], log_path: str) -> None:
logger.addHandler(file_handler) logger.addHandler(file_handler)
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
logger.info("Ran %s", " ".join(map(shlex.quote, cmd))) logger.info("Ran %s", shlex.join(cmd))
def get_environment() -> str: def get_environment() -> str:
@ -567,7 +562,7 @@ def get_config_file() -> configparser.RawConfigParser:
def get_deploy_options(config_file: configparser.RawConfigParser) -> List[str]: 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( def run_psql_as_postgres(
@ -575,20 +570,7 @@ def run_psql_as_postgres(
sql_query: str, sql_query: str,
) -> None: ) -> None:
dbname = get_config(config_file, "postgresql", "database_name", "zulip") dbname = get_config(config_file, "postgresql", "database_name", "zulip")
subcmd = " ".join( subcmd = shlex.join(["psql", "-v", "ON_ERROR_STOP=1", "-d", dbname, "-c", sql_query])
map(
shlex.quote,
[
"psql",
"-v",
"ON_ERROR_STOP=1",
"-d",
dbname,
"-c",
sql_query,
],
)
)
subprocess.check_call(["su", "postgres", "-c", subcmd]) subprocess.check_call(["su", "postgres", "-c", subcmd])

View File

@ -25,7 +25,7 @@ def run(check_func: Callable[[], bool]) -> None:
def run_command(args: List[str]) -> None: def run_command(args: List[str]) -> None:
print(" ".join(map(shlex.quote, args))) print(shlex.join(args))
subprocess.check_call(args) subprocess.check_call(args)

View File

@ -12,7 +12,7 @@ def exit(message: str) -> None:
def run(command: List[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) subprocess.check_call(command)

View File

@ -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) test_name = os.path.basename(test_file)
cmd_str = " ".join(map(shlex.quote, cmd)) cmd_str = shlex.join(cmd)
print( print(
f"\n\n===================== ({test_number}/{total_tests}) {test_name} =====================\nRunning {cmd_str}\n\n", f"\n\n===================== ({test_number}/{total_tests}) {test_name} =====================\nRunning {cmd_str}\n\n",
flush=True, flush=True,

View File

@ -300,7 +300,7 @@ def generate_curl_example(
example_endpoint = endpoint.format_map(format_dict) example_endpoint = endpoint.format_map(format_dict)
curl_first_line_parts = ["curl", *curl_method_arguments(example_endpoint, method, api_url)] 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"] insecure_operations = ["/dev_fetch_api_key:post", "/fetch_api_key:post"]
if operation_security is None: if operation_security is None:

View File

@ -67,9 +67,7 @@ def webathena_kerberos_login(
api_key, api_key,
base64.b64encode(ccache).decode(), base64.b64encode(ccache).decode(),
] ]
subprocess.check_call( subprocess.check_call(["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--", shlex.join(command)])
["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--", " ".join(map(shlex.quote, command))]
)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
logging.exception("Error updating the user's ccache", stack_info=True) logging.exception("Error updating the user's ccache", stack_info=True)
raise JsonableError(_("We were unable to set up mirroring for you")) raise JsonableError(_("We were unable to set up mirroring for you"))