summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Lint/InfoXML.py
blob: e34f387ffc2404b049465a9dcd6fe96812e0bb29 (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
""" ensure that all config files have an info.xml file"""

import os
import Bcfg2.Options
import Bcfg2.Server.Lint
from Bcfg2.Server.Plugins.Cfg.CfgInfoXML import CfgInfoXML
from Bcfg2.Server.Plugins.Cfg.CfgLegacyInfo import CfgLegacyInfo


class InfoXML(Bcfg2.Server.Lint.ServerPlugin):
    """ ensure that all config files have an info.xml file"""
    def Run(self):
        if 'Cfg' not in self.core.plugins:
            return

        for filename, entryset in self.core.plugins['Cfg'].entries.items():
            infoxml_fname = os.path.join(entryset.path, "info.xml")
            if self.HandlesFile(infoxml_fname):
                found = False
                for entry in entryset.entries.values():
                    if isinstance(entry, CfgInfoXML):
                        self.check_infoxml(infoxml_fname,
                                           entry.infoxml.pnode.data)
                        found = True
                if not found:
                    self.LintError("no-infoxml",
                                   "No info.xml found for %s" % filename)

            for entry in entryset.entries.values():
                if isinstance(entry, CfgLegacyInfo):
                    if not self.HandlesFile(entry.path):
                        continue
                    self.LintError("deprecated-info-file",
                                   "Deprecated %s file found at %s" %
                                   (os.path.basename(entry.name),
                                    entry.path))

    @classmethod
    def Errors(cls):
        return {"no-infoxml": "warning",
                "deprecated-info-file": "warning",
                "paranoid-false": "warning",
                "broken-xinclude-chain": "warning",
                "required-infoxml-attrs-missing": "error"}

    def check_infoxml(self, fname, xdata):
        """ verify that info.xml contains everything it should """
        for info in xdata.getroottree().findall("//Info"):
            required = []
            if "required_attrs" in self.config:
                required = self.config["required_attrs"].split(",")

            missing = [attr for attr in required if info.get(attr) is None]
            if missing:
                self.LintError("required-infoxml-attrs-missing",
                               "Required attribute(s) %s not found in %s:%s" %
                               (",".join(missing), fname,
                                self.RenderXML(info)))

            if ((Bcfg2.Options.MDATA_PARANOID.value and
                 info.get("paranoid") is not None and
                 info.get("paranoid").lower() == "false") or
                (not Bcfg2.Options.MDATA_PARANOID.value and
                 (info.get("paranoid") is None or
                  info.get("paranoid").lower() != "true"))):
                self.LintError("paranoid-false",
                               "Paranoid must be true in %s:%s" %
                               (fname, self.RenderXML(info)))