11import argparse
2- import contextlib
32import logging
43import os .path
54import sys
65
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
98
109
1110def comma_separated (values ):
@@ -136,19 +135,21 @@ def parse_args():
136135 cmds .append (cmd )
137136
138137 # 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 ],
140141 help = 'Actions on the virtual environment' )
141142 cmd .set_defaults (venv_action = 'show' )
142143 venvsubs = cmd .add_subparsers (dest = "venv_action" )
143- cmd = venvsubs .add_parser ('show' )
144+ cmd = venvsubs .add_parser ('show' , parents = [ venv_common ] )
144145 cmds .append (cmd )
145- cmd = venvsubs .add_parser ('create' )
146+ cmd = venvsubs .add_parser ('create' , parents = [ venv_common ] )
146147 filter_opts (cmd , allow_no_benchmarks = True )
147148 cmds .append (cmd )
148- cmd = venvsubs .add_parser ('recreate' )
149+ cmd = venvsubs .add_parser ('recreate' , parents = [ venv_common ] )
149150 filter_opts (cmd , allow_no_benchmarks = True )
150151 cmds .append (cmd )
151- cmd = venvsubs .add_parser ('remove' )
152+ cmd = venvsubs .add_parser ('remove' , parents = [ venv_common ] )
152153 cmds .append (cmd )
153154
154155 for cmd in cmds :
@@ -158,15 +159,9 @@ def parse_args():
158159 "names that are inherited from the parent "
159160 "environment when running benchmarking "
160161 "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." ))
165162 cmd .add_argument ("-p" , "--python" ,
166163 help = "Python executable (default: use running Python)" ,
167164 default = sys .executable )
168- cmd .add_argument ("--venv" ,
169- help = "Path to the virtual environment" )
170165
171166 options = parser .parse_args ()
172167
@@ -198,21 +193,6 @@ def parse_args():
198193 return (parser , options )
199194
200195
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-
216196def _manifest_from_options (options ):
217197 from pyperformance import _manifest
218198 return _manifest .load_manifest (options .manifest )
@@ -253,12 +233,18 @@ def _select_benchmarks(raw, manifest):
253233
254234
255235def _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+
256243 parser , options = parse_args ()
257244
258245 if options .action == 'venv' :
259246 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 )
262248 else :
263249 benchmarks = None
264250 cmd_venv (options , benchmarks )
@@ -280,23 +266,19 @@ def _main():
280266 cmd_show (options )
281267 sys .exit ()
282268 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 )
286271 cmd_run (options , benchmarks )
287272 elif options .action == 'compare' :
288- with _might_need_venv (options ):
289- from pyperformance .compare import cmd_compare
273+ from pyperformance .compare import cmd_compare
290274 cmd_compare (options )
291275 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 )
295278 cmd_list (options , benchmarks )
296279 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 )
300282 cmd_list_groups (manifest )
301283 else :
302284 parser .print_help ()
0 commit comments