11import gc
22import os
3+ import time
34import shutil
45import logging
56import traceback
1112
1213ROOT_DIR = os .path .dirname (os .path .abspath (__file__ ))
1314# Path used for download file generation.
14- DOWNLOAD_FILE_PATH = f'{ Path .cwd ()} /downloads'
15+ DOWNLOAD_DIR = f'{ Path .cwd ()} /downloads'
1516
1617logging .basicConfig ()
1718logger = logging .getLogger ('OSW_VALIDATION' )
@@ -26,46 +27,55 @@ def __init__(self, file_path=None, storage_client=None):
2627 self .file_path = file_path
2728 self .file_relative_path = file_path .split ('/' )[- 1 ]
2829 self .client = self .storage_client .get_container (container_name = self .container_name )
30+ is_exists = os .path .exists (DOWNLOAD_DIR )
31+ unique_id = self .get_unique_id ()
32+ if not is_exists :
33+ os .makedirs (DOWNLOAD_DIR )
34+ self .unique_dir_path = os .path .join (DOWNLOAD_DIR , unique_id )
35+ if not os .path .exists (self .unique_dir_path ):
36+ os .makedirs (self .unique_dir_path )
2937
3038 def validate (self , max_errors = 20 ) -> ValidationResult :
31- return self .is_osw_valid (max_errors )
39+ try :
40+ return self .is_osw_valid (max_errors )
41+ finally :
42+ Validation .clean_up (self .unique_dir_path )
3243
3344 def is_osw_valid (self , max_errors ) -> ValidationResult :
45+ start_time = time .time ()
3446 result = ValidationResult ()
3547 result .is_valid = False
3648 result .validation_message = ''
3749 root , ext = os .path .splitext (self .file_relative_path )
3850 if ext and ext .lower () == '.zip' :
3951 downloaded_file_path = self .download_single_file (self .file_path )
40- logger .info (f' Downloaded file path: { downloaded_file_path } ' )
41- validator = OSWValidation (zipfile_path = downloaded_file_path )
42- validation_result = validator .validate (max_errors )
43- result .is_valid = validation_result .is_valid
44- if not result .is_valid :
45- result .validation_message = validation_result .errors
46- logger .error (f' Error While Validating File: { str (validation_result .errors )} ' )
47- Validation .clean_up (downloaded_file_path )
52+ if downloaded_file_path :
53+ logger .info (f' Downloaded file path: { downloaded_file_path } ' )
54+ validator = OSWValidation (zipfile_path = downloaded_file_path )
55+ validation_result = validator .validate (max_errors )
56+ result .is_valid = validation_result .is_valid
57+ if not result .is_valid :
58+ result .validation_message = validation_result .errors
59+ logger .error (f' Error While Validating File: { str (validation_result .errors )} ' )
60+ Validation .clean_up (downloaded_file_path )
61+ else :
62+ result .validation_message = 'Failed to validate because unknown file format'
4863 else :
4964 result .validation_message = 'Failed to validate because unknown file format'
5065 logger .error (f' Failed to validate because unknown file format' )
66+ end_time = time .time ()
67+ time_taken = end_time - start_time
68+ logger .info (f'Validation completed in { time_taken } seconds' )
5169 gc .collect ()
5270 return result
5371
5472 # Downloads the single file into a unique directory
5573 def download_single_file (self , file_upload_path = None ) -> str :
56- is_exists = os .path .exists (DOWNLOAD_FILE_PATH )
57- unique_id = self .get_unique_id ()
58- if not is_exists :
59- os .makedirs (DOWNLOAD_FILE_PATH )
60- unique_directory = os .path .join (DOWNLOAD_FILE_PATH , unique_id )
61- if not os .path .exists (unique_directory ):
62- os .makedirs (unique_directory )
63-
6474 file = self .storage_client .get_file_from_url (self .container_name , file_upload_path )
6575 try :
6676 if file .file_path :
6777 file_path = os .path .basename (file .file_path )
68- local_download_path = os .path .join (unique_directory , file_path )
78+ local_download_path = os .path .join (self . unique_dir_path , file_path )
6979 with open (local_download_path , 'wb' ) as blob :
7080 blob .write (file .get_stream ())
7181 logger .info (f' File downloaded to location: { local_download_path } ' )
0 commit comments