|
|
Subject: mailbox.Maildir question/problem
From: tinnews@isbd.co.uk
Date: 12/11/2007 9:15:04 PM
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.
--
Chris Green
Subject: mailbox.Maildir question/problem
From: Ross Ridge
Date: 12/13/2007 5:40:35 PM
<tinnews@isbd.co.uk> wrote:
>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
--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rridge@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Subject: mailbox.Maildir question/problem
From: tinnews@isbd.co.uk
Date: 12/14/2007 10:01:28 AM
Ross Ridge <rridge@caffeine.csclub.uwaterloo.ca> wrote:
> <tinnews@isbd.co.uk> wrote:
> >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.
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.
--
Chris Green
|