-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommonRamdiskTemplate.py
More file actions
241 lines (180 loc) · 6.96 KB
/
commonRamdiskTemplate.py
File metadata and controls
241 lines (180 loc) · 6.96 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
"""
Template for the ramdisk classes
@author: Roy Nielsen
"""
#--- Native python libraries
from tempfile import mkdtemp
#--- non-native python libraries in this source tree
from lib.loggers import LogPriority as lp
from lib.loggers import CyLogger
###########################################################################
class NotValidForThisOS(Exception):
"""
Meant for being thrown when an action/class being run/instanciated is not
applicable for the running operating system.
@author: Roy Nielsen
"""
def __init__(self, *args, **kwargs):
Exception.__init__(self, *args, **kwargs)
###########################################################################
class BadRamdiskArguments(Exception):
"""
Meant for being thrown when an invalid values are passed in as arguments
to class instanciation or class methods.
@author: Roy Nielsen
"""
def __init__(self, *args, **kwargs):
Exception.__init__(self, *args, **kwargs)
###############################################################################
class RamDiskTemplate(object):
"""
"""
def __init__(self, size=0, mountpoint=False, logger=False):
"""
"""
#####
# Version/timestamp is
# <YYYY><MM><DD>.<HH><MM><SS>.<microseconds>
# in UTC time
self.module_version = '20160224.032043.009191'
if not isinstance(logger, CyLogger):
self.logger = CyLogger()
else:
self.logger = logger
self.logger.log(lp.INFO, "Logger: " + str(self.logger))
self.diskSize = size
self.success = False
self.myRamdiskDev = None
if not mountpoint:
self.getRandomizedMountpoint()
else:
self.mntPoint = mountpoint
self.logger.log(lp.DEBUG, "disk size: " + str(self.diskSize))
self.logger.log(lp.DEBUG, "volume name: " + str(self.mntPoint))
###########################################################################
def getData(self):
"""
Getter for mount data, and if the mounting of a ramdisk was successful
Does not print or log the data.
@author: Roy Nielsen
"""
return (self.success, str(self.mntPoint), str(self.myRamdiskDev))
###########################################################################
def getNlogData(self):
"""
Getter for mount data, and if the mounting of a ramdisk was successful
Also logs the data.
@author: Roy Nielsen
"""
self.logger.log(lp.INFO, "Success: " + str(self.success))
self.logger.log(lp.INFO, "Mount point: " + str(self.mntPoint))
self.logger.log(lp.INFO, "Device: " + str(self.myRamdiskDev))
return (self.success, str(self.mntPoint), str(self.myRamdiskDev))
###########################################################################
def getNprintData(self):
"""
Getter for mount data, and if the mounting of a ramdisk was successful
"""
print "Success: " + str(self.success)
print "Mount point: " + str(self.mntPoint)
print "Device: " + str(self.myRamdiskDev)
return (self.success, str(self.mntPoint), str(self.myRamdiskDev))
###########################################################################
def getRandomizedMountpoint(self):
"""
Create a randomized (secure) mount point - per python's implementation
of mkdtemp - a way to make an unguessable directory on the system
@author: Roy Nielsen
"""
success = False
self.mntPoint = ""
try :
self.mntPoint = mkdtemp()
except Exception, err :
self.logger.log(lp.WARNING, "Exception trying to create temporary directory")
raise err
else :
success = True
self.logger.log(lp.WARNING,
"Success: " + str(success) +
" in get_randomizedMountpoint: " +
str(self.mntPoint))
self.logger.log(lp.WARNING, "Randomized mount point: \"" + str(self.mntPoint) + "\"")
return success
###########################################################################
def umount(self):
"""
Unmount the disk - same functionality as __eject on the mac
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
success = False
return success
###########################################################################
def unmount(self):
"""
Unmount the disk - same functionality as __eject on the mac
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
success = False
return success
###########################################################################
def __isMemoryAvailable(self):
"""
Check to make sure there is plenty of memory of the size passed in
before creating the ramdisk
@author: Roy Nielsen
"""
success = False
return success
###########################################################################
def _format(self):
"""
Format the ramdisk
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
success = False
return success
###########################################################################
def getDevice(self):
"""
Getter for the device name the ramdisk is using
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
return self.myRamdiskDev
###########################################################################
def getMountPoint(self):
"""
Getter for the mount point name the ramdisk is using
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
return self.mntPoint
###########################################################################
def setDevice(self, device=None):
"""
Setter for the device so it can be ejected.
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
self.myRamdiskDev = device
###########################################################################
def getVersion(self):
"""
Getter for the version of the ramdisk
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
return self.module_version
###############################################################################
def detach(device=None):
"""
Eject the ramdisk
Must be over-ridden to provide OS/Method specific functionality
@author: Roy Nielsen
"""
success = False
return success