From 3a9cfe074e22e8f82cc3a3d707f1de1b24bb05ae Mon Sep 17 00:00:00 2001 From: Yuta Date: Sun, 20 Mar 2011 03:45:58 +0900 Subject: [PATCH] update command --- pythonbrew/commands/update.py | 61 +++++++++++++++++++++++++++++------ pythonbrew/curl.py | 5 +++ pythonbrew/define.py | 18 +++++++---- pythonbrew/etc/config.cfg | 9 +++--- pythonbrew/util.py | 6 ++-- tests/test_01_update.py | 6 +++- 6 files changed, 81 insertions(+), 24 deletions(-) diff --git a/pythonbrew/commands/update.py b/pythonbrew/commands/update.py index 2462eb3..1fbaca0 100644 --- a/pythonbrew/commands/update.py +++ b/pythonbrew/commands/update.py @@ -2,7 +2,7 @@ import os import sys from pythonbrew.basecommand import Command from pythonbrew.define import PATH_DISTS, VERSION, ROOT,\ - PATH_BUILD + PATH_BUILD, PYTHONBREW_UPDATE_URL_CONFIG, PATH_ETC_CONFIG from pythonbrew.log import logger from pythonbrew.downloader import Downloader, get_pythonbrew_update_url,\ get_stable_version, get_headerinfo_from_url @@ -11,18 +11,58 @@ from pythonbrew.util import rm_r, unpack_downloadfile, Link, is_gzip, Subprocess class UpdateCommand(Command): name = "update" usage = "%prog" - summary = "Upgrades pythonbrew to the latest version" + summary = "Update pythonbrew to the latest version" + + def __init__(self): + super(UpdateCommand, self).__init__() + self.parser.add_option( + '--head', + dest='head', + action='store_true', + default=False, + help='Update pythonbrew to the github version' + ) + self.parser.add_option( + '--config', + dest='config', + action='store_true', + default=False, + help='Update config.cfg' + ) def run_command(self, options, args): - if args: - version = args[0] + if options.config: + self._update_config(options, args) + else: + self._update_pythonbrew(options, args) + + def _update_config(self, options, args): + # config.cfg update + # TODO: Automatically create for config.cfg + download_url = PYTHONBREW_UPDATE_URL_CONFIG + if not download_url: + logger.error("Invalid download url in config.cfg. `%s`" % download_url) + sys.exit(1) + distname = Link(PYTHONBREW_UPDATE_URL_CONFIG).filename + download_file = PATH_ETC_CONFIG + try: + d = Downloader() + d.download(distname, download_url, download_file) + except: + logger.error("Failed to download. `%s`" % download_url) + sys.exit(1) + logger.info("The config.cfg has been updated.") + + def _update_pythonbrew(self, options, args): + # pythonbrew update + if options.head: + version = 'head' else: version = get_stable_version() - - # check for latest version - if version <= VERSION: - logger.info("You are already running the installed latest version of pythonbrew.") - return + # check for version + if version <= VERSION: + logger.info("You are already running the installed latest version of pythonbrew.") + return download_url = get_pythonbrew_update_url(version) if not download_url: @@ -30,7 +70,8 @@ class UpdateCommand(Command): sys.exit(1) headinfo = get_headerinfo_from_url(download_url) content_type = headinfo['content-type'] - if not is_gzip(content_type, Link(download_url).filename): + # head is only for gzip. + if not options.head and not is_gzip(content_type, Link(download_url).filename): logger.error("Invalid content-type: `%s`" % content_type) sys.exit(1) diff --git a/pythonbrew/curl.py b/pythonbrew/curl.py index b2a6ca5..3d72495 100644 --- a/pythonbrew/curl.py +++ b/pythonbrew/curl.py @@ -1,4 +1,5 @@ import sys +import re import subprocess from subprocess import Popen, PIPE from pythonbrew.log import logger @@ -23,6 +24,10 @@ class Curl(object): if p.returncode: raise respinfo = {} + for line in p.stdout: + line = line.strip() + if re.match('^HTTP.*? 200 OK$', line): + break for line in p.stdout: line = line.strip().split(":", 1) if len(line) == 2: diff --git a/pythonbrew/define.py b/pythonbrew/define.py index 71e33a5..4b94bcb 100644 --- a/pythonbrew/define.py +++ b/pythonbrew/define.py @@ -35,22 +35,28 @@ PATH_ETC_CONFIG = os.path.join(PATH_ETC,'config.cfg') # read config.cfg config = ConfigParser.SafeConfigParser() config.read([PATH_ETC_CONFIG, os.path.join(INSTALLER_ROOT,'etc','config.cfg')]) +def _get_or_default(section, option, default=''): + try: + return config.get(section, option) + except: + return default # setuptools download -DISTRIBUTE_SETUP_DLSITE = config.get('distribute', 'url') +DISTRIBUTE_SETUP_DLSITE = _get_or_default('distribute', 'url') # pythonbrew download -PYTHONBREW_UPDATE_URL_HEAD = config.get('pythonbrew', 'head') -PYTHONBREW_UPDATE_URL_PYPI = config.get('pythonbrew', 'pypi') +PYTHONBREW_UPDATE_URL_HEAD = _get_or_default('pythonbrew', 'head') +PYTHONBREW_UPDATE_URL_PYPI = _get_or_default('pythonbrew', 'pypi') +PYTHONBREW_UPDATE_URL_CONFIG = _get_or_default('pythonbrew', 'config') # stable version text -PYTHONBREW_STABLE_VERSION_URL = config.get('pythonbrew', 'stable-version') +PYTHONBREW_STABLE_VERSION_URL = _get_or_default('pythonbrew', 'stable-version') # python download LATEST_VERSIONS_OF_PYTHON = [] PYTHON_VERSION_URL = {} -PYTHON_VERSION_URL["1.5.6"] = config.get('Python-1.5.6', 'url') -PYTHON_VERSION_URL["1.6.1"] = config.get('Python-1.6.1', 'url') +PYTHON_VERSION_URL["1.5.6"] = _get_or_default('Python-1.5.6', 'url') +PYTHON_VERSION_URL["1.6.1"] = _get_or_default('Python-1.6.1', 'url') for section in sorted(config.sections()): m = re.search("^Python-(.*)$", section) if m: diff --git a/pythonbrew/etc/config.cfg b/pythonbrew/etc/config.cfg index a03efd2..62d3c71 100644 --- a/pythonbrew/etc/config.cfg +++ b/pythonbrew/etc/config.cfg @@ -1,10 +1,11 @@ [distribute] -url = "http://python-distribute.org/distribute_setup.py" +url = http://python-distribute.org/distribute_setup.py [pythonbrew] -head = 'http://github.com/utahta/pythonbrew/tarball/master' -pypi = 'http://pypi.python.org/packages/source/p/pythonbrew/pythonbrew-%%s.tar.gz' -stable-version = 'https://github.com/utahta/pythonbrew/raw/master/stable-version.txt' +head = https://github.com/utahta/pythonbrew/tarball/master +pypi = http://pypi.python.org/packages/source/p/pythonbrew/pythonbrew-%%s.tar.gz +stable-version = https://github.com/utahta/pythonbrew/raw/master/stable-version.txt +config = https://github.com/utahta/pythonbrew/raw/master/pythonbrew/etc/config.cfg [Python-1.5.6] url = http://www.python.org/ftp/python/src/py152.tgz diff --git a/pythonbrew/util.py b/pythonbrew/util.py index 3491017..4761384 100644 --- a/pythonbrew/util.py +++ b/pythonbrew/util.py @@ -54,9 +54,9 @@ def is_html(content_type): return False def is_gzip(content_type, filename): - if (content_type == 'application/x-gzip' - or tarfile.is_tarfile(filename) - or splitext(filename)[1].lower() in ('.tar', '.tar.gz', '.tar.bz2', '.tgz', '.tbz')): + if(content_type == 'application/x-gzip' + or tarfile.is_tarfile(filename) + or splitext(filename)[1].lower() in ('.tar', '.tar.gz', '.tgz')): return True return False diff --git a/tests/test_01_update.py b/tests/test_01_update.py index 884c11e..8a4dfd5 100644 --- a/tests/test_01_update.py +++ b/tests/test_01_update.py @@ -1,5 +1,9 @@ +class UpdateOptions(object): + head = False + config = False + def test_update(): from pythonbrew.commands.update import UpdateCommand c = UpdateCommand() - c.run_command(None, None) + c.run_command(UpdateOptions(), None) \ No newline at end of file