diff --git a/vasl_templates/webapp/main.py b/vasl_templates/webapp/main.py index aaea0d6..318919b 100644 --- a/vasl_templates/webapp/main.py +++ b/vasl_templates/webapp/main.py @@ -14,7 +14,7 @@ from flask import request, render_template, jsonify, send_file, redirect, url_fo from vasl_templates.webapp import app, shutdown_event from vasl_templates.webapp.vassal import VassalShim -from vasl_templates.webapp.utils import MsgStore, parse_int +from vasl_templates.webapp.utils import MsgStore, get_java_version, parse_int import vasl_templates.webapp.config.constants from vasl_templates.webapp.config.constants import BASE_DIR, DATA_DIR, IS_FROZEN from vasl_templates.webapp import globvars @@ -182,6 +182,7 @@ def get_program_info(): for key in [ "VASSAL_DIR", "VASL_MOD", "VASL_EXTNS_DIR", "BOARDS_DIR", "JAVA_PATH", "WEBDRIVER_PATH", "CHAPTER_H_NOTES_DIR", "USER_FILES_DIR" ]: params[ key ] = app.config.get( key ) + params[ "JAVA_VERSION" ] = get_java_version() def parse_timestamp( val ): """Parse a timestamp.""" diff --git a/vasl_templates/webapp/templates/program-info-content.html b/vasl_templates/webapp/templates/program-info-content.html index 1a5ccfc..76bcd92 100644 --- a/vasl_templates/webapp/templates/program-info-content.html +++ b/vasl_templates/webapp/templates/program-info-content.html @@ -33,7 +33,12 @@
Java: - {%if JAVA_PATH%} {{JAVA_PATH}} {%else%} - {%endif%} + {%if JAVA_PATH%} + {{JAVA_PATH}} + {%if JAVA_VERSION%} ({{JAVA_VERSION}}) {%endif%} + {%else%} + - + {%endif%}
Web driver: {%if WEBDRIVER_PATH%} {{WEBDRIVER_PATH}} {%else%} - {%endif%}
diff --git a/vasl_templates/webapp/utils.py b/vasl_templates/webapp/utils.py index cbd2bac..41a97d1 100644 --- a/vasl_templates/webapp/utils.py +++ b/vasl_templates/webapp/utils.py @@ -2,6 +2,7 @@ import os import shutil +import subprocess import io import tempfile import pathlib @@ -212,6 +213,48 @@ def remove_alpha_from_image( img ): # --------------------------------------------------------------------- +def get_java_version(): + """Get the version of the configured Java runtime.""" + java_path = get_java_path() + if not java_path: + return None + try: + args = [ java_path, "-version" ] + kwargs = { + "capture_output": True, "text": True, + "stdin": subprocess.DEVNULL, + } + if is_windows(): + kwargs["creationflags"] = 0x8000000 # nb: win32process.CREATE_NO_WINDOW + proc = subprocess.run( args, check=True, **kwargs ) + return proc.stderr.split( "\n" )[0] + except Exception as ex: #pylint: disable=broad-except + logging.error( "Can't get Java version: %s", ex ) + return "???" + +def get_java_path(): + """Locate the Java runtime.""" + + # get the configured path to Java + java_path = app.config.get( "JAVA_PATH" ) + + # check if we should use the Java that now comes bundled with VASSAL + if not java_path and is_windows(): + vassal_dir = app.config.get( "VASSAL_DIR" ) + if vassal_dir: + fname = os.path.join( vassal_dir, "jre/bin/java.exe" ) + if os.path.isfile( fname ): + java_path = fname + + # check if we've found a Java runtime + if not java_path: + # nope - hope that there's one on the PATH + java_path = "java" + + return java_path + +# --------------------------------------------------------------------- + def change_extn( fname, extn ): """Change a filename's extension.""" return pathlib.Path( fname ).with_suffix( extn ) diff --git a/vasl_templates/webapp/vassal.py b/vasl_templates/webapp/vassal.py index 9631fa2..51d8ef6 100644 --- a/vasl_templates/webapp/vassal.py +++ b/vasl_templates/webapp/vassal.py @@ -18,7 +18,7 @@ from flask import request, jsonify from vasl_templates.webapp import app, globvars from vasl_templates.webapp.config.constants import BASE_DIR, IS_FROZEN -from vasl_templates.webapp.utils import TempFile, SimpleError, compare_version_strings, is_windows +from vasl_templates.webapp.utils import TempFile, SimpleError, get_java_path, compare_version_strings from vasl_templates.webapp.webdriver import WebDriver from vasl_templates.webapp.vasl_mod import get_reverse_remapped_gpid @@ -394,8 +394,8 @@ class VassalShim: # initialize logger = logging.getLogger( "vassal_shim" ) - # figure out where Java is - java_path = app.config.get( "JAVA_PATH" ) + # locate the Java runtime + java_path = get_java_path() java8_path = app.config.get( "JAVA8_PATH" ) if java8_path: # FUDGE! From 3.3, VASSAL no longer works with Java 8. We want to mantain back-compatibility @@ -412,13 +412,6 @@ class VassalShim: if compare_version_strings( mo.group(), "3.3.0" ) < 0: # we're using a legacy version of VASSAL - use Java 8 java_path = java8_path - if not java_path and is_windows(): - # we're on Windows - try to use the Java that is now bundled with VASSAL - fname = os.path.join( self._get_vassal_dir(), "jre/bin/java.exe" ) - if os.path.isfile( fname ): - java_path = fname - if not java_path: - java_path = "java" # nb: this must be in the PATH # prepare the command class_path = app.config.get( "JAVA_CLASS_PATH" )