summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Packages/PackagesSources.py
blob: 4fbccab30404f8ef0d03d41232415a7656d429af (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
import os
import sys
import lxml.etree
import logging
import Bcfg2.Server.Plugin
from Bcfg2.Server.Plugins.Packages.Source import SourceInitError

logger = logging.getLogger("Packages")

class PackagesSources(Bcfg2.Server.Plugin.SingleXMLFileBacked,
                      Bcfg2.Server.Plugin.StructFile):
    __identifier__ = None
    
    def __init__(self, filename, cachepath, fam, packages, config):
        try:
            Bcfg2.Server.Plugin.SingleXMLFileBacked.__init__(self,
                                                             filename,
                                                             fam)
        except OSError:
            err = sys.exc_info()[1]
            msg = "Packages: Failed to read configuration file: %s" % err
            if not os.path.exists(self.name):
                msg += " Have you created it?"
            logger.error(msg)
            raise Bcfg2.Server.Plugin.PluginInitError(msg)
        Bcfg2.Server.Plugin.StructFile.__init__(self, filename)
        self.cachepath = cachepath
        self.config = config
        if not os.path.exists(self.cachepath):
            # create cache directory if needed
            os.makedirs(self.cachepath)
        self.pkg_obj = packages
        self.parsed = set()
        self.loaded = False

    def HandleEvent(self, event=None):
        Bcfg2.Server.Plugin.SingleXMLFileBacked.HandleEvent(self, event=event)
        if event.filename != self.name:
            self.parsed.add(os.path.basename(event.filename))

        if sorted(list(self.parsed)) == sorted(self.extras):
            logger.info("Reloading Packages plugin")
            self.pkg_obj.Reload()
            self.loaded = True

    def Index(self):
        Bcfg2.Server.Plugin.SingleXMLFileBacked.Index(self)
        self.entries = []
        for xsource in self.xdata.findall('.//Source'):
            source = self.source_from_xml(xsource)
            if source is not None:
                self.entries.append(source)

    def source_from_xml(self, xsource):
        """ create a *Source object from its XML representation in
        sources.xml """
        stype = xsource.get("type")
        if stype is None:
            logger.error("Packages: No type specified for source, skipping")
            return None

        try:
            module = getattr(__import__("Bcfg2.Server.Plugins.Packages.%s" %
                                        stype.title()).Server.Plugins.Packages,
                             stype.title())
            cls = getattr(module, "%sSource" % stype.title())
        except (ImportError, AttributeError):
            logger.error("Packages: Unknown source type %s" % stype)
            return None

        try:
            source = cls(self.cachepath, xsource, self.config)
        except SourceInitError:
            err = sys.exc_info()[1]
            logger.error("Packages: %s" % err)
            source = None

        return source

    def __getitem__(self, key):
        return self.entries[key]

    def __repr__(self):
        return "PackagesSources: %s" % repr(self.entries)

    def __str__(self):
        return "PackagesSources: %s" % str(self.entries)