summaryrefslogtreecommitdiffstats
path: root/scripts/parse-email
blob: bf457b913a4dfad96e87f8cbd0f038142908071c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import print_function
import datetime
import email
import os.path
import re
import subprocess
import sys

debug = False

roles = {
    'DM': 'debian-maintainers-gpg',
    'DD': 'debian-keyring-gpg',
    'DN': 'debian-nonupload-gpg'
}


def do_dch(logmsg):
    release = "unknown"
    with open('debian/changelog', 'r') as f:
        line = f.readline()
        m = re.match("debian-keyring \((.*)\) (.*); urgency=", line)
        version = m.group(1)
        release = m.group(2)
    if release == "UNRELEASED":
        if debug:
            print('dch --multimaint-merge -D UNRELEASED -a "' + logmsg + '"')
        else:
            subprocess.call(['dch', '--multimaint-merge', '-D', 'UNRELEASED',
                             '-a', logmsg])
    elif release == "unstable":
        newver = datetime.date.today().strftime("%Y.%m.xx")
        if newver == version:
            print(' * Warning: New version and previous released version are ')
            print('   the same: ' + newver + '. This should not be so!')
            print('   Check debian/changelog')
        if debug:
            print('dch -D UNRELEASED -v ' + newver + ' "' + logmsg + '"')
        else:
            subprocess.call(['dch', '-D', 'UNRELEASED', '-v', newver, logmsg])
    else:
        print("Unknown changelog release: " + release)

    if not debug:
        subprocess.call(['git', 'add', 'debian/changelog'])


def do_git_template(logmsg, state):
    with open('git-commit-template', 'w') as f:
        f.write(logmsg)
        f.write('\n\n')
        f.write("Action: add\n")
        f.write("Subject: " + state['name'] + "\n")
        if 'username' in state:
            f.write("Username: " + state['username'] + "\n")
        f.write("Role: " + state['role'] + "\n")
        f.write("Key: " + state['keyid'] + "\n")
        f.write("Key-type: " + state['keytype'] + "\n")
        f.write("RT-Ticket: " + state['rt'] + "\n")
        f.write("Request-signed-by: \n")
        f.write("Key-certified-by: \n")
        if 'details' in state:
            f.write("Details: " + state['details'] + "\n")
        if state['role'] == 'DM' and 'agreement' in state:
            f.write("Advocates:\n")
            for a in state['advocates']:
                f.write(" " + a + "\n")
            f.write("Agreement: " + state['agreement'] + "\n")
            f.write("BTS: " + state['bts'] + "\n")


# Change to our basedir, assuming we're in <basedir>/scripts/<us>
basedir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))
os.chdir(basedir)

inadvocates = False
state = {}
state['advocates'] = []

for line in sys.stdin:
    line = line.rstrip()

    if inadvocates:
        if line[:9] == 'KeyCheck:':
            inadvocates = False
        else:
            line = line.lstrip()
            state['advocates'].append(line)
            continue

    m = re.match("X-RT-Ticket: rt.debian.org #(.*)$", line)
    if m:
        state['rt'] = m.group(1)
        continue

    m = re.match("  please add key ID (.*)$", line)
    if m:
        state['keyid'] = m.group(1)
        state['role'] = 'DM'
        state['move'] = False
        continue

    m = re.match("Please make (.*) \(currently ([^ ]*) ([^ ]*)", line)
    if m:
        state['name'] = m.group(1)
        state['keytype'] = '<FIXME>'
        state['move'] = (not ((m.group(2) == 'NOT') or (m.group(2) != "'Debian") or (m.group(3) == "Contributor')"))) or m.group(3) == 'DM)'
        m = re.match(".*a non-uploading", line)
        if m:
            state['role'] = 'DN'
        else:
            state['role'] = 'DD'
        continue

    m = re.match("^Agreement: (.*)", line)
    if m:
        state['agreement'] = m.group(1)
        continue

    m = re.match("^BTS: (.*)$", line)
    if m:
        state['bts'] = m.group(1)
        continue

    m = re.match("^Comment: (Please add|Add) (.*) <", line)
    if m:
        state['name'] = m.group(2)
        continue

    m = re.match("^Advocates:(=20)?$", line)
    if m:
        inadvocates = True
        continue

    m = re.match("  pub   (.....)/", line)
    if m:
        state['keytype'] = m.group(1)
        continue

    m = re.match("  Key fingerprint:\s+(.*)", line)
    if m:
        state['keyid'] = m.group(1)
        continue

    m = re.match("  Target keyring:\s+(Deb.*)", line)
    if m:
        if (m.group(1) == 'Debian Maintainer' or
            m.group(1) == 'Debian Maintainer, with guest account'):
            state['role'] = 'DM'
            state['move'] = False
        elif m.group(1) == 'Debian Developer, uploading':
            state['role'] = 'DD'
        elif m.group(1) == 'Debian Developer, non-uploading':
            state['role'] = 'DN'
        else:
            state['role'] = "UNKNOWN"
        continue

    m = re.match("  (Account|Username):\s+(.*)", line)
    if m:
        state['username'] = m.group(2)
        continue

    m = re.match("  Details:\s+(http.*)", line)
    if m:
        state['details'] = m.group(1)
        continue

if 'role' not in state:
    print('Did not find recognised keyring related email.')
    sys.exit(1)

if not debug:
    if state['move']:
        if os.path.exists(roles['DM'] + '/0x' + state['keyid'][24:]):
            res = subprocess.call(["scripts/move-key", state['keyid'],
                                  roles[state['role']]], stdin=open('/dev/tty'))
        elif os.path.exists(roles['DN'] + '/0x' + state['keyid'][24:]):
            res = subprocess.call(["scripts/move-key", state['keyid'],
                                  roles[state['role']]], stdin=open('/dev/tty'))
        else:
            print('Trying to move non-existent key from DM keyring.')
            sys.exit(1)
    else:
        res = subprocess.call(["scripts/add-key", state['keyid'],
                               roles[state['role']]], stdin=open('/dev/tty'))
    if res:
        print('Failed to add key.')
        sys.exit(1)

logmsg = ("Add new " + state['role'] + " key 0x" + state['keyid'][24:] + " (" +
          state['name'] + ") (RT #" + state['rt'] + ")")

#if not state['move']:
#    do_dch(logmsg)
do_git_template(logmsg, state)