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 """Presubmit script for ui/accessibility.""" | |
| 6 | |
| 7 import os, re | |
| 8 | |
| 9 AX_IDL = 'ui/accessibility/ax_enums.idl' | |
| 10 AUTOMATION_IDL = 'chrome/common/extensions/api/automation.idl' | |
|
David Tseng
2016/08/31 19:39:29
Thanks for doing this. Can we check that anyone wh
dmazzoni
2016/08/31 19:51:42
Sounds like that'd be a good follow-up. I don't wa
David Tseng
2016/08/31 20:51:46
How about just changes to ax_enums.idl?
I mentio
| |
| 11 | |
| 12 def InitialLowerCamelCase(unix_name): | |
| 13 words = unix_name.split('_') | |
| 14 return words[0] + ''.join(word.capitalize() for word in words[1:]) | |
| 15 | |
| 16 # Given a full path to an IDL file containing enum definitions, | |
| 17 # parse the file for enums and return a dict mapping the enum name | |
| 18 # to a list of values for that enum. | |
| 19 def GetEnumsFromFile(fullpath): | |
| 20 enum_name = None | |
| 21 enums = {} | |
| 22 for line in open(fullpath).readlines(): | |
| 23 # Strip out comments | |
| 24 line = re.sub('//.*', '', line) | |
| 25 | |
| 26 # Look for lines of the form "enum ENUM_NAME {" and get the enum_name | |
| 27 m = re.search('enum ([\w]+) {', line) | |
| 28 if m: | |
| 29 enum_name = m.group(1) | |
| 30 continue | |
| 31 | |
| 32 # Look for a "}" character signifying the end of an enum | |
| 33 if line.find('}') >= 0: | |
| 34 enum_name = None | |
| 35 continue | |
| 36 | |
| 37 if not enum_name: | |
| 38 continue | |
| 39 | |
| 40 # If we're inside an enum definition, add the first string consisting of | |
| 41 # alphanumerics plus underscore ("\w") to the list of values for that enum. | |
| 42 m = re.search('([\w]+)', line) | |
| 43 if m: | |
| 44 enums.setdefault(enum_name, []) | |
| 45 enums[enum_name].append(m.group(1)) | |
| 46 | |
| 47 return enums | |
| 48 | |
| 49 def CheckMatchingEnum(ax_enums, | |
| 50 ax_enum_name, | |
| 51 automation_enums, | |
| 52 automation_enum_name, | |
| 53 errs, | |
| 54 output_api): | |
| 55 if ax_enum_name not in ax_enums: | |
| 56 errs.append(output_api.PresubmitError( | |
| 57 'Expected %s to have an enum named %s' % (AX_IDL, ax_enum_name))) | |
| 58 return | |
| 59 if automation_enum_name not in automation_enums: | |
| 60 errs.append(output_api.PresubmitError( | |
| 61 'Expected %s to have an enum named %s' % ( | |
| 62 AUTOMATION_IDL, automation_enum_name))) | |
| 63 return | |
| 64 src = ax_enums[ax_enum_name] | |
| 65 dst = automation_enums[automation_enum_name] | |
| 66 for value in src: | |
| 67 if InitialLowerCamelCase(value) not in dst: | |
| 68 errs.append(output_api.PresubmitError( | |
| 69 'Found %s.%s in %s, but did not find %s.%s in %s' % ( | |
| 70 ax_enum_name, value, AX_IDL, | |
| 71 automation_enum_name, InitialLowerCamelCase(value), | |
| 72 AUTOMATION_IDL))) | |
| 73 | |
| 74 def CheckEnumsMatch(input_api, output_api): | |
| 75 repo_root = input_api.change.RepositoryRoot() | |
| 76 ax_enums = GetEnumsFromFile(os.path.join(repo_root, AX_IDL)) | |
| 77 automation_enums = GetEnumsFromFile(os.path.join(repo_root, AUTOMATION_IDL)) | |
| 78 errs = [] | |
| 79 CheckMatchingEnum(ax_enums, 'AXRole', automation_enums, 'RoleType', errs, | |
| 80 output_api) | |
| 81 CheckMatchingEnum(ax_enums, 'AXState', automation_enums, 'StateType', errs, | |
| 82 output_api) | |
| 83 CheckMatchingEnum(ax_enums, 'AXEvent', automation_enums, 'EventType', errs, | |
| 84 output_api) | |
| 85 return errs | |
| 86 | |
| 87 def CheckChangeOnUpload(input_api, output_api): | |
| 88 if AX_IDL not in input_api.LocalPaths(): | |
| 89 return [] | |
| 90 return CheckEnumsMatch(input_api, output_api) | |
| 91 | |
| 92 def CheckChangeOnCommit(input_api, output_api): | |
| 93 if AX_IDL not in input_api.LocalPaths(): | |
| 94 return [] | |
| 95 return CheckEnumsMatch(input_api, output_api) | |
| OLD | NEW |