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._dm_init_lock = threading.Lock() |
| 68 self._dm = None |
| 69 |
| 70 def Initialize(self, configs=None, config_files=None): |
| 71 """Initialize devil's environment from configuration files. |
| 72 |
| 73 This uses all configurations provided via |configs| and |config_files| |
| 74 to determine the locations of devil's dependencies. Configurations should |
| 75 all take the form described by catapult_base.dependency_manager.BaseConfig. |
| 76 If no configurations are provided, a default one will be used if available. |
| 77 |
| 78 Args: |
| 79 configs: An optional list of dict configurations. |
| 80 config_files: An optional list of files to load |
| 81 """ |
| 82 |
| 83 # Make sure we only initialize self._dm once. |
| 84 with self._dm_init_lock: |
| 85 if self._dm is None: |
| 86 if configs is None: |
| 87 configs = [] |
| 88 |
| 89 env_config = _GetEnvironmentVariableConfig() |
| 90 if env_config: |
| 91 configs.insert(0, env_config) |
| 92 self._InitializeRecursive( |
| 93 configs=configs, |
| 94 config_files=config_files) |
| 95 assert self._dm is not None, 'Failed to create dependency manager.' |
| 96 |
| 97 def _InitializeRecursive(self, configs=None, config_files=None): |
| 98 # This recurses through configs to create temporary files for each and |
| 99 # take advantage of context managers to appropriately close those files. |
| 100 # TODO(jbudorick): Remove this recursion if/when dependency_manager |
| 101 # supports loading configurations directly from a dict. |
| 102 if configs: |
| 103 with tempfile.NamedTemporaryFile() as next_config_file: |
| 104 next_config_file.write(json.dumps(configs[0])) |
| 105 next_config_file.flush() |
| 106 self._InitializeRecursive( |
| 107 configs=configs[1:], |
| 108 config_files=[next_config_file.name] + (config_files or [])) |
| 109 else: |
| 110 config_files = config_files or [] |
| 111 if 'DEVIL_ENV_CONFIG' in os.environ: |
| 112 config_files.append(os.environ.get('DEVIL_ENV_CONFIG')) |
| 113 config_files.append(_DEVIL_DEFAULT_CONFIG) |
| 114 |
| 115 self._dm = dependency_manager.DependencyManager( |
| 116 [dependency_manager.BaseConfig(c) for c in config_files]) |
| 117 |
| 118 def FetchPath(self, dependency, arch=None, device=None): |
| 119 if self._dm is None: |
| 120 self.Initialize() |
| 121 if dependency in _ANDROID_BUILD_TOOLS: |
| 122 self.FetchPath('android_build_tools_libc++', arch=arch, device=device) |
| 123 return self._dm.FetchPath(dependency, _GetPlatform(arch, device)) |
| 124 |
| 125 def LocalPath(self, dependency, arch=None, device=None): |
| 126 if self._dm is None: |
| 127 self.Initialize() |
| 128 return self._dm.LocalPath(dependency, _GetPlatform(arch, device)) |
| 129 |
| 130 |
| 131 def _GetPlatform(arch, device): |
| 132 if not arch: |
| 133 arch = device.product_cpu_abi if device else 'host' |
| 134 return 'android_%s' % arch |
| 135 |
| 136 |
| 137 config = _Environment() |
| 138 |
OLD | NEW |