OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 # |
| 5 # A python wrapper to call autotest ebuild. |
| 6 # |
| 7 # DO NOT CALL THIS SCRIPT DIRECTLY, CALL src/scripts/autotest INSTEAD. |
| 8 |
| 9 import logging, optparse, os, subprocess, sys |
| 10 |
| 11 |
| 12 def run(cmd): |
| 13 return subprocess.call(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 14 |
| 15 |
| 16 class MyOptionPaser(optparse.OptionParser): |
| 17 """Override python's builtin OptionParser to accept any undefined args.""" |
| 18 |
| 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.') |
| 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 MyOptionPaser.help: |
| 90 if options.build: |
| 91 print |
| 92 print 'Options inherited from autotest_client, which is used in build', |
| 93 print 'only mode.' |
| 94 run([AUTOTEST_CLIENT, '--help']) |
| 95 else: |
| 96 print |
| 97 print 'Options inherited from autoserv:' |
| 98 run([AUTOSERV, '--help']) |
| 99 sys.exit(-1) |
| 100 return options, args |
| 101 |
| 102 |
| 103 def build_autotest(options): |
| 104 environ = os.environ |
| 105 if options.jobs != -1: |
| 106 emerge_jobs = '--jobs=%d' % options.jobs |
| 107 else: |
| 108 emerge_jobs = '' |
| 109 |
| 110 # Decide on USE flags based on options |
| 111 use_flag = environ.get('USE', '') |
| 112 if not options.autox: |
| 113 use_flag = use_flag + ' -autox' |
| 114 if options.buildcheck: |
| 115 use_flag = use_flag + ' buildcheck' |
| 116 |
| 117 board_blacklist_file = ('%s/src/overlays/overlay-%s/autotest-blacklist' % |
| 118 (os.environ['GCLIENT_ROOT'], options.board)) |
| 119 if os.path.exists(board_blacklist_file): |
| 120 blacklist = [line.strip() |
| 121 for line in open(board_blacklist_file).readlines()] |
| 122 else: |
| 123 blacklist = [] |
| 124 |
| 125 all_tests = 'compilebench,dbench,disktest,netperf2,ltp,unixbench' |
| 126 site_tests = '../third_party/autotest/files/client/site_tests' |
| 127 for site_test in os.listdir(site_tests): |
| 128 test_path = os.path.join(site_tests, site_test) |
| 129 if (os.path.exists(test_path) and os.path.isdir(test_path) |
| 130 and site_test not in blacklist): |
| 131 all_tests += ',' + site_test |
| 132 |
| 133 if 'all' == options.build.lower(): |
| 134 if options.noprompt is not True: |
| 135 print 'You want to pre-build all client tests and it may take a long', |
| 136 print 'time to finish.' |
| 137 print 'Are you sure you want to continue?(N/y)', |
| 138 answer = sys.stdin.readline() |
| 139 if 'y' != answer[0].lower(): |
| 140 print 'Use --build to specify tests you like to pre-compile. ' |
| 141 print 'E.g.: ./autotest --build=disktest,hardware_SAT' |
| 142 sys.exit(0) |
| 143 test_list = all_tests |
| 144 else: |
| 145 test_list = options.build |
| 146 |
| 147 environ['FEATURES'] = ('%s -buildpkg -collision-protect' % |
| 148 environ.get('FEATURES', '')) |
| 149 environ['TEST_LIST'] = test_list |
| 150 environ['USE'] = use_flag |
| 151 emerge_cmd = ['emerge-%s' % options.board, |
| 152 'chromeos-base/autotest'] |
| 153 if emerge_jobs: |
| 154 emerge_cmd.append(emerge_jobs) |
| 155 status = run(emerge_cmd) |
| 156 if status: |
| 157 print 'build_autotest failed.' |
| 158 sys.exit(status) |
| 159 |
| 160 |
| 161 def run_autoserv(board, args): |
| 162 environ = os.environ |
| 163 environ['AUTOSERV_ARGS'] = ' '.join(args) |
| 164 environ['FEATURES'] = ('%s -buildpkg -digest noauto' % |
| 165 environ.get('FEATURES', '')) |
| 166 ebuild_cmd = ['ebuild-%s' % board, |
| 167 '../third_party/chromiumos-overlay/chromeos-base/' |
| 168 'autotest/autotest-0.0.1.ebuild', |
| 169 'clean', 'unpack', 'test'] |
| 170 run(ebuild_cmd) |
| 171 |
| 172 |
| 173 def main(): |
| 174 options, args = parse_args_and_help() |
| 175 if options.build: |
| 176 build_autotest(options) |
| 177 else: |
| 178 run_autoserv(options.board, args) |
| 179 |
| 180 |
| 181 if __name__ == '__main__': |
| 182 main() |
| 183 |
OLD | NEW |