update_config_0.4.x-0.5.py
author Pascal Volk <neverseen@users.sourceforge.net>
Fri, 04 Sep 2009 19:38:50 +0000
changeset 141 ecd6a379e523
parent 130 9dca3e898ddb
child 162 0ac9ef587769
permissions -rwxr-xr-x
Small optimization in w_std()/w_err() 'avoid the dot'.
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 -*-
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
     3
# Copyright (c) 2008 - 2009, VEB IT
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')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    25
        os.sys.exit(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):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    28
    if VERSION == '0.4':
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    29
        upd_040(cp)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    30
    elif VERSION == '0.5':
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    31
        upd_050(cp)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    32
    elif VERSION == '0.5.1':
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    33
        upd_051(cp)
130
9dca3e898ddb avoid error message if used with version 0.5.2
Pascal Volk <neverseen@users.sourceforge.net>
parents: 119
diff changeset
    34
    elif VERSION == '0.5.2':
9dca3e898ddb avoid error message if used with version 0.5.2
Pascal Volk <neverseen@users.sourceforge.net>
parents: 119
diff changeset
    35
        os.sys.stdout.write('info: nothing to do for version %s\n' % VERSION)
9dca3e898ddb avoid error message if used with version 0.5.2
Pascal Volk <neverseen@users.sourceforge.net>
parents: 119
diff changeset
    36
        os.sys.exit(0)
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    37
    else:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    38
        os.sys.stderr.write(
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    39
            'error: the version %s is not supported by this script\n' % VERSION)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    40
        os.sys.exit(3)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    41
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    42
def get_cfg_parser(cf):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    43
    fh = file(cf, 'r')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    44
    cp = ConfigParser()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    45
    cp.readfp(fh)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    46
    fh.close()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    47
    return cp
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    48
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    49
def update_cfg_file(cp, cf):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    50
    copy2(cf, cf+'.bak.'+str(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
    51
    fh = file(cf, 'w')
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
    52
    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
    53
    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
    54
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    55
def upd_040(cp):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    56
    if not cp.has_option('maildir', 'name') or not cp.has_option('maildir',
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    57
        'folders') or cp.has_option('maildir', 'folder'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    58
        if not cp.has_option('maildir', 'name'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    59
            if cp.has_option('maildir', 'folder'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    60
                cp.set('maildir', 'name', cp.get('maildir', 'folder'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    61
                cp.remove_option('maildir', 'folder')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    62
                sect_opt.append(('maildir', 'name'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    63
            else:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    64
                cp.set('maildir', 'name', 'Maildir')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    65
                sect_opt.append(('maildir', 'name'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    66
        if not cp.has_option('maildir', 'folders'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    67
            cp.set('maildir', 'folders', 'Drafts:Sent:Templates:Trash')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    68
            sect_opt.append(('maildir', 'folders'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    69
        if cp.has_option('maildir', 'folder'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    70
            cp.remove_option('maildir', 'folder')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    71
    upd_050(cp)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    72
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    73
def upd_050(cp):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    74
    if not cp.has_option('bin', 'postconf'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    75
        try:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    76
            postconf = os.sys.argv[1].strip()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    77
            if len(postconf):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    78
                cp.set('bin', 'postconf', postconf)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    79
                sect_opt.append(('bin', 'postconf'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    80
            else: # possible?
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    81
                cp.set('bin', 'postconf', '/usr/sbin/postconf')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    82
                sect_opt.append(('bin', 'postconf'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    83
        except IndexError:
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
    84
            cp.set('bin', 'postconf', '/usr/sbin/postconf')
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    85
            sect_opt.append(('bin', 'postconf'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    86
    upd_051(cp)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    87
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    88
def upd_051(cp):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    89
    if not cp.has_option('misc', 'dovecotvers') or cp.has_option('services',
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    90
            'managesieve'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    91
        if not cp.has_option('misc', 'dovecotvers'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    92
            cp.set('misc', 'dovecotvers', os.sys.argv[2].strip())
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    93
            sect_opt.append(('misc', 'dovecotvers'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    94
        if cp.has_option('services', 'managesieve'):
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    95
            cp.set('services','sieve',cp.getboolean('services', 'managesieve'))
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    96
            cp.remove_option('services', 'managesieve')
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    97
            sect_opt.append(('services', 'sieve'))
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
    98
118
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
    99
# def main():
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   100
if __name__ == '__main__':
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   101
    sect_opt = []
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   102
    cf = get_config_file()
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   103
    cp = get_cfg_parser(cf)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   104
    update(cp)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   105
    if len(sect_opt): 
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   106
        update_cfg_file(cp, cf)
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   107
        print 'Please have a look at your configuration: %s' %cf
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   108
        print 'and verify the value from:'
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   109
        for s_o in sect_opt:
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   110
            print '  [%s] %s' % s_o
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   111
        print
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   112
cf85d78486ce Rewrote upgrade script and config update script
Pascal Volk <neverseen@users.sourceforge.net>
parents: 104
diff changeset
   113