Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 149 lines (124 sloc) 3.63 KB
#! /usr/bin/python
import os
import sys
import codecs
import json
import mysql.connector
from mysql.connector.errors import IntegrityError
from subprocess import Popen, PIPE
os.environ['PATH'] = '/usr/lib/mailman/bin/:' + os.environ['PATH']
config = json.loads(open('backend/config.json').read())
def FetchOneAssoc(cursor):
data = cursor.fetchone()
if data == None:
return None
desc = cursor.description
dict = {}
for (name, value) in zip(desc, data):
dict[name[0]] = value
return dict
if sys.stdout.encoding == None:
sys.stdout = codecs.getwriter("UTF-8")(sys.stdout)
if sys.stderr.encoding == None:
sys.stderr = codecs.getwriter("UTF-8")(sys.stderr)
if len(sys.argv) != 3:
print("USAGE: " + sys.argv[0] + " <serie> <listname>")
sys.exit(2)
serie = sys.argv[1]
listname = sys.argv[2]
c = config['database']
conn = mysql.connector.connect(host=c['host'], database=c['database'], user=c['user'], password=c['password'])
cursor = conn.cursor()
cursor.execute(('''
SELECT id, mtime
FROM series_events
JOIN events USING (id)
WHERE serie = %s AND tag NOT IN (
SELECT base
FROM series_events
JOIN events USING (id)
WHERE serie = %s AND base IS NOT NULL)
ORDER BY events.date DESC LIMIT 1
'''), (serie, serie,))
event = FetchOneAssoc(cursor)
if event == None:
print >> sys.stderr, "No event without successor in serie %s" % serie
sys.exit(1)
cursor.execute(('''
SELECT id, mtime
FROM mailman_series
WHERE serie = %s AND listname = %s
'''), (serie, listname))
last_update = FetchOneAssoc(cursor)
if last_update and last_update['id'] == event['id'] and last_update['mtime'] == event['mtime']:
#print "Last event %s in serie %s has not changed" % (event['id'], serie)
sys.exit(0)
email_map = {}
old_emails = set()
cursor.execute(('''
SELECT email
FROM mailman_emails
WHERE listname = %s
'''), (listname,))
for (email,) in cursor:
lc = email.lower()
email_map[lc] = email
old_emails.add(lc)
new_emails = set()
cursor.execute(('''
SELECT email
FROM riders
WHERE id = %s AND email IS NOT NULL
AND email NOT IN (
SELECT email
FROM users
WHERE NOT notify
)
'''), (event['id'],))
for (email,) in cursor:
lc = email.lower()
email_map[lc] = email
new_emails.add(lc)
added_emails = new_emails - old_emails
removed_emails = old_emails - new_emails
pipe = None
for email in added_emails:
email = email_map[email]
#print('+ ' + email)
try:
cursor.execute(('''
INSERT INTO mailman_emails
SET listname = %s, email = %s
'''), (listname, email))
except IntegrityError:
continue
if pipe == None:
pipe = Popen(['add_members', '-r', '-', '-w', 'n', '-a', 'n', listname], shell=False, stdin=PIPE)
pipe.stdin = codecs.getwriter("UTF-8")(pipe.stdin)
pipe.stdin.write(email + '\n')
if pipe:
pipe.stdin.close()
pipe.wait()
pipe = None
for email in removed_emails:
email = email_map[email]
#print('- ' + email)
if pipe == None:
pipe = Popen(['remove_members', '-f', '-', '-n', '-N', listname], shell=False, stdin=PIPE)
pipe.stdin = codecs.getwriter("UTF-8")(pipe.stdin)
pipe.stdin.write(email + '\n')
cursor.execute(('''
DELETE FROM mailman_emails
WHERE listname = %s AND email = %s
'''), (listname, email))
if pipe:
pipe.stdin.close()
pipe.wait()
cursor.execute(('''
INSERT INTO mailman_series
SET listname = %s, serie = %s, id = %s, mtime = %s
ON DUPLICATE KEY UPDATE id = %s, mtime = %s
'''), (listname, serie, event['id'], event['mtime'], event['id'], event['mtime']))
cursor.close()
conn.commit()
conn.close()