33Run with 'jote --help'
44"""
55import argparse
6- import glob
76import os
87import shutil
98from typing import Any , Dict , List , Optional , Tuple
2019
2120# Where can we expect to find Job definitions?
2221_DEFINITION_DIRECTORY : str = 'data-manager'
22+ # What's the default manifest file?
23+ _DEFAULT_MANIFEST : str = 'manifest.yaml'
2324# Where can we expect to find test data?
2425_DATA_DIRECTORY : str = 'data'
2526
@@ -85,6 +86,27 @@ def _validate_schema(definition_filename: str) -> bool:
8586 return True
8687
8788
89+ def _validate_manifest_schema (manifest_filename : str ) -> bool :
90+ """Checks the Manifest against the decoder's schema.
91+ """
92+
93+ with open (manifest_filename , 'rt' , encoding = 'UTF-8' ) as definition_file :
94+ job_def : Optional [Dict [str , Any ]] = \
95+ yaml .load (definition_file , Loader = yaml .FullLoader )
96+ assert job_def
97+
98+ # If the decoder returns something there's been an error.
99+ error : Optional [str ] = decoder .validate_manifest_schema (job_def )
100+ if error :
101+ print (f'! Manifest "{ manifest_filename } "'
102+ ' does not comply with schema' )
103+ print ('! Full response follows:' )
104+ print (error )
105+ return False
106+
107+ return True
108+
109+
88110def _check_cwd () -> bool :
89111 """Checks the execution directory for sanity (cwd). Here we must find
90112 a .yamllint file and a data-manager directory?
@@ -107,8 +129,9 @@ def _check_cwd() -> bool:
107129 return True
108130
109131
110- def _load (skip_lint : bool = False ) -> Tuple [List [DefaultMunch ], int ]:
111- """Loads definition files (all the YAML files in a given directory)
132+ def _load (manifest_filename : str , skip_lint : bool )\
133+ -> Tuple [List [DefaultMunch ], int ]:
134+ """Loads definition files listed in the manifest
112135 and extracts the definitions that contain at least one test. The
113136 definition blocks for those that have tests (ignored or otherwise)
114137 are returned along with a count of the number of tests found
@@ -117,23 +140,37 @@ def _load(skip_lint: bool = False) -> Tuple[List[DefaultMunch], int]:
117140 If there was a problem loading the files an empty list and
118141 -ve count is returned.
119142 """
143+ manifest_path : str = os .path .join (_DEFINITION_DIRECTORY , manifest_filename )
144+ if not os .path .isfile (manifest_path ):
145+ print (f'! The manifest file is missing ("{ manifest_path } ")' )
146+ return [], - 1
147+
148+ if not _validate_manifest_schema (manifest_path ):
149+ return [], - 1
150+
151+ with open (manifest_path , 'r' , encoding = 'UTF-8' ) as manifest_file :
152+ manifest : Dict [str , Any ] = yaml .load (manifest_file , Loader = yaml .FullLoader )
153+ if manifest :
154+ manifest_munch : DefaultMunch = DefaultMunch .fromDict (manifest )
155+
156+ # Iterate through the named files...
120157 job_definitions : List [DefaultMunch ] = []
121158 num_tests : int = 0
122159
123- jd_filenames : List [str ] = glob .glob (f'{ _DEFINITION_DIRECTORY } /*.yaml' )
124- for jd_filename in jd_filenames :
160+ for jd_filename in manifest_munch ['job-definition-files' ]:
125161
126- # Does the definition comply with the dschema,
127- # no options here - it must.
128- if not _validate_schema (jd_filename ):
162+ # Does the definition comply with the dschema?
163+ # No options here - it must.
164+ jd_path : str = os .path .join (_DEFINITION_DIRECTORY , jd_filename )
165+ if not _validate_schema (jd_path ):
129166 return [], - 1
130167
131168 # YAML-lint the definition?
132169 if not skip_lint :
133- if not _lint (jd_filename ):
170+ if not _lint (jd_path ):
134171 return [], - 2
135172
136- with open (jd_filename , 'r' , encoding = 'UTF-8' ) as jd_file :
173+ with open (jd_path , 'r' , encoding = 'UTF-8' ) as jd_file :
137174 job_def : Dict [str , Any ] = yaml .load (jd_file , Loader = yaml .FullLoader )
138175 if job_def :
139176 jd_munch : DefaultMunch = DefaultMunch .fromDict (job_def )
@@ -494,9 +531,14 @@ def main() -> int:
494531
495532 # Build a command-line parser
496533 # and process the command-line...
497- arg_parser : argparse .ArgumentParser = \
498- argparse .ArgumentParser (description = 'Data Manager Job Tester' )
499-
534+ arg_parser : argparse .ArgumentParser = argparse \
535+ .ArgumentParser (description = 'Data Manager Job Tester' ,
536+ formatter_class = argparse .ArgumentDefaultsHelpFormatter )
537+
538+ arg_parser .add_argument ('-m' , '--manifest' ,
539+ help = 'The manifest file.' ,
540+ default = _DEFAULT_MANIFEST ,
541+ type = str )
500542 arg_parser .add_argument ('-c' , '--collection' ,
501543 help = 'The Job collection to test. If not'
502544 ' specified the Jobs in all collections'
@@ -580,8 +622,10 @@ def main() -> int:
580622 print ('Done [Wiped]' )
581623 return 0
582624
625+ print (f'# Using manifest "{ args .manifest } "' )
626+
583627 # Load all the files we can and then run the tests.
584- job_definitions , num_tests = _load (args .skip_lint )
628+ job_definitions , num_tests = _load (args .manifest , args . skip_lint )
585629 if num_tests < 0 :
586630 print ('! FAILURE' )
587631 print ('! Definition file has failed yamllint' )
0 commit comments