-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathramdiskFactory.py
More file actions
188 lines (141 loc) · 6.03 KB
/
ramdiskFactory.py
File metadata and controls
188 lines (141 loc) · 6.03 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
"""
Factory for creating ramdisks.
@note: may be of more use in the tests/testFramework, or example directories
@author: Roy Nielsen
"""
#--- Native python libraries
import re
from tempfile import mkdtemp
from subprocess import Popen, PIPE, STDOUT
#--- non-native python libraries in this source tree
from lib.loggers import CyLogger
from lib.loggers import LogPriority as lp
from lib.run_commands import RunWith
def BadRamdiskTypeException(Exception):
"""
Custom Exception
"""
def __init__(self,*args,**kwargs):
Exception.__init__(self,*args,**kwargs)
def OSNotValidForRamdiskHelper(Exception):
"""
Custom Exception
"""
def __init__(self, *args, **kwargs):
Exception.__init__(self, *args, **kwargs)
class RamDiskFactory(object):
"""
Retrieve and OS specific ramdisk, and provide an interface to manage it.
Keeps a reference to a list of ramdisks. When calling getRamdisk(new), if
"new" is true, the method will add the ramdisk to the list of ramdisks.
@parameter message_level: Level of logging a person wishes to log at.
see logMessage in the log_message module.
@method: getRamdisk: Will return either a new ramdisk, or make the
self.activeRamdisk the ramdisk with the name of the
passed in mountpoint (if found). Otherwise, the
self.activeRamdisk is initialized to None.
@method getModuleVersion: gets the version of this module.
@method unmountActiveRamdisk: Unmounts the active ramdisk.
@method unmountRamdisk: Unmounts the mountpoint that is passed in.
@author: Roy Nielsen
"""
def __init__(self, environ, logger=None):
"""
Identify OS and instantiate an instance of a ramdisk
"""
self.module_version = '20160224.203258.288119'
self.size = 0
self.environ = environ
self.mountpoint = None
self.ramdiskType = None
if not logger:
self.logger = CyLogger()
else:
self.logger = logger
self.activeRamdisk = None
self.ramdisks = []
self.validRamdiskTypes = ["loop", "tmpfs"]
self.validOSFamilies = ["macos", "linux"]
self.myosfamily = self.environ.getosfamily()
if not self.myosfamily in self.validOSFamilies:
raise OSNotValidForRamdiskHelper("Needs to be MacOS or Linux...")
############################################################################
def getRamdisk(self, size=0, mountpoint="", ramdiskType=""):
"""
Getter for the ramdisk instance.
@var: ramdisks - a list of ramdisks this factory has created
@param: size - size of the ramdisk to create. If zero, it looks for
@author: Roy Nielsen
"""
if not ramdiskType in self.validRamdiskTypes:
raise BadRamdiskTypeException("Not a valid ramdisk type")
if size and mountpoint and ramdiskType:
#####
# Determine OS and ramdisk type, create ramdisk accordingly
if self.myosfamily == "darwin":
#####
# Found MacOS
from macRamdisk import RamDisk
self.activeRamdisk = RamDisk(size, mountpoint, self.logger)
elif self.myosfamily == "linux" and ramdiskType == "loop":
#####
# Found Linux with a loopback ramdisk request
from linuxLoopRamdisk import RamDisk
self.activeRamdisk = RamDisk(mountpoint, self.logger)
elif self.myosfamily == "linux" and ramdiskType == "tmpfs":
#####
# Found Linux with a tmpfs ramdisk request.
from linuxTmpfsRamdisk import RamDisk
self.activeRamdisk = RamDisk(size, mountpoint, self.logger)
else:
#####
# Bad method input parameters...
self.activeRamdisk = None
#####
# Append the newly assigned self.activeRamdisk to the self.ramdisks
# list
self.ramdisks.append(self.activeRamdisk)
elif not size and mountpoint:
#####
# Look for the ramdisk with "mountpoint" and return that instance.
for ramdisk in self.ramdisks:
if re.match("^%s$"%mountpoint, ramdisk.getMountPoint()):
self.activeRamdisk = ramdisk
break
return self.activeRamdisk
############################################################################
def getModuleVersion(self):
"""
Getter for the version of this module.
@author: Roy Nielsen
"""
return self.module_version
############################################################################
def unmountActiveRamdisk(self):
"""
Eject the currently active ramdisk in the Factory.
@return: success - successful = True, unsuccessful = False
@author: Roy Nielsen
"""
success = False
success = self.activeRamdisk.unmount(self.logger)
return success
############################################################################
def unmountRamdisk(self, mountpoint=""):
"""
Eject the ramdisk in the list with the passed in mountpoint.
@param mountpoint: the mountpoint to eject.
@return: True if successful, False if not successful
@author: Roy Nielsen
"""
success = False
if mountpoint:
for ramdisk in self.ramdisks:
if re.match("^%s$"%mountpoint, ramdisk.getMountPoint()):
self.activeRamdisk = ramdisk
success = self.unmountActiveRamdisk()
break
return success
############################################################################
############################################################################
############################################################################