Adopt the way which is described in /usr/share/doc/gnupg/DETAILS.gz which expands first line, and use email.utils.parseaddr()

This commit is contained in:
Olivier Berger 2013-11-15 14:31:46 +01:00
parent 875eb57d35
commit 2752c2e98d
1 changed files with 7 additions and 20 deletions

View File

@ -35,6 +35,7 @@ import os.path
import logging
import subprocess
import sys
import email.utils
def _get_keyrings():
@ -58,22 +59,8 @@ def _parse_uid(uid):
Parse a uid of the form 'Real Name <email@example.com>' into email
and realname parts.
"""
uid = uid.strip()
# First, strip comment
s = uid.find('(')
e = uid.find(')')
if s >= 0 and e >= 0:
uid = uid[:s] + uid[e + 1:]
s = uid.find('<')
e = uid.find('>')
email = None
if s >= 0 and e >= 0:
email = uid[s + 1:e]
uid = uid[:s] + uid[e + 1:]
uid = uid.strip()
if not email and uid.find('@') >= 0:
email, uid = uid, email
return (uid, email)
(uid, mail) = email.utils.parseaddr(uid)
return (uid, mail)
resultdict = {}
@ -85,6 +72,7 @@ def _get_canonical(key):
def _add_to_result(key, newvalue):
logging.debug("adding %s: %s", key, newvalue)
thekey = _get_canonical(key)
if newvalue not in resultdict[thekey]:
resultdict[thekey].append(newvalue)
@ -95,10 +83,10 @@ def process_keyrings():
file."""
for keyring in _get_keyrings():
logging.debug("get data from %s", keyring)
proc = subprocess.Popen(["gpg", "--no-default-keyring",
proc = subprocess.Popen(["gpg", "--no-options", "--no-default-keyring",
"--no-expensive-trust-checks",
"--keyring", keyring, "--list-keys",
"--with-colons", "--fingerprint"],
"--with-colons", "--fixed-list-mode", "--with-fingerprint", "--with-fingerprint"],
stdout=subprocess.PIPE)
fpr = None
entry = None
@ -108,11 +96,10 @@ def process_keyrings():
uid = None
if items[0] == 'pub':
fpr = entry = None
lastpub = items[9].strip()
lastpub = items[4].strip()
continue
elif items[0] == 'fpr':
fpr = items[9].strip()
uid = lastpub
elif items[0] == 'uid':
uid = items[9].strip()
else: