OLD | NEW |
1 #!/bin/bash | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 # |
7 # This script is a shell wrapper of autotest.py inside a chroot environment. | 7 # A python wrapper to call autotest ebuild. |
8 # Note you should/could not directly call autotest.py within the same directory. | 8 |
9 | 9 import commands, logging, optparse, os, subprocess, sys |
10 . "$(dirname $0)/common.sh" | 10 |
11 | 11 |
12 # Script must be run inside the chroot | 12 def run(cmd): |
13 assert_inside_chroot | 13 return subprocess.call(cmd, stdout=sys.stdout, stderr=sys.stderr) |
14 | 14 |
15 SSH_KEY=$(dirname $0)/mod_for_test_scripts/ssh_keys/testing_rsa | 15 |
16 chmod 400 ${SSH_KEY} | 16 class MyOptionPaser(optparse.OptionParser): |
17 export GCLIENT_ROOT | 17 """Override python's builtin OptionParser to accept any undefined args.""" |
18 python $(dirname $0)/autotest.py $@ || die "autotest failed." | 18 |
19 | 19 help = False |
| 20 |
| 21 def _process_args(self, largs, rargs, values): |
| 22 # see /usr/lib64/python2.6/optparse.py line 1414-1463 |
| 23 while rargs: |
| 24 arg = rargs[0] |
| 25 # We handle bare "--" explicitly, and bare "-" is handled by the |
| 26 # standard arg handler since the short arg case ensures that the |
| 27 # len of the opt string is greater than 1. |
| 28 if arg == "--": |
| 29 del rargs[0] |
| 30 return |
| 31 elif arg[0:2] == "--": |
| 32 # process a single long option (possibly with value(s)) |
| 33 try: |
| 34 self._process_long_opt(rargs, values) |
| 35 except optparse.BadOptionError: |
| 36 largs.append(arg) |
| 37 elif arg[:1] == "-" and len(arg) > 1: |
| 38 # process a cluster of short options (possibly with |
| 39 # value(s) for the last one only) |
| 40 try: |
| 41 self._process_short_opts(rargs, values) |
| 42 except optparse.BadOptionError: |
| 43 largs.append(arg) |
| 44 elif self.allow_interspersed_args: |
| 45 largs.append(arg) |
| 46 del rargs[0] |
| 47 else: |
| 48 return # stop now, leave this arg in rargs |
| 49 |
| 50 def print_help(self, file=None): |
| 51 optparse.OptionParser.print_help(self, file) |
| 52 MyOptionPaser.help = True |
| 53 |
| 54 |
| 55 parser = MyOptionPaser() |
| 56 parser.allow_interspersed_args = True |
| 57 |
| 58 DEFAULT_BOARD = os.environ.get('DEFAULT_BOARD', '') |
| 59 |
| 60 parser.add_option('--autox', dest='autox', action='store_true', |
| 61 help='Build autox along with autotest.') |
| 62 parser.add_option('--board', dest='board', action='store', |
| 63 default=DEFAULT_BOARD, |
| 64 help='The board for which you are building autotest.') |
| 65 parser.add_option('--build', dest='build', action='store', |
| 66 help='Only prebuild client tests, do not run tests.') |
| 67 parser.add_option('--buildcheck', dest='buildcheck', action='store_true', |
| 68 help='Fail if tests fail to build.') |
| 69 parser.add_option('--jobs', dest='jobs', action='store', type=int, |
| 70 default=-1, |
| 71 help='How many packages to build in parallel at maximum.') |
| 72 parser.add_option('--noprompt', dest='noprompt', action='store_true', |
| 73 help='Prompt user when building all tests.') |
| 74 |
| 75 |
| 76 AUTOSERV='../third_party/autotest/files/server/autoserv' |
| 77 AUTOTEST_CLIENT='../third_party/autotest/files/client/bin/autotest_client' |
| 78 |
| 79 def parse_args_and_help(): |
| 80 |
| 81 def nop(_): |
| 82 pass |
| 83 |
| 84 sys_exit = sys.exit |
| 85 sys.exit = nop |
| 86 options, args = parser.parse_args() |
| 87 sys.exit = sys_exit |
| 88 |
| 89 if not args and not options.build: |
| 90 parser.print_help() |
| 91 |
| 92 if MyOptionPaser.help: |
| 93 if options.build: |
| 94 print |
| 95 print 'Options inherited from autotest_client, which is used in build', |
| 96 print 'only mode.' |
| 97 run([AUTOTEST_CLIENT, '--help']) |
| 98 else: |
| 99 print |
| 100 print 'Options inherited from autoserv:' |
| 101 run([AUTOSERV, '--help']) |
| 102 sys.exit(-1) |
| 103 return options, args |
| 104 |
| 105 |
| 106 def assert_inside_chroot(common_sh): |
| 107 status, output = commands.getstatusoutput('/bin/bash -c ". %s && ' |
| 108 'assert_inside_chroot"' % common_sh) |
| 109 if status is not 0: |
| 110 print >> sys.stderr, output |
| 111 sys.exit(status) |
| 112 |
| 113 |
| 114 def set_common_env(common_sh, env_var): |
| 115 env_value = commands.getoutput('/bin/bash -c \'. %s && echo $%s\'' % |
| 116 (common_sh, env_var)) |
| 117 os.environ[env_var] = env_value |
| 118 |
| 119 |
| 120 def die(common_sh, msg): |
| 121 output = commands.getoutput('/bin/bash -c \'. %s && die "%s"\'' % |
| 122 (common_sh, msg)) |
| 123 print >> sys.stderr, output |
| 124 sys.exit(1) |
| 125 |
| 126 |
| 127 def build_autotest(options): |
| 128 environ = os.environ |
| 129 if options.jobs != -1: |
| 130 emerge_jobs = '--jobs=%d' % options.jobs |
| 131 else: |
| 132 emerge_jobs = '' |
| 133 |
| 134 # Decide on USE flags based on options |
| 135 use_flag = environ.get('USE', '') |
| 136 if not options.autox: |
| 137 use_flag = use_flag + ' -autox' |
| 138 if options.buildcheck: |
| 139 use_flag = use_flag + ' buildcheck' |
| 140 |
| 141 board_blacklist_file = ('%s/src/overlays/overlay-%s/autotest-blacklist' % |
| 142 (os.environ['GCLIENT_ROOT'], options.board)) |
| 143 if os.path.exists(board_blacklist_file): |
| 144 blacklist = [line.strip() |
| 145 for line in open(board_blacklist_file).readlines()] |
| 146 else: |
| 147 blacklist = [] |
| 148 |
| 149 all_tests = 'compilebench,dbench,disktest,netperf2,ltp,unixbench' |
| 150 site_tests = '../third_party/autotest/files/client/site_tests' |
| 151 for site_test in os.listdir(site_tests): |
| 152 test_path = os.path.join(site_tests, site_test) |
| 153 if (os.path.exists(test_path) and os.path.isdir(test_path) |
| 154 and site_test not in blacklist): |
| 155 all_tests += ',' + site_test |
| 156 |
| 157 if 'all' == options.build.lower(): |
| 158 if options.noprompt is not True: |
| 159 print 'You want to pre-build all client tests and it may take a long', |
| 160 print 'time to finish.' |
| 161 print 'Are you sure you want to continue?(N/y)', |
| 162 answer = sys.stdin.readline() |
| 163 if 'y' != answer[0].lower(): |
| 164 print 'Use --build to specify tests you like to pre-compile. ' |
| 165 print 'E.g.: ./autotest --build=disktest,hardware_SAT' |
| 166 sys.exit(0) |
| 167 test_list = all_tests |
| 168 else: |
| 169 test_list = options.build |
| 170 |
| 171 environ['FEATURES'] = ('%s -buildpkg -collision-protect' % |
| 172 environ.get('FEATURES', '')) |
| 173 environ['TEST_LIST'] = test_list |
| 174 environ['USE'] = use_flag |
| 175 emerge_cmd = ['emerge-%s' % options.board, |
| 176 'chromeos-base/autotest'] |
| 177 if emerge_jobs: |
| 178 emerge_cmd.append(emerge_jobs) |
| 179 return run(emerge_cmd) |
| 180 |
| 181 |
| 182 def run_autoserv(board, args): |
| 183 environ = os.environ |
| 184 environ['AUTOSERV_ARGS'] = ' '.join(args) |
| 185 environ['FEATURES'] = ('%s -buildpkg -digest noauto' % |
| 186 environ.get('FEATURES', '')) |
| 187 ebuild_cmd = ['ebuild-%s' % board, |
| 188 '../third_party/chromiumos-overlay/chromeos-base/' |
| 189 'autotest/autotest-0.0.1.ebuild', |
| 190 'clean', 'unpack', 'test'] |
| 191 run(ebuild_cmd) |
| 192 |
| 193 |
| 194 def main(): |
| 195 me = sys.argv[0] |
| 196 common_sh = os.path.join(os.path.dirname(me), 'common.sh') |
| 197 |
| 198 assert_inside_chroot(common_sh) |
| 199 set_common_env(common_sh, 'GCLIENT_ROOT') |
| 200 |
| 201 options, args = parse_args_and_help() |
| 202 if options.build: |
| 203 status = build_autotest(options) |
| 204 if status: |
| 205 die(common_sh, 'build_autotest failed.') |
| 206 else: |
| 207 ssh_key_file = os.path.join(os.path.dirname(me), |
| 208 'mod_for_test_scripts/ssh_keys/testing_rsa') |
| 209 os.chmod(ssh_key_file, 0400) |
| 210 run_autoserv(options.board, args) |
| 211 |
| 212 |
| 213 if __name__ == '__main__': |
| 214 main() |
| 215 |
OLD | NEW |