Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
111 lines (96 sloc) 3.47 KB
import os
import re
import sys
import operator
import cStringIO
import pkg_resources
from hashlib import md5
except ImportError:
from md5 import md5
from .utils import ext, right_egg
from .web import WebManager, PackageManager
from .types import Version, Egg, Archive, ReqSet
from .log import logger
class Requirement(object):
OPMAP = {'==': operator.eq,
'<=': operator.le,
'!=': lambda a,b: a != b,
None: lambda a,b: True
def __init__(self, req):
self.req = req
self.reqset = ReqSet()
def __repr__(self):
return 'Requirement({0})'.format(self.req)
def __str__(self):
return str(self.req)
def split(self):
for c in ('==', '>=', '>', '<=', '<'):
if c in self.req:, self.op, self.version = map(str.strip, self.req.partition(c))
self.version = Version(self.version)
else: = self.req.split()[0]
self.version = None
self.op = None
@ staticmethod
def find_version(s):
v = []
for c in s:
if c.isdigit() or c == '.':
return Version(''.join(v).strip('.')) ## FIXME do we really need .strip() ?
def match(self, v):
return self.OPMAP[self.op](v, self.version)
#def best_match(self, reqs):
# matched = {}
# for r in reqs:
# parts = r.split('-')
# version = Requirement.find_version('-'.join(parts[1:]))
# if self.version is None or self.match(version):
# matched[version] = r
# if len(matched) == 0:
# return None
# elif len(matched) == 1:
# return matched[matched.keys()[0]]
# ## The highest version possible
# return matched[max(matched)] ## OR matched[sorted(matched.keys(), reverse=True)[0]]?
def install(self):
p = PackageManager(self)
success = False
for pext in ('.tar.gz', '.tar.bz2', '.zip', '.egg'):
for v, name, hash, url in p.arrange_items()[pext]:
if pext == '.egg' and not right_egg(name):
e = ext(name)
if e not in ('.tar.gz', '.tar.bz2', '.zip', '.egg'):
continue'Best match: {0}=={1}',, v)'Downloading {0}',
fobj = cStringIO.StringIO(WebManager.request(url))'Checking md5 sum')
if md5(fobj.getvalue()).hexdigest() != hash:
logger.fatal('E: {0} appears to be corrupted',
if e in ('.tar.gz', '.tar.bz2', '.zip'):
installer = Archive(fobj, e,, self.reqset)
elif e == '.egg':
installer = Egg(fobj, name, self.reqset,
## There is no need to catch the exceptions now, this will be done by `pyg.inst.Installer.install`
if not self.version:
self.version = v
success = True
if success:
if not success:
raise InstallationError
Jump to Line
Something went wrong with that request. Please try again.