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 contextlib |
| 6 import json |
| 7 import logging |
| 8 import os |
| 9 import sys |
| 10 import tempfile |
| 11 import threading |
| 12 |
| 13 # TODO(jbudorick): Update this once dependency_manager moves to catapult. |
| 14 CATAPULT_BASE_PATH = os.path.abspath(os.path.join( |
| 15 os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, |
| 16 'tools', 'telemetry')) |
| 17 |
| 18 @contextlib.contextmanager |
| 19 def SysPath(path): |
| 20 sys.path.append(path) |
| 21 yield |
| 22 if sys.path[-1] != path: |
| 23 logging.debug('Expected %s at the end of sys.path. Full sys.path: %s', |
| 24 path, str(sys.path)) |
| 25 sys.path.remove(path) |
| 26 else: |
| 27 sys.path.pop() |
| 28 |
| 29 with SysPath(CATAPULT_BASE_PATH): |
| 30 from catapult_base import dependency_manager # pylint: disable=import-error |
| 31 |
| 32 _ANDROID_BUILD_TOOLS = {'aapt', 'dexdump', 'split-select'} |
| 33 |
| 34 _DEVIL_DEFAULT_CONFIG = os.path.abspath(os.path.join( |
| 35 os.path.dirname(__file__), 'devil_dependencies.json')) |
| 36 |
| 37 _LEGACY_ENVIRONMENT_VARIABLES = { |
| 38 'ADB_PATH': { |
| 39 'dependency_name': 'adb', |
| 40 'platform': 'android_host', |
| 41 }, |
| 42 'ANDROID_SDK_ROOT': { |
| 43 'dependency_name': 'android_sdk', |
| 44 'platform': 'android_host', |
| 45 }, |
| 46 } |
| 47 |
| 48 |
| 49 def _GetEnvironmentVariableConfig(): |
| 50 env_var_config = {} |
| 51 for k, v in _LEGACY_ENVIRONMENT_VARIABLES.iteritems(): |
| 52 path = os.environ.get(k) |
| 53 if path: |
| 54 env_var_config[v['dependency_name']] = { |
| 55 'file_info': { |
| 56 v['platform']: { |
| 57 'local_paths': [path] |
| 58 } |
| 59 } |
| 60 } |
| 61 return env_var_config |
| 62 |
| 63 |
| 64 class _Environment(object): |
| 65 |
| 66 def __init__(self): |
| 67 self._config = None |
| 68 self._config_init_lock = threading.Lock() |
| 69 self._dm = None |
| 70 |
| 71 def Initialize(self, configs=None, config_files=None): |
| 72 """Initialize devil's environment from configuration files. |
| 73 |
| 74 This uses all configurations provided via |configs| and |config_files| |
| 75 to determine the locations of devil's dependencies. Configurations should |
| 76 all take the form described by catapult_base.dependency_manager.BaseConfig. |
| 77 If no configurations are provided, a default one will be used if available. |
| 78 |
| 79 Args: |
| 80 configs: An optional list of dict configurations. |
| 81 config_files: An optional list of files to load |
| 82 """ |
| 83 |
| 84 # Make sure we only initialize self._config once. |
| 85 with self._config_init_lock: |
| 86 if self._config is not None: |
| 87 return |
| 88 self._config = {} |
| 89 |
| 90 if configs is None: |
| 91 configs = [] |
| 92 |
| 93 env_config = _GetEnvironmentVariableConfig() |
| 94 if env_config: |
| 95 configs.insert(0, env_config) |
| 96 self._InitializeRecursive( |
| 97 configs=configs, |
| 98 config_files=config_files) |
| 99 |
| 100 def _InitializeRecursive(self, configs=None, config_files=None): |
| 101 # This recurses through configs to create temporary files for each and |
| 102 # take advantage of context managers to appropriately close those files. |
| 103 # TODO(jbudorick): Remove this recursion if/when dependency_manager |
| 104 # supports loading configurations directly from a dict. |
| 105 if configs: |
| 106 with tempfile.NamedTemporaryFile() as next_config_file: |
| 107 next_config_file.write(json.dumps(configs[0])) |
| 108 next_config_file.flush() |
| 109 self._InitializeRecursive( |
| 110 configs=configs[1:], |
| 111 config_files=[next_config_file.name] + (config_files or [])) |
| 112 else: |
| 113 config_files = config_files or [] |
| 114 if 'DEVIL_ENV_CONFIG' in os.environ: |
| 115 config_files.append(os.environ.get('DEVIL_ENV_CONFIG')) |
| 116 config_files.append(_DEVIL_DEFAULT_CONFIG) |
| 117 |
| 118 self._dm = dependency_manager.DependencyManager( |
| 119 [dependency_manager.BaseConfig(c) for c in config_files]) |
| 120 |
| 121 def FetchPath(self, dependency, arch=None, device=None): |
| 122 if self._dm is None: |
| 123 self.Initialize() |
| 124 if dependency in _ANDROID_BUILD_TOOLS: |
| 125 self.FetchPath('android_build_tools_libc++', arch=arch, device=device) |
| 126 return self._dm.FetchPath(dependency, _GetPlatform(arch, device)) |
| 127 |
| 128 def LocalPath(self, dependency, arch=None, device=None): |
| 129 if self._dm is None: |
| 130 self.Initialize() |
| 131 return self._dm.LocalPath(dependency, _GetPlatform(arch, device)) |
| 132 |
| 133 |
| 134 def _GetPlatform(arch, device): |
| 135 if not arch: |
| 136 arch = device.product_cpu_abi if device else 'host' |
| 137 return 'android_%s' % arch |
| 138 |
| 139 |
| 140 config = _Environment() |
| 141 |
OLD | NEW |