@@ -11,15 +11,17 @@ class Cleaner:
11
11
Files are cleaned based on last modification time, which have to be in interval given in configuration.
12
12
"""
13
13
14
- def __init__ (self , config ):
14
+ def __init__ (self , config , logger ):
15
15
"""
16
16
Constructor which takes configuration of cleaner.
17
17
18
18
:param config: have to be instance of ConfigManager class
19
+ :param logger: System logger instance
19
20
:raises Exception: if values from configuration are not valid
20
21
"""
21
22
self ._cache_dir = config .get_cache_dir ()
22
23
self ._file_age = config .get_file_age ()
24
+ self ._logger = logger
23
25
24
26
self .check_cache_dir ()
25
27
self .check_file_age ()
@@ -47,10 +49,8 @@ def check_file_age(self):
47
49
if self ._file_age is None :
48
50
raise Exception ("File age not specified!" )
49
51
50
- if not self ._file_age .isdigit ():
51
- raise Exception ("File age is not a number!" )
52
-
53
- self ._file_age = int (self ._file_age )
52
+ if self ._file_age <= 0 :
53
+ raise Exception ("File age is not positive number!" )
54
54
55
55
def clean (self ):
56
56
"""
@@ -61,41 +61,61 @@ def clean(self):
61
61
62
62
# some general debug information about this time execution
63
63
now = round (time .time ())
64
- print ("****************************************" )
65
- print ("Cleaning files from \" " + self ._cache_dir + "\" " )
66
- print ("With maximum file age: " + str (self ._file_age ) + " seconds" )
67
- print ("Timestamp now: " + str (now ) + " seconds" )
68
- print ("****************************************" )
64
+ self ._logger .info ("Cleaning files from \" {}\" " .format (self ._cache_dir ))
65
+ self ._logger .info ("Maximum file age: {} seconds" .format (self ._file_age ))
69
66
70
- def process_path (root , file , action ):
67
+ def process_file (root , file ):
71
68
"""
72
- Process given path, path can be file or directory.
73
- Given action is executed on constructed full path
69
+ Process given file. If modification timestamp is too old, the file will be removed.
74
70
75
71
:param root: path to root directory, used as base
76
72
:param file: name of file itself, root is used as base and joined with this
77
- :param action: action which will be performed if modification timestamp is older than given interval
78
73
:return: Nothing
79
74
"""
80
75
81
76
full_path = os .path .join (root , file )
82
77
last_modification = round (os .stat (full_path ).st_mtime )
83
78
difference = now - last_modification
84
79
85
- print (full_path )
86
- print (" last modification: " + str (last_modification ))
87
- print (" difference: " + str (difference ))
80
+ self ._logger .debug ("last modification: {}" .format (last_modification ))
81
+ self ._logger .debug ("age from now: {} seconds" .format (difference ))
88
82
89
83
if difference > self ._file_age :
84
+ self ._logger .debug ("file \" {}\" marked for deletion" .format (full_path ))
90
85
try :
91
- action (full_path )
92
- print ( " >>> REMOVED <<<" )
86
+ os . remove (full_path )
87
+ self . _logger . info ( "file \" {} \" removed" . format ( full_path ) )
93
88
except Exception as ex :
94
- print (" >>> Exception occured: " + str (ex ))
89
+ self ._logger .warning ("removing file \" {}\" failed: {}" .format (full_path , ex ))
90
+ else :
91
+ self ._logger .debug ("file \" {}\" will be kept" .format (full_path ))
92
+
93
+ def process_directory (root , dir ):
94
+ """
95
+ Process given directory. If it is empty, the directory will be removed.
95
96
96
- # iterate recursively through given directory
97
- for root , dirs , files in os .walk (self ._cache_dir ):
97
+ :param root: path to root directory, used as base
98
+ :param file: name of directory itself, root is used as base and joined with this
99
+ :return: Nothing
100
+ """
101
+
102
+ full_path = os .path .join (root , dir )
103
+ if not os .listdir (full_path ):
104
+ self ._logger .debug ("directory \" {}\" is empty" .format (full_path ))
105
+ try :
106
+ os .rmdir (full_path )
107
+ self ._logger .info ("directory \" {}\" removed" .format (full_path ))
108
+ except Exception as ex :
109
+ self ._logger .warning ("removing directory \" {}\" failed: {}" .format (full_path , ex ))
110
+ else :
111
+ self ._logger .debug ("directory \" {}\" is not empty and will be kept" .format (full_path ))
112
+
113
+ # iterate recursively through given directory (in DFS order)
114
+ for root , dirs , files in os .walk (self ._cache_dir , topdown = False ):
98
115
for file in files :
99
- process_path (root , file , os .remove )
116
+ self ._logger .info ("Processing file: {}" .format (file ))
117
+ process_file (root , file )
100
118
for dir in dirs :
101
- process_path (root , dir , os .rmdir )
119
+ self ._logger .info ("Processing directory: {}" .format (dir ))
120
+ process_directory (root , dir )
121
+
0 commit comments