Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: net/data/update_net_gypi.py

Issue 1923203002: Clean net_test_support_data_sources and net_unittests_data_source. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@{interstitial}
Patch Set: Address comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/net.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/certificate_policies_unittest",
20 "net/data/name_constraints_unittest",
21 "net/data/parse_certificate_unittest",
22 "net/data/parse_ocsp_unittest",
23 "net/data/test.html",
24 "net/data/url_request_unittest",
25 "net/data/verify_certificate_chain_unittest",
26 "net/data/verify_name_match_unittest/names",
27 "net/data/verify_signed_data_unittest",
28 "net/third_party/nist-pkits/certs",
29 "net/third_party/nist-pkits/crls",
30 ],
31 }
32
33
34 def list_data_sources(root, paths, exclusion):
35 """Returns the list of data source found in |paths|.
36
37 Args:
38 root: string, path to the repository root
39 paths: list of string, paths relative to repository root
40 exclusion: compiled regular expression, filename matching this pattern
41 will be excluded from the result
42 """
43 data_sources = []
44 for path in paths:
45 fullpath = os.path.normpath(os.path.join(root, path))
46 if os.path.isfile(fullpath):
47 if not exclusion.match(os.path.basename(path)):
48 data_sources.append(path)
49 continue
50
51 for dirpath, dirnames, filenames in os.walk(fullpath):
52 for filename in filenames:
53 if not exclusion.match(filename):
54 data_sources.append(os.path.normpath(os.path.join(dirpath, filename)))
55 return data_sources
56
57
58 def format_data_sources(name, dir, data_sources, indentation):
59 """Converts |data_sources| to a gyp variable assignment.
60
61 Args:
62 name: string, name of the variable
63 dir: string, path to the directory containing the gyp file
64 data_sources: list of filenames
65 indentation: string
66 """
67 buffer = StringIO.StringIO()
68 buffer.write("%s'%s': [\n" % (indentation, name))
69 for data_source in sorted(data_sources):
70 buffer.write(" %s'%s',\n" % (
71 indentation, os.path.relpath(data_source, dir)))
72 buffer.write("%s],\n" % (indentation,))
73 return buffer.getvalue()
74
75
76 def save_file_if_changed(path, content):
77 """Writes |content| to file at |path| if file has changed.
78
79 Args:
80 path: string, path of the file to save
81 content: string, content to write to file
82 """
83 with open(path, "r") as file:
84 old_content = file.read()
85 if content != old_content:
86 with open(path, "w") as file:
87 file.write(content)
88 sys.stdout.write("updated %s, do not forget to run 'git add'\n" % (path,))
89
90
91 def edit_file(path, root, data_sources_for_variables):
92 """Updates file at |path| by rewriting variables values.
93
94 Args:
95 path: string, path of the file to edit
96 root: string, path to the repository root
97 data_sources_for_variables: dictionary mapping variable names to
98 the list of data sources to use
99 """
100 dir = os.path.relpath(os.path.dirname(path), root)
101 buffer = StringIO.StringIO()
102 with open(path, "r") as file:
103 indentation = ""
104 current_var = None
105 for line in file:
106 if not current_var:
107 match = VARIABLE_PATTERN.match(line)
108 if not match:
109 buffer.write(line)
110 continue
111 variable = match.group("name")
112 if variable not in data_sources_for_variables:
113 buffer.write(line)
114 continue
115 current_var = variable
116 indentation = match.group("indentation")
117 buffer.write(format_data_sources(
118 variable, dir, data_sources_for_variables[variable], indentation))
119 else:
120 if line == indentation + "],\n":
121 current_var = None
122 save_file_if_changed(path, buffer.getvalue())
123
124
125 def main(args):
126 root_dir = os.path.normpath(os.path.join(
127 os.path.dirname(__file__), os.pardir, os.pardir))
128 net_gypi = os.path.normpath(os.path.join(root_dir, "net", "net.gypi"))
129
130 data_sources_for_variables = {}
131 for variable in DATA_SOURCES_PATH_FOR_VARIABLES:
132 data_sources_for_variables[variable] = list_data_sources(
133 root_dir, DATA_SOURCES_PATH_FOR_VARIABLES[variable], EXCLUSION_PATTERN)
134
135 edit_file(net_gypi, root_dir, data_sources_for_variables)
136
137
138 if __name__ == "__main__":
139 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698