From a4f9cf12c8852f5ae17d4820904b814742108fa3 Mon Sep 17 00:00:00 2001 From: AutumnSun1996 Date: Sun, 21 Aug 2022 21:34:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=84=9A=E6=9C=AC,=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8pyinstaller=E6=89=93=E5=8C=85=E4=B8=BAexe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 +++-- ocrweb_multi/build.py | 21 ++++++++++++++ ocrweb_multi/main.spec | 52 ++++++++++++++++++++++++++++++++++ ocrweb_multi/utils/config.py | 10 ++++++- ocrweb_multi/wrapper.c | 55 ++++++++++++++++++++++++++++++++++++ ocrweb_multi/wrapper.rc | 1 + 6 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 ocrweb_multi/build.py create mode 100644 ocrweb_multi/main.spec create mode 100644 ocrweb_multi/wrapper.c create mode 100644 ocrweb_multi/wrapper.rc diff --git a/.gitignore b/.gitignore index 4c37a592b..edaddfb66 100755 --- a/.gitignore +++ b/.gitignore @@ -33,8 +33,9 @@ MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec +# *.manifest +# *.spec +*.res # Installer logs pip-log.txt @@ -154,4 +155,4 @@ long1.jpg *.pdiparams.info *.pdmodel -.DS_Store \ No newline at end of file +.DS_Store diff --git a/ocrweb_multi/build.py b/ocrweb_multi/build.py new file mode 100644 index 000000000..a23ee2791 --- /dev/null +++ b/ocrweb_multi/build.py @@ -0,0 +1,21 @@ +import os +import shutil + +print('Compile ocrweb') +os.system('pyinstaller -y main.spec') + +print('Compile wrapper') +os.system('windres .\wrapper.rc -O coff -o wrapper.res') +os.system('gcc .\wrapper.c wrapper.res -o dist/ocrweb.exe') + +print('Copy config.yaml') +shutil.copy2('config.yaml', 'dist/config.yaml') + +print('Copy models') +shutil.copytree('models', 'dist/models', dirs_exist_ok=True) +os.remove('dist/models/.gitkeep') + +print('Pack to ocrweb.zip') +shutil.make_archive('ocrweb', 'zip', 'dist') + +print('Done') diff --git a/ocrweb_multi/main.spec b/ocrweb_multi/main.spec new file mode 100644 index 000000000..8cd613c84 --- /dev/null +++ b/ocrweb_multi/main.spec @@ -0,0 +1,52 @@ +# -*- mode: python ; coding: utf-8 -*- + + +block_cipher = None + + +a = Analysis( + ['main.py'], + pathex=[], + binaries=[], + datas=[ + ('static', 'static'), + ], + hiddenimports=[], + hookspath=[], + hooksconfig={}, + runtime_hooks=[], + excludes=[], + win_no_prefer_redirects=False, + win_private_assemblies=False, + cipher=block_cipher, + noarchive=False, +) +pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) + +exe = EXE( + pyz, + a.scripts, + [], + exclude_binaries=True, + name='main', + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + console=True, + disable_windowed_traceback=False, + argv_emulation=False, + target_arch=None, + codesign_identity=None, + entitlements_file=None, +) +coll = COLLECT( + exe, + a.binaries, + a.zipfiles, + a.datas, + strip=False, + upx=True, + upx_exclude=[], + name='ocrweb', +) diff --git a/ocrweb_multi/utils/config.py b/ocrweb_multi/utils/config.py index 8a76e644a..337e01f56 100644 --- a/ocrweb_multi/utils/config.py +++ b/ocrweb_multi/utils/config.py @@ -4,15 +4,23 @@ root_dir = Path(__file__).parent.parent + def get_resource_path(name: str): """依次检查资源文件的多个可能路径, 返回首个存在的路径""" for path in [ - Path(name), + # wrapper.exe 所在目录 + Path(root_dir.parent, name), + # main.exe 所在目录 / main.py 所在目录 Path(root_dir, name), + # main.exe 所在目录 Path(sys.argv[0]).parent / name, + # 工作目录 + Path(name), ]: if path.exists(): + print('Loaded:', path) return path raise FileNotFoundError(name) + conf = yaml.safe_load(get_resource_path('config.yaml').read_text()) diff --git a/ocrweb_multi/wrapper.c b/ocrweb_multi/wrapper.c new file mode 100644 index 000000000..246726f08 --- /dev/null +++ b/ocrweb_multi/wrapper.c @@ -0,0 +1,55 @@ +/* +针对Pyinstaller目录下文件过多的问题, 使用外部exe+system调用的方式实现资源文件/依赖库分离 +*/ +#include +#include + +void combine(char *destination, const char *path1, const char *path2) +{ + if (path1 == NULL && path2 == NULL) + { + strcpy(destination, ""); + } + else if (path2 == NULL || strlen(path2) == 0) + { + strcpy(destination, path1); + } + else if (path1 == NULL || strlen(path1) == 0) + { + strcpy(destination, path2); + } + else + { + strcpy(destination, path1); + + size_t idx = 0, sepIdx = 0; + size_t size1 = strlen(path1); + while (idx < size1) + { + idx++; + if (destination[idx] == '\\' || destination[idx] == '/') + { + sepIdx = idx; + } + } + // Trim destination: delete from last separator to end. + destination[sepIdx + 1] = '\0'; + strcat(destination, path2); + } +} + +void main() +{ + // Set title + system("title Rapid OCR Server"); + // Get wrapper exe path + TCHAR path[MAX_PATH]; + GetModuleFileName(NULL, path, MAX_PATH); + + TCHAR exe_path[MAX_PATH]; + // Get real exe path from wrapper exe path + combine(exe_path, path, "ocrweb\\main.exe"); + printf("Run real exe: %s\n", exe_path); + // Run real exe + system(exe_path); +} diff --git a/ocrweb_multi/wrapper.rc b/ocrweb_multi/wrapper.rc new file mode 100644 index 000000000..3dc1b6377 --- /dev/null +++ b/ocrweb_multi/wrapper.rc @@ -0,0 +1 @@ +id ICON "static/favicon.ico"