Skip to content

Commit 7b2fa2f

Browse files
committed
Improve symlink command
1 parent 90face7 commit 7b2fa2f

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

pythonbrew/commands/symlink.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import sys
23
from 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
57
from pythonbrew.log import logger
68

79
class 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:

pythonbrew/commands/venv.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def run_command_create(self, options, args):
9696
if options.no_site_packages:
9797
virtualenv_options += '--no-site-packages'
9898

99-
print 'in create'
10099
output = []
101100
for arg in args[1:]:
102101
target_dir = os.path.join(self._workon_home, arg)

0 commit comments

Comments
 (0)