diff --git a/pythonbrew/commands/install.py b/pythonbrew/commands/install.py index 19fa64c..edbb013 100644 --- a/pythonbrew/commands/install.py +++ b/pythonbrew/commands/install.py @@ -3,6 +3,8 @@ from pythonbrew.log import logger from pythonbrew.installer.pythoninstaller import PythonInstaller,\ PythonInstallerMacOSX from pythonbrew.util import is_macosx_snowleopard +from pythonbrew.exceptions import UnknownVersionException,\ + AlreadyInstalledException, NotSupportedVersionException class InstallCommand(Command): name = "install" @@ -48,12 +50,20 @@ class InstallCommand(Command): def run_command(self, options, args): if args: - # Install Python - if is_macosx_snowleopard(): - p = PythonInstallerMacOSX(args[0], options) - else: - p = PythonInstaller(args[0], options) - p.install() + # Install pythons + for arg in args: + try: + if is_macosx_snowleopard(): + p = PythonInstallerMacOSX(arg, options) + else: + p = PythonInstaller(arg, options) + p.install() + except UnknownVersionException: + continue + except AlreadyInstalledException: + continue + except NotSupportedVersionException: + continue else: logger.info("Unknown python version.") diff --git a/pythonbrew/commands/uninstall.py b/pythonbrew/commands/uninstall.py index 90a3562..9e6f40f 100644 --- a/pythonbrew/commands/uninstall.py +++ b/pythonbrew/commands/uninstall.py @@ -1,5 +1,4 @@ import os -import sys from pythonbrew.basecommand import Command from pythonbrew.define import PATH_PYTHONS from pythonbrew.util import off, rm_r, Package, get_current_python_path @@ -12,15 +11,17 @@ class UninstallCommand(Command): def run_command(self, options, args): if args: - pkg = Package(args[0]) - pkgname = pkg.name - pkgpath = os.path.join(PATH_PYTHONS, pkgname) - if not os.path.isdir(pkgpath): - logger.info("`%s` is not installed." % pkgname) - sys.exit(1) - if get_current_python_path() == os.path.join(pkgpath,'bin','python'): - off() - rm_r(pkgpath) + # Uninstall pythons + for arg in args: + pkg = Package(arg) + pkgname = pkg.name + pkgpath = os.path.join(PATH_PYTHONS, pkgname) + if not os.path.isdir(pkgpath): + logger.info("`%s` is not installed." % pkgname) + continue + if get_current_python_path() == os.path.join(pkgpath,'bin','python'): + off() + rm_r(pkgpath) else: self.parser.print_help() diff --git a/pythonbrew/exceptions.py b/pythonbrew/exceptions.py index e3ae0b8..fd6cee9 100644 --- a/pythonbrew/exceptions.py +++ b/pythonbrew/exceptions.py @@ -4,4 +4,11 @@ class BuildingException(Exception): class ShellCommandException(Exception): """General exception during shell command""" + +class UnknownVersionException(Exception): + """General exception during installing""" +class AlreadyInstalledException(Exception): + """General exception during installing""" +class NotSupportedVersionException(Exception): + """General exception during installing""" \ No newline at end of file diff --git a/pythonbrew/installer/pythoninstaller.py b/pythonbrew/installer/pythoninstaller.py index 9c6df3d..0edb764 100644 --- a/pythonbrew/installer/pythoninstaller.py +++ b/pythonbrew/installer/pythoninstaller.py @@ -14,6 +14,8 @@ from pythonbrew.define import PATH_BUILD, PATH_DISTS, PATH_PYTHONS,\ from pythonbrew.downloader import get_python_version_url, Downloader,\ get_headerinfo_from_url from pythonbrew.log import logger +from pythonbrew.exceptions import UnknownVersionException,\ + AlreadyInstalledException, NotSupportedVersionException class PythonInstaller(object): """Python installer @@ -38,7 +40,7 @@ class PythonInstaller(object): self.download_url = get_python_version_url(pkg.version) if not self.download_url: logger.info("Unknown python version: `%s`" % pkg.name) - sys.exit(1) + raise UnknownVersionException filename = Link(self.download_url).filename self.pkg = pkg self.install_dir = os.path.join(PATH_PYTHONS, pkg.name) @@ -57,7 +59,7 @@ class PythonInstaller(object): def install(self): if os.path.isdir(self.install_dir): logger.info("You are already installed `%s`" % self.pkg.name) - sys.exit() + raise AlreadyInstalledException self.download_unpack() logger.info("") logger.info("This could take a while. You can run the following command on another shell to track the status:") @@ -78,7 +80,6 @@ class PythonInstaller(object): self.install_setuptools() 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.alias) def download_unpack(self): @@ -185,7 +186,7 @@ class PythonInstallerMacOSX(PythonInstaller): # check for version if version < '2.6' and (version != '2.4.6' and version != '2.5.5'): logger.info("`%s` is not supported on MacOSX Snow Leopard" % self.pkg.name) - sys.exit() + raise NotSupportedVersionException # set configure options if is_python24(version): self.configure_options = '--with-universal-archs="intel" MACOSX_DEPLOYMENT_TARGET=10.6 CPPFLAGS="-D__DARWIN_UNIX03"' diff --git a/tests/__init__.py b/tests/__init__.py index c54c0ff..e6eeae9 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,7 +3,7 @@ import shutil PYTHONBREW_ROOT = '/tmp/pythonbrew.test' TESTPY_FILE = '/tmp/pythonbrew_test.py' -TESTPY_VERSION = ['2.4.6', '2.5.5', '2.6.6'] +TESTPY_VERSION = ['2.4.6', '2.5.5', '2.6.6', '3.2'] def cleanall(): if os.path.isdir(PYTHONBREW_ROOT): shutil.rmtree(PYTHONBREW_ROOT) diff --git a/tests/test_01_update.py b/tests/test_01_update.py index 8a4dfd5..99e0f5f 100644 --- a/tests/test_01_update.py +++ b/tests/test_01_update.py @@ -1,9 +1,10 @@ class UpdateOptions(object): head = False config = False + force = False def test_update(): from pythonbrew.commands.update import UpdateCommand c = UpdateCommand() c.run_command(UpdateOptions(), None) - \ No newline at end of file + diff --git a/tests/test_04_install.py b/tests/test_04_install.py index fd65732..f54e26f 100644 --- a/tests/test_04_install.py +++ b/tests/test_04_install.py @@ -9,6 +9,8 @@ class InstallOptions(object): def test_install(): from pythonbrew.commands.install import InstallCommand - for py_version in TESTPY_VERSION: - c = InstallCommand() - c.run_command(InstallOptions(), [py_version]) + py_version = TESTPY_VERSION.pop(0) + c = InstallCommand() + c.run_command(InstallOptions(), [py_version]) # pybrew install -f -j2 2.4.6 + c.run_command(InstallOptions(), TESTPY_VERSION) # pybrew install -f -j2 2.5.6 2.6.6 3.2 + diff --git a/tests/test_09_py.py b/tests/test_09_py.py index cd82640..9a62b78 100644 --- a/tests/test_09_py.py +++ b/tests/test_09_py.py @@ -6,7 +6,7 @@ class PyOptions(object): def _create_pyfile(): fp = open(TESTPY_FILE, 'w') - fp.write("print 'test'") + fp.write("print('test')") fp.close() def test_py():