Exemplo n.º 1
0
def pypi_stub():
    with requests_mock.Mocker(real_http=True) as rm:
        wildcard_re = re.compile('^https://pypi\.internal/.*')
        rm.register_uri('GET', wildcard_re, status_code=404)

        pypi_dir = os.path.join(os.path.dirname(__file__), 'pypi_data')
        for filename in os.listdir(pypi_dir):
            with open(os.path.join(pypi_dir, filename), 'rb') as fh:
                content = fh.read()

            name, ext = os.path.splitext(filename)
            url = 'https://pypi.internal/pypi/%s/json' % name
            rm.register_uri('GET', url, content=content)

            # Register the alternative urls and redirect to original url
            for alt_name in get_search_names(name):
                if alt_name != name:
                    alt_url = 'https://pypi.internal/pypi/%s/json' % alt_name
                    rm.register_uri('GET',
                                    alt_url,
                                    headers={
                                        'Location': url,
                                    },
                                    status_code=301)

        yield 'https://pypi.internal/pypi/'
Exemplo n.º 2
0
def pypi_stub():
    with requests_mock.Mocker(real_http=True) as rm:
        wildcard_re = re.compile('^https://pypi\.internal/.*')
        rm.register_uri('GET', wildcard_re, status_code=404)

        pypi_dir = os.path.join(os.path.dirname(__file__), 'pypi_data')
        for filename in os.listdir(pypi_dir):
            with open(os.path.join(pypi_dir, filename), 'rb') as fh:
                content = fh.read()

            name, ext = os.path.splitext(filename)
            url = 'https://pypi.internal/pypi/%s/json' % name
            rm.register_uri('GET', url, content=content)

            # Register the alternative urls and redirect to original url
            for alt_name in get_search_names(name):
                if alt_name != name:
                    alt_url = 'https://pypi.internal/pypi/%s/json' % alt_name
                    rm.register_uri(
                        'GET',
                        alt_url,
                        headers={
                            'Location': url,
                        },
                        status_code=301)

        yield 'https://pypi.internal/pypi/'
Exemplo n.º 3
0
    def get(self, request, repo, slug):
        condition = Q()
        for name in get_search_names(slug):
            condition |= Q(name__iexact=name)

        try:
            package = self.repository.packages.get(condition)
        except ObjectDoesNotExist:
            if not self.repository.enable_auto_mirroring:
                raise Http404("Auto mirroring is not enabled")

            enqueue(fetch_package, self.repository.pk, slug)
            return redirect(self.repository.upstream_pypi_url + '/' + slug)

        # Redirect if slug is not an exact match
        if slug != package.name:
            url = reverse('packages:simple_detail',
                          kwargs={
                              'repo': self.repository.slug,
                              'slug': package.name
                          })
            return redirect(url)

        self.object = package
        context = self.get_context_data(object=self.object,
                                        releases=list(package.releases.all()))
        return self.render_to_response(context)
Exemplo n.º 4
0
    def get(self, request, slug, version=None):
        condition = Q()
        for name in get_search_names(slug):
            condition |= Q(name__iexact=name)

        try:
            package = models.Package.objects.get(condition)
        except ObjectDoesNotExist:
            package = get_package_data(slug)

        if package is None:
            raise Http404

        # Redirect if slug is not an exact match
        if slug != package.name:
            url = reverse('packages-simple:simple_detail', kwargs={
                'slug': package.name, 'version': version
            })
            return redirect(url)

        releases = package.releases
        if version and not package.is_local:
            releases = releases.filter(version=version)

            # Perhaps this version is new, refresh data
            if releases.count() == 0:
                get_package_data(slug, package)

        self.object = package
        context = self.get_context_data(
            object=self.object,
            releases=list(releases.all()))
        return self.render_to_response(context)
Exemplo n.º 5
0
    def get(self, request, slug, version=None):
        condition = Q()
        for name in get_search_names(slug):
            condition |= Q(name__iexact=name)

        try:
            package = models.Package.objects.get(condition)
        except ObjectDoesNotExist:
            package = get_package_data(slug)

        if package is None:
            raise Http404

        # Redirect if slug is not an exact match
        if slug != package.name:
            url = reverse('packages-simple:simple_detail', kwargs={
                'slug': package.name, 'version': version
            })
            return redirect(url)

        releases = package.releases
        if version and not package.is_local:
            releases = releases.filter(version=version)

            # Perhaps this version is new, refresh data
            if releases.count() == 0:
                get_package_data(slug, package)

        self.object = package
        context = self.get_context_data(
            object=self.object,
            releases=list(releases.all()))
        return self.render_to_response(context)
Exemplo n.º 6
0
    def get(self, request, slug):
        condition = Q()
        for name in get_search_names(slug):
            condition |= Q(name__iexact=name)

        try:
            package = models.Package.objects.get(condition)
        except ObjectDoesNotExist:
            enqueue(fetch_package, slug)
            return redirect('https://pypi.python.org/simple/{}'.format(slug))

        # Redirect if slug is not an exact match
        if slug != package.name:
            url = reverse('packages-simple:simple_detail',
                          kwargs={'slug': package.name})
            return redirect(url)

        self.object = package
        context = self.get_context_data(
            object=self.object,
            releases=list(package.releases.all()))
        return self.render_to_response(context)
Exemplo n.º 7
0
def handle_register_or_upload(post_data, files, user, repository):
    """Process a `register` or `upload` comment issued via distutils.

    This method is called with the authenticated user.

    """
    name = post_data.get('name')
    version = post_data.get('version')

    if settings.LOCALSHOP_VERSIONING_TYPE:
        scheme = get_versio_versioning_scheme(
            settings.LOCALSHOP_VERSIONING_TYPE)
        try:
            Version(version, scheme=scheme)
        except AttributeError:
            response = HttpResponseBadRequest(
                reason="Invalid version supplied '{!s}' for '{!s}' scheme.".
                format(version, settings.LOCALSHOP_VERSIONING_TYPE))
            return response

    if not name or not version:
        logger.info("Missing name or version for package")
        return HttpResponseBadRequest('No name or version given')

    try:
        condition = Q()
        for search_name in get_search_names(name):
            condition |= Q(name__iexact=search_name)

        package = repository.packages.get(condition)

        # Error out when we try to override a mirror'ed package for now
        # not sure what the best thing is
        if not package.is_local:
            return HttpResponseBadRequest('%s is a pypi package!' %
                                          package.name)

        try:
            release = package.releases.get(version=version)
        except ObjectDoesNotExist:
            release = None
    except ObjectDoesNotExist:
        package = None
        release = None

    # Validate the data
    form = forms.ReleaseForm(post_data, instance=release)
    if not form.is_valid():
        return HttpResponseBadRequest(reason=form.errors.values()[0][0])

    if not package:
        pkg_form = forms.PackageForm(post_data, repository=repository)
        if not pkg_form.is_valid():
            return HttpResponseBadRequest(
                reason=six.next(six.itervalues(pkg_form.errors))[0])
        package = pkg_form.save()

    release = form.save(commit=False)
    release.package = package
    release.save()

    # If this is an upload action then process the uploaded file
    if files:
        filename = files['distribution']._name
        try:
            release_file = release.files.get(filename=filename)
            if settings.LOCALSHOP_RELEASE_OVERWRITE is False:
                message = 'That it already released, please bump version.'
                return HttpResponseBadRequest(message)
        except ObjectDoesNotExist:
            release_file = models.ReleaseFile(release=release,
                                              filename=filename)

        form_file = forms.ReleaseFileForm(post_data,
                                          files,
                                          instance=release_file)
        if not form_file.is_valid():
            return HttpResponseBadRequest('ERRORS %s' % form_file.errors)
        release_file = form_file.save(commit=False)
        release_file.save()

    return HttpResponse()
Exemplo n.º 8
0
def handle_register_or_upload(post_data, files, user):
    """Process a `register` or `upload` comment issued via distutils.

    This method is called with the authenticated user.

    """
    name = post_data.get('name')
    version = post_data.get('version')
    if not name or not version:
        logger.info("Missing name or version for package")
        return HttpResponseBadRequest('No name or version given')

    try:
        condition = Q()
        for search_name in get_search_names(name):
            condition |= Q(name__iexact=search_name)

        package = models.Package.objects.get(condition)

        # Error out when we try to override a mirror'ed package for now
        # not sure what the best thing is
        if not package.is_local:
            return HttpResponseBadRequest(
                '%s is a pypi package!' % package.name)

        # Ensure that the user is one of the owners
        if not package.owners.filter(pk=user.pk).exists():
            if not user.is_superuser:
                return HttpResponseForbidden('No permission for this package')

            # User is a superuser, add him to the owners
            package.owners.add(user)

        try:
            release = package.releases.get(version=version)
        except ObjectDoesNotExist:
            release = None
    except ObjectDoesNotExist:
        package = None
        release = None

    # Validate the data
    form = forms.ReleaseForm(post_data, instance=release)
    if not form.is_valid():
        return HttpResponseBadRequest('ERRORS %s' % form.errors)

    if not package:
        package = models.Package.objects.create(name=name, is_local=True)
        package.owners.add(user)
        package.save()

    release = form.save(commit=False)
    release.package = package
    release.user = user
    release.save()

    # If this is an upload action then process the uploaded file
    if files:
        filename = files['distribution']._name
        try:
            release_file = release.files.get(filename=filename)
            if settings.LOCALSHOP_RELEASE_OVERWRITE is False:
                message = 'That it already released, please bump version.'
                return HttpResponseBadRequest(message)
        except ObjectDoesNotExist:
            release_file = models.ReleaseFile(
                release=release, filename=filename, user=user)

        form_file = forms.ReleaseFileForm(
            post_data, files, instance=release_file)
        if not form_file.is_valid():
            return HttpResponseBadRequest('ERRORS %s' % form_file.errors)
        release_file = form_file.save(commit=False)
        release_file.user = user
        release_file.save()

    return HttpResponse()