File indexing completed on 2024-04-28 03:52:27

0001 #!/usr/bin/python3
0002 # Version in sysadmin/ci-utilities should be single source of truth
0003 # SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0004 # SPDX-License-Identifier: BSD-2-Clause
0005 
0006 import os
0007 import subprocess
0008 import yaml
0009 import sys
0010 
0011 def get_changed_files():
0012     result = subprocess.run(['git', 'diff', '--name-only', 'HEAD'], capture_output=True, text=True)
0013     return [file for file in result.stdout.splitlines() if file.endswith('.json')]
0014 
0015 def get_all_files():
0016     files = []
0017     for root, dirs, filenames in os.walk('.'):
0018         for filename in filenames:
0019             if filename.endswith('.json'):
0020                 files.append(os.path.join(root, filename))
0021     return files
0022 
0023 def filter_excluded_json_files(files):
0024     config_file = '.kde-ci.yml'
0025     # Check if the file exists
0026     if os.path.exists(config_file):
0027         with open(config_file, 'r') as file:
0028             config = yaml.safe_load(file)
0029     else:
0030         print(f'{config_file} does not exist in current directory')
0031         config = {}
0032     # Extract excluded files, used for tests that intentionally have broken files
0033     excluded_files = ['compile_commands.json', 'ci-utilities']
0034     if 'Options' in config and 'json-validate-ignore' in config['Options']:
0035         excluded_files += config['Options']['json-validate-ignore']
0036 
0037     # Find JSON files
0038     filtered_files = []
0039     for file_path in files:
0040         if not any(excluded_file in file_path for excluded_file in excluded_files):
0041             filtered_files.append(file_path)
0042     return filtered_files
0043 
0044 is_kde_ci = "KDE_CI" in os.environ
0045 if is_kde_ci:
0046     files = get_all_files()
0047 else:
0048     files = get_changed_files()
0049 files = filter_excluded_json_files(files)
0050 if files:
0051     files_option = ' '.join(files)
0052     if len(sys.argv) > 1:
0053         schemafile = sys.argv[1]
0054     else:
0055         schemafile = os.path.join(os.path.dirname(__file__), 'resources', 'kpluginmetadata.schema.json')
0056     if is_kde_ci: # Only report files on CI, for pre-commit hook, we'd like to avoid verbose output in terminal sessions
0057         print(f"Validating {files_option} with {schemafile}")
0058     result = subprocess.run(['check-jsonschema', *files, '--schemafile', schemafile])
0059     # Fail the pipeline if command failed
0060     if result.returncode != 0:
0061         exit(1)
0062