def __new__(self, file, *args, **kwargs): import mem path = os.path.join(Mem.instance().cwd, file) if Mem.instance().failed: sys.exit(1) if not os.path.exists(path): raise NodeError("%s does not exist!" % path) return str.__new__(self, path)
def make_depends(prefix, source, args): (returncode, stdoutdata, stderrdata) = run_return_output_no_print(prefix, source, _open_pipe_, args) deps = stdoutdata.split() sys.stdout.write(_mark_output_(stderrdata)) if returncode != 0: Mem.instance().fail() deps = deps[1:] # first element is the target (eg ".c"), drop it return [dep for dep in deps if dep != "\\"]
def f(*args, **kwargs): mem = Mem.instance() if mem is None: raise RuntimeError("Mem Singleton has not yet been created") tchash = mem.get_hash(taskf.__name__, taskf.__module__, args, kwargs) try: f = open(mem._deps_path(tchash), "rb") deps = pickle.load(f) f.close() f = open(mem._results_path(mem.get_hash(tchash, deps)), "rb") result = pickle.load(f) f.close() def restore(o): if (hasattr(o, "restore")): o.restore() elif (hasattr(o, "__iter__")): for el in o: restore(el) restore(result) mem.deps_stack().add_deps_if_in_memoize(deps) return result except IOError: return mem._run_task(taskf, args, kwargs, tchash)
def join(self): mem = Mem.instance() if mem.failed: sys.exit(1) Thread.join(self) if mem.failed: sys.exit(1)
def run_return_output(prefix, source, fun, *args): """ run commands specified in args (a sequence or string), optionally in a shell, and returns a tuple (resultcode, stdoutdata, stderrdata), where stdoutdata and stderrdata are strings. As a side effect, it also dump both stdoutdata and stderrdata to sys.stdout. prefix will be truncated and right-justified to 25 characters source """ (returncode, stdoutdata, stderrdata) = run_return_output_no_print(prefix, source, fun, *args) sys.stdout.write(_mark_output_(stdoutdata)) sys.stdout.write(_mark_output_(stderrdata)) if returncode != 0: Mem.instance().fail() return (returncode, stdoutdata, stderrdata)
def run(self): mem = Mem.instance() if not mem.thread_limit == None: mem.thread_limit.acquire() if mem.failed: sys.exit(1) self.result = self.f(*(self.args), **(self.kwargs)) if not mem.thread_limit == None: mem.thread_limit.release()
def get_build_dir(env, arg_func): """ return a valid build directory given the environment """ mem = Mem.instance() if arg_func and type(arg_func) == str: return arg_func elif arg_func: return arg_func(env) if not env: return mem.cwd try: if env.BUILD_DIR_FUNC: return env.BUILD_DIR_FUNC(env) except AttributeError: pass if "BUILD_DIR" not in env or not env.BUILD_DIR: return mem.cwd root = mem.root src_dir = mem.cwd if not src_dir.startswith(root): mem.fail("source dir (%s) is not a subdir of root (%s) " "unable to generate a build path" % src_dir, root) sub_dir = src_dir[len(root) + 1:] dir = os.path.join(root, env.BUILD_DIR, sub_dir) try: os.makedirs(dir) except OSError: pass return dir
def _store_path(self): h = self._hash return os.path.join(Mem.instance().blob_dir, h[:2], h[2:])
def subdir(*args, **kwargs): return Mem.instance().subdir(*args, **kwargs)
def inst(): return Mem.instance()
def fail(*args, **kwargs): Mem.instance().fail(*args, **kwargs)
def add_deps(ds): Mem.instance().add_deps(ds)
def add_dep(d): Mem.instance().add_dep(d)