summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Lint/Bundles.py
blob: 417f76c2d412b292297e1592b8010c7de70e6089 (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
import lxml.etree
import Bcfg2.Server.Lint
                        
class Bundles(Bcfg2.Server.Lint.ServerPlugin):
    """ Perform various bundle checks """

    @Bcfg2.Server.Lint.returnErrors
    def Run(self):
        """ run plugin """
        if 'Bundler' in self.core.plugins:
            self.missing_bundles()
            for bundle in self.core.plugins['Bundler'].entries.values():
                if self.HandlesFile(bundle.name):
                    if (Bcfg2.Server.Plugins.Bundler.have_genshi and
                        type(bundle) is
                        Bcfg2.Server.Plugins.SGenshi.SGenshiTemplateFile):
                        self.sgenshi_groups(bundle)
                    else:
                        self.bundle_names(bundle)

    def missing_bundles(self):
        """ find bundles listed in Metadata but not implemented in Bundler """
        if self.files is None:
            # when given a list of files on stdin, this check is
            # useless, so skip it
            groupdata = self.metadata.groups_xml.xdata
            ref_bundles = set([b.get("name")
                               for b in groupdata.findall("//Bundle")])

            allbundles = self.core.plugins['Bundler'].entries.keys()
            for bundle in ref_bundles:
                xmlbundle = "%s.xml" % bundle
                genshibundle = "%s.genshi" % bundle
                if (xmlbundle not in allbundles and
                    genshibundle not in allbundles):
                    self.LintError("Bundle %s referenced, but does not exist" %
                                   bundle)

    def bundle_names(self, bundle):
        """ verify bundle name attribute matches filename """
        try:
            xdata = lxml.etree.XML(bundle.data)
        except AttributeError:
            # genshi template
            xdata = lxml.etree.parse(bundle.template.filepath).getroot()
            
        fname = bundle.name.split('Bundler/')[1].split('.')[0]
        bname = xdata.get('name')
        if fname != bname:
            self.LintWarning("Inconsistent bundle name: filename is %s, bundle name is %s" %
                             (fname, bname))

    def sgenshi_groups(self, bundle):
        """ ensure that Genshi Bundles do not include <Group> tags,
        which are not supported  """
        xdata = lxml.etree.parse(bundle.name)
        groups = [self.RenderXML(g)
                  for g in xdata.getroottree().findall("//Group")]
        if groups:
            self.LintWarning("<Group> tag is not allowed in SGenshi Bundle:\n%s" %
                             "\n".join(groups))