-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwnm-sync.py
executable file
·348 lines (289 loc) · 13.3 KB
/
pwnm-sync.py
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
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/python3
#
# pwnm-sync - Sync patch state between Patchwork and Notmuch
# Copyright (C) 2018 Stewart Smith, IBM Corp.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
import sys
import datetime
import notmuch
import sqlite3
import argparse
import os
import configparser
from requests_futures.sessions import FuturesSession
all_my_tags = ['accepted', 'superseded', 'changes-requested', 'rfc',
'rejected', 'new', 'under-review', 'not-applicable', 'deferred',
'awaiting-upstream']
def main():
initial_argp = argparse.ArgumentParser(add_help=False)
initial_argp.add_argument("-c", "--config", dest='config_file', type=str,
help="Configuration file for pwnm-sync",
default=os.path.join(os.path.expanduser("~"),
".pwnm-sync.ini"))
args, remaining_argv = initial_argp.parse_known_args()
argp = argparse.ArgumentParser()
defaults = {
"notmuch_database" : os.path.join(os.path.expanduser("~"),"Maildir","INBOX"),
"syncdb" : os.path.join(os.path.expanduser("~"),".pwnm-sync.db"),
"patchwork_url" : "https://patchwork.ozlabs.org",
"sync" : "skiboot=skiboot@lists.ozlabs.org",
"epoch" : None,
}
if not os.path.isfile(args.config_file):
print("Config file {} not found!".format(args.config_file))
args.config_file = None
if args.config_file:
config = configparser.SafeConfigParser()
config.read([args.config_file])
config_values = dict(config.items("Defaults"))
defaults = {**defaults, **config_values}
argp.set_defaults(**defaults)
argp.add_argument("-m", "--notmuch-database", dest='notmuch_database', type=str,
help="The notmuch database to sync")
argp.add_argument("-d", "--syncdb", dest="syncdb", type=str,
help="The path to the sqlite3 database that pwnm-sync " +
"uses to keep track of the state of the local notmuch and " +
"remote patchwork databases.")
argp.add_argument("-t", "--patchwork-token", dest="patchwork_token", type=str,
help="Your Patchwork API token. Get it from /user/ on your " +
"patchwork instance.")
argp.add_argument("-p", "--patchwork-url", dest="patchwork_url", type=str,
help="The URL to your patchwork instance. Must support REST API.")
argp.add_argument("-s", "--sync", dest="sync", type=str,
help="Projects and lists to sync. " +
"In the format project1=list1@server1,project2=list2@server2")
argp.add_argument("-e", "--epoch", dest="epoch",
type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'),
help="Only consider patches on or after this date")
args = argp.parse_args(remaining_argv)
print(args)
pw_token = args.patchwork_token
nmdb = os.path.expanduser(args.notmuch_database)
sync_db = args.syncdb
conn = sqlite3.connect(sync_db)
conn.execute('''CREATE TABLE IF NOT EXISTS pw_patch_status (
msgid text,
project text,
need_sync bool,
patchid int unique,
state text,
PRIMARY KEY(msgid,project))''')
conn.execute('''CREATE TABLE IF NOT EXISTS nm_patch_status (
msgid text,
project text,
need_sync bool,
state text,
PRIMARY KEY(msgid,project))''')
conn.commit()
s = FuturesSession()
s.headers.update({ 'Authorization': 'Token {}'.format(pw_token) })
patchwork_url = patchwork_login(s, args.patchwork_url)
projects = get_projects(s, patchwork_url)
#print(repr(projects))
for project in args.sync.split(","):
project_name, project_list = project.split("=")
if project_name not in projects:
raise Exception("ERROR couldn't find project '%s'" % project_name)
print("Looking at project {} (id {})".format(project_name,projects[project_name]))
db = notmuch.Database(nmdb)
if args.epoch is None:
oldest_msg = get_oldest_nm_message(db, project_list)
else:
oldest_msg = args.epoch
print("Going to look at things post {}".format(oldest_msg))
populate_nm_patch_status(db,conn,project_name,all_my_tags)
db.close() # close the read-only session
# we now have a map of project names to IDs, so we can use that.
process_pw_patches_for_project(s, nmdb, conn, patchwork_url, project_name, projects[project_name], oldest_msg)
# We now know:
# 1) What changed locally (nm_patch_status.need_sync=1)
# 2) What changed remotely (pw_patch_status.need_sync=1)
# 3) What has update conflicts (union of 1 and 2)
# Current algorithm for conflicts is "take patchwork status"
# This syncs the DB for anything with a update conflict
#
# We've already applied the PW status in the loop above.
conn.execute("BEGIN")
conn.execute('''UPDATE nm_patch_status
SET need_sync=0
WHERE
project=? AND
msgid in (SELECT msgid FROM pw_patch_status WHERE project=? and need_sync=1)''', [project_name,project_name])
conn.commit()
# We're now left with need_sync=1 on nm_patch_status for only
# things we need to update in PW.
update_patchwork(s, conn, patchwork_url, project_name)
# Things only updated in PW, ignore them (we've forced state sync above)
conn.execute("UPDATE pw_patch_status set need_sync=0 WHERE project=? and need_sync=1", [project_name])
conn.commit()
#print(json.dumps(r.json(), indent=2))
def get_oldest_nm_message(db, project_list):
pw_list = 'to:{}'.format(project_list)
qstr = pw_list
#qstr = qstr + ' and not (tag:{}'.format(all_my_tags[0])
#for t in all_my_tags[1:]:
# qstr = qstr + ' or tag:{}'.format(t)
#qstr = qstr + ')'
q = notmuch.Query(db, qstr)
#q.exclude_tag('pwsync')
q.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
#q.set_sort(notmuch.Query.SORT.NEWEST_FIRST)
msgs = q.search_messages()
since = datetime.datetime.fromtimestamp(next(msgs).get_date())
return since
def insert_nm_patch_status(conn,message_id,project_name,tag):
conn.execute('''INSERT OR REPLACE INTO nm_patch_status
(msgid, project, state, need_sync)
VALUES (?,?,?,COALESCE((SELECT 1 FROM nm_patch_status WHERE msgid=? and project=? and state IS NOT ?),
(SELECT need_sync FROM nm_patch_status WHERE msgid=? and project=?),
0)
)''',
(message_id, project_name, tag,
message_id, project_name, tag,
message_id, project_name))
def populate_nm_patch_status(db,conn,project_name,all_my_tags):
for t in all_my_tags:
qstr = 'tag:pw-{} and tag:pw-{}-{}'.format(project_name,project_name,t)
q = notmuch.Query(db, qstr)
msgs = q.search_messages()
for m in msgs:
insert_nm_patch_status(conn,m.get_message_id(),project_name,t)
conn.commit()
def patchwork_login(session, url):
patchwork_url = url + '/api'
r = session.get(patchwork_url, stream=False).result()
if r.status_code != 200:
raise Exception("ERROR patchwork API request failed status = %d" % r.status_code)
patchwork_url = patchwork_url + '/1.0'
return patchwork_url
def get_projects(session, patchwork_url):
url = patchwork_url + '/projects'
projects = {}
while True:
r = session.get(url, params = {'per_page': 100}, stream=False).result()
p = r.json()
for project in p:
#print(("{}\t{}".format(project['id'],project['link_name'])))
projects[project['link_name']] = project['id']
if not r.links.get('next'):
break
url = r.links['next']['url']
return projects
def process_pw_patches(session, nmdb, conn, project_name, r):
nr_patches_processed = 0
not_approved = {}
done = False
while not done:
p = r.result().json()
# We open the DB for each batch as to not hold the notmuch
# database open blocking other writers for too long.
db = notmuch.Database(nmdb, mode=notmuch.Database.MODE.READ_WRITE)
# We initiate the async load of the next page now, as we go and make the
# changes to our local DBs.
if r.result().links.get('next'):
r = session.get(r.result().links['next']['url'], stream=False)
else:
# This is the last page.
done = True
db.begin_atomic()
for patch in p:
nr_patches_processed = nr_patches_processed + 1
conn.execute('''INSERT OR REPLACE INTO pw_patch_status
(msgid,project,patchid,state,need_sync)
VALUES (?,?,?,?,
COALESCE((SELECT 1 FROM pw_patch_status WHERE msgid=? and project=? and state IS NOT ?),
(SELECT need_sync FROM pw_patch_status WHERE msgid=? and project=?),
0)
)''',
(patch['msgid'][1:-1], project_name, patch['id'], patch['state'],
patch['msgid'][1:-1], project_name, patch['state'],
patch['msgid'][1:-1], project_name,
))
query_str = 'id:{}'.format(patch['msgid'][1:-1])
q = notmuch.Query(db, query_str)
msgs = q.search_messages()
try:
msg = next(msgs)
except StopIteration:
print("MESSAGE NOT FOUND: '{}' - skipping".format(query_str))
# If we don't have the message, just continue.
continue
# If we need to update PW, skip setting the tags in nm
c = conn.cursor()
c.execute("SELECT state from nm_patch_status WHERE msgid=? AND project=? AND need_sync=1",
[patch['msgid'][1:-1],project_name])
curstate = c.fetchone()
tag = patch['state']
if curstate:
print("Going to sync {} to patchwork for {}".format(patch['msgid'],project_name))
tag = curstate[0]
#msg.freeze()
msg.add_tag('pw-{}'.format(project_name))
msg.add_tag('patchwork')
for t in all_my_tags:
msg.remove_tag('pw-{}-{}'.format(project_name,t))
if tag in all_my_tags:
msg.add_tag('pw-{}-{}'.format(project_name,tag))
else:
if not_approved.get(tag):
not_approved[tag] = not_approved[tag] + 1
else:
not_approved[tag] = 1
#print("Not adding tag, as '{}'not in approved list".format(tag))
#msg.thaw()
insert_nm_patch_status(conn,patch['msgid'][1:-1],project_name,tag)
db.end_atomic()
conn.commit()
db.close()
print("Processed {} {} patches...".format(nr_patches_processed, project_name))
print("Finished processing {} {} patches!".format(nr_patches_processed, project_name))
print(not_approved)
def process_pw_patches_for_project(session, nmdb, conn, patchwork_url, project_name, project_id, oldest_msg):
patches_url = patchwork_url + '/patches'
r = session.get(patches_url, stream=False,
params={ 'per_page' : 500, 'since' : oldest_msg, 'project': project_id, })
process_pw_patches(session, nmdb, conn, project_name, r)
def update_patchwork(session, conn, patchwork_url, project_name):
cur = conn.cursor()
for row in cur.execute('''
SELECT
pw_patch_status.patchid AS patchid,
nm_patch_status.state as state,
nm_patch_status.msgid as msgid
FROM nm_patch_status, pw_patch_status
WHERE pw_patch_status.msgid=nm_patch_status.msgid
AND pw_patch_status.project=nm_patch_status.project
AND nm_patch_status.project=? and nm_patch_status.need_sync=1''', [project_name]):
print("Updating patch {} (id:{}) to {}".format(row[0],row[2],row[1]))
session.patch(patchwork_url + '/patches/{}/'.format(row[0]),
json={'state': row[1]}).result()
r = session.get(patchwork_url + '/patches/{}/'.format(row[0]))
p = r.result().json()
if row[1] == p['state']:
conn.execute("UPDATE nm_patch_status SET need_sync=0 WHERE msgid=? AND project=?", [row[2],project_name])
else:
print("ERROR State didn't update for {} - are you maintainer of {}?".format(row[0],project_name))
if __name__ == '__main__':
try:
main()
except Exception as e:
# Uncomment for stack trace
#import traceback
#traceback.print_exc()
print("Error", e)
sys.exit(1)
sys.exit(0)