diff options
author | Rob Austein <sra@hactrn.net> | 2007-07-24 21:13:21 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2007-07-24 21:13:21 +0000 |
commit | 36f4a12d02c6558eeab5d373aaad4e947d410926 (patch) | |
tree | eb4ccb9ea744f2faa386daa92d28654e7ddc44b3 /scripts/rpki/relaxng.py | |
parent | 1f7c7823efe3135e930a0105d89b01bf58c87842 (diff) |
Internal RelaxNG validation without calling an external process.
svn path=/scripts/rpki/relaxng.py; revision=792
Diffstat (limited to 'scripts/rpki/relaxng.py')
-rw-r--r-- | scripts/rpki/relaxng.py | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/scripts/rpki/relaxng.py b/scripts/rpki/relaxng.py index 072d7798..f7aeb33b 100644 --- a/scripts/rpki/relaxng.py +++ b/scripts/rpki/relaxng.py @@ -1,6 +1,6 @@ # $Id$ -import os +import os, libxml2, sys def relaxng(xml, rng): """ @@ -14,10 +14,44 @@ def relaxng(xml, rng): # # If error messages from xmllint are incomprehensible, try jing too. - i, o = os.popen4(("xmllint", "--noout", "--relaxng", rng, "-")) - i.write(xml) - i.close() - v = o.read() - o.close() - if v != "- validates\n": - raise RuntimeError, "RelaxNG validation failure:\n" + v + if False: + + i, o = os.popen4(("xmllint", "--noout", "--relaxng", rng, "-")) + i.write(xml) + i.close() + v = o.read() + o.close() + if v != "- validates\n": + raise RuntimeError, "RelaxNG validation failure:\n" + v + + else: + + # First cut at internal RelaxNG validation. Not entirely + # satisfactory, see /usr/ports/devel/py-lxml/pkg-descr for a + # possible alternate approach. Error reporting in libxml2 module + # apparently uses a callback which I'm not yet setting. + + fh = open(rng, "r") + schema = fh.read() + fh.close() + rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema)) + rngs = rngp.relaxNGParse() + ctxt = rngs.relaxNGNewValidCtxt() + + doc = libxml2.parseDoc(xml) + ret = doc.relaxNGValidateDoc(ctxt) + if ret != 0: + raise RuntimeError, "RelaxNG validation error %d" % ret + + doc.freeDoc() + del rngp + del rngs + del ctxt + libxml2.relaxNGCleanupTypes() + + # Memory debug specific + libxml2.cleanupParser() + if libxml2.debugMemory(1) != 0: + print "Memory leak %d bytes" % (libxml2.debugMemory(1)) + libxml2.dumpMemory() + raise RuntimeError, "RelaxNG memory leak" |