Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for pull request #2 #3

Merged
merged 3 commits into from Apr 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions pythonz/curl.py
@@ -1,6 +1,5 @@

import re
import sys
import subprocess

from subprocess import Popen, PIPE
Expand All @@ -10,11 +9,10 @@
from pythonz.exceptions import CurlFetchException

class Curl(object):
def __init__(self):
@classmethod
def can_use(cls):
returncode = subprocess.call("command -v curl > /dev/null", shell=True)
if returncode:
logger.log("pythonz required curl. curl was not found in your path.")
sys.exit(1)
return not returncode

def read(self, url):
p = Popen("curl -skL %s" % url, stdout=PIPE, shell=True)
Expand Down
12 changes: 10 additions & 2 deletions pythonz/downloader.py
Expand Up @@ -2,15 +2,23 @@
from pythonz.define import PYTHON_VERSIONS_URLS
from pythonz.log import logger
from pythonz.curl import Curl
from pythonz.genericdownloader import GenericDownloader


def get_concrete_downloader():
if Curl.can_use():
return Curl()
else:
return GenericDownloader()

def get_headerinfo_from_url(url):
c = Curl()
c = get_concrete_downloader()
return c.readheader(url)

class Downloader(object):
def download(self, msg, url, path):
logger.info("Downloading %s as %s" % (msg, path))
c = Curl()
c = get_concrete_downloader()
c.fetch(url, path)

def get_python_version_url(type, version):
Expand Down
71 changes: 71 additions & 0 deletions pythonz/genericdownloader.py
@@ -0,0 +1,71 @@
# coding: utf-8

import re
import urllib
import urllib2
import sys
import traceback

from pythonz.log import logger
from pythonz.exceptions import CurlFetchException

class ProgressBar(object):
def __init__(self, out=sys.stdout):
self._term_width = 79
self._out = out

def update_line(self, current):
num_bar = int(current / 100.0 * (self._term_width - 5))
bars = u'#' * num_bar
spaces = u' ' * (self._term_width - 5 - num_bar)
percentage = u'%3d' % int(current) + u'%\r'
return bars + spaces + u' ' + percentage

def reporthook(self, blocknum, bs, size):
current = (blocknum * bs * 100) / size
if current > 100:
current = 100
self._out.write(self.update_line(current))
self._out.flush()

def finish(self):
self._out.write(self.update_line(100))
self._out.flush()

class HEADRequest(urllib2.Request):
def get_method(self):
return "HEAD"

class GenericDownloader(object):
@classmethod
def can_use(cls):
return True

def read(self, url):
try:
r = urllib.urlopen(url)
except IOError:
logger.log(traceback.format_exc())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch the appropriate exception here.

raise CurlFetchException('Failed to fetch.')
return r.read()

def readheader(self, url):
try:
req = HEADRequest(url)
res = urllib2.urlopen(req)
except IOError:
logger.log(traceback.format_exc())
raise CurlFetchException('Failed to fetch.')
if res.code != 200:
raise CurlFetchException('Failed to fetch.')
return res.info()

def fetch(self, url, filename):
b = ProgressBar()
try:
urllib.urlretrieve(url, filename, b.reporthook)
sys.stdout.write('\n')
except IOError:
sys.stdout.write('\n')
logger.log(traceback.format_exc())
raise CurlFetchException('Failed to fetch.')