OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 Google Inc. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """ |
| 6 This module helps emulate Visual Studio 2008 behavior on top of other |
| 7 build systems, primarily ninja. |
| 8 """ |
| 9 |
| 10 import re |
| 11 |
| 12 windows_quoter_regex = re.compile(r'(\\*)"') |
| 13 |
| 14 def QuoteCmdExeArgument(arg): |
| 15 """Quote a command line argument so that it appears as one argument when |
| 16 processed via cmd.exe and parsed by CommandLineToArgvW (as is typical for |
| 17 Windows programs).""" |
| 18 # See http://goo.gl/cuFbX and http://goo.gl/dhPnp including the comment |
| 19 # threads. This is actually the quoting rules for CommandLineToArgvW, not |
| 20 # for the shell, because the shell doesn't do anything in Windows. This |
| 21 # works more or less because most programs (including the compiler, etc.) |
| 22 # use that function to handle command line arguments. |
| 23 # |
| 24 # For a literal quote, CommandLineToArgvW requires 2n+1 backslashes |
| 25 # preceding it, and results in n backslashes + the quote. So we substitute |
| 26 # in 2* what we match, +1 more, plus the quote. |
| 27 tmp = windows_quoter_regex.sub(lambda mo: 2 * mo.group(1) + '\\"', arg) |
| 28 |
| 29 # Now, we need to escape some things that are actually for the shell. |
| 30 # ^-escape various characters that are otherwise interpreted by the shell. |
| 31 tmp = re.sub(r'([&|^])', r'^\1', tmp) |
| 32 |
| 33 # %'s also need to be doubled otherwise they're interpreted as batch |
| 34 # positional arguments. Also make sure to escape the % so that they're |
| 35 # passed literally through escaping so they can be singled to just the |
| 36 # original %. Otherwise, trying to pass the literal representation that |
| 37 # looks like an environment variable to the shell (e.g. %PATH%) would fail. |
| 38 tmp = tmp.replace('%', '^%^%') |
| 39 |
| 40 # Finally, wrap the whole thing in quotes so that the above quote rule |
| 41 # applies and whitespace isn't a word break. |
| 42 return '"' + tmp + '"' |
OLD | NEW |