Yilin Yang | 8e1b24c | 2020-02-03 15:02:25 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 2 | |
| 3 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | |
| 8 | """Updates testdata/ based on data pulled from Chromium sources.""" |
| 9 | |
| 10 | from __future__ import print_function |
| 11 | |
| 12 | import argparse |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 13 | import base64 |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 14 | import json |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 15 | # pylint: disable=cros-logging-import |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 16 | import logging |
| 17 | import os |
| 18 | import re |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 19 | import subprocess |
| 20 | import sys |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 21 | |
| 22 | import yaml |
| 23 | |
| 24 | |
| 25 | # URLs to GIT paths. |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 26 | SRC_GIT_URL = 'https://chromium.googlesource.com/chromium/src/+/master/' |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 27 | |
| 28 | TESTDATA_PATH = os.path.join(os.path.dirname(__file__), 'testdata') |
| 29 | |
| 30 | |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 31 | def GetChromiumSource(file_path): |
| 32 | """Gets Chromium source code by given path. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 33 | |
| 34 | Args: |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 35 | file_path: The relative path to retrieve. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 36 | """ |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 37 | return base64.b64decode(subprocess.check_output( |
Yilin Yang | 051156f | 2020-02-03 15:19:37 +0800 | [diff] [blame] | 38 | ['curl', '-s', SRC_GIT_URL + file_path + '?format=TEXT'])).decode('utf-8') |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | def WriteTestData(name, value): |
| 42 | if not value: |
| 43 | sys.exit('No values found for %s' % name) |
| 44 | |
| 45 | path = os.path.join(TESTDATA_PATH, name + '.yaml') |
| 46 | logging.info('%s: writing %r', path, value) |
| 47 | with open(path, 'w') as f: |
| 48 | f.write('# Automatically generated from ToT Chromium sources\n' |
| 49 | '# by update_testdata.py. Do not edit manually.\n' |
| 50 | '\n') |
| 51 | yaml.dump(value, f, default_flow_style=False) |
| 52 | |
| 53 | |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 54 | def UpdateLocales(): |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 55 | """Updates locales. |
| 56 | |
| 57 | Valid locales are entries of the kAcceptLanguageList array in |
| 58 | l10n_util.cc <http://goo.gl/z8XsZJ>. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 59 | """ |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 60 | cpp_code = GetChromiumSource('ui/base/l10n/l10n_util.cc') |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 61 | match = re.search(r'static[^\n]+kAcceptLanguageList\[\] = \{(.+?)^\}', |
| 62 | cpp_code, re.DOTALL | re.MULTILINE) |
| 63 | if not match: |
| 64 | sys.exit('Unable to find language list') |
| 65 | |
| 66 | locales = re.findall(r'"(.+)"', match.group(1)) |
| 67 | if not locales: |
| 68 | sys.exit('Unable to parse language list') |
| 69 | |
| 70 | WriteTestData('locales', sorted(locales)) |
| 71 | |
| 72 | |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 73 | def UpdateTimeZones(): |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 74 | """Updates time zones. |
| 75 | |
| 76 | Valid time zones are values of the kTimeZones array in timezone_settings.cc |
| 77 | <http://goo.gl/WSVUeE>. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 78 | """ |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 79 | cpp_code = GetChromiumSource('chromeos/settings/timezone_settings.cc') |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 80 | match = re.search(r'static[^\n]+kTimeZones\[\] = \{(.+?)^\}', |
| 81 | cpp_code, re.DOTALL | re.MULTILINE) |
| 82 | if not match: |
| 83 | sys.exit('Unable to find time zones') |
| 84 | |
| 85 | time_zones = re.findall(r'"(.+)"', match.group(1)) |
| 86 | if not time_zones: |
| 87 | sys.exit('Unable to parse time zones') |
| 88 | |
| 89 | WriteTestData('time_zones', time_zones) |
| 90 | |
| 91 | |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 92 | def UpdateMigrationMap(): |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 93 | """Updates the input method migration map. |
| 94 | |
| 95 | The source is the kEngineIdMigrationMap array in input_method_util.cc |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 96 | <https://chromium.googlesource.com/chromium/src/+/master/ui/base/ime/chromeos/input_method_util.cc>. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 97 | """ |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 98 | cpp_code = GetChromiumSource('ui/base/ime/chromeos/input_method_util.cc') |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 99 | match = re.search(r'kEngineIdMigrationMap\[\]\[2\] = \{(.+?)^\}', |
| 100 | cpp_code, re.DOTALL | re.MULTILINE) |
| 101 | if not match: |
| 102 | sys.exit('Unable to find kEngineIdMigrationMap') |
| 103 | |
| 104 | map_code = match.group(1) |
| 105 | migration_map = re.findall(r'{"(.+?)", "(.+?)"}', map_code) |
| 106 | if not migration_map: |
| 107 | sys.exit('Unable to parse kEngineIdMigrationMap') |
| 108 | |
| 109 | WriteTestData('migration_map', migration_map) |
| 110 | |
| 111 | |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 112 | def UpdateInputMethods(): |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 113 | """Updates input method IDs. |
| 114 | |
| 115 | This is the union of all 'id' fields in input_method/*.json |
| 116 | <http://goo.gl/z4JGvK>. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 117 | """ |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 118 | # entry format: 100644 blob 48de6e64885e472c6743543cc988ac0fd8edd55e FILE |
| 119 | json_dir = 'chrome/browser/resources/chromeos/input_method' |
| 120 | files = [line.strip().split()[-1] |
| 121 | for line in GetChromiumSource(json_dir).splitlines()] |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 122 | pattern = re.compile(r'\.json$') |
| 123 | json_files = [f for f in files if pattern.search(f)] |
| 124 | |
| 125 | input_methods = set() |
| 126 | for f in json_files: |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 127 | contents = json.loads(GetChromiumSource(os.path.join(json_dir, f))) |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 128 | for c in contents['input_components']: |
| 129 | input_methods.add(str(c['id'])) |
| 130 | |
| 131 | WriteTestData('input_methods', sorted(input_methods)) |
| 132 | |
| 133 | |
| 134 | def main(): |
| 135 | parser = argparse.ArgumentParser( |
| 136 | description=('Updates some constants in regions_unittest_data.py based ' |
| 137 | 'on data pulled from Chromium sources. This overwrites ' |
| 138 | 'files in testdata, which you must then submit.')) |
| 139 | unused_args = parser.parse_args() |
| 140 | logging.basicConfig(level=logging.INFO) |
| 141 | |
Hung-Te Lin | a6ba1ee | 2017-06-06 14:56:08 +0800 | [diff] [blame] | 142 | UpdateLocales() |
| 143 | UpdateTimeZones() |
| 144 | UpdateInputMethods() |
| 145 | UpdateMigrationMap() |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 146 | |
| 147 | logging.info('Run "git diff %s" to see changes (if any).', TESTDATA_PATH) |
| 148 | logging.info('Make sure to submit any changes to %s!', TESTDATA_PATH) |
| 149 | |
| 150 | |
| 151 | if __name__ == '__main__': |
| 152 | main() |