update_config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 01 Mar 2010 05:31:43 +0000
branchv0.6.x
changeset 225 a51809f7940b
parent 181 866a6d679fce
child 229 0fb2f12648a7
permissions -rwxr-xr-x
VMM/Transport: reworked Transport class. Use assertions for argument checks. Removed methods getID() and getTransport(). This values are now accessible through the read-only attributes id and transport. VMM/{Account,Domain}: adjusted to modifications in Transport class. A few code cleanups.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
#!/usr/bin/env python
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 130
diff changeset
     3
# Copyright (c) 2008 - 2010, Pascal Volk
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
# See COPYING for distribution information.
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
     6
import os
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
     7
os.sys.path.remove(os.sys.path[0])
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
     8
from time import time
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
from ConfigParser import ConfigParser
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
from shutil import copy2
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    11
from VirtualMailManager.constants.VERSION import VERSION
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    13
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    14
def get_config_file():
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    15
    f = None
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    16
    for d in ('/root', '/usr/local/etc', '/etc'):
119
014335f38962 Small code cleanups (replaced dir+'/'+file by os.path.join()).
Pascal Volk <neverseen@users.sourceforge.net>
parents: 118
diff changeset
    17
        tmp = os.path.join(d, 'vmm.cfg')
014335f38962 Small code cleanups (replaced dir+'/'+file by os.path.join()).
Pascal Volk <neverseen@users.sourceforge.net>
parents: 118
diff changeset
    18
        if os.path.isfile(tmp):
014335f38962 Small code cleanups (replaced dir+'/'+file by os.path.join()).
Pascal Volk <neverseen@users.sourceforge.net>
parents: 118
diff changeset
    19
            f = tmp
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    20
            break
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    21
    if f:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    22
        return f
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    23
    else:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    24
        os.sys.stderr.write('error: vmm.cfg not found\n')
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    25
        raise SystemExit(2)
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    27
def update(cp):
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    28
    if VERSION == '0.5.2':
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    29
        upd_052(cp)
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    30
    elif VERSION == '0.6.0':
130
9dca3e898ddb avoid error message if used with version 0.5.2
Pascal Volk <neverseen@users.sourceforge.net>
parents: 119
diff changeset
    31
        os.sys.stdout.write('info: nothing to do for version %s\n' % VERSION)
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    32
        return
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    33
    else:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    34
        os.sys.stderr.write(
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    35
            'error: the version %s is not supported by this script\n' % VERSION)
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    36
        raise SystemExit(3)
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    37
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    38
def get_cfg_parser(cf):
181
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
    39
    fh = open(cf, 'r')
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    40
    cp = ConfigParser()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    41
    cp.readfp(fh)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    42
    fh.close()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    43
    return cp
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    44
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    45
def update_cfg_file(cp, cf):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    46
    copy2(cf, cf+'.bak.'+str(time()))
181
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
    47
    fh = open(cf, 'w')
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
    cp.write(fh)
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
    fh.close()
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    51
def add_sections(cp, sections):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    52
    for section in sections:
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    53
        if not cp.has_section(section):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    54
            cp.add_section(section)
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    55
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    56
def move_option(cp, src, dst):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    57
    ds, do = dst.split('.')
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    58
    if not cp.has_option(ds, do):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    59
        ss, so = src.split('.')
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    60
        cp.set(ds, do, cp.get(ss, so))
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    61
        cp.remove_option(ss, so)
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    62
        sect_opt.append((dst, 'R'))
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    63
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    64
def add_option(cp, dst, val):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    65
    ds, do = dst.split('.')
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    66
    if not cp.has_option(ds, do):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    67
        cp.set(ds, do, val)
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    68
        sect_opt.append((dst, 'N'))
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    69
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    70
def get_option(cp, src):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    71
    ss, so = src.split('.')
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    72
    return cp.get(ss, so)
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    73
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    74
def upd_052(cp):
181
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
    75
    global had_config
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
    76
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
    77
    had_config = cp.remove_section('config')
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    78
    add_sections(cp, ('domain', 'account'))
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    79
    if cp.has_section('domdir'):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    80
        for src, dst in (('domdir.mode',   'domain.directory_mode'),
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    81
                         ('domdir.delete', 'domain.delete_directory'),
168
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 167
diff changeset
    82
                         ('domdir.base',   'misc.base_directory')):
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    83
            move_option(cp, src, dst)
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    84
        cp.remove_section('domdir')
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    85
    if cp.has_section('services'):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    86
        for service in cp.options('services'):
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    87
            move_option(cp, 'services.%s'%service, 'account.%s'%service)
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    88
        cp.remove_section('services')
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    89
    for src, dst in (('maildir.mode',      'account.directory_mode'),
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    90
                     ('maildir.diskusage', 'account.disk_usage'),
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    91
                     ('maildir.delete',    'account.delete_directory'),
168
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 167
diff changeset
    92
                     ('misc.forcedel',     'domain.force_deletion'),
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    93
                     ('misc.passwdscheme', 'misc.password_scheme'),
168
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 167
diff changeset
    94
                     ('misc.dovecotvers',  'misc.dovecot_version')):
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    95
        move_option(cp, src, dst)
104
c0b5afb89088 Restored update_*_0.4.x-0.5.p*, modified upgrade.sh for v. 0.5.1
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    97
# def main():
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    98
if __name__ == '__main__':
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    99
    sect_opt = []
181
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
   100
    had_config = False
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   101
    cf = get_config_file()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   102
    cp = get_cfg_parser(cf)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   103
    update(cp)
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   104
    if len(sect_opt):
181
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
   105
        had_config = False
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   106
        update_cfg_file(cp, cf)
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   107
        sect_opt.sort()
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   108
        print 'Please have a look at your configuration: %s' %cf
167
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   109
        print 'This are your Renamed/New settings:'
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   110
        for s_o, st in sect_opt:
bb58aedefa3a update_config.py: adjusted functions stuff to renamed settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   111
            print '%s   %s = %s' % (st, s_o, get_option(cp, s_o))
181
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
   112
    if had_config:
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
   113
        update_cfg_file(cp, cf)
866a6d679fce update_config: do not add options w/ default values.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 168
diff changeset
   114
        print 'Removed section "config" with option "done" (obsolte)'
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   115
        print