run-tests.py 5.24 KB
Newer Older
1
#!/usr/bin/env python
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# This file is part of postpic.
#
# postpic is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# postpic is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with postpic. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright Stephan Kuschel, 2014-2015

# run all tests and pep8 verification of this project.
# It is HIGHLY RECOMMENDED to link it as a git pre-commit hook!
# Please see pre-commit for instructions.

# THIS FILE MUST RUN WITHOUT ERROR ON EVERY COMMIT!

26
import sys
27
28
29
import os


30
31
32
33
34
35
36
37
def runcmd(cmd):
    '''
    run command cmd and exit if it fails.
    '''
    import subprocess
    print('=====  running next command =====')
    print('$ ' + cmd)
    exitstatus = subprocess.call(cmd, shell=True)
38
39
40
41
    if exitstatus == 0:
        print('OK')
    else:
        print('run-tests.py failed. aborting.')
42
43
        print('The failing command was:')
        print(cmd)
44
45
        exit(exitstatus)

46
47
48
49
50
51
def run_autopep8(args):
        import autopep8
        if autopep8.__version__ < '1.2':
            print('upgrade to autopep8 >= 1.2 (installed: {:})'.format(str(autopep8.__version__)))
            exit(1)
        autopep8mode = '--in-place' if args.autopep8 == 'fix' else '--diff'
52
53
        argv = ['autopep8', '-r', 'postpic', '--ignore-local-config', autopep8mode, \
                '--ignore=W391,E123,E226,E24' ,'--max-line-length=99']
54
        print('===== running autopep8 =====')
55
56
57
        print('autopep8 version: ' + autopep8.__version__)
        print('$ ' + ' '.join(argv))
        autopep8.main(argv)
58

59
def run_alltests(python='python', fast=False, skip_setup=False):
60
61
62
    '''
    runs all tests on postpic. This function has to exit without error on every commit!
    '''
Stephan Kuschel's avatar
Stephan Kuschel committed
63
    cmdrpl = dict(python=python)
64
    # make sure .pyx sources are up to date and compiled
65
    if not skip_setup:
Stephan Kuschel's avatar
Stephan Kuschel committed
66
67
        # should be the same as `./setup.py develop --user`
        runcmd('{python} -m pip install --user -e .'.format(**cmdrpl))
68

69
70
71
    # find pep8 or pycodestyle (its successor)
    try:
        import pep8
Stephan Kuschel's avatar
Stephan Kuschel committed
72
        cmdrpl['pycodestyle'] = 'pep8'
73
74
75
76
    except(ImportError):
        pass
    try:
        import pycodestyle
Stephan Kuschel's avatar
Stephan Kuschel committed
77
        cmdrpl['pycodestyle'] = 'pycodestyle'
78
79
    except(ImportError):
        pass
Stephan Kuschel's avatar
Stephan Kuschel committed
80
    if 'pycodestyle' not in cmdrpl:
81
82
        raise ImportError('Install pep8 or pycodestyle (its successor)')

83
84
    cmds = ['{python} -m pycodestyle --version',
            '{python} -m {pycodestyle} postpic --statistics --count --show-source '
Stephan Kuschel's avatar
Stephan Kuschel committed
85
86
87
88
89
90
            '--ignore=W391,E123,E226,E24,W504 --max-line-length=99',
            '{python} -m nose --exe']
    cmdo = ['{python} setup.py build_sphinx',
            '{python} ' + os.path.join('examples', 'simpleexample.py'),
            '{python} ' + os.path.join('examples', 'particleshapedemo.py'),
            '{python} ' + os.path.join('examples', 'time_cythonfunctions.py'),
91
92
            '{python} ' + os.path.join('examples', 'openPMD.py'),
            '{python} ' + os.path.join('examples', 'kspace-test-2d.py')]
93
94
    if not fast:
        cmds += cmdo
95
    for cmd in cmds:
Stephan Kuschel's avatar
Stephan Kuschel committed
96
        runcmd(cmd.format(**cmdrpl))
97

98
99
100
101
102
103
104
105
106
107
108
109
110
def main():
    import argparse
    parser = argparse.ArgumentParser(description='''
        Without arguments this runs all tests
        on the postpic codebase.
        This script MUST EXIT WITHOUT ERROR on EVERY commit!
        ''')
    parser.add_argument('--autopep8', default='', nargs='?', metavar='fix',
                        help='''
        run "autopep8" on the codebase.
        Use "--autopep8" to preview changes. To apply them, use
        "--autopep8 fix"
        ''')
111
112
    parser.add_argument('--fast', action='store_true', default=False,
                        help='Only run a subset of tests. Used for commit hook.')
113
114
    parser.add_argument('--skip-setup', action='store_true', default=False,
                        help='Skip the "setup.py develop --user" step.')
115
    pyversiongroup = parser.add_mutually_exclusive_group(required=False)
116
    pyversiongroup.add_argument('--pycmd', default='python{}'.format(sys.version_info.major),
Stephan Kuschel's avatar
Stephan Kuschel committed
117
                                help='use "PYCMD" as python interpreter for all subcommands. '
118
119
120
121
                                'default: "pythonX" where X is sys.version_info.major. '
                                'This means that by default the same version is used that '
                                'executes this script.'
                               )
122
    pyversiongroup.add_argument('-2', action='store_const', dest='pycmd', const='python2',
Stephan Kuschel's avatar
Stephan Kuschel committed
123
                                help='same as "--pycmd python2"')
124
    pyversiongroup.add_argument('-3', action='store_const', dest='pycmd', const='python3',
Stephan Kuschel's avatar
Stephan Kuschel committed
125
                                help='same as "--pycmd python3"')
126
127
128
129
130
131
    args = parser.parse_args()

    if args.autopep8 != '':
        run_autopep8(args)
        exit()

132
    run_alltests(args.pycmd, fast=args.fast, skip_setup=args.skip_setup)
133
134
135

if __name__ == '__main__':
    main()