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