| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2012 The Swarming Authors. All rights reserved. | 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 | 3 # Use of this source code is governed under the Apache License, Version 2.0 that |
| 4 # can be found in the LICENSE file. | 4 # can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Runs hello_world.py, through hello_world.isolate, remotely on a Swarming | 6 """Runs hello_world.py, through hello_world.isolate, remotely on a Swarming |
| 7 slave. | 7 bot. |
| 8 | 8 |
| 9 It first 'compiles' hello_world.isolate into hello_word.isolated, then requests | 9 It first 'compiles' hello_world.isolate into hello_word.isolated, then requests |
| 10 via swarming.py to archives, run and collect results for this task. | 10 via swarming.py to archives, run and collect results for this task. |
| 11 |
| 12 It generates example_result.json as a task summary. |
| 11 """ | 13 """ |
| 12 | 14 |
| 13 import os | 15 import os |
| 14 import shutil | 16 import shutil |
| 15 import subprocess | 17 import subprocess |
| 16 import sys | 18 import sys |
| 17 import tempfile | 19 import tempfile |
| 18 | 20 |
| 19 # Pylint can't find common.py that's in the same directory as this file. | 21 # Pylint can't find common.py that's in the same directory as this file. |
| 20 # pylint: disable=F0401 | 22 # pylint: disable=F0401 |
| 21 import common | 23 import common |
| 22 | 24 |
| 23 | 25 |
| 24 def main(): | 26 def main(): |
| 25 options = common.parse_args(use_isolate_server=True, use_swarming=True) | 27 options = common.parse_args(use_isolate_server=True, use_swarming=True) |
| 26 tempdir = tempfile.mkdtemp(prefix=u'hello_world') | 28 tempdir = tempfile.mkdtemp(prefix=u'hello_world') |
| 27 try: | 29 try: |
| 28 # All the files are put in a temporary directory. This is optional and | 30 isolated, _ = common.isolate( |
| 29 # simply done so the current directory doesn't have the following files | 31 tempdir, options.isolate_server, options.swarming_os, options.verbose) |
| 30 # created: | |
| 31 # - hello_world.isolated | |
| 32 # - hello_world.isolated.state | |
| 33 isolated = os.path.join(tempdir, 'hello_world.isolated') | |
| 34 | |
| 35 common.note( | |
| 36 'Creating hello_world.isolated. Note that this doesn\'t archives ' | |
| 37 'anything.') | |
| 38 common.run( | |
| 39 [ | |
| 40 'isolate.py', | |
| 41 'check', | |
| 42 '--isolate', os.path.join('payload', 'hello_world.isolate'), | |
| 43 '--isolated', isolated, | |
| 44 '--config-variable', 'OS', options.swarming_os, | |
| 45 ], options.verbose) | |
| 46 | |
| 47 common.note( | 32 common.note( |
| 48 'Running the job remotely. This:\n' | 33 'Running the job remotely. This:\n' |
| 49 ' - archives to %s\n' | 34 ' - archives to %s\n' |
| 50 ' - runs and collect results via %s' % | 35 ' - runs and collect results via %s' % |
| 51 (options.isolate_server, options.swarming)) | 36 (options.isolate_server, options.swarming)) |
| 52 cmd = [ | 37 cmd = [ |
| 53 'swarming.py', | 38 'swarming.py', |
| 54 'run', | 39 'run', |
| 55 '--swarming', options.swarming, | 40 '--swarming', options.swarming, |
| 56 '--isolate-server', options.isolate_server, | 41 '--isolate-server', options.isolate_server, |
| 57 '--dimension', 'os', options.swarming_os, | 42 '--dimension', 'os', options.swarming_os, |
| 58 '--task-name', options.task_name, | 43 '--task-name', options.task_name, |
| 44 '--task-summary-json', 'example_result.json', |
| 45 '--decorate', |
| 59 isolated, | 46 isolated, |
| 60 ] | 47 ] |
| 61 if options.idempotent: | 48 if options.idempotent: |
| 62 cmd.append('--idempotent') | 49 cmd.append('--idempotent') |
| 63 if options.priority is not None: | 50 if options.priority is not None: |
| 64 cmd.extend(('--priority', str(options.priority))) | 51 cmd.extend(('--priority', str(options.priority))) |
| 65 common.run(cmd, options.verbose) | 52 common.run(cmd, options.verbose) |
| 53 with open('example_result.json', 'rb') as f: |
| 54 print('example_result.json content:') |
| 55 print(f.read()) |
| 66 return 0 | 56 return 0 |
| 67 except subprocess.CalledProcessError as e: | 57 except subprocess.CalledProcessError as e: |
| 68 return e.returncode | 58 return e.returncode |
| 69 finally: | 59 finally: |
| 70 shutil.rmtree(tempdir) | 60 shutil.rmtree(tempdir) |
| 71 | 61 |
| 72 | 62 |
| 73 if __name__ == '__main__': | 63 if __name__ == '__main__': |
| 74 sys.exit(main()) | 64 sys.exit(main()) |
| OLD | NEW |