Example #1
0
def not_found(request, stop=None):
    if not proxy_activated(request.registry.settings) or stop is not None:
        return HTTPNotFound()
    proxy = PyPiProxy()
    package_name = request.matchdict['traverse'][0]
    if package_name in request.root:
        local_package = request.root[package_name]
    else:
        local_package = Package(package_name)
        local_package.fake = True

    merged_repository = proxy.merged_repository(local_package)

    if merged_repository:
        package = merged_repository[package_name]
        traversed = len(request.matchdict['traverse'])
        if traversed == 1:
            view = ListReleaseFileView(package, request)
            view.stop = True
        elif traversed == 2:
            context = package[request.matchdict['traverse'][1]]
            view = ListReleaseFileByReleaseView(context, request)
        elif traversed == 3:
            release_file = package[request.matchdict['traverse'][1]][request.matchdict['traverse'][2]]
            filename = request.matchdict['traverse'][2]
            package_name, release_name, _ = request.matchdict['traverse']
            download_release_from_pypi.delay(request.registry._zodb_databases[''], package_name, release_name, filename)
            return HTTPTemporaryRedirect(location=release_file.pypi_url)
        return view()
    return HTTPNotFound()
Example #2
0
    def test_merged_repository_without_repository_package(self, mock):
        from papaye.models import Package, Release, ReleaseFile, Root
        from papaye.proxy import PyPiProxy
        mock.return_value = FakeGRequestResponse(404, b'')
        package1 = Package(name='pyramid')
        package1['100.5'] = Release(version='100.5', metadata={})
        package1['100.5']['pyramid-100.5.tar.gz'] = ReleaseFile(
            filename='pyramid-100.5.tar.gz',
            content=b'a existing content',
            md5_digest='12345'
        )
        proxy = PyPiProxy()
        result = proxy.merged_repository(package1, metadata=True)

        assert isinstance(result, Root)
        assert [pack.__name__ for pack in result] == ["pyramid", ]
        assert len(list(result['pyramid'])) == 1
Example #3
0
 def __call__(self):
     package = self.context
     proxy = PyPiProxy()
     stop = hasattr(self, 'stop') and self.stop
     repository = package.__parent__ if stop else proxy.merged_repository(package)
     rfiles = [rfile for rel in repository[package.__name__] for rfile in rel]
     context = {'objects': ((self.request.resource_url(
         rfile,
         route_name='simple'
     )[:-1] + "#md5={}".format(rfile.md5_digest), rfile) for rfile in rfiles)}
     transaction.abort()
     if len(rfiles):
         return context
     elif stop:
         return HTTPNotFound()
     else:
         return not_found(self.request)
Example #4
0
class ListReleaseFileView(BaseView):
    _repository = None

    def __init__(self, context, request):
        super().__init__(context, request)
        self.proxy_instance = PyPiProxy()
        self.stop = hasattr(self, 'stop') and self.stop

    @property
    def repository(self):
        if self._repository is None:
            if self.stop:
                self._repository = self.context.root
            else:
                self._repository = self.proxy_instance.merged_repository(
                    self.context
                )
        return self._repository

    def __call__(self):
        package = self.context
        root = package.root
        if self.repository is not root:
            self.repository.name = root.name
        rfiles = [
            rfile for rel in self.repository[package.name]
            for rfile in rel
        ]
        context = {
            'objects': (self.format_release_file(rfile) for rfile in rfiles)
        }
        transaction.abort()
        if len(rfiles):
            return context
        elif self.stop:
            return HTTPNotFound()
        else:
            return not_found(self.request)

    def format_release_file(self, release_file):
        return self.request.resource_url(
            release_file,
            route_name='simple'
        )[:-1] + "#md5={}".format(release_file.md5_digest), release_file
Example #5
0
    def test_merged_repository_with_release_name(self, mock):
        from papaye.models import Package, Release, ReleaseFile, Root
        from papaye.proxy import PyPiProxy
        mock_proxy_response(mock)
        package1 = Package(name='pyramid')
        package1['100.5'] = Release(version='100.5', metadata={})
        package1['100.5']['pyramid-100.5.tar.gz'] = ReleaseFile(
            filename='pyramid-100.5.tar.gz',
            content=b'a existing content',
            md5_digest='12345'
        )
        proxy = PyPiProxy()
        result = proxy.merged_repository(package1, release_name='1.5')

        assert isinstance(result, Root)
        assert [pack.__name__ for pack in result] == ["pyramid", ]
        assert len(list(result['pyramid'])) == 2
        assert '1.5' in result['pyramid'].releases.keys()
        assert '100.5' in [rel for rel in result['pyramid'].releases]