Commit f920b2ca authored by Chris Lamb's avatar Chris Lamb 💬

Apply the black formatter to the source tree.

parent 1d0f8519
......@@ -2,7 +2,4 @@ from django.conf.urls import url
from . import views
urlpatterns = (
url(r'^api/submit$', views.submit,
name='submit'),
)
urlpatterns = (url(r'^api/submit$', views.submit, name='submit'),)
......@@ -15,7 +15,7 @@ from bidb.buildinfo.models import Buildinfo, Origin
SUPPORTED_FORMATS = {'0.2', '1.0'}
re_binary = re.compile(
r'^(?P<name>[^_]+)_(?P<version>[^_]+)_(?P<architecture>[^\.]+)\.u?deb$',
r'^(?P<name>[^_]+)_(?P<version>[^_]+)_(?P<architecture>[^\.]+)\.u?deb$'
)
re_installed_build_depends = re.compile(
r'^(?P<package>[^ ]+) \(= (?P<version>.+)\)'
......@@ -25,6 +25,7 @@ re_installed_build_depends = re.compile(
class InvalidSubmission(Exception):
pass
@transaction.atomic
def parse_submission(request):
raw_text = request.read()
......@@ -56,12 +57,11 @@ def parse_submission(request):
def create_submission(buildinfo):
submission = buildinfo.submissions.create(
key=Key.objects.get_or_create(uid=uid)[0],
key=Key.objects.get_or_create(uid=uid)[0]
)
default_storage.save(
submission.get_storage_name(),
ContentFile(raw_text),
submission.get_storage_name(), ContentFile(raw_text)
)
return submission
......@@ -77,7 +77,7 @@ def parse_submission(request):
if data.get('Format') not in SUPPORTED_FORMATS:
raise InvalidSubmission(
"Only {} 'Format:' versions are supported".format(
', '.join(sorted(SUPPORTED_FORMATS)),
', '.join(sorted(SUPPORTED_FORMATS))
)
)
......@@ -87,16 +87,15 @@ def parse_submission(request):
with transaction.atomic():
buildinfo = Buildinfo.objects.create(
sha1=sha1,
source=get_or_create(Source, 'Source'),
architecture=get_or_create(Architecture, 'Architecture'),
version=data['version'],
build_path=data.get('Build-Path', ''),
build_date=parse(data.get('Build-Date', '')),
build_origin=get_or_create(Origin, 'Build-Origin'),
build_architecture=get_or_create(Architecture, 'Build-Architecture'),
build_architecture=get_or_create(
Architecture, 'Build-Architecture'
),
environment=data.get('Environment', ''),
)
except IntegrityError:
......@@ -104,8 +103,7 @@ def parse_submission(request):
return create_submission(Buildinfo.objects.get(sha1=sha1)), False
default_storage.save(
buildinfo.get_storage_name(),
ContentFile(raw_text_gpg_stripped),
buildinfo.get_storage_name(), ContentFile(raw_text_gpg_stripped)
)
## Parse binaries #########################################################
......@@ -122,7 +120,7 @@ def parse_submission(request):
for x in binary_names:
# Save instances for lookup later
binaries[x] = buildinfo.binaries.create(
binary=Binary.objects.get_or_create(name=x)[0],
binary=Binary.objects.get_or_create(name=x)[0]
)
## Parse checksums ########################################################
......@@ -141,18 +139,19 @@ def parse_submission(request):
raise ValueError()
except ValueError:
raise InvalidSubmission(
"Invalid size for {}: {}".format(filename, size),
"Invalid size for {}: {}".format(filename, size)
)
checksums.setdefault(filename, {
'size': size,
'binary': None,
})['checksum_{}'.format(x.lower())] = checksum
checksums.setdefault(filename, {'size': size, 'binary': None})[
'checksum_{}'.format(x.lower())
] = checksum
existing = checksums[filename]['size']
if size != existing:
raise InvalidSubmission("Mismatched file size in "
"Checksums-{}: {} != {}".format(x, existing, size))
raise InvalidSubmission(
"Mismatched file size in "
"Checksums-{}: {} != {}".format(x, existing, size)
)
## Create Checksum instances ##############################################
......@@ -171,7 +170,7 @@ def parse_submission(request):
if m is None:
raise InvalidSubmission(
"Invalid entry in Installed-Build-Depends: {}".format(x),
"Invalid entry in Installed-Build-Depends: {}".format(x)
)
return create_submission(buildinfo), True
......@@ -14,7 +14,9 @@ def submit(request):
except InvalidSubmission as exc:
return HttpResponseBadRequest("Rejecting submission: {}\n".format(exc))
return HttpResponse('{}{}\n'.format(
settings.SITE_URL,
submission.buildinfo.get_absolute_url(),
), status=201 if created else 200)
return HttpResponse(
'{}{}\n'.format(
settings.SITE_URL, submission.buildinfo.get_absolute_url()
),
status=201 if created else 200,
)
......@@ -7,14 +7,13 @@ from django.utils.crypto import get_random_string
class Submission(models.Model):
buildinfo = models.ForeignKey(
'buildinfo.Buildinfo',
related_name='submissions',
'buildinfo.Buildinfo', related_name='submissions'
)
slug = models.CharField(
unique=True,
default=functools.partial(
get_random_string, 8, '3479abcdefghijkmnopqrstuvwxyz',
get_random_string, 8, '3479abcdefghijkmnopqrstuvwxyz'
),
max_length=8,
)
......@@ -28,22 +27,16 @@ class Submission(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"pk=%d buildinfo=%r" % (
self.pk,
self.buildinfo,
)
return u"pk=%d buildinfo=%r" % (self.pk, self.buildinfo)
@models.permalink
def get_absolute_url(self):
return 'buildinfo:submissions:view', (
self.buildinfo.sha1,
self.buildinfo.get_filename(),
self.slug,
return (
'buildinfo:submissions:view',
(self.buildinfo.sha1, self.buildinfo.get_filename(), self.slug),
)
def get_storage_name(self):
return 'buildinfo_submissions.Submission/{}/{}/{}'.format(
self.slug[:2],
self.slug[2:4],
self.slug,
self.slug[:2], self.slug[2:4], self.slug
)
......@@ -3,6 +3,9 @@ from django.conf.urls import url
from . import views
urlpatterns = (
url(r'^(?P<sha1>\w{40})/(?P<filename>[^/]+)/(?P<slug>\w+).buildinfo$', views.view,
name='view'),
url(
r'^(?P<sha1>\w{40})/(?P<filename>[^/]+)/(?P<slug>\w+).buildinfo$',
views.view,
name='view',
),
)
......@@ -6,11 +6,7 @@ from .models import Submission
def view(request, sha1, filename, slug):
submission = get_object_or_404(
Submission,
slug=slug,
buildinfo__sha1=sha1,
)
submission = get_object_or_404(Submission, slug=slug, buildinfo__sha1=sha1)
if submission.buildinfo.get_filename() != filename:
return redirect(submission)
......
......@@ -6,14 +6,10 @@ from django.db import models
class Buildinfo(models.Model):
sha1 = models.CharField(max_length=40, unique=True)
source = models.ForeignKey(
'packages.Source',
related_name='buildinfos',
)
source = models.ForeignKey('packages.Source', related_name='buildinfos')
architecture = models.ForeignKey(
'packages.Architecture',
related_name='buildinfos',
'packages.Architecture', related_name='buildinfos'
)
version = models.CharField(max_length=200)
......@@ -21,8 +17,7 @@ class Buildinfo(models.Model):
build_date = models.DateTimeField(null=True)
build_origin = models.ForeignKey('Origin', null=True)
build_architecture = models.ForeignKey(
'packages.Architecture',
related_name='buildinfos_build',
'packages.Architecture', related_name='buildinfos_build'
)
environment = models.TextField()
......@@ -57,20 +52,15 @@ class Buildinfo(models.Model):
def get_storage_name(self):
return 'buildinfo.Buildinfo/{}/{}/{}'.format(
self.sha1[:2],
self.sha1[2:4],
self.sha1,
self.sha1[:2], self.sha1[2:4], self.sha1
)
class Binary(models.Model):
buildinfo = models.ForeignKey(
Buildinfo,
related_name='binaries',
)
buildinfo = models.ForeignKey(Buildinfo, related_name='binaries')
binary = models.ForeignKey(
'packages.Binary',
related_name='generated_binaries',
'packages.Binary', related_name='generated_binaries'
)
created = models.DateTimeField(default=datetime.datetime.utcnow)
......@@ -78,15 +68,11 @@ class Binary(models.Model):
class Meta:
ordering = ('binary__name',)
get_latest_by = 'created'
unique_together = (
('buildinfo', 'binary'),
)
unique_together = (('buildinfo', 'binary'),)
def __unicode__(self):
return u"pk=%d binary=%r" % (
self.pk,
self.binary,
)
return u"pk=%d binary=%r" % (self.pk, self.binary)
class Checksum(models.Model):
"""
......@@ -110,15 +96,11 @@ class Checksum(models.Model):
class Meta:
ordering = ('-created',)
get_latest_by = 'created'
unique_together = (
('buildinfo', 'filename'),
)
unique_together = (('buildinfo', 'filename'),)
def __unicode__(self):
return u"pk=%d filename=%r" % (
self.pk,
self.filename,
)
return u"pk=%d filename=%r" % (self.pk, self.filename)
class Origin(models.Model):
name = models.CharField(max_length=255)
......@@ -130,7 +112,4 @@ class Origin(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"pk=%d name=%r" % (
self.pk,
self.name,
)
return u"pk=%d name=%r" % (self.pk, self.name)
......@@ -4,16 +4,23 @@ from . import views
urlpatterns = (
url(r'', include('bidb.buildinfo.buildinfo_submissions.urls',
namespace='submissions')),
url(r'^(?P<sha1>\w{40})$', views.view,
name='view'),
url(r'^(?P<sha1>\w{40})/(?P<filename>.+)\.buildinfo$', views.raw_text,
name='raw-text'),
url(r'^(?P<sha1>\w{40})/(?P<filename>.+)$', views.view,
name='view'),
url(r'^api/v1/buildinfos/checksums/sha1/(?P<sha1>\w{40})$', views.checksums,
name='checksums'),
url(
r'',
include(
'bidb.buildinfo.buildinfo_submissions.urls',
namespace='submissions',
),
),
url(r'^(?P<sha1>\w{40})$', views.view, name='view'),
url(
r'^(?P<sha1>\w{40})/(?P<filename>.+)\.buildinfo$',
views.raw_text,
name='raw-text',
),
url(r'^(?P<sha1>\w{40})/(?P<filename>.+)$', views.view, name='view'),
url(
r'^api/v1/buildinfos/checksums/sha1/(?P<sha1>\w{40})$',
views.checksums,
name='checksums',
),
)
......@@ -12,9 +12,8 @@ def view(request, sha1, filename=None):
if filename != buildinfo.get_filename():
return redirect(buildinfo)
return render(request, 'buildinfo/view.html', {
'buildinfo': buildinfo,
})
return render(request, 'buildinfo/view.html', {'buildinfo': buildinfo})
def raw_text(request, sha1, filename=None):
buildinfo = get_object_or_404(Buildinfo, sha1=sha1)
......@@ -22,24 +21,29 @@ def raw_text(request, sha1, filename=None):
with default_storage.open(buildinfo.get_storage_name()) as f:
return HttpResponse(f, content_type='text/plain')
def checksums(request, sha1):
buildinfo = Buildinfo.objects.filter(
checksums__checksum_sha1=sha1,
).select_related(
'source',
'architecture',
).order_by()
return JsonResponse({'buildinfos': [{
'uri': '{}{}'.format(
settings.SITE_URL,
x.get_absolute_url(),
),
'raw-uri': '{}{}'.format(
settings.SITE_URL,
x.get_absolute_raw_url(),
),
'source': x.source.name,
'version': x.version,
'architecture': x.architecture.name,
} for x in buildinfo]})
buildinfo = (
Buildinfo.objects.filter(checksums__checksum_sha1=sha1)
.select_related('source', 'architecture')
.order_by()
)
return JsonResponse(
{
'buildinfos': [
{
'uri': '{}{}'.format(
settings.SITE_URL, x.get_absolute_url()
),
'raw-uri': '{}{}'.format(
settings.SITE_URL, x.get_absolute_raw_url()
),
'source': x.source.name,
'version': x.version,
'architecture': x.architecture.name,
}
for x in buildinfo
]
}
)
......@@ -15,11 +15,7 @@ class Key(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"pk=%d uid=%r name=%r" % (
self.pk,
self.uid,
self.name,
)
return u"pk=%d uid=%r name=%r" % (self.pk, self.uid, self.name)
def save(self, *args, **kwargs):
created = not self.pk
......
......@@ -2,6 +2,7 @@ import datetime
from django.db import models
class Source(models.Model):
name = models.CharField(max_length=200, unique=True)
......@@ -12,15 +13,13 @@ class Source(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"pk=%d name=%r" % (
self.pk,
self.name,
)
return u"pk=%d name=%r" % (self.pk, self.name)
@models.permalink
def get_absolute_url(self):
return 'packages:source', (self.name,)
class Binary(models.Model):
name = models.CharField(max_length=200, unique=True)
......@@ -29,15 +28,13 @@ class Binary(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"pk=%d name=%r" % (
self.pk,
self.name,
)
return u"pk=%d name=%r" % (self.pk, self.name)
@models.permalink
def get_absolute_url(self):
return 'packages:binary', (self.name,)
class Architecture(models.Model):
name = models.CharField(max_length=200, unique=True)
......@@ -48,7 +45,4 @@ class Architecture(models.Model):
get_latest_by = 'created'
def __unicode__(self):
return u"pk=%d name=%r" % (
self.pk,
self.name,
)
return u"pk=%d name=%r" % (self.pk, self.name)
......@@ -4,19 +4,18 @@ from . import views
urlpatterns = (
url(r'^sources$', views.sources,
name='sources'),
url(r'^binaries$', views.binaries,
name='binaries'),
url(r'^sources/(?P<name>[^/]+)$', views.source,
name='source'),
url(r'^sources/(?P<name>[^/]+)/(?P<version>[^/]+)$', views.source_version,
name='source-version'),
url(r'^binaries/(?P<name>[^/]+)$', views.binary,
name='binary'),
url(r'^api/v1/sources/(?P<name>[^/]+)/(?P<version>[^/]+)/(?P<architecture>[^/]+)$',
url(r'^sources$', views.sources, name='sources'),
url(r'^binaries$', views.binaries, name='binaries'),
url(r'^sources/(?P<name>[^/]+)$', views.source, name='source'),
url(
r'^sources/(?P<name>[^/]+)/(?P<version>[^/]+)$',
views.source_version,
name='source-version',
),
url(r'^binaries/(?P<name>[^/]+)$', views.binary, name='binary'),
url(
r'^api/v1/sources/(?P<name>[^/]+)/(?P<version>[^/]+)/(?P<architecture>[^/]+)$',
views.api_source_version_architecture,
name='api-source-version-architecture'),
name='api-source-version-architecture',
),
)
......@@ -13,32 +13,34 @@ from .models import Binary, Source
def sources(request):
page = AutoPaginator(request, Source.objects.all(), 250).current_page()
return render(request, 'packages/sources.html', {
'page': page,
})
return render(request, 'packages/sources.html', {'page': page})
def binaries(request):
page = AutoPaginator(request, Binary.objects.all(), 250).current_page()
return render(request, 'packages/binaries.html', {
'page': page,
})
return render(request, 'packages/binaries.html', {'page': page})
def source(request, name):
source = get_object_or_404(Source, name=name)
binaries = Binary.objects.filter(
generated_binaries__buildinfo__source=source,
generated_binaries__buildinfo__source=source
).distinct()
versions = source.buildinfos.values_list('version', flat=True) \
.order_by('version').distinct()
versions = (
source.buildinfos.values_list('version', flat=True)
.order_by('version')
.distinct()
)
return render(
request,
'packages/source.html',
{'source': source, 'binaries': binaries, 'versions': versions},
)
return render(request, 'packages/source.html', {
'source': source,
'binaries': binaries,
'versions': versions,
})
def source_version(request, name, version):
source = get_object_or_404(Source, name=name)
......@@ -46,18 +48,14 @@ def source_version(request, name, version):
if not source.buildinfos.filter(version=version).exists():
raise Http404()
qs = source.buildinfos.filter(
version=version,
).order_by(
'architecture__name',
).prefetch_related(
'submissions__key',
qs = (
source.buildinfos.filter(version=version)
.order_by('architecture__name')
.prefetch_related('submissions__key')
)
buildinfos_by_arch = groupby(
qs,
lambda x: x.architecture.name,
lambda x: x.created,
qs, lambda x: x.architecture.name, lambda x: x.created
)
reproducible_by_arch = {}
......@@ -76,24 +74,32 @@ def source_version(request, name, version):
reproducible_by_arch[x] = reproducible
return render(request, 'packages/source_version.html', {
'source': source,
'version': version,
'buildinfos_by_arch': buildinfos_by_arch,
'reproducible_by_arch': reproducible_by_arch,
})
return render(
request,
'packages/source_version.html',
{
'source': source,
'version': version,
'buildinfos_by_arch': buildinfos_by_arch,
'reproducible_by_arch': reproducible_by_arch,
},
)
def binary(request, name):
binary = get_object_or_404(Binary, name=name)
versions = binary.generated_binaries.values_list(
'buildinfo__version', flat=True,
).order_by('buildinfo__version').distinct()
versions = (
binary.generated_binaries.values_list('buildinfo__version', flat=True)
.order_by('buildinfo__version')
.distinct()
)
return render(request, 'packages/binary.html', {
'binary': binary,
'versions': versions,
})
return render(
request,
'packages/binary.html',
{'binary': binary, 'versions': versions},
)
def api_source_version_architecture(request, name, version, architecture):
......@@ -102,25 +108,27 @@ def api_source_version_architecture(request, name, version, architecture):
if not source.buildinfos.filter(version=version).exists():
raise Http404()
qs = Submission.objects.filter(
buildinfo__version=version,
buildinfo__source_id=source,
buildinfo__architecture__name=architecture,
).select_related(
'key',
'buildinfo',
'buildinfo__architecture',
'buildinfo__source',
).only(
'slug',
'buildinfo__sha1',
'buildinfo__version',
'buildinfo__source__name',
'buildinfo__architecture__name',
'key__name',
'key__uid',
'created',
).order_by()
qs = (
Submission.objects.filter(
buildinfo__version=version,
buildinfo__source_id=source,
buildinfo__architecture__name=architecture,
)
.select_related(
'key', 'buildinfo', 'buildinfo__architecture', 'buildinfo__source'
)
.only(
'slug',
'buildinfo__sha1',
'buildinfo__version',
'buildinfo__source__name',
'buildinfo__architecture__name',
'key__name',
'key__uid',
'created',
)
.order_by()
)
if 'key__uid' in request.GET:
qs = qs.filter(key__uid__in=request.GET.getlist('key__uid'))
......@@ -133,18 +141,15 @@ def api_source_version_architecture(request, name, version, architecture):
by_sha1 = [
{
'uri': '{}{}'.format(
settings.SITE_URL,
xs[0].get_absolute_url(),
),
'uri': '{}{}'.format(settings.SITE_URL, xs[0].get_absolute_url()),
'sha1': sha1,
'submissions': [{
'key': {
'uid': x.key.uid,
'name': x.key.name,
},
'created': x.created,
} for x in xs],
'submissions': [
{
'key': {'uid': x.key.uid, 'name': x.key.name},
'created': x.created,
}
for x in xs
],
}
for sha1, xs in grouped
]
......
......@@ -33,7 +33,7 @@ DATABASES = {
'HOST': '127.0.0.1',
'PORT': '5432',
'ATOMIC_REQUESTS': True,
},
}
}
MIDDLEWARE_CLASSES = (
......@@ -54,9 +54,7 @@ WSGI_APPLICATION = 'bidb.wsgi.application'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......@@ -69,28 +67,29 @@ TEMPLATES = [
'builtins': [
'django.contrib.humanize.templatetags.humanize',
'django.contrib.staticfiles.templatetags.staticfiles',
'bidb.utils.templatetags.pagination',
'bidb.utils.templatetags.python',
],
},
},
}
]
LOGIN_URL = '/login' # 'account:login'
LOGIN_REDIRECT_URL = '/' # 'static:landing'
LOGIN_URL = '/login' # 'account:login'
LOGIN_REDIRECT_URL = '/' # 'static:landing'
USE_TZ = False
TIME_ZONE = 'UTC'