cursus.validation.alignment.analyzer.script_analyzer¶
Contract-Focused Script Analyzer
Analyzes Python scripts for contract alignment validation. Focuses on main function signature and parameter usage patterns.
Based on analysis of actual scripts: - currency_conversion.py - xgboost_training.py
- class ScriptAnalyzer(script_path)[source]¶
Bases:
objectContract alignment focused script analyzer.
Validates: - Main function signature compliance - Parameter usage patterns (input_paths, output_paths, environ_vars, job_args) - Contract alignment validation
- validate_main_function_signature()[source]¶
Validate main function has correct signature.
Expected signature: def main(input_paths: Dict[str, str], output_paths: Dict[str, str],
environ_vars: Dict[str, str], job_args: argparse.Namespace) -> Any
- extract_parameter_usage()[source]¶
Extract how script uses main function parameters.
- Returns:
input_paths_keys: Keys used in input_paths[“key”] or input_paths.get(“key”)
output_paths_keys: Keys used in output_paths[“key”] or output_paths.get(“key”)
environ_vars_keys: Keys used in environ_vars.get(“key”)
job_args_attrs: Attributes used in job_args.attribute
- Return type:
Dictionary with parameter usage patterns
- validate_contract_alignment(contract)[source]¶
Validate script usage aligns with contract declarations.
- extract_argparse_flags()[source]¶
Extract every
parser.add_argument(...)declared ANYWHERE in the module.Walks the WHOLE module (not just
main) because the parser lives in a module-levelif __name__ == "__main__":block (often inside atry:), and may sit in a helper. Returns one record per add_argument:{flag, canonical, dest, required, choices, has_default, dynamic}wherecanonical(the normalized flag) is the comparison key anddynamicmarks a non-constant flag (built from a variable — absence can’t be proven).
- extract_env_reads()[source]¶
Find env-var keys the script READS, in two tiers (the Cat-5 two-tier rule):
all: every key referenced anywhere viaos.environ.get/os.getenv/os.environ[...](Load) /environ_vars.get/environ_vars[...](Load).consuming: keys read in a CONSUMING position — anenviron_vars.*access insidemain()’s body, OR anos.environ.*/os.getenvread that is NOT merely a value harvested into a module-levelenviron_vars = {...}dict literal. A var that is only harvested into that dict but never consumed in main() is exactly the Cat-5 bug, so it must NOT count asconsuming.
Env writes (
os.environ['K'] = ...— Store ctx) are excluded from both tiers.
- validate_reverse_alignment(contract, sagemaker_step_type=None)[source]¶
Reverse-direction alignment: does the script PARSE the args / READ the required env the contract+builder declare? Returns issues in the same schema the forward checker emits, so a caller can
.extend()them into oneissues[]. Additive — the forward methods the B1 validator consumes are untouched.