update_config.py
branchv0.6.x
changeset 167 bb58aedefa3a
parent 162 0ac9ef587769
child 168 fd496561acc6
equal deleted inserted replaced
166:b152ad5c7071 167:bb58aedefa3a
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # Copyright (c) 2008 - 2010, Pascal Volk
       
     4 # See COPYING for distribution information.
       
     5 
       
     6 import os
       
     7 os.sys.path.remove(os.sys.path[0])
       
     8 from time import time
       
     9 from ConfigParser import ConfigParser
       
    10 from shutil import copy2
       
    11 from VirtualMailManager.constants.VERSION import VERSION
       
    12 
       
    13 
       
    14 def get_config_file():
       
    15     f = None
       
    16     for d in ('/root', '/usr/local/etc', '/etc'):
       
    17         tmp = os.path.join(d, 'vmm.cfg')
       
    18         if os.path.isfile(tmp):
       
    19             f = tmp
       
    20             break
       
    21     if f:
       
    22         return f
       
    23     else:
       
    24         os.sys.stderr.write('error: vmm.cfg not found\n')
       
    25         raise SystemExit(2)
       
    26 
       
    27 def update(cp):
       
    28     if VERSION == '0.5.2':
       
    29         upd_052(cp)
       
    30     elif VERSION == '0.6.0':
       
    31         os.sys.stdout.write('info: nothing to do for version %s\n' % VERSION)
       
    32         return
       
    33     else:
       
    34         os.sys.stderr.write(
       
    35             'error: the version %s is not supported by this script\n' % VERSION)
       
    36         raise SystemExit(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()))
       
    47     fh = file(cf, 'w')
       
    48     cp.write(fh)
       
    49     fh.close()
       
    50 
       
    51 def add_sections(cp, sections):
       
    52     for section in sections:
       
    53         if not cp.has_section(section):
       
    54             cp.add_section(section)
       
    55 
       
    56 def move_option(cp, src, dst):
       
    57     ds, do = dst.split('.')
       
    58     if not cp.has_option(ds, do):
       
    59         ss, so = src.split('.')
       
    60         cp.set(ds, do, cp.get(ss, so))
       
    61         cp.remove_option(ss, so)
       
    62         sect_opt.append((dst, 'R'))
       
    63 
       
    64 def add_option(cp, dst, val):
       
    65     ds, do = dst.split('.')
       
    66     if not cp.has_option(ds, do):
       
    67         cp.set(ds, do, val)
       
    68         sect_opt.append((dst, 'N'))
       
    69 
       
    70 def get_option(cp, src):
       
    71     ss, so = src.split('.')
       
    72     return cp.get(ss, so)
       
    73 
       
    74 def upd_052(cp):
       
    75     add_sections(cp, ('domain', 'account'))
       
    76     if cp.has_section('domdir'):
       
    77         for src, dst in (('domdir.mode',   'domain.directory_mode'),
       
    78                          ('domdir.delete', 'domain.delete_directory'),
       
    79                          ('domdir.base',   'misc.base_dir')):
       
    80             move_option(cp, src, dst)
       
    81         cp.remove_section('domdir')
       
    82     if cp.has_section('services'):
       
    83         for service in cp.options('services'):
       
    84             move_option(cp, 'services.%s'%service, 'account.%s'%service)
       
    85         cp.remove_section('services')
       
    86     for src, dst in (('maildir.mode',      'account.directory_mode'),
       
    87                      ('maildir.diskusage', 'account.disk_usage'),
       
    88                      ('maildir.delete',    'account.delete_directory'),
       
    89                      ('misc.forcedel',     'domain.force_del'),
       
    90                      ('misc.passwdscheme', 'misc.password_scheme'),
       
    91                      ('misc.dovecotvers',  'misc.dovecot_vers')):
       
    92         move_option(cp, src, dst)
       
    93     for dst, val in (('account.random_password', 'false'),
       
    94                      ('account.password_len',    '8'),
       
    95                      ('domain.auto_postmaster',  'true')):
       
    96         add_option(cp, dst, val)
       
    97 
       
    98 # def main():
       
    99 if __name__ == '__main__':
       
   100     sect_opt = []
       
   101     cf = get_config_file()
       
   102     cp = get_cfg_parser(cf)
       
   103     update(cp)
       
   104     if len(sect_opt):
       
   105         update_cfg_file(cp, cf)
       
   106         sect_opt.sort()
       
   107         print 'Please have a look at your configuration: %s' %cf
       
   108         print 'This are your Renamed/New settings:'
       
   109         for s_o, st in sect_opt:
       
   110             print '%s   %s = %s' % (st, s_o, get_option(cp, s_o))
       
   111         print
       
   112