+def sub_outdir(v):
+ return outdir
+
+def sub_cores(v):
+ return str(multiprocessing.cpu_count())
+
+def sub_jobid(v):
+ return os.environ['JOB_UUID']
+
+def sub_taskid(v):
+ return os.environ['TASK_UUID']
+
+def sub_jobsrc(v):
+ return os.environ['CRUNCH_SRC']
+
+subst.default_subs["task.tmpdir"] = sub_tmpdir
+subst.default_subs["task.outdir"] = sub_outdir
+subst.default_subs["job.srcdir"] = sub_jobsrc
+subst.default_subs["node.cores"] = sub_cores
+subst.default_subs["job.uuid"] = sub_jobid
+subst.default_subs["task.uuid"] = sub_taskid
+
+class SigHandler(object):
+ def __init__(self):
+ self.sig = None
+
+ def send_signal(self, sp, signum):
+ sp.send_signal(signum)
+ self.sig = signum
+
+def add_to_group(gr, match):
+ m = match.groups()
+ if m not in gr:
+ gr[m] = []
+ gr[m].append(match.group(0))
+
+def expand_item(p, c):
+ if isinstance(c, dict):
+ if "foreach" in c and "command" in c:
+ var = c["foreach"]
+ items = get_items(p, p[var])
+ r = []
+ for i in items:
+ params = copy.copy(p)
+ params[var] = i
+ r.extend(expand_list(params, c["command"]))
+ return r
+ if "list" in c and "index" in c and "command" in c:
+ var = c["list"]
+ items = get_items(p, p[var])
+ params = copy.copy(p)
+ params[var] = items[int(c["index"])]
+ return expand_list(params, c["command"])
+ if "regex" in c:
+ pattern = re.compile(c["regex"])
+ if "filter" in c:
+ items = get_items(p, p[c["filter"]])
+ return [i for i in items if pattern.match(i)]
+ elif "group" in c:
+ items = get_items(p, p[c["group"]])
+ groups = {}
+ for i in items:
+ match = pattern.match(i)
+ if match:
+ add_to_group(groups, match)
+ return [groups[k] for k in groups]
+ elif "extract" in c:
+ items = get_items(p, p[c["extract"]])
+ r = []
+ for i in items:
+ match = pattern.match(i)
+ if match:
+ r.append(list(match.groups()))
+ return r
+ elif isinstance(c, list):
+ return expand_list(p, c)
+ elif isinstance(c, basestring):
+ return [subst.do_substitution(p, c)]
+
+ return []
+
+def expand_list(p, l):
+ if isinstance(l, basestring):
+ return expand_item(p, l)
+ else:
+ return [exp for arg in l for exp in expand_item(p, arg)]
+
+def get_items(p, value):
+ if isinstance(value, dict):
+ return expand_item(p, value)
+
+ if isinstance(value, list):
+ return expand_list(p, value)