| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame^] | 1 | # Copyright 2012 The LUCI Authors. All rights reserved. |
| 2 | # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 | # that can be found in the LICENSE file. |
| 4 | |
| 5 | import datetime |
| 6 | import getpass |
| 7 | import hashlib |
| 8 | import optparse |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | |
| 14 | ROOT_DIR = os.path.dirname(os.path.abspath( |
| 15 | __file__.decode(sys.getfilesystemencoding()))) |
| 16 | sys.path.append(os.path.join(ROOT_DIR, '..', 'third_party')) |
| 17 | |
| 18 | import colorama |
| 19 | |
| 20 | |
| 21 | CHROMIUM_SWARMING_OSES = { |
| 22 | 'darwin': 'Mac', |
| 23 | 'cygwin': 'Windows', |
| 24 | 'linux2': 'Ubuntu', |
| 25 | 'win32': 'Windows', |
| 26 | } |
| 27 | |
| 28 | |
| 29 | def parse_args(use_isolate_server, use_swarming): |
| 30 | """Process arguments for the example scripts.""" |
| 31 | os.chdir(ROOT_DIR) |
| 32 | colorama.init() |
| 33 | |
| 34 | parser = optparse.OptionParser(description=sys.modules['__main__'].__doc__) |
| 35 | if use_isolate_server: |
| 36 | parser.add_option( |
| 37 | '-I', '--isolate-server', |
| 38 | metavar='URL', default=os.environ.get('ISOLATE_SERVER', ''), |
| 39 | help='Isolate server to use') |
| 40 | if use_swarming: |
| 41 | task_name = '%s-%s-hello_world' % ( |
| 42 | getpass.getuser(), |
| 43 | datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')) |
| 44 | parser.add_option( |
| 45 | '--idempotent', action='store_true', |
| 46 | help='Tells Swarming to reused previous task result if possible') |
| 47 | parser.add_option( |
| 48 | '-S', '--swarming', |
| 49 | metavar='URL', default=os.environ.get('SWARMING_SERVER', ''), |
| 50 | help='Swarming server to use') |
| 51 | parser.add_option( |
| 52 | '-o', '--os', default=sys.platform, |
| 53 | help='Swarming slave OS to request. Should be one of the valid ' |
| 54 | 'sys.platform values like darwin, linux2 or win32 default: ' |
| 55 | '%default.') |
| 56 | parser.add_option( |
| 57 | '-t', '--task-name', default=task_name, |
| 58 | help='Swarming task name, default is based on time: %default') |
| 59 | parser.add_option('-v', '--verbose', action='count', default=0) |
| 60 | parser.add_option( |
| 61 | '--priority', metavar='INT', type='int', help='Priority to use') |
| 62 | options, args = parser.parse_args() |
| 63 | |
| 64 | if args: |
| 65 | parser.error('Unsupported argument %s' % args) |
| 66 | if use_isolate_server and not options.isolate_server: |
| 67 | parser.error('--isolate-server is required.') |
| 68 | if use_swarming: |
| 69 | if not options.swarming: |
| 70 | parser.error('--swarming is required.') |
| 71 | options.swarming_os = CHROMIUM_SWARMING_OSES[options.os] |
| 72 | del options.os |
| 73 | |
| 74 | return options |
| 75 | |
| 76 | |
| 77 | def note(text): |
| 78 | """Prints a formatted note.""" |
| 79 | print( |
| 80 | colorama.Fore.YELLOW + colorama.Style.BRIGHT + '\n-> ' + text + |
| 81 | colorama.Fore.RESET) |
| 82 | |
| 83 | |
| 84 | def run(cmd, verbose): |
| 85 | """Prints the command it runs then run it.""" |
| 86 | cmd = cmd[:] |
| 87 | cmd.extend(['--verbose'] * verbose) |
| 88 | print( |
| 89 | 'Running: %s%s%s' % |
| 90 | (colorama.Fore.GREEN, ' '.join(cmd), colorama.Fore.RESET)) |
| 91 | cmd = [sys.executable, os.path.join('..', cmd[0])] + cmd[1:] |
| 92 | if sys.platform != 'win32': |
| 93 | cmd = ['time', '-p'] + cmd |
| 94 | subprocess.check_call(cmd) |
| 95 | |
| 96 | |
| 97 | def capture(cmd): |
| 98 | """Prints the command it runs then return stdout.""" |
| 99 | print( |
| 100 | 'Running: %s%s%s' % |
| 101 | (colorama.Fore.GREEN, ' '.join(cmd), colorama.Fore.RESET)) |
| 102 | cmd = [sys.executable, os.path.join('..', cmd[0])] + cmd[1:] |
| 103 | return subprocess.check_output(cmd) |
| 104 | |
| 105 | |
| 106 | def isolate(tempdir, isolate_server, swarming_os, verbose): |
| 107 | """Archives the payload.""" |
| 108 | # All the files are put in a temporary directory. This is optional and |
| 109 | # simply done so the current directory doesn't have the following files |
| 110 | # created: |
| 111 | # - hello_world.isolated |
| 112 | # - hello_world.isolated.state |
| 113 | isolated = os.path.join(tempdir, 'hello_world.isolated') |
| 114 | note('Archiving to %s' % isolate_server) |
| 115 | run( |
| 116 | [ |
| 117 | 'isolate.py', |
| 118 | 'archive', |
| 119 | '--isolate', os.path.join('payload', 'hello_world.isolate'), |
| 120 | '--isolated', isolated, |
| 121 | '--isolate-server', isolate_server, |
| 122 | '--config-variable', 'OS', swarming_os, |
| 123 | ], verbose) |
| 124 | with open(isolated, 'rb') as f: |
| 125 | hashval = hashlib.sha1(f.read()).hexdigest() |
| 126 | return isolated, hashval |