11import os
2+ import sys
23from pythonbrew .basecommand import Command
3- from pythonbrew .define import PATH_PYTHONS , PATH_BIN
4- from pythonbrew .util import Package , symlink , unlink
4+ from pythonbrew .define import PATH_PYTHONS , PATH_BIN , PATH_VENVS
5+ from pythonbrew .util import Package , symlink , unlink , get_using_python_pkgname ,\
6+ is_installed
57from pythonbrew .log import logger
68
79class SymlinkCommand (Command ):
@@ -32,6 +34,12 @@ def __init__(self):
3234 default = None ,
3335 help = "Use as default the specified python version."
3436 )
37+ self .parser .add_option (
38+ "-v" , "--venv" ,
39+ dest = "venv" ,
40+ default = None ,
41+ help = "Use the virtual environment python."
42+ )
3543
3644 def run_command (self , options , args ):
3745 if options .default :
@@ -43,6 +51,28 @@ def run_command(self, options, args):
4351 self ._symlink (bin , bin , pkgname )
4452 else :
4553 self ._symlink ('python' , 'py' , pkgname )
54+ elif options .venv :
55+ if options .pythons :
56+ pkgname = Package (options .pythons [0 ]).name
57+ else :
58+ pkgname = get_using_python_pkgname ()
59+ if not is_installed (pkgname ):
60+ logger .error ('`%s` is not installed.' )
61+ sys .exit (1 )
62+
63+ venv_pkgdir = os .path .join (PATH_VENVS , pkgname )
64+ venv_dir = os .path .join (venv_pkgdir , options .venv )
65+ if not os .path .isdir (venv_dir ):
66+ logger .error ("`%s` environment not found in %s." % (options .venv , venv_pkgdir ))
67+ sys .exit (1 )
68+ pkg = Package (pkgname )
69+ if args :
70+ bin = args [0 ]
71+ dstbin = '%s%s-%s' % (bin , pkg .version , options .venv )
72+ self ._symlink (bin , dstbin , pkgname )
73+ else :
74+ dstbin = 'py%s-%s' % (pkg .version , options .venv )
75+ self ._symlink ('python' , dstbin , pkgname )
4676 else :
4777 pythons = self ._get_pythons (options .pythons )
4878 for pkgname in pythons :
0 commit comments