Home | History | Annotate | Download | only in health
      1 #!/usr/bin/python
      2 #
      3 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 import argparse, logging, os, subprocess, sys
      8 
      9 THIS_DIR = os.path.dirname(__file__)
     10 UTILS_DIR = os.path.abspath(os.path.join(THIS_DIR, os.pardir, os.pardir,
     11                                          'utils'))
     12 TEST_IMPORTER = os.path.join(UTILS_DIR, 'test_importer.py')
     13 
     14 # The list of scripts are passed as lists as this allows us to add arguments
     15 # as the list is just passed to subprocess.call(). We expect the scripts to
     16 # return 0 on success and a non-zero value on failure.
     17 
     18 # Scripts that need to be ran first to do any preperation. E.g. update the
     19 # database.
     20 PREP_SCRIPTS = [
     21                     [TEST_IMPORTER]
     22                ]
     23 
     24 COMPLETE_FAILURES = os.path.join(THIS_DIR, 'complete_failures.py')
     25 PASSING_EXPERIMENTAL = os.path.join(THIS_DIR, 'passing_experimental.py')
     26 # Scripts ran to do the analysis.
     27 ANALYSIS_SCRIPTS = [
     28                         [COMPLETE_FAILURES],
     29                         [PASSING_EXPERIMENTAL]
     30                    ]
     31 
     32 
     33 def run_prep_scripts(scripts):
     34     """
     35     Run the scripts that are required to be ran before the analysis.
     36 
     37     This stops and returns False at the first script failure.
     38 
     39     @param scripts: A list of lists. Where the inner list is the script name
     40         and arguments to be called (as subprocess.call() expects).
     41 
     42     @return True if all the scripts succeeded and False otherwise.
     43 
     44     """
     45 
     46     for script in scripts:
     47         logging.info('Running %s', ' '.join(script))
     48         return_code = subprocess.call(script)
     49         if return_code != 0:
     50             logging.error('\'%s\' failed with return code %d',
     51                           (' '.join(script), return_code))
     52             return False
     53 
     54     return True
     55 
     56 
     57 def run_analysis_scripts(scripts):
     58     """
     59     Run the scripts that analyze the database.
     60 
     61     All scripts are ran even if one fails.
     62 
     63     @param scripts: A list of lists, where the inner list is the script name
     64         and arguments to be called (as subprocess.call() expects).
     65 
     66     @return True if all the scripts succeeded and False otherwise.
     67 
     68     """
     69 
     70     success = True
     71 
     72     for script in scripts:
     73         logging.info('Running %s', ' '.join(script))
     74         return_code = subprocess.call(script)
     75         if return_code != 0:
     76             logging.error('\'%s\' failed with return code %d',
     77                           (' '.join(script), return_code))
     78             success = False
     79 
     80     return success
     81 
     82 
     83 def parse_options(args):
     84     """Parse the command line options."""
     85 
     86     description = ('Runs test health and preparation scripts.')
     87     parser = argparse.ArgumentParser(description=description)
     88     parser.parse_args(args)
     89 
     90 
     91 def main(args=None):
     92     """
     93     The main function.
     94 
     95     This allows us to test this function by calling it in the unit test file.
     96 
     97     @param args: The command line arguments being passed in.
     98 
     99     @return 0 if everything succeeded and a non-zero integer otherwise.
    100 
    101     """
    102     args = [] if args is None else args
    103     parse_options(args)
    104 
    105     logging.getLogger().setLevel(logging.INFO)
    106 
    107     prep_success = run_prep_scripts(PREP_SCRIPTS)
    108     if not prep_success:
    109         return 1
    110 
    111     analysis_success = run_analysis_scripts(ANALYSIS_SCRIPTS)
    112     if not analysis_success:
    113         return 1
    114 
    115     return 0
    116 
    117 
    118 if __name__ == '__main__':
    119     sys.exit(main(sys.argv[1:]))
    120