import os import time import click from sdi_checker.app_cli import display_check from sdi_checker.libs.audits import Audits from sdi_checker.libs.sdi_consistence_check.check import Check @click.command(name='run') @click.argument('id', nargs=-1) # help = id of audit to check @click.option('--type', '-t', show_default=True, default='wms', type=click.Choice(['wms', 'wfs', 'csw'], case_sensitive=False), help="type of audit to check (e.g. 'wms', 'wfs', 'csw'), default to 'wms'") @click.option('--url', '-u', type=str, help="server to target (full URL, e.g. https://sdi.georchestra.org/geoserver/wms)") @click.option('--inspire', '-i', show_default=True, default='flexible', type=click.Choice(['flexible', 'strict'], case_sensitive=False), help='indicates if the checks should be strict or flexible, default to flexible') @click.option('--inspire', '-i', show_default=True, default='flexible', type=click.Choice(['flexible', 'strict'], case_sensitive=False), help='indicates if the checks should be strict or flexible, default to flexible') @click.option('--geoserver-to-check', '-g', default=None, type=str, multiple=True, help="space-separated list of geoserver hostname to check in CSW mode with inspire strict option activated. Ex: sdi.georchestra.org") @click.option('--check-layers', '-l', is_flag=True, type=bool, help="check WMS/WFS layer validity") @click.option('--ssl-verification', '-ssl', type=click.Choice(['', 'true', 'on', 'false', 'off', '1', '0'], case_sensitive=False), default='', help="enable/disable certificate verification") @click.option('--only-err', '-e', is_flag=True, type=bool, help="only display errors, no summary informations will be displayed") @click.option('--xunit', '-x', is_flag=True, type=bool, help="generate a XML xunit result report") @click.option('--xunit-output', '-xo', default="xunit.xml", show_default=True, type=str, help="name of the xunit report file, defaults to ./xunit.xml") @click.option('--log-file', '-lf', default='', type=str, help="if a file path is specified, log output to this file, not stdout") @click.option('--log-level', '-ll', default='INFO', type=click.Choice(['INFO', 'DEBUG', 'WARNING', 'CRITICAL', 'ERROR'], case_sensitive=False), help="if a file path is specified, log output to this file, not stdout") @click.option('--log-display', '-ld', default=None, type=str, help="if a file path is specified, log output to this file, not stdout") @click.option('--log-clear', '-lc', default=None, type=str, help="if a file path is specified, log output to this file, not stdout") @click.option('--timeout', '-t', default=30, type=int, help="specify a timeout for request to external audit.") @click.option('--report-format', '-rf', type=click.Choice(['', 'json', 'txt', 'csv', 'db'], case_sensitive=False), default='', help="report output format") @click.option('--report-file', '-ro', type=str, default='', help="report output file") @click.option('--report-name', '-rn', type=str, default='', help="report name") @click.pass_obj def run(app, id, type, url, inspire, geoserver_to_check, check_layers, ssl_verification, only_err, xunit, xunit_output, log_file, log_level, log_display, log_clear, timeout, report_format, report_file, report_name): """ > master add [TEXT] [--template TPL] [--file FILE] [--edit] [--repository REPOSITORY] [--login LOGIN] [--password PASSWORD] """ # Utiliser fonction commune à WFS et WMS on_check(app, id, type, url, inspire, geoserver_to_check, check_layers, ssl_verification, only_err, xunit, xunit_output, log_file, log_level, log_display, log_clear, timeout, report_format, report_file, report_name) def on_check(app, id, type, url, inspire, geoserver_to_check, check_layers, ssl_verification, only_err, xunit, xunit_output, log_file, log_level, log_display, log_clear, timeout, report_format, report_file, report_name): """ > master add [TEXT] [--template TPL] [--file FILE] [--edit] (=> open edit window) """ logs_config = app.set_logs_config(level=log_level, file=log_file, clear=log_clear, display=log_display) report_output_file, report_output_format = app.get_report_output_file(name=report_name, file=report_file, extension=report_format) ssl_verify = app.get_on_off_value(value=ssl_verification, default=app.ssl_verify) if (len(id) == 0 or id[0] is None) and (not url or url is None): app.echo('ID parameter or URL are mandatory') exit() if len(id) and id[0] is not None: id = int(id[0]) # TODO: Manage if ID parameter is str => use "name" of audit # TODO: Add file parameter as option # audits_file = file or app.audits_file audits = Audits(file=app.audits_file) audit = audits.get(id=id).result url = audit['url'] type = audit['type'] # TODO: à simplifier ssl_verify = audit['params']['ssl_verify'] if 'params' in audit and 'ssl_verify' in audit['params'] else app.ssl_verify check_layers = audit['params']['check_layers'] if 'params' in audit and 'check_layers' in audit['params'] else False only_err = audit['params']['only_err'] if 'params' in audit and 'only_err' in audit['params'] else False xunit = audit['params']['xunit'] if 'params' in audit and 'xunit' in audit['params'] else False xunit_output = audit['params']['xunit_output'] if 'params' in audit and 'xunit_output' in audit['params'] else '' timeout = audit['params']['timeout'] if 'params' in audit and 'timeout' in audit['params'] else app.timeout check = Check() check.run(mode=type, server=url, ssl_verification=ssl_verify, check_layers=check_layers, only_err=only_err, xunit=xunit, xunit_output=xunit_output, timeout=timeout, logs=app.logs, report=app.report) # print(app.report.errors) report = app.report.get_report(format=report_output_format) if report_output_file: with open(report_output_file, 'w') as file: file.write(report) else: print(report)