OLD | NEW |
1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Presubmit script for sync | 5 """Presubmit script for sync |
6 This checks that ModelTypeInfo entries in model_type.cc follow conventions. | 6 This checks that ModelTypeInfo entries in model_type.cc follow conventions. |
7 See CheckModelTypeInfoMap or model_type.cc for more detail on the rules. | 7 See CheckModelTypeInfoMap or model_type.cc for more detail on the rules. |
8 """ | 8 """ |
9 | 9 |
10 import os | 10 import os |
11 | 11 |
12 # Some definitions don't follow all the conventions we want to enforce. | 12 # Some definitions don't follow all the conventions we want to enforce. |
13 # It's either difficult or impossible to fix this, so we ignore the problem(s). | 13 # It's either difficult or impossible to fix this, so we ignore the problem(s). |
14 GRANDFATHERED_MODEL_TYPES = [ | 14 GRANDFATHERED_MODEL_TYPES = [ |
15 'UNSPECIFIED', # Doesn't have a root tag or notification type. | 15 'UNSPECIFIED', # Doesn't have a root tag or notification type. |
16 'TOP_LEVEL_FOLDER', # Doesn't have a root tag or notification type. | 16 'TOP_LEVEL_FOLDER', # Doesn't have a root tag or notification type. |
17 'AUTOFILL_WALLET_DATA', # Root tag and model type string lack DATA suffix. | 17 'AUTOFILL_WALLET_DATA', # Root tag and model type string lack DATA suffix. |
18 'APP_SETTINGS', # Model type string has inconsistent capitalization. | 18 'APP_SETTINGS', # Model type string has inconsistent capitalization. |
19 'EXTENSION_SETTINGS', # Model type string has inconsistent capitalization. | 19 'EXTENSION_SETTINGS', # Model type string has inconsistent capitalization. |
20 'SUPERVISED_USER_SETTINGS', # Root tag and model type string replace | 20 'SUPERVISED_USER_SETTINGS', # Root tag and model type string replace |
21 # 'Supervised' with 'Managed' | 21 # 'Supervised' with 'Managed' |
22 'SUPERVISED_USERS', # See previous. | 22 'SUPERVISED_USERS', # See previous. |
23 'SUPERVISED_USER_WHITELISTS', # See previous. | 23 'SUPERVISED_USER_WHITELISTS', # See previous. |
24 'SUPERVISED_USER_SHARED_SETTINGS', # See previous. | 24 'SUPERVISED_USER_SHARED_SETTINGS', # See previous. |
25 'PROXY_TABS', # Doesn't have a root tag or notification type. | 25 'PROXY_TABS', # Doesn't have a root tag or notification type. |
26 'NIGORI'] # Model type string is 'encryption keys'. | 26 'NIGORI'] # Model type string is 'encryption keys'. |
27 | 27 |
| 28 # Root tags are used as prefixes when creating storage keys, so certain strings |
| 29 # are blacklisted in order to prevent prefix collision. |
| 30 BLACKLISTED_ROOT_TAGS = [ |
| 31 '_mts_schema_descriptor' |
| 32 ] |
| 33 |
28 # Number of distinct fields in a map entry; used to create | 34 # Number of distinct fields in a map entry; used to create |
29 # sets that check for uniqueness. | 35 # sets that check for uniqueness. |
30 MAP_ENTRY_FIELD_COUNT = 6 | 36 MAP_ENTRY_FIELD_COUNT = 6 |
31 | 37 |
32 # String that precedes the ModelType when referencing the | 38 # String that precedes the ModelType when referencing the |
33 # proto field number enum e.g. | 39 # proto field number enum e.g. |
34 # sync_pb::EntitySpecifics::kManagedUserFieldNumber. | 40 # sync_pb::EntitySpecifics::kManagedUserFieldNumber. |
35 # Used to map from enum references to the ModelType. | 41 # Used to map from enum references to the ModelType. |
36 FIELD_NUMBER_PREFIX = 'sync_pb::EntitySpecifics::k' | 42 FIELD_NUMBER_PREFIX = 'sync_pb::EntitySpecifics::k' |
37 | 43 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 proto_field_definitions = ParseSyncProtoFieldIdentifiers( | 112 proto_field_definitions = ParseSyncProtoFieldIdentifiers( |
107 input_api, os.path.abspath(PROTO_FILE_PATH)) | 113 input_api, os.path.abspath(PROTO_FILE_PATH)) |
108 accumulated_problems.extend( | 114 accumulated_problems.extend( |
109 CheckNoDuplicatedFieldValues(output_api, map_entries)) | 115 CheckNoDuplicatedFieldValues(output_api, map_entries)) |
110 | 116 |
111 for map_entry in map_entries: | 117 for map_entry in map_entries: |
112 entry_problems = [] | 118 entry_problems = [] |
113 entry_problems.extend( | 119 entry_problems.extend( |
114 CheckNotificationTypeMatchesProtoMessageName( | 120 CheckNotificationTypeMatchesProtoMessageName( |
115 output_api, map_entry, proto_field_definitions)) | 121 output_api, map_entry, proto_field_definitions)) |
| 122 entry_problems.extend(CheckRootTagNotInBlackList(output_api, map_entry)) |
116 | 123 |
117 if map_entry.model_type not in GRANDFATHERED_MODEL_TYPES: | 124 if map_entry.model_type not in GRANDFATHERED_MODEL_TYPES: |
118 entry_problems.extend( | 125 entry_problems.extend( |
119 CheckModelTypeStringMatchesModelType(output_api, map_entry)) | 126 CheckModelTypeStringMatchesModelType(output_api, map_entry)) |
120 entry_problems.extend( | 127 entry_problems.extend( |
121 CheckRootTagMatchesModelType(output_api, map_entry)) | 128 CheckRootTagMatchesModelType(output_api, map_entry)) |
122 | 129 |
123 if len(entry_problems) > 0: | 130 if len(entry_problems) > 0: |
124 accumulated_problems.extend(entry_problems) | 131 accumulated_problems.extend(entry_problems) |
125 | 132 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 expected_root_tag = map_entry.model_type.lower() | 343 expected_root_tag = map_entry.model_type.lower() |
337 if (StripTrailingS(expected_root_tag) != | 344 if (StripTrailingS(expected_root_tag) != |
338 StripTrailingS(map_entry.root_tag)): | 345 StripTrailingS(map_entry.root_tag)): |
339 return [ | 346 return [ |
340 FormatPresubmitError( | 347 FormatPresubmitError( |
341 output_api,'root tag "%s" does not match model type. It should' | 348 output_api,'root tag "%s" does not match model type. It should' |
342 'be "%s"' % (map_entry.root_tag, expected_root_tag), | 349 'be "%s"' % (map_entry.root_tag, expected_root_tag), |
343 map_entry.affected_lines)] | 350 map_entry.affected_lines)] |
344 return [] | 351 return [] |
345 | 352 |
| 353 def CheckRootTagNotInBlackList(output_api, map_entry): |
| 354 """ Checks that map_entry's root isn't a blacklisted string. |
| 355 Args: |
| 356 output_api: presubmit_support OutputAPI instance |
| 357 map_entry: ModelTypeEnumEntry object to check |
| 358 Returns: |
| 359 A list of PresubmitError objects for each violation |
| 360 """ |
| 361 if map_entry.root_tag in BLACKLISTED_ROOT_TAGS: |
| 362 return [FormatPresubmitError( |
| 363 output_api,'root tag "%s" is a blacklisted root tag' |
| 364 % (map_entry.root_tag), map_entry.affected_lines)] |
| 365 return [] |
| 366 |
346 | 367 |
347 def FieldNumberToPrototypeString(field_number): | 368 def FieldNumberToPrototypeString(field_number): |
348 """Converts a field number enum reference to an EntitySpecifics string. | 369 """Converts a field number enum reference to an EntitySpecifics string. |
349 Converts a reference to the field number enum to the corresponding | 370 Converts a reference to the field number enum to the corresponding |
350 proto data type string. | 371 proto data type string. |
351 Args: | 372 Args: |
352 field_number: string representation of a field number enum reference | 373 field_number: string representation of a field number enum reference |
353 Returns: | 374 Returns: |
354 A string that is the corresponding proto field data type. e.g. | 375 A string that is the corresponding proto field data type. e.g. |
355 FieldNumberToPrototypeString('EntitySpecifics::kAppFieldNumber') | 376 FieldNumberToPrototypeString('EntitySpecifics::kAppFieldNumber') |
356 => 'AppSpecifics' | 377 => 'AppSpecifics' |
357 """ | 378 """ |
358 return field_number.replace(FIELD_NUMBER_PREFIX, '').replace( | 379 return field_number.replace(FIELD_NUMBER_PREFIX, '').replace( |
359 'FieldNumber', 'Specifics').replace( | 380 'FieldNumber', 'Specifics').replace( |
360 'AppNotificationSpecifics', 'AppNotification') | 381 'AppNotificationSpecifics', 'AppNotification') |
OLD | NEW |