diff --git a/pythonbrew/commands/install.py b/pythonbrew/commands/install.py index b78ea5e..fb71d2f 100644 --- a/pythonbrew/commands/install.py +++ b/pythonbrew/commands/install.py @@ -14,21 +14,27 @@ class InstallCommand(Command): dest="force", action="store_true", default=False, - help="Force installation of a Python." + help="Force install of python.(skip make test)" ) self.parser.add_option( "-C", "--configure", dest="configure", default="", metavar="CONFIGURE_OPTIONS", - help="Custom configure options." + help="Options passed directly to configure." ) self.parser.add_option( "-n", "--no-setuptools", dest="no_setuptools", action="store_true", default=False, - help="Skip installation of setuptools." + help="Skip install of setuptools." + ) + self.parser.add_option( + "--as", + dest="alias", + default=None, + help="Install a python under an alias." ) def run_command(self, options, args): diff --git a/pythonbrew/commands/list.py b/pythonbrew/commands/list.py index e3a2e7b..fe9b39f 100644 --- a/pythonbrew/commands/list.py +++ b/pythonbrew/commands/list.py @@ -38,7 +38,7 @@ class ListCommand(Command): logger.info('# installed pythons') cur = get_current_python_path() for d in sorted(os.listdir(PATH_PYTHONS)): - if cur == os.path.join(PATH_PYTHONS, d, 'bin','python'): + if cur and os.path.samefile(cur, os.path.join(PATH_PYTHONS, d, 'bin','python')): logger.info('%s (*)' % d) cur = None else: diff --git a/pythonbrew/commands/symlink.py b/pythonbrew/commands/symlink.py new file mode 100644 index 0000000..a79c2bd --- /dev/null +++ b/pythonbrew/commands/symlink.py @@ -0,0 +1,44 @@ +import os +from pythonbrew.basecommand import Command +from pythonbrew.define import PATH_PYTHONS, PATH_BIN +from pythonbrew.util import Package, symlink, unlink + +class SymlinkCommand(Command): + name = "symlink" + usage = "%prog" + summary = "Create/Remove a symbolic link to python" + + def __init__(self): + super(SymlinkCommand, self).__init__() + self.parser.add_option( + "-p", "--python", + dest="pythons", + action="append", + default=[], + help="Using specified python versions." + ) + self.parser.add_option( + "-r", "--remove", + dest="remove", + action="store_true", + default=False, + help="Remove a symbolic link." + ) + + def run_command(self, options, args): + pythons = self._get_pythons(options.pythons) + for python in pythons: + version = Package(python).version + src = os.path.join(PATH_PYTHONS, python, 'bin', 'python') + dst = os.path.join(PATH_BIN, 'py%s' % (version)) + if options.remove: + unlink(dst) + else: + symlink(src, dst) + + def _get_pythons(self, _pythons): + pythons = [Package(p).name for p in _pythons] + return [d for d in sorted(os.listdir(PATH_PYTHONS)) + if not pythons or d in pythons] + +SymlinkCommand() diff --git a/pythonbrew/installer.py b/pythonbrew/installer.py index d2724b6..eb7715a 100644 --- a/pythonbrew/installer.py +++ b/pythonbrew/installer.py @@ -119,9 +119,9 @@ class PythonInstaller(object): if is_url(name): self.download_url = name filename = Link(self.download_url).filename - pkg = Package(filename) + pkg = Package(filename, options.alias) else: - pkg = Package(name) + pkg = Package(name, options.alias) self.download_url = get_python_version_url(pkg.version) if not self.download_url: logger.info("Unknown python version: `%s`" % pkg.name) @@ -166,7 +166,7 @@ class PythonInstaller(object): logger.info("Installed %(pkgname)s successfully. Run the following command to switch to %(pkgname)s." % {"pkgname":self.pkg.name}) logger.info("") - logger.info(" pythonbrew switch %s" % self.pkg.version) + logger.info(" pythonbrew switch %s" % self.pkg.alias) def ensure(self): if is_macosx_snowleopard(): diff --git a/pythonbrew/util.py b/pythonbrew/util.py index 4761384..956ea68 100644 --- a/pythonbrew/util.py +++ b/pythonbrew/util.py @@ -242,18 +242,22 @@ class Subprocess(object): raise ShellCommandException('Failed to `%s` command' % cmd) class Package(object): - def __init__(self, name): + def __init__(self, name, alias=None): self.name = None self.version = None + self.alias = None if is_archive_file(name): name = splitext(name)[0] m = re.search("^Python-(.*)$", name) if m: self.name = name - self.version = m.group(1) + self.version = self.alias = m.group(1) else: self.name = "Python-%s" % name - self.version = name + self.version = self.alias = name + if alias: + self.name = 'Python-%s' % alias + self.alias = alias class Link(object): def __init__(self, url): diff --git a/tests/test_04_install.py b/tests/test_04_install.py index 5f6b68f..b2ec31a 100644 --- a/tests/test_04_install.py +++ b/tests/test_04_install.py @@ -4,6 +4,7 @@ class InstallOptions(object): force = True configure = "" no_setuptools = False + alias = None def test_install(): from pythonbrew.commands.install import InstallCommand