summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-repo-validate
blob: 67ebf12f1f837834a01988077f1b3a2d92d493fd (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
#!/usr/bin/env python

'''bcfg2-repo-validate checks all xml files in Bcfg2 repos against their respective XML schemas'''
__revision__ = '$Revision$'

import glob, lxml.etree, os, sys, ConfigParser

if __name__ == '__main__':
    verbose = False
    if '-v' in sys.argv:
        verbose = True
        sys.argv.remove('-v')
    cf = ConfigParser.ConfigParser()
    cf.read(['/etc/bcfg2.conf'])
    try:
        prefix = cf.get('server', 'prefix')
    except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
        prefix = '/usr'
    if verbose:
        print "Using installation prefix %s" % (prefix)
    schemadir = "%s/share/bcfg2/schemas" % (prefix)
    if len(sys.argv) > 1:
        repo = sys.argv[1]
    else:
        try:
            repo = cf.get('server', 'repository')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            print "Repository location not specified in config file or on command line"
            print "Usage: bcfg2-repo-validate [-v] <repo directory>"
            raise SystemExit, 1

    # add more validation as more schemas get written
    filesets = {'metadata':("%s/Metadata/groups.xml", "%s/metadata.xsd"),
                'clients':("%s/Metadata/clients.xml", "%s/clients.xsd"),
                'bundle':("%s/Bundler/*.xml", "%s/bundle.xsd"),
                'pkglist':("%s/Pkgmgr/*.xml", "%s/pkglist.xsd"),
                'base':("%s/Base/*.xml", "%s/base.xsd"),
                'rules':("%s/Rules/*.xml", "%s/rules.xsd"),
                'imageinfo':("%s/etc/reports.xml", "%s/report-configuration.xsd"),
                'services':("%s/Svcmgr/*.xml", "%s/services.xsd"),
                'deps':("%s/Deps/*.xml", "%s/deps.xsd")}

    failures  = 0
    for k, (spec, schemaname) in filesets.iteritems():
        try:
            if verbose:
                print "Processing schema %s" % (schemaname % (schemadir))
            schema = lxml.etree.XMLSchema(lxml.etree.parse(open(schemaname%(schemadir))))
        except:
            print "Failed to process schema %s" % (schemaname%(schemadir))
            failures = 1
            continue
        for filename in glob.glob(spec%(repo)):
            try:
                datafile = lxml.etree.parse(open(filename))
            except SyntaxError:
                print "%s ***FAILS*** to parse \t\t<----" % (filename)
                os.system("xmllint %s" % filename)
                failures = 1
                continue
            except IOError:
                print "Failed to open file %s \t\t<---" % (filename)
                failures = 1
                continue
            if schema.validate(datafile):
                if verbose:
                    print "%s checks out" % (filename)
            else:
                print "%s ***FAILS*** to verify \t\t<----" % (filename)
                os.system("xmllint --schema %s %s" % (schemaname % schemadir, filename))
                failures = 1
    raise SystemExit, failures