1+ #!/usr/bin/env python3
2+ import os
3+ import sys
4+ import json
5+ import yaml
6+ import boto3
7+ from pathlib import Path
8+ from botocore .exceptions import ClientError
9+
10+ def convert_yaml_to_json (yaml_path : Path , output_dir : Path ) -> Path :
11+ """Convert YAML file to JSON and return output file path."""
12+ try :
13+ with open (yaml_path , "r" ) as yf :
14+ data = yaml .safe_load (yf )
15+ except Exception as e :
16+ print (f"Error reading YAML file { yaml_path } : { e } " )
17+ raise
18+
19+ json_filename = yaml_path .stem + ".json"
20+ output_path = output_dir / json_filename
21+
22+ try :
23+ with open (output_path , "w" ) as jf :
24+ json .dump (data , jf , indent = 2 )
25+ except Exception as e :
26+ print (f"Error writing JSON file { output_path } : { e } " )
27+ raise
28+
29+ print (f"[OK] Converted { yaml_path } → { output_path } " )
30+ return output_path
31+
32+
33+ def upload_to_s3 (file_path : Path , bucket_name : str , folder_name : str ):
34+ """Upload one file to the given S3 bucket under folder_name/."""
35+ s3 = boto3 .client ("s3" )
36+ key = f"{ folder_name } /{ file_path .name } "
37+
38+ try :
39+ s3 .upload_file (str (file_path ), bucket_name , key )
40+ print (f"[OK] Uploaded → s3://{ bucket_name } /{ key } " )
41+ except ClientError as e :
42+ print (f"[ERROR] Upload failed: { file_path } → s3://{ bucket_name } /{ key } " )
43+ print (e )
44+ sys .exit (1 )
45+
46+
47+ def main (bucket_name : str ):
48+ repo_path = Path .cmd ()
49+ spec_dir = repo_path / "Specification"
50+
51+ repo_name = repo_path .name
52+
53+ if not spec_dir .exists ():
54+ print ("[SKIP] No Specification folder found — skipping all processing." )
55+ return 0
56+
57+ yaml_files = list (spec_dir .glob ("*.yaml" )) + list (spec_dir .glob ("*.yml" ))
58+
59+ if not yaml_files :
60+ print ("[SKIP] Specification folder exists, but no YAML files found — skipping." )
61+ return 0
62+
63+ output_dir = Path ("spec_output" )
64+ output_dir .mkdir (exist_ok = True )
65+
66+ generated_files = []
67+
68+ print (f"[INFO] Found { len (yaml_files )} YAML file(s). Converting..." )
69+
70+ for yaml_file in yaml_files :
71+ json_file = convert_yaml_to_json (yaml_file , output_dir )
72+ generated_files .append (json_file )
73+
74+ print (f"[INFO] Uploading { len (generated_files )} file(s) to S3 bucket '{ bucket_name } '..." )
75+ for json_file in generated_files :
76+ upload_to_s3 (json_file , bucket_name , repo_name )
77+
78+ print ("[DONE] Processing complete." )
79+ return 0
80+
81+
82+ if __name__ == "__main__" :
83+ print ("Hitting main" )
84+ #if len(sys.argv) != 2:
85+ # print("Usage: python copy_spec_to_s3.py <s3_bucket_name>")
86+ # sys.exit(1)
87+
88+ #bucket_name = sys.argv[1]
89+
90+ #sys.exit(main(bucket_name))
0 commit comments