Revert of Copy Widevine files for swarming tests (patchset #5 id:80001 of https:...
[chromium-blink-merge.git] / build / android / pylib / utils / isolator.py
blob8025b0fb733ac54bf1d964b15565db7b863f0458
1 # Copyright 2014 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.
5 import fnmatch
6 import glob
7 import os
8 import shutil
9 import sys
11 from pylib import cmd_helper
12 from pylib import constants
15 _ISOLATE_SCRIPT = os.path.join(
16 constants.DIR_SOURCE_ROOT, 'tools', 'swarming_client', 'isolate.py')
19 def DefaultPathVariables():
20 return {
21 'DEPTH': constants.DIR_SOURCE_ROOT,
22 'PRODUCT_DIR': constants.GetOutDirectory(),
26 def DefaultConfigVariables():
27 return {
28 'CONFIGURATION_NAME': constants.GetBuildType(),
29 'OS': 'android',
30 'asan': '0',
31 'chromeos': '0',
32 'component': 'static_library',
33 'enable_plugins': '0',
34 'fastbuild': '0',
35 'icu_use_data_file_flag': '1',
36 'lsan': '0',
37 'msan': '0',
38 # TODO(maruel): This may not always be true.
39 'target_arch': 'arm',
40 'tsan': '0',
41 'use_custom_libcxx': '0',
42 'use_instrumented_libraries': '0',
43 'use_prebuilt_instrumented_libraries': '0',
44 'use_openssl': '0',
45 'use_ozone': '0',
46 'use_x11': '0',
47 'v8_use_external_startup_data': '1',
51 class Isolator(object):
52 """Manages calls to isolate.py for the android test runner scripts."""
54 def __init__(self, isolate_deps_dir):
55 """
56 Args:
57 isolate_deps_dir: The directory in which dependencies specified by
58 isolate are or should be stored.
59 """
60 self._isolate_deps_dir = isolate_deps_dir
62 def Clear(self):
63 """Deletes the isolate dependency directory."""
64 if os.path.exists(self._isolate_deps_dir):
65 shutil.rmtree(self._isolate_deps_dir)
67 def Remap(self, isolate_abs_path, isolated_abs_path,
68 path_variables=None, config_variables=None):
69 """Remaps data dependencies into |self._isolate_deps_dir|.
71 Args:
72 isolate_abs_path: The absolute path to the .isolate file, which specifies
73 data dependencies in the source tree.
74 isolated_abs_path: The absolute path to the .isolated file, which is
75 generated by isolate.py and specifies data dependencies in
76 |self._isolate_deps_dir| and their digests.
77 path_variables: A dict containing everything that should be passed
78 as a |--path-variable| to the isolate script. Defaults to the return
79 value of |DefaultPathVariables()|.
80 config_variables: A dict containing everything that should be passed
81 as a |--config-variable| to the isolate script. Defaults to the return
82 value of |DefaultConfigVariables()|.
83 Raises:
84 Exception if the isolate command fails for some reason.
85 """
86 if not path_variables:
87 path_variables = DefaultPathVariables()
88 if not config_variables:
89 config_variables = DefaultConfigVariables()
91 isolate_cmd = [
92 sys.executable, _ISOLATE_SCRIPT, 'remap',
93 '--isolate', isolate_abs_path,
94 '--isolated', isolated_abs_path,
95 '--outdir', self._isolate_deps_dir,
97 for k, v in path_variables.iteritems():
98 isolate_cmd.extend(['--path-variable', k, v])
99 for k, v in config_variables.iteritems():
100 isolate_cmd.extend(['--config-variable', k, v])
102 if cmd_helper.RunCmd(isolate_cmd):
103 raise Exception('isolate command failed: %s' % ' '.join(isolate_cmd))
105 def VerifyHardlinks(self):
106 """Checks |isolate_deps_dir| for a hardlink.
108 Returns:
109 True if a hardlink is found.
110 False if nothing is found.
111 Raises:
112 Exception if a non-hardlink is found.
114 for root, _, filenames in os.walk(self._isolate_deps_dir):
115 if filenames:
116 linked_file = os.path.join(root, filenames[0])
117 orig_file = os.path.join(
118 self._isolate_deps_dir,
119 os.path.relpath(linked_file, self._isolate_deps_dir))
120 if os.stat(linked_file).st_ino == os.stat(orig_file).st_ino:
121 return True
122 else:
123 raise Exception('isolate remap command did not use hardlinks.')
124 return False
126 def PurgeExcluded(self, deps_exclusion_list):
127 """Deletes anything on |deps_exclusion_list| from |self._isolate_deps_dir|.
129 Args:
130 deps_exclusion_list: A list of globs to exclude from the isolate
131 dependency directory.
133 excluded_paths = (
134 x for y in deps_exclusion_list
135 for x in glob.glob(
136 os.path.abspath(os.path.join(self._isolate_deps_dir, y))))
137 for p in excluded_paths:
138 if os.path.isdir(p):
139 shutil.rmtree(p)
140 else:
141 os.remove(p)
143 def MoveOutputDeps(self):
144 """Moves files from the output directory to the top level of
145 |self._isolate_deps_dir|.
147 Moves pak files from the output directory to to <isolate_deps_dir>/paks
148 Moves files from the product directory to <isolate_deps_dir>
150 # On Android, all pak files need to be in the top-level 'paks' directory.
151 paks_dir = os.path.join(self._isolate_deps_dir, 'paks')
152 os.mkdir(paks_dir)
154 deps_out_dir = os.path.join(
155 self._isolate_deps_dir,
156 os.path.relpath(os.path.join(constants.GetOutDirectory(), os.pardir),
157 constants.DIR_SOURCE_ROOT))
158 for root, _, filenames in os.walk(deps_out_dir):
159 for filename in fnmatch.filter(filenames, '*.pak'):
160 shutil.move(os.path.join(root, filename), paks_dir)
162 # Move everything in PRODUCT_DIR to top level.
163 deps_product_dir = os.path.join(deps_out_dir, constants.GetBuildType())
164 if os.path.isdir(deps_product_dir):
165 for p in os.listdir(deps_product_dir):
166 shutil.move(os.path.join(deps_product_dir, p), self._isolate_deps_dir)
167 os.rmdir(deps_product_dir)
168 os.rmdir(deps_out_dir)