wilde: utils/admin hash-imap-spool,NONE,1.1

cvs at kolab.org cvs at kolab.org
Fri Oct 12 11:54:02 CEST 2007


Author: wilde

Update of /kolabrepository/utils/admin
In directory doto:/tmp/cvs-serv27383

Added Files:
	hash-imap-spool 
Log Message:
Added hash-imap-spool: converts plain spool dir to basic hashing.
  Initial version.

  Rational:

  Starting with version 2.1 Kolab Server activates "basic hashing" for
  the Cyrus IMAP spool directory.  Older versions of Kolab Server used
  no hashing for the spool directory, therefor the structure of the
  spool directory must be converted when upgrading to 2.1 or later.


--- NEW FILE: hash-imap-spool ---
#! /usr/bin/python
# -*- mode: python, coding: iso-8859-1 -*-
# --------------------------------------------------------------------
# hash-imap-spool
# $Id: hash-imap-spool,v 1.1 2007/10/12 09:54:00 wilde Exp $
# --------------------------------------------------------------------
# Copyright (C) 2007 by Intevation GmbH
# Author(s):
# Sascha Wilde <wilde at intevation.de>

# This program is free software under the GNU GPL (>=v2)
# Read the file COPYING coming with the software for details.
# -------------------------------------------------------------------

"""Convert plain imap spool to basic hashing."

Usage:

   hash-imap-spool.py [CONFFILE]

The program converts plain imap spool directory as found in Kolab
Server 2.0 to spool with basic hashing as needed for Kolab Server 2.1
and later.

The spool deirctories are read from the imapd.conf CONFFILE which
defaults to /kolab/etc/imapd/imapd.conf if not given as an argument.
"""

import sys
import os
import os.path
import re
import string


def basic_hash(name):
    c = name[0].lower()
    if c in string.ascii_lowercase:
        return c
    else:
        return "q"


def tmphash_dir(dir, tmp_hash_dirs, dstdir=".", extradir=""):
    hash_dir = "." + basic_hash(dir)
    if not hash_dir in tmp_hash_dirs:
        os.mkdir(dstdir + "/" + hash_dir, 0700)
        tmp_hash_dirs.append(hash_dir)
    if extradir:
        extradirpath = dstdir + "/" + hash_dir + "/" + extradir
        if not os.path.isdir(extradirpath):
            os.mkdir(extradirpath, 0700)
        if not extradir.endswith("/"): extradir = extradir + "/"
    dir_new = dstdir + "/" + hash_dir + "/" + extradir + dir
    os.rename(dir, dir_new)


def cleanup_tmp_hash_dirs(tmp_hash_dirs):
    for hash_dir in tmp_hash_dirs:
        os.rename(hash_dir, hash_dir[1])
        print ".",
    del tmp_hash_dirs[:]


def hash_users(tmp_hash_dirs):
    for user in os.listdir("."):
        tmphash_dir(user, tmp_hash_dirs, "..", "user")
        print ".",


def hash_mailboxes():
    tmp_hash_dirs = []
    for dir in os.listdir("."):
        if dir == "user":
            old_cwd = os.getcwd()
            os.chdir("user")
            hash_users(tmp_hash_dirs)
            os.chdir(old_cwd)
            os.rmdir("user")
        else:
            tmphash_dir(dir, tmp_hash_dirs)
            print ".",
    cleanup_tmp_hash_dirs(tmp_hash_dirs)


def hash_domains():
    tmp_hash_dirs = []
    for domain in os.listdir("."):
        print "Working on domain %s" % domain,
        old_cwd = os.getcwd()
        os.chdir(domain)
        hash_mailboxes()
        os.chdir(old_cwd)
        tmphash_dir(domain, tmp_hash_dirs)
        print ".",
        print "done"
    print "Cleaning up",
    cleanup_tmp_hash_dirs(tmp_hash_dirs)
    print "done"


def hash_spool_dir(path):
    print "Converting %s ..." % path
    try:
        os.chdir("%s/domain" % path)
    except:
        print "Can't enter `%s/domain'!" % path
    hash_domains()


def read_conffile(conffile):
    """Parse conffile and return list of paths to the partititions."""
    n = 0
    paths = []
    spoolpath_re = re.compile("^(?:meta)?partition-.*:\s+(?P<path>.+)")

    try:
        file = open(conffile)

        for line in file:
            m = spoolpath_re.match(line)
            if m:
              paths.append(m.group("path"))
        return paths

    except:
        print "Can't read configuration file `%s'." % conffile
        sys.exit(23)


def main():
    if len(sys.argv) == 1:
        conffile = '/kolab/etc/imapd/imapd.conf'
    elif len(sys.argv) == 2:
        conffile = sys.argv[1]
    else:
        print __doc__
        sys.exit(0)

    paths = read_conffile(conffile)

    if len(paths) > 0:
        for path in paths:
            hash_spool_dir(path)
    else:
        print "Could not find any spool directories in %s" % conffile
        sys.exit(23)


main()





More information about the commits mailing list