Skip to content

Commit

Permalink
#2300: scripts: add --validate_comm_links option
Browse files Browse the repository at this point in the history
  • Loading branch information
cz4rs committed Jul 11, 2024
1 parent ddf678a commit b160528
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions scripts/JSON_data_files_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,14 @@ def validate(self, schema_to_validate: dict):

class JSONDataFilesValidator:
""" Class validating VT data files according do defined schema. """
def __init__(self, file_path: str = None, dir_path: str = None, file_prefix: str = None, file_suffix: str = None):
def __init__(self, file_path: str = None, dir_path: str = None,
file_prefix: str = None, file_suffix: str = None,
validate_comm_links: bool = False):
self.__file_path = file_path
self.__dir_path = dir_path
self.__file_prefix = file_prefix
self.__file_suffix = file_suffix
self.__validate_comm_links = validate_comm_links
self.__cli()

def __cli(self):
Expand All @@ -390,6 +393,7 @@ def __cli(self):
group.add_argument("--file_path", help="Path to a validated file. Pass only when validating a single file.")
parser.add_argument("--file_prefix", help="File prefix. Optional. Pass only when --dir_path is provided.")
parser.add_argument("--file_suffix", help="File suffix. Optional. Pass only when --dir_path is provided.")
parser.add_argument("--validate_comm_links", help='Verify that comm links reference tasks.', action='store_true')
args = parser.parse_args()
if args.file_path:
self.__file_path = os.path.abspath(args.file_path)
Expand All @@ -399,6 +403,7 @@ def __cli(self):
self.__file_prefix = args.file_prefix
if args.file_suffix:
self.__file_suffix = args.file_suffix
self.__validate_comm_links = args.validate_comm_links

@staticmethod
def __check_if_file_exists(file_path: str) -> bool:
Expand Down Expand Up @@ -436,7 +441,7 @@ def __get_files_for_validation(dir_path: str, file_prefix: str, file_suffix: str
key=lambda x: int(x.split(os.sep)[-1].split('.')[-2]))

@staticmethod
def __validate_file(file_path):
def __validate_file(file_path, validate_comm_links):
""" Validates the file against the schema. """
logging.info(f"Validating file: {file_path}")
with open(file_path, "rb") as json_file:
Expand All @@ -458,15 +463,20 @@ def __validate_file(file_path):
if SchemaValidator(schema_type=schema_type).is_valid(schema_to_validate=json_data):
logging.info(f"Valid JSON schema in {file_path}")
else:
print(f"Invalid JSON schema in {file_path}")
logging.error(f"Invalid JSON schema in {file_path}")
SchemaValidator(schema_type=schema_type).validate(schema_to_validate=json_data)
else:
logging.warning(f"Schema type not found in file: {file_path}. \nPassing by default when schema type not found.")

if validate_comm_links:
logging.error("FIXME: comm_links validation not implemented.")


def main(self):
if self.__file_path is not None:
if self.__check_if_file_exists(file_path=self.__file_path):
self.__validate_file(file_path=self.__file_path)
self.__validate_file(file_path=self.__file_path,
validate_comm_links=self.__validate_comm_links)
else:
sys.excepthook = exc_handler
raise FileNotFoundError(f"File: {self.__file_path} NOT found")
Expand All @@ -476,7 +486,8 @@ def main(self):
file_prefix=self.__file_prefix,
file_suffix=self.__file_suffix)
for file in list_of_files_for_validation:
self.__validate_file(file_path=file)
self.__validate_file(file_path=file,
validate_comm_links=self.__validate_comm_links)
else:
sys.excepthook = exc_handler
raise FileNotFoundError(f"Directory: {self.__dir_path} does NOT exist")
Expand Down
2 changes: 1 addition & 1 deletion scripts/check_lb_data_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set +x
function run_schema_validator() {
file=$1
echo "Running schema validator on: $file"
if python3 "${path_to_vt_src_dir}/scripts/JSON_data_files_validator.py" --file_path="$file"
if python3 "${path_to_vt_src_dir}/scripts/JSON_data_files_validator.py" --file_path="$file" --validate_comm_links
then
echo "Valid file"
else
Expand Down

0 comments on commit b160528

Please sign in to comment.