Ejemplo n.º 1
0
 def setUpClass(cls):
     super().setUpClass()
     cls.repo = Repository(archive_type=Maven3.archive_type,
                           name='test_maven3')
     cls.repo.save()
     cls.elts = [
         Element(
             version='1.1.4c',
             archive='xpp3.xpp3_min',
             name='xpp3_min',
             filename='xpp3_min-1.1.4c.jar',
             full_name='xpp3_min-1.1.4c.jar',
             sha1='19d4e90b43059058f6e056f794f0ea4030d60b86',
         ),
         Element(
             version='1.0-2',
             archive='javax.xml.stream.stax-api',
             name='stax-api',
             filename='stax-api-1.0-2.jar',
             full_name='stax-api-1.0-2.jar',
             sha1='d6337b0de8b25e53e81b922352fbea9f9f57ba0b',
         ),
         Element(
             version='1.0-2',
             archive='javax.xml.stream.stax-api',
             name='stax-api',
             filename='stax-api-1.0-2.pom',
             full_name='stax-api-1.0-2.pom',
             sha1='5379b69f557c5ab7c144d22bf7c3768bd2adb93d',
         ),
         Element(
             version='2.2.2',
             archive='javax.xml.bind.jaxb-api',
             name='jaxb-api',
             filename='jaxb-api-2.2.2.jar',
             full_name='jaxb-api-2.2.2.jar',
             sha1='aeb3021ca93dde265796d82015beecdcff95bf09',
         ),
         Element(
             version='2.2.2',
             archive='javax.xml.bind.jaxb-api',
             name='jaxb-api',
             filename='jaxb-api-2.2.2.pom',
             full_name='jaxb-api-2.2.2.pom',
             sha1='a8368234f7555dd64d3a9060a0b02e6c215694fb',
         ),
     ]
     for elt in cls.elts:
         elt.repository = cls.repo
     Element.objects.bulk_create(cls.elts)
Ejemplo n.º 2
0
def add_element_signature(request: HttpRequest, rid):
    from moneta.repository.forms import SignatureForm

    if request.method != 'POST':
        return TemplateResponse(request,
                                'moneta/not_allowed.html', {},
                                status=405)
    form = SignatureForm(request.GET)
    if not form.is_valid():
        return TemplateResponse(request,
                                'moneta/not_allowed.html', {},
                                status=405)
    signature = base64.b64encode(request.read(16384))
    sha256 = form.cleaned_data['sha256']
    method = form.cleaned_data['method']
    # noinspection PyUnresolvedReferences
    user = None if request.user.is_anonymous else request.user
    element = get_object_or_404(Element.reader_queryset(request),
                                repository__id=rid,
                                sha256=sha256,
                                author=user)
    ElementSignature(element=element, signature=signature,
                     method=method).save()
    return HttpResponse(
        _('This signature has been added to %(filename)s') %
        {'filename': element.filename})
Ejemplo n.º 3
0
def generic_add_element(request: HttpRequest,
                        repo,
                        uploaded_file,
                        state_names,
                        archive=None,
                        name=None,
                        version=None):
    """ Generic upload (both form-based and raw POST-based methods)
    :param archive:
    :param name:
    :param version:
    :param request:
    :param repo: Repository
    :param uploaded_file: UploadedFile
    :param state_names: iterable of ArchiveState.name
    :return: successfully added Element
    """
    sha256 = hashlib.sha256()
    data = uploaded_file.file.read(4096)
    while data:
        sha256.update(data)
        data = uploaded_file.file.read(4096)
    sha256_sum = sha256.hexdigest()
    uploaded_file.file.seek(0)
    if uploaded_file.name:
        filename = os.path.basename(uploaded_file.name)
        elements = list(
            Element.objects.filter(repository=repo, filename=filename)[0:1])
    elif name:
        name = os.path.basename(name)
        elements = list(
            Element.objects.filter(repository=repo, name=name)[0:1])
    else:
        elements = list(
            Element.objects.filter(repository=repo, sha256=sha256_sum)[0:1])
    if elements:
        element = elements[0]
    else:
        # noinspection PyUnresolvedReferences
        user = None if request.user.is_anonymous else request.user
        element = Element(repository=repo, author=user)
    if archive:
        element.archive = archive
    if name:
        element.name = os.path.basename(name)
    if version:
        element.version = version
    element.archive_file = uploaded_file
    # form.cleaned_data['package'].file.name = form.cleaned_data['package'].name
    element.save()
    cls = element.repository.get_model()
    states = list(
        ArchiveState.objects.filter(repository=repo, slug__in=state_names))
    cls.finish_element(element, states)
    return element
Ejemplo n.º 4
0
def show_file(request: HttpRequest, eid):
    q = Element.reader_queryset(request).filter(id=eid).select_related()[0:1]
    elements = list(q)
    if len(elements) == 0:
        raise Http404
    element = elements[0]
    template_values = {'element': element, 'repo': element.repository,
                       'upload_allowed': element.repository.upload_allowed(request)}
    return render_to_response('moneta/show_package.html', template_values, RequestContext(request))
Ejemplo n.º 5
0
def show_file(request: HttpRequest, eid):
    q = Element.reader_queryset(request).filter(id=eid).select_related()[0:1]
    elements = list(q)
    if len(elements) == 0:
        raise Http404
    element = elements[0]
    template_values = {
        'element': element,
        'repo': element.repository,
        'upload_allowed': element.repository.upload_allowed(request)
    }
    return render_to_response('moneta/show_package.html', template_values,
                              RequestContext(request))
Ejemplo n.º 6
0
def add_element_signature(request: HttpRequest, rid):
    from moneta.repository.forms import SignatureForm

    if request.method != 'POST':
        return render_to_response('moneta/not_allowed.html', status=405)
    form = SignatureForm(request.GET)
    if not form.is_valid():
        return render_to_response('moneta/not_allowed.html', status=405)
    signature = base64.b64encode(request.read(16384))
    sha256 = form.cleaned_data['sha256']
    method = form.cleaned_data['method']
    user = None if request.user.is_anonymous() else request.user
    element = get_object_or_404(Element.reader_queryset(request), repository__id=rid, sha256=sha256, author=user)
    ElementSignature(element=element, signature=signature, method=method).save()
    return HttpResponse(_('This signature has been added to %(filename)s') % {'filename': element.filename})
Ejemplo n.º 7
0
def generic_add_element(request: HttpRequest, repo, uploaded_file, state_names, archive=None, name=None, version=None):
    """ Generic upload (both form-based and raw POST-based methods)
    :param archive:
    :param name:
    :param version:
    :param request:
    :param repo: Repository
    :param uploaded_file: UploadedFile
    :param state_names: iterable of ArchiveState.name
    :return: successfully added Element
    """
    sha256 = hashlib.sha256()
    data = uploaded_file.file.read(4096)
    while data:
        sha256.update(data)
        data = uploaded_file.file.read(4096)
    sha256_sum = sha256.hexdigest()
    uploaded_file.file.seek(0)
    if uploaded_file.name:
        filename = os.path.basename(uploaded_file.name)
        elements = list(Element.objects.filter(repository=repo, filename=filename)[0:1])
    elif name:
        name = os.path.basename(name)
        elements = list(Element.objects.filter(repository=repo, name=name)[0:1])
    else:
        elements = list(Element.objects.filter(repository=repo, sha256=sha256_sum)[0:1])
    if elements:
        element = elements[0]
    else:
        # noinspection PyUnresolvedReferences
        user = None if request.user.is_anonymous else request.user
        element = Element(repository=repo, author=user)
    if archive:
        element.archive = archive
    if name:
        element.name = os.path.basename(name)
    if version:
        element.version = version
    element.archive_file = uploaded_file
    # form.cleaned_data['package'].file.name = form.cleaned_data['package'].name
    element.save()
    cls = element.repository.get_model()
    states = list(ArchiveState.objects.filter(repository=repo, slug__in=state_names))
    cls.finish_element(element, states)
    return element
Ejemplo n.º 8
0
def get_file(request: HttpRequest,
             eid: int,
             compression: str = None,
             path: str = '',
             element: Element = None,
             name: str = None):
    """
    Send file to the client as a HttpResponse
    Multiple combinations:
        * case 1) if path != '' => send a file inside a compressed archive
        * case 2) elif compression is not None => required uncompressed archive to compress it to the new format
        * case 3) else => require original file

    :param request:
    :param eid:
    :param compression:
    :param path:
    :param element: avoid an extra DB query to fetch 
    :param name:
    :return:
    """
    # noinspection PyUnusedLocal
    name = name
    if element is None:
        element = get_object_or_404(
            Element.reader_queryset(request).select_related(), id=eid)
    arc_storage, arc_key, arc_path = None, None, None
    mimetype = 'application/octet-stream'
    if element.uncompressed_key and path:  # case 1
        path = os.path.normpath(path)
        if path.startswith('../'):
            raise Http404
    elif element.uncompressed_key and compression is not None:  # case 2
        arc_storage, arc_key, arc_path = storage(
            settings.STORAGE_UNCOMPRESSED), element.uncompressed_key, path
    elif element.archive_key:  # case 2 or 3
        if compression is not None:  # case 2
            arc_storage, arc_key, arc_path = storage(
                settings.STORAGE_ARCHIVE), element.archive_key, ''
    else:
        raise Http404
    if arc_storage is not None:
        temp_file = tempfile.TemporaryFile(mode='w+b', dir=settings.TEMP_ROOT)
        comp_file = None
        ext = ''
        if compression == 'zip':
            mimetype = 'application/zip'
            ext = '.zip'
            comp_file = zipfile.ZipFile(temp_file, 'w', zipfile.ZIP_DEFLATED)
        elif compression == 'tgz':
            mimetype = 'application/x-tar'
            ext = '.tgz'
            comp_file = tarfile.open(None, 'w:gz', fileobj=temp_file)
            comp_file.write = comp_file.addfile
        elif compression == 'tbz':
            mimetype = 'application/x-tar'
            ext = '.tbz'
            comp_file = tarfile.open(None, 'w:bz2', fileobj=temp_file)
            comp_file.write = comp_file.addfile
        reldir = None
        for root, dirs, files in arc_storage.walk(arc_key, arc_path):
            if reldir is None:
                reldir = root
            for name in files:
                fullname = os.path.join(root, name)
                fileobj = arc_storage.get_file(arc_key, fullname)
                arcname = os.path.relpath(fullname, reldir)
                tarinfo = tarfile.TarInfo(arcname)
                tarinfo.size = arc_storage.get_size(arc_key, fullname)
                comp_file.write(tarinfo, fileobj)
        comp_file.close()
        temp_file.seek(0)
        fileobj = temp_file
        filename = os.path.basename(element.filename) + ext
    elif path:
        mimetype = mimetypes.guess_type(path)[0]
        if mimetype is None:
            mimetype = 'application/octet-stream'
        return sendpath(settings.STORAGE_UNCOMPRESSED,
                        element.uncompressed_key, path, mimetype)
    else:
        return sendpath(settings.STORAGE_ARCHIVE, element.archive_key, '',
                        element.mimetype)
    response = StreamingHttpResponse(read_file_in_chunks(fileobj),
                                     content_type=mimetype)
    if mimetype[0:4] != 'text' and mimetype[0:5] != 'image':
        response['Content-Disposition'] = 'attachment; filename={0}'.format(
            filename)
    return response
Ejemplo n.º 9
0
def get_signature(request: HttpRequest, eid, sid):
    element = get_object_or_404(Element.reader_queryset(request), id=eid)
    signature = get_object_or_404(ElementSignature, element=element, id=sid)
    value = base64.b64decode(signature.signature)
    mimetype = 'application/pgp-signature' if signature.method == signature.GPG else 'application/x509'
    return HttpResponse(value, content_type=mimetype)
Ejemplo n.º 10
0
def get_checksum(request: HttpRequest, eid, value):
    element = get_object_or_404(Element.reader_queryset(request), id=eid)
    value = getattr(element, value)
    return HttpResponse('%s  %s\n' % (value, element.filename),
                        content_type='text/plain')
Ejemplo n.º 11
0
def get_file(request: HttpRequest, eid: int, compression: str=None, path: str='', element: Element=None,
             name: str=None):
    """
    Send file to the client as a HttpResponse
    Multiple combinations:
        * case 1) if path != '' => send a file inside a compressed archive
        * case 2) elif compression is not None => required uncompressed archive to compress it to the new format
        * case 3) else => require original file

    :param request:
    :param eid:
    :param compression:
    :param path:
    :param element: avoid an extra DB query to fetch 
    :param name:
    :return:
    """
    # noinspection PyUnusedLocal
    name = name
    if element is None:
        element = get_object_or_404(Element.reader_queryset(request).select_related(), id=eid)
    arc_storage, arc_key, arc_path = None, None, None
    mimetype = 'application/octet-stream'
    if element.uncompressed_key and path:  # case 1
        path = os.path.normpath(path)
        if path.startswith('../'):
            raise Http404
    elif element.uncompressed_key and compression is not None:  # case 2
        arc_storage, arc_key, arc_path = storage(settings.STORAGE_UNCOMPRESSED), element.uncompressed_key, path
    elif element.archive_key:  # case 2 or 3
        if compression is not None:  # case 2
            arc_storage, arc_key, arc_path = storage(settings.STORAGE_ARCHIVE), element.archive_key, ''
    else:
        raise Http404
    if arc_storage is not None:
        temp_file = tempfile.TemporaryFile(mode='w+b', dir=settings.TEMP_ROOT)
        comp_file = None
        ext = ''
        if compression == 'zip':
            mimetype = 'application/zip'
            ext = '.zip'
            comp_file = zipfile.ZipFile(temp_file, 'w', zipfile.ZIP_DEFLATED)
        elif compression == 'tgz':
            mimetype = 'application/x-tar'
            ext = '.tgz'
            comp_file = tarfile.open(None, 'w:gz', fileobj=temp_file)
            comp_file.write = comp_file.addfile
        elif compression == 'tbz':
            mimetype = 'application/x-tar'
            ext = '.tbz'
            comp_file = tarfile.open(None, 'w:bz2', fileobj=temp_file)
            comp_file.write = comp_file.addfile
        reldir = None
        for root, dirs, files in arc_storage.walk(arc_key, arc_path):
            if reldir is None:
                reldir = root
            for name in files:
                fullname = os.path.join(root, name)
                fileobj = arc_storage.get_file(arc_key, fullname)
                arcname = os.path.relpath(fullname, reldir)
                tarinfo = tarfile.TarInfo(arcname)
                tarinfo.size = arc_storage.get_size(arc_key, fullname)
                comp_file.write(tarinfo, fileobj)
        comp_file.close()
        temp_file.seek(0)
        fileobj = temp_file
        filename = os.path.basename(element.filename) + ext
    elif path:
        mimetype = mimetypes.guess_type(path)[0]
        if mimetype is None:
            mimetype = 'application/octet-stream'
        return sendpath(settings.STORAGE_UNCOMPRESSED, element.uncompressed_key, path, mimetype)
    else:
        return sendpath(settings.STORAGE_ARCHIVE, element.archive_key, '', element.mimetype)
    response = StreamingHttpResponse(read_file_in_chunks(fileobj), content_type=mimetype)
    if mimetype[0:4] != 'text' and mimetype[0:5] != 'image':
        response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
    return response
Ejemplo n.º 12
0
def get_signature(request: HttpRequest, eid, sid):
    element = get_object_or_404(Element.reader_queryset(request), id=eid)
    signature = get_object_or_404(ElementSignature, element=element, id=sid)
    value = base64.b64decode(signature.signature)
    mimetype = 'application/pgp-signature' if signature.method == signature.GPG else 'application/x509'
    return HttpResponse(value, content_type=mimetype)
Ejemplo n.º 13
0
def get_checksum(request: HttpRequest, eid, value):
    element = get_object_or_404(Element.reader_queryset(request), id=eid)
    value = getattr(element, value)
    return HttpResponse('%s  %s\n' % (value, element.filename), content_type='text/plain')