# Path: bidb/keys/models.py
# class Key(models.Model):
#     uid = models.CharField(max_length=255, unique=True)
# 
#     name = models.TextField()
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('-created',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d uid=%r name=%r" % (
#             self.pk,
#             self.uid,
#             self.name,
#         )
# 
#     def save(self, *args, **kwargs):
#         created = not self.pk
# 
#         super(Key, self).save(*args, **kwargs)
# 
#         if created:
#             from .tasks import update_or_create_key
# 
#             transaction.on_commit(lambda: update_or_create_key.delay(self.uid))
# 
# Path: bidb/packages/models.py
# class Source(models.Model):
#     name = models.CharField(max_length=200, unique=True)
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'packages:source', (self.name,)
# 
# class Architecture(models.Model):
#     name = models.CharField(max_length=200, unique=True)
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )
# 
# class Binary(models.Model):
#     name = models.CharField(max_length=200, unique=True)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'packages:binary', (self.name,)
# 
# Path: bidb/buildinfo/models.py
# class Buildinfo(models.Model):
#     sha1 = models.CharField(max_length=40, unique=True)
# 
#     source = models.ForeignKey(
#         'packages.Source',
#         related_name='buildinfos',
#     )
# 
#     architecture = models.ForeignKey(
#         'packages.Architecture',
#         related_name='buildinfos',
#     )
#     version = models.CharField(max_length=200)
# 
#     build_path = models.CharField(max_length=512)
#     build_date = models.DateTimeField(null=True)
#     build_origin = models.ForeignKey('Origin', null=True)
#     build_architecture = models.ForeignKey(
#         'packages.Architecture',
#         related_name='buildinfos_build',
#     )
# 
#     environment = models.TextField()
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('-created',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d source=%r version=%r" % (
#             self.pk,
#             self.source,
#             self.version,
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'buildinfo:view', (self.sha1, self.get_filename())
# 
#     @models.permalink
#     def get_absolute_raw_url(self):
#         return 'buildinfo:raw-text', (self.sha1, self.get_filename())
# 
#     def get_filename(self):
#         return '{}_{}_{}'.format(
#             self.source.name,
#             self.version,
#             self.architecture.name.split(' ')[0],
#         )
# 
#     def get_storage_name(self):
#         return 'buildinfo.Buildinfo/{}/{}/{}'.format(
#             self.sha1[:2],
#             self.sha1[2:4],
#             self.sha1,
#         )
# 
# class Origin(models.Model):
#     name = models.CharField(max_length=255)
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )


import re
import hashlib
from debian import deb822
from dateutil.parser import parse
from django.db import transaction, IntegrityError
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from bidb.keys.models import Key
from bidb.packages.models import Source, Architecture, Binary
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$',
)
re_installed_build_depends = re.compile(
    r'^(?P<package>[^ ]+) \(= (?P<version>.+)\)'
)


class InvalidSubmission(Exception):
    pass

@transaction.atomic
def parse_submission(request):
    raw_text = request.read()

    try:
        data = deb822.Deb822(raw_text)
    except TypeError:
        raise InvalidSubmission("Could not parse RFC-822 format data.")

    raw_text_gpg_stripped = data.dump()

    ## Parse GPG info #########################################################

    uid = None
    data.raw_text = raw_text
    gpg_info = data.get_gpg_info()


# Path: bidb/keys/models.py
# class Key(models.Model):
#     uid = models.CharField(max_length=255, unique=True)
# 
#     name = models.TextField()
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('-created',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d uid=%r name=%r" % (
#             self.pk,
#             self.uid,
#             self.name,
#         )
# 
#     def save(self, *args, **kwargs):
#         created = not self.pk
# 
#         super(Key, self).save(*args, **kwargs)
# 
#         if created:
#             from .tasks import update_or_create_key
# 
#             transaction.on_commit(lambda: update_or_create_key.delay(self.uid))
# 
# Path: bidb/packages/models.py
# class Source(models.Model):
#     name = models.CharField(max_length=200, unique=True)
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'packages:source', (self.name,)
# 
# class Architecture(models.Model):
#     name = models.CharField(max_length=200, unique=True)
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )
# 
# class Binary(models.Model):
#     name = models.CharField(max_length=200, unique=True)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'packages:binary', (self.name,)
# 
# Path: bidb/buildinfo/models.py
# class Buildinfo(models.Model):
#     sha1 = models.CharField(max_length=40, unique=True)
# 
#     source = models.ForeignKey(
#         'packages.Source',
#         related_name='buildinfos',
#     )
# 
#     architecture = models.ForeignKey(
#         'packages.Architecture',
#         related_name='buildinfos',
#     )
#     version = models.CharField(max_length=200)
# 
#     build_path = models.CharField(max_length=512)
#     build_date = models.DateTimeField(null=True)
#     build_origin = models.ForeignKey('Origin', null=True)
#     build_architecture = models.ForeignKey(
#         'packages.Architecture',
#         related_name='buildinfos_build',
#     )
# 
#     environment = models.TextField()
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('-created',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d source=%r version=%r" % (
#             self.pk,
#             self.source,
#             self.version,
#         )
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return 'buildinfo:view', (self.sha1, self.get_filename())
# 
#     @models.permalink
#     def get_absolute_raw_url(self):
#         return 'buildinfo:raw-text', (self.sha1, self.get_filename())
# 
#     def get_filename(self):
#         return '{}_{}_{}'.format(
#             self.source.name,
#             self.version,
#             self.architecture.name.split(' ')[0],
#         )
# 
#     def get_storage_name(self):
#         return 'buildinfo.Buildinfo/{}/{}/{}'.format(
#             self.sha1[:2],
#             self.sha1[2:4],
#             self.sha1,
#         )
# 
# class Origin(models.Model):
#     name = models.CharField(max_length=255)
# 
#     created = models.DateTimeField(default=datetime.datetime.utcnow)
# 
#     class Meta:
#         ordering = ('name',)
#         get_latest_by = 'created'
# 
#     def __unicode__(self):
#         return u"pk=%d name=%r" % (
#             self.pk,
#             self.name,
#         )


# Path: bidb/api/utils.py
import re
import hashlib
from debian import deb822
from dateutil.parser import parse
from django.db import transaction, IntegrityError
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from bidb.keys.models import Key
from bidb.packages.models import Source, Architecture, Binary
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$',
)
re_installed_build_depends = re.compile(
    r'^(?P<package>[^ ]+) \(= (?P<version>.+)\)'
)


class InvalidSubmission(Exception):
    pass

@transaction.atomic
def parse_submission(request):
    raw_text = request.read()

    try:
        data = deb822.Deb822(raw_text)
    except TypeError:
        raise InvalidSubmission("Could not parse RFC-822 format data.")

    raw_text_gpg_stripped = data.dump()

    ## Parse GPG info #########################################################

    uid = None
    data.raw_text = raw_text
    gpg_info = data.get_gpg_info()


    for x in ('VALIDSIG', 'NO_PUBKEY'):