Skip to content

Commit 9e4c054

Browse files
authored
Merge pull request #26024 from broadwaylamb/path-sanitizing-file-check-substitution
[utils] Fix PathSanitizingFileCheck to support paths with "weird" characters
2 parents 68687e7 + 9fbdfde commit 9e4c054

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

utils/PathSanitizingFileCheck

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ constants.""")
5656
"available if Windows compatibility is enabled.",
5757
action="store_true")
5858

59+
parser.add_argument(
60+
"--dry-run",
61+
help="Apply the replacements to the input and print the result "
62+
"to standard output",
63+
action="store_true")
64+
5965
args, unknown_args = parser.parse_known_args()
6066

6167
if args.enable_windows_compatibility:
@@ -70,19 +76,25 @@ constants.""")
7076

7177
for s in args.sanitize_strings:
7278
replacement, pattern = s.split('=', 1)
73-
# We are replacing the Unix path separators in the paths passed as
74-
# arguments with a broader pattern to also allow forward slashes and
75-
# double escaped slashes in the result that we are checking. Sigh.
76-
stdin = re.sub(re.sub(r'/', slashes_re, pattern), replacement, stdin)
77-
78-
p = subprocess.Popen(
79-
[args.file_check_path] + unknown_args, stdin=subprocess.PIPE)
80-
stdout, stderr = p.communicate(stdin)
81-
if stdout is not None:
82-
print(stdout)
83-
if stderr is not None:
84-
print(stderr, file=sys.stderr)
85-
return p.wait()
79+
# Since we want to use pattern as a regex in some platforms, we need
80+
# to escape it first, and then replace the escaped slash
81+
# literal (r'\\/') for our platform-dependent slash regex.
82+
stdin = re.sub(re.sub(r'\\/', slashes_re, re.escape(pattern)),
83+
replacement,
84+
stdin)
85+
86+
if args.dry_run:
87+
print(stdin)
88+
return 0
89+
else:
90+
p = subprocess.Popen(
91+
[args.file_check_path] + unknown_args, stdin=subprocess.PIPE)
92+
stdout, stderr = p.communicate(stdin)
93+
if stdout is not None:
94+
print(stdout)
95+
if stderr is not None:
96+
print(stderr, file=sys.stderr)
97+
return p.wait()
8698

8799

88100
if __name__ == '__main__':

0 commit comments

Comments
 (0)