mirror of
https://github.com/kennethreitz-archive/python-build.git
synced 2026-06-05 23:30:18 +00:00
Added venv command
This commit is contained in:
@@ -3,7 +3,7 @@ import sys
|
||||
import subprocess
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PATH_PYTHONS, BOOTSTRAP_DLSITE, PATH_DISTS
|
||||
from pythonbrew.util import Package, get_current_use_pkgname, Link, is_installed
|
||||
from pythonbrew.util import Package, get_using_python_pkgname, Link, is_installed
|
||||
from pythonbrew.log import logger
|
||||
from pythonbrew.downloader import Downloader
|
||||
|
||||
@@ -26,7 +26,7 @@ class BuildoutCommand(Command):
|
||||
if options.python:
|
||||
pkgname = Package(options.python).name
|
||||
else:
|
||||
pkgname = get_current_use_pkgname()
|
||||
pkgname = get_using_python_pkgname()
|
||||
if not is_installed(pkgname):
|
||||
logger.info('%s is not installed.' % pkgname)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -3,7 +3,8 @@ import re
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PYTHON_VERSION_URL, LATEST_VERSIONS_OF_PYTHON,\
|
||||
PATH_PYTHONS
|
||||
from pythonbrew.util import Package, get_current_use_pkgname
|
||||
from pythonbrew.util import Package, get_using_python_pkgname,\
|
||||
get_using_python_path
|
||||
from pythonbrew.log import logger
|
||||
|
||||
class ListCommand(Command):
|
||||
@@ -36,15 +37,14 @@ class ListCommand(Command):
|
||||
|
||||
def installed(self, options, args):
|
||||
logger.info('# installed pythons')
|
||||
cur = get_current_use_pkgname()
|
||||
cur = get_using_python_pkgname()
|
||||
for d in sorted(os.listdir(PATH_PYTHONS)):
|
||||
if cur and cur == d:
|
||||
logger.info('%s (*)' % d)
|
||||
cur = None
|
||||
else:
|
||||
logger.info('%s' % d)
|
||||
if cur:
|
||||
logger.info('%s (*)' % cur)
|
||||
if not cur:
|
||||
logger.info('%s (*)' % get_using_python_path())
|
||||
|
||||
def available_install(self, options, args):
|
||||
logger.info('# available install pythons')
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PATH_PYTHONS, PATH_BIN
|
||||
from pythonbrew.util import off, rm_r, Package, get_current_use_pkgname, unlink,\
|
||||
from pythonbrew.util import off, rm_r, Package, get_using_python_pkgname, unlink,\
|
||||
is_installed
|
||||
from pythonbrew.log import logger
|
||||
|
||||
@@ -20,7 +20,7 @@ class UninstallCommand(Command):
|
||||
if not is_installed(pkgname):
|
||||
logger.info("`%s` is not installed." % pkgname)
|
||||
continue
|
||||
if get_current_use_pkgname() == pkgname:
|
||||
if get_using_python_pkgname() == pkgname:
|
||||
off()
|
||||
for d in os.listdir(PATH_BIN):
|
||||
# remove symlink
|
||||
|
||||
+42
-51
@@ -1,11 +1,10 @@
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PATH_PYTHONS, BOOTSTRAP_DLSITE, PATH_DISTS
|
||||
from pythonbrew.util import Package, get_current_use_pkgname, Link
|
||||
from pythonbrew.define import PATH_PYTHONS, PATH_VENVS, PATH_ETC_VENV
|
||||
from pythonbrew.util import get_using_python_pkgname, Subprocess, Package,\
|
||||
is_installed
|
||||
from pythonbrew.log import logger
|
||||
from pythonbrew.downloader import Downloader
|
||||
|
||||
class VenvCommand(Command):
|
||||
name = "venv"
|
||||
@@ -24,61 +23,53 @@ class VenvCommand(Command):
|
||||
|
||||
def run_command(self, options, args):
|
||||
if not args:
|
||||
logger.error('Unrecognized command line argument: argument not found.')
|
||||
sys.exit(1)
|
||||
logger.error('Unrecognized command line argument: ( see: \'pythonbrew help venv\' )')
|
||||
sys.exit(1)
|
||||
cmd = args[0]
|
||||
if not cmd in ('create', 'use', 'delete', 'list'):
|
||||
logger.error('%s command not found.' % cmd)
|
||||
logger.error('Unrecognized command line argument: ( see: \'pythonbrew help venv\' )')
|
||||
sys.exit(1)
|
||||
|
||||
# Decide which version of python to use.
|
||||
if options.python:
|
||||
pkgname = Package(options.python).name
|
||||
else:
|
||||
pkgname = get_current_use_pkgname()
|
||||
logger.info('Using %s' % pkgname)
|
||||
pkgname = get_using_python_pkgname()
|
||||
if not is_installed(pkgname):
|
||||
logger.error("%s is not installed." % pkgname)
|
||||
sys.exit(1)
|
||||
pkg_dir = os.path.join(PATH_PYTHONS, pkgname)
|
||||
pkg_bin_dir = os.path.join(pkg_dir, 'bin')
|
||||
|
||||
if cmd == 'create':
|
||||
self._create(args[1:])
|
||||
elif cmd == 'use':
|
||||
self._use(args[1])
|
||||
elif cmd == 'delete':
|
||||
self._delete(args[1:])
|
||||
self._pkg_bin_dir = pkg_bin_dir
|
||||
self._venv_dir = os.path.join(PATH_VENVS, pkgname)
|
||||
|
||||
# check python package name
|
||||
if not pkgname:
|
||||
logger.error('Can not create virtual environment before using a python. Try \'pythonbrew switch <some python>\'.')
|
||||
sys.exit(1)
|
||||
|
||||
# has virtualenv & virtualenvwrapper?
|
||||
if(not os.path.exists(os.path.join(pkg_bin_dir, 'virtualenvwrapper.sh')) or
|
||||
not os.path.exists(os.path.join(pkg_bin_dir, 'virtualenv'))):
|
||||
logger.info('Installing virtualenv into %s' % pkg_dir)
|
||||
s = Subprocess(verbose=True)
|
||||
s.shell('%s %s %s' % (os.path.join(pkg_bin_dir,'pip'), 'install', 'virtualenvwrapper'))
|
||||
|
||||
# Initialize virtualenv
|
||||
self._init()
|
||||
|
||||
# check
|
||||
if cmd == 'use':
|
||||
if len(args) < 2:
|
||||
logger.error("Unrecognized command line argument: ( 'pythonbrew venv use <project>' )")
|
||||
sys.exit(1)
|
||||
elif cmd == 'list':
|
||||
self._list()
|
||||
|
||||
# Download bootstrap.py
|
||||
# download_url = BOOTSTRAP_DLSITE
|
||||
# filename = Link(download_url).filename
|
||||
# bootstrap = os.path.join(PATH_DISTS, filename)
|
||||
# try:
|
||||
# d = Downloader()
|
||||
# d.download(filename, download_url, bootstrap)
|
||||
# except:
|
||||
# logger.error("Failed to download. `%s`" % download_url)
|
||||
# sys.exit(1)
|
||||
#
|
||||
# # Using bootstrap.py
|
||||
# if subprocess.call([python, bootstrap, '-d']):
|
||||
# logger.error('Failed to bootstrap.')
|
||||
# sys.exit(1)
|
||||
#
|
||||
# # Using buildout
|
||||
# subprocess.call(['./bin/buildout'])
|
||||
|
||||
def _create(self, projects):
|
||||
"""Create python environment"""
|
||||
for proj in projects:
|
||||
print proj
|
||||
|
||||
def _use(self, project):
|
||||
print project
|
||||
|
||||
def _delete(self, projects):
|
||||
for proj in projects:
|
||||
print proj
|
||||
|
||||
def _list(self):
|
||||
print 'list'
|
||||
logger.info('# virtualenv for %s (found in %s)' % (pkgname, self._venv_dir))
|
||||
|
||||
def _init(self):
|
||||
fp = open(PATH_ETC_VENV, 'w')
|
||||
fp.write("VIRTUALENVWRAPPER_PYTHON=%s\n" % os.path.join(self._pkg_bin_dir, 'python'))
|
||||
fp.write("WORKON_HOME=%s" % self._venv_dir)
|
||||
fp.close()
|
||||
|
||||
VenvCommand()
|
||||
|
||||
Reference in New Issue
Block a user