Esempio n. 1
0
File: web.py Progetto: Malex/pyg
    def __init__(self, req, pref=None):
        self.req = req
        self.req.name = self.name = real_name(self.req.name)
        if self.req.op == '==': ## LOL
            self.package_manager = PyPIJson(self.name, self.req.version)
        else:
            self.package_manager = PyPIJson(self.name, highest_version(self.req))

        url = args_manager['install']['index_url'] + '/' + self.package_manager.URL.split('/pypi/', 1)[1]
        self.package_manager.URL = url
        self._set_prefs(pref)
        self.downloaded_name = None
        self.downloaded_version = None
Esempio n. 2
0
File: web.py Progetto: fdev31/pyg
 def __init__(self, req, pref=None):
     self.req = req
     self.req.name = self.name = real_name(self.req.name)
     if self.req.op is None:
         self.package_manager = PyPIJson(self.name)
     elif self.req.op == '==': ## LOL
         self.package_manager = PyPIJson(self.name, self.req.version)
     else:
         self.package_manager = PyPIJson(self.name, highest_version(self.req))
     url = args_manager['install']['packages_url']
     if url.endswith('/simple'):
         self.package_manager.URL = url[:-6] + self.package_manager.URL.rsplit('/pypi/', 1)[1]
     self._set_prefs(pref)
     self.downloaded_name = None
     self.downloaded_version = None
Esempio n. 3
0
File: web.py Progetto: fdev31/pyg
class ReqManager(object):
    def __init__(self, req, pref=None):
        self.req = req
        self.req.name = self.name = real_name(self.req.name)
        if self.req.op is None:
            self.package_manager = PyPIJson(self.name)
        elif self.req.op == '==': ## LOL
            self.package_manager = PyPIJson(self.name, self.req.version)
        else:
            self.package_manager = PyPIJson(self.name, highest_version(self.req))
        url = args_manager['install']['packages_url']
        if url.endswith('/simple'):
            self.package_manager.URL = url[:-6] + self.package_manager.URL.rsplit('/pypi/', 1)[1]
        self._set_prefs(pref)
        self.downloaded_name = None
        self.downloaded_version = None

    def _set_prefs(self, pref):
        if pref is None:
            pref = PREFERENCES
        pref = list(pref)
        if len(pref) < len(PREFERENCES):
            for p in PREFERENCES:
                if p not in pref:
                    pref.append(p)
        self.pref = pref

    def find(self):
        def _remove_query(url):
            return urlparse.urlunsplit(urlparse.urlsplit(url)[:3] + ('',) * 2)
        def _get_version(filename):
            ## A bit hacky but there is no solution because some packages
            ## are in the form {package_name}-{version}-{something_else}-{?pyx.y}.{ext}
            ## and we cannot predict where is the version in that mess.
            _version_re = re.compile(r'\d[\.\d\w]*')
            parts = name(filename).split('-')
            for part in parts:
                match = _version_re.search(part)
                if match is not None:
                    return match.group()

        found = list(self.package_manager.find())
        if not found:
            logger.warn('Warning: did not find any files on PyPI')
            for link in get_links(self.name, args_manager['install']['packages_url']):
                package_name = _remove_query(link).split('/')[-1]
                version = _get_version(package_name)
                found.append((version, package_name, None, link))
        return found

    def files(self):
        files = FileMapper(list)
        files.pref = self.pref
        for release in self.find():
            files[ext(release[1])].append(release)
        return files

    def download(self, dest):
        dest = os.path.abspath(dest)
        files = self.files()

        ## We need a placeholder because of the nested for loops
        success = False

        for p in self.pref:
            if success:
                break
            if not files[p]:
                logger.warn('{0} files not found. Continue searching...', p)
                continue
            for v, name, hash, url in files[p]:
                if success:
                    break
                if p == '.egg' and not right_egg(name):
                    logger.info('Found egg file for another Python version: {0}. Continue searching...',                               version_egg(name))
                    continue
                try:
                    data = download(url, 'Retrieving data for {0}'.format(self.name)).getvalue()
                except (urllib2.URLError, urllib2.HTTPError) as e:
                    logger.debug('urllib2 error: {0}', e.args)
                    continue
                if not data:
                    logger.debug('debug: Request failed')
                    continue
                if not os.path.exists(dest):
                    os.makedirs(dest)
                try:
                    logger.info('Writing data into {0}', name)
                    with open(os.path.join(dest, name), 'w') as f:
                        f.write(data)
                except (IOError, OSError):
                    logger.debug('debug: Error while writing data')
                    continue
                logger.success('{0} downloaded successfully', self.name)
                success = True
            self.downloaded_name = name
            self.downloaded_version = v