# Path: bidb/utils/tempfile.py
# @contextlib.contextmanager
# def TemporaryDirectory():
#     name = tempfile.mkdtemp()
# 
#     try:
#         yield name
#     finally:
#         shutil.rmtree(name, ignore_errors=True)
# 
# Path: bidb/utils/subprocess.py
# def check_output2(args, stdin=None):
#     p = subprocess.Popen(
#         args,
#         stdout=subprocess.PIPE,
#         stdin=subprocess.PIPE,
#         stderr=subprocess.STDOUT,
#     )
# 
#     out, _ = p.communicate(input=stdin)
# 
#     retcode = p.wait()
# 
#     if retcode:
#         raise subprocess.CalledProcessError(retcode, ' '.join(args), out)
# 
#     return out
# 
# 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))


import celery
import subprocess
from bidb.utils.tempfile import TemporaryDirectory
from bidb.utils.subprocess import check_output2
from .models import Key
                '--homedir', homedir,
                '--keyserver', 'pgpkeys.mit.edu',
                '--recv-keys', uid,
            ))
        except subprocess.CalledProcessError as exc:
            print "E: {}: {}".format(exc, exc.output)
            return None, False

        data = check_output2((
            'gpg',
            '--homedir', homedir,
            '--with-colons',
            '--fixed-list-mode',
            '--fingerprint',
            uid,
        ))

    for line in data.splitlines():
        if line.startswith('uid:'):
            name = line.split(':')[9]
            break
    else:
        raise ValueError("Could not parse name from key: {}".format(data))

    return Key.objects.update_or_create(uid=uid, defaults={
        'name': name,
    })

@celery.task()
def refresh_all():

# Path: bidb/utils/tempfile.py
# @contextlib.contextmanager
# def TemporaryDirectory():
#     name = tempfile.mkdtemp()
# 
#     try:
#         yield name
#     finally:
#         shutil.rmtree(name, ignore_errors=True)
# 
# Path: bidb/utils/subprocess.py
# def check_output2(args, stdin=None):
#     p = subprocess.Popen(
#         args,
#         stdout=subprocess.PIPE,
#         stdin=subprocess.PIPE,
#         stderr=subprocess.STDOUT,
#     )
# 
#     out, _ = p.communicate(input=stdin)
# 
#     retcode = p.wait()
# 
#     if retcode:
#         raise subprocess.CalledProcessError(retcode, ' '.join(args), out)
# 
#     return out
# 
# 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/keys/tasks.py
import celery
import subprocess
from bidb.utils.tempfile import TemporaryDirectory
from bidb.utils.subprocess import check_output2
from .models import Key

                '--homedir', homedir,
                '--keyserver', 'pgpkeys.mit.edu',
                '--recv-keys', uid,
            ))
        except subprocess.CalledProcessError as exc:
            print "E: {}: {}".format(exc, exc.output)
            return None, False

        data = check_output2((
            'gpg',
            '--homedir', homedir,
            '--with-colons',
            '--fixed-list-mode',
            '--fingerprint',
            uid,
        ))

    for line in data.splitlines():
        if line.startswith('uid:'):
            name = line.split(':')[9]
            break
    else:
        raise ValueError("Could not parse name from key: {}".format(data))

    return Key.objects.update_or_create(uid=uid, defaults={
        'name': name,
    })

@celery.task()
def refresh_all():

    for x in Key.objects.all():