summaryrefslogtreecommitdiffstats
path: root/src/sbin/ValidateBcfg2Repo
blob: 49b87c5c4f0c182059d6cda1e28e125d54bdddb1 (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
#!/usr/bin/env python

'''ValidateBcfg2Repo checks all xml files in Bcfg2 repos against their respective XML schemas'''
__revision__ = '0.7.3'

from glob import glob
from lxml.etree import parse, XMLSchema
from os import system
from sys import argv
from ConfigParser import ConfigParser, NoSectionError, NoOptionError

if __name__ == '__main__':
    cf = ConfigParser()
    schemadir = '/usr/share/bcfg2/schemas'
    cf.read(['/etc/bcfg2.conf'])
    try:
        repo = cf.get('server', 'repository')
    except (NoSectionError, NoOptionError):
        if len(argv) == 1:
            print "Repository location not specified in config file or on command line"
            print "Usage: validate_repo <repo directory>"
            raise SystemExit, 1
        repo = argv[1]

    # add more validation as more schemas get written
    filesets = {'metadata':("%s/etc/metadata.xml", "%s/metadata.xsd"),
                'bundle':("%s/Bundler/*.xml", "%s/bundle.xsd"),
                'pkglist':("%s/Pkgmgr/*.xml", "%s/pkglist.xsd"),
                'base':("%s/etc/base.xml", "%s/base.xsd"),
                'imageinfo':("%s/etc/imageinfo.xml", "%s/translation.xsd"),
                'imageinfo':("%s/etc/reports.xml", "%s/report-configuration.xsd"),
                'services':("%s/etc/services.xml", "%s/services.xsd")}

    for k, (spec, schema) in filesets.iteritems():
        schema = XMLSchema(parse(open(schema%(schemadir))))
        for filename in glob(spec%(repo)):
            try:
                datafile = parse(open(filename))
            except SyntaxError:
                print "%s ***FAILS*** to parse \t\t<----" % (filename)
                system("xmllint %s" % filename)
                continue
            except IOError:
                print "Failed to open file %s \t\t<---" % (filename)
                continue
            if schema.validate(datafile):
                if '-v' in argv:
                    print "%s checks out" % (filename)
            else:
                print "%s ***FAILS*** to verify \t\t<----" % (filename)
                system("xmllint --schema %s %s" % (schema % schemadir, filename))