Skip to content

Commit

Permalink
Fix venv issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuelopez-ansys committed Apr 26, 2024
1 parent ca66ccc commit 35631e4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 55 deletions.
31 changes: 21 additions & 10 deletions pyaedt/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -1737,21 +1737,34 @@ def add_custom_toolkit(self, toolkit_name, wheel_toolkit=None, install=True): #
)
)

self.logger.info(base_venv)

def run_command(command):
if is_windows:
command = '"{}"'.format(command)
ret_code = os.system(command)
return ret_code
try:
if is_linux: # pragma: no cover
process = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) # nosec
else:
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
ret_code = process.returncode
if ret_code != 0:
print("Error occurred:", stderr.decode("utf-8"))
return ret_code
except Exception as e:
print("Exception occurred:", str(e))
return 1 # Return non-zero exit code for indicating an error

version = self.odesktop.GetVersion()[2:6].replace(".", "")

if is_windows:
venv_dir = os.path.join(os.environ["APPDATA"], "pyaedt_env_ide", "toolkits_{}".format(version))
venv_dir = os.path.join(os.environ["APPDATA"], "pyaedt_env_ide", "toolkits_v{}".format(version))
python_exe = os.path.join(venv_dir, "Scripts", "python.exe")
pip_exe = os.path.join(venv_dir, "Scripts", "pip.exe")
package_dir = os.path.join(venv_dir, "Lib", "site-packages")
else:
venv_dir = os.path.join(os.environ["HOME"], "pyaedt_env_ide", "toolkits_{}".format(version))
venv_dir = os.path.join(os.environ["HOME"], "pyaedt_env_ide", "toolkits_v{}".format(version))
python_exe = os.path.join(venv_dir, "bin", "python")
pip_exe = os.path.join(venv_dir, "bin", "pip")
package_dir = os.path.join(venv_dir, "Lib", "site-packages")
Expand Down Expand Up @@ -1804,18 +1817,16 @@ def run_command(command):
)
elif install and not is_installed:
# Install the specified package
run_command('"{}" -m pip install --upgrade pip'.format(python_exe))
run_command('"{}" --default-timeout=1000 install {}'.format(pip_exe, toolkit["pip"]))
elif not install and is_installed:
# Uninstall toolkit
run_command('"{}" --default-timeout=1000 uninstall -y {}'.format(pip_exe, toolkit["pip"]))
run_command('"{}" --default-timeout=1000 uninstall -y {}'.format(pip_exe, toolkit["package_name"]))
elif install and is_installed:
# Update toolkit
run_command('"{}" --default-timeout=1000 install {} -U'.format(pip_exe, toolkit["pip"]))
else:
self.logger.info("Incorrect input".format(toolkit_name))
return

toolkit_dir = os.path.join(self.personallib, "Toolkits")
tool_dir = os.path.join(toolkit_dir, toolkit["installation_path"], toolkit_name)

Expand Down Expand Up @@ -1932,7 +1943,7 @@ def add_script_to_menu(
if not script_image:
script_image = os.path.join(os.path.dirname(__file__), "misc", "images", "large", "pyansys.png")
write_toolkit_config(os.path.join(toolkit_dir, product), lib_dir, toolkit_name, toolkit=script_image)
self.logger.info("{} toolkit installed.".format(toolkit_name))
self.logger.info("{} installed".format(toolkit_name))
return True

@pyaedt_function_handler()
Expand Down
1 change: 0 additions & 1 deletion pyaedt/misc/Run_Toolkit_Manager.py_build
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ else:
def main():
try:
# launch toolkit manager
version = oDesktop.GetVersion()[2:6].replace(".", "")
python_exe = r"##PYTHON_EXE##" % version
pyaedt_script = r"##TOOLKIT_MANAGER_SCRIPT##"
check_file(python_exe)
Expand Down
6 changes: 3 additions & 3 deletions pyaedt/misc/aedtlib_personalib_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def add_pyaedt_to_aedt(
student_version=is_student_version,
close_on_exit=close_on_exit,
) as d:
desktop = sys.modules["__main__"].oDesktop
pers1 = os.path.join(desktop.GetPersonalLibDirectory(), "pyaedt")
pid = desktop.GetProcessID()
# desktop = sys.modules["__main__"].oDesktop
pers1 = os.path.join(d.odesktop.GetPersonalLibDirectory(), "pyaedt")
pid = d.odesktop.GetProcessID()
# Linking pyaedt in PersonalLib for IronPython compatibility.
if os.path.exists(pers1):
d.logger.info("PersonalLib already mapped.")
Expand Down
117 changes: 76 additions & 41 deletions pyaedt/misc/toolkit_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
env_vars = ["PYAEDT_SCRIPT_VERSION", "PYAEDT_SCRIPT_PORT", "PYAEDT_STUDENT_VERSION"]
if all(var in os.environ for var in env_vars):
version = os.environ["PYAEDT_SCRIPT_VERSION"]
version = version[2:6].replace(".", "")
port = int(os.environ["PYAEDT_SCRIPT_PORT"])
student_version = False if os.environ["PYAEDT_STUDENT_VERSION"] == "False" else True
else:
version = None
version = "2024.1"
port = 0
student_version = False

if is_windows:
venv_dir = os.path.join(os.environ["APPDATA"], "pyaedt_env_ide", "toolkits_{}".format(version))
venv_dir = os.path.join(os.environ["APPDATA"], "pyaedt_env_ide", "toolkits_v{}".format(version))
python_exe = os.path.join(venv_dir, "Scripts", "python.exe")
package_dir = os.path.join(venv_dir, "Lib", "site-packages")

Expand Down Expand Up @@ -71,12 +72,16 @@ def create_toolkit_page(frame, open_source_toolkits):

def update_page(event=None):
selected_toolkit = toolkits_combo.get()
if is_toolkit_installed(selected_toolkit):
install_button.config(text="Update")
if selected_toolkit == "Custom":
install_button.config(text="Install")
uninstall_button.config(state="normal")
else:
install_button.config(text="Install")
uninstall_button.config(state="disabled")
if is_toolkit_installed(selected_toolkit):
install_button.config(text="Update")
uninstall_button.config(state="normal")
else:
install_button.config(text="Install")
uninstall_button.config(state="disabled")

if installation_option_action.get() == "Pip" and selected_toolkit != "Custom":
toolkit_name.config(state="disabled")
Expand All @@ -98,7 +103,7 @@ def update_page(event=None):

update_page()

return install_button, uninstall_button, installation_option_action, input_file, toolkits_combo
return install_button, uninstall_button, installation_option_action, input_file, toolkits_combo, toolkit_name


def is_toolkit_installed(toolkit_name):
Expand All @@ -113,18 +118,20 @@ def open_window(window, window_name, open_source_toolkits):
if not hasattr(window, "opened"):
window.opened = True
window.title(window_name)
install_button, uninstall_button, option_action, input_file, toolkits_combo = create_toolkit_page(
install_button, uninstall_button, option_action, input_file, toolkits_combo, toolkit_name = create_toolkit_page(
window, open_source_toolkits
)
root.minsize(500, 250)
return install_button, uninstall_button, option_action, input_file, toolkits_combo
return install_button, uninstall_button, option_action, input_file, toolkits_combo, toolkit_name
else:
window.deiconify()


def __get_command_function(is_install, option_action, input_file, toolkits_combo, install_button, uninstall_button):
def __get_command_function(
is_install, toolkit_level, input_file, toolkits_combo, toolkit_name, install_button, uninstall_button
):
return lambda: button_is_clicked(
is_install, option_action, input_file, toolkits_combo, install_button, uninstall_button
is_install, toolkit_level, input_file, toolkits_combo, toolkit_name, install_button, uninstall_button
)


Expand All @@ -136,60 +143,88 @@ def toolkit_window(toolkit_level="Project"):
if toolkit_info["installation_path"].lower() == toolkit_level.lower():
open_source_toolkits.append(toolkit_name)
toolkit_window_var.minsize(250, 150)
install_button, uninstall_button, option_action, input_file, toolkits_combo = open_window(
install_button, uninstall_button, option_action, input_file, toolkits_combo, toolkit_name = open_window(
toolkit_window_var, toolkit_level, open_source_toolkits
)

install_command = __get_command_function(
True, option_action, input_file, toolkits_combo, install_button, uninstall_button
True, toolkit_level, input_file, toolkits_combo, toolkit_name, install_button, uninstall_button
)
uninstall_command = __get_command_function(
False, option_action, input_file, toolkits_combo, install_button, uninstall_button
False, toolkit_level, input_file, toolkits_combo, toolkit_name, install_button, uninstall_button
)

install_button.configure(command=install_command)
uninstall_button.configure(command=uninstall_command)


def button_is_clicked(install_action, option_action, input_file, combo_toolkits, install_button, uninstall_button):
def button_is_clicked(
install_action, toolkit_level, input_file, combo_toolkits, toolkit_name, install_button, uninstall_button
):
"""Install/Uninstall button action"""
installation_option = option_action.get()
# installation_option = option_action.get()
file = input_file.get()
toolkit_name = combo_toolkits.get()

if toolkit_name != "Custom":
desktop = Desktop(
specified_version=version,
port=port,
new_desktop_session=False,
non_graphical=False,
close_on_exit=False,
student_version=student_version,
)
selected_toolkit_name = combo_toolkits.get()
name = toolkit_name.get()

desktop = Desktop(
specified_version=version,
port=port,
new_desktop_session=False,
non_graphical=False,
close_on_exit=False,
student_version=student_version,
)

desktop.odesktop.CloseAllWindows()

if is_toolkit_installed(toolkit_name) and install_action:
desktop.logger.info(f"Updating {toolkit_name}.")
desktop.add_custom_toolkit(toolkit_name, file)
if selected_toolkit_name != "Custom":
desktop.logger.info("TEST: VENV {}".format(venv_dir))
if is_toolkit_installed(selected_toolkit_name) and install_action:
desktop.logger.info("Updating {}".format(selected_toolkit_name))
desktop.add_custom_toolkit(selected_toolkit_name, file)
install_button.config(text="Update")
uninstall_button.config(state="normal")
desktop.logger.info(f"{toolkit_name} updated")
desktop.logger.info("{} updated".format(selected_toolkit_name))
elif install_action:
desktop.logger.info(f"Installing {toolkit_name}.")
desktop.add_custom_toolkit(toolkit_name, file)
desktop.logger.info("Installing {}".format(selected_toolkit_name))
desktop.add_custom_toolkit(selected_toolkit_name, file)
install_button.config(text="Update")
uninstall_button.config(state="normal")
desktop.logger.info(f"{toolkit_name} installed.")
elif is_toolkit_installed(toolkit_name) and not install_action:
desktop.logger.info(f"Uninstalling {toolkit_name}")
desktop.add_custom_toolkit(toolkit_name, install=False)
elif is_toolkit_installed(selected_toolkit_name) and not install_action:
desktop.logger.info("Uninstalling {}".format(selected_toolkit_name))
desktop.add_custom_toolkit(selected_toolkit_name, install=False)
install_button.config(text="Install")
uninstall_button.config(state="disabled")
desktop.logger.info(f"{toolkit_name} uninstalled")
desktop.logger.info("{} uninstalled".format(selected_toolkit_name))
else:
desktop.logger.info("{} not installed".format(selected_toolkit_name))

else:
if install_action:
desktop.logger.info("Install {}".format(name))
if is_windows:
pyaedt_venv_dir = os.path.join(os.environ["APPDATA"], "pyaedt_env_ide", "v{}".format(version))
executable_interpreter = os.path.join(pyaedt_venv_dir, "Scripts", "python.exe")
else:
pyaedt_venv_dir = os.path.join(os.environ["HOME"], "pyaedt_env_ide", "v{}".format(version))
executable_interpreter = os.path.join(pyaedt_venv_dir, "bin", "python")
if os.path.isfile(executable_interpreter):
desktop.add_script_to_menu(
toolkit_name=name,
script_path=file,
product=toolkit_level,
executable_interpreter=executable_interpreter,
)
else:
desktop.logger.info("PyAEDT environment is not installed")
else:
desktop.logger.info(f"{toolkit_name} not installed.")
desktop.logger.info("Uninstall {}".format(name))
desktop.remove_script_from_menu(name, product=toolkit_level)

desktop.odesktop.RefreshToolkitUI()
desktop.release_desktop(False, False)
desktop.odesktop.CloseAllWindows()
desktop.odesktop.RefreshToolkitUI()
desktop.release_desktop(False, False)


root = tk.Tk()
Expand Down

0 comments on commit 35631e4

Please sign in to comment.