-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathcgruTempFolder.py
More file actions
278 lines (226 loc) · 8.05 KB
/
cgruTempFolder.py
File metadata and controls
278 lines (226 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
import tempfile
import os
import shutil
# import stat
# import sys
import time
import datetime
import cgruutils
import uuid
MAX_AGE_LOCK_FILE = 300 # Value is in minutes
class cgruTempFolder(object):
"""Missing DocString
Creates a temp-folder
"""
def __init__(self, scene_name, service='generic', type_='render',
debug=False):
self.folderPath = None
self.debug = debug
self.type = type_
self.lock_uuid = str(uuid.uuid1())
self.lock_folder = None
self.lock_file = None
# Try to get the path to the main-temp-folder
self.temp_directory = tempfile.gettempdir()
# Get todays and yesterdays date for temp-folder-name and to know which
# ones to keep when the delete-old-temp-folder function runs
today_time = datetime.datetime.today()
yesterday_time = today_time - datetime.timedelta(1)
self.today = today_time.strftime("%Y-%m-%d")
self.yesterday = yesterday_time.strftime('%Y-%m-%d')
# Build the full-temp-path
self.folderPath = os.path.join(
self.temp_directory, self.type, self.today, service, scene_name
)
# Check if the temp-folder already exists and if not create it
if not os.path.isdir(self.folderPath):
creation_status = \
cgruutils.createFolder(self.folderPath, writeToAll=True)
# If the temp-folder does still not exist inform user
if not creation_status:
print("ERROR: The following Temp-Folder could not get "
"created: %s" % self.folderPath)
# If the temp-directory really exists go on
if os.path.isdir(self.folderPath):
if self.debug:
print("The Temp-Folder: %s" % self.folderPath)
# First create a lock-file for the folder
self.lock_folder = os.path.join(self.folderPath, ".lock")
self.lock_file = self.createLockFile()
# And then delete all the old-temp-folders which are not in use
# anymore
self.deleteOldTempFolders()
# This method should get called after the temp-folder is not needed any more
def closeTempFolder(self):
"""Missing DocString
"""
if self.lock_file is not None:
# Make sure that the lock-file gets deleted again
self.deleteLockFile()
else:
print("ERROR: The Temp-Folder was already closed")
# Set back the variables
self.folderPath = None
self.lock_folder = None
self.lock_file = None
# Returns if the temp-folder got created correctly and if it can get used
def checkStatus(self):
"""Missing DocString
"""
if self.lock_file is None:
return False
if self.folderPath is None:
return False
return True
def deleteTempFolder(self, folder_to_delete=""):
"""Deleted a temp-directory. Either the one with the given path or the
current one
"""
if folder_to_delete == "":
folder_to_delete = self.folderPath
try:
shutil.rmtree(folder_to_delete)
if self.debug:
print("The following Folder got deleted: %s" %
folder_to_delete)
except: # TODO: Too broad exception clause
# Then it seams that another task already deleted it in the
# meantime
pass
if self.debug:
print("The following Temp-Folder got deleted: %s" %
self.folderPath)
def checkIfTempFolderInUse(self, folder_to_check=None):
"""Checks if the temp-folder is in use (when none is given in checks
the own one)
"""
# When no folder is given check the own one
if folder_to_check is None:
folder_to_check = self.folderPath
this_lock_directory = os.path.join(folder_to_check, ".lock")
if os.path.isdir(this_lock_directory):
num_lock_files = len(os.listdir(this_lock_directory))
if num_lock_files == 0:
return False
else:
return True
else:
# Here we return "True" just for security reasons. If that folder
# does not exist it maybe means that there is something wrong and
# because this method decides if a whole folder gets deleted we
# should keep it that way.
return True
def deleteOldLockFiles(self, folder_name):
"""Missing DocString
:param folder_name:
:return:
"""
if os.path.isdir(folder_name):
# Now get all the lock-files in the lock-file-folder to check them
this_lock_directory = os.path.join(folder_name, ".lock")
# Check first if this directory really exits
if os.path.isdir(this_lock_directory):
# And if so look through the files in it
for this_file in os.listdir(this_lock_directory):
this_file_path = os.path.join(this_lock_directory,
this_file)
print(this_file_path)
if os.path.isfile(this_file_path):
# Get the age of the lock-file
change_time_file = int(
os.path.getmtime(this_file_path))
# That's the maximum age of a lock-file
max_file_age = \
int(time.time()) - (MAX_AGE_LOCK_FILE * 60)
# Check if it is older and if thats the case delete it
if change_time_file < max_file_age:
os.remove(this_file_path)
if self.debug:
print("The following lock-file got deleted "
"because it reached the maximum age: "
"%s" % this_file_path)
else:
# When it is a directory and not a file delete it
# because there are no directories allowed in the
# lock-folder
shutil.rmtree(this_file_path)
if self.debug:
print("The following folder got deleted because "
"no folders are allowed in the "
".lock-directory: %s" % this_file_path)
def deleteOldTempFolders(self):
"""Delete all old temp-folders
THINK ABOUT TO WRITE THIS METHOD NICER WITH SOME RECURSIVITY
"""
# Go through all files in the main-temp-folder
main_temp_folder = os.path.join(self.temp_directory, self.type)
if os.path.isdir(main_temp_folder):
# First go through the date directories
files_in_main_folder = os.listdir(main_temp_folder)
for this_date_file in files_in_main_folder:
this_date_folder = \
os.path.join(main_temp_folder, this_date_file)
if os.path.isdir(this_date_folder):
# Then go through the service-folders
files_in_date_folder = os.listdir(this_date_folder)
# First check if the folder has any content and if not
# delete it
if len(files_in_date_folder) == 0:
shutil.rmtree(this_date_folder)
continue
for this_service_file in files_in_date_folder:
this_service_folder = \
os.path.join(this_date_folder, this_service_file)
if os.path.isdir(this_service_folder):
# Then go through the job-folders
files_in_service_folder = os.listdir(
this_service_folder)
# First check if the folder has any content
# and if not delete it
if len(files_in_service_folder) == 0:
shutil.rmtree(this_service_folder)
continue
for this_job_file in files_in_service_folder:
this_job_folder = os.path.join(
this_service_folder, this_job_file)
if os.path.isdir(this_job_folder):
# First delete all the old lock-files
self.deleteOldLockFiles(this_job_folder)
# Then check if the folder is still in use
# or if it is from today.
#
# Only delete if nothing from both is the
# case
if not self.checkIfTempFolderInUse(
this_job_folder) \
and this_date_file != self.today:
self.deleteTempFolder(this_job_folder)
# Creates a lock file to know if a directory is currently in use
def createLockFile(self):
"""Missing DocString
"""
# Check if the lock-folder already exists and if not create it
if not os.path.isdir(self.lock_folder):
creation_status = \
cgruutils.createFolder(self.lock_folder, writeToAll=True)
# If the lock-folder does still not exist inform user
if creation_status:
if self.debug:
print("The Lock-Folder: %s" % self.lock_folder)
else:
print("ERROR: The following Lock-Folder could not get "
"created: %s" % self.lock_folder)
lock_file = os.path.join(self.lock_folder, self.lock_uuid)
the_lock_file = open(lock_file, "w")
the_lock_file.close()
if not os.path.isfile(lock_file):
print("ERROR: The following Lock-File could not get created: %s" %
lock_file)
return None
return lock_file
# Removes the current lock-file
def deleteLockFile(self):
"""Missing DocString
"""
os.remove(self.lock_file)