Skip to content

Commit

Permalink
add feat: set log dir based on executable path
Browse files Browse the repository at this point in the history
  • Loading branch information
zfb132 committed Dec 25, 2023
1 parent 7375013 commit dfc1e63
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/publish-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ jobs:
./config_env.bat
- name: Set version
if: contains(github.ref, 'tags/')
shell: bash
run: |
export VERSION="${{ github.ref_name }}"
if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
export VERSION="${{ github.ref_name }}"
else
export VERSION="v0.0.0"
fi
export VERSION="${VERSION#v}"
export MAJOR="${VERSION%%.*}"
export MINOR="${VERSION#*.}"
Expand All @@ -56,7 +59,6 @@ jobs:
./publish.bat
- name: Upload release file
if: contains(github.ref, 'tags/')
uses: actions/upload-artifact@v3
with:
name: release
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ __pycache__/
*$py.class
[Ll]og/
*.db
*.bat
*.sh
*.zip

# C extensions
*.so
Expand Down
12 changes: 8 additions & 4 deletions batch_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
from cv2 import imdecode, cvtColor, IMREAD_UNCHANGED, COLOR_GRAY2RGB, COLOR_RGBA2RGB, wechat_qrcode_WeChatQRCode
from numpy import fromfile, uint8, uint16
from shutil import move
from sys import exit

from multiprocessing import Event, cpu_count, Pool
from os.path import join, dirname, basename, exists, splitext, normpath
from os import walk, remove
from PyQt5.QtCore import QObject, pyqtSignal

from sql_helper import insert_file, insert_status, clean_files_table, clean_status_table, get_all_files
from utils import get_base_path

def setup_event(event):
global unpaused
Expand All @@ -23,12 +25,14 @@ def setup_event(event):
try:
# 使用opencv的wechat_qrcode模块创建二维码识别器
# https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode
model_base_path = join(get_base_path(), "models")
detector = wechat_qrcode_WeChatQRCode(
"models/detect.prototxt", "models/detect.caffemodel",
"models/sr.prototxt", "models/sr.caffemodel"
join(model_base_path, "detect.prototxt"), join(model_base_path, "detect.caffemodel"),
join(model_base_path, "sr.prototxt"), join(model_base_path, "sr.caffemodel")
)
# print("创建识别器成功")
except:
except Exception as e:
print(repr(e))
print("初始化识别器失败!")
exit(0)

Expand Down Expand Up @@ -360,7 +364,7 @@ def run(self):
self.save_qrcode(cut_path, qrcode)
else:
if self.log_file:
self.save_qrcode(join(dirname(__file__), "log"), qrcode, self.log_file)
self.save_qrcode(join(get_base_path(), "log"), qrcode, self.log_file)
self.notifyProgress.emit((i+1)*100//num_procs)
# logging.info(f"进程{i}结束")
logging.info(f"扫描任务结束:")
Expand Down
3 changes: 2 additions & 1 deletion custom_qwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from datetime import datetime

from sql_helper import get_status, clean_files_table, clean_status_table
from utils import get_base_path

class QPlainTextEditLogger(QObject, logging.Handler):
'''自定义Qt控件,继承自QObject,初始化时创建QPlainTextEdit控件\n
Expand Down Expand Up @@ -165,7 +166,7 @@ def get_cut_path(self):
self.cutPathTextBox.setText(directory)

def set_log_file(self):
log_name = path.join(path.dirname(__file__), "log", f"{datetime.now().strftime('%Y%m%d%H%M%S')}.txt")
log_name = path.join(get_base_path(), "log", f"{datetime.now().strftime('%Y%m%d%H%M%S')}.txt")
try:
self.log_file = open(log_name, "w", encoding="utf-8")
logging.info(f"日志文件{log_name}创建成功")
Expand Down
3 changes: 2 additions & 1 deletion pyqt5_qr_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from custom_qwidget import QrDetectDialog, scan_process
from sql_helper import create_files_table, create_status_table
from utils import get_base_path

# 用于把图片资源嵌入到Qt程序里面
# 发布exe时就不需要附该文件了
Expand All @@ -22,7 +23,7 @@
def log_init():
try:
# 当前程序的路径
log_dir = os.path.join(os.path.dirname(__file__), "log")
log_dir = os.path.join(get_base_path(), "log")
if not os.path.exists(log_dir):
os.makedirs(log_dir)
logging.info(f"日志文件夹{log_dir}创建成功")
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pyqt5
pyinstaller
pyinstaller>=6.3
opencv-python==4.6.0.66
opencv-contrib-python==4.6.0.66
pycryptodome
Expand Down
23 changes: 23 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: 'zfb'
# time: 2023-12-25 18:26

import sys
# from os import getcwd
from os.path import dirname, abspath

def get_base_path():
"""
获取当前运行目录,适应于Python和PyInstaller环境
"""
# path = getcwd()
path = dirname(abspath(__file__))
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
# 如果是PyInstaller环境
# 可以使用 sys.executable 获取当前运行的exe路径
# 也可以使用 sys._MEIPASS 获取当前运行的exe所在目录下的_internal文件夹的路径
# 例如 D:\QrScan\bin\QrScan\_internal
# 获取 _internal 文件夹的路径,然后使用 dirname 获取上一级目录
path = dirname(sys._MEIPASS)
return path

0 comments on commit dfc1e63

Please sign in to comment.