Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # subprocess - Subprocesses with accessible I/O streams |
| 2 | # |
| 3 | # For more information about this module, see PEP 324. |
| 4 | # |
| 5 | # This module should remain compatible with Python 2.2, see PEP 291. |
| 6 | # |
| 7 | # Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se> |
| 8 | # |
| 9 | # Licensed to PSF under a Contributor Agreement. |
| 10 | # See http://www.python.org/2.4/license for licensing details. |
| 11 | |
| 12 | def list2cmdline(seq): |
| 13 | """ |
| 14 | Translate a sequence of arguments into a command line |
| 15 | string, using the same rules as the MS C runtime: |
| 16 | |
| 17 | 1) Arguments are delimited by white space, which is either a |
| 18 | space or a tab. |
| 19 | |
| 20 | 2) A string surrounded by double quotation marks is |
| 21 | interpreted as a single argument, regardless of white space |
| 22 | contained within. A quoted string can be embedded in an |
| 23 | argument. |
| 24 | |
| 25 | 3) A double quotation mark preceded by a backslash is |
| 26 | interpreted as a literal double quotation mark. |
| 27 | |
| 28 | 4) Backslashes are interpreted literally, unless they |
| 29 | immediately precede a double quotation mark. |
| 30 | |
| 31 | 5) If backslashes immediately precede a double quotation mark, |
| 32 | every pair of backslashes is interpreted as a literal |
| 33 | backslash. If the number of backslashes is odd, the last |
| 34 | backslash escapes the next double quotation mark as |
| 35 | described in rule 3. |
| 36 | """ |
| 37 | |
| 38 | # See |
| 39 | # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp |
| 40 | result = [] |
| 41 | needquote = False |
| 42 | for arg in seq: |
| 43 | bs_buf = [] |
| 44 | |
| 45 | # Add a space to separate this argument from the others |
| 46 | if result: |
| 47 | result.append(' ') |
| 48 | |
| 49 | needquote = (" " in arg) or ("\t" in arg) or ("|" in arg) or arg == "" |
| 50 | if needquote: |
| 51 | result.append('"') |
| 52 | |
| 53 | for c in arg: |
| 54 | if c == '\\': |
| 55 | # Don't know if we need to double yet. |
| 56 | bs_buf.append(c) |
| 57 | elif c == '"': |
| 58 | # Double backspaces. |
| 59 | result.append('\\' * len(bs_buf)*2) |
| 60 | bs_buf = [] |
| 61 | result.append('\\"') |
| 62 | else: |
| 63 | # Normal char |
| 64 | if bs_buf: |
| 65 | result.extend(bs_buf) |
| 66 | bs_buf = [] |
| 67 | result.append(c) |
| 68 | |
| 69 | # Add remaining backspaces, if any. |
| 70 | if bs_buf: |
| 71 | result.extend(bs_buf) |
| 72 | |
| 73 | if needquote: |
| 74 | result.extend(bs_buf) |
| 75 | result.append('"') |
| 76 | |
| 77 | return ''.join(result) |