Chromium Code Reviews
|
| 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 | |
|
Nico
2012/02/20 23:17:49
Needs module docstring
scottmg
2012/02/21 00:14:44
Done.
| |
| 5 import re | |
| 6 | |
| 7 windows_quoter_regex = re.compile(r'(\\*)"') | |
| 8 | |
| 9 def QuoteCmdExeArgument(arg): | |
|
Nico
2012/02/20 23:17:49
need function docstring
scottmg
2012/02/21 00:14:44
Done.
| |
| 10 # See http://goo.gl/cuFbX and http://goo.gl/dhPnp including the comment | |
|
Nico
2012/02/20 23:17:49
nit: I wouldn't use a URL shortener
scottmg
2012/02/21 00:14:44
The urls were longer than 80 so it looked yucky.
| |
| 11 # threads. This is actually the quoting rules for CommandLineToArgvW, not | |
| 12 # for the shell, because the shell doesn't do anything in Windows. This | |
| 13 # works more or less because most programs (including the compiler, etc.) | |
| 14 # use that function to handle command line arguments. | |
| 15 # | |
| 16 # For a literal quote, CommandLineToArgvW requires 2n+1 backslashes | |
| 17 # preceding it, and results in n backslashes + the quote. So we substitute | |
| 18 # in 2* what we match, +1 more, plus the quote. | |
| 19 tmp = windows_quoter_regex.sub(lambda mo: 2 * mo.group(1) + '\\"', arg) | |
| 20 | |
| 21 # Now, we need to escape some things that are actually for the shell. %'s | |
|
Nico
2012/02/20 23:17:49
The '%' handling happens further down, so I wouldn
scottmg
2012/02/21 00:14:44
Done.
| |
| 22 # need to be doubled, and various other characters need to be ^ escaped. | |
| 23 tmp = re.sub(r'([&|^])', r'^\1', tmp) | |
| 24 | |
| 25 # Also make sure to escape the % so that they're passed literally through | |
| 26 # escaping so they can be singled to just the original %. Otherwise, trying | |
| 27 # to pass the literal representation that looks like an environment variable | |
| 28 # to the shell (e.g. %PATH%) would fail. | |
| 29 tmp = tmp.replace('%', '^%^%') | |
| 30 | |
| 31 # Finally, wrap the whole thing in quotes so that the above quote rule | |
| 32 # applies and whitespace isn't a word break. | |
| 33 return '"' + tmp + '"' | |
|
Nico
2012/02/20 23:17:49
I assume all this is covered by existing tests.
scottmg
2012/02/21 00:14:44
Yes, this change is just trying to get existing te
| |
| OLD | NEW |