OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2017 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 """Presubmit script for content/common.""" | |
6 | |
7 import os, re | |
8 | |
9 AX_JS_FILE = 'content/browser/resources/accessibility/accessibility.js' | |
10 AX_MODE_HEADER = 'content/common/accessibility_mode.h' | |
11 | |
12 # Given a full path to c++ header, return an array of the first | |
brettw
2017/06/20 16:48:00
This seems to return many items, so I'm getting co
dougt
2017/06/25 21:42:21
In a c++ there may be multiple static constexpr de
| |
13 # static constexpr defined. | |
14 def GetConstexprFromFile(fullpath): | |
15 values = [] | |
16 for line in open(fullpath).readlines(): | |
17 # Strip out comments | |
18 line = re.sub('//.*', '', line) | |
19 | |
20 # Look for lines of the form "static constexpr <type> NAME " | |
21 m = re.search('static constexpr [\w]+ ([\w]+)', line) | |
22 if m: | |
23 values.append(m.group(1)) | |
24 continue | |
brettw
2017/06/20 16:48:01
My Python is rusty, but is this continue statement
dougt
2017/06/25 21:42:21
Done.
| |
25 return values | |
26 | |
27 # Given a full path to js file, return the AccessibilityMode consts | |
28 # defined | |
29 def GetAccessibilityModesFromFile(fullpath): | |
30 values = [] | |
31 inside = False | |
32 for line in open(fullpath).readlines(): | |
33 # Strip out comments | |
34 line = re.sub('//.*', '', line) | |
35 | |
36 # Look for the block of code that defines AccessibilityMode | |
37 m = re.search('const AccessibilityMode = {', line) | |
38 if m: | |
39 inside = True | |
40 continue | |
41 | |
42 # Look for a "}" character signifying the end of an enum | |
43 if line.find('};') >= 0: | |
44 return values | |
45 | |
46 if not inside: | |
47 continue | |
48 | |
49 m = re.search('([\w]+):', line) | |
50 if m: | |
51 values.append(m.group(1)) | |
52 continue | |
53 | |
54 # getters | |
55 m = re.search('get ([\w]+)\(\)', line) | |
56 if m: | |
57 values.append(m.group(1)) | |
58 return values | |
59 | |
60 # Make sure that the modes defined in the C++ header match those deifned in | |
brettw
2017/06/20 16:48:00
"deifned"
dougt
2017/06/25 21:42:21
Done.
| |
61 # the js file. Note that this doesn't guarantee that the values are the same, | |
62 # but does make sure if we add or remove we can signal to the developer that | |
63 # they should be aware that this dependency exists. | |
64 def CheckModesMatch(input_api, output_api): | |
65 errs = [] | |
66 repo_root = input_api.change.RepositoryRoot() | |
67 | |
68 ax_modes_in_header = GetConstexprFromFile( | |
69 os.path.join(repo_root,AX_MODE_HEADER)) | |
70 ax_modes_in_js = GetAccessibilityModesFromFile( | |
71 os.path.join(repo_root, AX_JS_FILE)) | |
72 | |
73 for value in ax_modes_in_header: | |
74 if value not in ax_modes_in_js: | |
75 errs.append(output_api.PresubmitError( | |
76 'Found %s in %s, but did not find %s in %s' % ( | |
77 value, AX_MODE_HEADER, value, AX_JS_FILE))) | |
78 return errs | |
79 | |
80 def CheckChangeOnUpload(input_api, output_api): | |
81 if AX_MODE_HEADER not in input_api.LocalPaths(): | |
82 return [] | |
83 return CheckModesMatch(input_api, output_api) | |
84 | |
85 def CheckChangeOnCommit(input_api, output_api): | |
86 if AX_MODE_HEADER not in input_api.LocalPaths(): | |
87 return [] | |
88 return CheckModesMatch(input_api, output_api) | |
OLD | NEW |