5 class SubstitutionError(Exception):
19 elif state == DEFAULT:
35 raise SubstitutionError("Substitution error, mismatched parentheses {}".format(c))
39 path = os.path.join(os.environ['TASK_KEEPMOUNT'], v)
41 if st and stat.S_ISREG(st.st_mode):
44 raise SubstitutionError("$(file {}) is not accessible or is not a regular file".format(path))
47 d = os.path.dirname(v)
50 path = os.path.join(os.environ['TASK_KEEPMOUNT'], d)
52 if st and stat.S_ISDIR(st.st_mode):
55 raise SubstitutionError("$(dir {}) is not accessible or is not a directory".format(path))
58 return os.path.splitext(os.path.basename(v))[0]
63 raise SubstitutionError("$(glob {}) no match fonud".format(v))
67 default_subs = {"file ": sub_file,
69 "basename ": sub_basename,
72 def do_substitution(p, c, subs=default_subs):
78 v = do_substitution(p, c[m[0]+2 : m[1]])
82 r = subs[sub](v[len(sub):])
89 raise SubstitutionError("Unknown variable or function '%s' while performing substitution on '%s'" % (v, c))
91 raise SubstitutionError("Substitution for '%s' is null while performing substitution on '%s'" % (v, c))
92 if not isinstance(r, basestring):
93 raise SubstitutionError("Substitution for '%s' must be a string while performing substitution on '%s'" % (v, c))
95 c = c[:m[0]] + r + c[m[1]+1:]