Skip to content

Commit 219ad17

Browse files
committed
update
1 parent b039caf commit 219ad17

18 files changed

Lines changed: 157 additions & 36 deletions

pythonbrew/commands/buildout.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
from pythonbrew.basecommand import Command
55
from pythonbrew.define import PATH_PYTHONS, BOOTSTRAP_DLSITE, PATH_DISTS
6-
from pythonbrew.util import Package, get_current_python_path, Link
6+
from pythonbrew.util import Package, get_current_use_pkgname, Link, is_installed
77
from pythonbrew.log import logger
88
from pythonbrew.downloader import Downloader
99

@@ -21,18 +21,19 @@ def __init__(self):
2121
help="Use the specified version of python.",
2222
metavar='VERSION'
2323
)
24-
self.parser.disable_interspersed_args()
2524

2625
def run_command(self, options, args):
2726
if options.python:
28-
python = Package(options.python).name
29-
python = os.path.join(PATH_PYTHONS, python, 'bin', 'python')
30-
if not os.path.isfile(python):
31-
logger.info('%s is not installed.' % options.python)
32-
sys.exit(1)
27+
pkgname = Package(options.python).name
3328
else:
34-
python = get_current_python_path()
35-
logger.info('Using %s' % python)
29+
pkgname = get_current_use_pkgname()
30+
if not is_installed(pkgname):
31+
logger.info('%s is not installed.' % pkgname)
32+
sys.exit(1)
33+
logger.info('Using %s' % pkgname)
34+
35+
# build a path
36+
python = os.path.join(PATH_PYTHONS, pkgname, 'bin', 'python')
3637

3738
# Download bootstrap.py
3839
download_url = BOOTSTRAP_DLSITE

pythonbrew/commands/list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pythonbrew.basecommand import Command
44
from pythonbrew.define import PYTHON_VERSION_URL, LATEST_VERSIONS_OF_PYTHON,\
55
PATH_PYTHONS
6-
from pythonbrew.util import Package, get_current_python_path
6+
from pythonbrew.util import Package, get_current_use_pkgname
77
from pythonbrew.log import logger
88

99
class ListCommand(Command):
@@ -36,9 +36,9 @@ def run_command(self, options, args):
3636

3737
def installed(self, options, args):
3838
logger.info('# installed pythons')
39-
cur = get_current_python_path()
39+
cur = get_current_use_pkgname()
4040
for d in sorted(os.listdir(PATH_PYTHONS)):
41-
if cur and os.path.samefile(cur, os.path.join(PATH_PYTHONS, d, 'bin','python')):
41+
if cur and cur == d:
4242
logger.info('%s (*)' % d)
4343
cur = None
4444
else:

pythonbrew/commands/switch.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from pythonbrew.basecommand import Command
44
from pythonbrew.define import PATH_PYTHONS, PATH_BIN
5-
from pythonbrew.util import Package, set_current_path
5+
from pythonbrew.util import Package, set_current_path, is_installed
66
from pythonbrew.log import logger
77

88
class SwitchCommand(Command):
@@ -16,11 +16,10 @@ def run_command(self, options, args):
1616
sys.exit(1)
1717
pkg = Package(args[0])
1818
pkgname = pkg.name
19-
pkgdir = os.path.join(PATH_PYTHONS, pkgname)
20-
if not os.path.isdir(pkgdir):
19+
if not is_installed(pkgname):
2120
logger.info("`%s` is not installed." % pkgname)
2221
sys.exit(1)
23-
pkgbin = os.path.join(pkgdir,'bin')
22+
pkgbin = os.path.join(PATH_PYTHONS,pkgname,'bin')
2423

2524
set_current_path('%s:%s' % (PATH_BIN, pkgbin))
2625

pythonbrew/commands/uninstall.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
from pythonbrew.basecommand import Command
33
from pythonbrew.define import PATH_PYTHONS, PATH_BIN
4-
from pythonbrew.util import off, rm_r, Package, get_current_python_path, unlink
4+
from pythonbrew.util import off, rm_r, Package, get_current_use_pkgname, unlink,\
5+
is_installed
56
from pythonbrew.log import logger
67

78
class UninstallCommand(Command):
@@ -16,10 +17,10 @@ def run_command(self, options, args):
1617
pkg = Package(arg)
1718
pkgname = pkg.name
1819
pkgpath = os.path.join(PATH_PYTHONS, pkgname)
19-
if not os.path.isdir(pkgpath):
20+
if not is_installed(pkgname):
2021
logger.info("`%s` is not installed." % pkgname)
2122
continue
22-
if get_current_python_path() == os.path.join(pkgpath,'bin','python'):
23+
if get_current_use_pkgname() == pkgname:
2324
off()
2425
for d in os.listdir(PATH_BIN):
2526
# remove symlink

pythonbrew/commands/venv.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import os
2+
import sys
3+
import subprocess
4+
from pythonbrew.basecommand import Command
5+
from pythonbrew.define import PATH_PYTHONS, BOOTSTRAP_DLSITE, PATH_DISTS
6+
from pythonbrew.util import Package, get_current_use_pkgname, Link
7+
from pythonbrew.log import logger
8+
from pythonbrew.downloader import Downloader
9+
10+
class VenvCommand(Command):
11+
name = "venv"
12+
usage = "%prog [create|use|delete|list] [project]"
13+
summary = "Create isolated python environments"
14+
15+
def __init__(self):
16+
super(VenvCommand, self).__init__()
17+
self.parser.add_option(
18+
"-p", "--python",
19+
dest="python",
20+
default=None,
21+
help="Use the specified version of python.",
22+
metavar='VERSION'
23+
)
24+
25+
def run_command(self, options, args):
26+
if not args:
27+
logger.error('Unrecognized command line argument: argument not found.')
28+
sys.exit(1)
29+
cmd = args[0]
30+
if not cmd in ('create', 'use', 'delete', 'list'):
31+
logger.error('%s command not found.' % cmd)
32+
sys.exit(1)
33+
34+
# Decide which version of python to use.
35+
if options.python:
36+
pkgname = Package(options.python).name
37+
else:
38+
pkgname = get_current_use_pkgname()
39+
logger.info('Using %s' % pkgname)
40+
41+
if cmd == 'create':
42+
self._create(args[1:])
43+
elif cmd == 'use':
44+
self._use(args[1])
45+
elif cmd == 'delete':
46+
self._delete(args[1:])
47+
elif cmd == 'list':
48+
self._list()
49+
50+
# Download bootstrap.py
51+
# download_url = BOOTSTRAP_DLSITE
52+
# filename = Link(download_url).filename
53+
# bootstrap = os.path.join(PATH_DISTS, filename)
54+
# try:
55+
# d = Downloader()
56+
# d.download(filename, download_url, bootstrap)
57+
# except:
58+
# logger.error("Failed to download. `%s`" % download_url)
59+
# sys.exit(1)
60+
#
61+
# # Using bootstrap.py
62+
# if subprocess.call([python, bootstrap, '-d']):
63+
# logger.error('Failed to bootstrap.')
64+
# sys.exit(1)
65+
#
66+
# # Using buildout
67+
# subprocess.call(['./bin/buildout'])
68+
69+
def _create(self, projects):
70+
"""Create python environment"""
71+
for proj in projects:
72+
print proj
73+
74+
def _use(self, project):
75+
print project
76+
77+
def _delete(self, projects):
78+
for proj in projects:
79+
print proj
80+
81+
def _list(self):
82+
print 'list'
83+
84+
VenvCommand()

pythonbrew/util.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import subprocess
1111
import shlex
1212
import select
13-
from pythonbrew.define import PATH_BIN, PATH_ETC_CURRENT
13+
from pythonbrew.define import PATH_BIN, PATH_ETC_CURRENT, PATH_PYTHONS
1414
from pythonbrew.exceptions import ShellCommandException
1515
from pythonbrew.log import logger
1616

@@ -94,12 +94,8 @@ def is_python32(version):
9494
return version >= '3.2' and version < '3.3'
9595

9696
def makedirs(path):
97-
try:
97+
if not os.path.exists(path):
9898
os.makedirs(path)
99-
except OSError:
100-
e = sys.exc_info()[1]
101-
if errno.EEXIST != e.errno:
102-
raise
10399

104100
def symlink(src, dst):
105101
try:
@@ -213,12 +209,22 @@ def extract_downloadfile(content_type, download_file, target_dir):
213209
return False
214210
return True
215211

216-
def get_current_python_path():
217-
"""return: python path or ''
218-
"""
212+
def get_current_use_pkgname():
213+
"""return: Python-<VERSION> or None"""
219214
p = subprocess.Popen('command -v python', stdout=subprocess.PIPE, shell=True)
220-
return to_str(p.communicate()[0].strip())
215+
path = to_str(p.communicate()[0].strip())
216+
for d in sorted(os.listdir(PATH_PYTHONS)):
217+
if path and os.path.samefile(path, os.path.join(PATH_PYTHONS, d, 'bin','python')):
218+
return d
219+
return None
221220

221+
def is_installed(name):
222+
pkgname = Package(name).name
223+
pkgdir = os.path.join(PATH_PYTHONS, pkgname)
224+
if not os.path.isdir(pkgdir):
225+
return False
226+
return True
227+
222228
def set_current_path(path):
223229
fp = open(PATH_ETC_CURRENT, 'w')
224230
fp.write('PATH_PYTHONBREW="%s"\n' % (path))

tests/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
import shutil
33

44
PYTHONBREW_ROOT = '/tmp/pythonbrew.test'
5-
TESTPY_FILE = '/tmp/pythonbrew_test.py'
65
TESTPY_VERSION = ['2.4.6', '2.5.5', '2.6.6', '3.2']
76
def cleanall():
87
if os.path.isdir(PYTHONBREW_ROOT):
98
shutil.rmtree(PYTHONBREW_ROOT)
10-
if os.path.isfile(TESTPY_FILE):
11-
os.remove(TESTPY_FILE)
129

1310
def setup():
1411
os.environ['PYTHONBREW_ROOT'] = PYTHONBREW_ROOT
1512
cleanall()
13+
from pythonbrew.installer import install_pythonbrew
14+
install_pythonbrew()
1615

1716
def teardown():
1817
cleanall()

tests/test_00_install_pythonbrew.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)