1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Does one of the following depending on the --mode argument:
check verify all the inputs exist, touches the file specified with
--result and exits.
hashtable puts a manifest file and hard links each of the inputs into the
output directory.
remap stores all the inputs files in a directory without running the
executable.
run recreates a tree with all the inputs files and run the executable
in it.
See more information at
http://dev.chromium.org/developers/testing/isolated-testing
"""
import hashlib
import json
import logging
import optparse
import os
import re
import shutil
import subprocess
import sys
import tempfile
import time
import tree_creator
# Needs to be coherent with the file's docstring above.
VALID_MODES = ('check', 'hashtable', 'remap', 'run')
def touch(filename):
"""Implements the equivalent of the 'touch' command."""
if not os.path.exists(filename):
open(filename, 'a').close()
os.utime(filename, None)
def rmtree(root):
"""Wrapper around shutil.rmtree() to retry automatically on Windows."""
if sys.platform == 'win32':
for i in range(3):
try:
shutil.rmtree(root)
break
except WindowsError: # pylint: disable=E0602
delay = (i+1)*2
print >> sys.stderr, (
'The test has subprocess outliving it. Sleep %d seconds.' % delay)
time.sleep(delay)
else:
shutil.rmtree(root)
def relpath(path, root):
"""os.path.relpath() that keeps trailing slash."""
out = os.path.relpath(path, root)
if path.endswith('/'):
out += '/'
return out
def separate_inputs_command(args, root):
"""Strips off the command line from the inputs.
gyp provides input paths relative to cwd. Convert them to be relative to root.
"""
cmd = []
if '--' in args:
i = args.index('--')
cmd = args[i+1:]
args = args[:i]
cwd = os.getcwd()
return [relpath(os.path.join(cwd, arg), root) for arg in args], cmd
def isolate(outdir, resultfile, indir, infiles, mode, read_only, cmd):
"""Main function to isolate a target with its dependencies.
It's behavior depends on |mode|.
"""
if mode == 'run':
return run(outdir, resultfile, indir, infiles, read_only, cmd)
if mode == 'hashtable':
return hashtable(outdir, resultfile, indir, infiles)
assert mode in ('check', 'remap'), mode
if mode == 'remap':
if not outdir:
outdir = tempfile.mkdtemp(prefix='isolate')
tree_creator.recreate_tree(
outdir, indir, infiles, tree_creator.HARDLINK)
if read_only:
tree_creator.make_writable(outdir, True)
if resultfile:
# Signal the build tool that the test succeeded.
with open(resultfile, 'wb') as f:
for infile in infiles:
f.write(infile.encode('utf-8'))
f.write('\n')
def run(outdir, resultfile, indir, infiles, read_only, cmd):
"""Implements the 'run' mode."""
if not cmd:
print >> sys.stderr, 'Using first input %s as executable' % infiles[0]
cmd = [infiles[0]]
outdir = None
try:
outdir = tempfile.mkdtemp(prefix='isolate')
tree_creator.recreate_tree(
outdir, indir, infiles, tree_creator.HARDLINK)
if read_only:
tree_creator.make_writable(outdir, True)
# TODO(maruel): Remove me. Layering violation. Used by
# base/base_paths_linux.cc
env = os.environ.copy()
env['CR_SOURCE_ROOT'] = outdir.encode()
# Rebase the command to the right path.
cmd[0] = os.path.join(outdir, cmd[0])
logging.info('Running %s' % cmd)
result = subprocess.call(cmd, cwd=outdir, env=env)
if not result and resultfile:
# Signal the build tool that the test succeeded.
touch(resultfile)
return result
finally:
if read_only:
tree_creator.make_writable(outdir, False)
rmtree(outdir)
def hashtable(outdir, resultfile, indir, infiles):
"""Implements the 'hashtable' mode."""
results = {}
for relfile in infiles:
infile = os.path.join(indir, relfile)
h = hashlib.sha1()
with open(infile, 'rb') as f:
h.update(f.read())
digest = h.hexdigest()
outfile = os.path.join(outdir, digest)
tree_creator.process_file(outfile, infile, tree_creator.HARDLINK)
results[relfile] = {'sha1': digest}
json.dump(
{
'files': results,
},
open(resultfile, 'wb'))
def main():
parser = optparse.OptionParser(
usage='%prog [options] [inputs] -- [command line]',
description=sys.modules[__name__].__doc__)
parser.allow_interspersed_args = False
parser.format_description = lambda *_: parser.description
parser.add_option(
'-v', '--verbose', action='count', default=0, help='Use multiple times')
parser.add_option(
'--mode', choices=VALID_MODES,
help='Determines the action to be taken: %s' % ', '.join(VALID_MODES))
parser.add_option(
'--result', metavar='FILE',
help='File to be touched when the command succeeds')
parser.add_option(
'--root', metavar='DIR', help='Base directory to fetch files, required')
parser.add_option(
'--outdir', metavar='DIR',
help='Directory used to recreate the tree or store the hash table. '
'Defaults to a /tmp subdirectory for modes run and remap.')
parser.add_option(
'--read-only', action='store_true',
help='Make the temporary tree read-only')
options, args = parser.parse_args()
level = [logging.ERROR, logging.INFO, logging.DEBUG][min(2, options.verbose)]
logging.basicConfig(
level=level,
format='%(levelname)5s %(module)15s(%(lineno)3d): %(message)s')
if not options.root:
parser.error('--root is required.')
infiles, cmd = separate_inputs_command(args, options.root)
if not infiles:
parser.error('Need at least one input file to map')
# Preprocess the input files.
try:
infiles, root = tree_creator.preprocess_inputs(
options.root, infiles, lambda x: re.match(r'.*\.(svn|pyc)$', x))
return isolate(
options.outdir,
options.result,
root,
infiles,
options.mode,
options.read_only,
cmd)
except tree_creator.MappingError, e:
print >> sys.stderr, str(e)
return 1
if __name__ == '__main__':
sys.exit(main())
|