11308: Merge branch 'master' into 11308-python3
[arvados.git] / crunch_scripts / crunchutil / subst.py
index b7336fa3322775c3b2b22f0bbf1905a48ab5de28..bd99d3c71cafc76392be14ec0b0b38973a7c11fc 100644 (file)
@@ -1,7 +1,10 @@
-import os
 import glob
+import os
+import re
 import stat
 
+BACKSLASH_ESCAPE_RE = re.compile(r'\\(.)')
+
 class SubstitutionError(Exception):
     pass
 
@@ -41,7 +44,7 @@ def sub_file(v):
     if st and stat.S_ISREG(st.st_mode):
         return path
     else:
-        raise SubstitutionError("$(file {}) is not accessable or is not a regular file".format(path))
+        raise SubstitutionError("$(file {}) is not accessible or is not a regular file".format(path))
 
 def sub_dir(v):
     d = os.path.dirname(v)
@@ -52,7 +55,7 @@ def sub_dir(v):
     if st and stat.S_ISDIR(st.st_mode):
         return path
     else:
-        raise SubstitutionError("$(dir {}) is not accessable or is not a directory".format(path))
+        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]
@@ -60,7 +63,7 @@ def sub_basename(v):
 def sub_glob(v):
     l = glob.glob(v)
     if len(l) == 0:
-        raise SubstitutionError("$(glob {}) no match fonud".format(v))
+        raise SubstitutionError("$(glob {}) no match found".format(v))
     else:
         return l[0]
 
@@ -73,7 +76,7 @@ def do_substitution(p, c, subs=default_subs):
     while True:
         m = search(c)
         if m is None:
-            return c
+            return BACKSLASH_ESCAPE_RE.sub(r'\1', c)
 
         v = do_substitution(p, c[m[0]+2 : m[1]])
         var = True