-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvm.py
69 lines (57 loc) · 2.04 KB
/
kvm.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
# https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/jnlpFileSyntax.html
import argparse
import xml.dom.minidom
import os
import urllib.request
import subprocess
import platform
import zipfile
# os.chdir(os.path.realpath(__file__))
java_path = ".\\java6"
parser = argparse.ArgumentParser(description = "APC KVM launcher")
parser.add_argument("jnlp", metavar="JNLP_FILE", type=argparse.FileType('r'),
help="The JNLP file downloaded from the KVM to start a session (.JNLP)")
args = parser.parse_args()
jnlp_xml = args.jnlp.read().strip()
DOMTree = xml.dom.minidom.parseString(jnlp_xml)
codebase = DOMTree.getElementsByTagName("jnlp")[0].attributes["codebase"].value + "/"
jars = []
nativelibs = []
# scan for files
for resource in DOMTree.getElementsByTagName("resources"):
if "os" in resource.attributes:
if resource.attributes["os"].value != platform.system():
continue
for jar in resource.getElementsByTagName("jar"):
jars.append(jar.attributes["href"].value)
for nativelib in resource.getElementsByTagName("nativelib"):
nativelibs.append(nativelib.attributes["href"].value)
# download files
for file in jars + nativelibs:
if os.path.isfile(file):
continue
print("Downloading " + file)
urllib.request.urlretrieve(codebase + file, file)
if file in nativelibs:
print("Extracting " + file)
zip = zipfile.ZipFile(file)
for zip_file in zip.infolist():
if zip_file.filename[-4:] == ".dll":
zip.extract(zip_file, "lib")
zip.close()
# prepare app
app = DOMTree.getElementsByTagName("application-desc")[0]
main = app.attributes["main-class"].value
args = []
for arg in app.getElementsByTagName("argument"):
args.append(arg.firstChild.data)
# start app
cmd = "\"" + java_path + os.path.sep + "bin\\java\" "
# cmd = "java "
cmd += "-Djava.library.path=lib "
cmd += "-classpath " + os.pathsep.join(jars) + " "
cmd += main + " "
cmd += " ".join(args)
print("Running command:")
print(cmd)
subprocess.Popen(cmd)