Skip to content

Commit

Permalink
Merge branch 'javainfo' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sammko committed Sep 5, 2020
2 parents a2332da + d686afb commit 4824e45
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 93 deletions.
14 changes: 13 additions & 1 deletion picomc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
from picomc.utils import cached_property


def get_default_java():
java_home = os.getenv("JAVA_HOME")
if java_home is not None:
candidates = ["java", "java.exe"]
for candidate in candidates:
path = os.path.join(java_home, "bin", candidate)
if os.path.isfile(path):
logger.debug("Detected JAVA_HOME, using as default")
return path
return "java"


def get_default_config():
return {
"java.path": "java",
"java.path": get_default_java(),
"java.memory.min": "512M",
"java.memory.max": "2G",
"java.jvmargs": "-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M",
Expand Down
2 changes: 1 addition & 1 deletion picomc/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import picomc
from picomc import logging
from picomc.javainfo import assert_java
from picomc.java import assert_java
from picomc.logging import logger
from picomc.rules import match_ruleset
from picomc.utils import Directory, join_classpath, sanitize_name
Expand Down
Binary file added picomc/java/SysDump.class
Binary file not shown.
1 change: 1 addition & 0 deletions picomc/java/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .javainfo import assert_java, get_java_info
78 changes: 78 additions & 0 deletions picomc/java/javainfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import shutil
import subprocess
from importlib import resources
from tempfile import TemporaryDirectory

from picomc.logging import logger
from picomc.utils import die

#
# SysDump.class:
#
# import java.io.IOException;
#
# public class SysDump {
# public static void main(String[] args) throws IOException {
# System.getProperties().storeToXML(System.out, "");
# }
# }
#
# Compiled with an antique version of java for widest compatibility.
# Ideally we would distribute the source .java file and build it in the
# picomc build process, but that would bring in a dependency for (old) java
# and extra complexity.
#


def get_java_info(java):
with TemporaryDirectory() as tmpdir:
with resources.open_binary("picomc.java", "SysDump.class") as incf, open(
os.path.join(tmpdir, "SysDump.class"), "wb"
) as outcf:
shutil.copyfileobj(incf, outcf)
ret = subprocess.run(
[java, "-cp", ".", "SysDump"], cwd=tmpdir, capture_output=True
)
from xml.etree import ElementTree

xmlstr = ret.stdout.decode("utf8")
props = ElementTree.fromstring(xmlstr)
res = dict()
for entry in props:
if "entry" != entry.tag or "key" not in entry.attrib:
continue
res[entry.attrib["key"]] = entry.text
return res


def assert_java(java):
try:
jinfo = get_java_info(java)
badjv = not jinfo["java.version"].startswith("1.8.0")
bitness = jinfo.get("sun.arch.data.model", None)
if bitness and bitness != "64":
logger.warning(
"You are not using 64-bit java. Things will probably not work."
)

logger.info(
"Using java version: {} ({})".format(
jinfo["java.version"], jinfo["java.vm.name"]
)
)

if badjv:
logger.warning(
"Minecraft uses java 1.8.0 by default."
" You may experience issues, especially with older versions of Minecraft."
)

return jinfo

except FileNotFoundError:
die(
"Could not execute java at: {}. Have you installed it? Is it in yout PATH?".format(
java
)
)
89 changes: 0 additions & 89 deletions picomc/javainfo.py

This file was deleted.

2 changes: 1 addition & 1 deletion picomc/osinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_arch():
def get_os_version(java_info):
if not java_info:
return None, None
version = java_info.get("os.version").decode("ascii")
version = java_info.get("os.version")
return version


Expand Down
2 changes: 1 addition & 1 deletion picomc/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import requests

from picomc.downloader import DownloadQueue
from picomc.javainfo import get_java_info
from picomc.java import get_java_info
from picomc.library import Library
from picomc.logging import logger
from picomc.rules import match_ruleset
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
],
python_requires=">=3.7",
entry_points={"console_scripts": ["picomc = picomc:main"]},
package_data={"picomc.java": ["SysDump.class"]},
)

0 comments on commit 4824e45

Please sign in to comment.