1+ import os
2+ import sys
3+ import boto3
4+ import json
5+ from pathlib import Path
6+ from botocore .exceptions import ClientError
7+
8+ def upload_to_s3 (file_path : Path , bucket_name : str , folder_name : str ):
9+ """Upload one file to the given S3 bucket under folder_name/."""
10+ s3 = boto3 .client ("s3" )
11+ key = f"apis/{ folder_name } /{ file_path } "
12+
13+ try :
14+ s3 .upload_file (str (file_path ), bucket_name , key )
15+ print (f"[OK] Uploaded → s3://{ bucket_name } /apis/{ key } " )
16+ except ClientError as e :
17+ print (f"[ERROR] Upload failed: { file_path } → s3://{ bucket_name } /{ key } " )
18+ print (e )
19+ sys .exit (1 )
20+
21+
22+ def main (bucket_name : str , repo_name : str ):
23+ cwd = os .getcwd ()
24+ print ("Current working directory:" , cwd )
25+
26+ root_dir = Path .cwd ().parents [1 ]
27+ json_file = root_dir / f"{ repo_name } .json"
28+ minified_json = "spec.json"
29+
30+ print (json_file )
31+
32+ if not json_file .is_file ():
33+ print (f"[ERROR] JSON spec not found: { json_file } " )
34+ return 1
35+
36+ with open (json_file , "r" ) as f :
37+ data = json .load (f )
38+
39+ with open (minified_json , "w" ) as f :
40+ json .dump (data , f , separators = ("," , ":" ))
41+
42+ upload_to_s3 (minified_json , bucket_name , repo_name )
43+
44+ print ("[DONE] Processing complete." )
45+ return 0
46+
47+ if __name__ == "__main__" :
48+ print ("Hitting main" )
49+
50+ if len (sys .argv ) != 3 :
51+ print ("Usage: python copy_spec_to_s3.py <s3_bucket_name> <repo_name>" )
52+ sys .exit (1 )
53+
54+ bucket_name = sys .argv [1 ]
55+ repo_name = sys .argv [2 ]
56+ print (f"Repo name: { repo_name } " )
57+ print (f"Bucket name: { bucket_name } " )
58+
59+ sys .exit (main (bucket_name ,repo_name ))
0 commit comments