OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 from __future__ import print_function |
| 4 |
| 5 import argparse |
| 6 import os |
| 7 import subprocess |
| 8 import sys |
| 9 |
| 10 from tools import cov |
| 11 |
| 12 |
| 13 is_python3 = bool(sys.version_info.major == 3) |
| 14 has_python34 = False |
| 15 |
| 16 |
| 17 def call(*args, **kwargs): |
| 18 ret = subprocess.call(*args, **kwargs) |
| 19 if ret != 0: |
| 20 sys.exit(ret) |
| 21 |
| 22 |
| 23 def main(argv): |
| 24 parser = argparse.ArgumentParser() |
| 25 subps = parser.add_subparsers() |
| 26 |
| 27 subp = subps.add_parser('build', help='build the package') |
| 28 subp.set_defaults(func=run_build) |
| 29 |
| 30 subp = subps.add_parser('clean', help='Remove any local files.') |
| 31 subp.set_defaults(func=run_clean) |
| 32 |
| 33 subp = subps.add_parser('coverage', |
| 34 help='Run the tests and report code coverage.') |
| 35 subp.set_defaults(func=run_coverage) |
| 36 cov.add_arguments(subp) |
| 37 |
| 38 subp = subps.add_parser('develop', |
| 39 help='Install a symlinked package locally.') |
| 40 subp.set_defaults(func=run_develop) |
| 41 subp.add_argument('--system', action='store_true', |
| 42 help=('Install to the system site-package dir ' |
| 43 'rather than the user\'s (requires root).')) |
| 44 |
| 45 subp = subps.add_parser('format', |
| 46 help='Reformat the source code.') |
| 47 subp.set_defaults(func=run_format) |
| 48 |
| 49 subp = subps.add_parser('help', |
| 50 help='Get help on a subcommand.') |
| 51 subp.add_argument(nargs='?', action='store', dest='subcommand', |
| 52 help='The command to get help for.') |
| 53 subp.set_defaults(func=run_help) |
| 54 |
| 55 subp = subps.add_parser('install', |
| 56 help='build the package and install locally.') |
| 57 subp.set_defaults(func=run_install) |
| 58 subp.add_argument('--system', action='store_true', |
| 59 help=('Install to the system site-package dir ' |
| 60 'rather than the user\'s (requires root).')) |
| 61 |
| 62 subp = subps.add_parser('lint', |
| 63 help='run lint over the source') |
| 64 subp.set_defaults(func=run_lint) |
| 65 |
| 66 subp = subps.add_parser('tests', |
| 67 help='run the tests') |
| 68 subp.set_defaults(func=run_tests) |
| 69 |
| 70 args = parser.parse_args(argv) |
| 71 |
| 72 global has_python34 |
| 73 try: |
| 74 if subprocess.checkout(['python3', '--version']).startswith( |
| 75 'Python 3.4'): |
| 76 has_python34 = True |
| 77 except: |
| 78 pass |
| 79 args.func(args) |
| 80 |
| 81 |
| 82 def run_build(args): |
| 83 call([sys.executable, 'setup.py', 'build', '--quiet']) |
| 84 |
| 85 |
| 86 def run_clean(args): |
| 87 call(['git', 'clean', '-fxd']) |
| 88 |
| 89 |
| 90 def run_coverage(args): |
| 91 repo_dir = os.path.abspath(os.path.dirname(__file__)) |
| 92 path_to_cov = os.path.join(repo_dir, 'tools', 'cov.py') |
| 93 if not args.path: |
| 94 args.path = [repo_dir] |
| 95 if not args.source: |
| 96 args.source = [os.path.join(repo_dir, 'typ')] |
| 97 argv = cov.argv_from_args(args) |
| 98 cov_args = ['-m', 'typ', '-q', '-j', '1'] |
| 99 call(['python', path_to_cov] + argv + cov_args) |
| 100 if has_python34: |
| 101 call(['python3', path_to_cov] + argv + cov_args) |
| 102 |
| 103 |
| 104 def run_develop(args): |
| 105 call([sys.executable, 'setup.py', 'develop']) |
| 106 |
| 107 |
| 108 def run_format(args): |
| 109 call('autopep8 --in-place *.py */*.py */*/*.py', shell=True) |
| 110 |
| 111 |
| 112 def run_help(args): |
| 113 if args.subcommand: |
| 114 main([args.subcommand, '--help']) |
| 115 main(['--help']) |
| 116 |
| 117 |
| 118 def run_install(args): |
| 119 if args.system: |
| 120 argv = [] |
| 121 else: |
| 122 argv = ['--user'] |
| 123 call([sys.executable, 'setup.py', 'install'] + argv) |
| 124 |
| 125 |
| 126 def run_lint(args): |
| 127 call('pylint --rcfile=pylintrc */*.py */*/*.py', shell=True) |
| 128 call('pep8 *.py */*.py */*/*.py', shell=True) |
| 129 |
| 130 |
| 131 def run_tests(args): |
| 132 # Tests that we can run the command line directly if typ is in sys.path. |
| 133 call(['python', os.path.join('typ', 'cmdline.py'), '-q', |
| 134 'typ.tests.main_test.TestMain.test_basic']) |
| 135 |
| 136 # Test that we can run the command line directly if typ is not in sys.path. |
| 137 repo_dir = os.path.abspath(os.path.dirname(__file__)) |
| 138 home_dir = os.environ['HOME'] |
| 139 call(['python', os.path.join(repo_dir, 'typ', 'cmdline.py'), '-q', |
| 140 'typ.tests.main_test.TestMain.test_basic'], cwd=home_dir) |
| 141 |
| 142 # Now run all the tests under Python2 and Python3. |
| 143 call(['python', '-m', 'typ', '-q']) |
| 144 if has_python34: |
| 145 call(['python3', '-m', 'typ', '-q']) |
| 146 |
| 147 |
| 148 if __name__ == '__main__': |
| 149 sys.exit(main(sys.argv[1:])) |
OLD | NEW |