|
19 | 19 |
|
20 | 20 |
|
21 | 21 | 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 |
23 | 29 | 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 |
26 | 35 | # check system for existing jdk install (and the version number) |
| 36 | + system_jdk = 'TBD' |
27 | 37 | if os.environ.get('JAVA_HOME') is not None: |
28 | 38 | jdk_ver = os.environ['JAVA_HOME'].split('jdk-')[-1].split('.')[0] |
29 | 39 | 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 |
40 | 42 |
|
41 | 43 |
|
42 | 44 | 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) |
49 | 51 | 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) |
54 | 58 | get_workbench().set_option('general.environment', env_vars) |
55 | 59 |
|
56 | | - |
57 | 60 | class DownloadJDK(threading.Thread): |
58 | 61 | def __init__(self): |
59 | 62 | super().__init__() |
@@ -157,3 +160,4 @@ def install_jdk() -> None: |
157 | 160 | '''call this function from where this module is imported''' |
158 | 161 | if not jdk_install_exists(): |
159 | 162 | ui_utils.show_dialog(JdkDialog(get_workbench())) |
| 163 | + |
0 commit comments