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 | |
mbligh | f5fdfab | 2008-06-16 23:57:25 +0000 | [diff] [blame] | 19 | CLIENT_STEP_TEMPLATE = " job.next_step('step%d')\n" |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 20 | SERVER_STEP_TEMPLATE = ' step%d()\n' |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 21 | |
showard | 232b7ae | 2009-11-10 00:46:48 +0000 | [diff] [blame] | 22 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 23 | def _read_control_file(test): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 24 | """Reads the test control file from local disk. |
| 25 | |
| 26 | @param test The test name. |
| 27 | |
| 28 | @return The test control file string. |
| 29 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 30 | control_file = open(os.path.join(AUTOTEST_DIR, test.path)) |
| 31 | control_contents = control_file.read() |
| 32 | control_file.close() |
| 33 | return control_contents |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 34 | |
| 35 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 36 | def _add_boilerplate_to_nested_steps(lines): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 37 | """Adds boilerplate magic. |
| 38 | |
| 39 | @param lines The string of lines. |
| 40 | |
| 41 | @returns The string lines. |
| 42 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 43 | # Look for a line that begins with 'def step_init():' while |
| 44 | # being flexible on spacing. If it's found, this will be |
| 45 | # a nested set of steps, so add magic to make it work. |
| 46 | # See client/bin/job.py's step_engine for more info. |
| 47 | if re.search(r'^(.*\n)*def\s+step_init\s*\(\s*\)\s*:', lines): |
| 48 | lines += '\nreturn locals() ' |
| 49 | lines += '# Boilerplate magic for nested sets of steps' |
| 50 | return lines |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 51 | |
| 52 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 53 | def _format_step(item, lines): |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 54 | """Format a line item. |
| 55 | @param item The item number. |
| 56 | @param lines The string of lines. |
| 57 | |
| 58 | @returns The string lines. |
| 59 | """ |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 60 | lines = _indent_text(lines, ' ') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | lines = 'def step%d():\n%s' % (item, lines) |
| 62 | return lines |
showard | 9ca5270 | 2008-06-02 21:14:49 +0000 | [diff] [blame] | 63 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 64 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 65 | def _get_tests_stanza(tests, is_server, prepend=None, append=None, |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 66 | client_control_file='', test_source_build=None): |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 67 | """ Constructs the control file test step code from a list of tests. |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 68 | |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 69 | @param tests A sequence of test control files to run. |
| 70 | @param is_server bool, Is this a server side test? |
| 71 | @param prepend A list of steps to prepend to each client test. |
| 72 | Defaults to []. |
| 73 | @param append A list of steps to append to each client test. |
| 74 | Defaults to []. |
| 75 | @param client_control_file If specified, use this text as the body of a |
| 76 | 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] | 77 | @param test_source_build: Build to be used to retrieve test code. Default |
| 78 | to None. |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 79 | |
| 80 | @returns The control file test code to be run. |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 81 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 82 | assert not (client_control_file and is_server) |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 83 | if not prepend: |
| 84 | prepend = [] |
| 85 | if not append: |
| 86 | append = [] |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 87 | if test_source_build: |
| 88 | raw_control_files = site_rpc_interface.get_test_control_files_by_build( |
| 89 | tests, test_source_build) |
| 90 | else: |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 91 | raw_control_files = [_read_control_file(test) for test in tests] |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 92 | if client_control_file: |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 93 | # 'return locals()' is always appended in case the user forgot, it |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 94 | # is necessary to allow for nested step engine execution to work. |
| 95 | raw_control_files.append(client_control_file + '\nreturn locals()') |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 96 | raw_steps = prepend + [_add_boilerplate_to_nested_steps(step) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 97 | for step in raw_control_files] + append |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 98 | steps = [_format_step(index, step) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 99 | for index, step in enumerate(raw_steps)] |
mbligh | c86113b | 2009-04-28 18:32:51 +0000 | [diff] [blame] | 100 | if is_server: |
| 101 | step_template = SERVER_STEP_TEMPLATE |
| 102 | footer = '\n\nstep_init()\n' |
| 103 | else: |
| 104 | step_template = CLIENT_STEP_TEMPLATE |
| 105 | footer = '' |
| 106 | |
| 107 | header = ''.join(step_template % i for i in xrange(len(steps))) |
| 108 | return header + '\n' + '\n\n'.join(steps) + footer |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 109 | |
| 110 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 111 | def _indent_text(text, indent): |
showard | d262415 | 2009-04-29 21:29:01 +0000 | [diff] [blame] | 112 | """Indent given lines of python code avoiding indenting multiline |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 113 | quoted content (only for triple " and ' quoting for now). |
| 114 | |
| 115 | @param text The string of lines. |
| 116 | @param indent The indent string. |
| 117 | |
| 118 | @return The indented string. |
| 119 | """ |
showard | d262415 | 2009-04-29 21:29:01 +0000 | [diff] [blame] | 120 | regex = re.compile('(\\\\*)("""|\'\'\')') |
| 121 | |
| 122 | res = [] |
| 123 | in_quote = None |
| 124 | for line in text.splitlines(): |
| 125 | # if not within a multinline quote indent the line contents |
| 126 | if in_quote: |
| 127 | res.append(line) |
| 128 | else: |
| 129 | res.append(indent + line) |
| 130 | |
| 131 | while line: |
| 132 | match = regex.search(line) |
| 133 | if match: |
| 134 | # for an even number of backslashes before the triple quote |
| 135 | if len(match.group(1)) % 2 == 0: |
| 136 | if not in_quote: |
| 137 | in_quote = match.group(2)[0] |
| 138 | elif in_quote == match.group(2)[0]: |
| 139 | # if we found a matching end triple quote |
| 140 | in_quote = None |
| 141 | line = line[match.end():] |
| 142 | else: |
| 143 | break |
| 144 | |
| 145 | return '\n'.join(res) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 146 | |
| 147 | |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 148 | def _get_profiler_commands(profilers, is_server, profile_only): |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 149 | prepend, append = [], [] |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 150 | if profile_only is not None: |
mbligh | fbf73ae | 2009-12-19 05:22:42 +0000 | [diff] [blame] | 151 | prepend.append("job.default_profile_only = %r" % profile_only) |
showard | 2b9a88b | 2008-06-13 20:55:03 +0000 | [diff] [blame] | 152 | for profiler in profilers: |
| 153 | prepend.append("job.profilers.add('%s')" % profiler.name) |
| 154 | append.append("job.profilers.delete('%s')" % profiler.name) |
| 155 | return prepend, append |
| 156 | |
| 157 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 158 | def _sanity_check_generate_control(is_server, client_control_file): |
mbligh | 12eafff | 2008-11-05 23:42:42 +0000 | [diff] [blame] | 159 | """ |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 160 | Sanity check some of the parameters to generate_control(). |
| 161 | |
| 162 | This exists as its own function so that site_control_file may call it as |
| 163 | well from its own generate_control(). |
| 164 | |
| 165 | @raises ValidationError if any of the parameters do not make sense. |
| 166 | """ |
| 167 | if is_server and client_control_file: |
| 168 | raise model_logic.ValidationError( |
| 169 | {'tests' : 'You cannot run server tests at the same time ' |
| 170 | 'as directly supplying a client-side control file.'}) |
| 171 | |
| 172 | |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 173 | def generate_control(tests, is_server=False, profilers=(), |
| 174 | client_control_file='', profile_only=None, |
| 175 | test_source_build=None): |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 176 | """ |
| 177 | Generate a control file for a sequence of tests. |
| 178 | |
| 179 | @param tests A sequence of test control files to run. |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 180 | @param is_server bool, Is this a server control file rather than a client? |
| 181 | @param profilers A list of profiler objects to enable during the tests. |
| 182 | @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] | 183 | last test after everything in tests. Requires is_server=False. |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 184 | @param profile_only bool, should this control file run all tests in |
| 185 | profile_only mode by default |
Michael Tang | 84a2ecf | 2016-06-07 15:10:53 -0700 | [diff] [blame] | 186 | @param test_source_build: Build to be used to retrieve test code. Default |
| 187 | to None. |
mbligh | 120351e | 2009-01-24 01:40:45 +0000 | [diff] [blame] | 188 | |
| 189 | @returns The control file text as a string. |
| 190 | """ |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 191 | _sanity_check_generate_control(is_server=is_server, |
| 192 | client_control_file=client_control_file) |
| 193 | control_file_text = EMPTY_TEMPLATE |
showard | 91f8510 | 2009-10-12 20:34:52 +0000 | [diff] [blame] | 194 | prepend, append = _get_profiler_commands(profilers, is_server, profile_only) |
Richard Barnette | 8e33b4e | 2016-05-21 12:12:26 -0700 | [diff] [blame] | 195 | control_file_text += _get_tests_stanza(tests, is_server, prepend, append, |
| 196 | client_control_file, |
| 197 | test_source_build) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 198 | return control_file_text |