summaryrefslogtreecommitdiffstats
path: root/tools/posixunified.py
blob: 8eb4ed7348095a00f392a4fcffa1a4bf192b6622 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python

from copy import deepcopy
import lxml.etree
import os
import sys

import Bcfg2.Options

"""
NOTE: This script takes a conservative approach when it comes to
      updating your Rules. It creates a new unified-rules.xml file
      without the attributes you have defined in your current rules. The
      reason for this is to keep this script simple so we don't have
      to go through and determine the priorities associated with your
      current rules definitions.
"""

if __name__ == '__main__':
    opts = {
               'repo': Bcfg2.Options.SERVER_REPOSITORY,
           }
    setup = Bcfg2.Options.OptionParser(opts)
    setup.parse(sys.argv[1:])
    repo = setup['repo']
    unifiedposixrules = "%s/Rules/unified-rules.xml" % repo
    rulesroot = lxml.etree.Element("Rules")

    for plug in ['Base', 'Bundler']:
        for root, dirs, files in os.walk('%s/%s' % (repo, plug)):
            if '.svn' in dirs:
                dirs.remove('.svn')
            for filename in files:
                if filename.startswith('new'):
                    continue
                xdata = lxml.etree.parse(os.path.join(root, filename))
                # replace ConfigFile elements
                for c in xdata.findall('//ConfigFile'):
                    parent = c.getparent()
                    oldc = c
                    c.tag = 'Path'
                    parent.replace(oldc, c)
                # replace Directory elements
                for d in xdata.findall('//Directory'):
                    parent = d.getparent()
                    oldd = d
                    d.tag = 'Path'
                    parent.replace(oldd, d)
                    # Create new-style Rules entry
                    newd = deepcopy(d)
                    newd.set('type', 'directory')
                    rulesroot.append(newd)
                # replace BoundDirectory elements
                for d in xdata.findall('//BoundDirectory'):
                    parent = d.getparent()
                    oldd = d
                    d.tag = 'BoundPath'
                    parent.replace(oldd, d)
                    # Create new-style entry
                    newd = deepcopy(d)
                    newd.set('type', 'directory')
                # replace Permissions elements
                for p in xdata.findall('//Permissions'):
                    parent = p.getparent()
                    oldp = p
                    p.tag = 'Path'
                    parent.replace(oldp, p)
                    # Create new-style Rules entry
                    newp = deepcopy(p)
                    newp.set('type', 'permissions')
                    rulesroot.append(newp)
                # replace BoundPermissions elements
                for p in xdata.findall('//BoundPermissions'):
                    parent = p.getparent()
                    oldp = p
                    p.tag = 'BoundPath'
                    parent.replace(oldp, p)
                    # Create new-style entry
                    newp = deepcopy(p)
                    newp.set('type', 'permissions')
                # replace SymLink elements
                for s in xdata.findall('//SymLink'):
                    parent = s.getparent()
                    olds = s
                    s.tag = 'Path'
                    parent.replace(olds, s)
                    # Create new-style Rules entry
                    news = deepcopy(s)
                    news.set('type', 'symlink')
                    rulesroot.append(news)
                # replace BoundSymLink elements
                for s in xdata.findall('//BoundSymLink'):
                    parent = s.getparent()
                    olds = s
                    s.tag = 'BoundPath'
                    parent.replace(olds, s)
                    # Create new-style entry
                    news = deepcopy(s)
                    news.set('type', 'symlink')
                # write out the new bundle
                try:
                    newbundle = open("%s/%s/new%s" % (repo, plug, filename), 'w')
                except IOError:
                    print("Failed to write %s" % filename)
                    continue
                newbundle.write(lxml.etree.tostring(xdata, pretty_print=True))
                newbundle.close()

    try:
        newrules = open(unifiedposixrules, 'w')
        rulesroot.set('priority', '1')
        newrules.write(lxml.etree.tostring(rulesroot, pretty_print=True))
        newrules.close()
    except IOError:
        print("Failed to write %s" % unifiedposixrules)