forked from vitiral/cloudtb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempfiles.py
184 lines (156 loc) · 6.35 KB
/
tempfiles.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ****** The Cloud Toolbox v0.1.2******
# This is the cloud toolbox -- a single module used in several packages
# found at <https://github.com/cloudformdesign>
# For more information see <cloudformdesign.com>
#
# This module may be a part of a python package, and may be out of date.
# This behavior is intentional, do NOT update it.
#
# You are encouraged to use this pacakge, or any code snippets in it, in
# your own projects. Hopefully they will be helpful to you!
#
# This project is Licenced under The MIT License (MIT)
#
# Copyright (c) 2013 Garrett Berg cloudformdesign.com
# An updated version of this file can be found at:
# <https://github.com/cloudformdesign/cloudtb>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# http://opensource.org/licenses/MIT
"""
This module is created to extend the module tempfile. It handles things better
than tempfile, automatically deleting previous processes files if they have
not been used for a long time.
It will also be eventually extended to a new type of data, harddata. This data
type will automatically store it's variable if it hasn't been used in a while.
It uses the threading module after the first call of get_temp_file. If your
applicationc cannot support threading, then:
Define your own THREAD_LOCK object to handle locking, make sure to set
the global variable to the object you are using.
set THREAD_HANDLED = True
call create_temp_directory
call THREAD_manage_harddata about every .5 seconds
"""
import sys, os
import pdb
import dbe
import cPickle
import time
import shutil
import re
import tempfile
import errors
import system
import textools
THREAD_HANDLED = False
THREAD_LOCK = None
THREAD_PERIOD = 30 # How often the therad runs in seconds
DELETE_TMP_AFTER = 60*60 # deletes unupdated temporary files if their
# timer file is not updated in an hour
#ga = harddata_base.__getattribute__
#sa = harddata_base.__setattr__
ga = object.__getattribute__
sa = object.__setattr__
# DO NOT MODIFY THESE
TIMER_FILE = 'pytimer.time' # DO NOT CHANGE
TEMP_DIRECTORY = None
_HARDDATA = [] # not currently used
STR_TEMP_PREFIX = 'pyhdd08234'
STR_TEMP_SUFIX = '.hd'
tmp_regexp = r'^{0}(.*?){1}$'.format(textools.convert_to_regexp(STR_TEMP_PREFIX),
textools.convert_to_regexp(STR_TEMP_SUFIX))
tmp_regexp = re.compile(tmp_regexp)
def get_temp_file():
if not TEMP_DIRECTORY:
create_temp_directory()
tempfile.mkstemp(suffix = STR_TEMP_SUFIX, prefix = STR_TEMP_PREFIX,
dir = TEMP_DIRECTORY)
def create_harddata_thread():
global THREAD_harddata
global THREAD_lock
global THREAD_HANDLED
assert(not THREAD_HANDLED)
from errors import ModuleError
try:
THREAD_harddata
raise ModuleError("Thread already started")
except NameError:
pass
from threading import Thread, Lock
class harddata_thread(Thread):
def __init__(self, harddata, lock):
print "intializing thread"
self.harddata = harddata
self.lock = lock
Thread.__init__(self)
def run(self):
while True:
print "running thread"
start_time = time.time()
THREAD_manage_harddata()
if time.time() - start_time > THREAD_PERIOD:
# the _check took longer than the thread period!
# TODO: change this to logging. Here for debug
assert(0)
else:
print 'thread sleeping'
time.sleep(self.last_run - start_time)
THREAD_lock = Lock()
THREAD_harddata = harddata_thread(_HARDDATA, THREAD_lock)
THREAD_HANDLED = True
THREAD_harddata.run()
def create_temp_directory():
global TEMP_DIRECTORY
TEMP_DIRECTORY = tempfile.mkdtemp(suffix = '.hd', prefix = STR_TEMP_PREFIX,
dir = tempfile.gettempdir())
THREAD_manage_harddata()
if not THREAD_HANDLED:
create_harddata_thread()
def THREAD_manage_harddata():
THREAD_LOCK.acquire()
for hd in _HARDDATA:
hd._check(time.time())
_manage_temp_dirs()
THREAD_LOCK.release()
def _manage_temp_dirs():
update_timer_file()
temp_folders = (tmpf for tmpf in os.listdir(tempfile.gettempdir())
if os.path.isdir(tmpf) and tmp_regexp.match(tmpf, len(tmpf)))
for tmpfold in temp_folders:
tpath = os.path.join(tmpfold)
if not check_timer(tpath):
shutil.rmtree(tpath)
# shutil.rmtree(tpath, onerror = errors.print_prev_exception)
def update_timer_file():
'''updates the file timer so that external python processes don't
delete the temp data'''
global TEMP_DIRECTORY
with open(os.path.join(TEMP_DIRECTORY, TIMER_FILE), 'w') as f:
f.write(time.ctime(time.time()))
def check_timer(folder_path):
'''returns whether the data should be kept (True) or deleted (False)'''
timer_path = os.path.join(TEMP_DIRECTORY, TIMER_FILE)
if time.time() - os.path.getatime(timer_path) > DELETE_TMP_AFTER:
return False
return True
if __name__ == '__main__':
pass