diff --git a/openstack_releases/cmds/check_schema.py b/openstack_releases/cmds/check_schema.py new file mode 100644 index 0000000000..2f9e903a2d --- /dev/null +++ b/openstack_releases/cmds/check_schema.py @@ -0,0 +1,98 @@ +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Verify that all deliverable files match the schema. + +""" + +from __future__ import print_function + +import argparse +import glob +import logging +import os +import os.path +import pkgutil +import sys + +import jsonschema + +from openstack_releases import yamlutils + +_SCHEMA = yamlutils.loads( + pkgutil.get_data('openstack_releases', 'schema.yaml').decode('utf-8') +) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + '--debug', + default=False, + action='store_true', + help='throw exception on error', + ) + parser.add_argument( + 'input', + nargs='*', + help=('YAML files to validate, defaults to ' + 'files changed in the latest commit'), + ) + args = parser.parse_args() + + # Set up logging, including making some loggers quiet. + logging.basicConfig( + format='%(levelname)7s: %(message)s', + stream=sys.stdout, + level=logging.DEBUG, + ) + logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING) + log = logging.getLogger('') + + filenames = args.input or sorted(glob.glob('deliverables/*/*.yaml')) + + errors = [] + warnings = [] + + for filename in filenames: + log.info('Checking %s', filename) + if not os.path.isfile(filename): + log.info("File was deleted, skipping.") + continue + with open(filename, 'r', encoding='utf-8') as f: + deliverable_info = yamlutils.loads(f.read()) + + def mk_warning(msg): + log.warning(msg) + warnings.append('{}: {}'.format(filename, msg)) + + def mk_error(msg): + log.error(msg) + errors.append('{}: {}'.format(filename, msg)) + if args.debug: + raise RuntimeError(msg) + + validator = jsonschema.Draft4Validator(_SCHEMA) + for error in validator.iter_errors(deliverable_info): + mk_error(str(error)) + + print('\n\n%s warnings found' % len(warnings)) + for w in warnings: + print(w) + + print('\n\n%s errors found' % len(errors)) + for e in errors: + print(e) + + return 1 if errors else 0 diff --git a/openstack_releases/cmds/validate.py b/openstack_releases/cmds/validate.py index 20dcfd44f5..dcb8303990 100644 --- a/openstack_releases/cmds/validate.py +++ b/openstack_releases/cmds/validate.py @@ -24,13 +24,11 @@ import glob import logging import os import os.path -import pkgutil import re import shutil import sys import tempfile -import jsonschema import requests import six @@ -95,9 +93,6 @@ _NO_STABLE_BRANCH_CHECK = set([ ]) _PLEASE = ('It is too expensive to determine this value during ' 'the site build, please set it explicitly.') -_SCHEMA = yamlutils.loads( - pkgutil.get_data('openstack_releases', 'schema.yaml').decode('utf-8') -) def header(title): @@ -110,13 +105,6 @@ def is_a_hash(val): return re.search('^[a-f0-9]{40}$', val, re.I) is not None -def validate_schema(deliverable_info, mk_warning, mk_error): - header('Validate Schema') - validator = jsonschema.Draft4Validator(_SCHEMA) - for error in validator.iter_errors(deliverable_info): - mk_error(str(error)) - - def validate_series_open(deliverable_info, series_name, filename, mk_warning, mk_error): @@ -1308,7 +1296,6 @@ def main(): raise RuntimeError(msg) clone_deliverable(deliverable_info, workdir, mk_warning, mk_error) - validate_schema(deliverable_info, mk_warning, mk_error) validate_bugtracker(deliverable_info, mk_warning, mk_error) validate_team(deliverable_info, team_data, mk_warning, mk_error) validate_release_notes(deliverable_info, mk_warning, mk_error) diff --git a/setup.cfg b/setup.cfg index 1af97cbc2b..35dbdca009 100644 --- a/setup.cfg +++ b/setup.cfg @@ -40,6 +40,7 @@ console_scripts = edit-deliverable = openstack_releases.cmds.edit_deliverable:main send-mail = openstack_releases.cmds.mail:main release-notes = openstack_releases.cmds.release_notes:main + check-schema = openstack_releases.cmds.check_schema:main [extras] sphinxext = diff --git a/tox.ini b/tox.ini index 499f4cbcfa..26a9944a4e 100644 --- a/tox.ini +++ b/tox.ini @@ -24,6 +24,7 @@ deps = yamllint==1.4.1 commands = {toxinidir}/tools/tox-log-command.sh {toxinidir}/tools/run_yamllint.sh + {toxinidir}/tools/tox-log-command.sh check-schema {posargs} {toxinidir}/tools/tox-log-command.sh validate-request {posargs} [testenv:list-changes]