summaryrefslogtreecommitdiffstats
path: root/tools/upgrade/1.3/migrate_perms_to_mode.py
blob: 0aa9c574c5fb37f78b60b45215a694c76fa9558c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python

import lxml.etree
import os
import sys

import Bcfg2.Options


def setmodeattr(elem):
    """Set the mode attribute for a given element."""
    if elem.attrib.has_key('perms'):
        elem.set('mode', elem.get('perms'))
        del elem.attrib['perms']
        return True


def writefile(f, xdata):
    """Write xml data to a file"""
    newfile = open(f, 'w')
    newfile.write(lxml.etree.tostring(xdata, pretty_print=True))
    newfile.close()


def convertinfo(ifile):
    """Do perms -> mode conversion for info.xml files."""
    xdata = lxml.etree.parse(ifile)
    found = False
    for i in xdata.findall('//Info'):
        found = setmodeattr(i)
    if found:
        writefile(ifile, xdata)


def convertstructure(structfile):
    """Do perms -> mode conversion for structure files."""
    xdata = lxml.etree.parse(structfile)
    found = False
    for path in xdata.findall('//BoundPath'):
        found = setmodeattr(path)
    for path in xdata.findall('//Path'):
        found = setmodeattr(path)
    if found:
        writefile(structfile, xdata)


def main():
    opts = dict(repo=Bcfg2.Options.SERVER_REPOSITORY,
                configfile=Bcfg2.Options.CFILE,
                plugins=Bcfg2.Options.SERVER_PLUGINS)
    setup = Bcfg2.Options.OptionParser(opts)
    setup.parse(sys.argv[1:])
    repo = setup['repo']

    for plugin in setup['plugins']:
        if plugin in ['Base', 'Bundler', 'Rules']:
            for root, dirs, files in os.walk(os.path.join(repo, plugin)):
                for fname in files:
                    convertstructure(os.path.join(root, fname))
        if plugin not in ['Cfg', 'TGenshi', 'TCheetah']:
            continue
        for root, dirs, files in os.walk(os.path.join(repo, plugin)):
            for fname in files:
                if fname == 'info.xml':
                    convertinfo(os.path.join(root, fname))

if __name__ == '__main__':
    sys.exit(main())