1111
1212import os
1313import yaml
14+ import shutil
1415import ZODB
1516import datetime
1617import ZODB .FileStorage
1920from unskript_utils import *
2021
2122
22- def remove_old_ipynb_files ():
23+ # We also need to remove old directories in the execution directory
24+ # That are older than the threshold date
25+ def remove_old_directories ():
2326 # Read the Audit period from config file
2427 audit_period = get_audit_period ()
2528 current_date = datetime .datetime .now ()
2629 threshold_date = current_date - datetime .timedelta (days = audit_period )
27- files_deleted = False
28- for ipynb_file in os .listdir (UNSKRIPT_EXECUTION_DIR ):
29- if ipynb_file . endswith ( '.ipynb' ):
30- file_path = os .path .join ( UNSKRIPT_EXECUTION_DIR , ipynb_file )
31- file_ts = datetime .datetime .fromtimestamp (os .path .getmtime (file_path ))
32- if file_ts < threshold_date :
30+ directories_deleted = False
31+ for directory in os .listdir (UNSKRIPT_EXECUTION_DIR ):
32+ dir_path = os . path . join ( UNSKRIPT_EXECUTION_DIR , directory )
33+ if os .path .isdir ( dir_path ):
34+ dir_ts = datetime .datetime .fromtimestamp (os .path .getmtime (dir_path ))
35+ if dir_ts < threshold_date :
3336 try :
34- os .remove (file_path )
35- print (f"Deleted { file_path } " )
36- files_deleted = True
37+ # Use shutil.rmtree instead of os.rmdir to remove non-empty directories
38+ shutil .rmtree (dir_path )
39+ print (f"Deleted { dir_path } " )
40+ directories_deleted = True
3741 except Exception as e :
38- print (f"ERROR: { e } " )
39- return
42+ print (f"ERROR: Failed to delete { dir_path } : { e } " )
43+ # Continue with other directories rather than returning
44+ continue
4045
41- if files_deleted :
42- print (f"Deleted ipynb files older than { audit_period } days!" )
46+ if directories_deleted :
47+ print (f"Deleted directories older than { audit_period } days!" )
4348 else :
44- print (f"No ipynb files are older than { audit_period } . Nothing to delete" )
49+ print (f"No directories are older than { audit_period } . Nothing to delete" )
4550 return
4651
52+
4753def get_audit_period ():
4854 audit_period = 90
4955 try :
5056 if os .path .exists (GLOBAL_CTL_CONFIG ) is True :
51- with open (GLOBAL_CTL_CONFIG , 'r' , encoding = ' utf-8' ) as f :
57+ with open (GLOBAL_CTL_CONFIG , "r" , encoding = " utf-8" ) as f :
5258 data = yaml .safeload (f .read ())
53- if data and data .get ('global' ) and data .get ('global' ).get ('audit_period' ):
54- audit_period = data .get ('global' ).get ('audit_period' )
59+ if (
60+ data
61+ and data .get ("global" )
62+ and data .get ("global" ).get ("audit_period" )
63+ ):
64+ audit_period = data .get ("global" ).get ("audit_period" )
5565 except :
5666 # We use 90 days as the default period to cleanup then.
57- pass
67+ pass
5868 return audit_period
5969
6070
61- def clean_db () -> DB :
71+ def clean_db () -> None :
6272 """clean_db This function calls the db.pack(...) function to cleanup the ZoDB of data that are audit_period old
63- default is 90 days. This function can be called as docker cron job to cleanup ZoDB data that are 90 days old
73+ default is 90 days. This function can be called as docker cron job to cleanup ZoDB data that are 90 days old
6474 """
6575 audit_period = get_audit_period ()
6676
@@ -73,6 +83,6 @@ def clean_db() -> DB:
7383 print (f"ERROR: { e } " )
7484
7585
76- if __name__ == ' __main__' :
77- remove_old_ipynb_files ()
78- clean_db ()
86+ if __name__ == " __main__" :
87+ remove_old_directories ()
88+ clean_db ()
0 commit comments