Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / tools / swarming_client / example / 4_swarming_two_steps.py
1 #!/usr/bin/env python
2 # Copyright 2012 The Swarming Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 that
4 # can be found in the LICENSE file.
5
6 """Runs hello_world.py, through hello_world.isolate, remotely on a Swarming
7 slave.
8
9 It compiles and archives via 'isolate.py archive', then discard the local files.
10 After, it triggers and finally collects the results.
11 """
12
13 import hashlib
14 import os
15 import shutil
16 import subprocess
17 import sys
18 import tempfile
19
20 # Pylint can't find common.py that's in the same directory as this file.
21 # pylint: disable=F0401
22 import common
23
24
25 def main():
26   options = common.parse_args(use_isolate_server=True, use_swarming=True)
27   try:
28     tempdir = tempfile.mkdtemp(prefix='hello_world')
29     try:
30       # All the files are put in a temporary directory. This is optional and
31       # simply done so the current directory doesn't have the following files
32       # created:
33       # - hello_world.isolated
34       # - hello_world.isolated.state
35       isolated = os.path.join(tempdir, 'hello_world.isolated')
36       common.note('Archiving to %s' % options.isolate_server)
37       common.run(
38           [
39             'isolate.py',
40             'archive',
41             '--isolate', os.path.join('payload', 'hello_world.isolate'),
42             '--isolated', isolated,
43             '--isolate-server', options.isolate_server,
44             '--config-variable', 'OS', options.swarming_os,
45           ], options.verbose)
46       with open(isolated, 'rb') as f:
47         hashval = hashlib.sha1(f.read()).hexdigest()
48     finally:
49       shutil.rmtree(tempdir)
50
51     # At this point, the temporary directory is not needed anymore.
52     tempdir = None
53
54     common.note('Running on %s' % options.swarming)
55     cmd = [
56       'swarming.py',
57       'trigger',
58       '--swarming', options.swarming,
59       '--isolate-server', options.isolate_server,
60       '--dimension', 'os', options.swarming_os,
61       '--task-name', options.task_name,
62       hashval,
63     ]
64     if options.priority is not None:
65       cmd.extend(('--priority', str(options.priority)))
66     common.run(cmd, options.verbose)
67
68     common.note('Getting results from %s' % options.swarming)
69     common.run(
70         [
71           'swarming.py',
72           'collect',
73           '--swarming', options.swarming,
74           options.task_name,
75         ], options.verbose)
76     return 0
77   except subprocess.CalledProcessError as e:
78     print e.returncode or 1
79
80
81 if __name__ == '__main__':
82   sys.exit(main())