Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. 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 import os | |
| 6 import re | |
| 7 import sys | |
| 8 | |
| 9 try: | |
| 10 import cStringIO as StringIO | |
|
eroman
2016/04/27 16:23:14
I would remove this and just import StringIO -- I
sdefresne
2016/04/30 12:31:40
Done.
| |
| 11 except ImportError: | |
| 12 import StringIO | |
| 13 | |
| 14 | |
| 15 VARIABLE_PATTERN = re.compile("^(?P<indentation>\s*)'(?P<name>[^']*)':\s*\[$") | |
| 16 EXCLUSION_PATTERN = re.compile("^(?:README|OWNERS|.*\.(pyc?|sh))$") | |
|
eroman
2016/04/27 16:23:14
Can also exclude editor swap files:
*.swp
*~
Whe
sdefresne
2016/04/30 12:31:40
Done.
| |
| 17 | |
| 18 DATA_SOURCES_PATH_FOR_VARIABLES = { | |
| 19 "net_test_support_data_sources": [ | |
| 20 "net/data/ssl/certificates", | |
| 21 ], | |
| 22 "net_unittests_data_sources": [ | |
| 23 "net/data/certificate_policies_unittest", | |
| 24 "net/data/name_constraints_unittest", | |
| 25 "net/data/parse_certificate_unittest", | |
| 26 "net/data/parse_ocsp_unittest", | |
| 27 "net/data/test.html", | |
| 28 "net/data/url_request_unittest", | |
| 29 "net/data/verify_certificate_chain_unittest", | |
| 30 "net/data/verify_name_match_unittest/names", | |
| 31 "net/data/verify_signed_data_unittest", | |
| 32 "net/third_party/nist-pkits/certs", | |
| 33 "net/third_party/nist-pkits/crls", | |
| 34 ], | |
| 35 } | |
| 36 | |
| 37 | |
| 38 def RelativePath(dir, path): | |
|
eroman
2016/04/27 16:23:14
style: functions_are_like_this() -- https://google
sdefresne
2016/04/30 12:31:41
Done.
| |
| 39 """Converts |path| to be relative to |dir|. | |
|
eroman
2016/04/27 16:23:14
Can you use os.path.relpath() ?
Or alternately us
sdefresne
2016/04/30 12:31:40
Done.
| |
| 40 | |
| 41 Args: | |
| 42 dir: string, a directory path | |
| 43 path: string, path that should be converted to relative path. | |
| 44 """ | |
| 45 if not dir or dir == os.path.curdir or os.path.isabs(path): | |
| 46 return path | |
| 47 if dir == os.path.pardir: | |
| 48 return os.path.join(dir, path) | |
| 49 if dir == path: | |
| 50 return os.path.curdir | |
| 51 if path.startswith(dir) and path[len(dir)] in (os.path.sep, os.path.altsep): | |
| 52 return path[len(dir) + 1:] | |
| 53 return os.path.join(os.path.pardir, RelativePath(os.path.dirname(dir), path)) | |
| 54 | |
| 55 | |
| 56 def ListDataSources(root, paths, exclusion): | |
| 57 """Returns the list of data source found in |paths|. | |
| 58 | |
| 59 Args: | |
| 60 root: string, path to the repository root | |
| 61 paths: list of string, paths relative to repository root | |
| 62 exclusion: compiled regular expression, filename matching this pattern | |
| 63 will be excluded from the result | |
| 64 """ | |
| 65 data_sources = [] | |
| 66 for path in paths: | |
| 67 fullpath = os.path.normpath(os.path.join(root, path)) | |
| 68 if os.path.isfile(fullpath): | |
| 69 if not exclusion.match(os.path.basename(path)): | |
| 70 data_sources.append(path) | |
| 71 continue | |
| 72 | |
| 73 for dirpath, dirnames, filenames in os.walk(fullpath): | |
| 74 for filename in filenames: | |
| 75 if not exclusion.match(filename): | |
| 76 data_sources.append(os.path.normpath(os.path.join(dirpath, filename))) | |
| 77 return data_sources | |
| 78 | |
| 79 | |
| 80 def FormatDataSources(name, dir, data_sources, indentation): | |
| 81 """Converts |data_sources| to a gyp variable assignment. | |
| 82 | |
| 83 Args: | |
| 84 name: string, name of the variable | |
| 85 dir: string, path to the directory containing the gyp file | |
| 86 data_sources: list of filenames | |
| 87 indentation: string | |
| 88 """ | |
| 89 buffer = StringIO.StringIO() | |
| 90 buffer.write("%s'%s': [\n" % (indentation, name)) | |
| 91 for data_source in sorted(data_sources): | |
| 92 buffer.write(" %s'%s',\n" % (indentation, RelativePath(dir, data_source))) | |
| 93 buffer.write("%s],\n" % (indentation,)) | |
| 94 return buffer.getvalue() | |
| 95 | |
| 96 | |
| 97 def EditFile(path, root, data_sources_for_variables): | |
| 98 """Updates file at |path| by rewriting variables values. | |
| 99 | |
| 100 Args: | |
| 101 path: string, path of the file to edit | |
| 102 root: string, path to the repository root | |
| 103 data_sources_for_variables: dictionary mapping variable names to | |
| 104 the list of data sources to use | |
| 105 """ | |
| 106 dir = RelativePath(root, os.path.dirname(path)) | |
| 107 buffer = StringIO.StringIO() | |
| 108 with open(path, 'r') as file: | |
| 109 indentation = "" | |
| 110 current_var = None | |
| 111 for line in file: | |
| 112 if not current_var: | |
| 113 match = VARIABLE_PATTERN.match(line) | |
| 114 if not match: | |
| 115 buffer.write(line) | |
| 116 continue | |
| 117 variable = match.group('name') | |
| 118 if variable not in data_sources_for_variables: | |
| 119 buffer.write(line) | |
| 120 continue | |
| 121 current_var = variable | |
| 122 indentation = match.group('indentation') | |
| 123 buffer.write(FormatDataSources( | |
| 124 variable, dir, data_sources_for_variables[variable], indentation)) | |
| 125 else: | |
| 126 if line == indentation + '],\n': | |
| 127 current_var = None | |
| 128 with open(path, 'w') as file: | |
| 129 file.write(buffer.getvalue()) | |
|
eroman
2016/04/27 16:23:14
nit: I suggest including an output message from th
sdefresne
2016/04/30 12:31:41
Done.
| |
| 130 | |
| 131 | |
| 132 def Main(args): | |
| 133 root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), "../..")) | |
|
eroman
2016/04/27 16:23:14
nit: rather than "../../" would be more general to
sdefresne
2016/04/30 12:31:41
Done.
| |
| 134 net_gypi = os.path.normpath(os.path.join(root_dir, "net/net.gypi")) | |
| 135 | |
| 136 data_sources_for_variables = {} | |
| 137 for variable in DATA_SOURCES_PATH_FOR_VARIABLES: | |
| 138 data_sources_for_variables[variable] = ListDataSources( | |
| 139 root_dir, DATA_SOURCES_PATH_FOR_VARIABLES[variable], EXCLUSION_PATTERN) | |
| 140 | |
| 141 EditFile(net_gypi, root_dir, data_sources_for_variables) | |
| 142 | |
| 143 | |
| 144 if __name__ == '__main__': | |
| 145 sys.exit(Main(sys.argv[1:])) | |
| OLD | NEW |