mbligh | 7c8ea99 | 2009-06-22 19:03:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 2 | |
| 3 | import os, sys |
| 4 | import unittest_suite |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import utils |
| 6 | |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 7 | |
mbligh | bd1eb20 | 2008-07-29 22:09:29 +0000 | [diff] [blame] | 8 | root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 9 | |
| 10 | |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 11 | invalid_dirs = ['client/tests/', 'client/site_tests/', 'tko/migrations/', |
| 12 | 'server/tests/', 'server/site_tests/', 'server/self-test/', |
| 13 | 'contrib/', 'utils/', 'ui/', 'frontend/migrations', |
| 14 | 'frontend/afe/simplejson/', 'metrics/', 'old_cli/', |
| 15 | 'client/common_lib/test_utils/', 'client/profilers/', |
| 16 | 'site_packages'] |
| 17 | # append site specific invalid_dirs list, if any |
| 18 | invalid_dirs.extend(utils.import_site_symbol( |
| 19 | __file__, 'autotest_lib.utils.site_coverage_suite', 'invalid_dirs', [])) |
| 20 | |
| 21 | |
| 22 | invalid_files = ['unittest_suite.py', 'coverage_suite.py', '__init__.py', |
| 23 | 'common.py'] |
| 24 | # append site specific invalid_files list, if any |
| 25 | invalid_files.extend(utils.import_site_symbol( |
| 26 | __file__, 'autotest_lib.utils.site_coverage_suite', 'invalid_files', [])) |
| 27 | |
| 28 | |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 29 | def is_valid_directory(dirpath): |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 30 | dirpath += '/' |
| 31 | for invalid_dir in invalid_dirs: |
| 32 | if dirpath.startswith(os.path.join(root, invalid_dir)): |
| 33 | return False |
| 34 | |
| 35 | return True |
| 36 | |
| 37 | |
| 38 | def is_test_filename(filename): |
| 39 | return (filename.endswith('_unittest.py') or filename.endswith('_test.py')) |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def is_valid_filename(f): |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 43 | # has to be a .py file |
| 44 | if not f.endswith('.py'): |
| 45 | return False |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 46 | |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 47 | # but there are exceptions |
| 48 | if is_test_filename(f): |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 49 | return False |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 50 | elif f in invalid_files: |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 51 | return False |
| 52 | else: |
| 53 | return True |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 54 | |
| 55 | |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 56 | def run_unittests(prog, dirname, files): |
| 57 | for f in files: |
| 58 | if is_test_filename(f): |
| 59 | testfile = os.path.abspath(os.path.join(dirname, f)) |
| 60 | cmd = "%s -x %s" % (prog, testfile) |
| 61 | utils.system_output(cmd, ignore_status=True, timeout=100) |
| 62 | |
| 63 | |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 64 | def main(): |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 65 | coverage = os.path.join(root, "contrib/coverage.py") |
mbligh | 35b3ae1 | 2008-07-01 01:49:22 +0000 | [diff] [blame] | 66 | |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 67 | # remove preceeding coverage data |
| 68 | cmd = "%s -e" % (coverage) |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 69 | os.system(cmd) |
mbligh | 35b3ae1 | 2008-07-01 01:49:22 +0000 | [diff] [blame] | 70 | |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 71 | # I know this looks weird but is required for accurate results |
| 72 | cmd = "cd %s && find . -name '*.pyc' | xargs rm" % root |
| 73 | os.system(cmd) |
mbligh | 35b3ae1 | 2008-07-01 01:49:22 +0000 | [diff] [blame] | 74 | |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 75 | # now walk through directory grabbing list of files |
| 76 | if len(sys.argv) == 2: |
| 77 | start = os.path.join(root, sys.argv[1]) |
| 78 | else: |
| 79 | start = root |
| 80 | |
| 81 | # run unittests through coverage analysis |
| 82 | os.path.walk(start, run_unittests, coverage) |
| 83 | |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 84 | module_strings = [] |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 85 | for dirpath, dirnames, files in os.walk(start): |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 86 | if is_valid_directory(dirpath): |
| 87 | for f in files: |
| 88 | if is_valid_filename(f): |
| 89 | temp = os.path.join(dirpath, f) |
| 90 | module_strings.append(temp) |
mbligh | 35b3ae1 | 2008-07-01 01:49:22 +0000 | [diff] [blame] | 91 | |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 92 | # analyze files |
| 93 | cmd = "%s -r -m %s" % (coverage, " ".join(module_strings)) |
showard | 39fef9c | 2009-12-10 21:43:07 +0000 | [diff] [blame] | 94 | os.system(cmd) |
mbligh | 9cd69d3 | 2008-06-26 23:29:33 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
mbligh | 7139727 | 2009-02-03 21:58:10 +0000 | [diff] [blame] | 98 | main() |