This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomatarunner.py
executable file
·153 lines (128 loc) · 6.13 KB
/
automatarunner.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-
############################################################################
# #
# Copyright (c) 2017 eBay Inc. #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
############################################################################
from __future__ import print_function
from __future__ import division
from optparse import OptionParser
import sys
from importlib import import_module
from os.path import realpath
from inspect import getargspec
from os import environ
from compat import quote_plus, PY3
import automata_common
from dispatch import JobError
from autoflush import AutoFlush
def find_automata(a, package, script):
if package:
package = [package]
else:
package = sorted(a.config()['method_directories'])
if not script:
script = 'automata'
if not script.startswith('automata'):
script = 'automata_' + script
for p in package:
module_name = p + '.' + script
try:
module_ref = import_module(module_name)
print(module_name)
return module_ref
except ImportError as e:
if PY3:
if not e.msg[:-1].endswith(script):
raise
else:
if not e.message.endswith(script):
raise
raise Exception('No automata "%s" found in {%s}' % (script, ', '.join(package)))
def run_automata(options):
if options.port:
assert not options.socket, "Specify either socket or port (with optional hostname)"
url = 'http://' + (options.hostname or 'localhost') + ':' + str(options.port)
else:
assert not options.hostname, "Specify either socket or port (with optional hostname)"
url = 'unixhttp://' + quote_plus(realpath(options.socket or './socket.dir/default'))
a = automata_common.Automata(url, verbose=options.verbose, flags=options.flags.split(','), infoprints=True)
if options.abort:
a.abort()
return
try:
a.wait(ignore_old_errors=not options.just_wait)
except JobError:
# An error occured in a job we didn't start, which is not our problem.
pass
if options.just_wait:
return
module_ref = find_automata(a, options.package, options.script)
if getargspec(module_ref.main).args == ['urd']: # the future!
if 'URD_AUTH' in environ:
user, password = environ['URD_AUTH'].split(':', 1)
else:
user, password = None, None
info = a.info()
urd = automata_common.Urd(a, info, user, password, options.horizon)
if options.quick:
a.update_method_deps()
else:
a.update_methods()
module_ref.main(urd)
return
assert not options.horizon, '--horizon is only compatible with urd-enabled automatas'
module_ref.auto = automata_common
module_ref.a = a
module_ref.PATH = a.info()['path']
module_ref.Seq = a.seq
# Args you can get to autamata_foo.main
# I would say automata, seq and path are the only reasonable names here.
path = module_ref.PATH
argd = dict(a=a, automata=a, PATH=path, path=path)
# run automata script
kw = {}
for arg in getargspec(module_ref.main).args:
kw[arg] = argd[arg]
module_ref.main(**kw)
return
def main(argv):
parser = OptionParser(usage="Usage: %prog [options] [script]")
parser.add_option('-p', '--port', dest="port", default=None, help="framework listening port", )
parser.add_option('-H', '--hostname', dest="hostname", default=None, help="framework hostname", )
parser.add_option('-S', '--socket', dest="socket", default=None, help="framework unix socket (default ./socket.dir/default)", )
parser.add_option('-s', '--script', dest="script", default=None , help="automata script to run. package/[automata_]script.py. default \"automata\". Can be bare arg too.",)
parser.add_option('-P', '--package', dest="package", default=None , help="package where to look for script, default all method directories in alphabetical order", )
parser.add_option('-f', '--flags', dest="flags", default='', help="comma separated list of flags", )
parser.add_option('-A', '--abort', dest="abort", action='store_true', help="abort (fail) currently running job(s)", )
parser.add_option('-q', '--quick', dest="quick", action='store_true', help="skip method updates and checking workspaces for new jobs", )
parser.add_option('-w', '--just_wait',dest="just_wait",action='store_true', help="just wait for running job, don't run any automata", )
parser.add_option('--verbose', dest="verbose", default='status', help="verbosity style {no, status, dots, log}")
parser.add_option('--quiet', dest="quiet", action='store_true', help="same as --verbose=no")
parser.add_option('--horizon', dest="horizon", default=None, help="Time horizon - dates after this are not visible in urd.latest")
options, args = parser.parse_args(argv)
if len(args) == 1:
assert options.script is None, "Don't specify both --script and a bare script name."
options.script = args[0]
else:
assert not args, "Don't know what to do with args %r" % (args,)
options.verbose = {'no': False, 'status': True, 'dots': 'dots', 'log': 'log'}[options.verbose]
if options.quiet: options.verbose = False
run_automata(options)
if __name__ == "__main__":
sys.stdout = AutoFlush(sys.stdout)
sys.stderr = AutoFlush(sys.stderr)
main(sys.argv[1:])