-
Notifications
You must be signed in to change notification settings - Fork 10
/
jws_wrapper.py
112 lines (81 loc) · 3.35 KB
/
jws_wrapper.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
import sys
import subprocess
import logging
''' Wrapper for jws to execute java programs distributed as jnlp
author: Piotr Skowronski
Usage ::
python jws_wrapper.py pathToJws JwsOpton1 JwsOpton2 ... JnlpUrl JnlpOption1 JnlpOption2 ...
This script mangles the parameters and executes via system call jws like this
pathToJws JwsOpton1 JwsOpton2 ... JnlpUrl?arg1=JnlpOption1&arg2=JnlpOption2& ...
'''
#logging.basicConfig()
LOGGER = logging.getLogger("JwsWrapper")
class JwsWrapper:
def __init__(self):
if sys.flags.debug: # true of -d passed to python
LOGGER.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
LOGGER.addHandler(ch)
self.jws_path = ""
self.jws_pars = []
self.jnlp_url = ""
self.jnlp_pars = "" # arg1=-e&arg2=-time&arg3=...
def mangleargs(self,argv):
''' Decodes arguments.
Argument 1 is jws path
The argument ending with .jnlp is the url of the jnlp to execute.
All arguments before are options of jws
All arguments after are options of jnlp
'''
LOGGER.debug("This is the name of the script: %s ", argv[0])
LOGGER.debug("Number of arguments: %d", len(argv))
LOGGER.debug("The arguments are: %s" , str(argv))
if len(argv) < 3:
raise SyntaxError('Needs 2 arguments: path to jws and a jnlp url')
self.jws_path = argv[1]
idxjnlp=0
for i in range(2,len(argv)):
a = argv[i]
if a.endswith(".jnlp"):
idxjnlp = i
break
else:
self.jws_pars.append(a)
if idxjnlp < 1:
raise SyntaxError('Did not find a jnlp in list of arguments')
self.jnlp_url = argv[idxjnlp]
# arg1=-e&arg2=-time&arg3=...
for i in range(idxjnlp+1,len(argv)):
argno = i - idxjnlp
LOGGER.debug("jnlp arg no %d %s ", argno, argv[i])
argstr = "arg{}={}".format(argno,argv[i])
if argno > 1:
argstr = "&"+argstr
argstr = argstr.replace(" ", "_")
self.jnlp_pars += argstr
return 0
def calljws(self):
#self.jws_path = "ls"
#self.jws_pars.append("-l")
#self.jws_pars.append("/home")
#self.jnlp_url = "blabla.jnlp"
#self.jnlp_pars = "&par1=-m&par2=-e"
LOGGER.debug( "JWS path: %s", self.jws_path)
LOGGER.debug( "JWS options: %s", self.jws_pars)
LOGGER.debug( "JNLP URL: %s" , self.jnlp_url)
LOGGER.debug( "JNLP args: %s" , self.jnlp_pars)
self.jws_pars.append(self.jnlp_url +"?" +self.jnlp_pars)
subprocess.call([self.jws_path] + self.jws_pars)
def runjws(self, argv):
self.mangleargs(argv)
retval = self.calljws()
return retval
def runjws(argv):
jwsw = JwsWrapper()
retval = jwsw.runjws(argv)
return retval
if __name__ == "__main__":
runjws(sys.argv)