OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 from __future__ import print_function | 3 from __future__ import print_function |
4 | 4 |
5 import argparse | 5 import argparse |
6 import os | 6 import os |
7 import subprocess | 7 import subprocess |
8 import sys | 8 import sys |
9 | 9 |
10 from tools import cov | 10 from tools import cov |
11 | 11 |
12 | 12 |
13 verbose = False | 13 class Runner(object): |
14 | 14 |
15 repo_dir = os.path.abspath(os.path.dirname(__file__)) | 15 def __init__(self): |
16 path_to_cov = os.path.join(repo_dir, 'tools', 'cov.py') | 16 self.verbose = False |
17 path_to_runner = os.path.join(repo_dir, 'typ', 'runner.py') | 17 self.repo_dir = os.path.abspath(os.path.dirname(__file__)) |
nednguyen
2016/09/09 15:28:53
should repo_dir, path_to_cov, path_to_runner, pyth
Dirk Pranke
2016/09/09 18:43:26
Sure, can do.
| |
18 self.path_to_cov = os.path.join(self.repo_dir, 'tools', 'cov.py') | |
19 self.path_to_runner = os.path.join(self.repo_dir, 'typ', 'runner.py') | |
20 self.python = sys.executable | |
18 | 21 |
22 def main(self, argv): | |
23 parser = argparse.ArgumentParser(prog='run') | |
24 parser.add_argument('-v', '--verbose', action='store_true') | |
25 subps = parser.add_subparsers() | |
19 | 26 |
20 def call(*args, **kwargs): | 27 subp = subps.add_parser('clean', help='Remove any local files.') |
21 if verbose: | 28 subp.set_defaults(func=self.run_clean) |
22 print(' '.join(args[0])) | |
23 ret = subprocess.call(*args, **kwargs) | |
24 if ret != 0: | |
25 sys.exit(ret) | |
26 | 29 |
30 subp = subps.add_parser('coverage', | |
31 help='Run the tests and report code coverage.') | |
32 subp.set_defaults(func=self.run_coverage) | |
33 cov.add_arguments(subp) | |
27 | 34 |
28 def main(argv): | 35 subp = subps.add_parser('help', |
29 parser = argparse.ArgumentParser(prog='run') | 36 help='Get help on a subcommand.') |
30 parser.add_argument('-v', '--verbose', action='store_true') | 37 subp.add_argument(nargs='?', action='store', dest='subcommand', |
31 subps = parser.add_subparsers() | 38 help='The command to get help for.') |
39 subp.set_defaults(func=self.run_help) | |
32 | 40 |
33 subp = subps.add_parser('clean', help='Remove any local files.') | 41 subp = subps.add_parser('lint', |
34 subp.set_defaults(func=run_clean) | 42 help='run lint over the source') |
43 subp.set_defaults(func=self.run_lint) | |
35 | 44 |
36 subp = subps.add_parser('coverage', | 45 subp = subps.add_parser('tests', |
37 help='Run the tests and report code coverage.') | 46 help='run the tests') |
38 subp.set_defaults(func=run_coverage) | 47 subp.set_defaults(func=self.run_tests) |
39 cov.add_arguments(subp) | |
40 | 48 |
41 subp = subps.add_parser('help', | 49 args = parser.parse_args(argv) |
42 help='Get help on a subcommand.') | |
43 subp.add_argument(nargs='?', action='store', dest='subcommand', | |
44 help='The command to get help for.') | |
45 subp.set_defaults(func=run_help) | |
46 | 50 |
47 subp = subps.add_parser('lint', | 51 self.verbose = args.verbose |
48 help='run lint over the source') | 52 args.func(args) |
49 subp.set_defaults(func=run_lint) | |
50 | 53 |
51 subp = subps.add_parser('tests', | 54 def call(self, *args, **kwargs): |
52 help='run the tests') | 55 if self.verbose: |
53 subp.set_defaults(func=run_tests) | 56 print(' '.join(args[0])) |
57 ret = subprocess.call(*args, **kwargs) | |
58 if ret != 0: | |
59 sys.exit(ret) | |
54 | 60 |
55 args = parser.parse_args(argv) | 61 def run_clean(self, args): |
62 self.call(['git', 'clean', '-fxd']) | |
56 | 63 |
57 global verbose | 64 def run_coverage(self, args): |
58 if args.verbose: | 65 if not args.path: |
59 verbose = True | 66 args.path = [self.repo_dir] |
60 args.func(args) | 67 if not args.source: |
68 args.source = [os.path.join(repo_dir, 'typ')] | |
69 argv = cov.argv_from_args(args) | |
70 cov_args = [path_to_runner, '-j', '1'] | |
71 self.call([self.python, self.path_to_cov] + argv + cov_args) | |
61 | 72 |
73 def run_help(self, args): | |
74 if args.subcommand: | |
75 self.main([args.subcommand, '--help']) | |
76 self.main(['--help']) | |
62 | 77 |
63 def run_clean(args): | 78 def run_lint(self, args): |
64 call(['git', 'clean', '-fxd']) | 79 self.call('pylint --rcfile=pylintrc */*.py */*/*.py', shell=True) |
65 | 80 |
81 def run_tests(self, args): | |
82 # Test running the typ module directly if it is in sys.path. | |
83 self.call([ | |
84 self.python, '-m', 'typ', | |
85 'typ.tests.main_test.TestMain.test_basic', | |
86 ]) | |
66 | 87 |
67 def run_coverage(args): | 88 # Testing running the runner directly if nothing is in sys.path. |
68 if not args.path: | 89 home_dir = os.environ['HOME'] |
69 args.path = [repo_dir] | 90 self.call([self.python, self.path_to_runner, |
70 if not args.source: | 91 'typ.tests.main_test.TestMain.test_basic'], cwd=home_dir) |
71 args.source = [os.path.join(repo_dir, 'typ')] | |
72 argv = cov.argv_from_args(args) | |
73 cov_args = [path_to_runner, '-j', '1'] | |
74 python = sys.executable | |
75 call([python, path_to_cov] + argv + cov_args) | |
76 | 92 |
77 | 93 # Run the remaining tests. |
78 def run_help(args): | 94 self.call([self.python, self.path_to_runner]) |
79 if args.subcommand: | |
80 main([args.subcommand, '--help']) | |
81 main(['--help']) | |
82 | |
83 | |
84 def run_lint(args): | |
85 call('pylint --rcfile=pylintrc */*.py */*/*.py', shell=True) | |
86 | |
87 | |
88 def run_tests(args): | |
89 python = sys.executable | |
90 # Test running the typ module directly if it is in sys.path. | |
91 call([python, '-m', 'typ', 'typ.tests.main_test.TestMain.test_basic']) | |
92 | |
93 # Testing running the runner directly if nothing is in sys.path.' | |
94 home_dir = os.environ['HOME'] | |
95 call([python, path_to_runner, 'typ.tests.main_test.TestMain.test_basic'], | |
96 cwd=home_dir) | |
97 | |
98 # Run the remaining tests. | |
99 call([python, path_to_runner]) | |
100 | 95 |
101 | 96 |
102 if __name__ == '__main__': | 97 if __name__ == '__main__': |
103 sys.exit(main(sys.argv[1:])) | 98 sys.exit(Runner().main(sys.argv[1:])) |
OLD | NEW |