|
a/Allura/allura/scripts/scripttask.py |
|
b/Allura/allura/scripts/scripttask.py |
|
... |
|
... |
53 |
|
53 |
|
54 |
|
54 |
|
55 |
log = logging.getLogger(__name__)
|
55 |
log = logging.getLogger(__name__)
|
56 |
|
56 |
|
57 |
|
57 |
|
58 |
class Writer(object):
|
|
|
59 |
def __init__(self, func):
|
|
|
60 |
self.func = func
|
|
|
61 |
|
|
|
62 |
def write(self, buf):
|
|
|
63 |
self.func(buf)
|
|
|
64 |
|
|
|
65 |
|
58 |
|
66 |
class ScriptTask(object):
|
59 |
class ScriptTask(object):
|
67 |
"""Base class for a command-line script that is also executable as a task."""
|
60 |
"""Base class for a command-line script that is also executable as a task."""
|
68 |
|
61 |
|
69 |
class __metaclass__(type):
|
62 |
class __metaclass__(type):
|
|
... |
|
... |
77 |
cls._execute_task(arg_string)
|
70 |
cls._execute_task(arg_string)
|
78 |
|
71 |
|
79 |
@classmethod
|
72 |
@classmethod
|
80 |
def _execute_task(cls, arg_string):
|
73 |
def _execute_task(cls, arg_string):
|
81 |
try:
|
74 |
try:
|
82 |
_stdout = sys.stdout
|
|
|
83 |
_stderr = sys.stderr
|
|
|
84 |
sys.stdout = Writer(log.info)
|
|
|
85 |
sys.stderr = Writer(log.error)
|
|
|
86 |
try:
|
|
|
87 |
options = cls.parser().parse_args(shlex.split(arg_string or ''))
|
75 |
options = cls.parser().parse_args(shlex.split(arg_string or ''))
|
88 |
except SystemExit:
|
76 |
except SystemExit:
|
89 |
raise Exception("Error parsing args: '%s'" % arg_string)
|
77 |
raise Exception("Error parsing args: '%s'" % arg_string)
|
90 |
cls.execute(options)
|
78 |
cls.execute(options)
|
91 |
finally:
|
|
|
92 |
sys.stdout = _stdout
|
|
|
93 |
sys.stderr = _stderr
|
|
|
94 |
|
79 |
|
95 |
@classmethod
|
80 |
@classmethod
|
96 |
def execute(cls, options):
|
81 |
def execute(cls, options):
|
97 |
"""User code goes here."""
|
82 |
"""User code goes here."""
|
98 |
pass
|
83 |
pass
|