Skip to content

Commit

Permalink
FIX: Fix hardcoded APPDATA in extension manager (#5300)
Browse files Browse the repository at this point in the history
Co-authored-by: Sébastien Morais <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 457def8 commit 465935b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 8 deletions.
9 changes: 9 additions & 0 deletions doc/source/Getting_started/Installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ Finally, in the python console, run the following commands:
from ansys.aedt.core.workflows.installer.pyaedt_installer import add_pyaedt_to_aedt
add_pyaedt_to_aedt(“your_aedt_version", r“path_to_personalib")
You can also install the PyAEDT panels using the following steps, this is also useful if you have a centralized PyAEDT installation:

- Download the following file: :download:`PyAEDT panel Installer Python file <../Resources/toolkit_installer_from_aedt.py>`

- Define an environment variable called `PYAEDT_INTERPRETER` with the path of the python interpreter in which PyAEDT is installed.

- Open an Electronics Desktop Session and click on Tools->Run Script and execute the file. You do not need the previous step if
you pass as an argument the path of the python interpreter.


Linux support
~~~~~~~~~~~~~
Expand Down
100 changes: 100 additions & 0 deletions doc/source/Resources/toolkit_installer_from_aedt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import sys
import subprocessdotnet as subprocess

# This script installs PyAEDT tabs (PyAEDT Console, Jupyter, Run Script and Extension Manager)
# using a specific Python interpreter.
# It can be passed from an environment variable called "PYAEDT_INTERPRETER" or from the input argument.
# The environment variable has more priority.

is_linux = os.name == "posix"
is_windows = not is_linux

pyaedt_enviroment_variable = "PYAEDT_INTERPRETER"


def run_pyinstaller_from_c_python(oDesktop, pyaedt_interpreter):
# Iron Python script to create PyAEDT panels

# Get AEDT information
version = oDesktop.GetVersion()[2:6].replace(".", "")

# Add PyAEDT tabs in AEDT

# Create Toolkits in PersonalLib
import tempfile
python_script = os.path.join(tempfile.gettempdir(), "configure_pyaedt.py")
if os.path.isfile(python_script):
os.remove(python_script)
with open(python_script, "w") as f:
f.write("from ansys.aedt.core.workflows.installer.pyaedt_installer import add_pyaedt_to_aedt\n")
f.write(
'add_pyaedt_to_aedt(aedt_version="{}", personal_lib=r"{}")\n'.format(
oDesktop.GetVersion()[:6], oDesktop.GetPersonalLibDirectory()))

command = [pyaedt_interpreter, python_script]
oDesktop.AddMessage("", "", 0, "Configuring PyAEDT panels in automation tab.")
process = subprocess.Popen(command)
process.wait()

# Refresh UI
oDesktop.CloseAllWindows()
if version >= "232":
oDesktop.RefreshToolkitUI()
msg = "PyAEDT configuration complete."
if is_linux:
msg += " Please ensure Ansys Electronics Desktop is launched in gRPC mode (i.e. launch ansysedt with -grpcsrv" \
" argument) to take advantage of the new toolkits."

if "GetIsNonGraphical" in oDesktop.__dir__() and not oDesktop.GetIsNonGraphical():
from System.Windows.Forms import MessageBox, MessageBoxButtons, MessageBoxIcon
oDesktop.AddMessage("", "", 0, msg)
MessageBox.Show(msg, 'Info', MessageBoxButtons.OK, MessageBoxIcon.Information)
oDesktop.AddMessage("", "", 0, "Create a project if the PyAEDT panel is not visible.")


if __name__ == "__main__":

python_interpreter = os.getenv(pyaedt_enviroment_variable)
if python_interpreter and not os.path.exists(python_interpreter):
oDesktop.AddMessage("", "", 2, "Python environment does not exist.")
sys.exit()

# Check if interpreter path is defined.
# Retrieve the script arguments
script_args = ScriptArgument.split()
if len(script_args) == 1 and not python_interpreter:
python_interpreter = script_args[0]
if not os.path.exists(python_interpreter):
oDesktop.AddMessage("", "", 2, "Python environment does not exist.")
sys.exit()

if not python_interpreter:
oDesktop.AddMessage("", "", 2, "Invalid python environment.")
sys.exit()

run_pyinstaller_from_c_python(oDesktop, python_interpreter)
13 changes: 5 additions & 8 deletions src/ansys/aedt/core/workflows/installer/extension_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# SOFTWARE.

import os
import sys
import tkinter as tk
from tkinter import ttk

Expand Down Expand Up @@ -54,13 +55,10 @@
venv_dir = os.path.join(os.environ["APPDATA"], VENV_DIR_PREFIX, f"toolkits_{python_version.replace('.', '_')}")
python_exe = os.path.join(venv_dir, "Scripts", "python.exe")
package_dir = os.path.join(venv_dir, "Lib", "site-packages")
pyaedt_venv_dir = os.path.join(os.environ["APPDATA"], VENV_DIR_PREFIX, f"{python_version.replace('.', '_')}")

else:
venv_dir = os.path.join(os.environ["HOME"], VENV_DIR_PREFIX, f"toolkits_{python_version.replace('.', '_')}")
python_exe = os.path.join(venv_dir, "bin", "python")
package_dir = os.path.join(venv_dir, "lib", "site-packages")
pyaedt_venv_dir = os.path.join(os.environ["HOME"], VENV_DIR_PREFIX, f"{python_version.replace('.', '_')}")


def create_toolkit_page(frame, window_name, internal_toolkits):
Expand Down Expand Up @@ -281,11 +279,10 @@ def button_is_clicked(

else:
if install_action:
desktop.logger.info(f"Install {name}")
if is_windows:
executable_interpreter = os.path.join(pyaedt_venv_dir, "Scripts", "python.exe")
else:
executable_interpreter = os.path.join(pyaedt_venv_dir, "bin", "python")
desktop.logger.info("Install {}".format(name))

executable_interpreter = sys.executable

if not file:
file = os.path.join(
os.path.dirname(ansys.aedt.core.workflows.templates.__file__), "extension_template.py"
Expand Down

0 comments on commit 465935b

Please sign in to comment.