4831: Set initial values for filters.
[arvados.git] / crunch_scripts / crunchutil / subst.py
index 13c6aa0151e74ced8c58290b8174c16c7fd23889..06ef6c1198ffad07d888040794a42dce08e8af16 100644 (file)
@@ -1,9 +1,9 @@
 import os
 import glob
+import stat
 
 class SubstitutionError(Exception):
-    def __init__(self, message):
-        super(SubstitutionError, self).__init__(message)
+    pass
 
 def search(c):
     DEFAULT = 0
@@ -36,13 +36,23 @@ def search(c):
     return None
 
 def sub_file(v):
-    return os.path.join(os.environ['TASK_KEEPMOUNT'], v)
+    path = os.path.join(os.environ['TASK_KEEPMOUNT'], v)
+    st = os.stat(path)
+    if st and stat.S_ISREG(st.st_mode):
+        return path
+    else:
+        raise SubstitutionError("$(file {}) is not accessible or is not a regular file".format(path))
 
 def sub_dir(v):
     d = os.path.dirname(v)
     if d == '':
         d = v
-    return os.path.join(os.environ['TASK_KEEPMOUNT'], d)
+    path = os.path.join(os.environ['TASK_KEEPMOUNT'], d)
+    st = os.stat(path)
+    if st and stat.S_ISDIR(st.st_mode):
+        return path
+    else:
+        raise SubstitutionError("$(dir {}) is not accessible or is not a directory".format(path))
 
 def sub_basename(v):
     return os.path.splitext(os.path.basename(v))[0]
@@ -50,7 +60,7 @@ def sub_basename(v):
 def sub_glob(v):
     l = glob.glob(v)
     if len(l) == 0:
-        raise SubstitutionError("$(glob): No match on '%s'" % v)
+        raise SubstitutionError("$(glob {}) no match fonud".format(v))
     else:
         return l[0]
 
@@ -79,7 +89,7 @@ def do_substitution(p, c, subs=default_subs):
                 raise SubstitutionError("Unknown variable or function '%s' while performing substitution on '%s'" % (v, c))
             if r is None:
                 raise SubstitutionError("Substitution for '%s' is null while performing substitution on '%s'" % (v, c))
-            if not (isinstance(r, str) or isinstance(r, unicode)):
+            if not isinstance(r, basestring):
                 raise SubstitutionError("Substitution for '%s' must be a string while performing substitution on '%s'" % (v, c))
 
         c = c[:m[0]] + r + c[m[1]+1:]