2012-03-17 17:30:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
|
2022-08-15 18:55:25 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-03-17 17:30:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file python_scripting.cpp
|
2021-07-09 14:56:17 +00:00
|
|
|
* @brief methods to add scripting capabilities inside Pcbnew
|
2012-03-17 17:30:03 +00:00
|
|
|
*/
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
#include <python_scripting.h>
|
|
|
|
|
2023-12-26 04:28:33 +00:00
|
|
|
#undef pid_t
|
|
|
|
#include <pybind11/embed.h>
|
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-04-17 15:39:34 +00:00
|
|
|
#include <string>
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2024-01-16 22:20:45 +00:00
|
|
|
#include <env_vars.h>
|
2020-08-11 23:11:48 +00:00
|
|
|
#include <trace_helpers.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <macros.h>
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2021-03-18 13:28:04 +00:00
|
|
|
#include <kiface_ids.h>
|
2021-01-23 02:23:37 +00:00
|
|
|
#include <paths.h>
|
2016-12-28 09:26:01 +00:00
|
|
|
#include <pgm_base.h>
|
2022-08-15 18:55:25 +00:00
|
|
|
#include <wx_filename.h>
|
2020-09-17 03:06:57 +00:00
|
|
|
#include <settings/settings_manager.h>
|
2016-12-28 09:26:01 +00:00
|
|
|
|
2021-01-23 02:23:37 +00:00
|
|
|
#include <kiplatform/environment.h>
|
|
|
|
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/app.h>
|
2023-02-14 00:18:56 +00:00
|
|
|
#include <wx/regex.h>
|
2023-01-02 16:52:05 +00:00
|
|
|
#include <wx/utils.h>
|
2020-10-18 11:31:07 +00:00
|
|
|
|
2021-01-12 16:19:59 +00:00
|
|
|
#include <config.h>
|
2024-03-21 11:41:51 +00:00
|
|
|
#include <gestfich.h>
|
2021-01-12 16:19:59 +00:00
|
|
|
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2021-03-19 23:06:29 +00:00
|
|
|
SCRIPTING::SCRIPTING()
|
2021-03-18 13:28:04 +00:00
|
|
|
{
|
|
|
|
scriptingSetup();
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2021-03-19 23:06:29 +00:00
|
|
|
pybind11::initialize_interpreter();
|
2021-03-18 13:28:04 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
// Save the current Python thread state and release the Global Interpreter Lock.
|
2021-03-18 13:28:04 +00:00
|
|
|
m_python_thread_state = PyEval_SaveThread();
|
|
|
|
}
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
|
2021-03-18 13:28:04 +00:00
|
|
|
SCRIPTING::~SCRIPTING()
|
|
|
|
{
|
|
|
|
PyEval_RestoreThread( m_python_thread_state );
|
2021-07-09 14:56:17 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pybind11::finalize_interpreter();
|
|
|
|
}
|
|
|
|
catch( const std::runtime_error& exc )
|
|
|
|
{
|
|
|
|
wxLogError( wxT( "Run time error '%s' occurred closing Python scripting" ), exc.what() );
|
|
|
|
}
|
2021-03-18 13:28:04 +00:00
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
|
2021-03-18 13:28:04 +00:00
|
|
|
bool SCRIPTING::IsWxAvailable()
|
|
|
|
{
|
2021-04-23 20:13:03 +00:00
|
|
|
#ifdef KICAD_SCRIPTING_WXPYTHON
|
2023-01-02 18:26:55 +00:00
|
|
|
static bool run = false;
|
|
|
|
static bool available = true;
|
|
|
|
|
|
|
|
if( run )
|
|
|
|
return available;
|
|
|
|
|
2023-01-02 16:52:05 +00:00
|
|
|
PyLOCK lock;
|
|
|
|
using namespace pybind11::literals;
|
|
|
|
|
|
|
|
pybind11::dict locals;
|
|
|
|
|
|
|
|
pybind11::exec( R"(
|
2023-01-02 17:58:42 +00:00
|
|
|
wx_version = ""
|
|
|
|
try:
|
|
|
|
from wx import version
|
|
|
|
wx_version = version()
|
2023-01-21 22:06:44 +00:00
|
|
|
|
|
|
|
# Import wx modules that re-initialize wx globals, because they break wxPropertyGrid
|
|
|
|
# (and probably some other stuff) if we let this happen after we already have started
|
|
|
|
# mutating those globals.
|
|
|
|
import wx.adv, wx.html, wx.richtext
|
|
|
|
|
2023-01-02 18:06:20 +00:00
|
|
|
except:
|
2023-01-02 17:58:42 +00:00
|
|
|
pass
|
2023-01-02 16:52:05 +00:00
|
|
|
)", pybind11::globals(), locals );
|
|
|
|
|
|
|
|
// e.g. "4.0.7 gtk3 (phoenix) wxWidgets 3.0.4"
|
|
|
|
wxString version( locals["wx_version"].cast<std::string>().c_str(), wxConvUTF8 );
|
|
|
|
|
|
|
|
int idx = version.Find( wxT( "wxWidgets " ) );
|
|
|
|
|
2023-01-02 17:58:42 +00:00
|
|
|
if( idx == wxNOT_FOUND || version.IsEmpty() )
|
2023-01-02 16:52:05 +00:00
|
|
|
{
|
|
|
|
wxLogError( wxT( "Could not determine wxPython version. "
|
|
|
|
"Python plugins will not be available." ) );
|
2023-01-02 18:26:55 +00:00
|
|
|
available = false;
|
2023-01-02 16:52:05 +00:00
|
|
|
}
|
2023-01-02 20:20:01 +00:00
|
|
|
else
|
2023-01-02 16:52:05 +00:00
|
|
|
{
|
2023-01-02 20:20:01 +00:00
|
|
|
wxVersionInfo wxVI = wxGetLibraryVersionInfo();
|
|
|
|
wxString wxVersion = wxString::Format( wxT( "%d.%d.%d" ),
|
|
|
|
wxVI.GetMajor(), wxVI.GetMinor(), wxVI.GetMicro() );
|
|
|
|
version = version.Mid( idx + 10 );
|
|
|
|
|
2023-02-14 13:48:47 +00:00
|
|
|
long wxPy_major = 0;
|
|
|
|
long wxPy_minor = 0;
|
|
|
|
long wxPy_micro = 0;
|
|
|
|
long wxPy_rev = 0;
|
2023-02-14 00:18:56 +00:00
|
|
|
|
|
|
|
// Compile a regex to extract the wxPython version
|
|
|
|
wxRegEx re( "([0-9]+)\\.([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?" );
|
|
|
|
wxASSERT( re.IsValid() );
|
|
|
|
|
|
|
|
if( re.Matches( version ) )
|
|
|
|
{
|
|
|
|
wxString v = re.GetMatch( version, 1 );
|
|
|
|
|
|
|
|
if( !v.IsEmpty() )
|
2023-02-14 13:48:47 +00:00
|
|
|
v.ToLong( &wxPy_major );
|
2023-02-14 00:18:56 +00:00
|
|
|
|
|
|
|
v = re.GetMatch( version, 2 );
|
|
|
|
|
|
|
|
if( !v.IsEmpty() )
|
2023-02-14 13:48:47 +00:00
|
|
|
v.ToLong( &wxPy_minor );
|
2023-02-14 00:18:56 +00:00
|
|
|
|
|
|
|
v = re.GetMatch( version, 3 );
|
|
|
|
|
|
|
|
if( !v.IsEmpty() )
|
2023-02-14 13:48:47 +00:00
|
|
|
v.ToLong( &wxPy_micro );
|
2023-02-14 00:18:56 +00:00
|
|
|
|
|
|
|
v = re.GetMatch( version, 4 );
|
|
|
|
|
|
|
|
if( !v.IsEmpty() )
|
2023-02-14 13:48:47 +00:00
|
|
|
v.ToLong( &wxPy_rev );
|
2023-02-14 00:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( wxVI.GetMajor() != wxPy_major ) || ( wxVI.GetMinor() != wxPy_minor ) )
|
2023-01-02 20:20:01 +00:00
|
|
|
{
|
|
|
|
wxString msg = wxT( "The wxPython library was compiled against wxWidgets %s but KiCad is "
|
|
|
|
"using %s. Python plugins will not be available." );
|
|
|
|
wxLogError( wxString::Format( msg, version, wxVersion ) );
|
|
|
|
available = false;
|
|
|
|
}
|
2023-01-02 16:52:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:26:55 +00:00
|
|
|
run = true;
|
|
|
|
|
|
|
|
return available;
|
2021-04-23 20:13:03 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2021-03-18 13:28:04 +00:00
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
|
2021-06-07 20:55:28 +00:00
|
|
|
bool SCRIPTING::IsModuleLoaded( std::string& aModule )
|
|
|
|
{
|
|
|
|
PyLOCK lock;
|
|
|
|
using namespace pybind11::literals;
|
|
|
|
auto locals = pybind11::dict( "modulename"_a = aModule );
|
|
|
|
|
|
|
|
pybind11::exec( R"(
|
|
|
|
import sys
|
|
|
|
loaded = False
|
|
|
|
if modulename in sys.modules:
|
|
|
|
loaded = True
|
|
|
|
|
|
|
|
)", pybind11::globals(), locals );
|
|
|
|
|
|
|
|
return locals["loaded"].cast<bool>();
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
|
2021-03-18 13:28:04 +00:00
|
|
|
bool SCRIPTING::scriptingSetup()
|
2021-03-08 14:54:22 +00:00
|
|
|
{
|
|
|
|
#if defined( __WINDOWS__ )
|
2021-07-09 14:56:17 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Under vcpkg/msvc, we need to explicitly set the python home or else it'll start consuming
|
|
|
|
// system python registry keys and the like instead of the Python distributed with KiCad.
|
|
|
|
// We are going to follow the "unix" layout for the msvc/vcpkg distributions so executable
|
|
|
|
// files are in the /root/bin path and the Python library files are in the
|
|
|
|
// /root/lib/python3(/Lib,/DLLs) path(s).
|
2021-06-09 03:37:40 +00:00
|
|
|
wxFileName pyHome;
|
|
|
|
|
|
|
|
pyHome.Assign( Pgm().GetExecutablePath() );
|
|
|
|
|
2022-08-15 18:55:25 +00:00
|
|
|
// @warning Do we want to use our own ExpandEnvVarSubstitutions() here rather than depend
|
|
|
|
// on wxFileName::Normalize() to expand environment variables.
|
|
|
|
pyHome.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
|
2021-06-09 03:37:40 +00:00
|
|
|
|
|
|
|
// MUST be called before Py_Initialize so it will to create valid default lib paths
|
2021-06-15 01:12:45 +00:00
|
|
|
if( !wxGetEnv( wxT( "KICAD_USE_EXTERNAL_PYTHONHOME" ), nullptr ) )
|
2021-06-09 03:37:40 +00:00
|
|
|
{
|
2022-04-05 02:10:59 +00:00
|
|
|
// Global config flag to ignore PYTHONPATH & PYTHONHOME
|
|
|
|
Py_IgnoreEnvironmentFlag = 1;
|
|
|
|
|
|
|
|
// Extra insurance to ignore PYTHONPATH and PYTHONHOME
|
|
|
|
wxSetEnv( wxT( "PYTHONPATH" ), wxEmptyString );
|
|
|
|
wxSetEnv( wxT( "PYTHONHOME" ), wxEmptyString );
|
|
|
|
|
|
|
|
// Now initialize Python Home via capi
|
2021-06-09 03:37:40 +00:00
|
|
|
Py_SetPythonHome( pyHome.GetFullPath().c_str() );
|
|
|
|
}
|
2021-07-09 14:56:17 +00:00
|
|
|
#else
|
2021-06-09 03:37:40 +00:00
|
|
|
// Intended for msys2 but we could probably use the msvc equivalent code too
|
2021-03-08 14:54:22 +00:00
|
|
|
// If our python.exe (in kicad/bin) exists, force our kicad python environment
|
|
|
|
wxString kipython = FindKicadFile( "python.exe" );
|
|
|
|
|
|
|
|
// we need only the path:
|
|
|
|
wxFileName fn( kipython );
|
|
|
|
kipython = fn.GetPath();
|
|
|
|
|
|
|
|
// If our python install is existing inside kicad, use it
|
|
|
|
// Note: this is useful only when another python version is installed
|
|
|
|
if( wxDirExists( kipython ) )
|
|
|
|
{
|
|
|
|
// clear any PYTHONPATH and PYTHONHOME env var definition: the default
|
|
|
|
// values work fine inside Kicad:
|
|
|
|
wxSetEnv( wxT( "PYTHONPATH" ), wxEmptyString );
|
|
|
|
wxSetEnv( wxT( "PYTHONHOME" ), wxEmptyString );
|
|
|
|
|
|
|
|
// Add our python executable path in first position:
|
|
|
|
wxString ppath;
|
|
|
|
wxGetEnv( wxT( "PATH" ), &ppath );
|
|
|
|
|
|
|
|
kipython << wxT( ";" ) << ppath;
|
|
|
|
wxSetEnv( wxT( "PATH" ), kipython );
|
|
|
|
}
|
2021-07-09 14:56:17 +00:00
|
|
|
#endif
|
2022-01-31 23:57:56 +00:00
|
|
|
#elif defined( __WXMAC__ )
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2022-03-10 19:17:24 +00:00
|
|
|
// Prevent Mac builds from generating JIT versions as this will break
|
|
|
|
// the package signing
|
2022-03-10 19:22:18 +00:00
|
|
|
wxSetEnv( wxT( "PYTHONDONTWRITEBYTECODE" ), wxT( "1" ) );
|
2022-03-10 19:17:24 +00:00
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
// Add default paths to PYTHONPATH
|
|
|
|
wxString pypath;
|
|
|
|
|
|
|
|
// Bundle scripting folder (<kicad.app>/Contents/SharedSupport/scripting)
|
|
|
|
pypath += PATHS::GetOSXKicadDataDir() + wxT( "/scripting" );
|
|
|
|
|
|
|
|
// $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i
|
2022-03-10 19:22:18 +00:00
|
|
|
if( wxGetenv( "KICAD_PATH" ) != nullptr )
|
2021-03-08 14:54:22 +00:00
|
|
|
{
|
|
|
|
pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") );
|
|
|
|
}
|
|
|
|
|
|
|
|
// OSX_BUNDLE_PYTHON_SITE_PACKAGES_DIR is provided via the build system.
|
|
|
|
|
|
|
|
pypath += wxT( ":" ) + Pgm().GetExecutablePath() + wxT( OSX_BUNDLE_PYTHON_SITE_PACKAGES_DIR );
|
|
|
|
|
|
|
|
// Original content of $PYTHONPATH
|
2021-07-09 14:56:17 +00:00
|
|
|
if( wxGetenv( wxT( "PYTHONPATH" ) ) != nullptr )
|
2021-03-08 14:54:22 +00:00
|
|
|
{
|
|
|
|
pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
|
|
|
|
}
|
|
|
|
|
2023-11-14 04:08:53 +00:00
|
|
|
// Hack for run from build dir option
|
|
|
|
if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
|
|
|
|
{
|
|
|
|
pypath = wxString( wxT( PYTHON_SITE_PACKAGE_PATH ) ) + wxT( "/../:" )
|
|
|
|
+ wxT( PYTHON_SITE_PACKAGE_PATH ) + wxT( ":" ) + wxT( PYTHON_DEST );
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
// set $PYTHONPATH
|
2022-03-10 19:22:18 +00:00
|
|
|
wxSetEnv( wxT( "PYTHONPATH" ), pypath );
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
wxString pyhome;
|
|
|
|
|
|
|
|
pyhome += Pgm().GetExecutablePath() +
|
|
|
|
wxT( "Contents/Frameworks/Python.framework/Versions/Current" );
|
|
|
|
|
2023-11-14 12:14:13 +00:00
|
|
|
if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
|
|
|
|
{
|
|
|
|
pyhome = wxString( wxT( PYTHON_SITE_PACKAGE_PATH ) ) + wxT( "/../../../" );
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
// set $PYTHONHOME
|
2022-03-10 19:22:18 +00:00
|
|
|
wxSetEnv( wxT( "PYTHONHOME" ), pyhome );
|
2021-03-08 14:54:22 +00:00
|
|
|
#else
|
|
|
|
wxString pypath;
|
|
|
|
|
2021-06-02 01:37:33 +00:00
|
|
|
if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
|
|
|
|
{
|
2021-07-09 14:56:17 +00:00
|
|
|
// When running from build dir, python module gets built next to Pcbnew binary
|
2021-06-02 01:37:33 +00:00
|
|
|
pypath = Pgm().GetExecutablePath() + wxT( "../pcbnew" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// PYTHON_DEST is the scripts install dir as determined by the build system.
|
|
|
|
pypath = Pgm().GetExecutablePath() + wxT( "../" PYTHON_DEST );
|
|
|
|
}
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
if( !wxIsEmpty( wxGetenv( wxT( "PYTHONPATH" ) ) ) )
|
|
|
|
pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
|
|
|
|
|
|
|
|
wxSetEnv( wxT( "PYTHONPATH" ), pypath );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-05-29 10:56:25 +00:00
|
|
|
wxFileName path( PyPluginsPath( SCRIPTING::PATH_TYPE::USER ) + wxT( "/" ) );
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
// Ensure the user plugin path exists, and create it if not.
|
|
|
|
// However, if it cannot be created, this is not a fatal error.
|
|
|
|
if( !path.DirExists() && !path.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
|
2021-06-26 19:21:30 +00:00
|
|
|
wxLogError( _( "Could not create user scripting path %s." ), path.GetPath() );
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2012-08-01 11:54:20 +00:00
|
|
|
return true;
|
2012-03-11 19:07:10 +00:00
|
|
|
}
|
2012-08-02 07:47:30 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2020-08-10 23:50:09 +00:00
|
|
|
wxString PyEscapeString( const wxString& aSource )
|
|
|
|
{
|
|
|
|
wxString converted;
|
|
|
|
|
|
|
|
for( wxUniChar c: aSource )
|
|
|
|
{
|
|
|
|
if( c == '\\' )
|
|
|
|
converted += "\\\\";
|
|
|
|
else if( c == '\'' )
|
|
|
|
converted += "\\\'";
|
|
|
|
else if( c == '\"' )
|
|
|
|
converted += "\\\"";
|
|
|
|
else
|
|
|
|
converted += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
void UpdatePythonEnvVar( const wxString& aVar, const wxString& aValue )
|
2020-08-09 18:49:26 +00:00
|
|
|
{
|
|
|
|
char cmd[1024];
|
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
// Ensure the interpreter is initialized before we try to interact with it.
|
2020-08-09 18:49:26 +00:00
|
|
|
if( !Py_IsInitialized() )
|
|
|
|
return;
|
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
wxLogTrace( traceEnvVars, "UpdatePythonEnvVar: Updating Python variable %s = %s",
|
2020-08-11 23:11:48 +00:00
|
|
|
aVar, aValue );
|
|
|
|
|
2020-08-10 23:50:09 +00:00
|
|
|
wxString escapedVar = PyEscapeString( aVar );
|
|
|
|
wxString escapedVal = PyEscapeString( aValue );
|
|
|
|
|
2020-08-09 18:49:26 +00:00
|
|
|
snprintf( cmd, sizeof( cmd ),
|
2021-07-09 14:56:17 +00:00
|
|
|
"# coding=utf-8\n" // The values could potentially be UTF8.
|
2021-03-19 23:06:29 +00:00
|
|
|
"import os\n"
|
2020-08-09 18:49:26 +00:00
|
|
|
"os.environ[\"%s\"]=\"%s\"\n",
|
2020-08-10 23:50:09 +00:00
|
|
|
TO_UTF8( escapedVar ),
|
|
|
|
TO_UTF8( escapedVal ) );
|
2020-08-09 18:49:26 +00:00
|
|
|
|
|
|
|
PyLOCK lock;
|
|
|
|
|
|
|
|
int retv = PyRun_SimpleString( cmd );
|
|
|
|
|
|
|
|
if( retv != 0 )
|
2021-06-26 19:21:30 +00:00
|
|
|
wxLogError( "Python error %d running command:\n\n`%s`", retv, cmd );
|
2020-08-09 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2018-10-12 19:36:50 +00:00
|
|
|
wxString PyStringToWx( PyObject* aString )
|
|
|
|
{
|
|
|
|
wxString ret;
|
|
|
|
|
|
|
|
if( !aString )
|
|
|
|
return ret;
|
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
const char* str_res = nullptr;
|
2018-10-12 19:36:50 +00:00
|
|
|
PyObject* temp_bytes = PyUnicode_AsEncodedString( aString, "UTF-8", "strict" );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
if( temp_bytes != nullptr )
|
2018-10-12 19:36:50 +00:00
|
|
|
{
|
|
|
|
str_res = PyBytes_AS_STRING( temp_bytes );
|
2023-09-09 04:10:57 +00:00
|
|
|
ret = From_UTF8( str_res );
|
2018-10-12 19:36:50 +00:00
|
|
|
Py_DECREF( temp_bytes );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-22 14:41:33 +00:00
|
|
|
wxLogMessage( wxS( "cannot encode Unicode python string" ) );
|
2018-10-12 19:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-17 01:14:46 +00:00
|
|
|
wxArrayString PyArrayStringToWx( PyObject* aArrayString )
|
|
|
|
{
|
|
|
|
wxArrayString ret;
|
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
if( !aArrayString )
|
|
|
|
return ret;
|
2013-03-17 01:14:46 +00:00
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
int list_size = PyList_Size( aArrayString );
|
|
|
|
|
|
|
|
for( int n = 0; n < list_size; n++ )
|
2013-03-17 01:14:46 +00:00
|
|
|
{
|
|
|
|
PyObject* element = PyList_GetItem( aArrayString, n );
|
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
if( element )
|
2018-08-03 13:23:32 +00:00
|
|
|
{
|
2021-07-09 14:56:17 +00:00
|
|
|
const char* str_res = nullptr;
|
2018-10-18 06:39:36 +00:00
|
|
|
PyObject* temp_bytes = PyUnicode_AsEncodedString( element, "UTF-8", "strict" );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
if( temp_bytes != nullptr )
|
2018-10-18 06:39:36 +00:00
|
|
|
{
|
|
|
|
str_res = PyBytes_AS_STRING( temp_bytes );
|
2023-09-09 04:10:57 +00:00
|
|
|
ret.Add( From_UTF8( str_res ), 1 );
|
2018-10-18 06:39:36 +00:00
|
|
|
Py_DECREF( temp_bytes );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-22 14:41:33 +00:00
|
|
|
wxLogMessage( wxS( "cannot encode Unicode python string" ) );
|
2018-10-18 06:39:36 +00:00
|
|
|
}
|
2018-08-03 13:23:32 +00:00
|
|
|
}
|
2013-03-17 01:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString PyErrStringWithTraceback()
|
|
|
|
{
|
|
|
|
wxString err;
|
|
|
|
|
|
|
|
if( !PyErr_Occurred() )
|
|
|
|
return err;
|
|
|
|
|
|
|
|
PyObject* type;
|
|
|
|
PyObject* value;
|
|
|
|
PyObject* traceback;
|
|
|
|
|
|
|
|
PyErr_Fetch( &type, &value, &traceback );
|
|
|
|
|
2018-10-10 09:24:45 +00:00
|
|
|
PyErr_NormalizeException( &type, &value, &traceback );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2021-07-09 14:56:17 +00:00
|
|
|
if( traceback == nullptr )
|
2018-10-11 20:37:09 +00:00
|
|
|
{
|
2018-08-05 09:28:57 +00:00
|
|
|
traceback = Py_None;
|
|
|
|
Py_INCREF( traceback );
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:24:45 +00:00
|
|
|
PyException_SetTraceback( value, traceback );
|
2018-08-05 09:28:57 +00:00
|
|
|
|
2018-08-03 13:23:32 +00:00
|
|
|
PyObject* tracebackModuleString = PyUnicode_FromString( "traceback" );
|
2017-01-06 19:15:23 +00:00
|
|
|
PyObject* tracebackModule = PyImport_Import( tracebackModuleString );
|
|
|
|
Py_DECREF( tracebackModuleString );
|
|
|
|
|
|
|
|
PyObject* formatException = PyObject_GetAttrString( tracebackModule,
|
|
|
|
"format_exception" );
|
|
|
|
Py_DECREF( tracebackModule );
|
|
|
|
|
|
|
|
PyObject* args = Py_BuildValue( "(O,O,O)", type, value, traceback );
|
|
|
|
PyObject* result = PyObject_CallObject( formatException, args );
|
|
|
|
Py_XDECREF( formatException );
|
|
|
|
Py_XDECREF( args );
|
|
|
|
Py_XDECREF( type );
|
|
|
|
Py_XDECREF( value );
|
|
|
|
Py_XDECREF( traceback );
|
2013-03-17 01:14:46 +00:00
|
|
|
|
|
|
|
wxArrayString res = PyArrayStringToWx( result );
|
|
|
|
|
2013-03-17 19:04:42 +00:00
|
|
|
for( unsigned i = 0; i<res.Count(); i++ )
|
2013-03-17 01:14:46 +00:00
|
|
|
{
|
|
|
|
err += res[i] + wxT( "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2016-12-28 09:26:01 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2016-12-28 09:26:01 +00:00
|
|
|
/**
|
2019-05-23 20:55:27 +00:00
|
|
|
* Find the Python scripting path.
|
2016-12-28 09:26:01 +00:00
|
|
|
*/
|
2021-05-29 10:56:25 +00:00
|
|
|
wxString SCRIPTING::PyScriptingPath( PATH_TYPE aPathType )
|
2016-12-28 09:26:01 +00:00
|
|
|
{
|
|
|
|
wxString path;
|
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
//@todo This should this be a user configurable variable eg KISCRIPT?
|
2021-05-29 10:56:25 +00:00
|
|
|
switch( aPathType )
|
2020-09-17 03:06:57 +00:00
|
|
|
{
|
2020-10-26 10:54:36 +00:00
|
|
|
case STOCK:
|
|
|
|
path = PATHS::GetStockScriptingPath();
|
|
|
|
break;
|
2024-01-16 22:20:45 +00:00
|
|
|
|
2020-10-26 10:54:36 +00:00
|
|
|
case USER:
|
|
|
|
path = PATHS::GetUserScriptingPath();
|
|
|
|
break;
|
2024-01-16 22:20:45 +00:00
|
|
|
|
2021-05-29 10:56:25 +00:00
|
|
|
case THIRDPARTY:
|
2024-01-16 22:20:45 +00:00
|
|
|
{
|
2021-05-29 10:56:25 +00:00
|
|
|
const ENV_VAR_MAP& env = Pgm().GetLocalEnvVariables();
|
|
|
|
|
2024-01-16 22:20:45 +00:00
|
|
|
if( std::optional<wxString> v = ENV_VAR::GetVersionedEnvVarValue( env,
|
|
|
|
wxT( "3RD_PARTY" ) ) )
|
|
|
|
{
|
|
|
|
path = *v;
|
|
|
|
}
|
2021-05-29 10:56:25 +00:00
|
|
|
else
|
2024-01-16 22:20:45 +00:00
|
|
|
{
|
2021-05-29 10:56:25 +00:00
|
|
|
path = PATHS::GetDefault3rdPartyPath();
|
2024-01-16 22:20:45 +00:00
|
|
|
}
|
2021-05-29 10:56:25 +00:00
|
|
|
|
|
|
|
break;
|
2020-09-17 03:06:57 +00:00
|
|
|
}
|
2024-01-16 22:20:45 +00:00
|
|
|
}
|
2016-12-28 09:26:01 +00:00
|
|
|
|
|
|
|
wxFileName scriptPath( path );
|
|
|
|
scriptPath.MakeAbsolute();
|
|
|
|
|
2018-04-27 12:03:33 +00:00
|
|
|
// Convert '\' to '/' in path, because later python script read \n or \r
|
2018-04-27 14:01:46 +00:00
|
|
|
// as escaped sequence, and create issues, when calling it by PyRun_SimpleString() method.
|
2018-04-27 12:03:33 +00:00
|
|
|
// It can happen on Windows.
|
|
|
|
path = scriptPath.GetFullPath();
|
|
|
|
path.Replace( '\\', '/' );
|
|
|
|
|
|
|
|
return path;
|
2016-12-28 09:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2021-05-29 10:56:25 +00:00
|
|
|
wxString SCRIPTING::PyPluginsPath( PATH_TYPE aPathType )
|
2016-12-28 09:26:01 +00:00
|
|
|
{
|
2018-04-27 14:01:46 +00:00
|
|
|
// Note we are using unix path separator, because window separator sometimes
|
|
|
|
// creates issues when passing a command string to a python method by PyRun_SimpleString
|
2021-05-29 10:56:25 +00:00
|
|
|
return PyScriptingPath( aPathType ) + '/' + "plugins";
|
2016-12-28 09:26:01 +00:00
|
|
|
}
|