Skip to content

Commit

Permalink
Add script engine for the script language Python (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
QQxiaoming authored Jul 25, 2024
1 parent 964b467 commit f05d12d
Show file tree
Hide file tree
Showing 62 changed files with 4,619 additions and 8 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ jobs:
run: |
sudo apt update
sudo apt install -y make gcc patchelf chrpath qt5-default libxcb-cursor0 build-essential libgtk-3-dev
- name: Install setup python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: build fcitx_qt6
run: |
Expand Down Expand Up @@ -109,6 +114,11 @@ jobs:
run: |
sudo apt update
sudo apt install -y make gcc patchelf chrpath qt5-default libxcb-cursor0 build-essential libgtk-3-dev
- name: Install setup python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: build fcitx_qt6
run: |
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ jobs:
brew install create-dmg
brew install coreutils
- name: Install setup python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: build libssh2
run: |
cd depend
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/macos_arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ jobs:
brew install create-dmg
brew install coreutils
- name: Install setup python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: build libssh2
run: |
cd depend
Expand Down
35 changes: 33 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,32 @@ jobs:
with:
args: install innosetup --no-progress

- name: Install setup python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- uses: msys2/setup-msys2@v2
with:
msystem: mingw64

- name: build libssh2
- name: build mingw-w64-libpython
shell: msys2 {0}
run: |
pacman --noconfirm -S make cmake autoconf automake bison diffutils libtool texinfo xmlto
pacman --noconfirm -S base-devel msys2-devel mingw-w64-{x86_64,i686}-toolchain
mkdir mingw-w64-libpython
cp -r ${Python3_ROOT_DIR}/include ./mingw-w64-libpython/
mkdir mingw-w64-libpython/lib
cp ${Python3_ROOT_DIR}/python311.dll ./mingw-w64-libpython/lib/python311.dll
cd mingw-w64-libpython/lib
gendef python311.dll
dlltool -D python311.dll -d python311.def -l libpython311.a
- name: build libssh2
shell: msys2 {0}
run: |
cd depend
export PATH=/c/ProgramData/Chocolatey/lib/mingw/tools/install/mingw64/bin:$PATH
./build_libssh2_mingw.sh
mkdir /d/libssh2
mkdir /d/libssh2/include
Expand Down Expand Up @@ -108,6 +123,7 @@ jobs:
./tools/sed/sed.exe -i "s/mingw32-make -j8/mingw32-make -j1/g" ./build_setup.bat
./tools/sed/sed.exe -i "s/WINPTY_DIR=D:\\quardCRT\\depend\\winpty/WINPTY_DIR=D:\\winpty/g" ./lib/ptyqt/ptyqt.pri
./tools/sed/sed.exe -i "s/LIBSSH2_DIR=\/libssh2/LIBSSH2_DIR=D:\\libssh2/g" ./lib/qtssh/qtssh.pri
./tools/sed/sed.exe -i "s/Python3_DIR=\/python3_root_dir/Python3_DIR=mingw-w64-libpython/g" ./quardCRT.pro
mkdir prebuilt_plugins
python3 ./.github/scripts/get_plugin.py ./.github/scripts/plugin_list.json ./prebuilt_plugins
./build_setup.bat
Expand Down Expand Up @@ -147,6 +163,19 @@ jobs:
with:
args: install innosetup --no-progress

- name: Install setup python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: build msvc-libpython
run: |
mkdir msvc-libpython
cp -r $env:Python3_ROOT_DIR/include ./msvc-libpython/
mkdir msvc-libpython/lib
cp $env:Python3_ROOT_DIR/python311.dll ./msvc-libpython/lib/python311.dll
cp $env:Python3_ROOT_DIR/libs/python311.lib ./msvc-libpython/lib/python311.lib
- name: Setup MSVC
uses: ilammy/msvc-dev-cmd@v1
with:
Expand All @@ -172,6 +201,7 @@ jobs:
./tools/sed/sed.exe -i "s/QT_DIR=C:\/Qt\/Qt6.2.0\/6.2.0\/msvc64\/bin/QT_DIR=%Qt6_DIR%/g" ./build_setup_msvc.bat
./tools/sed/sed.exe -i "s/QT_TOOLS_DIR=C:\/Qt\/Qt6.2.0\/Tools\/msvc64\/bin/QT_TOOLS_DIR=C:\/ProgramData\/Chocolatey\/lib\/mingw\/tools\/install\/msvc64\/bin/g" ./build_setup_msvc.bat
./tools/sed/sed.exe -i "s/LIBSSH2_DIR=\/libssh2/LIBSSH2_DIR=D:\\libssh2/g" ./lib/qtssh/qtssh.pri
./tools/sed/sed.exe -i "s/Python3_DIR=\/python3_root_dir/Python3_DIR=msvc-libpython/g" ./quardCRT.pro
c:/Windows/system32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy Bypass -NoLogo -NoProfile -File "./tools/generate_info.ps1" > ./build_info.inc
mkdir prebuilt_plugins
python3 ./.github/scripts/get_plugin.py ./.github/scripts/plugin_list.json ./prebuilt_plugins msvc
Expand All @@ -187,3 +217,4 @@ jobs:
- name: Run tests
run: |
./InnoSetup/build/quardCRT.exe --version
2 changes: 2 additions & 0 deletions DEVELOPNOTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

Currently there are two types of upstream dependencies: one is direct source code dependency, located in the `lib` directory, these dependencies have been deeply modified to meet the needs of this project, and need to be manually merged when synchronizing with the upstream library, but no additional compilation steps are required during compilation; the other is a compilation dependency, located in the `depend` directory, these dependencies are basically extracted directly from the upstream library, and only need to be re-extracted when synchronizing with the upstream library, but need to be compiled into dynamic link libraries before compiling this project.

In addition to the above two types of dependencies, this project uses a dynamically linked Python interpreter to support script extension functionality, so you need to install Python 3.8 or later. On linux and macos, use `python3-config --cflags` and `python3-config --ldflags --embed` to obtain the compilation parameters of Python. If you use mingw to compile, you need to use the gendef and dlltool tools to generate the libpython.a file, and if you use msvc to compile, you can directly use the libpython.lib file provided by the official Python.

#### Dependencies under the `depend` directory

- fcitx-qt5-1.2.7.tar.gz
Expand Down
2 changes: 2 additions & 0 deletions DEVELOPNOTE_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

目前上游依赖分为两种:一种是直接源码依赖,位于`lib`目录下,这类依赖库被本项目深度修改,以适应本项目的需求,与上游库同步时需要手动合并修改,但在编译时不需要额外的编译步骤;另一种是编译依赖,位于`depend`目录下,这类依赖库基本上是直接从上游库中提取出来的,与上游库同步时只需要重新提取即可,但在编译时需要先编译这些依赖库为动态链接库,再编译本项目。

除以上两种依赖外,本项目使用动态链接Python解释器以此支持脚本扩展功能,因此需要安装Python3.8及以上版本,linux和macos下使用`python3-config --cflags``python3-config --ldflags --embed`获取Python的编译参数即可。如果使用mingw编译则需要gendef、dlltool工具转化生成libpython.a文件,msvc编译则可以直接使用Python官方提供的libpython.lib文件。

#### depend目录下的依赖库

- fcitx-qt5-1.2.7.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion build_AppImage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ mkdir -p ./AppDir/usr/plugins/QuardCRT
if [ -d "./prebuilt_plugins" ]; then
cp ./prebuilt_plugins/*.so ./AppDir/usr/plugins/QuardCRT/
fi
./tools/linuxdeploy-x86_64.AppImage --executable=./build_release/out/quardCRT --appdir=./AppDir --icon-file=./icons/ico.svg --desktop-file=./AppDirRes/quardCRT.desktop --plugin=qt --output appimage
ARCH=x86_64 ./tools/linuxdeploy-x86_64.AppImage --executable=./build_release/out/quardCRT --appdir=./AppDir --icon-file=./icons/ico.svg --desktop-file=./AppDirRes/quardCRT.desktop --plugin qt --plugin quardcrt --output appimage
mkdir AppImageOut
cp ./quardCRT-*.AppImage ./AppImageOut/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64.AppImage
3 changes: 3 additions & 0 deletions build_deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ mkdir -p ./dpkg/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64/opt/quardCRT/plugins/Q
if [ -d "./prebuilt_plugins" ]; then
cp ./prebuilt_plugins/*.so ./dpkg/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64/opt/quardCRT/plugins/QuardCRT/
fi
python_stdlib=$(python3 -c "import sysconfig; print(sysconfig.get_path('stdlib'))")
mkdir -p ./dpkg/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64/opt/quardCRT/pythonlib
cp -r $python_stdlib/../../lib ./dpkg/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64/opt/quardCRT/pythonlib/
# 打包
dpkg -b ./dpkg/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64 ./dpkg/quardCRT_Linux_"$QUARDCRT_VERSION"_x86_64.deb
echo build success!
Expand Down
7 changes: 7 additions & 0 deletions build_dmg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ mkdir -p ./quardCRT.app/Contents/MacOS/plugins/QuardCRT
if [ -d "../../prebuilt_plugins" ]; then
cp ../../prebuilt_plugins/*.dylib ./quardCRT.app/Contents/MacOS/plugins/QuardCRT/
fi
python_stdlib=$(python3 -c "import sysconfig; print(sysconfig.get_path('stdlib'))")
mkdir -p ./quardCRT.app/Contents/Frameworks/pythonlib/lib
cp -r $python_stdlib/../../lib/python3.11 ./quardCRT.app/Contents/Frameworks/pythonlib/lib/
cp -r $python_stdlib/../../lib/libpython3.11.dylib ./quardCRT.app/Contents/Frameworks/pythonlib/lib/
install_name_tool -change /Library/Frameworks/Python.framework/Versions/3.11/Python @executable_path/../Frameworks/pythonlib/lib/libpython3.11.dylib ./quardCRT.app/Contents/MacOS/quardCRT
install_name_tool -change /Library/Frameworks/Python.framework/Versions/3.11/Python @executable_path/../pythonlib/libpython3.11.dylib ./quardCRT.app/Contents/Frameworks/pythonlib/lib/libpython3.11.dylib
codesign --force --deep --sign - ./quardCRT.app/Contents/Frameworks/pythonlib/lib/libpython3.11.dylib
./build-dmg.sh quardCRT
cd ../../
mkdir dmgOut
Expand Down
3 changes: 3 additions & 0 deletions build_setup.bat
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ del /f /q /a .\sed*
xcopy /y .\build_release\out\quardCRT.exe .\InnoSetup\build\
:: 使用windeployqt拷贝依赖dll库到打包目录
windeployqt --dir .\InnoSetup\build .\InnoSetup\build\quardCRT.exe
:: 拷贝 %Python3_ROOT_DIR% 下的Python311.dll到打包目录 以及 python stdlib
xcopy /y "%Python3_ROOT_DIR%\python311.dll" ".\InnoSetup\build\"
xcopy /y "%Python3_ROOT_DIR%\Lib" ".\InnoSetup\build\pythonlib\lib" /E /I
xcopy /y "D:\libssh2\bin\*.dll" ".\InnoSetup\build\"
xcopy /y "D:\winpty\bin\winpty.dll" ".\InnoSetup\build\"
xcopy /y "D:\winpty\bin\winpty-agent.exe" ".\InnoSetup\build\"
Expand Down
2 changes: 2 additions & 0 deletions build_setup_msvc.bat
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ del /f /q /a .\sed*
xcopy /y .\build_release\out\quardCRT.exe .\InnoSetup\build\
:: 使用windeployqt拷贝依赖dll库到打包目录
windeployqt --dir .\InnoSetup\build .\InnoSetup\build\quardCRT.exe
xcopy /y "%Python3_ROOT_DIR%\python311.dll" ".\InnoSetup\build\"
xcopy /y "%Python3_ROOT_DIR%\Lib" ".\InnoSetup\build\pythonlib\lib" /E /I
xcopy /y "D:\libssh2\bin\*.dll" ".\InnoSetup\build\"
xcopy /y .\scripts\Profile.ps1 .\InnoSetup\build\
xcopy /y .\font\inziu-iosevkaCC-SC-regular.ttf .\InnoSetup\build\
Expand Down
Binary file added icons/aboutpython.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 54 additions & 3 deletions quardCRT.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DEFINES += ENABLE_SSH # depend libssh2
DEFINES += ENABLE_PYTHON # depend libpython

!versionAtLeast(QT_VERSION, 6.5.0) {
message("Cannot use Qt $$QT_VERSION")
Expand Down Expand Up @@ -191,6 +192,9 @@ FORMS += \
src/util/filedialog.ui \
src/mainwindow.ui

RESOURCES += \
res/resource.qrc

contains(DEFINES, ENABLE_SSH) {
INCLUDEPATH += \
src/sftpwindow
Expand All @@ -205,8 +209,36 @@ contains(DEFINES, ENABLE_SSH) {
src/sftpwindow/sftpwindow.ui
}

RESOURCES += \
res/resource.qrc
contains(DEFINES, ENABLE_PYTHON) {
INCLUDEPATH += \
src/scriptengine
SOURCES += \
src/scriptengine/pysessionconfiguration.cpp \
src/scriptengine/pycommandwindow.cpp \
src/scriptengine/pyarguments.cpp \
src/scriptengine/pyclipboard.cpp \
src/scriptengine/pycore.cpp \
src/scriptengine/pydialog.cpp \
src/scriptengine/pyfiletransfer.cpp \
src/scriptengine/pyrun.cpp \
src/scriptengine/pyscreen.cpp \
src/scriptengine/pysession.cpp \
src/scriptengine/pytab.cpp \
src/scriptengine/pywindow.cpp
HEADERS += \
src/scriptengine/pysessionconfiguration.h \
src/scriptengine/pycommandwindow.h \
src/scriptengine/pyarguments.h \
src/scriptengine/pyclipboard.h \
src/scriptengine/pycore.h \
src/scriptengine/pydialog.h \
src/scriptengine/pyfiletransfer.h \
src/scriptengine/pyrun.h \
src/scriptengine/pyscreen.h \
src/scriptengine/pysession.h \
src/scriptengine/pytab.h \
src/scriptengine/pywindow.h
}

TRANSLATIONS += \
lang/quardCRT_zh_CN.ts \
Expand Down Expand Up @@ -236,11 +268,22 @@ RCC_DIR = $$build_type/rcc
UI_DIR = $$build_type/ui

win32:{
contains(DEFINES, ENABLE_PYTHON) {
Python3_DIR=/python3_root_dir
}
win32-g++ {
QMAKE_CXXFLAGS += -Wno-deprecated-copy
RESOURCES += res/terminalfont.qrc
contains(DEFINES, ENABLE_PYTHON) {
INCLUDEPATH += $${Python3_DIR}/include
LIBS += -L$${Python3_DIR}/lib -lpython311
}
}
win32-msvc*{
contains(DEFINES, ENABLE_PYTHON) {
INCLUDEPATH += $${Python3_DIR}\include
LIBS += $${Python3_DIR}\lib\python311.lib
}
}

VERSION = $${BUILD_VERSION}.000
Expand All @@ -258,7 +301,11 @@ unix:!macx:{
QMAKE_RPATHDIR=$ORIGIN
QMAKE_LFLAGS += -no-pie
LIBS += -lutil

contains(DEFINES, ENABLE_PYTHON) {
QMAKE_CXXFLAGS += $$system("python3-config --cflags")
LIBS += $$system("python3-config --ldflags --embed")
}

build_info.commands = $$quote("cd $$PWD && ./tools/generate_info.sh > build_info.inc")
}

Expand All @@ -268,6 +315,10 @@ macx:{
QMAKE_RPATHDIR=$ORIGIN
ICON = "icons\ico.icns"
LIBS += -lutil
contains(DEFINES, ENABLE_PYTHON) {
QMAKE_CXXFLAGS += $$system("python3-config --cflags")
LIBS += $$system("python3-config --ldflags --embed")
}

build_info.commands = $$quote("cd $$PWD && ./tools/generate_info.sh > build_info.inc")
}
Expand Down
1 change: 1 addition & 0 deletions res/resource.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<file>../icons/about.png</file>
<file>../icons/about2.png</file>
<file>../icons/aboutqt.png</file>
<file>../icons/aboutpython.png</file>
</qresource>
<qresource prefix="/lang">
<file>../lang/quardCRT_zh_CN.qm</file>
Expand Down
17 changes: 17 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "mainwindow.h"
#include "globalsetting.h"
#include "logger.h"
#ifdef ENABLE_PYTHON
#include "pyrun.h"
#endif

#include "build_info.inc"

Expand Down Expand Up @@ -101,6 +104,14 @@ class CommandLineParser
""
)
},
{"override_python_lib",
QCommandLineOption(
{"ovp","override_python_lib"},
"override python lib path",
"override_python_lib",
""
)
},
};

public:
Expand Down Expand Up @@ -272,6 +283,12 @@ int main(int argc, char *argv[])
if(AppComLineParser->isSetOpt("start_know_session")) {
start_know_session = AppComLineParser->getOpt("start_know_session");
}
#ifdef ENABLE_PYTHON
if(AppComLineParser->isSetOpt("override_python_lib")) {
QString path = AppComLineParser->getOpt("override_python_lib");
PyRun::setOverridePythonLib(path);
}
#endif
QFileInfo start_dir(dir);
if(start_dir.isFile()) {
dir = start_dir.absolutePath();
Expand Down
Loading

0 comments on commit f05d12d

Please sign in to comment.