tools/setup: add gn_string() utility function

This commit is contained in:
Bert Belder 2018-09-25 14:45:27 -07:00
parent 1b9424e9d7
commit 023b4640fc
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
2 changed files with 21 additions and 1 deletions

View file

@ -54,6 +54,18 @@ gn_args_header = [
]
def gn_string(s):
# In gn, strings are enclosed in double-quotes and use backslash as the
# escape character. The only escape sequences supported are:
# \" (for literal quote)
# \$ (for literal dollars sign)
# \\ (for literal backslash)
# Any other use of a backslash is treated as a literal backslash.
s = re.sub(r'("|\$|\\(?=["$\\]))', r'\\\1', s)
s = '"' + s + '"'
return s
def gn_args_are_generated(lines):
for line in lines:
if re.match("^\s*#.*REMOVE THIS LINE", line):

View file

@ -1,11 +1,18 @@
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
import os
from setup import read_gn_args, write_gn_args
from setup import gn_string, read_gn_args, write_gn_args
from shutil import rmtree
from tempfile import mktemp
def gn_string_test():
assert '"abc"' == gn_string('abc')
assert '"foo\\$bar\\"baz"' == gn_string('foo$bar"baz')
assert '"do\\not\\escape"' == gn_string('do\\not\\escape')
assert '"so\\\\\\very\\\\\\"fun\\"' == gn_string('so\\\\very\\"fun\\')
def read_gn_args_test():
# Args file doesn't exist.
(args, hand_edited) = read_gn_args("/baddir/hopefully/nonexistent/args.gn")
@ -53,6 +60,7 @@ def write_gn_args_test():
def setup_test():
gn_string_test()
read_gn_args_test()
write_gn_args_test()