/
toxit.py
executable file
·150 lines (132 loc) · 5.39 KB
/
toxit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
"""
toxit.py - script to parse a tox.ini file in cwd and run the test commands
for a specified environment against the already-existing virtualenv (i.e. just
re-run only the test commands).
If you have ideas for improvements, or want the latest version, it's at:
<https://github.com/jantman/misc-scripts/blob/master/toxit.py>
Copyright 2016-2019 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
CHANGELOG:
2019-07-17 Jason Antman <jason@jasonantman.com>:
- BREAKING CHANGE: refactor to only support running one environment
at a time, but support specifying a single test script to run with py.test.
2017-03-22 Jason Antman <jason@jasonantman.com>:
- support passenv and setenv
- major internal refactor
2016-10-28 Jason Antman <jason@jasonantman.com>:
- ignore 'env' in commands
2016-07-05 Jason Antman <jason@jasonantman.com>:
- gut the whole script and use tox's own parseconfig()
2016-07-05 Jason Antman <jason@jasonantman.com>:
- bug fixes
2016-07-03 Jason Antman <jason@jasonantman.com>:
- initial version of script
"""
import sys
import os
import argparse
import logging
import subprocess
try:
from tox.config import parseconfig
except ImportError:
sys.stderr.write("ERROR: Could not import tox - is it installed?\n")
raise SystemExit(1)
FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger(__name__)
class ToxIt(object):
"""re-run tox commands against an existing environment"""
ignore_commands = [
['python', '--version'],
['virtualenv', '--version'],
['pip', '--version'],
['pip', 'freeze'],
['env']
]
def __init__(self):
self.env_config = self.parse_toxini()
def parse_toxini(self):
"""parse the tox ini, return dict of environments to list of commands"""
logger.debug('Calling tox.config.parseconfig()')
config = parseconfig(args=[])
logger.debug('Config parsed; envlist: %s', config.envlist)
env_config = {}
for envname in config.envlist:
bindir = os.path.join(
config.envconfigs[envname].envdir.strpath,
'bin'
)
env_config[envname] = {
'commands': [],
'passenv': [x for x in config.envconfigs[envname].passenv],
'setenv': {
a: config.envconfigs[envname].setenv.get(a) for a in
config.envconfigs[envname].setenv.keys()
}
}
for cmd in config.envconfigs[envname].commands:
if cmd in self.ignore_commands:
logger.debug('%s - skipping ignored command: %s',
envname, cmd)
continue
cmd[0] = os.path.join(bindir, cmd[0])
env_config[envname]['commands'].append(cmd)
logger.debug('env %s: %s', envname, env_config[envname])
return env_config
def run_env(self, env_config):
"""run a single env; return True (success) or False (failure)"""
for cmd in env_config['commands']:
logger.info('Running command: %s', cmd)
rcode = subprocess.call(cmd, env=self._make_env(env_config))
logger.info('Command exited %s', rcode)
if rcode != 0:
return False
return True
def _make_env(self, env_config):
e = {}
for n in env_config['passenv']:
if n in os.environ:
e[n] = os.environ[n]
for k, v in env_config['setenv'].items():
e[k] = v
return e
def run(self, envname, test_script=None):
"""run selected env"""
if test_script is not None:
for idx, cmd in enumerate(self.env_config[envname]['commands']):
if not cmd[0].endswith('py.test'):
continue
cmd[-1] = test_script
self.env_config[envname]['commands'][idx] = cmd
res = self.run_env(self.env_config[envname])
if not res:
print('Some commands failed.')
raise SystemExit(1)
print('All commands succeeded.')
raise SystemExit(0)
def parse_args(argv):
"""
parse arguments/options
this uses the new argparse module instead of optparse
see: <https://docs.python.org/2/library/argparse.html>
"""
p = argparse.ArgumentParser(description='Re-run tox test commands for a '
'given environment against the '
'already-existing and installed virtualenv.')
p.add_argument('-v', '--verbose', dest='verbose', action='store_true',
default=False,
help='verbose output')
p.add_argument('TOXENV', type=str, help='Tox environment name to run')
p.add_argument('TEST_FILE', type=str, nargs='?', default=None,
help='Optional - path to one test file to run in place of'
'running all tests (replaces last argument on the '
'pytest command line)')
return p.parse_args(argv)
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
if args.verbose:
logger.setLevel(logging.DEBUG)
script = ToxIt()
script.run(args.TOXENV, args.TEST_FILE)