diff options
author | Bert Belder <bertbelder@gmail.com> | 2018-09-25 13:30:09 -0700 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2018-09-26 09:52:30 -0700 |
commit | 1b9424e9d78990100ac82697a20515f9cb9e8335 (patch) | |
tree | c8149dcb803b094ef341cb025702a8ad6fd02685 /tools/util.py | |
parent | 3a6da19eb80c5959e0fe0ff00b093b9f7d85d618 (diff) |
tools: add shell_quote() utility function
Diffstat (limited to 'tools/util.py')
-rw-r--r-- | tools/util.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tools/util.py b/tools/util.py index 659d9584b..442aeb143 100644 --- a/tools/util.py +++ b/tools/util.py @@ -39,6 +39,26 @@ def run_output(args, quiet=False, cwd=None, env=None, merge_env={}): return subprocess.check_output(args, cwd=cwd, env=env, shell=shell) +def shell_quote_win(arg): + if re.search(r'[\x00-\x20"^%~!@&?*<>|()=]', arg): + # Double all " quote characters. + arg = arg.replace('"', '""') + # Wrap the entire string in " quotes. + arg = '"' + arg + '"' + # Double any N backslashes that are immediately followed by a " quote. + arg = re.sub(r'(\\+)(?=")', r'\1\1', arg) + return arg + + +def shell_quote(arg): + if os.name == "nt": + return shell_quote_win(arg) + else: + # Python 2 has posix shell quoting built in, albeit in a weird place. + from pipes import quote + return quote(arg) + + def remove_and_symlink(target, name, target_is_dir=False): try: # On Windows, directory symlink can only be removed with rmdir(). |