6 from PHPSerialize import *
7 from PHPUnserialize import *
9 print >>sys.stderr, "PHPSerial db type not allowed."
14 from config import config
20 def dbLoad(name, type=None):
21 return SPickle().load(name, type)
23 def dbExists(name, type=None):
24 #if self.config.debug:
25 # name = "debug.%s" % name
26 return SPickle().exists(name, type)
28 def dbDump(name, obj=None, type=None):
29 # depth of the dump is 2 now, since we're redirecting to '.dump'
30 return SPickle().dump(name, obj, type, 2)
32 def if_cached_else_refresh(cond, refresh, name, function, type=None):
35 if not config.debug and s.exists("production.%s" % name, type):
36 s.remove("production.%s" % name, type)
37 if config.debug and s.exists("debug.%s" % name, type):
38 s.remove("debug.%s" % name, type)
40 return if_cached_else(cond, name, function, type)
42 def if_cached_else(cond, name, function, type=None):
44 if (cond and s.exists("production.%s" % name, type)) or \
45 (cond and config.debug and s.exists("debug.%s" % name, type)):
46 o = s.load(name, type)
50 s.dump(name, o, type) # cache the object using 'name'
51 o = s.load(name, type)
52 # TODO: what if 'o' hasn't been converted...
59 def if_cached_else(self, cond, name, function, type=None):
60 if cond and self.exists("production.%s" % name, type):
61 o = self.load(name, type)
65 self.dump(name, o, type) # cache the object using 'name'
68 def __file(self, name, type=None):
70 return "%s/%s.pkl" % (PICKLE_PATH, name)
73 raise Exception("No PHPSerializer module available")
75 return "%s/%s.phpserial" % (PICKLE_PATH, name)
77 def exists(self, name, type=None):
78 return os.path.exists(self.__file(name, type))
80 def remove(self, name, type=None):
81 return os.remove(self.__file(name, type))
83 def load(self, name, type=None):
85 In debug mode, we should fail if neither file exists.
86 if the debug file exists, reset name
87 elif the original file exists, make a copy, reset name
88 else neither exist, raise an error
89 Otherwise, it's normal mode, if the file doesn't exist, raise error
94 if self.exists("debug.%s" % name, type):
95 name = "debug.%s" % name
96 elif self.exists("production.%s" % name, type):
97 debugname = "debug.%s" % name
98 if not self.exists(debugname, type):
99 name = "production.%s" % name
100 shutil.copyfile(self.__file(name, type), self.__file(debugname, type))
102 else: # neither exist
103 raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
105 if not self.exists("production.%s" % name, type):
106 raise Exception, "No such file %s" % name
107 name = "production.%s" % name
109 print "loading %s" % self.__file(name, type)
110 f = open(self.__file(name, type), 'r')
115 raise Exception("No PHPSerializer module available")
117 o = s.unserialize(f.read())
122 # use the environment to extract the data associated with the local
124 def dump(self, name, obj=None, type=None, depth=1):
126 o = inspect.getouterframes(inspect.currentframe())
127 up1 = o[depth][0] # get the frame one prior to (up from) this frame
128 argvals = inspect.getargvalues(up1)
129 # TODO: check that 'name' is a local variable; otherwise this would fail.
130 obj = argvals[3][name] # extract the local variable name 'name'
131 if not os.path.isdir("%s/" % PICKLE_PATH):
132 os.mkdir("%s" % PICKLE_PATH)
134 name = "debug.%s" % name
136 name = "production.%s" % name
137 f = open(self.__file(name, type), 'w')
142 raise Exception("No PHPSerializer module available")
144 f.write(s.serialize(obj))
150 ssh_options = { 'StrictHostKeyChecking':'no',
152 'PasswordAuthentication':'no',
153 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
154 from select import select
159 def run_noexcept(self, cmd):
161 (f_in, f_out, f_err) = os.popen3(cmd)
162 lout, lin, lerr = select([f_out,f_err], [], [], COMMAND_TIMEOUT*2)
163 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
165 print "TODO: kill subprocess: '%s'" % cmd
166 # TODO: kill subprocess??
167 return ("", "SCRIPTTIMEOUT")
168 o_value = f_out.read()
170 if o_value == "": # An error has occured
171 e_value = f_err.read()
173 o_value = o_value.strip()
174 e_value = e_value.strip()
179 return (o_value, e_value)
183 (f_in, f_out, f_err) = os.popen3(cmd)
186 raise Exception, f_err.read()
187 value = value.strip()
197 def __init__(self, user, host, options = ssh_options):
198 self.options = options
203 def __options_to_str(self):
205 for o,v in self.options.iteritems():
206 options = options + "-o %s=%s " % (o,v)
210 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
211 self.user, self.host, cmd)
212 return CMD.run(self, cmd)
214 def get_file(self, rmt_filename, local_filename=None):
215 if local_filename == None:
216 local_filename = "./"
217 cmd = "scp -B %s %s@%s:%s %s" % (self.__options_to_str(),
218 self.user, self.host,
219 rmt_filename, local_filename)
221 # errors will be on stderr,
222 # success will have a blank stderr...
223 return CMD.run_noexcept(self, cmd)
225 def run_noexcept(self, cmd):
226 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
227 self.user, self.host, cmd)
228 return CMD.run_noexcept(self, cmd)
231 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
232 self.user, self.host, cmd)
235 (f_in, f_out, f_err) = os.popen3(cmd)
238 if value == "": # An error has occured
240 value = value.strip()
243 print " == %s" % value
252 self.start = time.time()
255 self.end = time.time()
256 t = self.end-self.start
260 self.end = time.time()
261 t = self.end-self.start
262 self.start = self.end