From 8e288a9060545989533505699c19d9997e6a8aff Mon Sep 17 00:00:00 2001 From: aqua Date: Thu, 1 Sep 2022 19:32:26 +0300 Subject: Add scripts/qrc_lint.py --- scripts/qrc_lint.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 scripts/qrc_lint.py (limited to 'scripts/qrc_lint.py') diff --git a/scripts/qrc_lint.py b/scripts/qrc_lint.py new file mode 100755 index 00000000..9520f22a --- /dev/null +++ b/scripts/qrc_lint.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +# ============================================================ +# The rekonq project +# ============================================================ +# SPDX-License-Identifier: GPL-3.0-only +# Copyright (C) 2022 aqua +# ============================================================ +""" Lint qrc files """ + +import argparse +import sys +from os.path import (dirname, exists, join) +from lxml import etree + + +def lint(dir_name: str, tree: etree._ElementTree) -> bool: + """ Lint an etree and return if errors were found """ + has_check_errors = False + root = tree.getroot() + + # check doctype + if tree.docinfo.doctype != '': + print(f'unknown doctype { tree.docinfo.doctype }') + has_check_errors = True + + qresources = root.findall('qresource') + # check if qresource's have prefix + for qres in qresources: + if qres.get('prefix') is None: + print('qresource without prefix') + has_check_errors = True + + # check if qresource's are sorted by prefix + sortedqres = sorted(qresources, key=lambda x: x.get('prefix')) + if qresources != sortedqres: + print('qresources are not sorted') + has_check_errors = True + + for qres in sortedqres: + files = qres.findall('file') + + # check if files are sorted + if files != sorted(files, key=lambda x: x.get('alias')): + print(f'qresources prefix={ qres.get("prefix") }:\tfiles are not sorted') + has_check_errors = True + + # check for missing files and missing alias + for file in files: + if file.get('alias') is None: + print(f'file path={ file.text }:\tmissing file alias') + has_check_errors = True + path = join(dir_name, file.text) + if not exists(path): + print(f'file path={ path }:\tfile does not exist') + has_check_errors = True + + return has_check_errors + + +def regen(root: etree.Element, outfile: str): + """ Regenerate an etree """ + out_root = etree.Element('RCC', {'version': '1.0'}) + for qresource in sorted(root.findall('qresource'), key=lambda x: x.get('prefix')): + out_qres = etree.SubElement(out_root, 'qresource', {'prefix': qresource.get('prefix')}) + + for file in sorted(qresource.findall('file'), key=lambda x: x.get('alias')): + out_file = etree.SubElement(out_qres, 'file', {'alias': file.get('alias')}) + out_file.text = file.text + + with etree.xmlfile(outfile, encoding='utf-8', close=True) as xml_file: + xml_file.write_declaration(standalone=True) + xml_file.write_doctype('') + xml_file.write(out_root, pretty_print=True) + + +def main(): + """ Main function """ + parser = argparse.ArgumentParser(description='Lint qrc file') + parser.add_argument('file', type=str, help='qrc file') + parser.add_argument('--regen', action='store_true', help='Regenerate qrc file') + args = parser.parse_args() + + if not exists(args.file): + sys.exit('input file "{args.file}" doesn\'t exist') + else: + print(args.file) + + tree = etree.parse(args.file) + + if args.regen: + regen(tree.getroot(), args.file) + elif lint(dirname(args.file), tree): + sys.exit('Errors during lint') + + +if __name__ == '__main__': + main() -- cgit v1.2.1