diff --git a/doc/source/Resources/pyaedt_installer_from_aedt.py b/doc/source/Resources/pyaedt_installer_from_aedt.py index 8836d21d080..f92cffe78a6 100644 --- a/doc/source/Resources/pyaedt_installer_from_aedt.py +++ b/doc/source/Resources/pyaedt_installer_from_aedt.py @@ -28,26 +28,25 @@ import shutil import sys +try: + import subprocess +except ImportError: + import subprocessdotnet as subprocess + is_iron_python = platform.python_implementation().lower() == "ironpython" is_linux = os.name == "posix" is_windows = not is_linux -VENV_DIR_PREFIX = ".pyaedt_env" - - -def disclaimer(): - """Notify users about extra packages.""" - DISCLAIMER = ( - "This script will download and install certain third-party software and/or " - "open-source software (collectively, 'Third-Party Software'). Such Third-Party " - "Software is subject to separate terms and conditions and not the terms of your " - "Ansys software license agreement. Ansys does not warrant or support such " - "Third-Party Software." - ) - print(DISCLAIMER) - response = input("Do you want to proceed ? (y/n)").strip().lower() - return response == "y" +VENV_DIR_PREFIX = ".pyaedt_env" +DISCLAIMER = ( + "This script will download and install certain third-party software and/or " + "open-source software (collectively, 'Third-Party Software'). Such Third-Party " + "Software is subject to separate terms and conditions and not the terms of your " + "Ansys software license agreement. Ansys does not warrant or support such " + "Third-Party Software.\n" + "Do you want to proceed ?" +) def run_pyinstaller_from_c_python(oDesktop): @@ -75,7 +74,7 @@ def run_pyinstaller_from_c_python(oDesktop): # Launch this script again from the CPython interpreter. This calls the ``install_pyaedt()`` method, # which creates a virtual environment and installs PyAEDT and its dependencies - command = ['"{}"'.format(python_exe), '"{}"'.format(os.path.normpath(__file__)), "--version=" + version] + command = [python_exe, os.path.normpath(__file__), "--version=" + version] if is_student_version(oDesktop): command.append("--student") @@ -86,19 +85,11 @@ def run_pyinstaller_from_c_python(oDesktop): command.extend(['--wheel="{}"'.format(wheelpyaedt)]) oDesktop.AddMessage("", "", 0, "Installing PyAEDT.") - if is_windows: - import subprocess - - process = subprocess.Popen(" ".join(command)) - process.wait() - return_code = process.returncode - err_msg = "There was an error while installing PyAEDT." - else: - return_code = run_command(" ".join(command)) - err_msg = ( - "There was an error while installing PyAEDT. Refer to the Terminal window where AEDT was launched " "from." - ) + return_code = subprocess.call(command) + err_msg = "There was an error while installing PyAEDT." + if is_linux: + err_msg += " Refer to the Terminal window where AEDT was launched from." if str(return_code) != "0": oDesktop.AddMessage("", "", 2, err_msg) return @@ -148,7 +139,7 @@ def run_pyinstaller_from_c_python(oDesktop): command = r'"{}" "{}"'.format(python_exe, python_script) oDesktop.AddMessage("", "", 0, "Configuring PyAEDT panels in automation tab.") - ret_code = os.system(command) + ret_code = subprocess.call([python_exe, python_script]) if ret_code != 0: oDesktop.AddMessage("", "", 2, "Error occurred configuring the PyAEDT panels.") return @@ -223,16 +214,12 @@ def install_pyaedt(): args.edt_root, args.python_version.replace(".", "_") ) - response = disclaimer() - if not response: - exit(1) - if not os.path.exists(venv_dir): if args.version == "231": - run_command('"{}" -m venv "{}" --system-site-packages'.format(sys.executable, venv_dir)) + subprocess.call([sys.executable, "-m", "venv", venv_dir, "--system-site-packages"]) else: - run_command('"{}" -m venv "{}"'.format(sys.executable, venv_dir)) + subprocess.call([sys.executable, "-m", "venv", venv_dir]) if args.wheel and os.path.exists(args.wheel): wheel_pyaedt = args.wheel @@ -251,33 +238,44 @@ def install_pyaedt(): # Extracted folder. unzipped_path = wheel_pyaedt if args.version <= "231": - run_command( - '"{}" install --no-cache-dir --no-index --find-links={} pyaedt[all,dotnet]'.format( - pip_exe, unzipped_path - ) ) + subprocess.call( + [ + pip_exe, + "install", + "--no-cache-dir", + "--no-index", + "--find-links={}".format(unzipped_path), + "pyaedt[all,dotnet]", + ] + ) else: - run_command( - '"{}" install --no-cache-dir --no-index --find-links={} pyaedt[installer]'.format( - pip_exe, unzipped_path - ) + subprocess.call( + [ + pip_exe, + "install", + "--no-cache-dir", + "--no-index", + "--find-links={}".format(unzipped_path), + "pyaedt[installer]", + ] ) else: - run_command('"{}" -m pip install --upgrade pip'.format(python_exe)) - run_command('"{}" --default-timeout=1000 install wheel'.format(pip_exe)) + subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "wheel"]) if args.version <= "231": - run_command('"{}" --default-timeout=1000 install pyaedt[all]=="0.9.0"'.format(pip_exe)) - run_command('"{}" --default-timeout=1000 install jupyterlab'.format(pip_exe)) - run_command('"{}" --default-timeout=1000 install ipython -U'.format(pip_exe)) - run_command('"{}" --default-timeout=1000 install ipyvtklink'.format(pip_exe)) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "pyaedt[all]=='0.9.0'"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "jupyterlab"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "ipython", "-U"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "ipyvtklink"]) else: - run_command('"{}" --default-timeout=1000 install pyaedt[installer]'.format(pip_exe)) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "pyaedt[installer]"]) if args.version == "231": - run_command('"{}" uninstall -y pywin32'.format(pip_exe)) + subprocess.call([pip_exe, "uninstall", "-y", "pywin32"]) else: - run_command('"{}" uninstall --yes pyaedt'.format(pip_exe)) + subprocess.call([pip_exe, "uninstall", "-y", "pyaedt"]) if args.wheel and os.path.exists(args.wheel): wheel_pyaedt = args.wheel @@ -292,25 +290,35 @@ def install_pyaedt(): # Extract all contents to a directory. (You can specify a different extraction path if needed.) zip_ref.extractall(unzipped_path) if args.version <= "231": - run_command( - '"{}" install --no-cache-dir --no-index --find-links={} pyaedt[all]=="0.9.0"'.format( - pip_exe, unzipped_path - ) + subprocess.call( + [ + pip_exe, + "install", + "--no-cache-dir", + "--no-index", + "--find-links={}".format(unzipped_path), + "pyaedt[all]=='0.9.0'", + ] ) else: - run_command( - '"{}" install --no-cache-dir --no-index --find-links={} pyaedt[installer]'.format( - pip_exe, unzipped_path - ) + subprocess.call( + [ + pip_exe, + "install", + "--no-cache-dir", + "--no-index", + "--find-links={}".format(unzipped_path), + "pyaedt[installer]", + ] ) else: if args.version <= "231": - run_command('"{}" --default-timeout=1000 install pyaedt[all]=="0.9.0"'.format(pip_exe)) - run_command('"{}" --default-timeout=1000 install jupyterlab'.format(pip_exe)) - run_command('"{}" --default-timeout=1000 install ipython -U'.format(pip_exe)) - run_command('"{}" --default-timeout=1000 install ipyvtklink'.format(pip_exe)) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "pyaedt[all]=='0.9.0'"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "jupyterlab"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "ipython", "-U"]) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "ipyvtklink"]) else: - run_command('"{}" --default-timeout=1000 install pyaedt[installer]'.format(pip_exe)) + subprocess.call([pip_exe, "--default-timeout=1000", "install", "pyaedt[installer]"]) sys.exit(0) @@ -322,24 +330,34 @@ def is_student_version(oDesktop): return False -def run_command(command): - if is_windows: - command = '"{}"'.format(command) - ret_code = os.system(command) - return ret_code +def validate_disclaimer(): + """Display dialog box and evaluate the response to the disclaimer.""" + from System.Windows.Forms import DialogResult + from System.Windows.Forms import MessageBox + from System.Windows.Forms import MessageBoxButtons + + response = MessageBox.Show(DISCLAIMER, "Disclaimer", MessageBoxButtons.YesNo) + return response == DialogResult.Yes if __name__ == "__main__": if is_iron_python: - # Check if wheelhouse defined. Wheelhouse is created for Windows only. - wheelpyaedt = [] - # Retrieve the script arguments - script_args = ScriptArgument.split() - if len(script_args) == 1: - wheelpyaedt = script_args[0] - if not os.path.exists(wheelpyaedt): - wheelpyaedt = [] - run_pyinstaller_from_c_python(oDesktop) + if "GetIsNonGraphical" in oDesktop.__dir__() and oDesktop.GetIsNonGraphical(): + print("When using IronPython, this script is expected to be run in graphical mode.") + sys.exit(1) + if validate_disclaimer(): + oDesktop.AddMessage("", "", 0, "Disclaimer accepted.") + # Check if wheelhouse defined. Wheelhouse is created for Windows only. + wheelpyaedt = [] + # Retrieve the script arguments + script_args = ScriptArgument.split() + if len(script_args) == 1: + wheelpyaedt = script_args[0] + if not os.path.exists(wheelpyaedt): + wheelpyaedt = [] + run_pyinstaller_from_c_python(oDesktop) + else: + oDesktop.AddMessage("", "", 1, "Disclaimer refused, installation canceled.") else: install_pyaedt() diff --git a/src/ansys/aedt/core/workflows/installer/extension_manager.py b/src/ansys/aedt/core/workflows/installer/extension_manager.py index d5a52976290..7435a06562f 100644 --- a/src/ansys/aedt/core/workflows/installer/extension_manager.py +++ b/src/ansys/aedt/core/workflows/installer/extension_manager.py @@ -318,36 +318,6 @@ def button_is_clicked( desktop.release_desktop(False, False) -def close_widget(widget): - """Close specific widget.""" - widget.destroy() - - -def create_disclaimer_window(root: tk.Tk): - """Notify users about extra packages.""" - DISCLAIMER = ( - "The extension manager will download and install certain third-party software and/or " - "open-source software (collectively, 'Third-Party Software'). Such Third-Party " - "Software is subject to separate terms and conditions and not the terms of your " - "Ansys software license agreement. Ansys does not warrant or support such " - "Third-Party Software. Do you still wish to proceed?" - ) - - disclaimer_window = tk.Toplevel(root) - disclaimer_window.title("Disclaimer") - disclaimer_window.grab_set() - disclaimer_window.protocol("WM_DELETE_WINDOW", lambda: None) - disclaimer_window.transient(root) - label = tk.Label(disclaimer_window, text=DISCLAIMER, wraplength=275) - label.pack() - yes_button = tk.Button(disclaimer_window, text="Yes", command=lambda: close_widget(disclaimer_window)) - yes_button.pack(side=tk.LEFT, padx=50, pady=10) - no_button = tk.Button(disclaimer_window, text="No", command=lambda: close_widget(root)) - no_button.pack(side=tk.RIGHT, padx=50, pady=10) - - return disclaimer_window - - root = tk.Tk() root.title("Extension Manager") @@ -390,9 +360,6 @@ def create_disclaimer_window(root: tk.Tk): root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}") -disclaimer_window = create_disclaimer_window(root) -disclaimer_window.geometry("300x170+{}+{}".format(x_position + 110, y_position + 45)) - # Create buttons in a 4x4 grid, centered for i, level in enumerate(toolkit_levels): row_num = i // 4 diff --git a/src/ansys/aedt/core/workflows/templates/pyaedt_utils.py b/src/ansys/aedt/core/workflows/templates/pyaedt_utils.py index 8f65431a8c8..22d1f7b1d81 100644 --- a/src/ansys/aedt/core/workflows/templates/pyaedt_utils.py +++ b/src/ansys/aedt/core/workflows/templates/pyaedt_utils.py @@ -31,6 +31,7 @@ import string import sys +from System.Windows.Forms import DialogResult from System.Windows.Forms import MessageBox from System.Windows.Forms import MessageBoxButtons from System.Windows.Forms import MessageBoxIcon @@ -147,3 +148,18 @@ def generate_unique_name(root_name, suffix="", n=6): if suffix: unique_name += suffix return unique_name + + +def validate_disclaimer(): + """Display dialog box and evaluate the response to the disclaimer.""" + DISCLAIMER = ( + "This script will download and install certain third-party software and/or " + "open-source software (collectively, 'Third-Party Software'). Such Third-Party " + "Software is subject to separate terms and conditions and not the terms of your " + "Ansys software license agreement. Ansys does not warrant or support such " + "Third-Party Software.\n" + "Do you want to proceed ?" + ) + + response = MessageBox.Show(DISCLAIMER, "Disclaimer", MessageBoxButtons.YesNo) + return response == DialogResult.Yes diff --git a/src/ansys/aedt/core/workflows/templates/run_extension_manager.py_build b/src/ansys/aedt/core/workflows/templates/run_extension_manager.py_build index 38811b98fcd..8ec46c8c71f 100644 --- a/src/ansys/aedt/core/workflows/templates/run_extension_manager.py_build +++ b/src/ansys/aedt/core/workflows/templates/run_extension_manager.py_build @@ -44,45 +44,49 @@ import pyaedt_utils def main(): - try: - # Get AEDT version - version_short = oDesktop.GetVersion()[2:6].replace(".", "") - # Launch extension manager - python_exe = r"##PYTHON_EXE##" % version - # Extensions directory - current_dir = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) - pyaedt_extensions_dir = os.path.normpath(os.path.join(current_dir, r"##TOOLKIT_REL_LIB_DIR##")) - pyaedt_script = os.path.join(pyaedt_extensions_dir, "extension_manager.py") - # Check if CPython interpreter and AEDT release match - python_exe = pyaedt_utils.sanitize_interpreter_path(python_exe, version_short) - # Check python executable - python_exe_flag = pyaedt_utils.check_file(python_exe, oDesktop) - if not python_exe_flag: - return - # Check script file - pyaedt_script_flag = pyaedt_utils.check_file(pyaedt_script, oDesktop) - if not pyaedt_script_flag: - return - # Add environment variables - pyaedt_utils.environment_variables(oDesktop) - # Open extension manager - if is_linux: - pyaedt_utils.set_ansys_em_environment(oDesktop) - command = [ - python_exe, - pyaedt_script, - ] - my_env = os.environ.copy() - subprocess.Popen(command, env=my_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) - else: - command = [ - '"{}"'.format(python_exe), - '"{}"'.format(pyaedt_script), - ] - my_env = os.environ.copy() - subprocess.Popen(" ".join(command), env=my_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) - except Exception as e: - pyaedt_utils.show_error(str(e), oDesktop) + if pyaedt_utils.validate_disclaimer(): + oDesktop.AddMessage("", "", 0, "Disclaimer accepted.") + try: + # Get AEDT version + version_short = oDesktop.GetVersion()[2:6].replace(".", "") + # Launch extension manager + python_exe = r"C:\Users\smorais\AppData\Roaming\.pyaedt_env\3_10\Scripts\python.exe" + # Extensions directory + current_dir = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) + pyaedt_extensions_dir = os.path.normpath(os.path.join(current_dir, r"Lib")) + pyaedt_script = os.path.join(pyaedt_extensions_dir, "extension_manager.py") + # Check if CPython interpreter and AEDT release match + python_exe = pyaedt_utils.sanitize_interpreter_path(python_exe, version_short) + # Check python executable + python_exe_flag = pyaedt_utils.check_file(python_exe, oDesktop) + if not python_exe_flag: + return + # Check script file + pyaedt_script_flag = pyaedt_utils.check_file(pyaedt_script, oDesktop) + if not pyaedt_script_flag: + return + # Add environment variables + pyaedt_utils.environment_variables(oDesktop) + # Open extension manager + if is_linux: + pyaedt_utils.set_ansys_em_environment(oDesktop) + command = [ + python_exe, + pyaedt_script, + ] + my_env = os.environ.copy() + subprocess.Popen(command, env=my_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) + else: + command = [ + '"{}"'.format(python_exe), + '"{}"'.format(pyaedt_script), + ] + my_env = os.environ.copy() + subprocess.Popen(" ".join(command), env=my_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) + except Exception as e: + pyaedt_utils.show_error(str(e), oDesktop) + else: + oDesktop.AddMessage("", "", 1, "Disclaimer refused.") if __name__ == "__main__":