create utils file with get_python_exe

This commit is contained in:
Pesc0
2023-07-11 15:56:10 +02:00
parent c608c77d2b
commit 0e6ec6e3ac
4 changed files with 115 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ endif()
configure_file(__init__.py.template ${NAMESPACE_INIT})
configure_file(project_utility.py ${NAMESPACE_DIR}/project_utility.py)
configure_file(UiTools.py ${NAMESPACE_DIR}/UiTools.py)
configure_file(utils.py ${NAMESPACE_DIR}/utils.py)
if (INSTALL_TO_SITEPACKAGES)
SET(SITE_PACKAGE_DIR ${PYTHON_MAIN_DIR}/freecad)
@@ -39,6 +40,7 @@ INSTALL(
${NAMESPACE_INIT}
project_utility.py
UiTools.py
utils.py
DESTINATION
${SITE_PACKAGE_DIR}
)

63
src/Ext/freecad/utils.py Normal file
View File

@@ -0,0 +1,63 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2022-2023 FreeCAD Project Association *
# * Copyright (c) 2018 Gaël Écorchard <galou_breizh@yahoo.fr> *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
import os
import platform
import shutil
import FreeCAD
def get_python_exe() -> str:
"""Find Python. In preference order
A) The value of the PythonExecutableForPip user preference
B) The executable located in the same bin directory as FreeCAD and called "python3"
C) The executable located in the same bin directory as FreeCAD and called "python"
D) The result of a shutil search for your system's "python3" executable
E) The result of a shutil search for your system's "python" executable"""
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/PythonConsole")
python_exe = prefs.GetString("ExternalPythonExecutable", "Not set")
fc_dir = FreeCAD.getHomePath()
if not python_exe or python_exe == "Not set" or not os.path.exists(python_exe):
python_exe = os.path.join(fc_dir, "bin", "python3")
if "Windows" in platform.system():
python_exe += ".exe"
if not python_exe or not os.path.exists(python_exe):
python_exe = os.path.join(fc_dir, "bin", "python")
if "Windows" in platform.system():
python_exe += ".exe"
if not python_exe or not os.path.exists(python_exe):
python_exe = shutil.which("python3")
if not python_exe or not os.path.exists(python_exe):
python_exe = shutil.which("python")
if not python_exe or not os.path.exists(python_exe):
return ""
python_exe = python_exe.replace("/", os.path.sep)
prefs.SetString("ExternalPythonExecutable", python_exe)
return python_exe

View File

@@ -45,6 +45,7 @@ void DlgSettingsPythonConsole::saveSettings()
ui->PythonBlockCursor->onSave();
ui->PythonSaveHistory->onSave();
ui->ProfilerInterval->onSave();
ui->ExternalPythonExecutable->onSave();
}
void DlgSettingsPythonConsole::loadSettings()
@@ -53,6 +54,7 @@ void DlgSettingsPythonConsole::loadSettings()
ui->PythonBlockCursor->onRestore();
ui->PythonSaveHistory->onRestore();
ui->ProfilerInterval->onRestore();
ui->ExternalPythonExecutable->onRestore();
}
void DlgSettingsPythonConsole::changeEvent(QEvent* event)

View File

@@ -11,13 +11,13 @@
</rect>
</property>
<property name="windowTitle">
<string>Python console</string>
<string>General</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QGroupBox" name="GroupBox11">
<property name="title">
<string>Settings</string>
<string>Console</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
@@ -111,6 +111,47 @@ horizontal space in Python console</string>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="GroupBox11">
<property name="title">
<string>Other</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="fclabel">
<property name="text">
<string>Path to external Python executable (optional):</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="Gui::PrefFileChooser" name="ExternalPythonExecutable" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="toolTip">
<string>Used for package installation with pip and debugging with debugpy. Autodetected if needed and not specified.</string>
</property>
<property name="prefEntry" stdset="0">
<cstring>ExternalPythonExecutable</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>PythonConsole</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@@ -135,6 +176,11 @@ horizontal space in Python console</string>
<class>Gui::PrefSpinBox</class>
<extends>QSpinBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefFileChooser</class>
<extends>QWidget</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
</customwidgets>
<resources/>