mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | """\ |
| 2 | Logic for control file generation. |
| 3 | """ |
| 4 | |
| 5 | __author__ = 'showard@google.com (Steve Howard)' |
| 6 | |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 7 | import re, os |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 8 | |
| 9 | import common |
| 10 | from autotest_lib.frontend.afe import model_logic |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 11 | from autotest_lib.frontend.afe import site_rpc_interface |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 12 | import frontend.settings |
| 13 | |
| 14 | AUTOTEST_DIR = os.path.abspath(os.path.join( |
| 15 | os.path.dirname(frontend.settings.__file__), '..')) |
| 16 | |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 17 | EMPTY_TEMPLATE = 'def step_init():\n' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 18 | |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 19 | CLIENT_KERNEL_TEMPLATE = """\ |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 20 | kernel_list = %(client_kernel_list)s |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 21 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 22 | def step_init(): |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 23 | for kernel_info in kernel_list: |
| 24 | job.next_step(boot_kernel, kernel_info) |
| 25 | job.next_step(step_test, kernel_info['version']) |
mbligh | e39c3c0 | 2008-11-18 15:00:06 +0000 | [diff] [blame] | 26 | if len(kernel_list) > 1: |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 27 | job.use_sequence_number = True # include run numbers in directory names |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 28 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 29 | |
| 30 | def boot_kernel(kernel_info): |
mbligh | 3c0ea96 | 2009-11-06 03:02:38 +0000 | [diff] [blame] | 31 | # remove kernels (and associated data) not referenced by the bootloader |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 32 | for host in job.hosts: |
| 33 | host.cleanup_kernels() |
mbligh | 3c0ea96 | 2009-11-06 03:02:38 +0000 | [diff] [blame] | 34 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 35 | testkernel = job.kernel(kernel_info['version']) |
| 36 | if kernel_info['config_file']: |
| 37 | testkernel.config(kernel_info['config_file']) |
mbligh | eaa75e5 | 2009-11-06 03:08:08 +0000 | [diff] [blame] | 38 | testkernel.build() |
mbligh | f5fdfab | 2008-06-16 23:57:25 +0000 | [diff] [blame] | 39 | testkernel.install() |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 40 | |
| 41 | cmdline = ' '.join((kernel_info.get('cmdline', ''), '%(kernel_args)s')) |
| 42 | testkernel.boot(args=cmdline) |
| 43 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 44 | |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 45 | def step_test(kernel_version): |
mbligh | 6d2a6f9 | 2008-11-13 16:47:52 +0000 | [diff] [blame] | 46 | global kernel |
| 47 | kernel = kernel_version # Set the global in case anyone is using it. |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 48 | if len(kernel_list) > 1: |
| 49 | # this is local to a machine, safe to assume there's only one host |
| 50 | host, = job.hosts |
| 51 | job.automatic_test_tag = host.get_kernel_ver() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 52 | """ |
| 53 | |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 54 | SERVER_KERNEL_TEMPLATE = """\ |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 55 | kernel_list = %%(server_kernel_list)s |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 56 | kernel_install_control = \""" |
mbligh | f5fdfab | 2008-06-16 23:57:25 +0000 | [diff] [blame] | 57 | %s pass |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 58 | \""" |
| 59 | |
showard | a5512cd | 2009-06-30 01:59:22 +0000 | [diff] [blame] | 60 | from autotest_lib.client.common_lib import error |
| 61 | |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 62 | at = autotest.Autotest() |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 63 | |
| 64 | %%(upload_config_func)s |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 65 | def install_kernel(machine, kernel_info): |
jadmanski | 8d631c9 | 2008-08-18 21:12:40 +0000 | [diff] [blame] | 66 | host = hosts.create_host(machine) |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 67 | at.install(host=host) |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 68 | %%(call_upload_config)s |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 69 | at.run(kernel_install_control %%%% |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 70 | {'client_kernel_list': repr([kernel_info])}, host=host) |
| 71 | |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 72 | |
mbligh | 415dc21 | 2009-06-15 21:53:34 +0000 | [diff] [blame] | 73 | num_machines_required = len(machines) |
| 74 | if len(machines) > 4: |
| 75 | # Allow a large multi-host tests to proceed despite a couple of hosts |
| 76 | # failing to properly install the desired kernel (exclude those hosts). |
| 77 | # TODO(gps): Figure out how to get and use SYNC_COUNT here. It is defined |
| 78 | # within some control files and will end up inside of stepN functions below. |
| 79 | num_machines_required = len(machines) - 2 |
| 80 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 81 | |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 82 | def step_init(): |
| 83 | # a host object we use solely for the purpose of finding out the booted |
| 84 | # kernel version, we use machines[0] since we already check that the same |
| 85 | # kernel has been booted on all machines |
| 86 | if len(kernel_list) > 1: |
| 87 | kernel_host = hosts.create_host(machines[0]) |
| 88 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 89 | for kernel_info in kernel_list: |
| 90 | func = lambda machine: install_kernel(machine, kernel_info) |
mbligh | 415dc21 | 2009-06-15 21:53:34 +0000 | [diff] [blame] | 91 | good_machines = job.parallel_on_machines(func, machines) |
| 92 | if len(good_machines) < num_machines_required: |
| 93 | raise error.TestError( |
| 94 | "kernel installed on only %%%%d of %%%%d machines." |
| 95 | %%%% (len(good_machines), num_machines_required)) |
| 96 | |
| 97 | # Replace the machines list that step_test() will use with the |
| 98 | # ones that successfully installed the kernel. |
| 99 | machines[:] = good_machines |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 100 | |
| 101 | # have server_job.run_test() automatically add the kernel version as |
| 102 | # a suffix to the test name otherwise we cannot run the same test on |
| 103 | # different kernel versions |
| 104 | if len(kernel_list) > 1: |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 105 | job.automatic_test_tag = kernel_host.get_kernel_ver() |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 106 | step_test() |
| 107 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 108 | |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 109 | def step_test(): |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 110 | """ % CLIENT_KERNEL_TEMPLATE |
| 111 | |
mbligh | f5fdfab | 2008-06-16 23:57:25 +0000 | [diff] [blame] | 112 | CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n" |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 113 | SERVER_STEP_TEMPLATE = ' step%d()\n' |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 114 | |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 115 | UPLOAD_CONFIG_FUNC = """ |
| 116 | def upload_kernel_config(host, kernel_info): |
| 117 | \""" |
| 118 | If the kernel_info['config_file'] is a URL it will be downloaded |
| 119 | locally and then uploaded to the client and a copy of the original |
| 120 | dictionary with the new path to the config file will be returned. |
| 121 | If the config file is not a URL the function returns the original |
| 122 | dictionary. |
| 123 | \""" |
| 124 | import os |
| 125 | from autotest_lib.client.common_lib import autotemp, utils |
| 126 | |
| 127 | config_orig = kernel_info.get('config_file') |
| 128 | |
| 129 | # if the file is not an URL then we assume it's a local client path |
| 130 | if not config_orig or not utils.is_url(config_orig): |
| 131 | return kernel_info |
| 132 | |
| 133 | # download it locally (on the server) and send it to the client |
| 134 | config_tmp = autotemp.tempfile('kernel_config_upload', dir=job.tmpdir) |
| 135 | try: |
| 136 | utils.urlretrieve(config_orig, config_tmp.name) |
| 137 | config_new = os.path.join(host.get_autodir(), 'tmp', |
| 138 | os.path.basename(config_orig)) |
| 139 | host.send_file(config_tmp.name, config_new) |
| 140 | finally: |
| 141 | config_tmp.clean() |
| 142 | |
| 143 | return dict(kernel_info, config_file=config_new) |
| 144 | |
| 145 | """ |
| 146 | |
| 147 | CALL_UPLOAD_CONFIG = 'kernel_info = upload_kernel_config(host, kernel_info)' |
| 148 | |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 149 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 150 | def kernel_config_file(kernel, platform): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 151 | """Gets the kernel config. |
| 152 | |
| 153 | @param kernel The kernel rpm . |
| 154 | @param platform The platform object. |
| 155 | |
| 156 | @return The kernel config string or None. |
| 157 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 158 | if (not kernel.endswith('.rpm') and platform and |
| 159 | platform.kernel_config): |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 160 | return platform.kernel_config |
| 161 | return None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def read_control_file(test): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 165 | """Reads the test control file from local disk. |
| 166 | |
| 167 | @param test The test name. |
| 168 | |
| 169 | @return The test control file string. |
| 170 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 171 | control_file = open(os.path.join(AUTOTEST_DIR, test.path)) |
| 172 | control_contents = control_file.read() |
| 173 | control_file.close() |
| 174 | return control_contents |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 175 | |
| 176 | |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 177 | def get_kernel_stanza(kernel_list, platform=None, kernel_args='', |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 178 | is_server=False, upload_kernel_config=False): |
showard | 1d445e9 | 2008-03-12 21:33:31 +0000 | [diff] [blame] | 179 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 180 | template_args = {'kernel_args' : kernel_args} |
| 181 | |
| 182 | # add 'config_file' keys to the kernel_info dictionaries |
| 183 | new_kernel_list = [] |
| 184 | for kernel_info in kernel_list: |
showard | 39c843b | 2009-11-10 00:46:14 +0000 | [diff] [blame] | 185 | if kernel_info.get('config_file'): |
| 186 | # already got a config file from the user |
| 187 | new_kernel_info = kernel_info |
| 188 | else: |
| 189 | config_file = kernel_config_file(kernel_info['version'], platform) |
| 190 | new_kernel_info = dict(kernel_info, config_file=config_file) |
| 191 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 192 | new_kernel_list.append(new_kernel_info) |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 193 | |
| 194 | if is_server: |
| 195 | template = SERVER_KERNEL_TEMPLATE |
| 196 | # leave client_kernel_list as a placeholder |
| 197 | template_args['client_kernel_list'] = '%(client_kernel_list)s' |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 198 | template_args['server_kernel_list'] = repr(new_kernel_list) |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 199 | |
| 200 | if upload_kernel_config: |
| 201 | template_args['call_upload_config'] = CALL_UPLOAD_CONFIG |
| 202 | template_args['upload_config_func'] = UPLOAD_CONFIG_FUNC |
| 203 | else: |
| 204 | template_args['call_upload_config'] = '' |
| 205 | template_args['upload_config_func'] = '' |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 206 | else: |
| 207 | template = CLIENT_KERNEL_TEMPLATE |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 208 | template_args['client_kernel_list'] = repr(new_kernel_list) |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 209 | |
| 210 | return template % template_args |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 211 | |
| 212 | |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 213 | def add_boilerplate_to_nested_steps(lines): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 214 | """Adds boilerplate magic. |
| 215 | |
| 216 | @param lines The string of lines. |
| 217 | |
| 218 | @returns The string lines. |
| 219 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 220 | # Look for a line that begins with 'def step_init():' while |
| 221 | # being flexible on spacing. If it's found, this will be |
| 222 | # a nested set of steps, so add magic to make it work. |
| 223 | # See client/bin/job.py's step_engine for more info. |
| 224 | if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines): |
| 225 | lines += '\nreturn locals() ' |
| 226 | lines += '# Boilerplate magic for nested sets of steps' |
| 227 | return lines |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def format_step(item, lines): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 231 | """Format a line item. |
| 232 | @param item The item number. |
| 233 | @param lines The string of lines. |
| 234 | |
| 235 | @returns The string lines. |
| 236 | """ |
mbligh | f5fdfab | 2008-06-16 23:57:25 +0000 | [diff] [blame] | 237 | lines = indent_text(lines, ' ') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 238 | lines = 'def step%d():\n%s' % (item, lines) |
| 239 | return lines |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 240 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 241 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 242 | def get_tests_stanza(tests, is_server, prepend=None, append=None, |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 243 | client_control_file='', test_source_build=None): |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 244 | """ Constructs the control file test step code from a list of tests. |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 245 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 246 | @param tests A sequence of test control files to run. |
| 247 | @param is_server bool, Is this a server side test? |
| 248 | @param prepend A list of steps to prepend to each client test. |
| 249 | Defaults to []. |
| 250 | @param append A list of steps to append to each client test. |
| 251 | Defaults to []. |
| 252 | @param client_control_file If specified, use this text as the body of a |
| 253 | final client control file to run after tests. is_server must be False. |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 254 | @param test_source_build: Build to be used to retrieve test code. Default |
| 255 | to None. |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 256 | |
| 257 | @returns The control file test code to be run. |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 258 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 259 | assert not (client_control_file and is_server) |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 260 | if not prepend: |
| 261 | prepend = [] |
| 262 | if not append: |
| 263 | append = [] |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 264 | if test_source_build: |
| 265 | raw_control_files = site_rpc_interface.get_test_control_files_by_build( |
| 266 | tests, test_source_build) |
| 267 | else: |
| 268 | raw_control_files = [read_control_file(test) for test in tests] |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 269 | return _get_tests_stanza(raw_control_files, is_server, prepend, append, |
| 270 | client_control_file=client_control_file) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 271 | |
| 272 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 273 | def _get_tests_stanza(raw_control_files, is_server, prepend, append, |
| 274 | client_control_file=''): |
| 275 | """ |
| 276 | Implements the common parts of get_test_stanza. |
| 277 | |
| 278 | A site_control_file that wants to implement its own get_tests_stanza |
| 279 | likely wants to call this in the end. |
| 280 | |
| 281 | @param raw_control_files A list of raw control file data to be combined |
| 282 | into a single control file. |
| 283 | @param is_server bool, Is this a server side test? |
| 284 | @param prepend A list of steps to prepend to each client test. |
| 285 | @param append A list of steps to append to each client test. |
| 286 | @param client_control_file If specified, use this text as the body of a |
| 287 | final client control file to append to raw_control_files after fixups. |
| 288 | |
| 289 | @returns The combined mega control file. |
| 290 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 291 | if client_control_file: |
| 292 | # 'return locals()' is always appended incase the user forgot, it |
| 293 | # is necessary to allow for nested step engine execution to work. |
| 294 | raw_control_files.append(client_control_file + '\nreturn locals()') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 295 | raw_steps = prepend + [add_boilerplate_to_nested_steps(step) |
| 296 | for step in raw_control_files] + append |
| 297 | steps = [format_step(index, step) |
| 298 | for index, step in enumerate(raw_steps)] |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 299 | if is_server: |
| 300 | step_template = SERVER_STEP_TEMPLATE |
| 301 | footer = '\n\nstep_init()\n' |
| 302 | else: |
| 303 | step_template = CLIENT_STEP_TEMPLATE |
| 304 | footer = '' |
| 305 | |
| 306 | header = ''.join(step_template % i for i in xrange(len(steps))) |
| 307 | return header + '\n' + '\n\n'.join(steps) + footer |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | def indent_text(text, indent): |
showard | d262415 | 2009-04-29 21:29:01 +0000 | [diff] [blame] | 311 | """Indent given lines of python code avoiding indenting multiline |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 312 | quoted content (only for triple " and ' quoting for now). |
| 313 | |
| 314 | @param text The string of lines. |
| 315 | @param indent The indent string. |
| 316 | |
| 317 | @return The indented string. |
| 318 | """ |
showard | d262415 | 2009-04-29 21:29:01 +0000 | [diff] [blame] | 319 | regex = re.compile('(\\\\*)("""|\'\'\')') |
| 320 | |
| 321 | res = [] |
| 322 | in_quote = None |
| 323 | for line in text.splitlines(): |
| 324 | # if not within a multinline quote indent the line contents |
| 325 | if in_quote: |
| 326 | res.append(line) |
| 327 | else: |
| 328 | res.append(indent + line) |
| 329 | |
| 330 | while line: |
| 331 | match = regex.search(line) |
| 332 | if match: |
| 333 | # for an even number of backslashes before the triple quote |
| 334 | if len(match.group(1)) % 2 == 0: |
| 335 | if not in_quote: |
| 336 | in_quote = match.group(2)[0] |
| 337 | elif in_quote == match.group(2)[0]: |
| 338 | # if we found a matching end triple quote |
| 339 | in_quote = None |
| 340 | line = line[match.end():] |
| 341 | else: |
| 342 | break |
| 343 | |
| 344 | return '\n'.join(res) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 345 | |
| 346 | |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 347 | def _get_profiler_commands(profilers, is_server, profile_only): |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 348 | prepend, append = [], [] |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 349 | if profile_only is not None: |
mbligh | fbf73ae | 2009-12-19 05:22:42 +0000 | [diff] [blame] | 350 | prepend.append("job.default_profile_only = %r" % profile_only) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 351 | for profiler in profilers: |
| 352 | prepend.append("job.profilers.add('%s')" % profiler.name) |
| 353 | append.append("job.profilers.delete('%s')" % profiler.name) |
| 354 | return prepend, append |
| 355 | |
| 356 | |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 357 | def _sanity_check_generate_control(is_server, client_control_file, kernels, |
| 358 | upload_kernel_config): |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 359 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 360 | Sanity check some of the parameters to generate_control(). |
| 361 | |
| 362 | This exists as its own function so that site_control_file may call it as |
| 363 | well from its own generate_control(). |
| 364 | |
| 365 | @raises ValidationError if any of the parameters do not make sense. |
| 366 | """ |
| 367 | if is_server and client_control_file: |
| 368 | raise model_logic.ValidationError( |
| 369 | {'tests' : 'You cannot run server tests at the same time ' |
| 370 | 'as directly supplying a client-side control file.'}) |
| 371 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 372 | if kernels: |
| 373 | # make sure that kernel is a list of dictionarions with at least |
| 374 | # the 'version' key in them |
| 375 | kernel_error = model_logic.ValidationError( |
| 376 | {'kernel': 'The kernel parameter must be a sequence of ' |
| 377 | 'dictionaries containing at least the "version" key ' |
| 378 | '(got: %r)' % kernels}) |
| 379 | try: |
| 380 | iter(kernels) |
| 381 | except TypeError: |
| 382 | raise kernel_error |
| 383 | for kernel_info in kernels: |
| 384 | if (not isinstance(kernel_info, dict) or |
| 385 | 'version' not in kernel_info): |
| 386 | raise kernel_error |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 387 | |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 388 | if upload_kernel_config and not is_server: |
| 389 | raise model_logic.ValidationError( |
| 390 | {'upload_kernel_config': 'Cannot use upload_kernel_config ' |
| 391 | 'with client side tests'}) |
| 392 | |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 393 | |
| 394 | def generate_control(tests, kernels=None, platform=None, is_server=False, |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 395 | profilers=(), client_control_file='', profile_only=None, |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 396 | upload_kernel_config=False, test_source_build=None): |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 397 | """ |
| 398 | Generate a control file for a sequence of tests. |
| 399 | |
| 400 | @param tests A sequence of test control files to run. |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 401 | @param kernels A sequence of kernel info dictionaries configuring which |
| 402 | kernels to boot for this job and other options for them |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 403 | @param platform A platform object with a kernel_config attribute. |
| 404 | @param is_server bool, Is this a server control file rather than a client? |
| 405 | @param profilers A list of profiler objects to enable during the tests. |
| 406 | @param client_control_file Contents of a client control file to run as the |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 407 | last test after everything in tests. Requires is_server=False. |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 408 | @param profile_only bool, should this control file run all tests in |
| 409 | profile_only mode by default |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 410 | @param upload_kernel_config: if enabled it will generate server control |
| 411 | file code that uploads the kernel config file to the client and |
| 412 | tells the client of the new (local) path when compiling the kernel; |
| 413 | the tests must be server side tests |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 414 | @param test_source_build: Build to be used to retrieve test code. Default |
| 415 | to None. |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 416 | |
| 417 | @returns The control file text as a string. |
| 418 | """ |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 419 | _sanity_check_generate_control(is_server=is_server, kernels=kernels, |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 420 | client_control_file=client_control_file, |
| 421 | upload_kernel_config=upload_kernel_config) |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 422 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 423 | control_file_text = '' |
mbligh | a3c58d2 | 2009-08-24 22:01:51 +0000 | [diff] [blame] | 424 | if kernels: |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 425 | control_file_text = get_kernel_stanza( |
| 426 | kernels, platform, is_server=is_server, |
| 427 | upload_kernel_config=upload_kernel_config) |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 428 | else: |
| 429 | control_file_text = EMPTY_TEMPLATE |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 430 | |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 431 | prepend, append = _get_profiler_commands(profilers, is_server, profile_only) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 432 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 433 | control_file_text += get_tests_stanza(tests, is_server, prepend, append, |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame^] | 434 | client_control_file, |
| 435 | test_source_build) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 436 | return control_file_text |