[ARVADOS] created: e32be386f2378807a79da5810414982769aa046c

git at public.curoverse.com git at public.curoverse.com
Thu Aug 28 09:40:17 EDT 2014


        at  e32be386f2378807a79da5810414982769aa046c (commit)


commit e32be386f2378807a79da5810414982769aa046c
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Thu Aug 28 09:25:52 2014 -0400

    3719: Raise more specific SubstitutionError instead of generic exception on
    user syntax errors.  Don't print stack trace on SubstitutionError.

diff --git a/crunch_scripts/crunchutil/subst.py b/crunch_scripts/crunchutil/subst.py
index b352688..13c6aa0 100644
--- a/crunch_scripts/crunchutil/subst.py
+++ b/crunch_scripts/crunchutil/subst.py
@@ -1,6 +1,10 @@
 import os
 import glob
 
+class SubstitutionError(Exception):
+    def __init__(self, message):
+        super(SubstitutionError, self).__init__(message)
+
 def search(c):
     DEFAULT = 0
     DOLLAR = 1
@@ -28,7 +32,7 @@ def search(c):
             state = DEFAULT
         i += 1
     if depth != 0:
-        raise Exception("Substitution error, mismatched parentheses {}".format(c))
+        raise SubstitutionError("Substitution error, mismatched parentheses {}".format(c))
     return None
 
 def sub_file(v):
@@ -46,7 +50,7 @@ def sub_basename(v):
 def sub_glob(v):
     l = glob.glob(v)
     if len(l) == 0:
-        raise Exception("$(glob): No match on '%s'" % v)
+        raise SubstitutionError("$(glob): No match on '%s'" % v)
     else:
         return l[0]
 
@@ -57,19 +61,25 @@ default_subs = {"file ": sub_file,
 
 def do_substitution(p, c, subs=default_subs):
     while True:
-        #print("c is", c)
         m = search(c)
-        if m is not None:
-            v = do_substitution(p, c[m[0]+2 : m[1]])
-            var = True
-            for sub in subs:
-                if v.startswith(sub):
-                    r = subs[sub](v[len(sub):])
-                    var = False
-                    break
-            if var:
+        if m is None:
+            return c
+
+        v = do_substitution(p, c[m[0]+2 : m[1]])
+        var = True
+        for sub in subs:
+            if v.startswith(sub):
+                r = subs[sub](v[len(sub):])
+                var = False
+                break
+        if var:
+            if v in p:
                 r = p[v]
+            else:
+                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)):
+                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:]
-        else:
-            return c
+        c = c[:m[0]] + r + c[m[1]+1:]
diff --git a/crunch_scripts/run-command b/crunch_scripts/run-command
index c624e3c..916e6b2 100755
--- a/crunch_scripts/run-command
+++ b/crunch_scripts/run-command
@@ -166,7 +166,11 @@ try:
         stdoutfile = open(stdoutname, "wb")
 
     logging.info("{}{}{}".format(' '.join(cmd), (" < " + stdinname) if stdinname is not None else "", (" > " + stdoutname) if stdoutname is not None else ""))
-
+except subst.SubstitutionError as e:
+    logging.error(str(e))
+    logging.error("task parameters was:")
+    logging.error(pprint.pformat(taskp))
+    sys.exit(1)
 except Exception as e:
     logging.exception("caught exception")
     logging.error("task parameters was:")

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list