1#!/usr/bin/python -u
2"""
3Wrapper to run 'suite_scheduler.py --sanity' before uploading a patch.
4This script is invoked through PRESUBMIT.cfg from repohooks, and expects a
5list of commit files in the environment variable.
6"""
7
8import os, re, sys
9import common
10from autotest_lib.client.common_lib import utils
11
12
13def _commit_contains_ini_or_control():
14    """
15    Checks if commit contains suite_scheduler.ini or a control file.
16
17    @return: True if one of the files in the commit is suite_scheduler.ini.
18    """
19    file_list = os.environ.get('PRESUBMIT_FILES')
20    if file_list is None:
21        print 'Expected a list of presubmit files in the environment variable.'
22        sys.exit(1)
23
24    pattern = re.compile(r'.*files/suite_scheduler.ini$|.*/control(?:\.\w+)?$')
25    return any (pattern.search(file_path)
26                for file_path in file_list.split('\n'))
27
28
29def main():
30    """
31    Main function, invokes suite scheduler's sanity checker if the
32    commit contains either suite_scheduler.ini or a control file.
33    """
34    if _commit_contains_ini_or_control():
35        site_utils_dir = os.path.dirname(
36                             os.path.dirname(os.path.abspath(__file__)))
37        suite_scheduler = os.path.join(site_utils_dir,
38                                       'suite_scheduler/suite_scheduler.py')
39        output = utils.system_output(suite_scheduler + ' --sanity')
40        if output:
41            print output
42            sys.exit(1)
43
44
45if __name__ == '__main__':
46    main()
47