blob: cac8c26d4bd2175c307e1eb9c4f68475ecaafca1 [file] [log] [blame]
Hung-Te Lin98501ae2017-06-02 15:53:33 +08001#!/usr/bin/env python2
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +08002
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
10from __future__ import print_function
11
12import argparse
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080013import base64
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080014import json
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080015# pylint: disable=cros-logging-import
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080016import logging
17import os
18import re
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080019import subprocess
20import sys
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080021
22import yaml
23
24
25# URLs to GIT paths.
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080026SRC_GIT_URL = 'https://chromium.googlesource.com/chromium/src/+/master/'
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080027
28TESTDATA_PATH = os.path.join(os.path.dirname(__file__), 'testdata')
29
30
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080031def GetChromiumSource(file_path):
32 """Gets Chromium source code by given path.
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080033
34 Args:
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080035 file_path: The relative path to retrieve.
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080036 """
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080037 return base64.b64decode(subprocess.check_output(
Yilin Yang051156f2020-02-03 15:19:37 +080038 ['curl', '-s', SRC_GIT_URL + file_path + '?format=TEXT'])).decode('utf-8')
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080039
40
41def 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 Lina6ba1ee2017-06-06 14:56:08 +080054def UpdateLocales():
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080055 """Updates locales.
56
57 Valid locales are entries of the kAcceptLanguageList array in
58 l10n_util.cc <http://goo.gl/z8XsZJ>.
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080059 """
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080060 cpp_code = GetChromiumSource('ui/base/l10n/l10n_util.cc')
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080061 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 Lina6ba1ee2017-06-06 14:56:08 +080073def UpdateTimeZones():
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080074 """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 Lin2c89ccd2015-04-07 15:20:35 +080078 """
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080079 cpp_code = GetChromiumSource('chromeos/settings/timezone_settings.cc')
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080080 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 Lina6ba1ee2017-06-06 14:56:08 +080092def UpdateMigrationMap():
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080093 """Updates the input method migration map.
94
95 The source is the kEngineIdMigrationMap array in input_method_util.cc
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080096 <https://chromium.googlesource.com/chromium/src/+/master/ui/base/ime/chromeos/input_method_util.cc>.
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080097 """
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +080098 cpp_code = GetChromiumSource('ui/base/ime/chromeos/input_method_util.cc')
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +080099 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 Lina6ba1ee2017-06-06 14:56:08 +0800112def UpdateInputMethods():
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +0800113 """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 Lin2c89ccd2015-04-07 15:20:35 +0800117 """
Hung-Te Lina6ba1ee2017-06-06 14:56:08 +0800118 # 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 Lin2c89ccd2015-04-07 15:20:35 +0800122 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 Lina6ba1ee2017-06-06 14:56:08 +0800127 contents = json.loads(GetChromiumSource(os.path.join(json_dir, f)))
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +0800128 for c in contents['input_components']:
129 input_methods.add(str(c['id']))
130
131 WriteTestData('input_methods', sorted(input_methods))
132
133
134def 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 Lina6ba1ee2017-06-06 14:56:08 +0800142 UpdateLocales()
143 UpdateTimeZones()
144 UpdateInputMethods()
145 UpdateMigrationMap()
Hung-Te Lin2c89ccd2015-04-07 15:20:35 +0800146
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
151if __name__ == '__main__':
152 main()