Hung-Te Lin | 98501ae | 2017-06-02 15:53:33 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
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 |
| 13 | import json |
| 14 | import logging |
| 15 | import os |
| 16 | import re |
| 17 | import shutil |
| 18 | import subprocess |
| 19 | import sys |
| 20 | import tempfile |
| 21 | |
| 22 | import yaml |
| 23 | |
| 24 | |
| 25 | # URLs to GIT paths. |
| 26 | SRC_GIT_URL = 'https://chromium.googlesource.com/chromium/src' |
| 27 | SRC_REMOTE_BRANCH = 'remotes/origin/master' |
| 28 | |
| 29 | TESTDATA_PATH = os.path.join(os.path.dirname(__file__), 'testdata') |
| 30 | |
| 31 | |
| 32 | def GetChromiumSource(temp_dir): |
| 33 | """Gets Chromium source code under a temp directory. |
| 34 | |
| 35 | Args: |
| 36 | temp_dir: a temp directory to store the Chromium source code. |
| 37 | """ |
| 38 | subprocess.check_call( |
| 39 | ['git', 'clone', SRC_GIT_URL, '--no-checkout', '--depth', '1'], |
| 40 | cwd=temp_dir) |
| 41 | |
| 42 | |
| 43 | def WriteTestData(name, value): |
| 44 | if not value: |
| 45 | sys.exit('No values found for %s' % name) |
| 46 | |
| 47 | path = os.path.join(TESTDATA_PATH, name + '.yaml') |
| 48 | logging.info('%s: writing %r', path, value) |
| 49 | with open(path, 'w') as f: |
| 50 | f.write('# Automatically generated from ToT Chromium sources\n' |
| 51 | '# by update_testdata.py. Do not edit manually.\n' |
| 52 | '\n') |
| 53 | yaml.dump(value, f, default_flow_style=False) |
| 54 | |
| 55 | |
| 56 | def UpdateLocales(source_dir): |
| 57 | """Updates locales. |
| 58 | |
| 59 | Valid locales are entries of the kAcceptLanguageList array in |
| 60 | l10n_util.cc <http://goo.gl/z8XsZJ>. |
| 61 | |
| 62 | Args: |
| 63 | source_dir: the directory storing Chromium source. |
| 64 | """ |
| 65 | cpp_code = subprocess.check_output( |
| 66 | ['git', 'show', SRC_REMOTE_BRANCH + ':ui/base/l10n/l10n_util.cc'], |
| 67 | cwd=source_dir) |
| 68 | match = re.search(r'static[^\n]+kAcceptLanguageList\[\] = \{(.+?)^\}', |
| 69 | cpp_code, re.DOTALL | re.MULTILINE) |
| 70 | if not match: |
| 71 | sys.exit('Unable to find language list') |
| 72 | |
| 73 | locales = re.findall(r'"(.+)"', match.group(1)) |
| 74 | if not locales: |
| 75 | sys.exit('Unable to parse language list') |
| 76 | |
| 77 | WriteTestData('locales', sorted(locales)) |
| 78 | |
| 79 | |
| 80 | def UpdateTimeZones(source_dir): |
| 81 | """Updates time zones. |
| 82 | |
| 83 | Valid time zones are values of the kTimeZones array in timezone_settings.cc |
| 84 | <http://goo.gl/WSVUeE>. |
| 85 | |
| 86 | Args: |
| 87 | source_dir: the directory storing Chromium source. |
| 88 | """ |
| 89 | cpp_code = subprocess.check_output( |
| 90 | ['git', 'show', |
| 91 | SRC_REMOTE_BRANCH + ':chromeos/settings/timezone_settings.cc'], |
| 92 | cwd=source_dir) |
| 93 | match = re.search(r'static[^\n]+kTimeZones\[\] = \{(.+?)^\}', |
| 94 | cpp_code, re.DOTALL | re.MULTILINE) |
| 95 | if not match: |
| 96 | sys.exit('Unable to find time zones') |
| 97 | |
| 98 | time_zones = re.findall(r'"(.+)"', match.group(1)) |
| 99 | if not time_zones: |
| 100 | sys.exit('Unable to parse time zones') |
| 101 | |
| 102 | WriteTestData('time_zones', time_zones) |
| 103 | |
| 104 | |
| 105 | def UpdateMigrationMap(source_dir): |
| 106 | """Updates the input method migration map. |
| 107 | |
| 108 | The source is the kEngineIdMigrationMap array in input_method_util.cc |
| 109 | <http://goo.gl/cDO53r>. |
| 110 | |
| 111 | Args: |
| 112 | source_dir: the directory storing Chromium source. |
| 113 | """ |
| 114 | cpp_code = subprocess.check_output( |
| 115 | ['git', 'show', |
| 116 | (SRC_REMOTE_BRANCH + |
| 117 | ':chrome/browser/chromeos/input_method/input_method_util.cc')], |
| 118 | cwd=source_dir) |
| 119 | match = re.search(r'kEngineIdMigrationMap\[\]\[2\] = \{(.+?)^\}', |
| 120 | cpp_code, re.DOTALL | re.MULTILINE) |
| 121 | if not match: |
| 122 | sys.exit('Unable to find kEngineIdMigrationMap') |
| 123 | |
| 124 | map_code = match.group(1) |
| 125 | migration_map = re.findall(r'{"(.+?)", "(.+?)"}', map_code) |
| 126 | if not migration_map: |
| 127 | sys.exit('Unable to parse kEngineIdMigrationMap') |
| 128 | |
| 129 | WriteTestData('migration_map', migration_map) |
| 130 | |
| 131 | |
| 132 | def UpdateInputMethods(source_dir): |
| 133 | """Updates input method IDs. |
| 134 | |
| 135 | This is the union of all 'id' fields in input_method/*.json |
| 136 | <http://goo.gl/z4JGvK>. |
| 137 | |
| 138 | Args: |
| 139 | source_dir: the directory storing Chromium source. |
| 140 | """ |
| 141 | files = [line.strip() for line in subprocess.check_output( |
| 142 | ['git', 'show', SRC_REMOTE_BRANCH + |
| 143 | ':chrome/browser/resources/chromeos/input_method'], |
| 144 | cwd=source_dir).split()] |
| 145 | pattern = re.compile(r'\.json$') |
| 146 | json_files = [f for f in files if pattern.search(f)] |
| 147 | |
| 148 | input_methods = set() |
| 149 | for f in json_files: |
| 150 | contents = json.loads(subprocess.check_output( |
| 151 | ['git', 'show', (SRC_REMOTE_BRANCH + |
| 152 | ':chrome/browser/resources/chromeos/input_method/' + |
| 153 | f)], |
| 154 | cwd=source_dir)) |
| 155 | for c in contents['input_components']: |
| 156 | input_methods.add(str(c['id'])) |
| 157 | |
| 158 | WriteTestData('input_methods', sorted(input_methods)) |
| 159 | |
| 160 | |
| 161 | def main(): |
| 162 | parser = argparse.ArgumentParser( |
| 163 | description=('Updates some constants in regions_unittest_data.py based ' |
| 164 | 'on data pulled from Chromium sources. This overwrites ' |
| 165 | 'files in testdata, which you must then submit.')) |
| 166 | unused_args = parser.parse_args() |
| 167 | logging.basicConfig(level=logging.INFO) |
| 168 | |
| 169 | temp_dir = tempfile.mkdtemp() |
| 170 | try: |
| 171 | GetChromiumSource(temp_dir) |
| 172 | source_dir = os.path.join(temp_dir, 'src') |
| 173 | UpdateLocales(source_dir) |
| 174 | UpdateTimeZones(source_dir) |
| 175 | UpdateInputMethods(source_dir) |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 176 | finally: |
| 177 | shutil.rmtree(temp_dir) |
| 178 | |
| 179 | logging.info('Run "git diff %s" to see changes (if any).', TESTDATA_PATH) |
| 180 | logging.info('Make sure to submit any changes to %s!', TESTDATA_PATH) |
| 181 | |
| 182 | |
| 183 | if __name__ == '__main__': |
| 184 | main() |