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."""
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
')
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.',
25 '# This file is auto-generated using update.py.',
29 # Entry-points required to build a virtual keyboard.
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',
51 # Any additional required files.
58 def process_file(filename
):
59 """Extracts provided and required namespaces.
62 Scans Javascript file for provied and required namespaces.
65 filename: name of the file to process.
68 Pair of lists, where the first list contains namespaces provided by the file
69 and the second contains a list of requirements.
73 file_handle
= open(filename
, 'r')
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))
82 return provides
, requires
85 def expand_directories(refs
):
86 """Expands any directory references into inputs.
89 Looks for any directories in the provided references. Found directories
90 are recursively searched for .js files.
93 refs: a list of directories.
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
103 if os
.path
.isdir(ref
):
104 for (root
, _
, files
) in os
.walk(ref
):
106 if name
.endswith('js'):
107 filename
= os
.path
.join(root
, name
)
108 provides
, requires
= process_file(filename
)
110 providers
[p
] = filename
111 requirements
[filename
] = []
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.
121 Recursively extracts all dependencies for a namespace.
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.
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
)
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.
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.
162 options: Flags to update.py.
164 Path to the google-input-tools sandbox.
169 path
= expand_path_relative_to_home('google-input-tools')
170 print 'Unspecified path for google-input-tools. Defaulting to %s' % path
174 def get_closure_library_sandbox_from_options(options
):
175 """Generate the closure-library path from the --input flag.
178 options: Flags to update.py.
180 Path to the closure-library sandbox.
185 path
= expand_path_relative_to_home('closure-library')
186 print 'Unspecified path for closure-library. Defaulting to %s' % path
190 def copy_file(source
, target
):
191 """Copies a file from the source to the target location.
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
])
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.
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.
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:])
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.
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.
248 Recursive copy of a directory under google-input-tools. Used to copy
249 localization and resource files.
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
):
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
,
262 copy_file(filename
, target
)
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',
276 help='Path to the google-input-tools sandbox.')
277 parser
.add_option('-l',
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.'
291 if not os
.path
.isdir(closure_library_path
):
292 print 'Could not find closure-library sandbox.'
295 (providers
, requirements
) = expand_directories([
296 os
.path
.join(input_path
, 'chrome'),
297 closure_library_path
])
300 for name
in namespaces
:
301 extract_dependencies(name
, providers
, requirements
, dependencies
)
304 for name
in dependencies
:
305 update_file(name
, input_path
, closure_library_path
, target_files
)
307 generate_build_file(target_files
)
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.
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__':