Peter's Blog 20.8.2004

2004-08-20

Hello from Dallas

Got a version of Visual Traceroute free on a cover disk. It's a souped-up traceroute that shows a world map and gives a host of peripheral information.

It confirms that this blog is hosted in Dallas, Texas and is on a Debian linux box. These days you don't always know where the servers may be.

Ooh Er, it even shows the town I live in if I search for my home server. Do I want the world to know?

My friend zugz uses NTL World for hosting and it seems he is hosted in Canada. He owes me some maple syrup.

posted at 18:48:48    #    comment []    trackback []
 

Maildir Email archiving

I've written a python script to handle email archives. This script scans my Maildir directories and moves messages that are older than a directory-dependant age to a compressed archive. I will run this from cron.

#!/usr/bin/python
#
# Walks through Maildir store, compressing and deleting old email messages.
#
import email
import os
import time
import bz2

strRootMailDir = '/home/pcw/Maildir'

#
# Folders that can be pruned
# Defined as a dictionary where keys are directory names and values 
# are maximum age in weeks
#
oPrunable = { '.IT.Auto.ISA Server Alert': 2,
        '.IT.Auto.Message Deleted': 2,
        '.IT.Auto.Sophos Alert': 2,
        '.IT.Auto.Sweep Report': 2,
        '.IT.Auto.Virus': 2,
        '.INBOX': 12,       # can sit in inbox for 3 months
        '.Sent': 4}

#
# Callback from os.walk to prune a particular directory.
#
def ProcessDir( strArg, strDir, strNames):
  #
  # Split tail part of directory name.
  # This should be cur, new, tmp etc.
  #
  strPartDir, strTailDir = os.path.split( strDir)

  if strTailDir != 'cur':
    return

  #
  # Get maildir mail folder name.
  #
  strPartDir, strMailDir = os.path.split( strPartDir)

  #
  # Make sure folder is in list that are subject to pruning.
  #
  if not oPrunable.has_key( strMailDir):
    return

  #
  # Go through the files.
  #
  for strFile in strNames:
    strPath = strDir + '/' + strFile

    #
    # Get file creation time. Don't look in message itself as sent time 
    # is untrustworthy. Process old files
    # 
    oStat = os.stat( strPath)
    nFileTime = oStat.st_ctime
    if time.time() - nFileTime > (3600 * 24 * 7 * oPrunable[strMailDir]):
      #
      # Attempt to parse message. Make sure it is a mail message.
      #
      oMessage = email.message_from_file( open(strPath))
      strFrom = oMessage['from']

      #
      # Archive old email comressed.
      # Shove in existing BZ2 archive in mbox format.
      # The mbox is readable by the python mailbox module.
      #
      oBZ2 = bz2.BZ2Compressor()
      oBZ2.compress( 'From %s  %s\n' % (strFrom,
            time.strftime( "%a %b %d %H:%M:%S %Y", time.localtime( nFileTime))))

      oBZ2.compress( open( strPath).read())
      oBZ2.compress( '\n')

      #
      # Create Archive directory, per maildir, per month.
      #
      strArchiveName = strRootMailDir + '/Archive.%s.%s.bz2' % (
                      strMailDir[1:],
                      time.strftime( '%b%Y', time.localtime( nFileTime)))
      open( strArchiveName, 'a').write( oBZ2.flush())

      #
      # Delete the file.
      #
      os.unlink( strPath)

os.path.walk( strRootMailDir, ProcessDir, '')

posted at 12:33:20    #    comment []    trackback []
August 2004
MoTuWeThFrSaSu
       1
2 3 4 5 6 7 8
9101112131415
16171819202122
23242526272829
3031     
Jul
2004
 Sep
2004

A blog documenting Peter's dabblings with Python, Gentoo Linux and any other cool toys he comes across.

XML-Image Letterimage

© 2004, Peter Wilkinson

Bisi and me