OOP PDF: Update the dimensions of the plugin in JS rather than CSS
[chromium-blink-merge.git] / third_party / google_input_tools / update.py
blobdc0800fbbedc3fbcc7d47aa840e222aef8d57959
1 #!/usr/bin/python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Performs pull of google-input-tools from local clone of GitHub repository."""
8 import json
9 import logging
10 import optparse
11 import os
12 import re
13 import shutil
14 import subprocess
16 _BASE_REGEX_STRING = r'^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)'
17 require_regex = re.compile(_BASE_REGEX_STRING % 'require')
18 provide_regex = re.compile(_BASE_REGEX_STRING % 'provide')
20 preamble = [
21 '# Copyright 2014 The Chromium Authors. All rights reserved.',
22 '# Use of this source code is governed by a BSD-style license that can be',
23 '# found in the LICENSE file.',
24 '',
25 '# This file is auto-generated using update.py.',
29 # Entry-points required to build a virtual keyboard.
30 namespaces = [
31 'i18n.input.chrome.inputview.Controller',
32 'i18n.input.chrome.inputview.content.compact.letter',
33 'i18n.input.chrome.inputview.content.compact.util',
34 'i18n.input.chrome.inputview.content.compact.symbol',
35 'i18n.input.chrome.inputview.content.compact.more',
36 'i18n.input.chrome.inputview.content.compact.numberpad',
37 'i18n.input.chrome.inputview.content.ContextlayoutUtil',
38 'i18n.input.chrome.inputview.content.util',
39 'i18n.input.chrome.inputview.EmojiType',
40 'i18n.input.chrome.inputview.layouts.CompactSpaceRow',
41 'i18n.input.chrome.inputview.layouts.RowsOf101',
42 'i18n.input.chrome.inputview.layouts.RowsOf102',
43 'i18n.input.chrome.inputview.layouts.RowsOfCompact',
44 'i18n.input.chrome.inputview.layouts.RowsOfJP',
45 'i18n.input.chrome.inputview.layouts.RowsOfNumberpad',
46 'i18n.input.chrome.inputview.layouts.SpaceRow',
47 'i18n.input.chrome.inputview.layouts.util',
48 'i18n.input.hwt.util'
51 # Any additional required files.
52 extras = [
53 'common.css',
54 'emoji.css'
58 def process_file(filename):
59 """Extracts provided and required namespaces.
61 Description:
62 Scans Javascript file for provied and required namespaces.
64 Args:
65 filename: name of the file to process.
67 Returns:
68 Pair of lists, where the first list contains namespaces provided by the file
69 and the second contains a list of requirements.
70 """
71 provides = []
72 requires = []
73 file_handle = open(filename, 'r')
74 try:
75 for line in file_handle:
76 if re.match(require_regex, line):
77 requires.append(re.search(require_regex, line).group(1))
78 if re.match(provide_regex, line):
79 provides.append(re.search(provide_regex, line).group(1))
80 finally:
81 file_handle.close()
82 return provides, requires
85 def expand_directories(refs):
86 """Expands any directory references into inputs.
88 Description:
89 Looks for any directories in the provided references. Found directories
90 are recursively searched for .js files.
92 Args:
93 refs: a list of directories.
95 Returns:
96 Pair of maps, where the first maps each namepace to the filename that
97 provides the namespace, and the second maps a filename to prerequisite
98 namespaces.
99 """
100 providers = {}
101 requirements = {}
102 for ref in refs:
103 if os.path.isdir(ref):
104 for (root, _, files) in os.walk(ref):
105 for name in files:
106 if name.endswith('js'):
107 filename = os.path.join(root, name)
108 provides, requires = process_file(filename)
109 for p in provides:
110 providers[p] = filename
111 requirements[filename] = []
112 for r in requires:
113 requirements[filename].append(r)
114 return providers, requirements
117 def extract_dependencies(namespace, providers, requirements, dependencies):
118 """Recursively extracts all dependencies for a namespace.
120 Description:
121 Recursively extracts all dependencies for a namespace.
123 Args:
124 namespace: The namespace to process.
125 providers: Mapping of namespace to filename that provides the namespace.
126 requirements: Mapping of filename to a list of prerequisite namespaces.
127 dependencies: List of files required to build inputview.
128 Returns:
131 if namespace in providers:
132 filename = providers[namespace]
133 if filename not in dependencies:
134 for ns in requirements[filename]:
135 extract_dependencies(ns, providers, requirements, dependencies)
136 dependencies.add(filename)
139 def home_dir():
140 """Resolves the user's home directory."""
142 return os.path.expanduser('~')
145 def expand_path_relative_to_home(path):
146 """Resolves a path that is relative to the home directory.
148 Args:
149 path: Relative path.
151 Returns:
152 Resolved path.
155 return os.path.join(os.path.expanduser('~'), path)
158 def get_google_input_tools_sandbox_from_options(options):
159 """Generate the input-input-tools path from the --input flag.
161 Args:
162 options: Flags to update.py.
163 Returns:
164 Path to the google-input-tools sandbox.
167 path = options.input
168 if not path:
169 path = expand_path_relative_to_home('google-input-tools')
170 print 'Unspecified path for google-input-tools. Defaulting to %s' % path
171 return path
174 def get_closure_library_sandbox_from_options(options):
175 """Generate the closure-library path from the --input flag.
177 Args:
178 options: Flags to update.py.
179 Returns:
180 Path to the closure-library sandbox.
183 path = options.lib
184 if not path:
185 path = expand_path_relative_to_home('closure-library')
186 print 'Unspecified path for closure-library. Defaulting to %s' % path
187 return path
190 def copy_file(source, target):
191 """Copies a file from the source to the target location.
193 Args:
194 source: Path to the source file to copy.
195 target: Path to the target location to copy the file.
198 if not os.path.exists(os.path.dirname(target)):
199 os.makedirs(os.path.dirname(target))
200 shutil.copy(source, target)
201 # Ensure correct file permissions.
202 if target.endswith('py'):
203 subprocess.call(['chmod', '+x', target])
204 else:
205 subprocess.call(['chmod', '-x', target])
208 def update_file(filename, input_source, closure_source, target_files):
209 """Updates files in third_party/google_input_tools.
211 Args:
212 filename: The file to update.
213 input_source: Root of the google_input_tools sandbox.
214 closure_source: Root of the closure_library sandbox.
215 target_files: List of relative paths to target files.
218 target = ''
219 if filename.startswith(input_source):
220 target = os.path.join('src', filename[len(input_source)+1:])
221 elif filename.startswith(closure_source):
222 target = os.path.join('third_party/closure_library',
223 filename[len(closure_source)+1:])
224 if target:
225 copy_file(filename, target)
226 target_files.append(os.path.relpath(target, os.getcwd()))
229 def generate_build_file(target_files):
230 """Updates inputview.gypi.
232 Args:
233 target_files: List of files required to build inputview.js.
236 sorted_files = sorted(target_files)
237 with open('inputview.gypi', 'w') as file_handle:
238 file_handle.write(os.linesep.join(preamble))
239 json_data = {'variables': {'inputview_sources': sorted_files}}
240 json_str = json.dumps(json_data, indent=2, separators=(',', ': '))
241 file_handle.write(json_str.replace('\"', '\''))
244 def copy_dir(input_path, sub_dir):
245 """Copies all files in a subdirectory of google-input-tools.
247 Description:
248 Recursive copy of a directory under google-input-tools. Used to copy
249 localization and resource files.
251 Args:
252 input_path: Path to the google-input-tools-sandbox.
253 sub_dir: Subdirectory to copy within google-input-tools sandbox.
255 source_dir = os.path.join(input_path, 'chrome', 'os', 'inputview', sub_dir)
256 for (root, _, files) in os.walk(source_dir):
257 for name in files:
258 filename = os.path.join(root, name)
259 relative_path = filename[len(source_dir) + 1:]
260 target = os.path.join('src', 'chrome', 'os', 'inputview', sub_dir,
261 relative_path)
262 copy_file(filename, target)
265 def main():
266 """The entrypoint for this script."""
268 logging.basicConfig(format='update.py: %(message)s', level=logging.INFO)
270 usage = 'usage: %prog [options] arg'
271 parser = optparse.OptionParser(usage)
272 parser.add_option('-i',
273 '--input',
274 dest='input',
275 action='append',
276 help='Path to the google-input-tools sandbox.')
277 parser.add_option('-l',
278 '--lib',
279 dest='lib',
280 action='store',
281 help='Path to the closure-library sandbox.')
283 (options, _) = parser.parse_args()
285 input_path = get_google_input_tools_sandbox_from_options(options)
286 closure_library_path = get_closure_library_sandbox_from_options(options)
288 if not os.path.isdir(input_path):
289 print 'Could not find google-input-tools sandbox.'
290 exit(1)
291 if not os.path.isdir(closure_library_path):
292 print 'Could not find closure-library sandbox.'
293 exit(1)
295 (providers, requirements) = expand_directories([
296 os.path.join(input_path, 'chrome'),
297 closure_library_path])
299 dependencies = set()
300 for name in namespaces:
301 extract_dependencies(name, providers, requirements, dependencies)
303 target_files = []
304 for name in dependencies:
305 update_file(name, input_path, closure_library_path, target_files)
307 generate_build_file(target_files)
309 # Copy resources
310 copy_dir(input_path, '_locales')
311 copy_dir(input_path, 'images')
312 copy_dir(input_path, 'config')
313 copy_dir(input_path, 'layouts')
314 copy_dir(input_path, 'sounds')
316 # Copy extra support files.
317 for name in extras:
318 source = os.path.join(input_path, 'chrome', 'os', 'inputview', name)
319 target = os.path.join('src', 'chrome', 'os', 'inputview', name)
320 copy_file(source, target)
323 if __name__ == '__main__':
324 main()