Skip to content

Commit

Permalink
Run the set_python_path.py script when starting OpenCOR (closes openc…
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 1, 2019
2 parents e7597e8 + d3b8020 commit 8bc603f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 34 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Expand Up @@ -1380,12 +1380,6 @@ if(WIN32)
\\\${UnregisterExtension} \\\".cellml\\\" \\\"CellML File\\\"
System::Call \\\"Shell32::SHChangeNotify(i 0x08000000, i 0, i 0, i 0)\\\"
")

# Update Python scripts to refer to our installed Python

set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}
Exec '\\\"\\\$INSTDIR\\\\Python\\\\bin\\\\python.exe\\\" \\\"\\\$INSTDIR\\\\Python\\\\Scripts\\\\set_python_path.py\\\" \\\"\\\$INSTDIR\\\\Python\\\" -s'
")
endif()
elseif(APPLE)
# Select PackageMaker and ZIP as the packagers on macOS
Expand Down
2 changes: 0 additions & 2 deletions distrib/linux/runpython.in
Expand Up @@ -2,6 +2,4 @@

appDir="$(cd "$(dirname "$0")"; pwd)"

${appDir}/setpythonpath

${appDir}/bin/@CMAKE_PROJECT_NAME@ -c PythonShell $*
2 changes: 0 additions & 2 deletions distrib/macos/runpython.in
Expand Up @@ -2,6 +2,4 @@

appDir="$(cd "$(dirname "$0")"; pwd)"

${appDir}/setpythonpath

${appDir}/@CMAKE_PROJECT_NAME@.app/Contents/MacOS/@CMAKE_PROJECT_NAME@ -c PythonShell $*
2 changes: 0 additions & 2 deletions distrib/windows/runpython.bat.in
Expand Up @@ -2,6 +2,4 @@

SET appDir=%~dp0

CALL "%appDir%setpythonpath.bat"

"%appDir%bin\${CMAKE_PROJECT_NAME}" -c PythonShell %*
54 changes: 44 additions & 10 deletions src/plugins/support/PythonQtSupport/src/pythonqtsupportplugin.cpp
Expand Up @@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include "pythoninterface.h"
#include "pythonqtsupport.h"
#include "pythonqtsupportplugin.h"
#include "solverinterface.h"

Expand Down Expand Up @@ -98,16 +99,6 @@ void PythonQtSupportPlugin::initializePlugin()
signal(SIGINT, SIG_DFL);
#endif

// Set sys.argv
//---ISSUE1255--- DO WE REALLY NEED THIS?...

mArgV = reinterpret_cast<wchar_t **>(PyMem_RawMalloc(2*sizeof(wchar_t*)));

mArgV[0] = const_cast<wchar_t *>(L"");
mArgV[1] = nullptr;

PySys_SetArgvEx(1, mArgV, 0);

// Initialise Qt binding for Python

PythonQt_QtAll::init();
Expand All @@ -121,6 +112,49 @@ void PythonQtSupportPlugin::initializePlugin()
// file name...

mModule.addVariable("__file__", "OpenCOR");

// We already have set PYTHONHOME, so get it

QString pythonHome = qEnvironmentVariable("PYTHONHOME");

// The script to update Python scripts

QString setPythonPathScript = pythonHome
#if defined(Q_OS_WIN)
+ "/Scripts"
#else
+ "/bin"
#endif
+ "/set_python_path.py";

// Create a buffer in which to pass arguments to Python

mArgV = reinterpret_cast<wchar_t **>(PyMem_RawMalloc(4*sizeof(wchar_t*)));

// Set arguments for `set_python_path`
// Note: we need to use an intermediate variable as otherwise the cast
// results in an empty string

auto wSetPythonPathScript = setPythonPathScript.toStdWString();
mArgV[0] = const_cast<wchar_t *>(wSetPythonPathScript.c_str());

auto wPythonHome = pythonHome.toStdWString();
mArgV[1] = const_cast<wchar_t *>(wPythonHome.c_str());

mArgV[2] = const_cast<wchar_t *>(L"-s");
mArgV[3] = nullptr;

PySys_SetArgvEx(3, mArgV, 0);

// Actually update the path to Python in scripts

PythonQtSupport::evaluateFile(setPythonPathScript);

// Clear the argument buffer so that `sys.argv` is empty in the GUI console

mArgV[0] = const_cast<wchar_t *>(L"");
mArgV[1] = nullptr;
PySys_SetArgvEx(1, mArgV, 0);
}

//==============================================================================
Expand Down
19 changes: 8 additions & 11 deletions src/plugins/thirdParty/Python/scripts/set_python_path.py
Expand Up @@ -46,7 +46,6 @@
import sys
import logging
import marshal
import argparse
from types import CodeType
from collections import OrderedDict

Expand Down Expand Up @@ -217,15 +216,16 @@ def update_paths(base, scripts_dir, clear_args, extra_args):
return True


def main():
if __name__ == '__main__':
import argparse

parser = argparse.ArgumentParser(description='Update the path of scripts '
'to a new Python prefix')

parser.add_argument('-c', '--clear-args', dest='clear_args',
default=False, action='store_true',
help='Clear all existing arguments to Python')

parser.add_argument('-u', '--update-path', dest='update_path',
parser.add_argument('-u', '--scripts-dir', dest='scripts_dir',
help='Path to scripts. Set to "auto" for autodetection')

parser.add_argument('-v', '--verbose', dest='verbose',
Expand All @@ -238,14 +238,11 @@ def main():
help='Additional arguments to append to Python')

args = parser.parse_args()
if not args.update_path:
args.update_path = 'auto'
if not args.scripts_dir:
args.scripts_dir = 'auto'

if not args.verbose:
logging.disable(logging.INFO)

sys.exit(0 if update_paths(args.path, args.update_path, args.clear_args, args.extra_args) else 1)


if __name__ == '__main__':
main()
if not update_paths(args.path, args.scripts_dir, args.clear_args, args.extra_args):
sys.exit(1)
2 changes: 1 addition & 1 deletion src/plugins/thirdParty/PythonPackages/CMakeLists.txt
Expand Up @@ -479,7 +479,7 @@ endif()

add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_SCRIPT_DIR}/set_python_path.py --update-path ${FULL_LOCAL_EXTERNAL_PACKAGE_DIR}/${PYTHON_RELATIVE_SCRIPT_DIR}
COMMAND ${PYTHON_EXECUTABLE} ${PYTHON_SCRIPT_DIR}/set_python_path.py --scripts-dir ${FULL_LOCAL_EXTERNAL_PACKAGE_DIR}/${PYTHON_RELATIVE_SCRIPT_DIR}
${PYTHON_ROOT_DIR}
-s)

Expand Down

0 comments on commit 8bc603f

Please sign in to comment.