Skip to content

Commit 985326b

Browse files
authored
jdk_install_exists() and set_java_home() changes
These changes are necessary to complement another __init__.py change that triggers a JAVA_HOME var update on each run of the portable version of Thonny.
1 parent 474327d commit 985326b

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

thonnycontrib/thonny-py5mode/install_jdk.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,44 @@
1919

2020

2121
def jdk_install_exists() -> bool:
22-
'''check system for existing jdk that meets the py5 version requirements'''
22+
'''
23+
Look for a jdk in Thonny's user directory first, and if one is present,
24+
set up JAVA_HOME, add it to Thonny's options, and return True. Otherwise,
25+
look for existing JAVA_HOME pointing to a jdk that meets the py5 version
26+
requirements and return True/False accordingly.
27+
'''
28+
# search for jdk in Thonny's user config directory
2329
jdk_dir = 'jdk-' + str(_REQUIRE_JDK)
24-
system_jdk = 'TBD'
25-
30+
for name in os.listdir(THONNY_USER_DIR):
31+
if name.startswith(jdk_dir):
32+
# set JAVA_HOME in Thonny's config (and in current env) with jdk_dir path
33+
set_java_home(pathlib.Path(THONNY_USER_DIR) / jdk_dir)
34+
return True
2635
# check system for existing jdk install (and the version number)
36+
system_jdk = 'TBD'
2737
if os.environ.get('JAVA_HOME') is not None:
2838
jdk_ver = os.environ['JAVA_HOME'].split('jdk-')[-1].split('.')[0]
2939
system_jdk = jdk_ver
30-
31-
for name in os.listdir(THONNY_USER_DIR):
32-
# check for jdk in the thonny config directory
33-
if name.startswith(jdk_dir):
34-
# set jdk path to thonny config directory
35-
set_java_home(pathlib.Path(THONNY_USER_DIR) / jdk_dir)
36-
return True
37-
38-
if not system_jdk.isdigit() or int(system_jdk) < _REQUIRE_JDK:
39-
return False
40+
# False if no JAVA_HOME was set, or if it is not the required version
41+
return system_jdk.isdigit() and int(system_jdk) >= _REQUIRE_JDK
4042

4143

4244
def set_java_home(jdk_path: pathlib.Path) -> None:
43-
'''add jdk path to config file (tools > options > general > env vars)'''
44-
jdk_path = 'JAVA_HOME=' + str(jdk_path)
45-
# if mac arm system, add /contents/home to jdk path
46-
if str(jdk.OS) == 'mac' and str(jdk.ARCH) == 'arm':
47-
jdk_path += '/Contents/Home'
48-
45+
'''
46+
Add jdk path to current envirnonment and to Thonny's config file
47+
as visible in (tools > options > general > env vars)
48+
'''
49+
os.environ['JAVA_HOME'] = str(pathlib.Path(THONNY_USER_DIR) / jdk_path)
50+
java_home_var = 'JAVA_HOME=' + str(jdk_path)
4951
env_vars = get_workbench().get_option('general.environment')
50-
51-
if jdk_path not in env_vars:
52-
env_vars.append(jdk_path)
53-
52+
for i, env_var in enumerate(env_vars):
53+
if 'JAVA_HOME' in env_var:
54+
env_vars[i] = java_home_var
55+
break
56+
else:
57+
env_vars.append(java_home_var)
5458
get_workbench().set_option('general.environment', env_vars)
5559

56-
5760
class DownloadJDK(threading.Thread):
5861
def __init__(self):
5962
super().__init__()
@@ -157,3 +160,4 @@ def install_jdk() -> None:
157160
'''call this function from where this module is imported'''
158161
if not jdk_install_exists():
159162
ui_utils.show_dialog(JdkDialog(get_workbench()))
163+

0 commit comments

Comments
 (0)