summaryrefslogtreecommitdiffstats
path: root/tools/upgrade/1.3/migrate_perms_to_mode.py
blob: c825c84d27d4152f9527206d19fc60e23081711b (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python

import lxml.etree
import os
import sys
from fnmatch import fnmatch
from Bcfg2.Compat import any  # pylint: disable=W0622
from Bcfg2.Server.FileMonitor import FileMonitor
import Bcfg2.Options


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


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."""
    try:
        xdata = lxml.etree.parse(ifile)
    except lxml.etree.XMLSyntaxError:
        err = sys.exc_info()[1]
        print("Could not parse %s, skipping: %s" % (ifile, err))
        return
    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."""
    try:
        xdata = lxml.etree.parse(structfile)
    except lxml.etree.XMLSyntaxError:
        err = sys.exc_info()[1]
        print("Could not parse %s, skipping: %s" % (structfile, err))
        return
    found = False
    for path in xdata.xpath('//BoundPath|//Path'):
        found |= setmodeattr(path)
    if found:
        writefile(structfile, xdata)


def skip_path(path):
    return any(fnmatch(path, p) or fnmatch(os.path.basename(path), p)
               for p in Bcfg2.Options.setup.ignore_files)


def main():
    parser = Bcfg2.Options.get_parser(
        description="Migrate from Bcfg2 1.2 'perms' attribute to 1.3 'mode' "
        "attribute",
        components=[FileMonitor])
    parser.add_options([Bcfg2.Options.Common.repository,
                        Bcfg2.Options.Common.plugins])
    parser.parse()
    repo = Bcfg2.Options.setup.repository

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

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