Bundle Yamale schema

TIL! Using the existence of obscure member sys._MEIPASS, I can detect if
we're running from a bundled PyInstaller binary, versus running from Python
directly.

Add schema.yaml to the datas of the PyInstaller spec. Then, if the
-/--schema flag is given, use it, and if it's not given, default to the
built-in one if we're running from a bundled binary, or fall-through to
./schema.yaml in other cases.

This avoids the need for config/schema.py as a carbon-copy of the schema,
slick!
This commit is contained in:
Pim van Pelt
2022-04-05 12:40:05 +00:00
parent fdb732142a
commit 289138da94
5 changed files with 25 additions and 95 deletions

View File

@ -20,6 +20,9 @@ from __future__ import (
)
import logging
import ipaddress
import os.path
import sys
try:
import yamale
except ImportError:
@ -30,10 +33,8 @@ from config.bondethernet import validate_bondethernets
from config.interface import validate_interfaces
from config.bridgedomain import validate_bridgedomains
from config.vxlan_tunnel import validate_vxlan_tunnels
from config.schema import yamale_schema
from yamale.validators import DefaultValidators, Validator
import ipaddress
class IPInterfaceWithPrefixLength(Validator):
""" Custom IPAddress config - takes IP/prefixlen as input:
@ -74,15 +75,25 @@ class Validator(object):
if not yaml:
return ret_rv, ret_msgs
validators = DefaultValidators.copy()
validators[IPInterfaceWithPrefixLength.tag] = IPInterfaceWithPrefixLength
if self.schema:
fn = self.schema
self.logger.debug("Validating against --schema %s" % fn)
elif hasattr(sys, "_MEIPASS"):
## See vppcfg.spec data_files that includes schema.yaml into the bundle
self.logger.debug("Validating against built-in schema")
fn = os.path.join(sys._MEIPASS, "schema.yaml")
else:
fn = "./schema.yaml"
self.logger.debug("Validating against fallthrough default schema %s" % fn)
if not os.path.isfile(fn):
self.logger.error("Cannot file schema file: %s" % fn)
return False, ret_msgs
try:
validators = DefaultValidators.copy()
validators[IPInterfaceWithPrefixLength.tag] = IPInterfaceWithPrefixLength
if self.schema:
self.logger.debug("Validating against schema %s" % self.schema)
schema = yamale.make_schema(self.schema, validators=validators)
else:
self.logger.debug("Validating against built-in schema")
schema = yamale.make_schema(content=yamale_schema, validators=validators)
schema = yamale.make_schema(fn, validators=validators)
data = yamale.make_data(content=str(yaml))
yamale.validate(schema, data)
self.logger.debug("Schema correctly validated by yamale")