@@ -253,6 +253,21 @@ def get_backend_from_location(self, location):
253
253
type = 'str' ,
254
254
default = '' ,
255
255
help = optparse .SUPPRESS_HELP )
256
+ parser .add_option (
257
+ # Run only if inside a virtualenv, bail if not.
258
+ '--require-virtualenv' , '--require-venv' ,
259
+ dest = 'require_venv' ,
260
+ action = 'store_true' ,
261
+ default = False ,
262
+ help = optparse .SUPPRESS_HELP )
263
+ parser .add_option (
264
+ # Use automatically an activated virtualenv instead of installing
265
+ # globally. -E will be ignored if used.
266
+ '--respect-virtualenv' , '--respect-venv' ,
267
+ dest = 'respect_venv' ,
268
+ action = 'store_true' ,
269
+ default = False ,
270
+ help = optparse .SUPPRESS_HELP )
256
271
257
272
parser .add_option (
258
273
'-v' , '--verbose' ,
@@ -359,11 +374,34 @@ def main(self, complete_args, args, initial_options):
359
374
options , args = self .parser .parse_args (args )
360
375
self .merge_options (initial_options , options )
361
376
377
+ if options .require_venv and not options .venv :
378
+ # If a venv is required check if it can really be found
379
+ if not os .environ .get ('VIRTUAL_ENV' ):
380
+ print 'Could not find an activated virtualenv (required).'
381
+ sys .exit (3 )
382
+ # Automatically install in currently activated venv if required
383
+ options .respect_venv = True
384
+
362
385
if args and args [- 1 ] == '___VENV_RESTART___' :
363
386
## FIXME: We don't do anything this this value yet:
364
387
venv_location = args [- 2 ]
365
388
args = args [:- 2 ]
366
389
options .venv = None
390
+ else :
391
+ # If given the option to respect the activated environment
392
+ # check if no venv is given as a command line parameter
393
+ if options .respect_venv and os .environ .get ('VIRTUAL_ENV' ):
394
+ if options .venv and os .path .exists (options .venv ):
395
+ # Make sure command line venv and environmental are the same
396
+ if (os .path .realpath (os .path .expanduser (options .venv )) !=
397
+ os .path .realpath (os .environ .get ('VIRTUAL_ENV' ))):
398
+ print ("Given virtualenv (%s) doesn't match "
399
+ "currently activated virtualenv (%s)."
400
+ % (options .venv , os .environ .get ('VIRTUAL_ENV' )))
401
+ sys .exit (3 )
402
+ else :
403
+ options .venv = os .environ .get ('VIRTUAL_ENV' )
404
+ print 'Using already activated environment %s' % options .venv
367
405
level = 1 # Notify
368
406
level += options .verbose
369
407
level -= options .quiet
@@ -550,8 +588,7 @@ def __init__(self):
550
588
help = "Extra arguments to be supplied to the setup.py install "
551
589
"command (use like --install-option=\" --install-scripts=/usr/local/bin\" ). "
552
590
"Use multiple --install-option options to pass multiple options to setup.py install. "
553
- "If you are using an option with a directory path, be sure to use absolute path."
554
- )
591
+ "If you are using an option with a directory path, be sure to use absolute path." )
555
592
556
593
def run (self , options , args ):
557
594
if not options .build_dir :
0 commit comments