diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /pyuno/zipcore | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream/4%7.4.7.tar.xz libreoffice-upstream/4%7.4.7.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pyuno/zipcore')
-rw-r--r-- | pyuno/zipcore/mac.sh | 14 | ||||
-rw-r--r-- | pyuno/zipcore/nonmac.sh | 12 | ||||
-rw-r--r-- | pyuno/zipcore/python.cxx | 212 | ||||
-rwxr-xr-x | pyuno/zipcore/python.sh | 40 |
4 files changed, 278 insertions, 0 deletions
diff --git a/pyuno/zipcore/mac.sh b/pyuno/zipcore/mac.sh new file mode 100644 index 000000000..830bcdaa1 --- /dev/null +++ b/pyuno/zipcore/mac.sh @@ -0,0 +1,14 @@ +# Set URE_BOOTSTRAP so that "uno.getComponentContext()" bootstraps a complete +# OOo UNO environment: +: ${URE_BOOTSTRAP=vnd.sun.star.pathname:$sd_prog/../Resources/fundamentalrc} +export URE_BOOTSTRAP + +PYTHONHOME=$sd_prog/../Frameworks/LibreOfficePython.framework +export PYTHONHOME + +pybasislibdir=$PYTHONHOME/Versions/%%PYVERSION%%/lib/python%%PYVERSION%% +PYTHONPATH=$sd_prog/../Resources:$sd_prog/../Frameworks:$pybasislibdir:$pybasislibdir/lib-dynload:$pybasislibdir/lib-tk:$pybasislibdir/site-packages${PYTHONPATH+:$PYTHONPATH} +export PYTHONPATH + +# execute binary +exec "$PYTHONHOME/Versions/%%PYVERSION%%/Resources/Python.app/Contents/MacOS/LibreOfficePython" "$@" diff --git a/pyuno/zipcore/nonmac.sh b/pyuno/zipcore/nonmac.sh new file mode 100644 index 000000000..5e7cca14d --- /dev/null +++ b/pyuno/zipcore/nonmac.sh @@ -0,0 +1,12 @@ +# Set URE_BOOTSTRAP so that "uno.getComponentContext()" bootstraps a complete +# OOo UNO environment: +: ${URE_BOOTSTRAP=vnd.sun.star.pathname:$sd_prog/fundamentalrc} +export URE_BOOTSTRAP + +PYTHONPATH=$sd_prog:$sd_prog/python-core-%%PYVERSION%%/lib:$sd_prog/python-core-%%PYVERSION%%/lib/lib-dynload:$sd_prog/python-core-%%PYVERSION%%/lib/lib-tk:$sd_prog/python-core-%%PYVERSION%%/lib/site-packages${PYTHONPATH+:$PYTHONPATH} +export PYTHONPATH +PYTHONHOME=$sd_prog/python-core-%%PYVERSION%% +export PYTHONHOME + +# execute binary +exec "$sd_prog/python.bin" "$@" diff --git a/pyuno/zipcore/python.cxx b/pyuno/zipcore/python.cxx new file mode 100644 index 000000000..7580e3278 --- /dev/null +++ b/pyuno/zipcore/python.cxx @@ -0,0 +1,212 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <config_python.h> + +#include <cstddef> +#include <stdlib.h> +#include <wchar.h> + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> + +#include <tools/pathutils.hxx> + +#define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1) +#define MY_STRING(s) (s), MY_LENGTH(s) + +static wchar_t * encode(wchar_t * buffer, wchar_t const * text) { + *buffer++ = L'"'; + std::size_t n = 0; + for (;;) { + wchar_t c = *text++; + if (c == L'\0') { + break; + } else if (c == L'"') { + // Double any preceding backslashes as required by Windows: + for (std::size_t i = 0; i < n; ++i) { + *buffer++ = L'\\'; + } + *buffer++ = L'\\'; + *buffer++ = L'"'; + n = 0; + } else if (c == L'\\') { + *buffer++ = L'\\'; + ++n; + } else { + *buffer++ = c; + n = 0; + } + } + // The command line will continue with a double quote, so double any + // preceding backslashes as required by Windows: + for (std::size_t i = 0; i < n; ++i) { + *buffer++ = L'\\'; + } + *buffer++ = L'"'; + return buffer; +} + +int wmain(int argc, wchar_t ** argv, wchar_t **) { + wchar_t path[MAX_PATH]; + DWORD n = GetModuleFileNameW(nullptr, path, MAX_PATH); + if (n == 0 || n >= MAX_PATH) { + exit(EXIT_FAILURE); + } + wchar_t * pathEnd = tools::filename(path); + *pathEnd = L'\0'; + n = GetEnvironmentVariableW(L"UNO_PATH", nullptr, 0); + if (n == 0) { + if (GetLastError() != ERROR_ENVVAR_NOT_FOUND || + !SetEnvironmentVariableW(L"UNO_PATH", path)) + { + exit(EXIT_FAILURE); + } + } + wchar_t bootstrap[MY_LENGTH(L"vnd.sun.star.pathname:") + MAX_PATH] = + L"vnd.sun.star.pathname:"; //TODO: overflow + wchar_t * bootstrapEnd = tools::buildPath( + bootstrap + MY_LENGTH(L"vnd.sun.star.pathname:"), path, pathEnd, + MY_STRING(L"fundamental.ini")); + if (bootstrapEnd == nullptr) { + exit(EXIT_FAILURE); + } + wchar_t pythonpath2[MAX_PATH]; + wchar_t * pythonpath2End = tools::buildPath( + pythonpath2, path, pathEnd, + MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING L"\\lib")); + if (pythonpath2End == nullptr) { + exit(EXIT_FAILURE); + } + wchar_t pythonpath3[MAX_PATH]; + wchar_t * pythonpath3End = tools::buildPath( + pythonpath3, path, pathEnd, + MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING L"\\lib\\site-packages")); + if (pythonpath3End == nullptr) { + exit(EXIT_FAILURE); + } + wchar_t pythonhome[MAX_PATH]; + wchar_t * pythonhomeEnd = tools::buildPath( + pythonhome, path, pathEnd, MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING)); + if (pythonhomeEnd == nullptr) { + exit(EXIT_FAILURE); + } + wchar_t pythonexe[MAX_PATH]; + wchar_t * pythonexeEnd = tools::buildPath( + pythonexe, path, pathEnd, + MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING L"\\bin\\python.exe")); + if (pythonexeEnd == nullptr) { + exit(EXIT_FAILURE); + } + std::size_t clSize = MY_LENGTH(L"\"") + 4 * (pythonexeEnd - pythonexe) + + MY_LENGTH(L"\"\0"); //TODO: overflow + // 4 * len: each char preceded by backslash, each trailing backslash + // doubled + for (int i = 1; i < argc; ++i) { + clSize += MY_LENGTH(L" \"") + 4 * wcslen(argv[i]) + MY_LENGTH(L"\""); + //TODO: overflow + } + wchar_t * cl = new wchar_t[clSize]; + wchar_t * cp = encode(cl, pythonhome); + for (int i = 1; i < argc; ++i) { + *cp++ = L' '; + cp = encode(cp, argv[i]); + } + *cp = L'\0'; + n = GetEnvironmentVariableW(L"PATH", nullptr, 0); + wchar_t * orig; + if (n == 0) { + if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) { + exit(EXIT_FAILURE); + } + orig = const_cast<wchar_t *>(L""); + } else { + orig = new wchar_t[n]; + if (GetEnvironmentVariableW(L"PATH", orig, n) != n - 1) + { + exit(EXIT_FAILURE); + } + } + std::size_t len = (pathEnd - path) + (n == 0 ? 0 : MY_LENGTH(L";") + + (n - 1)) + 1; + //TODO: overflow + wchar_t * value = new wchar_t[len]; + _snwprintf( + value, len, L"%s%s%s", path, n == 0 ? L"" : L";", orig); + if (!SetEnvironmentVariableW(L"PATH", value)) { + exit(EXIT_FAILURE); + } + if (n != 0) { + delete [] orig; + } + delete [] value; + n = GetEnvironmentVariableW(L"PYTHONPATH", nullptr, 0); + if (n == 0) { + if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) { + exit(EXIT_FAILURE); + } + orig = const_cast<wchar_t *>(L""); + } else { + orig = new wchar_t[n]; + if (GetEnvironmentVariableW(L"PYTHONPATH", orig, n) != n - 1) + { + exit(EXIT_FAILURE); + } + } + len = (pathEnd - path) + MY_LENGTH(L";") + (pythonpath2End - pythonpath2) + + MY_LENGTH(L";") + (pythonpath3End - pythonpath3) + + (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1; //TODO: overflow + value = new wchar_t[len]; + _snwprintf( + value, len, L"%s;%s;%s%s%s", path, pythonpath2, pythonpath3, + n == 0 ? L"" : L";", orig); + if (!SetEnvironmentVariableW(L"PYTHONPATH", value)) { + exit(EXIT_FAILURE); + } + if (n != 0) { + delete [] orig; + } + delete [] value; + if (!SetEnvironmentVariableW(L"PYTHONHOME", pythonhome)) { + exit(EXIT_FAILURE); + } + n = GetEnvironmentVariableW(L"URE_BOOTSTRAP", nullptr, 0); + if (n == 0) { + if (GetLastError() != ERROR_ENVVAR_NOT_FOUND || + !SetEnvironmentVariableW(L"URE_BOOTSTRAP", bootstrap)) + { + exit(EXIT_FAILURE); + } + } + STARTUPINFOW startinfo; + ZeroMemory(&startinfo, sizeof (STARTUPINFOW)); + startinfo.cb = sizeof (STARTUPINFOW); + PROCESS_INFORMATION procinfo; + if (!CreateProcessW( + pythonexe, cl, nullptr, nullptr, FALSE, CREATE_UNICODE_ENVIRONMENT, nullptr, + nullptr, &startinfo, &procinfo)) { + exit(EXIT_FAILURE); + } + WaitForSingleObject(procinfo.hProcess,INFINITE); + DWORD exitStatus; + GetExitCodeProcess(procinfo.hProcess,&exitStatus); + exit(exitStatus); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/pyuno/zipcore/python.sh b/pyuno/zipcore/python.sh new file mode 100755 index 000000000..bfe6785bd --- /dev/null +++ b/pyuno/zipcore/python.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# + +# resolve installation directory +sd_cwd="`pwd`" +if [ -h "$0" ] ; then + sd_basename=`basename "$0"` + sd_script=`ls -l "$0" | sed "s/.*${sd_basename} -> //g"` + cd "`dirname "$0"`" + cd "`dirname "$sd_script"`" +else + cd "`dirname "$0"`" +fi +sd_prog=`pwd` +cd "$sd_cwd" + +# Set PATH so that crash_report is found: +PATH=$sd_prog${PATH+:$PATH} +export PATH + +# Set UNO_PATH so that "officehelper.bootstrap()" can find soffice executable: +: ${UNO_PATH=$sd_prog} +export UNO_PATH + |