summaryrefslogtreecommitdiff
path: root/scripts/validate_xml.py
blob: 4f716d6074aa977265260faee208bc9c54029e22 (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
#!/usr/bin/env python3
# ============================================================
#     The rekonq project
# ============================================================
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
# ============================================================
""" Validate xml files """


import argparse
import sys
from lxml import etree


def validate(xml_path: str, validator) -> bool:
    ''' Load and validate file using lxml '''
    xml_doc = etree.parse(xml_path)
    return validator.validate(xml_doc)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Validate xml files')
    parser.add_argument('files', type=str, nargs='+', help='One or more files to validate')
    parser.add_argument('--dtd', type=str, help='DTD')
    args = parser.parse_args()

    validator = etree.DTD(args.dtd)
    files_ok = True
    for file in args.files:
        if not validate(file, validator):
            print(f'{file} failed validation')
            files_ok = False

    if not files_ok:
        sys.exit('validation failed')