From 815b296a1dc2f212788732814b089ed5de4f80e9 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Sat, 12 Nov 2005 21:23:56 +0000 Subject: switch to using lxml schema validation (Logical change 1.358) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1496 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/ValidateBcfg2Repo | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/sbin/ValidateBcfg2Repo b/src/sbin/ValidateBcfg2Repo index e77f33500..4da868690 100644 --- a/src/sbin/ValidateBcfg2Repo +++ b/src/sbin/ValidateBcfg2Repo @@ -1,9 +1,12 @@ #!/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 sys import argv, exit -from validate import validate, ValidationException -from ConfigParser import ConfigParser +from lxml.etree import parse, XMLSchema +from sys import argv +from ConfigParser import ConfigParser, NoSectionError, NoOptionError if __name__ == '__main__': cf = ConfigParser() @@ -11,8 +14,9 @@ if __name__ == '__main__': cf.read(['/etc/bcfg2.conf']) try: repo = cf.get('server', 'repository') - except: + except (NoSectionError, NoOptionError): if len(argv) == 1: + print "Repository location not specified in config file or on command line" print "Usage: validate_repo " raise SystemExit, 1 repo = argv[1] @@ -27,11 +31,17 @@ if __name__ == '__main__': '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: - validate(open(filename).read(), schema%(schemadir)) - print "%s checks out"%(filename) - except ValidationException, v: - print "file %s fails to verify:\n%s"%(filename, v) + datafile = parse(open(filename)) + except SyntaxError: + print "%s ***FAILS*** to parse \t\t<----" % (filename) + continue except IOError: - print "failed to open file %s"%(filename) + print "Failed to open file %s \t\t<---" % (filename) + continue + if schema.validate(datafile): + print "%s checks out" % (filename) + else: + print "%s ***FAILS*** to verify \t\t<----" % (filename) -- cgit v1.2.3-1-g7c22