1#!/usr/bin/env python
2
3from __future__ import print_function
4
5import argparse
6import os
7import subprocess
8import sys
9
10from tools import cov
11
12
13is_python3 = bool(sys.version_info.major == 3)
14has_python34 = False
15verbose = False
16repo_dir = os.path.abspath(os.path.dirname(__file__))
17path_to_cov = os.path.join(repo_dir, 'tools', 'cov.py')
18path_to_runner = os.path.join(repo_dir, 'typ', 'runner.py')
19
20
21def call(*args, **kwargs):
22    if verbose:
23        print(' '.join(args[0]))
24    ret = subprocess.call(*args, **kwargs)
25    if ret != 0:
26        sys.exit(ret)
27
28
29def main(argv):
30    parser = argparse.ArgumentParser(prog='run')
31    parser.add_argument('--no3', action='store_true',
32                        help='Do not run the tests under Python 3.')
33    parser.add_argument('-v', '--verbose', action='store_true')
34    subps = parser.add_subparsers()
35
36    subp = subps.add_parser('clean', help='Remove any local files.')
37    subp.set_defaults(func=run_clean)
38
39    subp = subps.add_parser('coverage',
40                            help='Run the tests and report code coverage.')
41    subp.set_defaults(func=run_coverage)
42    cov.add_arguments(subp)
43
44    subp = subps.add_parser('help',
45                            help='Get help on a subcommand.')
46    subp.add_argument(nargs='?', action='store', dest='subcommand',
47                      help='The command to get help for.')
48    subp.set_defaults(func=run_help)
49
50    subp = subps.add_parser('lint',
51                            help='run lint over the source')
52    subp.set_defaults(func=run_lint)
53
54    subp = subps.add_parser('tests',
55                            help='run the tests')
56    subp.set_defaults(func=run_tests)
57
58    args = parser.parse_args(argv)
59
60    global verbose
61    if args.verbose:
62        verbose = True
63    global has_python34
64    if not args.no3:
65        try:
66            ver = subprocess.check_output(['python3', '--version'])
67            has_python34 = ver.split()[1] >= '3.4'
68        except:
69            pass
70    args.func(args)
71
72
73def run_clean(args):
74    call(['git', 'clean', '-fxd'])
75
76
77def run_coverage(args):
78    if not args.path:
79        args.path = [repo_dir]
80    if not args.source:
81        args.source = [os.path.join(repo_dir, 'typ')]
82    argv = cov.argv_from_args(args)
83    cov_args = [path_to_runner, '-j', '1']
84    print('Running coverage of unit tests for Python 2.7.')
85    call(['python', path_to_cov] + argv + cov_args)
86    if has_python34:
87        print('Running coverage of unit tests for Python 3.4.')
88        call(['python3', path_to_cov] + argv + cov_args)
89
90
91def run_help(args):
92    if args.subcommand:
93        main([args.subcommand, '--help'])
94    main(['--help'])
95
96
97def run_lint(args):
98    call('pylint --rcfile=pylintrc */*.py */*/*.py', shell=True)
99
100
101def run_tests(args):
102    print('Testing running the typ module directly if it is in sys.path.')
103    call(['python', '-m', 'typ', 'typ.tests.main_test.TestMain.test_basic'])
104
105    print('Testing running the runner directly if nothing is in sys.path.')
106    home_dir = os.environ['HOME']
107    call(['python', path_to_runner, 'typ.tests.main_test.TestMain.test_basic'],
108         cwd=home_dir)
109
110    # Now run all the tests under Python2 and Python3.
111    print('Running the unit tests under Python 2.')
112    call(['python', path_to_runner])
113    if has_python34:
114        print('Running the unit tests under Python 3.4.')
115        call(['python3', path_to_runner])
116
117
118if __name__ == '__main__':
119    sys.exit(main(sys.argv[1:]))
120