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 StringIO | |
8 import sys | |
9 | |
10 | |
11 VARIABLE_PATTERN = re.compile("^(?P<indentation>\s*)'(?P<name>[^']*)':\s*\[$") | |
12 EXCLUSION_PATTERN = re.compile("^(?:README|OWNERS|.*\.(pyc?|sh|swp)|.*~)$") | |
13 | |
14 DATA_SOURCES_PATH_FOR_VARIABLES = { | |
15 "net_test_support_data_sources": [ | |
16 "net/data/ssl/certificates", | |
17 ], | |
18 "net_unittests_data_sources": [ | |
19 "net/data/cert_issuer_source_aia_unittest", | |
20 "net/data/cert_issuer_source_static_unittest", | |
21 "net/data/certificate_policies_unittest", | |
22 "net/data/filter_unittests", | |
23 "net/data/name_constraints_unittest", | |
24 "net/data/parse_certificate_unittest", | |
25 "net/data/parse_ocsp_unittest", | |
26 "net/data/test.html", | |
27 "net/data/url_request_unittest", | |
28 "net/data/verify_certificate_chain_unittest", | |
29 "net/data/verify_name_match_unittest/names", | |
30 "net/data/verify_signed_data_unittest", | |
31 "net/third_party/nist-pkits/certs", | |
32 "net/third_party/nist-pkits/crls", | |
33 ], | |
34 } | |
35 | |
36 | |
37 def list_data_sources(root, paths, exclusion): | |
38 """Returns the list of data source found in |paths|. | |
39 | |
40 Args: | |
41 root: string, path to the repository root | |
42 paths: list of string, paths relative to repository root | |
43 exclusion: compiled regular expression, filename matching this pattern | |
44 will be excluded from the result | |
45 """ | |
46 data_sources = [] | |
47 for path in paths: | |
48 fullpath = os.path.normpath(os.path.join(root, path)) | |
49 if os.path.isfile(fullpath): | |
50 if not exclusion.match(os.path.basename(path)): | |
51 data_sources.append(path) | |
52 continue | |
53 | |
54 for dirpath, dirnames, filenames in os.walk(fullpath): | |
55 for filename in filenames: | |
56 if not exclusion.match(filename): | |
57 data_sources.append(os.path.normpath(os.path.join(dirpath, filename))) | |
58 return data_sources | |
59 | |
60 | |
61 def format_data_sources(name, dir, data_sources, indentation): | |
62 """Converts |data_sources| to a gyp variable assignment. | |
63 | |
64 Args: | |
65 name: string, name of the variable | |
66 dir: string, path to the directory containing the gyp file | |
67 data_sources: list of filenames | |
68 indentation: string | |
69 """ | |
70 buffer = StringIO.StringIO() | |
71 buffer.write("%s'%s': [\n" % (indentation, name)) | |
72 for data_source in sorted(data_sources): | |
73 buffer.write(" %s'%s',\n" % ( | |
74 indentation, os.path.relpath(data_source, dir))) | |
75 buffer.write("%s],\n" % (indentation,)) | |
76 return buffer.getvalue() | |
77 | |
78 | |
79 def save_file_if_changed(path, content): | |
80 """Writes |content| to file at |path| if file has changed. | |
81 | |
82 Args: | |
83 path: string, path of the file to save | |
84 content: string, content to write to file | |
85 """ | |
86 with open(path, "r") as file: | |
87 old_content = file.read() | |
88 if content != old_content: | |
89 with open(path, "w") as file: | |
90 file.write(content) | |
91 sys.stdout.write("updated %s, do not forget to run 'git add'\n" % (path,)) | |
92 | |
93 | |
94 def edit_file(path, root, data_sources_for_variables): | |
95 """Updates file at |path| by rewriting variables values. | |
96 | |
97 Args: | |
98 path: string, path of the file to edit | |
99 root: string, path to the repository root | |
100 data_sources_for_variables: dictionary mapping variable names to | |
101 the list of data sources to use | |
102 """ | |
103 dir = os.path.relpath(os.path.dirname(path), root) | |
104 buffer = StringIO.StringIO() | |
105 with open(path, "r") as file: | |
106 indentation = "" | |
107 current_var = None | |
108 for line in file: | |
109 if not current_var: | |
110 match = VARIABLE_PATTERN.match(line) | |
111 if not match: | |
112 buffer.write(line) | |
113 continue | |
114 variable = match.group("name") | |
115 if variable not in data_sources_for_variables: | |
116 buffer.write(line) | |
117 continue | |
118 current_var = variable | |
119 indentation = match.group("indentation") | |
120 buffer.write(format_data_sources( | |
121 variable, dir, data_sources_for_variables[variable], indentation)) | |
122 else: | |
123 if line == indentation + "],\n": | |
124 current_var = None | |
125 save_file_if_changed(path, buffer.getvalue()) | |
126 | |
127 | |
128 def main(args): | |
129 root_dir = os.path.normpath(os.path.join( | |
130 os.path.dirname(__file__), os.pardir, os.pardir)) | |
131 net_gypi = os.path.normpath(os.path.join(root_dir, "net", "net.gypi")) | |
132 | |
133 data_sources_for_variables = {} | |
134 for variable in DATA_SOURCES_PATH_FOR_VARIABLES: | |
135 data_sources_for_variables[variable] = list_data_sources( | |
136 root_dir, DATA_SOURCES_PATH_FOR_VARIABLES[variable], EXCLUSION_PATTERN) | |
137 | |
138 edit_file(net_gypi, root_dir, data_sources_for_variables) | |
139 | |
140 | |
141 if __name__ == "__main__": | |
142 sys.exit(main(sys.argv[1:])) | |
OLD | NEW |