incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject [2/2] git commit: [#5703] Redirect stdout/stderr during task
Date Mon, 11 Feb 2013 19:13:36 GMT
Updated Branches:
  refs/heads/tv/5703 [created] 0e28be71e


[#5703] Redirect stdout/stderr during task


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/0e28be71
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/0e28be71
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/0e28be71

Branch: refs/heads/tv/5703
Commit: 0e28be71e12cbddfd8d25d358d7b5658343d9566
Parents: 3dc36a3
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Mon Feb 11 19:13:10 2013 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Mon Feb 11 19:13:10 2013 +0000

----------------------------------------------------------------------
 Allura/allura/scripts/scripttask.py |   29 +++++++++++++++++++++++++----
 1 files changed, 25 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/0e28be71/Allura/allura/scripts/scripttask.py
----------------------------------------------------------------------
diff --git a/Allura/allura/scripts/scripttask.py b/Allura/allura/scripts/scripttask.py
index ce36d1a..91a3a2f 100644
--- a/Allura/allura/scripts/scripttask.py
+++ b/Allura/allura/scripts/scripttask.py
@@ -2,6 +2,7 @@ import argparse
 import copy_reg
 import logging
 import pickle
+import sys
 import types
 
 from allura.lib.decorators import task
@@ -21,11 +22,31 @@ def dispatcher(pickled_method, *args, **kw):
     return method(*args, **kw)
 
 
+class Writer(object):
+    def __init__(self, func):
+        self.func = func
+
+    def write(self, buf):
+        self.func(buf)
+
+
 class ScriptTask(object):
+    """Base class for a command-line script that is also executable as a task."""
     @classmethod
-    def _execute(cls, arg_string):
-        options = cls.parser().parse_args(arg_string.split(' '))
-        cls.execute(options)
+    def _execute_task(cls, arg_string):
+        try:
+            _stdout = sys.stdout
+            _stderr = sys.stderr
+            sys.stdout = Writer(log.info)
+            sys.stderr = Writer(log.error)
+            try:
+                options = cls.parser().parse_args(arg_string.split(' '))
+            except SystemExit:
+                raise Exception("Error parsing args: '%s'" % arg_string)
+            cls.execute(options)
+        finally:
+            sys.stdout = _stdout
+            sys.stderr = _stderr
 
     @classmethod
     def execute(cls, options):
@@ -39,7 +60,7 @@ class ScriptTask(object):
 
     @classmethod
     def post(cls, arg_string=''):
-        pickled_method = pickle.dumps(cls._execute)
+        pickled_method = pickle.dumps(cls._execute_task)
         return dispatcher.post(pickled_method, arg_string)
 
     @classmethod


Mime
View raw message