1
1
import argparse
2
- import contextlib
3
2
import logging
4
3
import os .path
5
4
import sys
6
5
7
- from pyperformance import _utils , is_installed
8
- from pyperformance .venv import exec_in_virtualenv , cmd_venv
6
+ from pyperformance import _utils , is_installed , is_dev
7
+ from pyperformance .venv import cmd_venv
9
8
10
9
11
10
def comma_separated (values ):
@@ -136,19 +135,21 @@ def parse_args():
136
135
cmds .append (cmd )
137
136
138
137
# venv
139
- cmd = subparsers .add_parser ('venv' ,
138
+ venv_common = argparse .ArgumentParser (add_help = False )
139
+ venv_common .add_argument ("--venv" , help = "Path to the virtual environment" )
140
+ cmd = subparsers .add_parser ('venv' , parents = [venv_common ],
140
141
help = 'Actions on the virtual environment' )
141
142
cmd .set_defaults (venv_action = 'show' )
142
143
venvsubs = cmd .add_subparsers (dest = "venv_action" )
143
- cmd = venvsubs .add_parser ('show' )
144
+ cmd = venvsubs .add_parser ('show' , parents = [ venv_common ] )
144
145
cmds .append (cmd )
145
- cmd = venvsubs .add_parser ('create' )
146
+ cmd = venvsubs .add_parser ('create' , parents = [ venv_common ] )
146
147
filter_opts (cmd , allow_no_benchmarks = True )
147
148
cmds .append (cmd )
148
- cmd = venvsubs .add_parser ('recreate' )
149
+ cmd = venvsubs .add_parser ('recreate' , parents = [ venv_common ] )
149
150
filter_opts (cmd , allow_no_benchmarks = True )
150
151
cmds .append (cmd )
151
- cmd = venvsubs .add_parser ('remove' )
152
+ cmd = venvsubs .add_parser ('remove' , parents = [ venv_common ] )
152
153
cmds .append (cmd )
153
154
154
155
for cmd in cmds :
@@ -158,15 +159,9 @@ def parse_args():
158
159
"names that are inherited from the parent "
159
160
"environment when running benchmarking "
160
161
"subprocesses." ))
161
- cmd .add_argument ("--inside-venv" , action = "store_true" ,
162
- help = ("Option for internal usage only, don't use "
163
- "it directly. Notice that we are already "
164
- "inside the virtual environment." ))
165
162
cmd .add_argument ("-p" , "--python" ,
166
163
help = "Python executable (default: use running Python)" ,
167
164
default = sys .executable )
168
- cmd .add_argument ("--venv" ,
169
- help = "Path to the virtual environment" )
170
165
171
166
options = parser .parse_args ()
172
167
@@ -198,21 +193,6 @@ def parse_args():
198
193
return (parser , options )
199
194
200
195
201
- @contextlib .contextmanager
202
- def _might_need_venv (options ):
203
- try :
204
- if not is_installed ():
205
- # Always force a local checkout to be installed.
206
- assert not options .inside_venv
207
- raise ModuleNotFoundError
208
- yield
209
- except ModuleNotFoundError :
210
- if not options .inside_venv :
211
- print ('switching to a venv.' , flush = True )
212
- exec_in_virtualenv (options )
213
- raise # re-raise
214
-
215
-
216
196
def _manifest_from_options (options ):
217
197
from pyperformance import _manifest
218
198
return _manifest .load_manifest (options .manifest )
@@ -253,12 +233,18 @@ def _select_benchmarks(raw, manifest):
253
233
254
234
255
235
def _main ():
236
+ if not is_installed ():
237
+ # Always require a local checkout to be installed.
238
+ print ('ERROR: pyperformance should not be run without installing first' )
239
+ if is_dev ():
240
+ print ('(consider using the dev.py script)' )
241
+ sys .exit (1 )
242
+
256
243
parser , options = parse_args ()
257
244
258
245
if options .action == 'venv' :
259
246
if options .venv_action in ('create' , 'recreate' ):
260
- with _might_need_venv (options ):
261
- benchmarks = _benchmarks_from_options (options )
247
+ benchmarks = _benchmarks_from_options (options )
262
248
else :
263
249
benchmarks = None
264
250
cmd_venv (options , benchmarks )
@@ -280,23 +266,19 @@ def _main():
280
266
cmd_show (options )
281
267
sys .exit ()
282
268
elif options .action == 'run' :
283
- with _might_need_venv (options ):
284
- from pyperformance .cli_run import cmd_run
285
- benchmarks = _benchmarks_from_options (options )
269
+ from pyperformance .cli_run import cmd_run
270
+ benchmarks = _benchmarks_from_options (options )
286
271
cmd_run (options , benchmarks )
287
272
elif options .action == 'compare' :
288
- with _might_need_venv (options ):
289
- from pyperformance .compare import cmd_compare
273
+ from pyperformance .compare import cmd_compare
290
274
cmd_compare (options )
291
275
elif options .action == 'list' :
292
- with _might_need_venv (options ):
293
- from pyperformance .cli_run import cmd_list
294
- benchmarks = _benchmarks_from_options (options )
276
+ from pyperformance .cli_run import cmd_list
277
+ benchmarks = _benchmarks_from_options (options )
295
278
cmd_list (options , benchmarks )
296
279
elif options .action == 'list_groups' :
297
- with _might_need_venv (options ):
298
- from pyperformance .cli_run import cmd_list_groups
299
- manifest = _manifest_from_options (options )
280
+ from pyperformance .cli_run import cmd_list_groups
281
+ manifest = _manifest_from_options (options )
300
282
cmd_list_groups (manifest )
301
283
else :
302
284
parser .print_help ()
0 commit comments