From c5f31138c17d2e1b2dc35466ca4669f1573ed17e Mon Sep 17 00:00:00 2001 From: utahta Date: Sun, 21 Nov 2010 12:44:15 +0900 Subject: [PATCH] use curl instead of urllib --- PKG-INFO | 2 +- README.rst | 2 +- pythonbrew/commands/update.py | 6 ++--- pythonbrew/downloader.py | 48 +++++++---------------------------- pythonbrew/installer.py | 6 ++--- 5 files changed, 17 insertions(+), 47 deletions(-) diff --git a/PKG-INFO b/PKG-INFO index 4d420ba..684b6f9 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -104,7 +104,7 @@ Options ======= \-f | --force - Force installation of a Python. + Force installation of a Python. (skip `make test`) \-C | --configure Custom configure options. diff --git a/README.rst b/README.rst index 8b9ad93..7c099aa 100644 --- a/README.rst +++ b/README.rst @@ -103,7 +103,7 @@ Options ======= \-f | --force - Force installation of a Python. + Force installation of a Python. (skip `make test`) \-C | --configure Custom configure options. diff --git a/pythonbrew/commands/update.py b/pythonbrew/commands/update.py index 71f1da4..1e455c0 100644 --- a/pythonbrew/commands/update.py +++ b/pythonbrew/commands/update.py @@ -5,7 +5,7 @@ from pythonbrew.define import PATH_DISTS, VERSION, ROOT,\ PATH_BUILD from pythonbrew.log import logger from pythonbrew.downloader import Downloader, get_pythonbrew_update_url,\ - get_response_from_url, get_stable_version + get_stable_version, get_headerinfo_from_url from pythonbrew.util import rm_r, unpack_downloadfile, Link, is_gzip, Subprocess class UpdateCommand(Command): @@ -28,8 +28,8 @@ class UpdateCommand(Command): if not download_url: logger.error("`%s` of pythonbrew not found." % version) sys.exit(1) - resp = get_response_from_url(download_url) - content_type = resp.info()['content-type'] + headinfo = get_headerinfo_from_url(download_url) + content_type = headinfo['content-type'] if not is_gzip(content_type, Link(download_url).filename): logger.error("Invalid content-type: `%s`" % content_type) sys.exit(1) diff --git a/pythonbrew/downloader.py b/pythonbrew/downloader.py index a6f472b..7b9134e 100644 --- a/pythonbrew/downloader.py +++ b/pythonbrew/downloader.py @@ -1,51 +1,21 @@ -import sys -import urllib -import urllib2 -from pythonbrew.util import size_format from pythonbrew.define import PYTHON_VERSION_URL, PYTHONBREW_STABLE_VERSION_URL, \ PYTHONBREW_UPDATE_URL_PYPI, PYTHONBREW_UPDATE_URL_HEAD from pythonbrew.log import logger +from pythonbrew.curl import Curl -def get_response_from_url(url): - try: - resp = urllib2.urlopen(url) - except urllib2.HTTPError, e: - logger.error("HTTP error %s while getting %s" % (e.code, url)) - sys.exit(1) - except IOError, e: - # Typically an FTP error - logger.error("Error %s while getting %s" % (e, url)) - sys.exit(1) - return resp +def get_headerinfo_from_url(url): + c = Curl() + return c.readheader(url) def get_stable_version(): - resp = get_response_from_url(PYTHONBREW_STABLE_VERSION_URL) - return resp.read() + c = Curl() + return c.read(PYTHONBREW_STABLE_VERSION_URL) class Downloader(object): - def __init__(self): - self._msg = "" - self._last_msg = "" - self._bytes = 0.0 - def download(self, msg, url, path): - self._msg = msg - self._bytes = 0 - urllib.urlretrieve(url, path, self._download_progress) - print " downloaded." - - def _download_progress(self, block, blockbytes, maxbytes): - self._bytes += float(blockbytes) - if self._bytes >= maxbytes: - self._bytes = maxbytes - percent = (self._bytes / maxbytes) * 100 - max_size = size_format(maxbytes) - now_size = size_format(self._bytes) - now_msg = "\rDownloading %s (%s): %3i%% %s" % (self._msg, max_size, percent, now_size) - padding = " " * (len(self._last_msg) - len(now_msg)) - sys.stdout.write("%s%s" % (now_msg, padding)) - sys.stdout.flush() - self._last_msg = now_msg + logger.info("Downloading %s as %s" % (msg, path)) + c = Curl() + c.fetch(url, path) def get_pythonbrew_update_url(version): if version == "head": diff --git a/pythonbrew/installer.py b/pythonbrew/installer.py index 752e033..b96a95b 100644 --- a/pythonbrew/installer.py +++ b/pythonbrew/installer.py @@ -13,7 +13,7 @@ from pythonbrew.define import PATH_BUILD, PATH_BIN, PATH_DISTS, PATH_PYTHONS,\ PATH_BIN_PYBREW, ROOT, PATH_LOG, DISTRIBUTE_SETUP_DLSITE, PATH_PATCHES,\ PATH_PATCHES_MACOSX_PYTHON25, PATH_PATCHES_MACOSX_PYTHON24 from pythonbrew.downloader import get_python_version_url, Downloader,\ - get_response_from_url + get_headerinfo_from_url from pythonbrew.log import logger def install_pythonbrew(): @@ -115,8 +115,8 @@ class PythonInstaller(object): self.install_dir = "%s/%s" % (PATH_PYTHONS, pkg.name) self.build_dir = "%s/%s" % (PATH_BUILD, pkg.name) self.download_file = "%s/%s" % (PATH_DISTS, filename) - resp = get_response_from_url(self.download_url) - self.content_type = resp.info()['content-type'] + headerinfo = get_headerinfo_from_url(self.download_url) + self.content_type = headerinfo['content-type'] self.options = options self.logfile = "%s/build.log" % PATH_LOG