1+ import gc
12import os
3+ import time
24import shutil
35import logging
46import traceback
1012
1113ROOT_DIR = os .path .dirname (os .path .abspath (__file__ ))
1214# Path used for download file generation.
13- DOWNLOAD_FILE_PATH = f'{ Path .cwd ()} /downloads'
15+ DOWNLOAD_DIR = f'{ Path .cwd ()} /downloads'
1416
1517logging .basicConfig ()
1618logger = logging .getLogger ('OSW_VALIDATION' )
@@ -25,46 +27,55 @@ def __init__(self, file_path=None, storage_client=None):
2527 self .file_path = file_path
2628 self .file_relative_path = file_path .split ('/' )[- 1 ]
2729 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 )
2837
2938 def validate (self , max_errors = 20 ) -> ValidationResult :
30- 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 )
3143
3244 def is_osw_valid (self , max_errors ) -> ValidationResult :
45+ start_time = time .time ()
3346 result = ValidationResult ()
3447 result .is_valid = False
3548 result .validation_message = ''
3649 root , ext = os .path .splitext (self .file_relative_path )
3750 if ext and ext .lower () == '.zip' :
3851 downloaded_file_path = self .download_single_file (self .file_path )
39- logger .info (f' Downloaded file path: { downloaded_file_path } ' )
40- validator = OSWValidation (zipfile_path = downloaded_file_path )
41- validation_result = validator .validate (max_errors )
42- result .is_valid = validation_result .is_valid
43- if not result .is_valid :
44- result .validation_message = validation_result .errors
45- logger .error (f' Error While Validating File: { str (validation_result .errors )} ' )
46- 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'
4763 else :
4864 result .validation_message = 'Failed to validate because unknown file format'
4965 logger .error (f' Failed to validate because unknown file format' )
50-
66+ end_time = time .time ()
67+ time_taken = end_time - start_time
68+ logger .info (f'Validation completed in { time_taken } seconds' )
69+ gc .collect ()
5170 return result
5271
5372 # Downloads the single file into a unique directory
5473 def download_single_file (self , file_upload_path = None ) -> str :
55- is_exists = os .path .exists (DOWNLOAD_FILE_PATH )
56- unique_id = self .get_unique_id ()
57- if not is_exists :
58- os .makedirs (DOWNLOAD_FILE_PATH )
59- unique_directory = os .path .join (DOWNLOAD_FILE_PATH ,unique_id )
60- if not os .path .exists (unique_directory ):
61- os .makedirs (unique_directory )
62-
6374 file = self .storage_client .get_file_from_url (self .container_name , file_upload_path )
6475 try :
6576 if file .file_path :
6677 file_path = os .path .basename (file .file_path )
67- local_download_path = os .path .join (unique_directory , file_path )
78+ local_download_path = os .path .join (self . unique_dir_path , file_path )
6879 with open (local_download_path , 'wb' ) as blob :
6980 blob .write (file .get_stream ())
7081 logger .info (f' File downloaded to location: { local_download_path } ' )
@@ -74,14 +85,14 @@ def download_single_file(self, file_upload_path=None) -> str:
7485 except Exception as e :
7586 traceback .print_exc ()
7687 logger .error (e )
88+ finally :
89+ gc .collect ()
7790
7891 # Generates a unique string for directory
7992 def get_unique_id (self ) -> str :
8093 unique_id = uuid .uuid1 ().hex [0 :24 ]
8194 return unique_id
8295
83-
84-
8596 @staticmethod
8697 def clean_up (path ):
8798 if os .path .isfile (path ):
@@ -91,3 +102,4 @@ def clean_up(path):
91102 # folder = os.path.join(DOWNLOAD_FILE_PATH, path)
92103 logger .info (f' Removing Folder: { path } ' )
93104 shutil .rmtree (path , ignore_errors = False )
105+ gc .collect ()
0 commit comments