Commit 59c7ae5b authored by Barry Warsaw's avatar Barry Warsaw

- Clean up and simplify remove_list()

- Boost test coverage of lifecycle.py to 100%
parent 11d3120d
......@@ -27,6 +27,7 @@ __all__ = [
import os
import errno
import shutil
import logging
......@@ -98,27 +99,13 @@ def create_list(fqdn_listname, owners=None, style_name=None):
def remove_list(mlist):
"""Remove the list and all associated artifacts and subscriptions."""
fqdn_listname = mlist.fqdn_listname
removeables = []
# Delete the mailing list from the database.
getUtility(IListManager).delete(mlist)
# Do the MTA-specific list deletion tasks
call_name(config.mta.incoming).delete(mlist)
# Remove the list directory.
removeables.append(os.path.join(config.LIST_DATA_DIR, fqdn_listname))
# Remove any stale locks associated with the list.
for filename in os.listdir(config.LOCK_DIR):
fn_listname, dot, rest = filename.partition('.')
if fn_listname == fqdn_listname:
removeables.append(os.path.join(config.LOCK_DIR, filename))
# Now that we know what files and directories to delete, delete them.
for target in removeables:
if not os.path.exists(target):
pass
elif os.path.islink(target):
os.unlink(target)
elif os.path.isdir(target):
shutil.rmtree(target)
elif os.path.isfile(target):
os.unlink(target)
else:
log.error('Could not delete list artifact: %s', target)
# Remove the list directory, if it exists.
try:
shutil.rmtree(os.path.join(config.LIST_DATA_DIR, fqdn_listname))
except OSError as error:
if error.errno != errno.ENOENT:
raise
......@@ -25,11 +25,14 @@ __all__ = [
]
import os
import shutil
import unittest
from mailman.config import config
from mailman.interfaces.address import InvalidEmailAddressError
from mailman.interfaces.domain import BadDomainSpecificationError
from mailman.app.lifecycle import create_list
from mailman.app.lifecycle import create_list, remove_list
from mailman.testing.layers import ConfigLayer
......@@ -48,3 +51,12 @@ class TestLifecycle(unittest.TestCase):
# Creating a list with an unregistered domain raises an exception.
self.assertRaises(BadDomainSpecificationError,
create_list, 'test@nodomain.example.org')
def test_remove_list_error(self):
# An error occurs while deleting the list's data directory.
mlist = create_list('test@example.com')
data_dir = os.path.join(config.LIST_DATA_DIR, mlist.fqdn_listname)
os.chmod(data_dir, 0)
self.addCleanup(shutil.rmtree, data_dir)
self.assertRaises(OSError, remove_list, mlist)
os.chmod(data_dir, 0o777)
......@@ -91,7 +91,6 @@ class Prototype:
mailbox = Maildir(list_dir, create=True, factory=None)
lock_file = os.path.join(
config.LOCK_DIR, '{0}-maildir.lock'.format(mlist.fqdn_listname))
# Lock the maildir as Maildir.add() is not threadsafe. Don't use the
# context manager because it's not an error if we can't acquire the
# archiver lock. We'll just log the problem and continue.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment