26 """Find the closing mustache for a given number of open mustaches."""
28 start_len =
len(contents)
31 if contents[0] ==
'"':
32 contents = contents[1:]
33 while contents[0] !=
'"':
34 if contents.startswith(
'\\\\'):
35 contents = contents[2:]
36 elif contents.startswith(
'\\"'):
37 contents = contents[2:]
39 contents = contents[1:]
40 contents = contents[1:]
42 elif contents.startswith(
"'{'")
or contents.startswith(
43 "'\"'")
or contents.startswith(
"'}'"):
44 contents = contents[3:]
46 elif contents.startswith(
"//"):
47 contents = contents[contents.find(
'\n'):]
48 elif contents.startswith(
"/*"):
49 contents = contents[contents.find(
'*/') + 2:]
51 elif contents[0] ==
'{':
52 contents = contents[1:]
54 elif contents[0] ==
'}':
55 contents = contents[1:]
58 return start_len -
len(contents)
61 contents = contents[1:]
66 """See if the matching line begins with #define"""
68 m = re.search(
r"^#define.*{}$".
format(match.group(0)), body[:match.end()],
74 """Scan the contents of a file, and for top-level namespaces in namespaces remove redundant usages."""
77 m = re.search(
r'namespace ([a-zA-Z0-9_]*) {', contents)
81 output += contents[:m.end()]
82 contents = contents[m.end():]
85 print(
'Failed to find closing mustache for namespace {}'.
format(
87 print(
'Remaining text:')
91 namespace = m.group(1)
92 if namespace
in namespaces:
95 m = re.search(
r'\b' + namespace +
r'::\b', body)
99 if m.start() >= 2
and body[m.start() - 2:].startswith(
'::'):
100 output += body[:m.end()]
103 output += body[:m.end()]
105 output += body[:m.start()]
106 body = body[m.end():]
108 contents = contents[end:]
134 if output != _TEST_EXPECTED:
136 print(
'FAILED: self check')
138 difflib.ndiff(_TEST_EXPECTED.splitlines(1), output.splitlines(1))))
142 Config = collections.namedtuple(
'Config', [
'dirs',
'namespaces'])
144 _CONFIGURATION = (
Config([
'src/core',
'test/core'], [
'grpc_core']),)
148 for config
in _CONFIGURATION:
149 for dir
in config.dirs:
150 for root, dirs, files
in os.walk(dir):
152 if file.endswith(
'.cc')
or file.endswith(
'.h'):
153 path = os.path.join(root, file)
155 with open(path)
as f:
160 if updated != contents:
162 with open(os.path.join(root, file),
'w')
as f:
166 print(
'The following files were changed:')