From 2d5777b11d229d115a31a6c82236570002c2dd57 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 21 Oct 2011 18:49:00 -0500 Subject: Add a generate_keyring command This grabs all the PGP keys from the developer profiles and adds them to the keyrings. Obviously we may want to do more in the future such as filter by groups, active status, etc. but this is just a first iteration. Signed-off-by: Dan McGee --- devel/management/commands/generate_keyring.py | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 devel/management/commands/generate_keyring.py (limited to 'devel/management/commands/generate_keyring.py') diff --git a/devel/management/commands/generate_keyring.py b/devel/management/commands/generate_keyring.py new file mode 100644 index 00000000..b95d5a8e --- /dev/null +++ b/devel/management/commands/generate_keyring.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +""" +generate_keyring command + +Assemble a GPG keyring with all known developer keys. + +Usage: ./manage.py generate_keyring +""" + +from django.core.management.base import BaseCommand, CommandError +from django.db.models import Q + +import logging +import subprocess +import sys + +from main.models import UserProfile + +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s -> %(levelname)s: %(message)s', + datefmt='%Y-%m-%d %H:%M:%S', + stream=sys.stderr) +logger = logging.getLogger() + +class Command(BaseCommand): + args = " " + help = "Assemble a GPG keyring with all known developer keys." + + def handle(self, *args, **options): + v = int(options.get('verbosity', None)) + if v == 0: + logger.level = logging.ERROR + elif v == 1: + logger.level = logging.INFO + elif v == 2: + logger.level = logging.DEBUG + + if len(args) != 2: + raise CommandError("keyserver and keyring_path must be provided") + + return generate_keyring(args[0], args[1]) + +def generate_keyring(keyserver, keyring): + logger.info("getting all known key IDs") + + exclude = Q(pgp_key__isnull=True) & Q(pgp_key__exact="") + key_ids = UserProfile.objects.exclude( + exclude).values_list("pgp_key", flat=True) + logger.info("%d keys fetched from user profiles", len(key_ids)) + + gpg_cmd = ["gpg", "--no-default-keyring", "--keyring", keyring, + "--keyserver", keyserver, "--recv-keys"] + logger.info("running command: %r", gpg_cmd) + gpg_cmd.extend(key_ids) + subprocess.check_call(gpg_cmd) + logger.info("keyring at %s successfully updated", keyring) + +# vim: set ts=4 sw=4 et: -- cgit v1.2.3-54-g00ecf From ade2c08899abf77f6d836432c9988ad3f9652a95 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 1 Nov 2011 16:47:37 -0500 Subject: Really ensure we don't catch any NULL or blank values Fuck you too, Django. Signed-off-by: Dan McGee --- devel/management/commands/generate_keyring.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'devel/management/commands/generate_keyring.py') diff --git a/devel/management/commands/generate_keyring.py b/devel/management/commands/generate_keyring.py index b95d5a8e..35ab8874 100644 --- a/devel/management/commands/generate_keyring.py +++ b/devel/management/commands/generate_keyring.py @@ -8,7 +8,6 @@ """ from django.core.management.base import BaseCommand, CommandError -from django.db.models import Q import logging import subprocess @@ -44,9 +43,10 @@ def handle(self, *args, **options): def generate_keyring(keyserver, keyring): logger.info("getting all known key IDs") - exclude = Q(pgp_key__isnull=True) & Q(pgp_key__exact="") - key_ids = UserProfile.objects.exclude( - exclude).values_list("pgp_key", flat=True) + # Screw you Django, for not letting one natively do value != + key_ids = UserProfile.objects.filter(user__is_active=True, + pgp_key__isnull=False).extra(where=["pgp_key != ''"]).values_list( + "pgp_key", flat=True) logger.info("%d keys fetched from user profiles", len(key_ids)) gpg_cmd = ["gpg", "--no-default-keyring", "--keyring", keyring, -- cgit v1.2.3-54-g00ecf