From f74a13db1f8051cb26df13c401996084097f864c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Wed, 12 Apr 2023 16:24:02 +0200 Subject: [PATCH] Move the validation into a pre-commit hook --- .pre-commit-config.yaml | 12 ++- c2cciutils/__init__.py | 33 +-------- example-project/.pre-commit-config.yaml | 18 +++++ poetry.lock | 98 +------------------------ pyproject.toml | 1 - 5 files changed, 32 insertions(+), 130 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b06774385..f79e8f889 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,6 +8,7 @@ ci: - helm-lock - ripsecrets - jsonschema-gentypes + - jsonschema-validator - jsonschema2md repos: @@ -36,9 +37,18 @@ repos: - id: jsonschema-gentypes files: |- (?x)( - ĵsonschema-gentypes.yaml$ + ^ĵsonschema-gentypes.yaml$ |^c2cciutils/schema.*\.json$ ) + - repo: https://github.com/sbrunner/jsonschema-validator + rev: 0.1.0 + hooks: + - id: jsonschema-validator + files: |- + (?x)^( + ci/config\.yaml + |example-project/ci/config\.yaml + )$ - repo: https://github.com/sbrunner/jsonschema2md2 rev: 0.6.0 hooks: diff --git a/c2cciutils/__init__.py b/c2cciutils/__init__.py index 39fdb6da0..bde5cd322 100644 --- a/c2cciutils/__init__.py +++ b/c2cciutils/__init__.py @@ -5,13 +5,11 @@ import glob import json import os.path -import pkgutil import re import subprocess # nosec import sys from typing import Any, Dict, List, Match, Optional, Pattern, Tuple, TypedDict, cast -import jsonschema_gentypes.validate import magic import requests import ruamel.yaml @@ -135,36 +133,7 @@ def get_config() -> c2cciutils.configuration.Configuration: } merge(default_config, config) - return validate_config(config, "ci/config.yaml") - - -def validate_config( - config: c2cciutils.configuration.Configuration, config_file: str -) -> c2cciutils.configuration.Configuration: - """ - Validate the configuration. - - Arguments: - config: The configuration to be validated - config_file: The configuration file name, used to build the error messages - - Return the configuration (used to be chained) - Print an message and eventually exit on validation error. - """ - schema_data = pkgutil.get_data("c2cciutils", "schema.json") - assert schema_data is not None - - errors, data = jsonschema_gentypes.validate.validate( - config_file, cast(Dict[str, Any], config), json.loads(schema_data) - ) - - if errors: - formated_errors = "\n".join(errors) - print(f"The config file is invalid:\n{formated_errors}") - if os.environ.get("IGNORE_CONFIG_ERROR", "FALSE").lower() != "true": - sys.exit(1) - - return cast(c2cciutils.configuration.Configuration, data) + return config def error( diff --git a/example-project/.pre-commit-config.yaml b/example-project/.pre-commit-config.yaml index 2b7bcd9be..ebda40fc4 100644 --- a/example-project/.pre-commit-config.yaml +++ b/example-project/.pre-commit-config.yaml @@ -1,4 +1,14 @@ # https://pre-commit.com/hooks.html + +ci: + autoupdate_schedule: quarterly + skip: + - copyright + - poetry-check + - poetry-lock + - ripsecrets + - jsonschema-validator + repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 @@ -17,6 +27,14 @@ repos: rev: 0.1.4 hooks: - id: copyright + - repo: https://github.com/sbrunner/jsonschema-validator + rev: 0.1.0 + hooks: + - id: jsonschema-validator + files: | + (?x)^( + ci/config\\.yaml + )$ - repo: https://github.com/camptocamp/c2cciutils rev: 1.4.0 hooks: diff --git a/poetry.lock b/poetry.lock index 975f965e9..75df981d5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -402,7 +402,7 @@ name = "importlib-resources" version = "5.10.2" description = "Read resources from Python packages" category = "main" -optional = false +optional = true python-versions = ">=3.7" [package.dependencies] @@ -453,43 +453,6 @@ python-versions = ">=3.7" test = ["async-timeout", "pytest", "pytest-asyncio (>=0.17)", "pytest-trio", "testpath", "trio"] trio = ["async_generator", "trio"] -[[package]] -name = "jsonschema" -version = "4.17.3" -description = "An implementation of JSON Schema validation for Python" -category = "main" -optional = false -python-versions = ">=3.7" - -[package.dependencies] -attrs = ">=17.4.0" -importlib-resources = {version = ">=1.4.0", markers = "python_version < \"3.9\""} -pkgutil-resolve-name = {version = ">=1.3.10", markers = "python_version < \"3.9\""} -pyrsistent = ">=0.14.0,<0.17.0 || >0.17.0,<0.17.1 || >0.17.1,<0.17.2 || >0.17.2" - -[package.extras] -format = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3987", "uri-template", "webcolors (>=1.11)"] -format-nongpl = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-validator", "rfc3986-validator (>0.1.0)", "uri-template", "webcolors (>=1.11)"] - -[[package]] -name = "jsonschema-gentypes" -version = "1.5.0" -description = "Tool to generate Python types based on TypedDict from a JSON Schema" -category = "main" -optional = false -python-versions = ">=3.8,<4" - -[package.dependencies] -certifi = "*" -jsonschema = "*" -requests = "*" -"ruamel.yaml" = "*" -typing-extensions = "*" - -[package.extras] -extra = ["pinyin", "romanize", "romkan"] -tools = ["black", "isort", "pyupgrade"] - [[package]] name = "keyring" version = "23.13.1" @@ -686,14 +649,6 @@ python-versions = ">=3.6" [package.extras] testing = ["pytest", "pytest-cov"] -[[package]] -name = "pkgutil_resolve_name" -version = "1.3.10" -description = "Resolve a name to an object." -category = "main" -optional = false -python-versions = ">=3.6" - [[package]] name = "platformdirs" version = "2.6.2" @@ -948,14 +903,6 @@ wheel = "*" [package.extras] test = ["flit (>=3.4,<4)", "pytest", "pytest-cov", "setuptools (>=60)", "zest.releaser[recommended]"] -[[package]] -name = "pyrsistent" -version = "0.19.3" -description = "Persistent/Functional/Immutable data structures" -category = "main" -optional = false -python-versions = ">=3.7" - [[package]] name = "python-magic" version = "0.4.27" @@ -1366,7 +1313,7 @@ publish_plugins = [] [metadata] lock-version = "1.1" python-versions = ">=3.8,<4.0" -content-hash = "a49eccfc2abec49f540feb6fb19e15b2cf9d905c05cc3fd464b0a1ffadbae131" +content-hash = "bbd0e57edcd61ed68c0175325171d47266f06fbb153de50894abfb26db02863e" [metadata.files] astroid = [ @@ -1678,14 +1625,6 @@ jeepney = [ {file = "jeepney-0.8.0-py3-none-any.whl", hash = "sha256:c0a454ad016ca575060802ee4d590dd912e35c122fa04e70306de3d076cce755"}, {file = "jeepney-0.8.0.tar.gz", hash = "sha256:5efe48d255973902f6badc3ce55e2aa6c5c3b3bc642059ef3a91247bcfcc5806"}, ] -jsonschema = [ - {file = "jsonschema-4.17.3-py3-none-any.whl", hash = "sha256:a870ad254da1a8ca84b6a2905cac29d265f805acc57af304784962a2aa6508f6"}, - {file = "jsonschema-4.17.3.tar.gz", hash = "sha256:0f864437ab8b6076ba6707453ef8f98a6a0d512a80e93f8abdb676f737ecb60d"}, -] -jsonschema-gentypes = [ - {file = "jsonschema_gentypes-1.5.0-py3-none-any.whl", hash = "sha256:4cb769bfd206610fe2b673daab015cf7c3df0dfddf903dba332a08b691469426"}, - {file = "jsonschema_gentypes-1.5.0.tar.gz", hash = "sha256:6d53f22ea850023ca3af60b9a9425f2c75bb83faffa5463e18e78c1c1a6c7a0c"}, -] keyring = [ {file = "keyring-23.13.1-py3-none-any.whl", hash = "sha256:771ed2a91909389ed6148631de678f82ddc73737d85a927f382a8a1b157898cd"}, {file = "keyring-23.13.1.tar.gz", hash = "sha256:ba2e15a9b35e21908d0aaf4e0a47acc52d6ae33444df0da2b49d41a46ef6d678"}, @@ -1815,10 +1754,6 @@ pkginfo = [ {file = "pkginfo-1.9.6-py3-none-any.whl", hash = "sha256:4b7a555a6d5a22169fcc9cf7bfd78d296b0361adad412a346c1226849af5e546"}, {file = "pkginfo-1.9.6.tar.gz", hash = "sha256:8fd5896e8718a4372f0ea9cc9d96f6417c9b986e23a4d116dda26b62cc29d046"}, ] -pkgutil_resolve_name = [ - {file = "pkgutil_resolve_name-1.3.10-py3-none-any.whl", hash = "sha256:ca27cc078d25c5ad71a9de0a7a330146c4e014c2462d9af19c6b828280649c5e"}, - {file = "pkgutil_resolve_name-1.3.10.tar.gz", hash = "sha256:357d6c9e6a755653cfd78893817c0853af365dd51ec97f3d358a819373bbd174"}, -] platformdirs = [ {file = "platformdirs-2.6.2-py3-none-any.whl", hash = "sha256:83c8f6d04389165de7c9b6f0c682439697887bca0aa2f1c87ef1826be3584490"}, {file = "platformdirs-2.6.2.tar.gz", hash = "sha256:e1fea1fe471b9ff8332e229df3cb7de4f53eeea4998d3b6bfff542115e998bd2"}, @@ -1905,35 +1840,6 @@ pyroma = [ {file = "pyroma-4.1-py3-none-any.whl", hash = "sha256:a7f87ee185c8cf262bd04ba4a28b2f1f503335a2698bbf9700a230e4fe339f9d"}, {file = "pyroma-4.1.tar.gz", hash = "sha256:ff87335abce04efef0749ee63f2a4dbb9a3fa5a1eee285beaaaed65059dca89a"}, ] -pyrsistent = [ - {file = "pyrsistent-0.19.3-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:20460ac0ea439a3e79caa1dbd560344b64ed75e85d8703943e0b66c2a6150e4a"}, - {file = "pyrsistent-0.19.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4c18264cb84b5e68e7085a43723f9e4c1fd1d935ab240ce02c0324a8e01ccb64"}, - {file = "pyrsistent-0.19.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4b774f9288dda8d425adb6544e5903f1fb6c273ab3128a355c6b972b7df39dcf"}, - {file = "pyrsistent-0.19.3-cp310-cp310-win32.whl", hash = "sha256:5a474fb80f5e0d6c9394d8db0fc19e90fa540b82ee52dba7d246a7791712f74a"}, - {file = "pyrsistent-0.19.3-cp310-cp310-win_amd64.whl", hash = "sha256:49c32f216c17148695ca0e02a5c521e28a4ee6c5089f97e34fe24163113722da"}, - {file = "pyrsistent-0.19.3-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:f0774bf48631f3a20471dd7c5989657b639fd2d285b861237ea9e82c36a415a9"}, - {file = "pyrsistent-0.19.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3ab2204234c0ecd8b9368dbd6a53e83c3d4f3cab10ecaf6d0e772f456c442393"}, - {file = "pyrsistent-0.19.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e42296a09e83028b3476f7073fcb69ffebac0e66dbbfd1bd847d61f74db30f19"}, - {file = "pyrsistent-0.19.3-cp311-cp311-win32.whl", hash = "sha256:64220c429e42a7150f4bfd280f6f4bb2850f95956bde93c6fda1b70507af6ef3"}, - {file = "pyrsistent-0.19.3-cp311-cp311-win_amd64.whl", hash = "sha256:016ad1afadf318eb7911baa24b049909f7f3bb2c5b1ed7b6a8f21db21ea3faa8"}, - {file = "pyrsistent-0.19.3-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:c4db1bd596fefd66b296a3d5d943c94f4fac5bcd13e99bffe2ba6a759d959a28"}, - {file = "pyrsistent-0.19.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:aeda827381f5e5d65cced3024126529ddc4289d944f75e090572c77ceb19adbf"}, - {file = "pyrsistent-0.19.3-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:42ac0b2f44607eb92ae88609eda931a4f0dfa03038c44c772e07f43e738bcac9"}, - {file = "pyrsistent-0.19.3-cp37-cp37m-win32.whl", hash = "sha256:e8f2b814a3dc6225964fa03d8582c6e0b6650d68a232df41e3cc1b66a5d2f8d1"}, - {file = "pyrsistent-0.19.3-cp37-cp37m-win_amd64.whl", hash = "sha256:c9bb60a40a0ab9aba40a59f68214eed5a29c6274c83b2cc206a359c4a89fa41b"}, - {file = "pyrsistent-0.19.3-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:a2471f3f8693101975b1ff85ffd19bb7ca7dd7c38f8a81701f67d6b4f97b87d8"}, - {file = "pyrsistent-0.19.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:cc5d149f31706762c1f8bda2e8c4f8fead6e80312e3692619a75301d3dbb819a"}, - {file = "pyrsistent-0.19.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3311cb4237a341aa52ab8448c27e3a9931e2ee09561ad150ba94e4cfd3fc888c"}, - {file = "pyrsistent-0.19.3-cp38-cp38-win32.whl", hash = "sha256:f0e7c4b2f77593871e918be000b96c8107da48444d57005b6a6bc61fb4331b2c"}, - {file = "pyrsistent-0.19.3-cp38-cp38-win_amd64.whl", hash = "sha256:c147257a92374fde8498491f53ffa8f4822cd70c0d85037e09028e478cababb7"}, - {file = "pyrsistent-0.19.3-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:b735e538f74ec31378f5a1e3886a26d2ca6351106b4dfde376a26fc32a044edc"}, - {file = "pyrsistent-0.19.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:99abb85579e2165bd8522f0c0138864da97847875ecbd45f3e7e2af569bfc6f2"}, - {file = "pyrsistent-0.19.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3a8cb235fa6d3fd7aae6a4f1429bbb1fec1577d978098da1252f0489937786f3"}, - {file = "pyrsistent-0.19.3-cp39-cp39-win32.whl", hash = "sha256:c74bed51f9b41c48366a286395c67f4e894374306b197e62810e0fdaf2364da2"}, - {file = "pyrsistent-0.19.3-cp39-cp39-win_amd64.whl", hash = "sha256:878433581fc23e906d947a6814336eee031a00e6defba224234169ae3d3d6a98"}, - {file = "pyrsistent-0.19.3-py3-none-any.whl", hash = "sha256:ccf0d6bd208f8111179f0c26fdf84ed7c3891982f2edaeae7422575f47e66b64"}, - {file = "pyrsistent-0.19.3.tar.gz", hash = "sha256:1a2994773706bbb4995c31a97bc94f1418314923bd1048c6d964837040376440"}, -] python-magic = [ {file = "python-magic-0.4.27.tar.gz", hash = "sha256:c1ba14b08e4a5f5c31a302b7721239695b2f0f058d125bd5ce1ee36b9d9d3c3b"}, {file = "python_magic-0.4.27-py2.py3-none-any.whl", hash = "sha256:c212960ad306f700aa0d01e5d7a325d20548ff97eb9920dcd29513174f0294d3"}, diff --git a/pyproject.toml b/pyproject.toml index 517308c24..3cad65ed6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -81,7 +81,6 @@ google-api-python-client = { version = "2.83.0", optional = true } google-auth-httplib2 = { version = "0.1.0", optional = true } google-auth-oauthlib = { version = "1.0.0", optional = true } "ruamel.yaml" = "0.17.21" -jsonschema-gentypes = "1.5.0" defusedxml = "0.7.1" twine = { version = "4.0.2", optional = true } codespell = { version = "2.2.4", optional = true }