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,355 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
并行构建器 支持多个构建步骤的并行处理.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import concurrent.futures
|
|
8
|
+
import os
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, List, Optional
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ParallelBuilder:
|
|
14
|
+
"""
|
|
15
|
+
并行构建器,支持多格式同时打包.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init__(self, progress_manager, max_workers: Optional[int] = None):
|
|
19
|
+
"""初始化并行构建器.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
progress_manager: 进度管理器
|
|
23
|
+
max_workers: 最大工作线程数,默认为CPU核心数
|
|
24
|
+
"""
|
|
25
|
+
self.progress = progress_manager
|
|
26
|
+
self.max_workers = max_workers or min(4, (os.cpu_count() or 1) + 1)
|
|
27
|
+
self._executor = None
|
|
28
|
+
|
|
29
|
+
def __enter__(self):
|
|
30
|
+
"""
|
|
31
|
+
进入上下文管理器.
|
|
32
|
+
"""
|
|
33
|
+
self._executor = concurrent.futures.ThreadPoolExecutor(
|
|
34
|
+
max_workers=self.max_workers
|
|
35
|
+
)
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
39
|
+
"""
|
|
40
|
+
退出上下文管理器.
|
|
41
|
+
"""
|
|
42
|
+
if self._executor:
|
|
43
|
+
self._executor.shutdown(wait=True)
|
|
44
|
+
self._executor = None
|
|
45
|
+
|
|
46
|
+
def build_multiple_formats(
|
|
47
|
+
self,
|
|
48
|
+
platform: str,
|
|
49
|
+
formats: List[str],
|
|
50
|
+
packager_registry,
|
|
51
|
+
source_path: Path,
|
|
52
|
+
output_dir: Path,
|
|
53
|
+
config: Dict[str, Any],
|
|
54
|
+
) -> Dict[str, bool]:
|
|
55
|
+
"""并行构建多种格式的安装包.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
platform: 平台名称
|
|
59
|
+
formats: 格式列表
|
|
60
|
+
packager_registry: 打包器注册表
|
|
61
|
+
source_path: 源文件路径
|
|
62
|
+
output_dir: 输出目录
|
|
63
|
+
config: 配置字典
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
Dict[str, bool]: 每种格式的构建结果
|
|
67
|
+
"""
|
|
68
|
+
if not self._executor:
|
|
69
|
+
raise RuntimeError("ParallelBuilder must be used as a context manager")
|
|
70
|
+
|
|
71
|
+
# 准备任务
|
|
72
|
+
tasks = []
|
|
73
|
+
format_info = {}
|
|
74
|
+
|
|
75
|
+
for format_type in formats:
|
|
76
|
+
packager_class = packager_registry.get_packager(platform, format_type)
|
|
77
|
+
if not packager_class:
|
|
78
|
+
self.progress.warning(f"未找到 {platform}/{format_type} 格式的打包器")
|
|
79
|
+
continue
|
|
80
|
+
|
|
81
|
+
# 创建打包器实例
|
|
82
|
+
packager = packager_class(
|
|
83
|
+
self.progress,
|
|
84
|
+
None, # 每个线程将创建自己的runner
|
|
85
|
+
None, # 每个线程将创建自己的tool_manager
|
|
86
|
+
config,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# 生成输出文件名
|
|
90
|
+
app_name = config.get("name", "myapp")
|
|
91
|
+
version = config.get("version", "1.0.0")
|
|
92
|
+
output_filename = packager.get_output_filename(
|
|
93
|
+
format_type, app_name, version
|
|
94
|
+
)
|
|
95
|
+
output_path = output_dir / output_filename
|
|
96
|
+
|
|
97
|
+
format_info[format_type] = {
|
|
98
|
+
"packager": packager,
|
|
99
|
+
"source_path": source_path,
|
|
100
|
+
"output_path": output_path,
|
|
101
|
+
"format_type": format_type,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
# 提交任务到线程池
|
|
105
|
+
future = self._executor.submit(
|
|
106
|
+
self._build_single_format,
|
|
107
|
+
format_type,
|
|
108
|
+
packager,
|
|
109
|
+
source_path,
|
|
110
|
+
output_path,
|
|
111
|
+
)
|
|
112
|
+
tasks.append((format_type, future))
|
|
113
|
+
|
|
114
|
+
# 等待所有任务完成
|
|
115
|
+
results = {}
|
|
116
|
+
completed = 0
|
|
117
|
+
total = len(tasks)
|
|
118
|
+
|
|
119
|
+
for format_type, future in tasks:
|
|
120
|
+
try:
|
|
121
|
+
success = future.result(timeout=600) # 10分钟超时
|
|
122
|
+
results[format_type] = success
|
|
123
|
+
completed += 1
|
|
124
|
+
|
|
125
|
+
progress_percentage = int(80 * completed / total)
|
|
126
|
+
status = "✅ 成功" if success else "❌ 失败"
|
|
127
|
+
self.progress.update_stage(
|
|
128
|
+
"安装包生成",
|
|
129
|
+
progress_percentage,
|
|
130
|
+
f"{format_type.upper()} 格式: {status} ({completed}/{total})",
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
except concurrent.futures.TimeoutError:
|
|
134
|
+
self.progress.on_error(
|
|
135
|
+
Exception(f"{format_type} 格式打包超时"), "并行构建"
|
|
136
|
+
)
|
|
137
|
+
results[format_type] = False
|
|
138
|
+
except Exception as e:
|
|
139
|
+
self.progress.on_error(
|
|
140
|
+
Exception(f"{format_type} 格式打包失败: {e}"), "并行构建"
|
|
141
|
+
)
|
|
142
|
+
results[format_type] = False
|
|
143
|
+
|
|
144
|
+
return results
|
|
145
|
+
|
|
146
|
+
def _build_single_format(
|
|
147
|
+
self, format_type: str, packager, source_path: Path, output_path: Path
|
|
148
|
+
) -> bool:
|
|
149
|
+
"""构建单个格式的安装包(在独立线程中执行)
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
format_type: 格式类型
|
|
153
|
+
packager: 打包器实例
|
|
154
|
+
source_path: 源文件路径
|
|
155
|
+
output_path: 输出文件路径
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
bool: 构建是否成功
|
|
159
|
+
"""
|
|
160
|
+
try:
|
|
161
|
+
# 为此线程创建独立的组件
|
|
162
|
+
from ..utils.command_runner import SilentRunner
|
|
163
|
+
from ..utils.tool_manager import ToolManager
|
|
164
|
+
|
|
165
|
+
runner = SilentRunner(self.progress)
|
|
166
|
+
tool_manager = ToolManager()
|
|
167
|
+
|
|
168
|
+
# 更新打包器的组件引用
|
|
169
|
+
packager.runner = runner
|
|
170
|
+
packager.tool_manager = tool_manager
|
|
171
|
+
|
|
172
|
+
# 执行打包
|
|
173
|
+
return packager.package(format_type, source_path, output_path)
|
|
174
|
+
|
|
175
|
+
except Exception as e:
|
|
176
|
+
self.progress.on_error(
|
|
177
|
+
Exception(f"线程中构建 {format_type} 失败: {e}"), "并行构建"
|
|
178
|
+
)
|
|
179
|
+
return False
|
|
180
|
+
|
|
181
|
+
def optimize_pyinstaller_build(
|
|
182
|
+
self, config: Dict[str, Any], entry_script: str, project_dir: Path
|
|
183
|
+
) -> bool:
|
|
184
|
+
"""优化PyInstaller构建过程.
|
|
185
|
+
|
|
186
|
+
Args:
|
|
187
|
+
config: PyInstaller配置
|
|
188
|
+
entry_script: 入口脚本
|
|
189
|
+
project_dir: 项目目录
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
bool: 构建是否成功
|
|
193
|
+
"""
|
|
194
|
+
try:
|
|
195
|
+
# 检查是否可以使用增量构建
|
|
196
|
+
if self._can_use_incremental_build(project_dir):
|
|
197
|
+
self.progress.info("使用增量构建模式...")
|
|
198
|
+
config = config.copy()
|
|
199
|
+
config.pop("clean", None) # 移除clean选项
|
|
200
|
+
|
|
201
|
+
# 检查是否可以启用PyInstaller优化
|
|
202
|
+
optimizations = self._get_pyinstaller_optimizations(config)
|
|
203
|
+
if optimizations:
|
|
204
|
+
self.progress.info("启用PyInstaller优化选项...")
|
|
205
|
+
config.update(optimizations)
|
|
206
|
+
|
|
207
|
+
return True
|
|
208
|
+
|
|
209
|
+
except Exception as e:
|
|
210
|
+
self.progress.warning(f"PyInstaller优化失败: {e}")
|
|
211
|
+
return False
|
|
212
|
+
|
|
213
|
+
def _can_use_incremental_build(self, project_dir: Path) -> bool:
|
|
214
|
+
"""
|
|
215
|
+
检查是否可以使用增量构建.
|
|
216
|
+
"""
|
|
217
|
+
try:
|
|
218
|
+
build_dir = project_dir / "build"
|
|
219
|
+
dist_dir = project_dir / "dist"
|
|
220
|
+
|
|
221
|
+
# 检查是否存在之前的构建
|
|
222
|
+
if not (build_dir.exists() and dist_dir.exists()):
|
|
223
|
+
return False
|
|
224
|
+
|
|
225
|
+
# 检查源文件修改时间
|
|
226
|
+
source_files = list(project_dir.glob("**/*.py"))
|
|
227
|
+
if not source_files:
|
|
228
|
+
return False
|
|
229
|
+
|
|
230
|
+
latest_source_time = max(f.stat().st_mtime for f in source_files)
|
|
231
|
+
|
|
232
|
+
# 检查构建目录的修改时间
|
|
233
|
+
build_files = list(build_dir.glob("**/*"))
|
|
234
|
+
if not build_files:
|
|
235
|
+
return False
|
|
236
|
+
|
|
237
|
+
latest_build_time = max(
|
|
238
|
+
f.stat().st_mtime for f in build_files if f.is_file()
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
# 如果构建文件比源文件新,可以增量构建
|
|
242
|
+
return latest_build_time > latest_source_time
|
|
243
|
+
|
|
244
|
+
except Exception:
|
|
245
|
+
return False
|
|
246
|
+
|
|
247
|
+
def _get_pyinstaller_optimizations(self, config: Dict[str, Any]) -> Dict[str, Any]:
|
|
248
|
+
"""
|
|
249
|
+
获取PyInstaller优化选项.
|
|
250
|
+
"""
|
|
251
|
+
optimizations = {}
|
|
252
|
+
|
|
253
|
+
# 如果没有指定优化级别,启用基本优化
|
|
254
|
+
if "optimize" not in config:
|
|
255
|
+
optimizations["optimize"] = 1
|
|
256
|
+
|
|
257
|
+
# 如果没有调试信息,启用strip
|
|
258
|
+
if not config.get("debug", False) and "strip" not in config:
|
|
259
|
+
optimizations["strip"] = True
|
|
260
|
+
|
|
261
|
+
# 启用UPX压缩(如果可用且未禁用)
|
|
262
|
+
if not config.get("noupx", False) and "upx_dir" not in config:
|
|
263
|
+
import shutil
|
|
264
|
+
|
|
265
|
+
if shutil.which("upx"):
|
|
266
|
+
optimizations["noupx"] = False
|
|
267
|
+
self.progress.info("检测到UPX,启用可执行文件压缩")
|
|
268
|
+
|
|
269
|
+
return optimizations
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
class CacheManager:
|
|
273
|
+
"""
|
|
274
|
+
缓存管理器,避免重复下载和构建.
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
def __init__(self, cache_dir: Optional[Path] = None):
|
|
278
|
+
"""初始化缓存管理器.
|
|
279
|
+
|
|
280
|
+
Args:
|
|
281
|
+
cache_dir: 缓存目录路径
|
|
282
|
+
"""
|
|
283
|
+
self.cache_dir = cache_dir or Path.home() / ".unifypy_cache"
|
|
284
|
+
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
|
285
|
+
|
|
286
|
+
# 创建子目录
|
|
287
|
+
(self.cache_dir / "tools").mkdir(exist_ok=True)
|
|
288
|
+
(self.cache_dir / "downloads").mkdir(exist_ok=True)
|
|
289
|
+
(self.cache_dir / "builds").mkdir(exist_ok=True)
|
|
290
|
+
|
|
291
|
+
def get_tool_cache_path(self, tool_name: str, version: str = "latest") -> Path:
|
|
292
|
+
"""
|
|
293
|
+
获取工具缓存路径.
|
|
294
|
+
"""
|
|
295
|
+
return self.cache_dir / "tools" / f"{tool_name}_{version}"
|
|
296
|
+
|
|
297
|
+
def get_download_cache_path(self, url: str) -> Path:
|
|
298
|
+
"""
|
|
299
|
+
获取下载文件缓存路径.
|
|
300
|
+
"""
|
|
301
|
+
import hashlib
|
|
302
|
+
|
|
303
|
+
url_hash = hashlib.md5(url.encode()).hexdigest()
|
|
304
|
+
return self.cache_dir / "downloads" / url_hash
|
|
305
|
+
|
|
306
|
+
def get_build_cache_path(self, config_hash: str) -> Path:
|
|
307
|
+
"""
|
|
308
|
+
获取构建缓存路径.
|
|
309
|
+
"""
|
|
310
|
+
return self.cache_dir / "builds" / config_hash
|
|
311
|
+
|
|
312
|
+
def is_cached(self, cache_path: Path) -> bool:
|
|
313
|
+
"""
|
|
314
|
+
检查是否已缓存.
|
|
315
|
+
"""
|
|
316
|
+
return cache_path.exists() and cache_path.stat().st_size > 0
|
|
317
|
+
|
|
318
|
+
def cleanup_old_cache(self, max_age_days: int = 30):
|
|
319
|
+
"""
|
|
320
|
+
清理旧的缓存文件.
|
|
321
|
+
"""
|
|
322
|
+
import time
|
|
323
|
+
|
|
324
|
+
current_time = time.time()
|
|
325
|
+
max_age_seconds = max_age_days * 24 * 3600
|
|
326
|
+
|
|
327
|
+
for cache_file in self.cache_dir.rglob("*"):
|
|
328
|
+
if cache_file.is_file():
|
|
329
|
+
file_age = current_time - cache_file.stat().st_mtime
|
|
330
|
+
if file_age > max_age_seconds:
|
|
331
|
+
try:
|
|
332
|
+
cache_file.unlink()
|
|
333
|
+
except:
|
|
334
|
+
pass
|
|
335
|
+
|
|
336
|
+
def get_cache_size(self) -> int:
|
|
337
|
+
"""
|
|
338
|
+
获取缓存大小(字节)
|
|
339
|
+
"""
|
|
340
|
+
total_size = 0
|
|
341
|
+
for cache_file in self.cache_dir.rglob("*"):
|
|
342
|
+
if cache_file.is_file():
|
|
343
|
+
total_size += cache_file.stat().st_size
|
|
344
|
+
return total_size
|
|
345
|
+
|
|
346
|
+
def format_cache_size(self) -> str:
|
|
347
|
+
"""
|
|
348
|
+
格式化缓存大小显示.
|
|
349
|
+
"""
|
|
350
|
+
size = self.get_cache_size()
|
|
351
|
+
for unit in ["B", "KB", "MB", "GB"]:
|
|
352
|
+
if size < 1024:
|
|
353
|
+
return f"{size:.1f} {unit}"
|
|
354
|
+
size /= 1024
|
|
355
|
+
return f"{size:.1f} TB"
|