Skip to content

Commit

Permalink
Refactor implementation of validating apps file
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed May 6, 2024
1 parent 1801cae commit f9eaff2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 87 deletions.
78 changes: 25 additions & 53 deletions apps_validation/validation/app_version.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,47 @@
import os
import yaml

from typing import Optional

from semantic_version import Version
from jsonschema import validate as json_schema_validate, ValidationError as JsonValidationError

from apps_validation.exceptions import ValidationErrors

from .json_schema_utils import APP_METADATA_JSON_SCHEMA
from .scale_version import validate_min_max_version_values


def validate_app_version_file(
verrors: ValidationErrors, app_version_path: str, schema: str, item_name: str, version_name: Optional[str] = None,
train_name: Optional[str] = None,
) -> ValidationErrors:
if os.path.exists(app_version_path):
with open(app_version_path, 'r') as f:
try:
app_config = yaml.safe_load(f.read())
except yaml.YAMLError:
verrors.add(schema, 'Must be a valid yaml file')
else:
if not isinstance(app_config, dict):
verrors.add(schema, 'Must be a dictionary')
else:
if app_config.get('name') != item_name:
verrors.add(f'{schema}.item_name', 'Item name not correctly set in "app.yaml".')

if not isinstance(app_config.get('annotations', {}), dict):
verrors.add(f'{schema}.annotations', 'Annotations must be a dictionary')
elif app_config.get('annotations'):
validate_min_max_version_values(app_config['annotations'], verrors, schema)
if not os.path.exists(app_version_path):
verrors.add(schema, 'Missing app version file')
return verrors

if not isinstance(app_config.get('sources', []), list):
verrors.add(f'{schema}.sources', 'Sources must be a list')
else:
for index, source in enumerate(app_config.get('sources', [])):
if not isinstance(source, str):
verrors.add(f'{schema}.sources.{index}', 'Source must be a string')
with open(app_version_path, 'r') as f:
try:
app_config = yaml.safe_load(f.read())
except yaml.YAMLError:
verrors.add(schema, 'Must be a valid yaml file')
return verrors

if not isinstance(app_config.get('maintainers', []), list):
verrors.add(f'{schema}.maintainers', 'Maintainers must be a list')
else:
for index, maintainer in enumerate(app_config.get('maintainers', [])):
if not isinstance(maintainer, dict):
verrors.add(f'{schema}.maintainers.{index}', 'Maintainer must be a dictionary')
elif not all(k in maintainer and isinstance(maintainer[k], str) for k in ('name', 'email')):
verrors.add(
f'{schema}.maintainers.{index}',
'Maintainer must have name and email attributes defined and be strings.'
)
try:
json_schema_validate(app_config, APP_METADATA_JSON_SCHEMA)
except JsonValidationError as e:
verrors.add(schema, f'Failed to validate app version file: {e.message}')
return verrors

app_version = app_config.get('version')
if app_version is None:
verrors.add(f'{schema}.version', 'Version must be configured in "app.yaml"')
else:
try:
Version(app_version)
except ValueError:
verrors.add(f'{schema}.version', f'{app_version!r} is not a valid version name')
if app_config.get('name') != item_name:
verrors.add(f'{schema}.item_name', 'Item name not correctly set in "app.yaml"')

if version_name is not None and app_version != version_name:
verrors.add(
f'{schema}.version',
'Configured version in "app.yaml" does not match version directory name.'
)
if app_config.get('annotations'):
validate_min_max_version_values(app_config['annotations'], verrors, schema)

if train_name is not None:
if app_config.get('train') != train_name:
verrors.add(f'{schema}.train', 'Train name not correctly set in "app.yaml".')
if version_name is not None and app_config['version'] != version_name:
verrors.add(f'{schema}.version', 'Version name does not match with the version name in the app version file')

else:
verrors.add(schema, 'Missing app version file')
if train_name is not None and app_config['train'] != train_name:
verrors.add(f'{schema}.train', 'Train name does not match with the train name in the app version file')

return verrors
34 changes: 0 additions & 34 deletions apps_validation/validation/app_version_refactor.py

This file was deleted.

0 comments on commit f9eaff2

Please sign in to comment.