-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathflo-run
executable file
·126 lines (109 loc) · 4.52 KB
/
flo-run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf8 -*-
import logging
import os
import sys
from optparse import OptionParser
import gettext
import florun
import florun.gui
import florun.flow
from florun.flow import FlowError
def showversion():
print _("""%(__title__)s version %(__version__)s
%(__copyright__)s by %(__author__)s, %(__credits__)s
%(__license__)s""") % florun.__dict__
def main(prefix):
if prefix.startswith('/usr'):
prefix = os.path.join(prefix, '..')
prefix = os.path.abspath(os.path.normpath(prefix))
florun.base_dir = os.path.expanduser("~")
florun.locale_dir = os.path.join(prefix, 'share', 'locale')
florun.icons_dir = os.path.join(prefix, 'share', 'florun', 'icons')
# Set up the path to translation files
gettext.install('florun', florun.locale_dir, unicode=True)
florun._ = _
# Parse command-line arguments
parser = OptionParser(usage='%s [options]' % __file__)
parser.add_option("-v", "--version",
dest="version", default=False, action="store_true",
help=_("Show version"))
parser.add_option("--plugins-dir",
dest="plugins_dirs", default=None,
help=_("Plugins folders to scan "
"(separated with %(sep)s). Extends if starts with '%(sep)s', overrides otherwise.") % {'sep': os.pathsep})
parser.add_option("-e", "--edit",
dest="edit", default=None,
help=_("Edit specified Florun file"))
parser.add_option("-x", "--execute",
dest="execute", default=None,
help=_("Execute specified Florun file"))
parser.add_option("-l", "--level",
dest="level", default=logging.DEBUG, type='int',
help=_("Logging level for messages (1:debug 2:info, 3:warning, 4:errors, 5:critical)"))
# We distinguish official args from args passed to flow
args = sys.argv[1:]
last = len(args)
for i, arg in enumerate(args):
if not arg.startswith('--') and arg.startswith('-') and \
parser.get_option(arg) is None:
last = i
break
# Parse only first part of args
(options, useless) = parser.parse_args(args[:last])
logging.basicConfig()
florun.flow.logger.setLevel(options.level)
florun.gui.logger.setLevel(options.level)
if options.version:
showversion()
return 0
if options.plugins_dirs:
# If plugins_dirs starts with ":", append to default dirs, else override
if options.plugins_dirs.startswith(os.pathsep):
florun.plugins_dirs += options.plugins_dirs
else:
florun.plugins_dirs = options.plugins_dirs
# Extends current python path with all plugins dirs
sys.path.extend(florun.plugins_dirs.split(os.pathsep))
florun.gui.logger.debug("Prefix : '%s'" % prefix)
florun.gui.logger.debug("Icons : '%s'" % florun.icons_dir)
florun.flow.logger.debug("Plugins: '%s'" % florun.plugins_dirs)
if options.edit is not None:
# Run editor on specified flow
return florun.gui.main(args, options.edit)
elif options.execute is not None:
try:
# Load flow definition
wf = florun.flow.Flow.load(options.execute)
# Check if flow uses cmd line args
cmdparamnodes = wf.CLIParameterNodes()
if len(cmdparamnodes) > 0:
# Parse the rest of args
parser = OptionParser()
for node in cmdparamnodes:
parser.add_option("-%s" % node.paramname[:1],
"--%s" % node.paramname,
dest=node.paramname)
(options, parsedargs) = parser.parse_args(args[last:])
for node in cmdparamnodes:
node.options = options
runner = florun.flow.Runner(wf)
runner.start()
except FlowError, e:
florun.flow.logger.exception(e)
return 1
except KeyboardInterrupt, e:
florun.flow.logger.error(_("Interrupted by user"))
return 2
else:
return florun.gui.main(args)
return 0
if __name__ == "__main__":
# Paths
florun_script = sys.argv[0]
if os.path.islink(florun_script):
florun_script = os.readlink(florun_script)
prefix = os.path.dirname(florun_script)
code = main(prefix)
florun.flow.logger.info(florun._("Exit. (status=%s)") % code)
sys.exit(code)