thomas: utils/admin hash-imap-spool,1.2,1.3

cvs at kolab.org cvs at kolab.org
Wed Jan 23 17:43:36 CET 2008


Author: thomas

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

Modified Files:
	hash-imap-spool 
Log Message:
Various improvements for hash-imap-spool:
- Abort if chdir failed (otherwise bad things will happen, especially as root)
- Fix trailing spaces and indentation
- Use python in $PATH
- Suggest to use the user 'kolab-r'
- Don't shadow the built-ins dir and file
- Catch specific exceptions instead of any errors
- Make it importable from other scripts


Index: hash-imap-spool
===================================================================
RCS file: /kolabrepository/utils/admin/hash-imap-spool,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- hash-imap-spool	12 Oct 2007 12:47:12 -0000	1.2
+++ hash-imap-spool	23 Jan 2008 16:43:33 -0000	1.3
@@ -1,10 +1,10 @@
-#! /usr/bin/python
+#!/usr/bin/env python
 # -*- mode: python, coding: iso-8859-1 -*-
 # --------------------------------------------------------------------
 # hash-imap-spool
 # $Id$
 # --------------------------------------------------------------------
-# Copyright (C) 2007 by Intevation GmbH
+# Copyright (C) 2007, 2008 by Intevation GmbH
 # Author(s):
 # Sascha Wilde <wilde at intevation.de>
 
@@ -16,19 +16,20 @@
 
 Usage:
 
-   hash-imap-spool.py [CONFFILE]
+   hash-imap-spool [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
+The spool directories are read from the imapd.conf CONFFILE which
 defaults to /kolab/etc/imapd/imapd.conf if not given as an argument.
+
+Please run this script as the user 'kolab-r'.
 """
 
 import sys
 import os
-import os.path
 import re
 import string
 
@@ -41,8 +42,8 @@
         return "q"
 
 
-def tmphash_dir(dir, tmp_hash_dirs, dstdir=".", extradir=""):
-    hash_dir = "." + basic_hash(dir)
+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)
@@ -50,9 +51,10 @@
         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)
+        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):
@@ -63,24 +65,24 @@
 
 
 def hash_submb(name, tmp_hash_dirs):
-    for file in os.listdir("."):
-        if os.path.isdir(file):
-            tmphash_dir(file, tmp_hash_dirs, "..", name)
+    for file_ in os.listdir("."):
+        if os.path.isdir(file_):
+            tmphash_dir(file_, tmp_hash_dirs, "..", name)
             print ".",
 
 
 def hash_mailboxes():
     tmp_hash_dirs = []
-    for dir in os.listdir("."):
-        if dir == "user" or dir[0:6] == "shared":
+    for dir_ in os.listdir("."):
+        if dir_ == "user" or dir_[0:6] == "shared":
             old_cwd = os.getcwd()
-            os.chdir(dir)
-            hash_submb(dir, tmp_hash_dirs)
+            os.chdir(dir_)
+            hash_submb(dir_, tmp_hash_dirs)
             os.chdir(old_cwd)
-            if dir == "user": 
-                os.rmdir(dir)
+            if dir_ == "user":
+                os.rmdir(dir_)
                 continue
-        tmphash_dir(dir, tmp_hash_dirs)
+        tmphash_dir(dir_, tmp_hash_dirs)
         print ".",
     cleanup_tmp_hash_dirs(tmp_hash_dirs)
 
@@ -105,28 +107,30 @@
     print "Converting %s ..." % path
     try:
         os.chdir("%s/domain" % path)
-    except:
-        print "Can't enter `%s/domain'!" % path
+    except OSError, inst:
+        print "Can't change to spool directory."
+        print inst
+        sys.exit(23)
     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)
+        file_ = open(conffile)
 
-        for line in file:
+        for line in file_:
             m = spoolpath_re.match(line)
             if m:
-              paths.append(m.group("path"))
+                paths.append(m.group("path"))
         return paths
 
-    except:
-        print "Can't read configuration file `%s'." % conffile
+    except IOError, inst:
+        print "Can't read configuration file."
+        print inst
         sys.exit(23)
 
 
@@ -141,12 +145,11 @@
 
     paths = read_conffile(conffile)
 
-    if len(paths) > 0:
-        for path in paths:
-            hash_spool_dir(path)
+    for path in paths:
+        hash_spool_dir(path)
     else:
-        print "Could not find any spool directories in %s" % conffile
+        print "Could not find any spool directories in `%s'." % conffile
         sys.exit(23)
 
-
-main()
+if __name__ == "__main__":
+    main()





More information about the commits mailing list