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(cond, name, function):
26 if (cond and s.exists(name)) or \
27 (cond and config.debug and s.exists("debug.%s" % name)):
32 s.dump(name, o) # cache the object using 'name'
39 def if_cached_else(self, cond, name, function):
40 if cond and self.exists(name):
45 self.dump(name, o) # cache the object using 'name'
48 def __file(self, name):
49 return "%s/%s.pkl" % (PICKLE_PATH, name)
51 def exists(self, name):
52 return os.path.exists(self.__file(name))
56 In debug mode, we should fail if neither file exists.
57 if the debug file exists, reset name
58 elif the original file exists, make a copy, reset name
59 else neither exist, raise an error
60 Otherwise, it's normal mode, if the file doesn't exist, raise error
65 if self.exists("debug.%s" % name):
66 name = "debug.%s" % name
67 elif self.exists(name):
68 debugname = "debug.%s" % name
69 if not self.exists(debugname):
70 shutil.copyfile(self.__file(name), self.__file(debugname))
73 raise Exception, "No such pickle based on %s" % self.__file(name)
75 if not self.exists(name):
76 raise Exception, "No such file %s" % name
78 print "loading %s" % self.__file(name)
79 f = open(self.__file(name), 'r')
85 # use the environment to extract the data associated with the local
87 def dump(self, name, obj=None, depth=1):
89 o = inspect.getouterframes(inspect.currentframe())
90 up1 = o[depth][0] # get the frame one prior to (up from) this frame
91 argvals = inspect.getargvalues(up1)
92 # TODO: check that 'name' is a local variable; otherwise this would fail.
93 obj = argvals[3][name] # extract the local variable name 'name'
94 if not os.path.isdir("%s/" % PICKLE_PATH):
95 os.mkdir("%s" % PICKLE_PATH)
97 name = "debug.%s" % name
98 f = open(self.__file(name), 'w')
104 ssh_options = { 'StrictHostKeyChecking':'no',
106 'PasswordAuthentication':'no',
107 'ConnectTimeout':'20'}
110 def __init__(self, user, host, options = ssh_options):
111 self.options = options
116 def __options_to_str(self):
118 for o,v in self.options.iteritems():
119 options = options + "-o %s=%s " % (o,v)
123 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
124 self.user, self.host, cmd)
127 (f_in, f_out, f_err) = os.popen3(cmd)
130 raise Exception, f_err.read()
132 print " == %s" % value
139 cmd = "ssh %s %s@%s '%s'" % (self.__options_to_str(),
140 self.user, self.host, cmd)
143 (f_in, f_out, f_err) = os.popen3(cmd)
146 if value == "": # An error has occured
150 print " == %s" % value
159 self.start = time.time()
162 self.end = time.time()
163 t = self.end-self.start
167 self.end = time.time()
168 t = self.end-self.start
169 self.start = self.end