script: fix mailmanUser indent

This commit is contained in:
Mathieu Maret 2018-11-21 16:13:19 +01:00
parent 8fbcb6eedc
commit 1f83da3b74

View File

@ -1,10 +1,11 @@
#!/usr/bin/python2 #!/usr/bin/python2
import subprocess import subprocess
import sys import sys
MAILMAN_HOME="/usr/lib/mailman/bin" MAILMAN_HOME = "/usr/lib/mailman/bin"
MAILING_PREFIX="noel" MAILING_PREFIX = "noel"
ADMIN_MAIL="mathieu@mathux.org" ADMIN_MAIL = "mathieu@mathux.org"
ADMIN_PWD="" ADMIN_PWD = ""
def getListUser(listName): def getListUser(listName):
cmd = MAILMAN_HOME+"/list_members "+listName cmd = MAILMAN_HOME+"/list_members "+listName
@ -17,13 +18,12 @@ def getListUser(listName):
(stdout, stderr) = proc.communicate() (stdout, stderr) = proc.communicate()
ret = proc.returncode ret = proc.returncode
if ret != 0: if ret != 0:
print "error when checking list %s : %s"%(listName, stderr) print "error when checking list %s : %s" % (listName, stderr)
return [] return []
return stdout.split('\n') return stdout.split('\n')
def getMailingLists(prefix):
def getMailingLists (prefix):
cmd = MAILMAN_HOME+"/list_lists -b" cmd = MAILMAN_HOME+"/list_lists -b"
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
shell=True, shell=True,
@ -34,16 +34,18 @@ def getMailingLists (prefix):
(stdout, stderr) = proc.communicate() (stdout, stderr) = proc.communicate()
ret = proc.returncode ret = proc.returncode
if ret != 0: if ret != 0:
print "error when getting mailing lists : %s"%stderr print "error when getting mailing lists : %s" % stderr
sys.exit(1) sys.exit(1)
lists=[] lists = []
for line in stdout.split('\n'): for line in stdout.split('\n'):
if line.startswith(prefix): if line.startswith(prefix):
lists.append(line) lists.append(line)
return lists return lists
def createList(name): def createList(name):
cmd = MAILMAN_HOME+"/newlist -a -l fr %s %s %s"%(name,ADMIN_MAIL,ADMIN_PWD) cmd = MAILMAN_HOME + \
"/newlist -a -l fr %s %s %s" % (name, ADMIN_MAIL, ADMIN_PWD)
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
shell=True, shell=True,
stdin=sys.stdin, stdin=sys.stdin,
@ -53,9 +55,10 @@ def createList(name):
(stdout, stderr) = proc.communicate() (stdout, stderr) = proc.communicate()
ret = proc.returncode ret = proc.returncode
if ret != 0: if ret != 0:
print "error when creating mailing lists : %s"%stderr print "error when creating mailing lists : %s" % stderr
sys.exit(1) sys.exit(1)
def listContainsUser(user, listName): def listContainsUser(user, listName):
return user in getListUser(listName) return user in getListUser(listName)
@ -63,7 +66,7 @@ def listContainsUser(user, listName):
def addUserToList(userEmail, listName): def addUserToList(userEmail, listName):
if listContainsUser(userEmail, listName): if listContainsUser(userEmail, listName):
return return
print "Add %s to list %s"%(userEmail, listName) print "Add %s to list %s" % (userEmail, listName)
cmd = MAILMAN_HOME+"/add_members -w n -r - "+listName cmd = MAILMAN_HOME+"/add_members -w n -r - "+listName
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
shell=True, shell=True,
@ -74,14 +77,14 @@ def addUserToList(userEmail, listName):
(stdout, stderr) = proc.communicate(input=""+userEmail) (stdout, stderr) = proc.communicate(input=""+userEmail)
ret = proc.returncode ret = proc.returncode
if ret != 0: if ret != 0:
print "error when adding user %s to list %s : %s"%(userEmail, listName, stderr) print "error when adding user %s to list %s : %s" % (userEmail, listName, stderr)
sys.exit(1) sys.exit(1)
def delUserFromList(userEmail, listName): def delUserFromList(userEmail, listName):
if not listContainsUser(userEmail, listName): if not listContainsUser(userEmail, listName):
return return
print "Remove %s to list %s"%(userEmail, listName) print "Remove %s from list %s" % (userEmail, listName)
cmd = MAILMAN_HOME+"/remove_members "+listName+" "+userEmail cmd = MAILMAN_HOME+"/remove_members "+listName+" "+userEmail
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,
shell=True, shell=True,
@ -93,22 +96,22 @@ def delUserFromList(userEmail, listName):
ret = proc.returncode ret = proc.returncode
#--------------------MAIN-------------------- # --------------------MAIN--------------------
if len(sys.argv) < 3: if len(sys.argv) < 3:
print "Usage %s nom email [ancien_email]"%sys.argv[0] print "Usage %s nom email [ancien_email]" % sys.argv[0]
print "nom : nom de la mailing list de l'user sans prefix (ex: celine et pas %s-celine). Sera creee si elle n'exite pas"%MAILING_PREFIX print "nom : nom de la mailing list de l'user sans prefix (ex: celine et pas %s-celine). Sera creee si elle n'exite pas" % MAILING_PREFIX
print "email : email a ajouter dans toutes les mailing lists sauf dans %s-nom"%MAILING_PREFIX print "email : email a ajouter dans toutes les mailing lists sauf dans %s-nom" % MAILING_PREFIX
print "ancien_email : email a supprimer de toutes les mailing lists" print "ancien_email : email a supprimer de toutes les mailing lists"
sys.exit(1) sys.exit(1)
mailingName = MAILING_PREFIX+"-"+sys.argv[1] mailingName = MAILING_PREFIX+"-"+sys.argv[1]
existingLists = getMailingLists(MAILING_PREFIX) existingLists = getMailingLists(MAILING_PREFIX)
print "existing lists : %s"%existingLists print "existing lists : %s" % existingLists
if mailingName not in existingLists: if mailingName not in existingLists:
print "Create list %s "%mailingName print "Create list %s " % mailingName
createList(mailingName) createList(mailingName)
#populate new mailing list with existing users # populate new mailing list with existing users
users = getListUser(MAILING_PREFIX) users = getListUser(MAILING_PREFIX)
for user in users: for user in users:
addUserToList(user, mailingName) addUserToList(user, mailingName)
@ -124,4 +127,3 @@ for listName in allButUserList:
if len(sys.argv) == 4: if len(sys.argv) == 4:
for listName in existingLists: for listName in existingLists:
delUserFromList(sys.argv[3], listName) delUserFromList(sys.argv[3], listName)