diff --git a/MANIFEST.in b/MANIFEST.in index 1c7ec3f..b506a9f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ recursive-include pythonbrew/patches * +recursive-include pythonbrew/scripts * include pythonbrew_install.py diff --git a/README.rst b/README.rst index aa1d003..8b9ad93 100644 --- a/README.rst +++ b/README.rst @@ -13,9 +13,7 @@ Following python version is required to use pythonbrew: The recommended way to download and install pythonbrew is to run these statements in your shell.:: - curl -LO http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install - chmod +x pythonbrew-install - ./pythonbrew-install + curl -kL http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install | bash After that, pythonbrew installs itself to ~/.pythonbrew, and you should follow the instruction on screen to setup your .bashrc or .cshrc to put it in your PATH. diff --git a/pythonbrew-install b/pythonbrew-install index ad06a95..b138d0c 100755 --- a/pythonbrew-install +++ b/pythonbrew-install @@ -32,11 +32,11 @@ parse_arguments() parse_arguments $@ if [[ ! -x $PYTHON ]] ; then - echo "Python (2.4, 2.5 or 2.6) is required." + echo "pythonbrew required Python (2.4, 2.5 or 2.6)." exit fi if [[ ! -x $CURL ]] ; then - echo "curl is required." + echo "pythonbrew required curl. curl was not found in your path." fi PYTHON_VERSION=`$PYTHON -V 2>&1` @@ -55,9 +55,8 @@ if [[ -n $PYTHONBREW_ROOT ]] ; then ROOT=$PYTHONBREW_ROOT fi PATH_DISTS="$ROOT/dists" -PATH_ETC="$ROOT/etc" -STABLE_VERSION=`curl -sL https://github.com/utahta/pythonbrew/raw/master/stable-version.txt` +STABLE_VERSION=`curl -skL https://github.com/utahta/pythonbrew/raw/master/stable-version.txt` TEMP_FILE="pythonbrew-$STABLE_VERSION" TEMP_TARBALL="$TEMP_FILE.tar.gz" DOWNLOAD_URL="http://pypi.python.org/packages/source/p/pythonbrew/$TEMP_TARBALL" @@ -67,7 +66,7 @@ rm -rf $PATH_DISTS/$TEMP_TARBALL rm -rf $PATH_DISTS/$TEMP_FILE echo "Downloading $DOWNLOAD_URL" -builtin cd $PATH_DISTS ; curl -sL $DOWNLOAD_URL -o "$TEMP_TARBALL" +builtin cd $PATH_DISTS ; curl --progress-bar -kL $DOWNLOAD_URL -o "$TEMP_TARBALL" echo "Extracting $PATH_DISTS/$TEMP_TARBALL" builtin cd $PATH_DISTS ; tar zxf $TEMP_TARBALL diff --git a/pythonbrew/commands/switch.py b/pythonbrew/commands/switch.py index 7273ca3..c5630bf 100644 --- a/pythonbrew/commands/switch.py +++ b/pythonbrew/commands/switch.py @@ -1,6 +1,5 @@ import os import sys -import re from pythonbrew.basecommand import Command from pythonbrew.define import PATH_PYTHONS, PATH_BIN from pythonbrew.util import off, symlink, Package @@ -13,13 +12,13 @@ class SwitchCommand(Command): def run_command(self, options, args): if not args: - logger.error("Unrecognized command line argument: argument not found.") + logger.info("Unrecognized command line argument: argument not found.") sys.exit(1) pkg = Package(args[0]) pkgname = pkg.name - pkgdir = "%s/%s" % (PATH_PYTHONS, pkgname) + pkgdir = os.path.join(PATH_PYTHONS, pkgname) if not os.path.isdir(pkgdir): - logger.error("`%s` is not installed." % pkgname) + logger.info("`%s` is not installed." % pkgname) sys.exit(1) self._switch_dir(pkgdir) logger.info("Switched to %s" % pkgname) @@ -27,15 +26,13 @@ class SwitchCommand(Command): def _switch_dir(self, pkgdir): off() symlink(pkgdir, "%s/current" % PATH_PYTHONS) - for root, dirs, files in os.walk("%s/current/bin/" % PATH_PYTHONS): - for f in files: - symlink("%s%s" % (root, f), "%s/%s" % (PATH_BIN, f)) - break - # I want better code - if not os.path.isfile("%s/python" % PATH_BIN): - if os.path.isfile("%s/python3" % PATH_BIN): - symlink(os.path.realpath("%s/python3" % PATH_BIN), "%s/python" % PATH_BIN) - elif os.path.isfile("%s/python3.0" % PATH_BIN): - symlink(os.path.realpath("%s/python3.0" % PATH_BIN), "%s/python" % PATH_BIN) + + # I want better code... + current_bin = os.path.join(PATH_PYTHONS,'current','bin') + if not os.path.isfile(os.path.join(current_bin,"python")): + if os.path.isfile(os.path.join(current_bin,"python3")): + symlink(os.path.realpath("%s/python3" % current_bin), os.path.join(PATH_BIN,"python")) + elif os.path.isfile(os.path.join(current_bin,"python3.0")): + symlink(os.path.realpath("%s/python3.0" % current_bin), os.path.join(PATH_BIN,"python")) SwitchCommand() diff --git a/pythonbrew/commands/update.py b/pythonbrew/commands/update.py index a025846..71f1da4 100644 --- a/pythonbrew/commands/update.py +++ b/pythonbrew/commands/update.py @@ -6,8 +6,7 @@ from pythonbrew.define import PATH_DISTS, VERSION, ROOT,\ from pythonbrew.log import logger from pythonbrew.downloader import Downloader, get_pythonbrew_update_url,\ get_response_from_url, get_stable_version -from pythonbrew.util import rm_r, unpack_downloadfile, Link, is_gzip -from pythonbrew.installer import PythonbrewInstaller +from pythonbrew.util import rm_r, unpack_downloadfile, Link, is_gzip, Subprocess class UpdateCommand(Command): name = "update" @@ -51,9 +50,9 @@ class UpdateCommand(Command): sys.exit(1) try: - installer_path = "%s/pythonbrew" % (extract_dir) logger.info("Installing %s into %s" % (extract_dir, ROOT)) - PythonbrewInstaller().install(installer_path) + s = Subprocess() + s.check_call('%s %s/pythonbrew_install.py --upgrade' % (sys.executable, extract_dir)) except: logger.error("Failed to update pythonbrew.") raise diff --git a/pythonbrew/curl.py b/pythonbrew/curl.py new file mode 100644 index 0000000..b2a6ca5 --- /dev/null +++ b/pythonbrew/curl.py @@ -0,0 +1,36 @@ +import sys +import subprocess +from subprocess import Popen, PIPE +from pythonbrew.log import logger + +class Curl(object): + def __init__(self): + returncode = subprocess.call("command -v curl > /dev/null", shell=True) + if returncode: + logger.info("pythonbrew required curl. curl was not found in your path.") + sys.exit(1) + + def read(self, url): + p = Popen("curl -skL %s" % url, stdout=PIPE, shell=True) + p.wait() + if p.returncode: + raise + return p.stdout.read() + + def readheader(self, url): + p = Popen("curl --head -skL %s" % url, stdout=PIPE, shell=True) + p.wait() + if p.returncode: + raise + respinfo = {} + for line in p.stdout: + line = line.strip().split(":", 1) + if len(line) == 2: + respinfo[line[0].strip().lower()] = line[1].strip() + return respinfo + + def fetch(self, url, filename): + p = Popen("curl -# -kL %s -o %s" % (url, filename), shell=True) + p.wait() + if p.returncode: + raise diff --git a/pythonbrew/define.py b/pythonbrew/define.py index eea26f9..6936fd3 100644 --- a/pythonbrew/define.py +++ b/pythonbrew/define.py @@ -1,6 +1,6 @@ import os -VERSION = "0.6.1" +VERSION = "0.6.2" if os.environ.has_key("PYTHONBREW_ROOT"): ROOT = os.environ["PYTHONBREW_ROOT"] diff --git a/pythonbrew/exceptions.py b/pythonbrew/exceptions.py index 89749ac..e3ae0b8 100644 --- a/pythonbrew/exceptions.py +++ b/pythonbrew/exceptions.py @@ -1,3 +1,7 @@ class BuildingException(Exception): """General exception during building""" + +class ShellCommandException(Exception): + """General exception during shell command""" + \ No newline at end of file diff --git a/pythonbrew/installer.py b/pythonbrew/installer.py index 3254851..752e033 100644 --- a/pythonbrew/installer.py +++ b/pythonbrew/installer.py @@ -52,6 +52,9 @@ The default help messages will popup and tell you what to do! Enjoy pythonbrew at %(ROOT)s!! """ % {'ROOT':ROOT, 'yourshrc':yourshrc, 'shrc':shrc, 'PATH_ETC':PATH_ETC}) +def upgrade_pythonbrew(): + PythonbrewInstaller().install(INSTALLER_ROOT) + class PythonbrewInstaller(object): def install(self, installer_root): makedirs(PATH_PYTHONS) @@ -88,7 +91,11 @@ if __name__ == "__main__": os.chmod(PATH_BIN_PYTHONBREW, 0755) symlink(PATH_BIN_PYTHONBREW, PATH_BIN_PYBREW) # pybrew is symbolic pythonbrew - os.system("echo 'export PATH=%s/bin:%s/current/bin:${PATH}' > %s/bashrc" % (ROOT, PATH_PYTHONS, PATH_ETC)) + fp = open(os.path.join(PATH_ETC,'bashrc'), 'w') + for line in open(os.path.join(installer_root,'scripts','bashrc')): + line = line.replace('@ROOT@', ROOT) + fp.write(line) + fp.close() os.system("echo 'setenv PATH %s/bin:%s/current/bin:$PATH' > %s/cshrc" % (ROOT, PATH_PYTHONS, PATH_ETC)) class PythonInstaller(object): diff --git a/pythonbrew/scripts/bashrc b/pythonbrew/scripts/bashrc new file mode 100644 index 0000000..f4f2ed6 --- /dev/null +++ b/pythonbrew/scripts/bashrc @@ -0,0 +1,20 @@ +PYTHONBREW_ROOT="@ROOT@" +PYTHONBREW_ETC="$PYTHONBREW_ROOT/etc" + +export PATH=$PYTHONBREW_ROOT/bin:$PYTHONBREW_ROOT/pythons/current/bin:${PATH} + +__pythonbrew_reload() +{ + if [[ -s "$PYTHONBREW_ETC/bashrc" ]] ; then + source "$PYTHONBREW_ETC/bashrc" + fi +} + +pythonbrew() +{ + command pythonbrew "$@" + case $1 in + update) __pythonbrew_reload ;; + esac + hash -r +} diff --git a/pythonbrew/util.py b/pythonbrew/util.py index 6877a96..62299fb 100644 --- a/pythonbrew/util.py +++ b/pythonbrew/util.py @@ -5,7 +5,7 @@ import subprocess import re import posixpath from pythonbrew.define import PATH_BIN, PATH_PYTHONS -from pythonbrew.exceptions import BuildingException +from pythonbrew.exceptions import ShellCommandException from pythonbrew.log import logger import tarfile import platform @@ -183,7 +183,7 @@ def unpack_downloadfile(content_type, download_file, target_dir): logger.error("Cannot determine archive format of %s" % download_file) return False return True - + class Subprocess(object): def __init__(self, log=None, shell=True, cwd=None, print_cmd=False): self._log = log @@ -205,7 +205,7 @@ class Subprocess(object): cmd = "(%s) >> '%s' 2>&1" % (cmd, self._log) retcode = subprocess.call(cmd, shell=self._shell, cwd=self._cwd) if retcode != 0: - raise BuildingException() + raise ShellCommandException('Failed to `%s` command' % cmd) class Package(object): def __init__(self, name): diff --git a/pythonbrew_install.py b/pythonbrew_install.py index e8508ed..0f78ffd 100644 --- a/pythonbrew_install.py +++ b/pythonbrew_install.py @@ -1,3 +1,16 @@ -from pythonbrew.installer import install_pythonbrew +from pythonbrew.installer import install_pythonbrew, upgrade_pythonbrew +from optparse import OptionParser if __name__ == "__main__": - install_pythonbrew() + parser = OptionParser() + parser.add_option( + '-U', '--upgrade', + dest="upgrade", + action="store_true", + default=False, + help="Upgrade." + ) + (opt, arg) = parser.parse_args() + if opt.upgrade: + upgrade_pythonbrew() + else: + install_pythonbrew()