mailbox.Maildir question/problem

Discussion in 'Python' started by tinnews@isbd.co.uk, Dec 11, 2007.

  1. Guest

    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
     
    , Dec 11, 2007
    #1
    1. Advertising

  2. Ross Ridge Guest

    <> 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]
    -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
    db //
     
    Ross Ridge, Dec 13, 2007
    #2
    1. Advertising

  3. Guest

    Ross Ridge <> wrote:
    > <> 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
     
    , Dec 14, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matej Cepl

    mailbox.py examples (Maildir)

    Matej Cepl, Aug 6, 2003, in forum: Python
    Replies:
    1
    Views:
    483
    David M. Cooke
    Aug 6, 2003
  2. Replies:
    3
    Views:
    470
  3. Replies:
    0
    Views:
    360
  4. Replies:
    0
    Views:
    412
  5. Replies:
    2
    Views:
    307
Loading...

Share This Page