File: validate_xml.py

package info (click to toggle)
open-build-service 2.9.4-10
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 30,544 kB
  • sloc: ruby: 73,389; perl: 51,999; xml: 9,782; sh: 5,540; javascript: 2,743; sql: 1,232; python: 435; makefile: 229; cpp: 46
file content (48 lines) | stat: -rwxr-xr-x 2,476 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python
#
# Copyright (c) 2010, Sascha Peilicke <saschpe@suse.de>, Novell Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import os, sys
from subprocess import call


def validate_schema(arg, dirname, filenames):
    """Validates XML files in a directory against their provided schema definition.

    Calls 'xmllint' to do the validation when a supported schema definition file is
    found. The supported schema definitions are RelaxNG, Schematron and XML Schema.
    """
    for filename in filenames:
        if filename.endswith('.xml'):                   # check for XML files
            relname = os.path.join(dirname, filename)   # relative filename from working directory
            basename = relname.rsplit('.', 1)[0]        # split of the file ending (aka '.xml')
            if os.path.exists(basename + '.xsd'):       # has a XML Schema file?
                call("xmllint --noout --schema {0} {1}".format(basename + '.xsd', relname).split(' '))
            elif os.path.exists(basename + '.rng'):     # has a RelaxNG schema file?
                call("xmllint --noout --relaxng {0} {1}".format(basename + '.rng', relname).split(' '))
            elif os.path.exists(basename + '.sch'):     # has a Schematron schema file? 
                call("xmllint --noout --schematron {0} {1}".format(basename + '.sch', relname).split(' '))
            else:                                       # has none unfortunately
                print("no schema to validate {0}".format(relname))


if __name__ == "__main__":                              # we're called directly
    if len(sys.argv) != 2 or not os.path.isdir(sys.argv[1]):
        print "please provide a directory with XML files to validate!"
        sys.exit(1)

    os.path.walk(sys.argv[1], validate_schema, None)    # walk all files in the provided directory recursively