Skip to content

[utils] Move quote_shell_command into swift_build_support. #2827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions utils/SwiftBuildSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
import configparser as ConfigParser

import os
import pipes
import platform
import subprocess
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))

# E402 means module level import not at top of file
from swift_build_support import shell # noqa (E402)


HOME = os.environ.get("HOME", "/")

Expand Down Expand Up @@ -70,10 +74,6 @@ def print_with_argv0(message):
sys.stdout.flush()


def quote_shell_command(args):
return " ".join([pipes.quote(a) for a in args])


def check_call(args, print_command=False, verbose=False, disable_sleep=False):
if disable_sleep:
if platform.system() == 'Darwin':
Expand All @@ -82,7 +82,7 @@ def check_call(args, print_command=False, verbose=False, disable_sleep=False):
args.insert(0, "caffeinate")

if print_command:
print(os.getcwd() + "$ " + quote_shell_command(args))
print(os.getcwd() + "$ " + shell.quote_command(args))
sys.stdout.flush()
try:
return subprocess.check_call(args)
Expand All @@ -94,15 +94,15 @@ def check_call(args, print_command=False, verbose=False, disable_sleep=False):
sys.exit(1)
except OSError as e:
print_with_argv0(
"could not execute '" + quote_shell_command(args) +
"could not execute '" + shell.quote_command(args) +
"': " + e.strerror)
sys.stdout.flush()
sys.exit(1)


def check_output(args, print_command=False, verbose=False):
if print_command:
print(os.getcwd() + "$ " + quote_shell_command(args))
print(os.getcwd() + "$ " + shell.quote_command(args))
sys.stdout.flush()
try:
return subprocess.check_output(args)
Expand All @@ -114,7 +114,7 @@ def check_output(args, print_command=False, verbose=False):
sys.exit(1)
except OSError as e:
print_with_argv0(
"could not execute '" + quote_shell_command(args) +
"could not execute '" + shell.quote_command(args) +
"': " + e.strerror)
sys.stdout.flush()
sys.exit(1)
Expand Down
3 changes: 1 addition & 2 deletions utils/build-script
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ from SwiftBuildSupport import (
get_all_preset_names,
get_preset_options,
print_with_argv0,
quote_shell_command,
) # noqa (E402 module level import not at top of file)

sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))
Expand Down Expand Up @@ -124,7 +123,7 @@ def main_preset():

print_with_argv0(
"using preset '" + args.preset + "', which expands to \n\n" +
quote_shell_command(build_script_args) + "\n")
shell.quote_command(build_script_args) + "\n")

check_call(build_script_args, disable_sleep=True)

Expand Down
9 changes: 9 additions & 0 deletions utils/swift_build_support/swift_build_support/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ def _quote(arg):
return pipes.quote(str(arg))


def quote_command(args):
"""
quote_command(args) -> str

Quote the command for passing to a shell.
"""
return ' '.join([_quote(a) for a in args])


def _coerce_dry_run(dry_run_override):
if dry_run_override is None:
return dry_run
Expand Down
3 changes: 3 additions & 0 deletions utils/swift_build_support/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def tearDown(self):
if os.path.exists(self.tmpdir):
shutil.rmtree(self.tmpdir)

def test_quote_command(self):
self.assertEqual(shell.quote_command(["a b", "", "c"]), "'a b' '' c")

def test_call(self):
shell.dry_run = False
foo_file = os.path.join(self.tmpdir, 'foo.txt')
Expand Down