Skip to content

Commit

Permalink
完成了多平台统一配置版本信息
Browse files Browse the repository at this point in the history
Signed-off-by: Sadam·Sadik <1903249375@qq.com>
  • Loading branch information
Haoke98 committed Sep 30, 2024
1 parent 2c61e92 commit 30e6db4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 47 deletions.
55 changes: 9 additions & 46 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,16 @@
@disc:
======================================="""
import logging
import os
from importlib.metadata import version

from pip._internal.network.session import PipSession
from pip._internal.req import parse_requirements
from setuptools import setup

from setup_utils import parse_version_file, _parse_requirements, tree_replace

def _parse_requirements(file_path):
# 获取pip的版本
pip_ver = version('pip')
pip_version = list(map(int, pip_ver.split('.')[:2]))

# 根据pip的版本选择不同的方法来解析requirements
if pip_version >= [20, 0]: # 这里的版本号需要根据实际情况调整
requirements = parse_requirements(
file_path,
session=PipSession()
)
else:
requirements = parse_requirements(file_path)

# 返回requirements列表
return [str(req.requirement) for req in requirements]


def tree(src):
resp = []
for (_root, dirs, files) in os.walk(os.path.normpath(src)):
fps = []
for f in files:
fp = os.path.join(_root, f)
fps.append(fp)
resp.append((_root, fps))
return resp


def tree_replace(src_folder, dst_folder):
resp = []
for (_root, dirs, files) in os.walk(os.path.normpath(src_folder)):
fps = []
for f in files:
fp = os.path.join(_root, f)
fps.append(fp)
resp.append((dst_folder, fps))
return resp

# 获取版本信息
file_version, product_version, copyright_text = parse_version_file('version.txt')
print("File version({}): {}".format(type(file_version), file_version))
print("Product version({}): {}".format(type(product_version), product_version))
print("Copyright text({}): {}".format(type(copyright_text), copyright_text))

# parse_requirements() returns generator of pip.req.InstallRequirement objects
try:
Expand All @@ -75,9 +38,9 @@ def tree_replace(src_folder, dst_folder):
'CFBundleDisplayName': 'AllKeeper',
'CFBundleGetInfoString': "AllKeeper",
'CFBundleIdentifier': 'com.0p.AllKeeper',
'CFBundleVersion': "2024.8.22.19",
'CFBundleShortVersionString': "1.0.0",
'NSHumanReadableCopyright': 'Copyright © 2024 新疆数智创想软件开发工作室. All rights reserved.',
'CFBundleVersion': file_version, # 使用从 version.txt 中解析的 FileVersion
'CFBundleShortVersionString': product_version, # 使用从 version.txt 中解析的 ProductVersion
'NSHumanReadableCopyright': copyright_text, # 使用从 version.txt 中解析的版权信息
}
}

Expand Down
70 changes: 70 additions & 0 deletions setup_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# _*_ codign:utf8 _*_
"""====================================
@Author:Sadam·Sadik
@Email:1903249375@qq.com
@Date:2024/9/30
@Software: PyCharm
@disc:
======================================="""
import os
import re

from pip._internal.network.session import PipSession
from pip._internal.req import parse_requirements


# 从version.txt读取并解析版本信息
def parse_version_file(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

# 使用正则表达式提取版本信息
file_version_match = re.search(r'FileVersion\', u\'([0-9.]+)\'', content)
product_version_match = re.search(r'ProductVersion\', u\'([0-9.]+)\'', content)
copyright_match = re.search(r'LegalCopyright\', u\'(.*?)\'', content)

file_version = file_version_match.group(1) if file_version_match else None
product_version = product_version_match.group(1) if product_version_match else None
copyright_text = copyright_match.group(1) if copyright_match else None

return file_version, product_version, copyright_text


def _parse_requirements(file_path):
# 获取pip的版本
pip_ver = version('pip')
pip_version = list(map(int, pip_ver.split('.')[:2]))

# 根据pip的版本选择不同的方法来解析requirements
if pip_version >= [20, 0]: # 这里的版本号需要根据实际情况调整
requirements = parse_requirements(
file_path,
session=PipSession()
)
else:
requirements = parse_requirements(file_path)

# 返回requirements列表
return [str(req.requirement) for req in requirements]


def tree(src):
resp = []
for (_root, dirs, files) in os.walk(os.path.normpath(src)):
fps = []
for f in files:
fp = os.path.join(_root, f)
fps.append(fp)
resp.append((_root, fps))
return resp


def tree_replace(src_folder, dst_folder):
resp = []
for (_root, dirs, files) in os.walk(os.path.normpath(src_folder)):
fps = []
for f in files:
fp = os.path.join(_root, f)
fps.append(fp)
resp.append((dst_folder, fps))
return resp
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ VSVersionInfo(
StringStruct(u'FileDescription', u'密码和密钥保存工具'), # 文件说明
StringStruct(u'FileVersion', u'2024.9.30.12'),
StringStruct(u'InternalName', u'SVN'),
StringStruct(u'LegalCopyright', u'@Copyright 新疆数智创想软件开发工作室有限公司版权所有'), #版权
StringStruct(u'LegalCopyright', u'Copyright © 2024 新疆数智创想软件开发工作室. All rights reserved.'), #版权
StringStruct(u'OriginalFilename', u'AllKeeper.exe'), #原始文件名
StringStruct(u'ProductName', u'AllKeeper'), #产品名称
StringStruct(u'ProductVersion', u'1.1.0')]) #产品版本
Expand Down

0 comments on commit 30e6db4

Please sign in to comment.