Skip to content

Commit

Permalink
添加脚本, 使用pyinstaller打包为exe
Browse files Browse the repository at this point in the history
  • Loading branch information
AutumnSun1996 committed Aug 21, 2022
1 parent 36c5080 commit a4f9cf1
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 4 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,4 +155,4 @@ long1.jpg
*.pdiparams.info
*.pdmodel

.DS_Store
.DS_Store
21 changes: 21 additions & 0 deletions ocrweb_multi/build.py
Original file line number Diff line number Diff line change
@@ -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')
52 changes: 52 additions & 0 deletions ocrweb_multi/main.spec
Original file line number Diff line number Diff line change
@@ -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',
)
10 changes: 9 additions & 1 deletion ocrweb_multi/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
55 changes: 55 additions & 0 deletions ocrweb_multi/wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
针对Pyinstaller目录下文件过多的问题, 使用外部exe+system调用的方式实现资源文件/依赖库分离
*/
#include <windows.h>
#include <stdio.h>

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);
}
1 change: 1 addition & 0 deletions ocrweb_multi/wrapper.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id ICON "static/favicon.ico"

0 comments on commit a4f9cf1

Please sign in to comment.