6 from config import config
13 return SPickle().load(name)
16 #if self.config.debug:
17 # name = "debug.%s" % name
18 return SPickle().exists(name)
20 def dbDump(name, obj=None):
21 # depth of the dump is 2 now, since we're redirecting to '.dump'
22 return SPickle().dump(name, obj, 2)
24 def if_cached_else_refresh(cond, refresh, name, function):
27 if not config.debug and s.exists("production.%s" % name):
28 s.remove("production.%s" % name)
29 if config.debug and s.exists("debug.%s" % name):
30 s.remove("debug.%s" % name)
32 return if_cached_else(cond, name, function)
34 def if_cached_else(cond, name, function):
36 if (cond and s.exists("production.%s" % name)) or \
37 (cond and config.debug and s.exists("debug.%s" % name)):
42 s.dump(name, o) # cache the object using 'name'
49 def if_cached_else(self, cond, name, function):
50 if cond and self.exists("production.%s" % name):
55 self.dump(name, o) # cache the object using 'name'
58 def __file(self, name):
59 return "%s/%s.pkl" % (PICKLE_PATH, name)
61 def exists(self, name):
62 return os.path.exists(self.__file(name))
64 def remove(self, name):
65 return os.remove(self.__file(name))
69 In debug mode, we should fail if neither file exists.
70 if the debug file exists, reset name
71 elif the original file exists, make a copy, reset name
72 else neither exist, raise an error
73 Otherwise, it's normal mode, if the file doesn't exist, raise error
78 if self.exists("debug.%s" % name):
79 name = "debug.%s" % name
80 elif self.exists("production.%s" % name):
81 debugname = "debug.%s" % name
82 if not self.exists(debugname):
83 name = "production.%s" % name
84 shutil.copyfile(self.__file(name), self.__file(debugname))
87 raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name)
89 if not self.exists("production.%s" % name):
90 raise Exception, "No such file %s" % name
91 name = "production.%s" % name
93 print "loading %s" % self.__file(name)
94 f = open(self.__file(name), 'r')
100 # use the environment to extract the data associated with the local
102 def dump(self, name, obj=None, depth=1):
104 o = inspect.getouterframes(inspect.currentframe())
105 up1 = o[depth][0] # get the frame one prior to (up from) this frame
106 argvals = inspect.getargvalues(up1)
107 # TODO: check that 'name' is a local variable; otherwise this would fail.
108 obj = argvals[3][name] # extract the local variable name 'name'
109 if not os.path.isdir("%s/" % PICKLE_PATH):
110 os.mkdir("%s" % PICKLE_PATH)
112 name = "debug.%s" % name
114 name = "production.%s" % name
115 f = open(self.__file(name), 'w')
122 ssh_options = { 'StrictHostKeyChecking':'no',
124 'PasswordAuthentication':'no',
125 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
126 from select import select
131 def run_noexcept(self, cmd):
133 (f_in, f_out, f_err) = os.popen3(cmd)
134 lout, lin, lerr = select([f_out,f_err], [], [], COMMAND_TIMEOUT*2)
135 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
137 print "TODO: kill subprocess: '%s'" % cmd
138 # TODO: kill subprocess??
139 return ("", "SCRIPTTIMEOUT")
140 o_value = f_out.read()
142 if o_value == "": # An error has occured
143 e_value = f_err.read()
145 o_value = o_value.strip()
146 e_value = e_value.strip()
151 return (o_value, e_value)
155 (f_in, f_out, f_err) = os.popen3(cmd)
158 raise Exception, f_err.read()
159 value = value.strip()
169 def __init__(self, user, host, options = ssh_options):
170 self.options = options
175 def __options_to_str(self):
177 for o,v in self.options.iteritems():
178 options = options + "-o %s=%s " % (o,v)
182 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
183 self.user, self.host, cmd)
184 return CMD.run(self, cmd)
186 def get_file(self, rmt_filename, local_filename=None):
187 if local_filename == None:
188 local_filename = "./"
189 cmd = "scp -B %s %s@%s:%s %s" % (self.__options_to_str(),
190 self.user, self.host,
191 rmt_filename, local_filename)
193 # errors will be on stderr,
194 # success will have a blank stderr...
195 return CMD.run_noexcept(self, cmd)
197 def run_noexcept(self, cmd):
198 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
199 self.user, self.host, cmd)
200 return CMD.run_noexcept(self, cmd)
203 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
204 self.user, self.host, cmd)
207 (f_in, f_out, f_err) = os.popen3(cmd)
210 if value == "": # An error has occured
212 value = value.strip()
215 print " == %s" % value
224 self.start = time.time()
227 self.end = time.time()
228 t = self.end-self.start
232 self.end = time.time()
233 t = self.end-self.start
234 self.start = self.end