Home | History | Annotate | Download | only in lib
      1 # Copyright (c) 2013 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 """Control file generation for the autoupdate_EndToEnd server-side test.
      6 
      7 This library is used by chromite/lib/paygen/paygen_build_lib.py to generate
      8 the paygen_au_* control files during the PaygenBuild* builder phase.
      9 
     10 """
     11 
     12 import os
     13 import re
     14 
     15 import common
     16 from autotest_lib.client.common_lib import control_data
     17 
     18 
     19 _name_re = re.compile('\s*NAME\s*=')
     20 _autotest_test_name = 'autoupdate_EndToEndTest'
     21 
     22 
     23 def generate_full_control_file(test, env, orig_control_code):
     24     """Returns the parameterized control file for the test config.
     25 
     26     @param test: the test config object (TestConfig)
     27     @param env: the test environment parameters (TestEnv or None)
     28     @param orig_control_code: string containing the template control code
     29 
     30     @returns Parameterized control file based on args (string)
     31 
     32     """
     33     orig_name = control_data.parse_control_string(orig_control_code).name
     34     code_lines = orig_control_code.splitlines()
     35     for i, line in enumerate(code_lines):
     36         if _name_re.match(line):
     37             new_name = '%s_%s' % (orig_name, test.unique_name_suffix())
     38             code_lines[i] = line.replace(orig_name, new_name)
     39             break
     40 
     41     env_code_args = env.get_code_args() if env else ''
     42     return test.get_code_args() + env_code_args + '\n'.join(code_lines) + '\n'
     43 
     44 
     45 def dump_autotest_control_file(test, env, control_code, directory):
     46     """Creates control file for test and returns the path to created file.
     47 
     48     @param test: the test config object (TestConfig)
     49     @param env: the test environment parameters (TestEnv)
     50     @param control_code: string containing the template control code
     51     @param directory: the directory to dump the control file to
     52 
     53     @returns Path to the newly dumped control file
     54 
     55     """
     56     if not os.path.exists(directory):
     57         os.makedirs(directory)
     58 
     59     parametrized_control_code = generate_full_control_file(
     60             test, env, control_code)
     61 
     62     control_file = os.path.join(directory,
     63                                 test.get_control_file_name())
     64     with open(control_file, 'w') as fh:
     65         fh.write(parametrized_control_code)
     66 
     67     return control_file
     68 
     69 
     70 def get_test_name():
     71   """Returns the name of the server side test."""
     72   return _autotest_test_name
     73 
     74 
     75 def get_control_file_name():
     76   """Returns the path of the end-to-end base control file."""
     77   return os.path.join(
     78       common.autotest_dir, 'server', 'site_tests',
     79       get_test_name(), 'control')
     80