|
1 |
| -import xml.etree.ElementTree as ET |
2 | 1 | import os
|
| 2 | +import pathlib |
3 | 3 | import subprocess
|
4 | 4 | import platform
|
5 | 5 | import shutil
|
| 6 | +import tkinter as tk |
| 7 | +from tkinter import messagebox, filedialog |
6 | 8 |
|
7 | 9 |
|
8 |
| -def check_parallel(path) -> bool: |
| 10 | +def find_workbench() -> pathlib.Path: |
9 | 11 | """
|
10 |
| - Checks if a ravenframework input file uses parallel processing. This poses a problem for the executable |
11 |
| - on MacOS and possibly Linux. |
12 |
| -
|
13 |
| - @In, path, str, path to the input file |
14 |
| - @Out, is_parallel, bool, True if parallel processing is used, False otherwise |
| 12 | + Finds the NEAMS Workbench executable. A ".workbench" file in the FORCE app's main directory tracks |
| 13 | + the location of the Workbench executable. If that file doesn't exist, we look in common directories |
| 14 | + to find Workbench ourselves. If we still can't find it, we ask the user to locate it manually, if |
| 15 | + desired. |
15 | 16 | """
|
16 |
| - is_parallel = False |
17 |
| - |
18 |
| - tree = ET.parse(path) |
19 |
| - tree.find() |
20 |
| - |
21 |
| - return is_parallel |
| 17 | + workbench_path = None |
22 | 18 |
|
| 19 | + # Check if a ".workbench" file exists in the main build directory (same directory as the heron and |
| 20 | + # raven_framework executables). That should be 2 directories up from the directory of this file. |
| 21 | + current_file_dir = pathlib.Path(__file__).parent |
| 22 | + workbench_file = current_file_dir.parent.parent / ".workbench" |
| 23 | + workbench_file_exists = workbench_file.exists() |
| 24 | + if workbench_file_exists: |
| 25 | + with open(workbench_file, 'r') as f: |
| 26 | + workbench_path = f.read().strip() |
| 27 | + if not os.path.exists(workbench_path): |
| 28 | + # If the path in the .workbench file is invalid, delete the file so we don't keep trying |
| 29 | + # to use it. |
| 30 | + workbench_path = None |
| 31 | + os.remove(workbench_file) |
| 32 | + else: |
| 33 | + return workbench_path |
23 | 34 |
|
24 |
| -def find_workbench(): |
25 |
| - """ |
26 |
| - Finds the NEAMS Workbench executable |
27 |
| - """ |
28 |
| - workbench_path = None |
| 35 | + # If that .workbench file doesn't exist, we can look around for the Workbench executable |
29 | 36 | if platform.system() == "Windows":
|
30 |
| - if os.environ.get('WORKBENCH_PATH'): |
31 |
| - workbench_path = os.environ['WORKBENCH_PATH'] |
| 37 | + if wb_path := os.environ.get('WORKBENCH_PATH', None): |
| 38 | + workbench_path = wb_path |
| 39 | + elif wb_path := shutil.which('Workbench'): |
| 40 | + workbench_path = wb_path |
32 | 41 | else:
|
33 |
| - workbench_path = shutil.which('workbench') |
| 42 | + # Manually search through a few common directories for the Workbench installation |
| 43 | + for path in ["$HOMEDRIVE\\", "$PROGRAMFILES", "$HOME", "$APPDATA", "$LOCALAPPDATA"]: |
| 44 | + path = os.path.expandvars(path) |
| 45 | + if not os.path.exists(path): |
| 46 | + continue |
| 47 | + for file in os.listdir(path): |
| 48 | + if file.startswith("Workbench"): |
| 49 | + wb_path = os.path.join(path, file, "bin", "Workbench.exe") |
| 50 | + if os.path.exists(wb_path): |
| 51 | + workbench_path = wb_path |
| 52 | + break |
34 | 53 | elif platform.system() == "Darwin":
|
35 | 54 | # Look in the /Applications directory for a directory starting with "Workbench"
|
36 | 55 | for app in os.listdir("/Applications"):
|
37 | 56 | if app.startswith("Workbench") and os.path:
|
38 | 57 | workbench_path = os.path.join("/Applications", app, "Contents/MacOS/Workbench")
|
39 | 58 | break
|
40 |
| - else: |
41 |
| - print("ERROR: Could not find the NEAMS Workbench application in the /Applications directory. " |
42 |
| - "Has Workbench been installed?") |
43 |
| - else: # Linux, not yet supported |
44 |
| - print("Automatic connection of FORCE tools to the NEAMS Workbench is not yet supported on Linux.") |
| 59 | + |
| 60 | + # If we still haven't found Workbench, let the user help us out. Throw up a tkinter warning dialog to |
| 61 | + # ask the user to locate the Workbench executable. |
| 62 | + if workbench_path is None: |
| 63 | + root = tk.Tk() |
| 64 | + root.withdraw() |
| 65 | + |
| 66 | + response = messagebox.askyesno( |
| 67 | + "NEAMS Workbench could not be found!", |
| 68 | + "The NEAMS Workbench executable could not be found. Would you like to locate it manually?" |
| 69 | + ) |
| 70 | + if response: |
| 71 | + workbench_path = filedialog.askopenfilename( |
| 72 | + title="Locate NEAMS Workbench", |
| 73 | + filetypes=[("Workbench Executable", "*.exe")] |
| 74 | + ) |
| 75 | + if workbench_path: |
| 76 | + with open(workbench_file, 'w') as f: |
| 77 | + f.write(workbench_path) |
| 78 | + |
| 79 | + if isinstance(workbench_path, str): |
| 80 | + workbench_path = pathlib.Path(workbench_path) |
| 81 | + |
45 | 82 | return workbench_path
|
46 | 83 |
|
47 | 84 |
|
| 85 | +def create_workbench_heron_default(workbench_path: pathlib.Path): |
| 86 | + """ |
| 87 | + Creates a configuration file for Workbench so it knows where HERON is located. |
| 88 | + @ In, workbench_path, pathlib.Path, the path to the NEAMS Workbench executable |
| 89 | + """ |
| 90 | + # First, we need to find the HERON executable. This will be "heron.exe" on Windows |
| 91 | + # and just "heron" on MacOS and Linux. It should be located 2 directories up from the |
| 92 | + # directory of this file. |
| 93 | + current_file_dir = pathlib.Path(__file__).parent |
| 94 | + heron_path = current_file_dir.parent.parent / "heron" |
| 95 | + if platform.system() == "Windows": |
| 96 | + heron_path = heron_path.with_suffix(".exe") |
| 97 | + # If the HERON executable doesn't exist, we can't create the Workbench configuration file |
| 98 | + if not heron_path.exists(): |
| 99 | + print(f"ERROR: Could not find the HERON executable in the directory {heron_path.parent}.") |
| 100 | + return |
| 101 | + |
| 102 | + # Create the configuration file for Workbench |
| 103 | + workbench_root_dir = workbench_path.parent.parent |
| 104 | + workbench_config_file = workbench_root_dir / "default.apps.son" |
| 105 | + if workbench_config_file.exists(): |
| 106 | + # File already exists, but does a HERON entry already exist? See if HERON mentioned in the |
| 107 | + # file. This isn't as robust as actually parsing the file, but it should work for now. |
| 108 | + with open(workbench_config_file, 'r') as f: |
| 109 | + for line in f: |
| 110 | + if "heron" in line.lower(): |
| 111 | + return |
| 112 | + # If the file doesn't exist or doesn't mention HERON, we need to add it |
| 113 | + print("Adding HERON configuration to NEAMS Workbench", workbench_config_file) |
| 114 | + with open(workbench_config_file, 'a') as f: |
| 115 | + f.write("\napplications {\n" |
| 116 | + " HERON {\n" |
| 117 | + " configurations {\n" |
| 118 | + " default {\n" |
| 119 | + " options {\n" |
| 120 | + " shared {\n" |
| 121 | + f" \"Executable\"=\"{heron_path}\"\n" |
| 122 | + " }\n" |
| 123 | + " }\n" |
| 124 | + " }\n" |
| 125 | + " }\n" |
| 126 | + " }\n" |
| 127 | + "}\n") |
| 128 | + |
| 129 | + |
48 | 130 | def run_in_workbench(file: str | None = None):
|
49 | 131 | """
|
50 | 132 | Opens the given file in the NEAMS Workbench
|
51 | 133 | @ In, file, str, optional, the file to open in NEAMS Workbench
|
52 | 134 | """
|
53 |
| - # Find the workbench executable |
| 135 | + # Find the Workbench executable |
54 | 136 | workbench_path = find_workbench()
|
55 | 137 | if workbench_path is None:
|
56 | 138 | print("ERROR: Could not find the NEAMS Workbench executable. Please set the "
|
57 | 139 | "WORKBENCH_PATH environment variable or add it to the system path.")
|
58 | 140 | raise RuntimeError
|
59 | 141 |
|
60 |
| - # Open the file in the workbench |
61 |
| - command = workbench_path |
| 142 | + # Point Workbench to the HERON executable if it's not already configured |
| 143 | + create_workbench_heron_default(workbench_path) |
| 144 | + |
| 145 | + # Open the file in Workbench |
62 | 146 | # Currently, we're only able to open the MacOS version of Workbench by opening the app itself.
|
63 | 147 | # This does not accept a file as an argument, so users will need to open Workbench, then open
|
64 | 148 | # the desired file manually from within the app.
|
| 149 | + command = str(workbench_path) |
65 | 150 | if file is not None and platform.system() == "Windows":
|
66 | 151 | command += ' ' + file
|
67 | 152 |
|
|
0 commit comments