unifypy 2.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unifypy/__init__.py +7 -0
- unifypy/__main__.py +21 -0
- unifypy/cli/__init__.py +10 -0
- unifypy/cli/argument_parser.py +209 -0
- unifypy/core/__init__.py +8 -0
- unifypy/core/config.py +491 -0
- unifypy/core/context.py +36 -0
- unifypy/core/engine.py +93 -0
- unifypy/core/environment.py +509 -0
- unifypy/core/event_bus.py +37 -0
- unifypy/core/events.py +16 -0
- unifypy/core/platforms.py +58 -0
- unifypy/core/plugin.py +65 -0
- unifypy/core/validators/__init__.py +10 -0
- unifypy/core/validators/tool_validator.py +144 -0
- unifypy/platforms/__init__.py +10 -0
- unifypy/platforms/base.py +127 -0
- unifypy/platforms/linux/__init__.py +10 -0
- unifypy/platforms/linux/deb_packager.py +289 -0
- unifypy/platforms/linux/rpm_packager.py +332 -0
- unifypy/platforms/macos/__init__.py +10 -0
- unifypy/platforms/macos/dmg_packager.py +477 -0
- unifypy/platforms/macos/post_processor.py +272 -0
- unifypy/platforms/registry.py +108 -0
- unifypy/platforms/windows/__init__.py +9 -0
- unifypy/platforms/windows/inno_setup.py +715 -0
- unifypy/plugins/__init__.py +3 -0
- unifypy/plugins/auto_rollback_plugin.py +156 -0
- unifypy/plugins/cleanup_plugin.py +22 -0
- unifypy/plugins/config_plugin.py +42 -0
- unifypy/plugins/dry_run_plugin.py +22 -0
- unifypy/plugins/environment_plugin.py +61 -0
- unifypy/plugins/external_plugins_loader.py +42 -0
- unifypy/plugins/packaging_plugin.py +125 -0
- unifypy/plugins/prepare_plugin.py +89 -0
- unifypy/plugins/progress_plugin.py +32 -0
- unifypy/plugins/pyinstaller_plugin.py +95 -0
- unifypy/plugins/rollback_plugin.py +62 -0
- unifypy/plugins/summary_plugin.py +36 -0
- unifypy/plugins/windows_language_plugin.py +85 -0
- unifypy/pyinstaller/__init__.py +7 -0
- unifypy/pyinstaller/config_builder.py +908 -0
- unifypy/templates/ChineseSimplified.isl.template +403 -0
- unifypy/templates/setup.iss.template +51 -0
- unifypy/tools/create-dmg/.this-is-the-create-dmg-repo +3 -0
- unifypy/tools/create-dmg/LICENSE +22 -0
- unifypy/tools/create-dmg/Makefile +33 -0
- unifypy/tools/create-dmg/README.md +126 -0
- unifypy/tools/create-dmg/builder/create-dmg.builder +26 -0
- unifypy/tools/create-dmg/create-dmg +621 -0
- unifypy/tools/create-dmg/doc-project/Developer Notes.md +35 -0
- unifypy/tools/create-dmg/doc-project/Release Checklist.md +10 -0
- unifypy/tools/create-dmg/examples/01-main-example/installer_background.png +0 -0
- unifypy/tools/create-dmg/examples/01-main-example/sample +30 -0
- unifypy/tools/create-dmg/examples/01-main-example/source_folder/Application.app +0 -0
- unifypy/tools/create-dmg/support/eula-resources-template.xml +105 -0
- unifypy/tools/create-dmg/support/template.applescript +74 -0
- unifypy/tools/create-dmg/tests/007-space-in-dir-name/my files/hello.txt +1 -0
- unifypy/tools/create-dmg/tests/007-space-in-dir-name/run-test +5 -0
- unifypy/utils/__init__.py +10 -0
- unifypy/utils/cache_manager.py +957 -0
- unifypy/utils/command_runner.py +195 -0
- unifypy/utils/entitlements_generator.py +450 -0
- unifypy/utils/file_ops.py +333 -0
- unifypy/utils/icon_converter.py +144 -0
- unifypy/utils/info_plist_updater.py +247 -0
- unifypy/utils/macos_codesign.py +225 -0
- unifypy/utils/parallel_builder.py +355 -0
- unifypy/utils/progress.py +371 -0
- unifypy/utils/progress_callback_factory.py +56 -0
- unifypy/utils/rollback.py +484 -0
- unifypy/utils/tool_manager.py +701 -0
- unifypy-2.0.2.dist-info/METADATA +609 -0
- unifypy-2.0.2.dist-info/RECORD +78 -0
- unifypy-2.0.2.dist-info/WHEEL +5 -0
- unifypy-2.0.2.dist-info/entry_points.txt +2 -0
- unifypy-2.0.2.dist-info/licenses/LICENSE +21 -0
- unifypy-2.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
文件操作工具 提供跨平台的文件和目录操作功能.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
from unifypy.core.platforms import normalize_platform
|
|
9
|
+
import shutil
|
|
10
|
+
import tempfile
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Dict, List, Optional
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FileOperations:
|
|
16
|
+
"""
|
|
17
|
+
文件操作工具类.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self):
|
|
21
|
+
"""
|
|
22
|
+
初始化文件操作工具.
|
|
23
|
+
"""
|
|
24
|
+
self.current_platform = normalize_platform()
|
|
25
|
+
|
|
26
|
+
def create_temp_dir(self, prefix: str = "unifypy_") -> str:
|
|
27
|
+
"""创建临时目录.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
prefix: 目录名前缀
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
str: 临时目录路径
|
|
34
|
+
"""
|
|
35
|
+
return tempfile.mkdtemp(prefix=prefix)
|
|
36
|
+
|
|
37
|
+
def cleanup_temp_dir(self, temp_dir: str):
|
|
38
|
+
"""清理临时目录.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
temp_dir: 临时目录路径
|
|
42
|
+
"""
|
|
43
|
+
if os.path.exists(temp_dir):
|
|
44
|
+
try:
|
|
45
|
+
shutil.rmtree(temp_dir)
|
|
46
|
+
except Exception as e:
|
|
47
|
+
print(f"警告: 无法完全清理临时目录 {temp_dir}: {e}")
|
|
48
|
+
|
|
49
|
+
def ensure_dir(self, dir_path: str):
|
|
50
|
+
"""确保目录存在,不存在则创建.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
dir_path: 目录路径
|
|
54
|
+
"""
|
|
55
|
+
Path(dir_path).mkdir(parents=True, exist_ok=True)
|
|
56
|
+
|
|
57
|
+
def copy_file(self, src: str, dst: str):
|
|
58
|
+
"""复制文件.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
src: 源文件路径
|
|
62
|
+
dst: 目标文件路径
|
|
63
|
+
"""
|
|
64
|
+
self.ensure_dir(os.path.dirname(dst))
|
|
65
|
+
shutil.copy2(src, dst)
|
|
66
|
+
|
|
67
|
+
def copy_tree(self, src: str, dst: str):
|
|
68
|
+
"""复制目录树.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
src: 源目录路径
|
|
72
|
+
dst: 目标目录路径
|
|
73
|
+
"""
|
|
74
|
+
if os.path.exists(dst):
|
|
75
|
+
shutil.rmtree(dst)
|
|
76
|
+
shutil.copytree(src, dst)
|
|
77
|
+
|
|
78
|
+
def move_file(self, src: str, dst: str):
|
|
79
|
+
"""移动文件.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
src: 源文件路径
|
|
83
|
+
dst: 目标文件路径
|
|
84
|
+
"""
|
|
85
|
+
self.ensure_dir(os.path.dirname(dst))
|
|
86
|
+
shutil.move(src, dst)
|
|
87
|
+
|
|
88
|
+
def remove_file(self, file_path: str):
|
|
89
|
+
"""删除文件.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
file_path: 文件路径
|
|
93
|
+
"""
|
|
94
|
+
if os.path.exists(file_path):
|
|
95
|
+
os.remove(file_path)
|
|
96
|
+
|
|
97
|
+
def remove_dir(self, dir_path: str):
|
|
98
|
+
"""删除目录.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
dir_path: 目录路径
|
|
102
|
+
"""
|
|
103
|
+
if os.path.exists(dir_path):
|
|
104
|
+
shutil.rmtree(dir_path)
|
|
105
|
+
|
|
106
|
+
def get_file_size(self, file_path: str) -> int:
|
|
107
|
+
"""获取文件大小.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
file_path: 文件路径
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
int: 文件大小(字节)
|
|
114
|
+
"""
|
|
115
|
+
return os.path.getsize(file_path) if os.path.exists(file_path) else 0
|
|
116
|
+
|
|
117
|
+
def format_file_size(self, size_bytes: int) -> str:
|
|
118
|
+
"""格式化文件大小.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
size_bytes: 字节数
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
str: 格式化的大小字符串
|
|
125
|
+
"""
|
|
126
|
+
for unit in ["B", "KB", "MB", "GB", "TB"]:
|
|
127
|
+
if size_bytes < 1024.0:
|
|
128
|
+
return f"{size_bytes:.1f} {unit}"
|
|
129
|
+
size_bytes /= 1024.0
|
|
130
|
+
return f"{size_bytes:.1f} PB"
|
|
131
|
+
|
|
132
|
+
def find_files(
|
|
133
|
+
self, directory: str, pattern: str = "*", recursive: bool = True
|
|
134
|
+
) -> List[str]:
|
|
135
|
+
"""查找文件.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
directory: 搜索目录
|
|
139
|
+
pattern: 文件名模式
|
|
140
|
+
recursive: 是否递归搜索
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
List[str]: 匹配的文件路径列表
|
|
144
|
+
"""
|
|
145
|
+
from pathlib import Path
|
|
146
|
+
|
|
147
|
+
path = Path(directory)
|
|
148
|
+
if not path.exists():
|
|
149
|
+
return []
|
|
150
|
+
|
|
151
|
+
files = []
|
|
152
|
+
if recursive:
|
|
153
|
+
for file_path in path.rglob(pattern):
|
|
154
|
+
if file_path.is_file():
|
|
155
|
+
files.append(str(file_path))
|
|
156
|
+
else:
|
|
157
|
+
for file_path in path.glob(pattern):
|
|
158
|
+
if file_path.is_file():
|
|
159
|
+
files.append(str(file_path))
|
|
160
|
+
|
|
161
|
+
return files
|
|
162
|
+
|
|
163
|
+
def get_executable_extension(self) -> str:
|
|
164
|
+
"""获取当前平台的可执行文件扩展名.
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
str: 扩展名
|
|
168
|
+
"""
|
|
169
|
+
return ".exe" if self.current_platform == "windows" else ""
|
|
170
|
+
|
|
171
|
+
def make_executable(self, file_path: str):
|
|
172
|
+
"""设置文件为可执行.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
file_path: 文件路径
|
|
176
|
+
"""
|
|
177
|
+
if os.path.exists(file_path):
|
|
178
|
+
current_mode = os.stat(file_path).st_mode
|
|
179
|
+
os.chmod(file_path, current_mode | 0o755)
|
|
180
|
+
|
|
181
|
+
def check_disk_space(self, path: str, required_mb: int = 100) -> bool:
|
|
182
|
+
"""检查磁盘空间是否足够.
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
path: 路径
|
|
186
|
+
required_mb: 所需空间(MB)
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
bool: 空间是否足够
|
|
190
|
+
"""
|
|
191
|
+
try:
|
|
192
|
+
statvfs = os.statvfs(path) if hasattr(os, "statvfs") else None
|
|
193
|
+
if statvfs:
|
|
194
|
+
# Unix/Linux/macOS
|
|
195
|
+
free_bytes = statvfs.f_frsize * statvfs.f_bavail
|
|
196
|
+
else:
|
|
197
|
+
# Windows
|
|
198
|
+
import shutil
|
|
199
|
+
|
|
200
|
+
free_bytes = shutil.disk_usage(path).free
|
|
201
|
+
|
|
202
|
+
required_bytes = required_mb * 1024 * 1024
|
|
203
|
+
return free_bytes >= required_bytes
|
|
204
|
+
except:
|
|
205
|
+
return True # 无法检查时假设有足够空间
|
|
206
|
+
|
|
207
|
+
def create_version_info_file(self, app_info: Dict[str, Any], output_path: str):
|
|
208
|
+
"""创建Windows版本信息文件.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
app_info: 应用信息字典
|
|
212
|
+
output_path: 输出文件路径
|
|
213
|
+
"""
|
|
214
|
+
version_template = """# UTF-8
|
|
215
|
+
#
|
|
216
|
+
# For more details about fixed file info 'ffi' see:
|
|
217
|
+
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
|
|
218
|
+
VSVersionInfo(
|
|
219
|
+
ffi=FixedFileInfo(
|
|
220
|
+
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
|
|
221
|
+
# Set not needed items to zero 0.
|
|
222
|
+
filevers={file_version},
|
|
223
|
+
prodvers={product_version},
|
|
224
|
+
# Contains a bitmask that specifies the valid bits 'flags'r
|
|
225
|
+
mask=0x3f,
|
|
226
|
+
# Contains a bitmask that specifies the Boolean attributes of the file.
|
|
227
|
+
flags=0x0,
|
|
228
|
+
# The operating system for which this file was designed.
|
|
229
|
+
# 0x4 - NT and there is no need to change it.
|
|
230
|
+
OS=0x4,
|
|
231
|
+
# The general type of file.
|
|
232
|
+
# 0x1 - the file is an application.
|
|
233
|
+
fileType=0x1,
|
|
234
|
+
# The function of the file.
|
|
235
|
+
# 0x0 - the function is not defined for this fileType
|
|
236
|
+
subtype=0x0,
|
|
237
|
+
# Creation date and time stamp.
|
|
238
|
+
date=(0, 0)
|
|
239
|
+
),
|
|
240
|
+
kids=[
|
|
241
|
+
StringFileInfo(
|
|
242
|
+
[
|
|
243
|
+
StringTable(
|
|
244
|
+
'040904B0',
|
|
245
|
+
[StringStruct('CompanyName', '{company}'),
|
|
246
|
+
StringStruct('FileDescription', '{description}'),
|
|
247
|
+
StringStruct('FileVersion', '{file_version_str}'),
|
|
248
|
+
StringStruct('InternalName', '{internal_name}'),
|
|
249
|
+
StringStruct('LegalCopyright', '{copyright}'),
|
|
250
|
+
StringStruct('OriginalFilename', '{original_filename}'),
|
|
251
|
+
StringStruct('ProductName', '{product_name}'),
|
|
252
|
+
StringStruct('ProductVersion', '{product_version_str}')])
|
|
253
|
+
]),
|
|
254
|
+
VarFileInfo([VarStruct('Translation', [1033, 1200])])
|
|
255
|
+
]
|
|
256
|
+
)"""
|
|
257
|
+
|
|
258
|
+
# 解析版本号
|
|
259
|
+
version = app_info.get("version", "1.0.0")
|
|
260
|
+
version_parts = version.split(".")
|
|
261
|
+
while len(version_parts) < 4:
|
|
262
|
+
version_parts.append("0")
|
|
263
|
+
|
|
264
|
+
file_version = tuple(int(part) for part in version_parts[:4])
|
|
265
|
+
product_version = file_version
|
|
266
|
+
|
|
267
|
+
# 填充模板
|
|
268
|
+
version_info = version_template.format(
|
|
269
|
+
file_version=file_version,
|
|
270
|
+
product_version=product_version,
|
|
271
|
+
company=app_info.get("publisher", "Unknown Publisher"),
|
|
272
|
+
description=app_info.get(
|
|
273
|
+
"display_name", app_info.get("name", "Application")
|
|
274
|
+
),
|
|
275
|
+
file_version_str=version,
|
|
276
|
+
internal_name=app_info.get("name", "app"),
|
|
277
|
+
copyright=f"Copyright © {app_info.get('publisher', 'Unknown Publisher')}",
|
|
278
|
+
original_filename=f"{app_info.get('name', 'app')}.exe",
|
|
279
|
+
product_name=app_info.get(
|
|
280
|
+
"display_name", app_info.get("name", "Application")
|
|
281
|
+
),
|
|
282
|
+
product_version_str=version,
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
with open(output_path, "w", encoding="utf-8") as f:
|
|
286
|
+
f.write(version_info)
|
|
287
|
+
|
|
288
|
+
def normalize_path(self, path: str, target_platform: Optional[str] = None) -> str:
|
|
289
|
+
"""规范化路径格式.
|
|
290
|
+
|
|
291
|
+
Args:
|
|
292
|
+
path: 原始路径
|
|
293
|
+
target_platform: 目标平台,None表示当前平台
|
|
294
|
+
|
|
295
|
+
Returns:
|
|
296
|
+
str: 规范化的路径
|
|
297
|
+
"""
|
|
298
|
+
platform = target_platform or self.current_platform
|
|
299
|
+
|
|
300
|
+
if platform == "windows":
|
|
301
|
+
return path.replace("/", "\\")
|
|
302
|
+
else:
|
|
303
|
+
return path.replace("\\", "/")
|
|
304
|
+
|
|
305
|
+
def get_relative_path(self, path: str, base_path: str) -> str:
|
|
306
|
+
"""获取相对路径.
|
|
307
|
+
|
|
308
|
+
Args:
|
|
309
|
+
path: 目标路径
|
|
310
|
+
base_path: 基准路径
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
str: 相对路径
|
|
314
|
+
"""
|
|
315
|
+
return os.path.relpath(path, base_path)
|
|
316
|
+
|
|
317
|
+
def resolve_path(self, path: str, base_path: Optional[str] = None) -> str:
|
|
318
|
+
"""解析路径为绝对路径.
|
|
319
|
+
|
|
320
|
+
Args:
|
|
321
|
+
path: 路径
|
|
322
|
+
base_path: 基准路径
|
|
323
|
+
|
|
324
|
+
Returns:
|
|
325
|
+
str: 绝对路径
|
|
326
|
+
"""
|
|
327
|
+
if os.path.isabs(path):
|
|
328
|
+
return path
|
|
329
|
+
|
|
330
|
+
if base_path:
|
|
331
|
+
return os.path.abspath(os.path.join(base_path, path))
|
|
332
|
+
else:
|
|
333
|
+
return os.path.abspath(path)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
简化的图标格式转换工具 支持基本的 PNG -> ICNS 转换.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import subprocess
|
|
9
|
+
import tempfile
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class IconConverter:
|
|
15
|
+
"""
|
|
16
|
+
简化的图标格式转换器.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, verbose: bool = False, progress_callback=None):
|
|
20
|
+
self.verbose = verbose
|
|
21
|
+
self.progress_callback = progress_callback
|
|
22
|
+
|
|
23
|
+
def _print(self, message: str):
|
|
24
|
+
"""
|
|
25
|
+
只在 verbose 模式下打印信息.
|
|
26
|
+
"""
|
|
27
|
+
if self.verbose:
|
|
28
|
+
print(message)
|
|
29
|
+
|
|
30
|
+
def ensure_icon_format(
|
|
31
|
+
self, icon_path: str, target_format: str, project_dir: Path
|
|
32
|
+
) -> Optional[str]:
|
|
33
|
+
"""确保图标是指定格式,如果不是则转换.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
icon_path: 图标路径(相对或绝对)
|
|
37
|
+
target_format: 目标格式 ('icns', 'ico', 'png')
|
|
38
|
+
project_dir: 项目目录
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
Optional[str]: 最终的图标路径,失败返回 None
|
|
42
|
+
"""
|
|
43
|
+
if not icon_path:
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
# 解析路径
|
|
47
|
+
if os.path.isabs(icon_path):
|
|
48
|
+
source_path = Path(icon_path)
|
|
49
|
+
else:
|
|
50
|
+
source_path = project_dir / icon_path
|
|
51
|
+
|
|
52
|
+
if not source_path.exists():
|
|
53
|
+
print(f"❌ 图标文件不存在: {source_path}") # 错误信息始终显示
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
# 检查是否已经是目标格式
|
|
57
|
+
current_ext = source_path.suffix.lower()
|
|
58
|
+
target_ext = f".{target_format.lower()}"
|
|
59
|
+
|
|
60
|
+
if current_ext == target_ext:
|
|
61
|
+
return str(source_path)
|
|
62
|
+
|
|
63
|
+
# 对于 macOS,如果是 PNG 转 ICNS,尝试转换
|
|
64
|
+
if target_format.lower() == "icns" and current_ext == ".png":
|
|
65
|
+
target_path = source_path.parent / f"{source_path.stem}.icns"
|
|
66
|
+
|
|
67
|
+
if self._convert_png_to_icns(source_path, target_path):
|
|
68
|
+
return str(target_path)
|
|
69
|
+
|
|
70
|
+
# 如果转换失败或不支持,返回原始路径
|
|
71
|
+
print(f"ℹ️ 使用原始图标格式: {source_path}")
|
|
72
|
+
return str(source_path)
|
|
73
|
+
|
|
74
|
+
def _convert_png_to_icns(self, png_path: Path, icns_path: Path) -> bool:
|
|
75
|
+
"""使用 macOS 系统工具将 PNG 转换为 ICNS.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
png_path: 源 PNG 文件
|
|
79
|
+
icns_path: 目标 ICNS 文件
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
bool: 转换是否成功
|
|
83
|
+
"""
|
|
84
|
+
try:
|
|
85
|
+
# 创建临时 iconset 目录
|
|
86
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
|
87
|
+
iconset_dir = Path(temp_dir) / "icon.iconset"
|
|
88
|
+
iconset_dir.mkdir()
|
|
89
|
+
|
|
90
|
+
# 生成标准尺寸
|
|
91
|
+
sizes = [16, 32, 64, 128, 256, 512, 1024]
|
|
92
|
+
|
|
93
|
+
for size in sizes:
|
|
94
|
+
# 标准分辨率
|
|
95
|
+
target_file = iconset_dir / f"icon_{size}x{size}.png"
|
|
96
|
+
if not self._resize_with_sips(png_path, target_file, size):
|
|
97
|
+
print(f"❌ 生成 {size}x{size} 图标失败") # 错误信息始终显示
|
|
98
|
+
return False
|
|
99
|
+
|
|
100
|
+
# 高分辨率版本(@2x,除了 1024px)
|
|
101
|
+
if size < 1024:
|
|
102
|
+
target_file_2x = iconset_dir / f"icon_{size}x{size}@2x.png"
|
|
103
|
+
if not self._resize_with_sips(
|
|
104
|
+
png_path, target_file_2x, size * 2
|
|
105
|
+
):
|
|
106
|
+
print(
|
|
107
|
+
f"❌ 生成 {size}x{size}@2x 图标失败"
|
|
108
|
+
) # 错误信息始终显示
|
|
109
|
+
|
|
110
|
+
# 使用 iconutil 转换为 ICNS
|
|
111
|
+
cmd = ["iconutil", "-c", "icns", str(iconset_dir), "-o", str(icns_path)]
|
|
112
|
+
result = subprocess.run(
|
|
113
|
+
cmd, capture_output=True, text=True, check=False
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if result.returncode == 0:
|
|
117
|
+
self._print(f"✅ 图标转换成功: {png_path.name} -> {icns_path.name}")
|
|
118
|
+
return True
|
|
119
|
+
else:
|
|
120
|
+
print(f"❌ iconutil 执行失败: {result.stderr}") # 错误信息始终显示
|
|
121
|
+
return False
|
|
122
|
+
|
|
123
|
+
except Exception as e:
|
|
124
|
+
print(f"❌ 图标转换异常: {e}") # 错误信息始终显示
|
|
125
|
+
return False
|
|
126
|
+
|
|
127
|
+
def _resize_with_sips(self, source: Path, target: Path, size: int) -> bool:
|
|
128
|
+
"""
|
|
129
|
+
使用 sips 调整图像大小.
|
|
130
|
+
"""
|
|
131
|
+
try:
|
|
132
|
+
cmd = [
|
|
133
|
+
"sips",
|
|
134
|
+
"-z",
|
|
135
|
+
str(size),
|
|
136
|
+
str(size),
|
|
137
|
+
str(source),
|
|
138
|
+
"--out",
|
|
139
|
+
str(target),
|
|
140
|
+
]
|
|
141
|
+
result = subprocess.run(cmd, capture_output=True, check=False)
|
|
142
|
+
return result.returncode == 0
|
|
143
|
+
except Exception:
|
|
144
|
+
return False
|