mirror of
https://github.com/kennethreitz-archive/python-build.git
synced 2026-06-05 23:30:18 +00:00
release 0.9
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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_using_python_pkgname, Link, is_installed
|
||||
from pythonbrew.log import logger
|
||||
from pythonbrew.downloader import Downloader
|
||||
|
||||
class BuildoutCommand(Command):
|
||||
name = "buildout"
|
||||
usage = "%prog"
|
||||
summary = "Runs the buildout with specified or current using python"
|
||||
|
||||
def __init__(self):
|
||||
super(BuildoutCommand, self).__init__()
|
||||
self.parser.add_option(
|
||||
"-p", "--python",
|
||||
dest="python",
|
||||
default=None,
|
||||
help="Use the specified version of python.",
|
||||
metavar='VERSION'
|
||||
)
|
||||
|
||||
def run_command(self, options, args):
|
||||
if options.python:
|
||||
pkgname = Package(options.python).name
|
||||
else:
|
||||
pkgname = get_using_python_pkgname()
|
||||
if not is_installed(pkgname):
|
||||
logger.info('%s is not installed.' % pkgname)
|
||||
sys.exit(1)
|
||||
logger.info('Using %s' % pkgname)
|
||||
|
||||
# build a path
|
||||
python = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python')
|
||||
|
||||
# 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'])
|
||||
|
||||
BuildoutCommand()
|
||||
@@ -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_python_path
|
||||
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_python_path()
|
||||
cur = get_using_python_pkgname()
|
||||
for d in sorted(os.listdir(PATH_PYTHONS)):
|
||||
if cur and os.path.samefile(cur, os.path.join(PATH_PYTHONS, d, 'bin','python')):
|
||||
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')
|
||||
|
||||
@@ -2,7 +2,7 @@ import os
|
||||
import sys
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PATH_PYTHONS, PATH_BIN
|
||||
from pythonbrew.util import Package, set_current_path
|
||||
from pythonbrew.util import Package, set_current_path, is_installed
|
||||
from pythonbrew.log import logger
|
||||
|
||||
class SwitchCommand(Command):
|
||||
@@ -16,11 +16,10 @@ class SwitchCommand(Command):
|
||||
sys.exit(1)
|
||||
pkg = Package(args[0])
|
||||
pkgname = pkg.name
|
||||
pkgdir = os.path.join(PATH_PYTHONS, pkgname)
|
||||
if not os.path.isdir(pkgdir):
|
||||
if not is_installed(pkgname):
|
||||
logger.info("`%s` is not installed." % pkgname)
|
||||
sys.exit(1)
|
||||
pkgbin = os.path.join(pkgdir,'bin')
|
||||
pkgbin = os.path.join(PATH_PYTHONS,pkgname,'bin')
|
||||
|
||||
set_current_path('%s:%s' % (PATH_BIN, pkgbin))
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
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_python_path, unlink
|
||||
from pythonbrew.define import PATH_PYTHONS, PATH_BIN, PATH_VENVS
|
||||
from pythonbrew.util import off, rm_r, Package, get_using_python_pkgname, unlink,\
|
||||
is_installed
|
||||
from pythonbrew.log import logger
|
||||
|
||||
class UninstallCommand(Command):
|
||||
@@ -16,10 +17,11 @@ class UninstallCommand(Command):
|
||||
pkg = Package(arg)
|
||||
pkgname = pkg.name
|
||||
pkgpath = os.path.join(PATH_PYTHONS, pkgname)
|
||||
if not os.path.isdir(pkgpath):
|
||||
venvpath = os.path.join(PATH_VENVS, pkgname)
|
||||
if not is_installed(pkgname):
|
||||
logger.info("`%s` is not installed." % pkgname)
|
||||
continue
|
||||
if get_current_python_path() == os.path.join(pkgpath,'bin','python'):
|
||||
if get_using_python_pkgname() == pkgname:
|
||||
off()
|
||||
for d in os.listdir(PATH_BIN):
|
||||
# remove symlink
|
||||
@@ -30,6 +32,7 @@ class UninstallCommand(Command):
|
||||
if os.path.isfile(tgtpath) and os.path.samefile(path, tgtpath):
|
||||
unlink(path)
|
||||
rm_r(pkgpath)
|
||||
rm_r(venvpath)
|
||||
else:
|
||||
self.parser.print_help()
|
||||
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
import os
|
||||
import sys
|
||||
from pythonbrew.basecommand import Command
|
||||
from pythonbrew.define import PATH_PYTHONS, PATH_VENVS, PATH_ETC_VENV
|
||||
from pythonbrew.util import Subprocess, Package,\
|
||||
is_installed, get_installed_pythons_pkgname, get_using_python_pkgname
|
||||
from pythonbrew.log import logger
|
||||
|
||||
class VenvCommand(Command):
|
||||
name = "venv"
|
||||
usage = "%prog [create|use|delete|list] [project]"
|
||||
summary = "Create isolated python environments"
|
||||
|
||||
def __init__(self):
|
||||
super(VenvCommand, self).__init__()
|
||||
self.parser.add_option(
|
||||
"-p", "--python",
|
||||
dest="python",
|
||||
default=None,
|
||||
help="Use the specified version of python.",
|
||||
metavar='VERSION'
|
||||
)
|
||||
self.parser.add_option(
|
||||
"-a", "--all",
|
||||
dest="all",
|
||||
action='store_true',
|
||||
default=False,
|
||||
help="Show the all python environments.",
|
||||
metavar='VERSION'
|
||||
)
|
||||
self.template_env = """export VIRTUALENVWRAPPER_PYTHON=%(venv_py)s
|
||||
export VIRTUALENVWRAPPER_VIRTUALENV=%(venv_venv)s
|
||||
export WORKON_HOME=%(workon_home)s
|
||||
export VIRTUALENVWRAPPER_HOOK_DIR=%(workon_home)s
|
||||
export VIRTUALENVWRAPPER_LOG_DIR=%(workon_home)s
|
||||
source %(venv_sh)s
|
||||
"""
|
||||
|
||||
def run_command(self, options, args):
|
||||
if not args:
|
||||
logger.error('Unrecognized command line argument: ( see: \'pythonbrew help venv\' )')
|
||||
sys.exit(1)
|
||||
cmd = args[0]
|
||||
if not cmd in ('create', 'delete', 'use', 'list'):
|
||||
logger.error('Unrecognized command line argument: ( see: \'pythonbrew help venv\' )')
|
||||
sys.exit(1)
|
||||
|
||||
# find python2
|
||||
venv_pkgname = None
|
||||
for pkgname in reversed(get_installed_pythons_pkgname()):
|
||||
# virtualenvwrapper require Python2
|
||||
venv_pkgver = Package(pkgname).version
|
||||
if venv_pkgver >= '2.4' and venv_pkgver < '3':
|
||||
venv_pkgname = pkgname
|
||||
break
|
||||
if not venv_pkgname:
|
||||
logger.error('Can not create virtual environment before installing a python2. Try \'pythonbrew install <python2.4 - 2.7>\'.')
|
||||
sys.exit(1)
|
||||
venv_dir = os.path.join(PATH_PYTHONS, venv_pkgname)
|
||||
venv_bin = os.path.join(venv_dir, 'bin')
|
||||
|
||||
# target python interpreter
|
||||
if options.python:
|
||||
pkgname = Package(options.python).name
|
||||
if not is_installed(pkgname):
|
||||
logger.error('%s is not installed.' % pkgname)
|
||||
sys.exit(1)
|
||||
else:
|
||||
pkgname = get_using_python_pkgname()
|
||||
if not pkgname:
|
||||
logger.error('Can not use venv command before using a python. Try \'pythonbrew switch <some python>\'.')
|
||||
sys.exit(1)
|
||||
self._pkgname = pkgname
|
||||
self._target_py = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python')
|
||||
self._workon_home = os.path.join(PATH_VENVS, pkgname)
|
||||
self._venv_py = os.path.join(venv_bin, 'python')
|
||||
self._venv_venv = os.path.join(venv_bin, 'virtualenv')
|
||||
self._venv_sh = os.path.join(venv_bin, 'virtualenvwrapper.sh')
|
||||
|
||||
# has virtualenv & virtualenvwrapper?
|
||||
if not self._venv_venv or not self._venv_sh:
|
||||
logger.info('Installing virtualenv into %s' % venv_dir)
|
||||
s = Subprocess(verbose=True)
|
||||
s.shell('%s %s %s' % (os.path.join(venv_bin,'pip'), 'install', 'virtualenvwrapper'))
|
||||
|
||||
# Create a shell script
|
||||
try:
|
||||
self.__getattribute__('run_command_%s' % cmd)(options, args)
|
||||
except:
|
||||
logger.error('`%s` command not found.' % cmd)
|
||||
sys.exit(1)
|
||||
|
||||
def run_command_create(self, options, args):
|
||||
output = [self.template_env % {'venv_py': self._venv_py,
|
||||
'venv_venv': self._venv_venv,
|
||||
'workon_home': self._workon_home,
|
||||
'venv_sh': self._venv_sh}]
|
||||
for arg in args[1:]:
|
||||
output.append("""echo '# Create `%(arg)s` environment into %(workon_home)s'
|
||||
mkvirtualenv -p '%(target_py)s' '%(arg)s'
|
||||
""" % {'arg': arg,
|
||||
'workon_home': self._workon_home,
|
||||
'target_py': self._target_py})
|
||||
self._write(''.join(output))
|
||||
|
||||
def run_command_delete(self, options, args):
|
||||
output = [self.template_env % {'venv_py': self._venv_py,
|
||||
'venv_venv': self._venv_venv,
|
||||
'workon_home': self._workon_home,
|
||||
'venv_sh': self._venv_sh}]
|
||||
for arg in args[1:]:
|
||||
output.append("""echo '# Delete `%(arg)s` environment in %(workon_home)s'
|
||||
rmvirtualenv '%(arg)s'
|
||||
""" % {'arg': arg,
|
||||
'workon_home': self._workon_home})
|
||||
self._write(''.join(output))
|
||||
|
||||
def run_command_use(self, options, args):
|
||||
if len(args) < 2:
|
||||
logger.error("Unrecognized command line argument: ( 'pythonbrew venv use <project>' )")
|
||||
sys.exit(1)
|
||||
template = self.template_env + """echo '# Using `%(arg)s` environment (found in %(workon_home)s)'
|
||||
echo '# To leave an environment, simply run `deactivate`'
|
||||
workon '%(arg)s'
|
||||
"""
|
||||
self._write(template % {'venv_py': self._venv_py,
|
||||
'venv_venv': self._venv_venv,
|
||||
'workon_home': self._workon_home,
|
||||
'venv_sh': self._venv_sh,
|
||||
'arg': args[1]})
|
||||
|
||||
def run_command_list(self, options, args):
|
||||
template = self.template_env + """echo '# virtualenv for %(pkgname)s (found in %(workon_home)s)'
|
||||
workon
|
||||
"""
|
||||
if options.all:
|
||||
output = []
|
||||
for pkgname in get_installed_pythons_pkgname():
|
||||
workon_home = os.path.join(PATH_VENVS, pkgname)
|
||||
output.append(template % {'venv_py': self._venv_py,
|
||||
'venv_venv': self._venv_venv,
|
||||
'workon_home': workon_home,
|
||||
'venv_sh': self._venv_sh,
|
||||
'pkgname': pkgname})
|
||||
self._write(''.join(output))
|
||||
else:
|
||||
self._write(template % {'venv_py': self._venv_py,
|
||||
'venv_venv': self._venv_venv,
|
||||
'workon_home': self._workon_home,
|
||||
'venv_sh': self._venv_sh,
|
||||
'pkgname': self._pkgname})
|
||||
|
||||
def _write(self, src):
|
||||
fp = open(PATH_ETC_VENV, 'w')
|
||||
fp.write(src)
|
||||
fp.close()
|
||||
|
||||
VenvCommand()
|
||||
Reference in New Issue
Block a user