* 'VirtualMailManager/Exceptions.py' vmm-0.3.1
authorPascal Volk <neverseen@users.sourceforge.net>
Tue, 08 Jan 2008 21:35:40 +0000
changeset 2 9b39f828aa8a
parent 1 d08cda9d7c1a
child 3 a9b44e04bf01
* 'VirtualMailManager/Exceptions.py' - VMMNotRootException.__init__() calls parent CTor * 'VirtualMailManager/VirtualMailManager.py' - activated check for missing sections/options * 'VirtualMailManager/constants/ERROR.py' - added CONF_* constants from EXIT.py - renumbered * 'VirtualMailManager/constants/EXIT.py' - moved CONF_* constants to ERROR.py * 'VirtualMailManager/Config.py' - implemented VMMConfig.check() - some code cleanups * 'vmm' - some code cleanups
VirtualMailManager/Config.py
VirtualMailManager/Exceptions.py
VirtualMailManager/VirtualMailManager.py
VirtualMailManager/constants/ERROR.py
VirtualMailManager/constants/EXIT.py
setup.py
vmm
--- a/VirtualMailManager/Config.py	Tue Jan 08 12:30:20 2008 +0000
+++ b/VirtualMailManager/Config.py	Tue Jan 08 21:35:40 2008 +0000
@@ -16,18 +16,18 @@
 import sys
 from shutil import copy2
 from ConfigParser import ConfigParser
+from cStringIO import StringIO
 
 from Exceptions import VMMConfigException
-import constants.EXIT as EXIT
+import constants.ERROR as ERR
 
 class VMMConfig(ConfigParser):
-    """This class is for configure the mailadmin.
+    """This class is for configure the Virtual Mail Manager.
 
     You can specify settings for the database connection
     and maildirectories.
 
     """
-    missingOptCtr = -1
 
     def __init__(self, filename):
         """Creates a new VMMConfig instance
@@ -41,7 +41,7 @@
         self.__VMMsections = ['database', 'maildir', 'domdir', 'bin', 'misc',
                 'config']
         self.__changes = False
-        self.__missingSect = []
+        self.__missing = {}
         self.__dbopts = [
                 ['host', 'localhot'],
                 ['user', 'vmm'],
@@ -78,6 +78,18 @@
         self.readfp(self.__cfgFile)
         self.__cfgFile.close()
 
+    def check(self):
+        if not self.__chkSections():
+            errmsg = StringIO()
+            for k,v in self.__missing.items():
+                if v[0] is True:
+                    errmsg.write("missing section: %s\n" % k)
+                else:
+                    errmsg.write("missing options in section %s:\n" % k)
+                    for o in v:
+                        errmsg.write(" * %s\n" % o)
+            raise VMMConfigException((errmsg.getvalue(), ERR.CONF_ERROR))
+
     def getsections(self):
         """Return a list with all configurable sections."""
         return self.__VMMsections[:-1]
@@ -122,13 +134,13 @@
 
     def __chkSections(self):
         """Checks if all configuration sections are existing."""
-        retval = False
+        errors = False
         for s in self.__VMMsections:
             if not self.has_section(s):
-                self.__missingSect.append(s)
-            else:
-                retval = self.__chkOptions(s)
-        return retval
+                self.__missing[s] = [True]
+            elif not self.__chkOptions(s):
+                errors = True
+        return not errors
 
     def __chkOptions(self, section):
         """Checks if all configuration options in section are existing.
@@ -137,18 +149,23 @@
         section -- the section to be checked
         """
         retval = True
-        VMMConfig.missingOptCtr += 1
-        self.__missingOpt.append([])
+        missing = []
         if section == 'database':
             opts = self.__dbopts
         elif section == 'maildir':
             opts = self.__mdopts
+        elif section == 'domdir':
+            opts = self.__domdopts
         elif section == 'bin':
             opts = self.__binopts
         elif section == 'misc':
             opts = self.__miscopts
+        elif section == 'config':
+            opts = [['done', 'false']]
         for o, v in opts:
             if not self.has_option(section, o):
-                self.__missingOpt[VMMConfig.missingOptCtr].append(o)
+                missing.append(o)
                 retval = False
+        if len(missing):
+            self.__missing[section] = missing
         return retval
--- a/VirtualMailManager/Exceptions.py	Tue Jan 08 12:30:20 2008 +0000
+++ b/VirtualMailManager/Exceptions.py	Tue Jan 08 21:35:40 2008 +0000
@@ -26,7 +26,8 @@
 
 class VMMNotRootException(Exception):
     """Ausnahmeklasse für unberechtige Zugriffe"""
-    pass
+    def __init__(self, msg):
+        Exception.__init__(self, msg)
 
 class VMMDomainException(VMMException):
     """Ausnahmeklasse für Domainausnamhem"""
--- a/VirtualMailManager/VirtualMailManager.py	Tue Jan 08 12:30:20 2008 +0000
+++ b/VirtualMailManager/VirtualMailManager.py	Tue Jan 08 21:35:40 2008 +0000
@@ -49,18 +49,20 @@
         self.__dbh = None
 
         if os.geteuid():
-            raise VMMNotRootException("You are not root.\n\tGood bye!\n")
+            raise VMMNotRootException(("You are not root.\n\tGood bye!\n",
+                ERR.CONF_NOPERM))
         if self.__chkCfgFile():
             self.__Cfg = Cfg(self.__cfgFileName)
             self.__Cfg.load()
+            self.__Cfg.check()
             self.__cfgSections = self.__Cfg.getsections()
         self.__chkenv()
 
     def __chkCfgFile(self):
         """Checks the configuration file, returns bool"""
         if not os.path.isfile(self.__cfgFileName):
-            raise IOError("Fatal error: The file "+self.__cfgFileName+ \
-                    " does not exists.\n")
+            raise VMMException(("The file »%s« does not exists." %
+                self.__cfgFileName, ERR.CONF_NOFILE))
         fstat = os.stat(self.__cfgFileName)
         try:
             fmode = self.__getFileMode()
@@ -68,7 +70,7 @@
             raise
         if fmode % 100 and fstat.st_uid != fstat.st_gid \
         or fmode % 10 and fstat.st_uid == fstat.st_gid:
-            raise VMMPermException(self.__permWarnMsg)
+            raise VMMPermException((self.__permWarnMsg, ERR.CONF_ERROR))
         else:
             return True
 
--- a/VirtualMailManager/constants/ERROR.py	Tue Jan 08 12:30:20 2008 +0000
+++ b/VirtualMailManager/constants/ERROR.py	Tue Jan 08 21:35:40 2008 +0000
@@ -11,21 +11,25 @@
 ALIAS_EXISTS = 24
 ALIAS_MISSING_DEST = 25
 ALIAS_PRESENT = 26
-DATABASE_ERROR = 27
-DOMAINDIR_GROUP_MISMATCH = 28
-DOMAIN_EXISTS = 29
-DOMAIN_INVALID = 30
-DOMAIN_TOO_LONG = 31
-FOUND_DOTS_IN_PATH = 32
-INVALID_ADDRESS = 33
-INVALID_OPTION = 34
-INVALID_SECTION = 35
-LOCALPART_INVALID = 36
-LOCALPART_TOO_LONG = 37
-MAILDIR_PERM_MISMATCH = 38
-NOT_EXECUTABLE = 39
-NO_SUCH_ACCOUNT = 40
-NO_SUCH_ALIAS = 41
-NO_SUCH_BINARY = 42
-NO_SUCH_DIRECTORY = 43
-NO_SUCH_DOMAIN = 44
+CONF_WRONGPERM = 27
+CONF_NOPERM = 28
+CONF_NOFILE = 29
+CONF_ERROR = 30
+DATABASE_ERROR = 31
+DOMAINDIR_GROUP_MISMATCH = 32
+DOMAIN_EXISTS = 33
+DOMAIN_INVALID = 34
+DOMAIN_TOO_LONG = 35
+FOUND_DOTS_IN_PATH = 36
+INVALID_ADDRESS = 37
+INVALID_OPTION = 38
+INVALID_SECTION = 39
+LOCALPART_INVALID = 40
+LOCALPART_TOO_LONG = 41
+MAILDIR_PERM_MISMATCH = 42
+NOT_EXECUTABLE = 43
+NO_SUCH_ACCOUNT = 44
+NO_SUCH_ALIAS = 45
+NO_SUCH_BINARY = 46
+NO_SUCH_DIRECTORY = 47
+NO_SUCH_DOMAIN = 48
--- a/VirtualMailManager/constants/EXIT.py	Tue Jan 08 12:30:20 2008 +0000
+++ b/VirtualMailManager/constants/EXIT.py	Tue Jan 08 21:35:40 2008 +0000
@@ -7,8 +7,3 @@
 MISSING_ARGS = 1
 UNKNOWN_OPTION = 2
 USER_INTERRUPT = 3
-
-CONF_WRONGPERM = 76
-CONF_NOPERM = 77
-CONF_NOFILE = 78
-CONF_ERROR = 79
--- a/setup.py	Tue Jan 08 12:30:20 2008 +0000
+++ b/setup.py	Tue Jan 08 21:35:40 2008 +0000
@@ -7,7 +7,7 @@
 import os
 from distutils.core import setup
 
-VERSION = '0.3'
+VERSION = '0.3.1'
 
 long_description = """
 Virtual Mail Manager is a command line tool for administrators/postmasters to
@@ -27,26 +27,11 @@
       description='Tool to manage mail domains/accounts/aliases for Dovecot and Postfix',
       long_description=long_description,
       packages=['VirtualMailManager', 'VirtualMailManager.constants'],
-#      data_files=[(libdir, [
-#          'VirtualMailManager/Account.py',
-#          'VirtualMailManager/Alias.py',
-#          'VirtualMailManager/Config.py',
-#          'VirtualMailManager/Domain.py',
-#          'VirtualMailManager/Exceptions.py',
-#          'VirtualMailManager/__init__.py',
-#          'VirtualMailManager/VirtualMailManager.py']
-#          ),
-#          (libdir+'/constants', [
-#              'VirtualMailManager/constants/ERROR.py',
-#              'VirtualMailManager/constants/EXIT.py',
-#              'VirtualMailManager/constants/__init__.py']
-#          )
-#      ],
       author='Pascal Volk',
       author_email='p.volk@veb-it.de',
       license='BSD License',
       url='http://vmm.sf.net/',
-      download_url='http://sourceforge.net/project/showfiles.php?group_id=213727',
+      download_url='http://sf.net/project/showfiles.php?group_id=213727',
       classifiers=[
           'Development Status :: 4 - Beta',
           'Development Status :: 5 - Production/Stable',
--- a/vmm	Tue Jan 08 12:30:20 2008 +0000
+++ b/vmm	Tue Jan 08 21:35:40 2008 +0000
@@ -14,8 +14,6 @@
 import sys
 from getpass import getpass
 
-#sys.path.insert(0, '/usr/local/lib/VirtualMailManager')
-
 from VirtualMailManager.VirtualMailManager import VirtualMailManager
 from VirtualMailManager.Config import VMMConfig
 import VirtualMailManager.Exceptions as VMME
@@ -54,15 +52,10 @@
     try:
         vmm = VirtualMailManager()
         return vmm
-    except VMME.VMMNotRootException, e:
-        sys.stderr.write(str(e))
-        sys.exit(EXIT.CONF_NOPERM)
-    except IOError, e:
-        sys.stderr.write(str(e))
-        sys.exit(EXIT.CONF_NOFILE)
-    except VMME.VMMPermException, e:
-        sys.stderr.write(str(e))
-        sys.exit(EXIT.CONF_WRONGPERM)
+    except (VMME.VMMException, VMME.VMMNotRootException, VMME.VMMPermException,
+            VMME.VMMConfigException), e:
+        sys.stderr.write("\aERROR: %s\n" % e[0][0])
+        sys.exit(e[0][1])
 
 def configure():
     try:
@@ -187,8 +180,10 @@
 
 def user_name():
     global argc
+    if argc < 3:
+        usage(EXIT.MISSING_ARGS, 'Missing email address and users name.')
     if argc < 4:
-        usage(EXIT.MISSING_ARGS, 'Missing email address an users name.')
+        usage(EXIT.MISSING_ARGS, 'Missing users name.')
     else:
         vmm.user_name(sys.argv[2].lower(), sys.argv[3])
 
@@ -293,8 +288,7 @@
             print "%s: %s (Date: %s)\n" % (os.path.basename(sys.argv[0]),
                     __version__, __date__)
         else:
-            sys.stderr.write('Unknown option: "%s"\n' % sys.argv[1])
-            usage(EXIT.UNKNOWN_OPTION)
+            usage(EXIT.UNKNOWN_OPTION, 'Unknown option: »%s«' % sys.argv[1])
         showWarnings()
     except (EOFError, KeyboardInterrupt):
         sys.stderr.write('\nOuch!\n')