update 0.5

This commit is contained in:
utahta
2010-11-13 03:25:56 +09:00
parent 640fb13834
commit 3f455e161b
26 changed files with 969 additions and 1423 deletions
View File
+19
View File
@@ -0,0 +1,19 @@
import os
from pythonbrew.basecommand import Command
from pythonbrew.define import PATH_BUILD, PATH_DISTS
from pythonbrew.util import rm_r
class CleanCommand(Command):
name = "clean"
usage = "%prog"
summary = "Remove stale source folders and archives"
def run_command(self, options, args):
self._clean(PATH_BUILD)
self._clean(PATH_DISTS)
def _clean(self, root):
for dir in os.listdir(root):
rm_r("%s/%s" % (root, dir))
CleanCommand()
+27
View File
@@ -0,0 +1,27 @@
from pythonbrew.basecommand import Command, command_dict
from pythonbrew.baseparser import parser
from pythonbrew.log import logger
class HelpCommand(Command):
name = "help"
usage = "%prog [COMMAND]"
summary = "Show available commands"
def run_command(self, options, args):
if args:
command = args[0]
if command not in command_dict:
parser.error("Unknown command: `%s`" % command)
return
command = command_dict[command]
command.parser.print_help()
return
parser.print_help()
logger.info("\nCommands available:")
commands = [command_dict[key] for key in sorted(command_dict.keys())]
for command in commands:
logger.info(" %s: %s" % (command.name, command.summary))
logger.info("\nFurther Instructions:")
logger.info(" http://github.com/utahta/pythonbrew")
HelpCommand()
+184
View File
@@ -0,0 +1,184 @@
import os
import sys
import re
import shutil
from pythonbrew.basecommand import Command
from pythonbrew.util import unlink, splitext, Subprocess, Package, makedirs,\
rm_r
from pythonbrew.define import ROOT, PATH_PYTHONS, PATH_DISTS, PATH_BUILD, PATH_LOG, DISTRIBUTE_SETUP_DLSITE
from pythonbrew.log import logger
from pythonbrew.downloader import get_python_package_url, Downloader
class InstallCommand(Command):
name = "install"
usage = "%prog [OPTIONS] VERSION"
summary = "Build and install the given version of python"
def __init__(self):
super(InstallCommand, self).__init__()
self.parser.add_option(
"-f", "--force",
dest="force",
action="store_true",
default=False,
help="Force installation of a Python."
)
self.parser.add_option(
"-C", "--configure",
dest="configure",
default="",
metavar="CONFIGURE_OPTIONS",
help="Custom configure options."
)
self.parser.add_option(
"-n", "--no-setuptools",
dest="no_setuptools",
action="store_true",
default=False,
help="Skip installation of setuptools."
)
self._logfile = "%s/build.log" % PATH_LOG
def run_command(self, options, args):
if args:
# Install Python
self._install_python(args[0], options)
else:
logger.error("Package not found.")
def _install_python(self, dist, options):
pkg = Package(dist)
distname = self._download_package(pkg)
pkgname = pkg.name
version = pkg.version
install_dir = "%s/%s" % (PATH_PYTHONS, pkgname)
configure = "--prefix=%s %s" % (install_dir, options.configure)
logger.info("")
logger.info("This could take a while. You can run the following command on another shell to track the status:")
logger.info(" tail -f %s" % self._logfile)
logger.info("")
try:
s = Subprocess(log=self._logfile, shell=True, cwd=PATH_BUILD, print_cmd=False)
logger.info("Extracting %s" % distname)
s.check_call(self._get_uncompress_command(distname))
logger.info("Installing %s into %s" % (pkgname, install_dir))
s.chdir("%s/%s" % (PATH_BUILD, pkgname))
s.check_call("./configure %s" % (configure))
if options.force:
s.check_call("make")
else:
s.check_call("make")
s.check_call("make test")
if version == "1.5.2" or version == "1.6.1":
makedirs(install_dir)
s.check_call("make install")
except:
rm_r(install_dir)
logger.error("""Failed to install %(pkgname)s. See %(ROOT)s/build.log to see why.
pythonbrew install --force %(version)s""" % {"pkgname":pkgname, "ROOT":ROOT, "version":version})
sys.exit(1)
# install setuptools
self._install_setuptools(pkgname, options.no_setuptools)
logger.info("""
Installed %(pkgname)s successfully. Run the following command to switch to %(pkgname)s.
pythonbrew switch %(version)s""" % {"pkgname":pkgname, "version":version})
def _download_package(self, pkg):
pkgname = pkg.name
version = pkg.version
if os.path.isdir("%s/%s" % (PATH_PYTHONS, pkgname)):
logger.error("You are already installed `%s`." % pkgname)
sys.exit(1)
download_url = get_python_package_url(version)
if not download_url:
logger.error("Unknown package: `%s`" % pkgname)
sys.exit(1)
distname = os.path.basename(download_url)
download_path = "%s/%s" % (PATH_DISTS, distname)
if os.path.isfile(download_path):
logger.info("Use the previously fetched %s" % (download_path))
else:
try:
dl = Downloader()
dl.download(
distname,
download_url,
download_path
)
except:
unlink(download_path)
logger.info("\nInterrupt to abort. `%s`" % (download_url))
sys.exit(1)
# iffy
if os.path.getsize(download_path) < 1000000:
logger.error("Failed to download. `%s`" % (download_url))
unlink(download_path)
sys.exit(1)
return distname
def _get_uncompress_command(self, basename):
distpath = "%s/%s" % (PATH_DISTS, basename)
if os.path.isfile(distpath):
ext = splitext(basename)[1]
if ext == ".tar.gz" or ext == ".tgz":
return "tar zxf %s" % (distpath)
elif ext == ".tar.bz2":
return "tar jxf %s" % (distpath)
elif ext == ".tar":
return "tar xf %s" % (distpath)
elif ext == ".zip":
return "unzip %s" % (distpath)
elif os.path.isdir(distpath):
return "mv %s %s/%s" % (distpath, PATH_BUILD, basename)
else:
logger.error("File not found. `%s`" % (basename))
return ""
def _install_setuptools(self, pkgname, no_setuptools):
if no_setuptools:
logger.info("Skip installation setuptools.")
return
if re.match("^Python-3.*", pkgname):
is_python3 = True
else:
is_python3 = False
download_url = DISTRIBUTE_SETUP_DLSITE
basename = os.path.basename(download_url)
dl = Downloader()
dl.download(basename, download_url, "%s/%s" % (PATH_DISTS, basename))
install_dir = "%s/%s" % (PATH_PYTHONS, pkgname)
if is_python3:
if os.path.isfile("%s/bin/python3" % (install_dir)):
pyexec = "%s/bin/python3" % (install_dir)
elif os.path.isfile("%s/bin/python3.0" % (install_dir)):
pyexec = "%s/bin/python3.0" % (install_dir)
else:
logger.error("Python3 binary not found. `%s`" % (install_dir))
return
else:
pyexec = "%s/bin/python" % (install_dir)
try:
s = Subprocess(log=self._logfile, shell=True, cwd=PATH_DISTS, print_cmd=False)
logger.info("Installing distribute into %s" % install_dir)
s.check_call("%s %s" % (pyexec, basename))
if os.path.isfile("%s/bin/easy_install" % (install_dir)) and not is_python3:
logger.info("Installing pip into %s" % install_dir)
s.check_call("%s/bin/easy_install pip" % (install_dir), cwd=None)
except:
logger.error("Failed to install setuptools. See %s/build.log to see why." % (ROOT))
logger.info("Skip install setuptools.")
InstallCommand()
+26
View File
@@ -0,0 +1,26 @@
import os
from pythonbrew.basecommand import Command
from pythonbrew.define import PATH_PYTHONS
from pythonbrew.log import logger
import sys
class InstalledCommand(Command):
name = "installed"
usage = "%prog"
summary = "List the installed versions of python"
def run_command(self, options, args):
cur = ""
if not os.path.islink("%s/current" % PATH_PYTHONS):
logger.info("%s (*)" % sys.executable)
elif os.path.islink("%s/current" % PATH_PYTHONS):
cur = os.path.basename(os.path.realpath("%s/current" % PATH_PYTHONS))
for d in sorted(os.listdir("%s/" % PATH_PYTHONS)):
if d == "current":
continue
if cur == d:
logger.info("%s (*)" % cur)
else:
logger.info("%s" % (d))
InstalledCommand()
+34
View File
@@ -0,0 +1,34 @@
import re
from pythonbrew.basecommand import Command
from pythonbrew.define import PYTHON_PACKAGE_URL
from pythonbrew.util import Package
from pythonbrew.log import logger
class ListCommand(Command):
name = "list"
usage = "%prog [VERSION]"
summary = "List the available install version of python"
def run_command(self, options, args):
if args:
pkg = Package(args[0])
_re = re.compile(r"%s" % pkg.name)
pkgs = []
for pkgname in self._get_packages_name():
if _re.match(pkgname):
pkgs.append(pkgname)
if pkgs:
logger.info("Pythons:")
for pkgname in pkgs:
logger.info(" %s" % pkgname)
else:
print "Package not found. `%s`" % pkg.name
else:
logger.info("Pythons:")
for pkgname in self._get_packages_name():
logger.info(" %s" % pkgname)
def _get_packages_name(self):
return ["Python-%s" % version for version in sorted(PYTHON_PACKAGE_URL.keys())]
ListCommand()
+12
View File
@@ -0,0 +1,12 @@
from pythonbrew.basecommand import Command
from pythonbrew.util import off
class OffCommand(Command):
name = "off"
usage = "%prog"
summary = "Disable pythonbrew"
def run_command(self, options, args):
off()
OffCommand()
+41
View File
@@ -0,0 +1,41 @@
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
from pythonbrew.log import logger
class SwitchCommand(Command):
name = "switch"
usage = "%prog VERSION"
summary = "Switch to the given version"
def run_command(self, options, args):
if not args:
logger.error("Unrecognized command line argument: argument not found.")
sys.exit(1)
pkg = Package(args[0])
pkgname = pkg.name
pkgdir = "%s/%s" % (PATH_PYTHONS, pkgname)
if not os.path.isdir(pkgdir):
logger.error("`%s` is not installed." % pkgname)
sys.exit(1)
self._switch_dir(pkgdir)
logger.info("Switched to %s" % pkgname)
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)
SwitchCommand()
+29
View File
@@ -0,0 +1,29 @@
import os
import sys
from pythonbrew.basecommand import Command
from pythonbrew.define import PATH_PYTHONS
from pythonbrew.util import off, rm_r, Package
from pythonbrew.log import logger
class UninstallCommand(Command):
name = "uninstall"
usage = "%prog VERSION"
summary = "Uninstall the given version of python"
def run_command(self, options, args):
if args:
pkg = Package(args[0])
pkgname = pkg.name
pkgpath = "%s/%s" % (PATH_PYTHONS, pkgname)
if not os.path.isdir(pkgpath):
logger.error("`%s` is not installed." % pkgname)
sys.exit(1)
if os.path.islink("%s/current" % PATH_PYTHONS):
curpath = os.path.realpath("%s/current" % PATH_PYTHONS)
if pkgpath == curpath:
off()
rm_r(pkgpath)
else:
self.parser.print_help()
UninstallCommand()
+64
View File
@@ -0,0 +1,64 @@
import os
import sys
import re
from pythonbrew.basecommand import Command
from pythonbrew.define import PATH_DISTS, VERSION, PYTHONBREW_DIRNAME, ROOT
from pythonbrew.log import logger
from pythonbrew.downloader import Downloader, get_pythonbrew_update_url
from pythonbrew.util import Subprocess, rm_r
class UpdateCommand(Command):
name = "update"
usage = "%prog"
summary = "Upgrades pythonbrew to the latest version"
def run_command(self, options, args):
version = "head"
if args:
version = args[0]
# check for latest version
if version == VERSION:
logger.info("You are already running the installed latest version of pythonbrew.")
sys.exit()
download_url = get_pythonbrew_update_url(version)
if not download_url:
logger.error("`%s` of pythonbrew not found." % version)
sys.exit(1)
distname = "pythonbrew.tgz"
download_path = "%s/%s" % (PATH_DISTS, distname)
try:
d = Downloader()
d.download(distname, download_url, download_path)
except:
logger.error("Failed to download. `%s`" % download_url)
sys.exit(1)
_re = re.compile("^%s.*" % PYTHONBREW_DIRNAME)
for name in os.listdir(PATH_DISTS):
if _re.match(name):
rm_r("%s/%s" % (PATH_DISTS, name))
try:
s = Subprocess(shell=True, cwd=PATH_DISTS, print_cmd=False)
logger.info("Extracting %s" % download_path)
s.check_call("tar zxf %s" % download_path)
except:
logger.error("Failed to update pythonbrew.")
sys.exit(1)
for name in os.listdir(PATH_DISTS):
if _re.match(name):
try:
installer_path = "%s/%s" % (PATH_DISTS, name)
s = Subprocess(shell=True, cwd=PATH_DISTS, print_cmd=False)
logger.info("Installing %s into %s" % (installer_path, ROOT))
s.check_call("%s %s/pythonbrew_install.py" % (sys.executable, installer_path))
except:
logger.error("Failed to update pythonbrew.")
sys.exit(1)
break
logger.info("The pythonbrew has been updated.")
UpdateCommand()
+13
View File
@@ -0,0 +1,13 @@
from pythonbrew.basecommand import Command
from pythonbrew.define import VERSION
from pythonbrew.log import logger
class VersionCommand(Command):
name = "version"
usage = "%prog"
summary = "Show version"
def run_command(self, options, args):
logger.info(VERSION)
VersionCommand()