Tri de boites de courriels

Top Page

Reply to this message
Author: Frederic Mantegazza
Date:  
To: ML Guilde
Subject: Tri de boites de courriels
Bonjour,

Voici un script ecrit en Python permettant de classer les courriels d'une
boite (type Unix) sur le champ 'Date:'. Comme vous le constatez, le script
est vraiment tout petit; c'est beau, Python (il faut que je m'y mette).

Ce script est ecrit par Benjamin Longuet, avec qui je travaille. Comme il
n'est pas inscrit sur la Guilde, si vous rencontrez des problemes avec son
script, pensez a lui envoyer un courriel directement (son adresse est dans
le script). Egalement si vous faites des ameliorations.

Bonne journee,

-- 
    Frederic

#!/usr/bin/env python

# python script to sort messages in a mailbox file by date
# file : sortMailbox.py
# $Revision: 1.1.1.1 $
# created by Benjamin Longuet, longuet@???
# you are free to use or modify this script

import sys
import mailbox
import rfc822
from string import find
from time import mktime
import tempfile

if (not 1 < len(sys.argv) < 4) or (sys.argv[1] in ('-h', '--help', '-?')):
    print """usage: sortMailbox.py input [output]
if output is not specified, input mailbox file is modified in place"""
    sys.exit()


try:
    fpin = open(sys.argv[1])
except IOError:
    sys.exit("It's impossible to read file %s" % sys.argv[1])


mbox = mailbox.UnixMailbox(fpin)
messages = []
offset = 0
msg = mbox.next()

try:
    while msg is not None:
        msg.absoluteStartOfBody = msg.startofbody + offset - 1
        msg.absoluteEndOfBody = mbox.seekp
        for line in msg.headers:
            if find(line, 'Date: ') == 0:
                date = line[6:-1]
                break
        else:
            raise 'MissingDate'
        try:
            msg.time = mktime(rfc822.parsedate(date))
        except TypeError, OverflowError:
            raise 'UnreadableDate'
        offset = mbox.seekp
        messages.append(msg)
        msg = mbox.next()
    else:
        if not len(messages): raise 'UnvalidMailbox'
    # sort the messages by date
    messages.sort(lambda m1, m2: cmp(m1.time, m2.time))
    # create a mailbox file with the messages sorted by date
    fptemp = tempfile.TemporaryFile()
    for msg in messages:
        fptemp.write(msg.unixfrom)
        fptemp.writelines(msg.headers)
        fpin.seek(msg.absoluteStartOfBody)
        body = fpin.read(msg.absoluteEndOfBody - msg.absoluteStartOfBody)
        fptemp.write(body)
    fpin.close()
    # copy the mailbox to the destination file
    try:
        fpout = open(sys.argv[len(sys.argv)-1], 'w')
    except IOError:
        raise 'outputFileError', sys.argv[len(sys.argv)-1]
    fptemp.seek(0)
    while 1:
        line = fptemp.readline()
        if not line: break
        fpout.write(line)
    fptemp.close()
    fpout.close()


except 'outputFileError', file:
    sys.exit("It's impossible to create output file %s" % file)
except 'UnvalidMailbox':
    sys.exit("The file %s is not a valid Unix-style mailbox" % sys.argv[1])
except 'MissingDate':
    sys.exit("At least one header's message hasn't date field")
except 'UnreadableDate':
    sys.exit("At least one header's message contains an unreadable date field")