mailbox.Maildir question/problem

T

tinnews

I am trying to write a utility to remove empty maildir mailboxes. It
sounds like this should be very simple but it's proving really
difficult.

I'm doing this on a Fedora 7 system with python 2.5.

The first question is how to detect whether a directory is a maildir
mailbox. The following code snippet *never* says that a directory is
not a maildir:-

try:
x = mailbox.Maildir(dirpath, None, False)
except:
print dirpath, "is not a maildir"

The "x = mailbox.Maildir(dirpath, None, False)" always succeeds even
when dirpath is most definitely *not* a maildir (i.e. it doesn't have
cur, new and tmp sub-directories). It's only when you try calling a
method of x that an exception results.


The second question is how to manage a hierarchy of directories with
maildirs in them. For example I have:-

Mail
Mail/bcs
Mail/ben
Mail/cg
Mail/spam
Mail/usenet

Where bcs ben cg spam usenet are maildir mailboxes, I can't get
python's mailbox.Maildir to do anything useful with them at all.

My test program currently is:-

#!/usr/bin/python
#
#
# Remove empty maildir mailboxes
#
import mailbox
import os.path
import sys

def checkDir(dummy, dirpath, filelist):
print "Directory is ", dirpath
try:
x = mailbox.Maildir(dirpath, None, False).list_folders()
except:
print dirpath, "is not a maildir"
return
for msg in x:
print msg

for d in sys.argv[1:]:
if os.path.isdir(d):
os.path.walk(d, checkDir, None)

It would seem that the list_folders() method only works with the
Courier style maildirs where the diretory name of the maildir starts
with a dot. Is there *any* way I can get python to access maildirs
which are not named using this (IMHO stupid) convention?

I know my test program is far from complete but I can't get it to do
anything sensible at present.
 
R

Ross Ridge

Is there *any* way I can get python to access maildirs
which are not named using this (IMHO stupid) convention?

Well, the mailbox module doesn't support deleting mailboxes, so I'm not
sure why you want to use it. Since you also seem to have a better idea
of what your maildirs look like, why not just use the lower level file
functions directly? Something like:

def remove_empty_maildir(dirname):
expected = set(["cur", "new", "tmp"])
ents = set(os.listdir(dirname))
if ents != expected:
if expected.issubset(ents):
raise error, "unexpected subdirs in maildir"
raise error, "not a maildir"
subdirs = [os.path.join(dirname, d)
for d in expected]
for d in subdirs:
if len(os.listdir(d)) != 0:
return False
for d in subdirs:
os.rmdir(d)
os.rmdir(dirname)
return True

Your case is presumably different somehow, so you'll have to update and
fix this completely untested code if you want to use it.

Ross Ridge
 
T

tinnews

Ross Ridge said:
Well, the mailbox module doesn't support deleting mailboxes, so I'm not
sure why you want to use it.

I was hoping to be able to use it for other things as well as deleting
mailboxes.

Since you also seem to have a better idea
of what your maildirs look like, why not just use the lower level file
functions directly? Something like:

def remove_empty_maildir(dirname):
expected = set(["cur", "new", "tmp"])
ents = set(os.listdir(dirname))
if ents != expected:
if expected.issubset(ents):
raise error, "unexpected subdirs in maildir"
raise error, "not a maildir"
subdirs = [os.path.join(dirname, d)
for d in expected]
for d in subdirs:
if len(os.listdir(d)) != 0:
return False
for d in subdirs:
os.rmdir(d)
os.rmdir(dirname)
return True

Your case is presumably different somehow, so you'll have to update and
fix this completely untested code if you want to use it.
I guess I will have to do something like this but the problem is more
subtle than that, what if another program writes a new message to the
mailbox just after you've checked that cur, new and tmp are all empty?
The whole point of maildir is that locking isn't needed and I was
hoping that the maildir() object in python would encapsulate correct
handling of the maildir including deletion.

As it is I will have to write code to do the correct handling,
presumably one checks the new directory last before deleting the whole
maildir and, if the deletion fails, someone must have put something
there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top