update_config_0.4.x-0.5.py
changeset 118 cf85d78486ce
parent 104 c0b5afb89088
child 119 014335f38962
equal deleted inserted replaced
117:c96b5768c76d 118:cf85d78486ce
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 # -*- coding: UTF-8 -*-
     2 # -*- coding: UTF-8 -*-
     3 # Copyright (c) 2008 - 2009, VEB IT
     3 # Copyright (c) 2008 - 2009, VEB IT
     4 # See COPYING for distribution information.
     4 # See COPYING for distribution information.
     5 
     5 
     6 import sys
     6 import os
       
     7 os.sys.path.remove(os.sys.path[0])
       
     8 from time import time
     7 from ConfigParser import ConfigParser
     9 from ConfigParser import ConfigParser
     8 from shutil import copy2
    10 from shutil import copy2
       
    11 from VirtualMailManager.constants.VERSION import VERSION
     9 
    12 
    10 cf = '/usr/local/etc/vmm.cfg'
       
    11 fh = file(cf, 'r')
       
    12 cp = ConfigParser()
       
    13 cp.readfp(fh)
       
    14 fh.close()
       
    15 
    13 
    16 if not cp.has_option('maildir', 'name') or not cp.has_option('maildir',
    14 def get_config_file():
    17         'folders') or cp.has_option('maildir', 'folder'):
    15     f = None
    18     copy2(cf, cf+'.bak_upd_0.4.x-0.5')
    16     for d in ('/root', '/usr/local/etc', '/etc'):
       
    17         if os.path.isfile(d+'/vmm.cfg'):
       
    18             f = d+'/vmm.cfg'
       
    19             break
       
    20     if f:
       
    21         return f
       
    22     else:
       
    23         os.sys.stderr.write('error: vmm.cfg not found\n')
       
    24         os.sys.exit(2)
       
    25 
       
    26 def update(cp):
       
    27     if VERSION == '0.4':
       
    28         upd_040(cp)
       
    29     elif VERSION == '0.5':
       
    30         upd_050(cp)
       
    31     elif VERSION == '0.5.1':
       
    32         upd_051(cp)
       
    33     else:
       
    34         os.sys.stderr.write(
       
    35             'error: the version %s is not supported by this script\n' % VERSION)
       
    36         os.sys.exit(3)
       
    37 
       
    38 def get_cfg_parser(cf):
       
    39     fh = file(cf, 'r')
       
    40     cp = ConfigParser()
       
    41     cp.readfp(fh)
       
    42     fh.close()
       
    43     return cp
       
    44 
       
    45 def update_cfg_file(cp, cf):
       
    46     copy2(cf, cf+'.bak.'+str(time()))
    19     fh = file(cf, 'w')
    47     fh = file(cf, 'w')
    20     if not cp.has_option('maildir', 'name'):
       
    21         if cp.has_option('maildir', 'folder'):
       
    22             cp.set('maildir', 'name', cp.get('maildir', 'folder'))
       
    23             cp.remove_option('maildir', 'folder')
       
    24         else:
       
    25             cp.set('maildir', 'name', 'Maildir')
       
    26     if not cp.has_option('maildir', 'folders'):
       
    27         cp.set('maildir', 'folders', 'Drafts:Sent:Templates:Trash')
       
    28     if cp.has_option('maildir', 'folder'):
       
    29         cp.remove_option('maildir', 'folder')
       
    30     cp.write(fh)
    48     cp.write(fh)
    31     fh.close()
    49     fh.close()
    32 
    50 
    33 if not cp.has_option('bin', 'postconf'):
    51 def upd_040(cp):
    34     fh = file(cf, 'w')
    52     if not cp.has_option('maildir', 'name') or not cp.has_option('maildir',
    35     try:
    53         'folders') or cp.has_option('maildir', 'folder'):
    36         postconf = sys.argv[1].strip()
    54         if not cp.has_option('maildir', 'name'):
    37         if len(postconf):
    55             if cp.has_option('maildir', 'folder'):
    38             cp.set('bin', 'postconf', postconf)
    56                 cp.set('maildir', 'name', cp.get('maildir', 'folder'))
    39         else: # possible?
    57                 cp.remove_option('maildir', 'folder')
       
    58                 sect_opt.append(('maildir', 'name'))
       
    59             else:
       
    60                 cp.set('maildir', 'name', 'Maildir')
       
    61                 sect_opt.append(('maildir', 'name'))
       
    62         if not cp.has_option('maildir', 'folders'):
       
    63             cp.set('maildir', 'folders', 'Drafts:Sent:Templates:Trash')
       
    64             sect_opt.append(('maildir', 'folders'))
       
    65         if cp.has_option('maildir', 'folder'):
       
    66             cp.remove_option('maildir', 'folder')
       
    67     upd_050(cp)
       
    68 
       
    69 def upd_050(cp):
       
    70     if not cp.has_option('bin', 'postconf'):
       
    71         try:
       
    72             postconf = os.sys.argv[1].strip()
       
    73             if len(postconf):
       
    74                 cp.set('bin', 'postconf', postconf)
       
    75                 sect_opt.append(('bin', 'postconf'))
       
    76             else: # possible?
       
    77                 cp.set('bin', 'postconf', '/usr/sbin/postconf')
       
    78                 sect_opt.append(('bin', 'postconf'))
       
    79         except IndexError:
    40             cp.set('bin', 'postconf', '/usr/sbin/postconf')
    80             cp.set('bin', 'postconf', '/usr/sbin/postconf')
    41     except IndexError:
    81             sect_opt.append(('bin', 'postconf'))
    42         cp.set('bin', 'postconf', '/usr/sbin/postconf')
    82     upd_051(cp)
    43     cp.write(fh)
       
    44     fh.close()
       
    45     print
       
    46     print "Please have a look at your %s" %cf
       
    47     print "and verify the value from option 'postconf' in section 'bin'."
       
    48     print
       
    49 
    83 
       
    84 def upd_051(cp):
       
    85     if not cp.has_option('misc', 'dovecotvers') or cp.has_option('services',
       
    86             'managesieve'):
       
    87         if not cp.has_option('misc', 'dovecotvers'):
       
    88             cp.set('misc', 'dovecotvers', os.sys.argv[2].strip())
       
    89             sect_opt.append(('misc', 'dovecotvers'))
       
    90         if cp.has_option('services', 'managesieve'):
       
    91             cp.set('services','sieve',cp.getboolean('services', 'managesieve'))
       
    92             cp.remove_option('services', 'managesieve')
       
    93             sect_opt.append(('services', 'sieve'))
       
    94 
       
    95 # def main():
       
    96 if __name__ == '__main__':
       
    97     sect_opt = []
       
    98     cf = get_config_file()
       
    99     cp = get_cfg_parser(cf)
       
   100     update(cp)
       
   101     if len(sect_opt): 
       
   102         update_cfg_file(cp, cf)
       
   103         print 'Please have a look at your configuration: %s' %cf
       
   104         print 'and verify the value from:'
       
   105         for s_o in sect_opt:
       
   106             print '  [%s] %s' % s_o
       
   107         print
       
   108 
       
   109