forked from knipknap/exscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exclaim
executable file
·280 lines (265 loc) · 8.69 KB
/
exclaim
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!C:\Python34\python.exe
# Copyright (C) 2010 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import sys
import time
import codecs
from optparse import OptionParser
from Exscript import __version__
from Exscriptd import Client, Order
parser = OptionParser(usage = '%prog [options] ip[:port] [filename]',
version = __version__)
parser.add_option('--user', '-u',
dest = 'user',
metavar = 'STRING',
help = '''
The username on the server.
'''.strip())
parser.add_option('--password', '-p',
dest = 'password',
metavar = 'STRING',
help = '''
The password on the server.
'''.strip())
parser.add_option('--wait', '-w',
dest = 'wait',
action = 'store_true',
default = False,
help = '''
After placing the order, wait until it is completed.
'''.strip())
parser.add_option('--csv',
dest = 'csv',
action = 'store_true',
default = False,
help = '''
The order is using CSV format.
'''.strip())
parser.add_option('--encoding',
dest = 'encoding',
metavar = 'STRING',
default = 'utf-8',
help = '''
The encoding of the input file when using --csv. Defaults to utf8.
'''.strip())
parser.add_option('--service',
dest = 'service',
metavar = 'STRING',
help = '''
Send to the service with the given name, instead of
the service that is specified in the order.
'''.strip())
parser.add_option('--status', '-s',
dest = 'status',
metavar = 'STRING',
help = '''
Prints the status of the order with the given id and exit.
'''.strip())
parser.add_option('--test',
dest = 'test',
action = 'store_true',
default = False,
help = '''
Print the XML formatted order to stdout.
'''.strip())
parser.add_option('--order-get',
dest = 'order_get',
metavar = 'INTEGER',
help = '''
Prints the order with the given id.
'''.strip())
parser.add_option('--order-count',
dest = 'order_count',
action = 'store_true',
default = False,
help = '''
Prints the total number of orders.
'''.strip())
parser.add_option('--order-list',
dest = 'order_list',
action = 'store_true',
default = False,
help = '''
Lists the last 50 orders.
'''.strip())
parser.add_option('--task-get',
dest = 'task_get',
metavar = 'INTEGER',
help = '''
Prints the task with the given id.
'''.strip())
parser.add_option('--task-count',
dest = 'task_count',
metavar = 'INTEGER',
help = '''
Prints the number of tasks in the order with the given id.
'''.strip())
parser.add_option('--task-list',
dest = 'task_list',
metavar = 'INTEGER',
help = '''
Lists the last 50 tasks of the order with the given id.
'''.strip())
parser.add_option('--quite', '-q',
dest = 'quite',
action = 'store_true',
default = False,
help = '''
Do not write anything to stdout.
'''.strip())
# Parse options.
options, args = parser.parse_args(sys.argv)
args.pop(0)
if not options.test:
if not options.user:
parser.error('Required option --user not set')
if not options.password:
parser.error('Required option --password not set')
try:
address = args.pop(0)
except IndexError:
parser.error('ip argument missing')
if options.status:
pass
elif options.order_get:
pass
elif options.order_count:
pass
elif options.order_list:
pass
elif options.task_get:
pass
elif options.task_count:
pass
elif options.task_list:
pass
else:
try:
filename = args.pop(0)
except IndexError:
parser.error('order argument missing')
if args:
parser.error('too many arguments')
def say(*args):
if not options.quite:
sys.stdout.write(' '.join(str(a) for a in args) + '\n')
def print_table(rows):
if not rows:
return
widths = [1 for h in rows[0]]
for row in rows:
for n, col in enumerate(row):
widths[n] = max(widths[n], len(str(col)) + 2)
for i, row in enumerate(rows):
for n, col in enumerate(row):
sys.stdout.write(str(col).ljust(widths[n]))
print
if i == 0:
print('-' * sum(widths))
client = Client(address, options.user, options.password)
if options.status:
status, progress, closed = client.get_order_status_from_id(options.status)
print(status, '%.1f%%' % (progress * 100.0), closed)
sys.exit(0)
elif options.order_get:
order = client.get_order_from_id(int(options.order_get))
print('ID:', order.get_id())
print('Service:', order.get_service_name())
print('Status:', order.get_status())
print('Description:', order.get_description())
print('Created by:', order.get_created_by())
print('Created:', order.get_created_timestamp())
print('Closed:', order.get_closed_timestamp())
sys.exit(0)
elif options.order_count:
print(client.count_orders())
sys.exit(0)
elif options.order_list:
rows = [('OrderID',
'Service',
'Description',
'Status',
'Progress',
'Created by',
'Created',
'Closed')]
for order in client.get_order_list(offset = 0, limit = 100):
row = (order.get_id(),
order.get_service_name(),
order.get_description(),
order.get_status(),
order.get_progress_percent(),
order.get_created_by(),
str(order.get_created_timestamp()),
str(order.get_closed_timestamp() or 'open'))
rows.append(row)
print_table(rows)
sys.exit(0)
elif options.task_get:
task = client.get_task_from_id(int(options.task_get))
print('ID:', task.get_id())
print('Name:', task.get_name())
print('Status:', task.get_status())
print('Started:', task.get_started_timestamp())
print('Closed:', task.get_closed_timestamp())
print('Logfile:', task.get_logfile())
print('Trace file:', task.get_tracefile())
sys.exit(0)
elif options.task_count:
print(client.count_tasks(int(options.task_count)))
sys.exit(0)
elif options.task_list:
rows = [('TaskID',
'Name',
'Status',
'Progress',
'Started',
'Closed')]
for task in client.get_task_list(int(options.task_list), 0, 100):
row = (task.get_id(),
task.get_name(),
task.get_status(),
'%.1f' % (task.get_progress() * 100.0),
str(task.get_started_timestamp()),
str(task.get_closed_timestamp() or 'open'))
rows.append(row)
print_table(rows)
sys.exit(0)
try:
codecs.lookup(options.encoding)
except LookupError:
parser.error('no such encoding: ' + str(options.encoding))
if options.csv:
if not options.service:
parser.error('Need --service when using --csv.')
order = Order.from_csv_file(options.service, filename, options.encoding)
else:
order = Order.from_xml_file(filename)
if options.service:
order.set_service_name(options.service)
if options.test:
print(order.toxml())
sys.exit(0)
client.place_order(order)
say('Placed order', order.id)
if options.wait:
while True:
status, progress, closed = client.get_order_status_from_id(order.id)
say('Status:', status)
if closed is not None:
print('Order', order.id, 'closed', 'in status', status, 'on', str(closed) + '.')
break
time.sleep(1)