Ejemplo n.º 1
0
    def test_build_repository_with_specified_root(self, mock):
        from papaye.proxy import PyPiProxy
        from papaye.models import Package, ReleaseFile, Root
        mock.return_value = self.pypi_response
        info_dict = json.loads(self.pypi_response.content.decode('utf-8'))
        root = Root()

        proxy = PyPiProxy(self.request, 'pyramid')
        result = proxy.build_repository(with_metadata=True, root=root)

        self.assertIsInstance(result, Package)
        for release in result.releases.values():
            self.assertIn(release.__name__, info_dict['releases'].keys())
        self.assertEqual(len(result.releases.keys()),
                         len(info_dict['releases'].keys()))
        self.assertEqual(len(result['1.5'].release_files.keys()), 1)
        self.assertIsInstance(result['1.5'].release_files.values()[0],
                              ReleaseFile)
        self.assertTrue(
            getattr(result['1.5'].release_files.values()[0], 'pypi_url', None))
        self.assertEqual(
            result['1.5'].release_files.values()[0].pypi_url,
            "https://pypi.python.org/packages/source/p/pyramid/pyramid-1.5.tar.gz"
        )
        self.assertIsNotNone(result['1.5'].metadata)
        assert result.__parent__ is root
Ejemplo n.º 2
0
def not_found(request):
    settings = request.registry.settings
    proxy = settings.get('papaye.proxy', False)
    proxy = True if proxy and proxy == 'true' else False
    if not proxy:
        return HTTPNotFound()
    try:
        proxy = PyPiProxy(request, request.matchdict['traverse'][0])
        package = proxy.build_repository()
        if not package:
            return HTTPNotFound()
        if len(request.matchdict['traverse']) == 1:
            view = ListReleaseFileView(package, request)
        elif len(request.matchdict['traverse']) == 2:
            context = package[request.matchdict['traverse'][1]]
            view = ListReleaseFileByReleaseView(context, request)
        elif len(request.matchdict['traverse']) == 3:
            release_file = package[request.matchdict['traverse'][1]][
                request.matchdict['traverse'][2]]
            package_name, release_name, _ = request.matchdict['traverse']
            download_release_from_pypi.delay(package_name, release_name)
            return HTTPTemporaryRedirect(location=release_file.pypi_url)
    except KeyError:
        return HTTPNotFound()
    return view()
Ejemplo n.º 3
0
def download_release_from_pypi(config, package_name, release_name):
    try:
        repository_is_updated = False
        conn = get_connection(config)
        proxy = PyPiProxy(conn, package_name)
        root = repository_root_factory(conn)
        package = proxy.build_repository(release_name=release_name,
                                         with_metadata=True)
        if not package:
            logger.error('Package {} not found on PYPI'.format(package_name))
        for release_file in package[release_name].release_files.values():
            logger.info('Download file "{}"'.format(release_file.filename))
            release_file.set_content(
                requests.get(release_file.pypi_url).content)
            with release_file.content.open() as content:
                binary_content = content.read()
                if hashlib.md5(
                        binary_content).hexdigest() != release_file.md5_digest:
                    continue
            release_file.size = len(binary_content)
            repository_is_updated = True

        package = proxy.smart_merge(root, package)
        if repository_is_updated:
            root[package.name] = package
            for release in package:
                release.__parent__ = package
                for release_file in release:
                    release_file.__parent__ = release

        transaction.commit()
    except:
        transaction.abort()
Ejemplo n.º 4
0
    def test_build_repository_404_error(self, mock):
        from papaye.proxy import PyPiProxy
        mock.return_value = FakeGRequestResponse(404, b'')

        proxy = PyPiProxy(self.request, 'pyramid')
        result = proxy.build_repository()

        self.assertIsNone(result)
Ejemplo n.º 5
0
    def test_build_repository_404_error(self, mock):
        from papaye.proxy import PyPiProxy
        mock.return_value = FakeGRequestResponse(404, b'')

        proxy = PyPiProxy(self.request, 'pyramid')
        result = proxy.build_repository()

        self.assertIsNone(result)
Ejemplo n.º 6
0
    def test_build_repository(self, mock):
        from papaye.proxy import PyPiProxy
        from papaye.models import Package, ReleaseFile
        mock.return_value = self.pypi_response
        info_dict = json.loads(self.pypi_response.content.decode('utf-8'))

        proxy = PyPiProxy(self.request, 'pyramid')
        result = proxy.build_repository()

        self.assertIsInstance(result, Package)
        for release in result.releases.values():
            self.assertIn(release.__name__, info_dict['releases'].keys())
        self.assertEqual(len(result.releases.keys()), len(info_dict['releases'].keys()))
        self.assertEqual(len(result['1.5'].release_files.keys()), 1)
        self.assertIsInstance(result['1.5'].release_files.values()[0], ReleaseFile)
        self.assertTrue(getattr(result['1.5'].release_files.values()[0], 'pypi_url', None))
        self.assertEqual(result['1.5'].release_files.values()[0].pypi_url,
                         "https://pypi.python.org/packages/source/p/pyramid/pyramid-1.5.tar.gz")
        self.assertIsNotNone(result['1.5'].metadata)
        self.assertIsInstance(result['1.5'].metadata, dict)
Ejemplo n.º 7
0
def download_release_from_pypi(config, package_name, release_name):
    conn = get_connection(config)
    proxy = PyPiProxy(conn, package_name)
    package = proxy.build_repository(release_name=release_name)
    if not package:
        logger.error('Package {} not found on PYPI'.format(package_name))
    root = repository_root_factory(conn)
    for release_file in package[release_name].release_files.values():
        logger.info('Download file "{}"'.format(release_file.filename))
        release_file.set_content(requests.get(release_file.pypi_url).content)
        with release_file.content.open() as content:
            binary_content = content.read()
            if hashlib.md5(binary_content).hexdigest() != release_file.md5_digest:
                raise IOError('md5 check error')
        release_file.size = len(binary_content)
    root[package.name] = package
    try:
        transaction.commit()
    except:
        transaction.abort()
Ejemplo n.º 8
0
def not_found(request):
    settings = request.registry.settings
    proxy = settings.get('papaye.proxy', False)
    proxy = True if proxy and proxy == 'true' else False
    if not proxy:
        return HTTPNotFound()
    try:
        proxy = PyPiProxy(request, request.matchdict['traverse'][0])
        package = proxy.build_repository()
        if not package:
            return HTTPNotFound()
        if len(request.matchdict['traverse']) == 1:
            view = ListReleaseFileView(package, request)
        elif len(request.matchdict['traverse']) == 2:
            context = package[request.matchdict['traverse'][1]]
            view = ListReleaseFileByReleaseView(context, request)
        elif len(request.matchdict['traverse']) == 3:
            release_file = package[request.matchdict['traverse'][1]][request.matchdict['traverse'][2]]
            package_name, release_name, _ = request.matchdict['traverse']
            download_release_from_pypi.delay(package_name, release_name)
            return HTTPTemporaryRedirect(location=release_file.pypi_url)
    except KeyError:
        return HTTPNotFound()
    return view()