6 BACKSLASH_ESCAPE_RE = re.compile(r'\\(.)')
8 class SubstitutionError(Exception):
22 elif state == DEFAULT:
38 raise SubstitutionError("Substitution error, mismatched parentheses {}".format(c))
42 path = os.path.join(os.environ['TASK_KEEPMOUNT'], v)
44 if st and stat.S_ISREG(st.st_mode):
47 raise SubstitutionError("$(file {}) is not accessible or is not a regular file".format(path))
50 d = os.path.dirname(v)
53 path = os.path.join(os.environ['TASK_KEEPMOUNT'], d)
55 if st and stat.S_ISDIR(st.st_mode):
58 raise SubstitutionError("$(dir {}) is not accessible or is not a directory".format(path))
61 return os.path.splitext(os.path.basename(v))[0]
66 raise SubstitutionError("$(glob {}) no match fonud".format(v))
70 default_subs = {"file ": sub_file,
72 "basename ": sub_basename,
75 def do_substitution(p, c, subs=default_subs):
79 return BACKSLASH_ESCAPE_RE.sub(r'\1', c)
81 v = do_substitution(p, c[m[0]+2 : m[1]])
85 r = subs[sub](v[len(sub):])
92 raise SubstitutionError("Unknown variable or function '%s' while performing substitution on '%s'" % (v, c))
94 raise SubstitutionError("Substitution for '%s' is null while performing substitution on '%s'" % (v, c))
95 if not isinstance(r, basestring):
96 raise SubstitutionError("Substitution for '%s' must be a string while performing substitution on '%s'" % (v, c))
98 c = c[:m[0]] + r + c[m[1]+1:]