Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / tools / swarming_client / example / 5_swarming_run_manual_upload.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 a manually crafted hello_world.isolated, remotely
7 on a Swarming slave.
8
9 No .isolate file is involved at all.
10
11 It creates hello_world.isolated and archives via 'isolateserver.py archive',
12 then trigger and finally collect the results.
13
14 It never create a local file.
15 """
16
17 import json
18 import os
19 import subprocess
20 import sys
21 import tempfile
22
23 # Pylint can't find common.py that's in the same directory as this file.
24 # pylint: disable=F0401
25 import common
26
27
28 def main():
29   options = common.parse_args(use_isolate_server=True, use_swarming=True)
30   try:
31     common.note(
32         'Archiving directory \'payload\' to %s' % options.isolate_server)
33     payload_isolated_sha1 = common.capture(
34         [
35           'isolateserver.py',
36           'archive',
37           '--isolate-server', options.isolate_server,
38           'payload',
39         ]).split()[0]
40
41     common.note(
42         'Archiving custom .isolated file to %s' % options.isolate_server)
43     handle, isolated = tempfile.mkstemp(
44         prefix='hello_world', suffix='.isolated')
45     os.close(handle)
46     try:
47       data = {
48         'algo': 'sha-1',
49         'command': ['python', 'hello_world.py', 'Custom'],
50         'includes': [payload_isolated_sha1],
51         'version': '1.0',
52       }
53       with open(isolated, 'wb') as f:
54         json.dump(data, f, sort_keys=True, separators=(',',':'))
55       isolated_sha1 = common.capture(
56           [
57             'isolateserver.py',
58             'archive',
59             '--isolate-server', options.isolate_server,
60             isolated,
61           ]).split()[0]
62     finally:
63       common.note('Deleting temporary file, it is not necessary anymore.')
64       os.remove(isolated)
65
66     # Now trigger as usual. You could look at run_exmaple_swarming_involved for
67     # the involved way but use the short way here.
68
69     common.note('Running %s on %s' % (isolated_sha1, options.swarming))
70     cmd = [
71       'swarming.py',
72       'run',
73       '--swarming', options.swarming,
74       '--isolate-server', options.isolate_server,
75       '--dimension', 'os', options.swarming_os,
76       '--task-name', options.task_name,
77       isolated_sha1,
78     ]
79     if options.priority is not None:
80       cmd.extend(('--priority', str(options.priority)))
81     common.run(cmd, options.verbose)
82     return 0
83   except subprocess.CalledProcessError as e:
84     print e.returncode or 1
85
86
87 if __name__ == '__main__':
88   sys.exit(main())