@@ -15,75 +15,79 @@ def connect_firebase(credentials_file, bucket_name):
1515 except Exception as e :
1616 print (f"Failed to connect to Firebase bucket: { e } " )
1717
18- def execute (args ):
19- results = []
20- shouldDownload = True
21- connections = system .get_connection ()
18+ def execute (args , programmatic = False ):
19+ try :
20+ results = []
21+ shouldDownload = True
22+ connections = system .get_connection (args , programmatic )
23+ fingerprints = system .get_fingerprint_file (args , programmatic )
2224
23- if 'sources' in connections :
24- sources_config = connections ['sources' ]
25- firebase_config = sources_config .get ('firebase' )
25+ if 'sources' in connections :
26+ sources_config = connections ['sources' ]
27+ firebase_config = sources_config .get ('firebase' )
2628
27- if firebase_config :
28- for key , config in firebase_config .items ():
29- credentials_file = config .get ('credentials_file' )
30- bucket_name = config .get ('bucket_name' )
31- exclude_patterns = config .get (key , {}).get ('exclude_patterns' , [])
29+ if firebase_config :
30+ for key , config in firebase_config .items ():
31+ credentials_file = config .get ('credentials_file' )
32+ bucket_name = config .get ('bucket_name' )
33+ exclude_patterns = config .get (key , {}).get ('exclude_patterns' , [])
3234
33- if credentials_file and bucket_name :
34- bucket = connect_firebase (credentials_file , bucket_name )
35- if bucket :
36- for blob in bucket .list_blobs ():
37- file_name = blob .name
38- ## get unique etag or hash of file
39- remote_etag = blob .etag
40- system .print_debug (f"Remote etag: { remote_etag } " )
35+ if credentials_file and bucket_name :
36+ bucket = connect_firebase (credentials_file , bucket_name )
37+ if bucket :
38+ for blob in bucket .list_blobs ():
39+ file_name = blob .name
40+ ## get unique etag or hash of file
41+ remote_etag = blob .etag
42+ system .print_debug (f"Remote etag: { remote_etag } " )
4143
42- if system .should_exclude_file (file_name , exclude_patterns ):
43- continue
44+ if system .should_exclude_file (file_name , exclude_patterns ):
45+ continue
4446
45- file_path = f"data/firebase/{ remote_etag } -{ file_name } "
46- os .makedirs (os .path .dirname (file_path ), exist_ok = True )
47+ file_path = f"data/firebase/{ remote_etag } -{ file_name } "
48+ os .makedirs (os .path .dirname (file_path ), exist_ok = True )
4749
48- if config .get ("cache" ) == True :
49- if os .path .exists (file_path ):
50- shouldDownload = False
51- local_etag = file_path .split ('/' )[- 1 ].split ('-' )[0 ]
52- system .print_debug (f"Local etag: { local_etag } " )
53- system .print_debug (f"File already exists in cache, using it. You can disable cache by setting 'cache: false' in connection.yml" )
54- if remote_etag != local_etag :
55- system .print_debug (f"File in firebase bucket has changed, downloading it again..." )
56- shouldDownload = True
57- else :
50+ if config .get ("cache" ) == True :
51+ if os .path .exists (file_path ):
5852 shouldDownload = False
53+ local_etag = file_path .split ('/' )[- 1 ].split ('-' )[0 ]
54+ system .print_debug (f"Local etag: { local_etag } " )
55+ system .print_debug (f"File already exists in cache, using it. You can disable cache by setting 'cache: false' in connection.yml" )
56+ if remote_etag != local_etag :
57+ system .print_debug (f"File in firebase bucket has changed, downloading it again..." )
58+ shouldDownload = True
59+ else :
60+ shouldDownload = False
5961
60- if shouldDownload :
61- file_path = f"data/firebase/{ remote_etag } -{ file_name } "
62- system .print_debug (f"Downloading file: { file_name } to { file_path } ..." )
63- blob .download_to_filename (file_path )
64-
65- matches = system .read_match_strings (file_path , 'google_cloud_storage' )
66- if matches :
67- for match in matches :
68- results .append ({
69- 'bucket' : bucket_name ,
70- 'file_path' : file_name ,
71- 'pattern_name' : match ['pattern_name' ],
72- 'matches' : match ['matches' ],
73- 'sample_text' : match ['sample_text' ],
74- 'profile' : key ,
75- 'data_source' : 'firebase'
76- })
62+ if shouldDownload :
63+ file_path = f"data/firebase/{ remote_etag } -{ file_name } "
64+ system .print_debug (f"Downloading file: { file_name } to { file_path } ..." )
65+ blob .download_to_filename (file_path )
66+
67+ matches = system .analyze_file (file_path , 'google_cloud_storage' , connections , fingerprints , programmatic = programmatic )
68+ if matches :
69+ for match in matches :
70+ results .append ({
71+ 'bucket' : bucket_name ,
72+ 'file_path' : file_name ,
73+ 'pattern_name' : match ['pattern_name' ],
74+ 'matches' : match ['matches' ],
75+ 'sample_text' : match ['sample_text' ],
76+ 'profile' : key ,
77+ 'data_source' : 'firebase'
78+ })
7779
80+ else :
81+ system .print_error (f"Failed to connect to Firebase bucket: { bucket_name } " )
7882 else :
79- system .print_error (f"Failed to connect to Firebase bucket : { bucket_name } " )
80- else :
81- system .print_error (f"Incomplete Firebase configuration for key: { key } " )
83+ system .print_error (f"Incomplete Firebase configuration for key : { key } " )
84+ else :
85+ system .print_error ("No Firebase connection details found in connection file " )
8286 else :
83- system .print_error ("No Firebase connection details found in connection file " )
84- else :
85- system . print_error ( "No 'sources' section found in connection.yml" )
86-
87- if config . get ( "cache" ) == False :
88- os . system ( "rm -rf data/firebase " )
87+ system .print_error ("No 'sources' section found in connection.yml " )
88+
89+ if config . get ( "cache" ) == False :
90+ os . system ( "rm -rf data/firebase" )
91+ except Exception as e :
92+ print ( f"Failed to connect to Firebase bucket: { e } " )
8993 return results
0 commit comments