-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpm.py
executable file
·380 lines (298 loc) · 9.08 KB
/
cpm.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/python
import sys
import zmq
import os
import re
import click
import webbrowser
from message import com
def initConf() :
defaultservername = "localhost"#"192.168.1.27"#"localhost"#"rscpm"
defaultserverport = "5555"
conffile = os.environ["HOME"]+"/.config/cpm.conf"
conf = {
"host":defaultservername,
"port":defaultserverport
}
errormessage = "Using default value for connection : "+defaultservername+":"+defaultserverport
if os.path.exists(conffile) :
f = open(conffile,"r")
try :
for l in f:
l = l.strip()
if l.startswith("#") or l == "":
continue
else :
variable = l.split(":")
conf[variable[0].strip()] = variable[1].strip()
except :
conf = {
"host":defaultservername,
"port":defaultserverport
}
print "Unexpected error while reading configuration :", sys.exc_info()[0]
else :
print "Couldn't find configuration file!"
print errormessage
return conf
conf=initConf()
connectionInfo = "tcp://"+conf["host"]+":"+conf["port"]
context = zmq.Context()
sock = context.socket(zmq.REQ)
sock.setsockopt(zmq.LINGER, 0)
sock.connect(connectionInfo)
@click.group()
def cli() :
"""CPM CLI - Corpus & Process Manager Command Line Interface"""
pass
@cli.group()
def process():
"""Manage cpm process"""
pass
@cli.command()
def reload() :
"""Refresh cpm modules defintion"""
print com.sendCommand(sock,"reload")
@cli.command()
def log() :
"""Refresh cpm modules defintion"""
print com.sendCommand(sock,"log")
@cli.command()
def test() :
"""Test command for dev debug purpose"""
print com.sendCommand(sock,"test")
@cli.command()
def status() :
"""Get information about cpm server status"""
print com.sendCommand(sock,"status",timeout = 10)
@cli.group()
def corpus() :
"""Depreciated functions (use fs)"""
pass
@corpus.command()
@click.option("--all","-a",default=False,is_flag=True,help="Print also results")
@click.option("--json",default=False,is_flag=True,help="Print in json format")
def ls(all,json):
optionall = ""
optionjson = ""
if all :
optionall = " --all"
if json :
optionjson = " --json"
print com.sendCommand(sock,"corpus ls"+optionall+optionjson)
@corpus.command()
@click.argument('filepath')
@click.argument('offset')
def lsdir(filepath,offset):
print com.sendCommand(sock,"corpus lsdir "+filepath+" "+offset)
@cli.group()
def module() :
"""Manage cpm modules"""
pass
def modulesls(name):
return com.sendCommand(sock,"module ls"+name,timeout=10)
def processls(allp):
return com.sendCommand(sock,"process ls "+allp,timeout=10)
def modulehelp(name):
return com.sendCommand(sock,"module getdesc "+name+" --extended",timeout=10)
def modulefunc(name):
@cli.command(help=modulehelp(name))
@click.option('--sync',default=False,is_flag=True,help="Run the module synchronously")
@click.option('--config','-c',type=click.File('rb'),default=None,help="Yaml environment input configuration file")
@click.option('--arg',multiple=True,help="Input (overrides configuration file if given) of the form INPUTNAME:VALUE (yaml field format)")
def func(arg,config,sync):
confdata = ""
args = {}
if config :
for line in config:
if line.strip() != "":
var = line.split(":")
if len(var) < 2:
print "Wrong input paramater format : "+line
return
paramater = var[0].strip()
if paramater in args:
print "warning multiple input values for paramater "+paramater
args[paramater] = ":".join(var[1:]).strip()
for item in arg :
var = item.split(":")
if len(var) < 2:
print "Wrong input paramater format : "+item
return
paramater = var[0].strip()
if paramater in args:
print "warning multiple input values for paramater "+paramater
args[paramater] = ":".join(var[1:]).strip()
for paramater in args:
confdata += paramater+" : "+args[paramater]+"\n"
synced = ""
if(sync):
synced = " --sync"
print com.sendCommand(sock,"module run "+name+synced,data=confdata)
return func
def moduleinfofunc(name):
@cli.command(help=modulehelp(name))
def func():
print com.sendCommand(sock,"module info "+name)
return func
class CPMModulesRun(click.MultiCommand):
def list_commands(self, ctx):
rv = []
modules = modulesls(" --name")
for filename in modules.split("\n"):
if filename.strip() != "":
rv.append(filename)
rv.sort()
return rv
def get_command(self, ctx, name):
return modulefunc(name)
class CPMModulesInfo(click.MultiCommand):
def list_commands(self, ctx):
rv = []
modules = modulesls(" --name")
for filename in modules.split("\n"):
if filename.strip() != "":
rv.append(filename)
rv.sort()
return rv
def get_command(self, ctx, name):
return moduleinfofunc(name)
@cli.command(cls=CPMModulesRun)
def run():
"""Shorthand for "module run" command"""
pass
@module.command()
@click.option('--name', default=False,is_flag=True,help="Only print modules' name")
@click.option('--json', default=False,is_flag=True,help="JSON Format")
def ls(name,json):
"""List all available modules"""
optname = ""
jsonopt = ""
if name :
optname = " --name"
if json :
jsonopt = " --json"
print modulesls(optname+jsonopt)
@module.command(cls=CPMModulesInfo)
def info():
"""Get information about a module"""
pass
@module.command(cls=CPMModulesRun)
def run():
"""Run a MODULE with configuration file CONF_FILE"""
pass
@cli.command()
def settings():
"""Retrieve appfm settings"""
print com.sendCommand(sock,"settings")
@process.command()
def queue():
print com.sendCommand(sock,"process queue")
@process.command()
@click.option('--all','-a', default=False,is_flag=True,help="List all processes (including stopped/finished processes)")
#@click.option('--recursive','-r', default=False,is_flag=True,help="List also children processes")
@click.option('--head','-h', default=False,is_flag=True,help="List only latest owned")
@click.option('--user','-u', default=False,is_flag=True,help="List only owned processes")
def ls(all,head,user):
"""List running processes"""
optall = ""
optrec = ""
optowned = ""
if all :
optall = " -a"
if head :
optrec = " -h"
if user :
optowned = " -u"
print com.sendCommand(sock,"process ls"+optall+optrec+optowned)
class CPMProcessActions(click.MultiCommand):
def list_commands(self, ctx):
rv = []
runs = processls("-a -h")
for run in runs.split("\n"):
m = re.search(".*?:\s*([a-zA-Z0-9_\-]+)",run)
if m:
rv.append(m.group(1))
#rv.sort()
return rv
class CPMProcessView(CPMProcessActions):
def get_command(self,ctx,name):
@cli.command()
@click.argument('outputname',default="__ALL__")
def processtestview(outputname):
print com.sendCommand(sock,"process view "+name+" "+outputname)
return processtestview
@process.command(cls=CPMProcessView)
def view():
pass
class CPMProcessGet(CPMProcessActions):
def get_command(self,ctx,pid):
@cli.command()
def func():
print com.sendCommand(sock,"process get "+pid)
return func
@process.command(cls=CPMProcessGet)
def get():
""" Print serialized information of a process """
pass
class CPMProcessStatus(CPMProcessActions):
def get_command(self,ctx,pid):
@cli.command()
def func():
print com.sendCommand(sock,"process status "+pid)
return func
@process.command(cls=CPMProcessStatus)
def status():
"""Print the status of a process and its children processes if any"""
pass
class CPMProcessLog(CPMProcessActions):
def get_command(self,ctx,pid):
@cli.command()
def func():
print com.sendCommand(sock,"process log "+pid)
return func
@process.command(cls=CPMProcessLog)
def log():
"""View the default error log of a module run PID"""
pass
class CPMProcessDel(CPMProcessActions):
def get_command(self,ctx,pid):
@cli.command()
def func():
print com.sendCommand(sock,"process del "+pid)
return func
@process.command(cls=CPMProcessDel)
def delete():
"""View the default error log of a module run PID"""
pass
class CPMProcessKill(CPMProcessActions):
def get_command(self,ctx,pid):
@cli.command()
def func():
print com.sendCommand(sock,"process kill "+pid)
return func
@process.command(cls=CPMProcessKill)
def kill():
"""View the default error log of a module run PID"""
pass
@cli.group()
def fs():
"""Access functions to filesystem (retrieve file, list directories)"""
pass
@fs.command()
@click.argument('file')
def get(file):
"""Retrieve file content"""
print com.sendCommand(sock,"fs get "+file)
@fs.command()
@click.argument('directory')
def ls(directory):
"""List directory content"""
import json
jsonobj = json.loads(com.sendCommand(sock,"fs ls "+directory+" 0"))
while "..." in jsonobj[len(jsonobj)-1] :
jsonobj = jsonobj[:-1]
jsonobj += json.loads(com.sendCommand(sock,"fs ls "+directory+" "+str(len(jsonobj))))
print json.dumps(jsonobj)
if __name__ == '__main__':
cli()