-
Notifications
You must be signed in to change notification settings - Fork 13
/
package-sponsor
executable file
·337 lines (309 loc) · 10.9 KB
/
package-sponsor
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python3
"""A little script to report any package upload activity for a specific user
It processes <series>-changes mailing list archive files to track
upload activity for a given user in each series.
This is a work in progress
TODO:
- db-backed (constantly reprocessing the mbox files takes 1.5 mins)
- automatically download or re-download mailbox files when not present
"""
import argparse
import json
import os
import re
import sys
from email.parser import Parser
from prettytable import PrettyTable
AUTOMATION_EMAILS = [
"Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>"
]
HEADER_FMT = "\n\n'{pkg_name}' package uploads by {creator}"
HEADER_FMT_WIKI = "\n== '{pkg_name}' package uploads by {creator} =="
DISTRO_SERIES = ["xenial", "yakkety", "zesty", "artful", "bionic", "cosmic"]
UPLOAD_HEADER = r"From bounces@canonical.com.*"
CHANGES_KEYS = [
"Checksums-Sha1",
"Original-Maintainer",
"Changes",
"Description",
"Changed-By",
"Maintainer",
"Urgency",
"Distribution",
"Version",
"Architecture",
"Binary",
"Source",
"Signed-By",
"Date",
"Format",
"http",
"https",
]
MULTILINE_KEYS = ["CheckSums-Sha1", "Changes", "Description"]
def process_upload_message(msg_content, emails=None, packages=None):
msg = Parser().parsestr(msg_content)
for part in msg.walk():
if part.get_content_maintype() != "multipart":
continue
upload = {"Changes": ""}
[base, changesfile] = part.get_payload()
base_payload = base.get_payload()
changes_payload = changesfile.get_payload()
lines = base_payload.splitlines()
header_match = re.match(
r"(?P<Source>[^ ]*) \((?P<Version>[^)]*)\)"
r" (?P<Distribution>[^;]*);.*",
lines[0],
)
if header_match:
upload.update(header_match.groupdict())
lines = lines[2:]
if "Sorry, changesfile not available" not in changes_payload:
lines += changes_payload.splitlines()
key = "Changes"
for line in lines:
if "-----BEGIN PGP SIGNATURE-----" in line:
break
toks = line.split(":")
if len(toks) != 1 and toks[0] in CHANGES_KEYS:
key = toks[0]
if "http" in key: # Alias any direct project urls http(s)
upload["URL"] = line
key = None
else:
upload[key] = ":".join(toks[1:]).strip()
if key not in MULTILINE_KEYS:
key = None # Ensure we only grab the current line
else:
try:
upload[key] += "\n" + line
except KeyError:
pass
if emails:
uploader = upload.get("Changed-By")
if not uploader:
uploader = upload.get("Signed-By")
found_email = False
for email in emails:
if email in uploader:
found_email = True
break
if not found_email:
return {}
if packages and upload.get("Source") not in packages:
return {}
return upload
return {}
def format_publishing_history(history, style="prettytable"):
"""Print human-readable format of package upload history."""
if style == "json":
print(json.dumps(history, indent=4, separators=(",", ": ")))
return
series = None
for creator, person_history in history.items():
table = PrettyTable()
table.field_names = [
"Series",
"Version",
"Publish Date",
"Uploader",
"Sponsor",
]
for pkg_name, series_data in person_history.items():
if style in ("wiki", "markdown"):
print(
HEADER_FMT_WIKI.format(pkg_name=pkg_name, creator=creator)
)
elif style == "prettytable":
print(HEADER_FMT.format(pkg_name=series, creator=creator))
for series, pkg_data in sorted(series_data.items()):
for version, upload in sorted(pkg_data.items()):
sponsor = upload.get("Signed-By")
if not sponsor:
sponsor = upload.get("Maintainer")
if not sponsor or sponsor in AUTOMATION_EMAILS:
sponsor = upload.get("Changed-By")
table.add_row(
[
upload["Distribution"],
version,
upload["Date"],
creator,
sponsor,
]
)
if style == "markdown":
# Courtesy https://gist.github.com/dbzm/68256c86c60d70072576
table.junction_char = "|"
lines = [
row[1:-1] for row in table.get_string().split("\n")[1:-1]
]
print("\n".join(lines))
elif style == "wiki":
print(
"||%s||"
% "||".join(["'''%s'''" % f for f in table.field_names])
)
lines = table.get_string().splitlines()[3:-1]
print("\n".join(line.replace("|", "||") for line in lines))
else:
print(table)
print("\n")
def process_mbox_history(stream, history=None, emails=None, packages=None):
"""Process a package upload history from mail archive.
@return: A dict of uploads filtered specific uploader or package name.
"""
if history is None:
history = {}
msg = []
if stream is None: # Filter from existing history object
filtered_history = {}
for uploader, pkg_data in history.items():
matched_emails = [email for email in emails if email in uploader]
if emails and not matched_emails:
continue
filtered_history[uploader] = {}
for pkg_name, series_data in pkg_data.items():
matched_packages = [
p_name for p_name in packages if p_name == pkg_name
]
if packages and not matched_packages:
continue
filtered_history[uploader][pkg_name] = series_data
return filtered_history
for line in stream.readlines():
if re.match(UPLOAD_HEADER, line):
if msg:
upload = process_upload_message("".join(msg), emails, packages)
if upload:
pkg_name = upload.get("Source")
uploader = upload.get("Changed-By")
if not history.get(uploader):
history[uploader] = {}
if not history[uploader].get(pkg_name):
history[uploader][pkg_name] = {}
series = upload.get("Distribution")
if not history[uploader][pkg_name].get(series):
history[uploader][pkg_name][series] = {}
history[uploader][pkg_name][series][
upload.get("Version")
] = upload
msg = []
msg.append(line)
if msg:
upload = process_upload_message("".join(msg), emails, packages)
if upload:
pkg_name = upload.get("Source")
uploader = upload.get("Changed-By")
if not history.get(uploader):
history[uploader] = {}
if not history[uploader].get(pkg_name):
history[uploader][pkg_name] = {}
series = upload.get("Distribution")
if not history[uploader][pkg_name].get(series):
history[uploader][pkg_name][series] = {}
history[uploader][pkg_name][series][upload.get("Version")] = upload
return history
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--directory",
type=str,
default=".",
help="Directory in which series-changes mbox files are located",
)
parser.add_argument(
"-j",
"--json-archive",
type=str,
default=None,
dest="json_archive",
help=(
"Optionally source pre-existing json-archive of changes instead"
" of mbox archives"
),
)
parser.add_argument(
"--series",
action="append",
default=[],
choices=DISTRO_SERIES,
help="Limit results to a specific series",
)
parser.add_argument(
"--created-after",
type=str,
default="2013-01-01",
dest="created_after",
help=(
"Only report package publishes after this date."
" Default: 2013-01-01"
),
)
parser.add_argument(
"--packages",
action="append",
required=False,
default=[],
help="Limit results to a specific packages.",
)
parser.add_argument(
"--emails",
action="append",
required=False,
default=[],
help="Find package changes related to specific email addresses.",
)
parser.add_argument(
"--format",
type=str,
required=False,
choices=["json", "markdown", "wiki"],
help="Specify alternative formatting: markdown",
)
args = parser.parse_args()
if args.series:
all_series = args.series
else:
all_series = DISTRO_SERIES
history = {}
if args.json_archive:
if not os.path.exists(args.json_archive):
print(
"Error: json archive {json_file} does not exist".format(
json_file=args.json_archive
)
)
sys.exit(1)
history = json.load(open(args.json_archive))
history = process_mbox_history(
stream=None,
history=history,
emails=args.emails,
packages=args.packages,
)
format_publishing_history(history, args.format)
return
for series in all_series:
mbox_path = "{directory}/{series}-changes.mbox".format(
directory=args.directory, series=series
)
if not os.path.exists(mbox_path):
print(
"Download mbox archive file https://lists.ubuntu.com/archives/"
"{series}-changes.mbox/{series}-changes.mbox to"
" {mbox_path}".format(series=series, mbox_path=mbox_path)
)
continue
with open(mbox_path) as stream:
process_mbox_history(
stream,
history=history,
emails=args.emails,
packages=args.packages,
)
format_publishing_history(history, args.format)
if __name__ == "__main__":
main()