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