update_config.py
changeset 623 d7101e496795
parent 568 14abdd04ddf5
child 643 df1e3b67882a
child 675 d24f094d1cb5
equal deleted inserted replaced
622:e60b8ed5fd35 623:d7101e496795
     6 import os
     6 import os
     7 os.sys.path.remove(os.sys.path[0])
     7 os.sys.path.remove(os.sys.path[0])
     8 from time import time
     8 from time import time
     9 from ConfigParser import ConfigParser
     9 from ConfigParser import ConfigParser
    10 from shutil import copy2
    10 from shutil import copy2
       
    11 
       
    12 pre_060 = False
       
    13 
    11 try:
    14 try:
    12     from VirtualMailManager.constants.VERSION import VERSION
    15     from VirtualMailManager.constants.VERSION import VERSION
       
    16     pre_060 = True
    13 except ImportError:
    17 except ImportError:
    14     os.sys.stderr.write('error: no pre 0.6.0 version information found\n')
    18     try:
    15     raise SystemExit(2)
    19         from VirtualMailManager.constants import VERSION
       
    20     except ImportError:
       
    21         os.sys.stderr.write('error: no vmm version information found\n')
       
    22         raise SystemExit(2)
    16 
    23 
    17 # we have to remove the old CamelCase files
    24 # we have to remove the old CamelCase files
    18 import VirtualMailManager
    25 if pre_060:
    19 vmm_inst_dir = os.path.dirname(VirtualMailManager.__file__)
    26     import VirtualMailManager
    20 tmp_info = open('/tmp/vmm_inst_dir', 'w')
    27     vmm_inst_dir = os.path.dirname(VirtualMailManager.__file__)
    21 tmp_info.write(vmm_inst_dir)
    28     tmp_info = open('/tmp/vmm_inst_dir', 'w')
    22 tmp_info.close()
    29     tmp_info.write(vmm_inst_dir)
       
    30     tmp_info.close()
    23 
    31 
    24 try:
    32 try:
    25     import psycopg2
    33     import psycopg2
    26 except ImportError:
    34 except ImportError:
    27     has_psycopg2 = False
    35     has_psycopg2 = False
    28 else:
    36 else:
    29     has_psycopg2 = True
    37     has_psycopg2 = True
       
    38 
    30 
    39 
    31 def get_config_file():
    40 def get_config_file():
    32     f = None
    41     f = None
    33     for d in ('/root', '/usr/local/etc', '/etc'):
    42     for d in ('/root', '/usr/local/etc', '/etc'):
    34         tmp = os.path.join(d, 'vmm.cfg')
    43         tmp = os.path.join(d, 'vmm.cfg')
    39         return f
    48         return f
    40     else:
    49     else:
    41         os.sys.stderr.write('error: vmm.cfg not found\n')
    50         os.sys.stderr.write('error: vmm.cfg not found\n')
    42         raise SystemExit(2)
    51         raise SystemExit(2)
    43 
    52 
       
    53 
    44 def update(cp):
    54 def update(cp):
    45     if VERSION == '0.5.2':
    55     if VERSION == '0.5.2':
    46         upd_052(cp)
    56         upd_052(cp)
    47     elif VERSION == '0.6.0':
    57     elif VERSION == '0.6.0':
    48         os.sys.stdout.write('info: nothing to do for version %s\n' % VERSION)
    58         os.sys.stdout.write('info: vmm.cfg: nothing to do for version %s\n' %
       
    59                             VERSION)
    49         return
    60         return
    50     else:
    61     else:
    51         os.sys.stderr.write(
    62         os.sys.stderr.write('error: the version %s is not supported by this '
    52             'error: the version %s is not supported by this script\n' % VERSION)
    63                             'script\n' % VERSION)
    53         raise SystemExit(3)
    64         raise SystemExit(3)
       
    65 
    54 
    66 
    55 def get_cfg_parser(cf):
    67 def get_cfg_parser(cf):
    56     fh = open(cf, 'r')
    68     fh = open(cf, 'r')
    57     cp = ConfigParser()
    69     cp = ConfigParser()
    58     cp.readfp(fh)
    70     cp.readfp(fh)
    59     fh.close()
    71     fh.close()
    60     return cp
    72     return cp
    61 
    73 
       
    74 
    62 def update_cfg_file(cp, cf):
    75 def update_cfg_file(cp, cf):
    63     copy2(cf, cf+'.bak.'+str(time()))
    76     copy2(cf, cf + '.bak.' + str(time()))
    64     fh = open(cf, 'w')
    77     fh = open(cf, 'w')
    65     cp.write(fh)
    78     cp.write(fh)
    66     fh.close()
    79     fh.close()
       
    80 
    67 
    81 
    68 def add_sections(cp, sections):
    82 def add_sections(cp, sections):
    69     for section in sections:
    83     for section in sections:
    70         if not cp.has_section(section):
    84         if not cp.has_section(section):
    71             cp.add_section(section)
    85             cp.add_section(section)
       
    86 
    72 
    87 
    73 def move_option(cp, src, dst):
    88 def move_option(cp, src, dst):
    74     ds, do = dst.split('.')
    89     ds, do = dst.split('.')
    75     if not cp.has_option(ds, do):
    90     if not cp.has_option(ds, do):
    76         ss, so = src.split('.')
    91         ss, so = src.split('.')
    77         cp.set(ds, do, cp.get(ss, so))
    92         cp.set(ds, do, cp.get(ss, so))
    78         cp.remove_option(ss, so)
    93         cp.remove_option(ss, so)
    79         sect_opt.append((dst, 'R'))
    94         sect_opt.append((dst, 'R'))
       
    95 
    80 
    96 
    81 def add_option(cp, dst, val):
    97 def add_option(cp, dst, val):
    82     ds, do = dst.split('.')
    98     ds, do = dst.split('.')
    83     if not cp.has_option(ds, do):
    99     if not cp.has_option(ds, do):
    84         cp.set(ds, do, val)
   100         cp.set(ds, do, val)
    97 
   113 
    98 
   114 
    99 def get_option(cp, src):
   115 def get_option(cp, src):
   100     ss, so = src.split('.')
   116     ss, so = src.split('.')
   101     return cp.get(ss, so)
   117     return cp.get(ss, so)
       
   118 
   102 
   119 
   103 def upd_052(cp):
   120 def upd_052(cp):
   104     global had_config
   121     global had_config
   105     global had_gid_mail
   122     global had_gid_mail
   106 
   123 
   142     cp = get_cfg_parser(cf)
   159     cp = get_cfg_parser(cf)
   143     update(cp)
   160     update(cp)
   144     if len(sect_opt):
   161     if len(sect_opt):
   145         update_cfg_file(cp, cf)
   162         update_cfg_file(cp, cf)
   146         sect_opt.sort()
   163         sect_opt.sort()
   147         print 'Please have a look at your configuration: %s' %cf
   164         print 'Please have a look at your configuration: %s' % cf
   148         print 'This are your Modified/Renamed/New settings:'
   165         print 'This are your Modified/Renamed/New settings:'
   149         for s_o, st in sect_opt:
   166         for s_o, st in sect_opt:
   150             print '%s   %s = %s' % (st, s_o, get_option(cp, s_o))
   167             print '%s   %s = %s' % (st, s_o, get_option(cp, s_o))
   151         if had_config:
   168         if had_config:
   152             print '\nRemoved section "config" with option "done" (obsolte)'
   169             print '\nRemoved section "config" with option "done" (obsolte)'