Debian Codegraph @

third_party/python/dlmanager/check.py

raw
1 #!/usr/bin/env python
2 """
3 Run flake8 checks and tests.
4 """
5
6 import os
7 import argparse
8 import pipes
9 import shutil
10 import tempfile
11
12 from subprocess import check_call
13
14
15 def parse_args():
16 parser = argparse.ArgumentParser()
17
18 parser.add_argument('-C', '--with-coverage', action='store_true',
19 help="Generate coverage data from the tests run")
20 parser.add_argument('-H', '--cover-html', action='store_true',
21 help='generate html files to see test coverage')
22 return parser.parse_args()
23
24
25 def run(cmd, **kwargs):
26 msg = 'Running: |%s|' % ' '.join(pipes.quote(c) for c in cmd)
27 if kwargs.get('cwd'):
28 msg += ' in %s' % kwargs['cwd']
29 print(msg)
30 check_call(cmd, **kwargs)
31
32
33 def rm(path):
34 if os.path.isfile(path):
35 os.unlink(path)
36 elif os.path.isdir(path):
37 shutil.rmtree(path)
38
39
40 if __name__ == '__main__':
41 options = parse_args()
42
43 here = os.path.dirname(os.path.abspath(__file__))
44 os.chdir(here)
45
46 run(['flake8', 'dlmanager', 'tests', 'setup.py', __file__])
47
48 if options.with_coverage:
49 rm('.coverage')
50 test_run_cmd = ['coverage', 'run']
51 else:
52 test_run_cmd = ['python']
53
54 tmpdir = tempfile.gettempdir()
55 tmpfiles = set(os.listdir(tmpdir))
56 run(test_run_cmd + ['setup.py', 'test'])
57
58 remaining_tmpfiles = tmpfiles - set(os.listdir(tmpdir))
59 assert not remaining_tmpfiles, "tests leaked some temp files: %s" % (
60 ", ".join("`%s`" % os.path.join(tmpdir, f) for f in remaining_tmpfiles)
61 )
62
63 if options.with_coverage and options.cover_html:
64 rm('htmlcov')
65 run(['coverage', 'html'])
66 print("See coverage: |firefox %s|"
67 % os.path.join(here, 'htmlcov', 'index.html'))