add script to discover installed jdk

This commit is contained in:
Robert Kaussow 2018-09-16 12:09:04 +02:00
parent 2447a10186
commit a71e61daf0

63
jdk.py Normal file
View File

@ -0,0 +1,63 @@
#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-
import re
import json
import subprocess
from collections import defaultdict
def fetch_java_version():
lines = []
cmd = "java -version"
args = cmd.split(" ")
try:
proc = subprocess.check_output(args, stderr=subprocess.STDOUT)
proc = proc.splitlines()
for line in proc:
lines.append(line.rstrip())
except Exception:
raise
return lines
def get_vendor_version():
lines = fetch_java_version()
line = lines[1].lower()
vendor = (line.split()[0]).lower()
if vendor == "openjdk" and "zulu" in line:
exp_jdk = re.compile("(\d(\.\d+){1,}_\d+)")
exp_zulu = re.compile("zulu (\d(\.\d+){1,})")
vendor = "zulu"
version = "%s-jdk%s" % (exp_zulu.search(line).group(1), exp_jdk.search(line).group())
else:
exp = re.compile("(\d(\.\d+){1,})")
version = exp.search(line).group()
return (vendor, version)
def main():
result = defaultdict(dict)
try:
vendor, version = get_vendor_version()
result.update(vendor=vendor)
result.update(version=version)
result.update(error=False)
except Exception as e:
result.update(error=True)
result.update(message=e.strerror)
print json.dumps(result)
if __name__ == "__main__":
main()