1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name
"""Script to validate dtFabric format definitions."""
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import glob
import logging
import os
import sys
from dtfabric import errors
from dtfabric import reader
from dtfabric import registry
class DefinitionsValidator(object):
"""dtFabric definitions validator."""
def CheckDirectory(self, path, extension='yaml'):
"""Validates definition files in a directory.
Args:
path (str): path of the definition file.
extension (Optional[str]): extension of the filenames to read.
Returns:
bool: True if the directory contains valid definitions.
"""
result = True
if extension:
glob_spec = os.path.join(path, '*.{0:s}'.format(extension))
else:
glob_spec = os.path.join(path, '*')
for definition_file in sorted(glob.glob(glob_spec)):
if not self.CheckFile(definition_file):
result = False
return result
def CheckFile(self, path):
"""Validates the definition in a file.
Args:
path (str): path of the definition file.
Returns:
bool: True if the file contains valid definitions.
"""
print('Checking: {0:s}'.format(path))
definitions_registry = registry.DataTypeDefinitionsRegistry()
definitions_reader = reader.YAMLDataTypeDefinitionsFileReader()
result = False
try:
definitions_reader.ReadFile(definitions_registry, path)
result = True
except KeyError as exception:
logging.warning((
'Unable to register data type definition in file: {0:s} with '
'error: {1:s}').format(path, exception))
except errors.FormatError as exception:
logging.warning(
'Unable to validate file: {0:s} with error: {1:s}'.format(
path, exception))
return result
def Main():
"""The main program function.
Returns:
bool: True if successful or False if not.
"""
argument_parser = argparse.ArgumentParser(
description='Validates dtFabric format definitions.')
argument_parser.add_argument(
'source', nargs='?', action='store', metavar='PATH', default=None,
help=(
'path of the file or directory containing the dtFabric format '
'definitions.'))
options = argument_parser.parse_args()
if not options.source:
print('Source value is missing.')
print('')
argument_parser.print_help()
print('')
return False
if not os.path.exists(options.source):
print('No such file: {0:s}'.format(options.source))
print('')
return False
logging.basicConfig(
level=logging.INFO, format='[%(levelname)s] %(message)s')
source_is_directory = os.path.isdir(options.source)
validator = DefinitionsValidator()
if source_is_directory:
source_description = os.path.join(options.source, '*.yaml')
else:
source_description = options.source
print('Validating dtFabric definitions in: {0:s}'.format(source_description))
if source_is_directory:
result = validator.CheckDirectory(options.source)
else:
result = validator.CheckFile(options.source)
if not result:
print('FAILURE')
else:
print('SUCCESS')
return result
if __name__ == '__main__':
if not Main():
sys.exit(1)
else:
sys.exit(0)
|