OLD | NEW |
| (Empty) |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import json | |
6 import os | |
7 import sys | |
8 import tempfile | |
9 import threading | |
10 | |
11 # TODO(jbudorick): Update this once dependency_manager moves to catapult. | |
12 _TELEMETRY_PATH = os.path.abspath(os.path.join( | |
13 os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, | |
14 'tools', 'telemetry')) | |
15 sys.path.append(_TELEMETRY_PATH) | |
16 from catapult_base import dependency_manager # pylint: disable=import-error | |
17 | |
18 _ANDROID_BUILD_TOOLS = {'aapt', 'dexdump', 'split-select'} | |
19 | |
20 _DEVIL_DEFAULT_CONFIG = os.path.abspath(os.path.join( | |
21 os.path.dirname(__file__), 'devil_dependencies.json')) | |
22 | |
23 | |
24 class _Environment(object): | |
25 | |
26 def __init__(self): | |
27 self._config = None | |
28 self._config_init_lock = threading.Lock() | |
29 self._dm = None | |
30 | |
31 def Initialize(self, configs=None, config_files=None): | |
32 """Initialize devil's environment from configuration files. | |
33 | |
34 This uses all configurations provided via |configs| and |config_files| | |
35 to determine the locations of devil's dependencies. Configurations should | |
36 all take the form described by catapult_base.dependency_manager.BaseConfig. | |
37 If no configurations are provided, a default one will be used if available. | |
38 | |
39 Args: | |
40 configs: An optional list of dict configurations. | |
41 config_files: An optional list of files to load | |
42 """ | |
43 | |
44 # Make sure we only initialize self._config once. | |
45 with self._config_init_lock: | |
46 if self._config is not None: | |
47 return | |
48 self._config = {} | |
49 | |
50 self._InitializeRecursive( | |
51 configs=configs, config_files=config_files) | |
52 | |
53 def _InitializeRecursive(self, configs=None, config_files=None): | |
54 # This recurses through configs to create temporary files for each and | |
55 # take advantage of context managers to appropriately close those files. | |
56 # TODO(jbudorick): Remove this recursion if/when dependency_manager | |
57 # supports loading configurations directly from a dict. | |
58 if configs: | |
59 with tempfile.NamedTemporaryFile() as next_config_file: | |
60 next_config_file.write(json.dumps(configs[0])) | |
61 next_config_file.flush() | |
62 self._InitializeRecursive( | |
63 configs=configs[1:], | |
64 config_files=[next_config_file.name] + (config_files or [])) | |
65 else: | |
66 config_files = config_files or [] | |
67 if 'DEVIL_ENV_CONFIG' in os.environ: | |
68 config_files.append(os.environ.get('DEVIL_ENV_CONFIG')) | |
69 config_files.append(_DEVIL_DEFAULT_CONFIG) | |
70 | |
71 self._dm = dependency_manager.DependencyManager( | |
72 [dependency_manager.BaseConfig(c) for c in config_files]) | |
73 | |
74 def FetchPath(self, dependency, arch=None, device=None): | |
75 if self._dm is None: | |
76 self.Initialize() | |
77 if dependency in _ANDROID_BUILD_TOOLS: | |
78 self.FetchPath('android_build_tools_libc++', arch=arch, device=device) | |
79 return self._dm.FetchPath(dependency, _GetPlatform(arch, device)) | |
80 | |
81 def LocalPath(self, dependency, arch=None, device=None): | |
82 if self._dm is None: | |
83 self.Initialize() | |
84 return self._dm.LocalPath(dependency, _GetPlatform(arch, device)) | |
85 | |
86 | |
87 def _GetPlatform(arch, device): | |
88 if not arch: | |
89 arch = device.product_cpu_abi if device else 'host' | |
90 return 'android_%s' % arch | |
91 | |
92 | |
93 config = _Environment() | |
94 | |
OLD | NEW |