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.
Files changed (78) hide show
  1. unifypy/__init__.py +7 -0
  2. unifypy/__main__.py +21 -0
  3. unifypy/cli/__init__.py +10 -0
  4. unifypy/cli/argument_parser.py +209 -0
  5. unifypy/core/__init__.py +8 -0
  6. unifypy/core/config.py +491 -0
  7. unifypy/core/context.py +36 -0
  8. unifypy/core/engine.py +93 -0
  9. unifypy/core/environment.py +509 -0
  10. unifypy/core/event_bus.py +37 -0
  11. unifypy/core/events.py +16 -0
  12. unifypy/core/platforms.py +58 -0
  13. unifypy/core/plugin.py +65 -0
  14. unifypy/core/validators/__init__.py +10 -0
  15. unifypy/core/validators/tool_validator.py +144 -0
  16. unifypy/platforms/__init__.py +10 -0
  17. unifypy/platforms/base.py +127 -0
  18. unifypy/platforms/linux/__init__.py +10 -0
  19. unifypy/platforms/linux/deb_packager.py +289 -0
  20. unifypy/platforms/linux/rpm_packager.py +332 -0
  21. unifypy/platforms/macos/__init__.py +10 -0
  22. unifypy/platforms/macos/dmg_packager.py +477 -0
  23. unifypy/platforms/macos/post_processor.py +272 -0
  24. unifypy/platforms/registry.py +108 -0
  25. unifypy/platforms/windows/__init__.py +9 -0
  26. unifypy/platforms/windows/inno_setup.py +715 -0
  27. unifypy/plugins/__init__.py +3 -0
  28. unifypy/plugins/auto_rollback_plugin.py +156 -0
  29. unifypy/plugins/cleanup_plugin.py +22 -0
  30. unifypy/plugins/config_plugin.py +42 -0
  31. unifypy/plugins/dry_run_plugin.py +22 -0
  32. unifypy/plugins/environment_plugin.py +61 -0
  33. unifypy/plugins/external_plugins_loader.py +42 -0
  34. unifypy/plugins/packaging_plugin.py +125 -0
  35. unifypy/plugins/prepare_plugin.py +89 -0
  36. unifypy/plugins/progress_plugin.py +32 -0
  37. unifypy/plugins/pyinstaller_plugin.py +95 -0
  38. unifypy/plugins/rollback_plugin.py +62 -0
  39. unifypy/plugins/summary_plugin.py +36 -0
  40. unifypy/plugins/windows_language_plugin.py +85 -0
  41. unifypy/pyinstaller/__init__.py +7 -0
  42. unifypy/pyinstaller/config_builder.py +908 -0
  43. unifypy/templates/ChineseSimplified.isl.template +403 -0
  44. unifypy/templates/setup.iss.template +51 -0
  45. unifypy/tools/create-dmg/.this-is-the-create-dmg-repo +3 -0
  46. unifypy/tools/create-dmg/LICENSE +22 -0
  47. unifypy/tools/create-dmg/Makefile +33 -0
  48. unifypy/tools/create-dmg/README.md +126 -0
  49. unifypy/tools/create-dmg/builder/create-dmg.builder +26 -0
  50. unifypy/tools/create-dmg/create-dmg +621 -0
  51. unifypy/tools/create-dmg/doc-project/Developer Notes.md +35 -0
  52. unifypy/tools/create-dmg/doc-project/Release Checklist.md +10 -0
  53. unifypy/tools/create-dmg/examples/01-main-example/installer_background.png +0 -0
  54. unifypy/tools/create-dmg/examples/01-main-example/sample +30 -0
  55. unifypy/tools/create-dmg/examples/01-main-example/source_folder/Application.app +0 -0
  56. unifypy/tools/create-dmg/support/eula-resources-template.xml +105 -0
  57. unifypy/tools/create-dmg/support/template.applescript +74 -0
  58. unifypy/tools/create-dmg/tests/007-space-in-dir-name/my files/hello.txt +1 -0
  59. unifypy/tools/create-dmg/tests/007-space-in-dir-name/run-test +5 -0
  60. unifypy/utils/__init__.py +10 -0
  61. unifypy/utils/cache_manager.py +957 -0
  62. unifypy/utils/command_runner.py +195 -0
  63. unifypy/utils/entitlements_generator.py +450 -0
  64. unifypy/utils/file_ops.py +333 -0
  65. unifypy/utils/icon_converter.py +144 -0
  66. unifypy/utils/info_plist_updater.py +247 -0
  67. unifypy/utils/macos_codesign.py +225 -0
  68. unifypy/utils/parallel_builder.py +355 -0
  69. unifypy/utils/progress.py +371 -0
  70. unifypy/utils/progress_callback_factory.py +56 -0
  71. unifypy/utils/rollback.py +484 -0
  72. unifypy/utils/tool_manager.py +701 -0
  73. unifypy-2.0.2.dist-info/METADATA +609 -0
  74. unifypy-2.0.2.dist-info/RECORD +78 -0
  75. unifypy-2.0.2.dist-info/WHEEL +5 -0
  76. unifypy-2.0.2.dist-info/entry_points.txt +2 -0
  77. unifypy-2.0.2.dist-info/licenses/LICENSE +21 -0
  78. unifypy-2.0.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,477 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ MacOS DMG 打包器 使用create-dmg工具创建DMG安装包.
5
+ """
6
+
7
+ import os
8
+ import shutil
9
+ from pathlib import Path
10
+ from typing import Any, Dict, List
11
+
12
+ from ..base import BasePackager
13
+
14
+
15
+ class DMGPackager(BasePackager):
16
+ """
17
+ DMG 打包器.
18
+ """
19
+
20
+ def get_supported_formats(self) -> List[str]:
21
+ """
22
+ 获取支持的打包格式.
23
+ """
24
+ return ["dmg"]
25
+
26
+ def can_package_format(self, format_type: str) -> bool:
27
+ """
28
+ 检查是否支持指定格式.
29
+ """
30
+ return format_type in self.get_supported_formats()
31
+
32
+ def package(self, format_type: str, source_path: Path, output_path: Path) -> bool:
33
+ """执行DMG打包.
34
+
35
+ Args:
36
+ format_type: 打包格式 (dmg)
37
+ source_path: PyInstaller生成的.app目录路径
38
+ output_path: 输出DMG文件路径
39
+
40
+ Returns:
41
+ bool: 打包是否成功
42
+ """
43
+ if not self.can_package_format(format_type):
44
+ self.progress.on_error(
45
+ Exception(f"不支持的格式: {format_type}"), "macOS DMG打包"
46
+ )
47
+ return False
48
+
49
+ # 确保create-dmg工具可用
50
+ create_dmg_path = self.tool_manager.ensure_tool("create-dmg")
51
+ if not create_dmg_path:
52
+ self.progress.on_error(Exception("无法获取create-dmg工具"), "macOS DMG打包")
53
+ return False
54
+
55
+ # 获取DMG配置
56
+ dmg_config = self.get_format_config("dmg")
57
+
58
+ # 查找.app bundle
59
+ app_path = self._find_app_bundle(source_path)
60
+
61
+ # 如果没有找到.app bundle,尝试创建.app结构
62
+ if not app_path:
63
+ if source_path.is_file():
64
+ # 单个可执行文件
65
+ app_path = self._create_app_bundle(source_path, dmg_config)
66
+ elif source_path.is_dir():
67
+ # PyInstaller目录输出,查找主可执行文件
68
+ app_name = self.config.get('name', 'MyApp')
69
+ exe_path = source_path / app_name
70
+ if exe_path.exists() and exe_path.is_file():
71
+ app_path = self._create_app_bundle(exe_path, dmg_config)
72
+ else:
73
+ self.progress.on_error(
74
+ Exception(
75
+ f"在PyInstaller输出目录中找不到可执行文件: {exe_path}"
76
+ ),
77
+ "macOS DMG打包",
78
+ )
79
+ return False
80
+
81
+ if not app_path:
82
+ self.progress.on_error(
83
+ Exception(
84
+ f"无法创建 .app bundle,DMG 打包失败:{source_path}"
85
+ ),
86
+ "macOS DMG打包",
87
+ )
88
+ return False
89
+
90
+ # 构建create-dmg命令
91
+ command = self._build_create_dmg_command(
92
+ dmg_config, app_path, output_path, create_dmg_path
93
+ )
94
+
95
+ # 执行命令
96
+ success = self.runner.run_command(
97
+ command, "macOS DMG打包", "正在创建DMG安装包...", 80, shell=False
98
+ )
99
+
100
+ if success and output_path.exists():
101
+ return True
102
+ else:
103
+ self.progress.on_error(
104
+ Exception(f"DMG文件生成失败: {output_path}"), "macOS DMG打包"
105
+ )
106
+ return False
107
+
108
+ def _find_app_bundle(self, source_path: Path) -> Path:
109
+ """查找.app bundle.
110
+
111
+ Args:
112
+ source_path: 源路径(可能是目录或文件)
113
+
114
+ Returns:
115
+ Path: .app bundle路径,如果未找到返回None
116
+ """
117
+ if source_path.is_file():
118
+ # 如果是文件,检查是否是.app bundle内的可执行文件
119
+ return None
120
+
121
+ # 如果是目录,查找.app bundle
122
+ if source_path.is_dir():
123
+ # 首先检查源路径本身是否是.app bundle
124
+ if source_path.name.endswith(".app"):
125
+ return source_path
126
+
127
+ # 在目录中查找.app bundle(递归查找)
128
+ for item in source_path.rglob("*.app"):
129
+ if item.is_dir():
130
+ return item
131
+
132
+ # 如果源路径是PyInstaller输出目录,检查父目录中是否有同名的.app文件
133
+ parent = source_path.parent
134
+ if parent.exists():
135
+ app_name = source_path.name + ".app"
136
+ app_path = parent / app_name
137
+ if app_path.exists() and app_path.is_dir():
138
+ return app_path
139
+
140
+ # 检查父目录中是否有与项目名称匹配的.app文件
141
+ app_name = self.config.get('name', 'MyApp')
142
+ app_path = source_path.parent / f"{app_name}.app"
143
+ if app_path.exists() and app_path.is_dir():
144
+ return app_path
145
+
146
+ return None
147
+
148
+ def _create_app_bundle(self, exe_path: Path, config: Dict[str, Any]) -> Path:
149
+ """为单个可执行文件创建.app包结构.
150
+
151
+ Args:
152
+ exe_path: 可执行文件路径
153
+ config: DMG配置
154
+
155
+ Returns:
156
+ Path: 生成的.app路径,失败返回None
157
+ """
158
+ app_name = self.config.get("name", "MyApp")
159
+ app_dir = exe_path.parent / f"{app_name}.app"
160
+
161
+ try:
162
+ # 创建.app目录结构
163
+ contents_dir = app_dir / "Contents"
164
+ macos_dir = contents_dir / "MacOS"
165
+ resources_dir = contents_dir / "Resources"
166
+
167
+ contents_dir.mkdir(parents=True, exist_ok=True)
168
+ macos_dir.mkdir(exist_ok=True)
169
+ resources_dir.mkdir(exist_ok=True)
170
+
171
+ # 复制可执行文件和相关资源
172
+ if exe_path.parent.name == app_name:
173
+ # PyInstaller目录输出模式 - 复制整个目录内容
174
+ pyinstaller_dir = exe_path.parent
175
+ for item in pyinstaller_dir.iterdir():
176
+ if item.is_file():
177
+ shutil.copy2(item, macos_dir / item.name)
178
+ elif item.is_dir():
179
+ shutil.copytree(item, macos_dir / item.name)
180
+ # 确保主可执行文件有执行权限
181
+ app_exe = macos_dir / app_name
182
+ if app_exe.exists():
183
+ app_exe.chmod(0o755)
184
+ else:
185
+ # 单个可执行文件模式
186
+ app_exe = macos_dir / app_name
187
+ shutil.copy2(exe_path, app_exe)
188
+ app_exe.chmod(0o755) # 确保可执行
189
+
190
+ # 处理图标文件(必须在创建Info.plist之前)
191
+ icon_filename = self._handle_icon_for_app(resources_dir, exe_path, config)
192
+
193
+ # 创建Info.plist
194
+ info_plist = self._create_info_plist(config, icon_filename)
195
+ with open(contents_dir / "Info.plist", "w") as f:
196
+ f.write(info_plist)
197
+
198
+ return app_dir
199
+
200
+ except Exception as e:
201
+ self.progress.on_error(Exception(f"创建.app包失败: {e}"), "macOS DMG打包")
202
+ return None
203
+
204
+ def _create_info_plist(self, config: Dict[str, Any], icon_filename: str = None) -> str:
205
+ """
206
+ 创建Info.plist文件内容.
207
+
208
+ Args:
209
+ config: 配置字典
210
+ icon_filename: 图标文件名(不含路径),如果为None则不添加图标配置
211
+ """
212
+ app_name = self.config.get("name", "MyApp")
213
+ app_version = self.config.get("version", "1.0.0")
214
+ bundle_id = config.get("bundle_identifier", f"com.example.{app_name.lower()}")
215
+
216
+ # 图标配置部分
217
+ icon_config = ""
218
+ if icon_filename:
219
+ icon_config = f""" <key>CFBundleIconFile</key>
220
+ <string>{icon_filename}</string>"""
221
+
222
+ return f"""<?xml version="1.0" encoding="UTF-8"?>
223
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
224
+ <plist version="1.0">
225
+ <dict>
226
+ <key>CFBundleDisplayName</key>
227
+ <string>{app_name}</string>
228
+ <key>CFBundleExecutable</key>
229
+ <string>{app_name}</string>{icon_config}
230
+ <key>CFBundleIdentifier</key>
231
+ <string>{bundle_id}</string>
232
+ <key>CFBundleInfoDictionaryVersion</key>
233
+ <string>6.0</string>
234
+ <key>CFBundleName</key>
235
+ <string>{app_name}</string>
236
+ <key>CFBundlePackageType</key>
237
+ <string>APPL</string>
238
+ <key>CFBundleShortVersionString</key>
239
+ <string>{app_version}</string>
240
+ <key>CFBundleVersion</key>
241
+ <string>{app_version}</string>
242
+ <key>LSMinimumSystemVersion</key>
243
+ <string>10.9</string>
244
+ <key>NSHighResolutionCapable</key>
245
+ <true/>
246
+ </dict>
247
+ </plist>"""
248
+
249
+ def _convert_icon_to_icns(self, icon_path: str, output_path: Path):
250
+ """
251
+ 将图标转换为icns格式.
252
+ """
253
+ try:
254
+ # 使用macOS的iconutil工具
255
+ import tempfile
256
+
257
+ with tempfile.TemporaryDirectory() as temp_dir:
258
+ iconset_dir = Path(temp_dir) / "icon.iconset"
259
+ iconset_dir.mkdir()
260
+
261
+ # 生成不同尺寸的图标
262
+ sizes = [16, 32, 64, 128, 256, 512, 1024]
263
+
264
+ for size in sizes:
265
+ # 使用sips调整图标大小
266
+ resized_icon = iconset_dir / f"icon_{size}x{size}.png"
267
+ cmd = [
268
+ "sips",
269
+ "-z",
270
+ str(size),
271
+ str(size),
272
+ icon_path,
273
+ "--out",
274
+ str(resized_icon),
275
+ ]
276
+ self.runner.run_command(
277
+ cmd, "图标转换", f"生成{size}x{size}图标", 5, shell=False
278
+ )
279
+
280
+ # 转换为icns
281
+ cmd = [
282
+ "iconutil",
283
+ "-c",
284
+ "icns",
285
+ str(iconset_dir),
286
+ "-o",
287
+ str(output_path),
288
+ ]
289
+ self.runner.run_command(
290
+ cmd, "图标转换", "生成icns文件", 10, shell=False
291
+ )
292
+
293
+ except Exception as e:
294
+ self.progress.warning(f"图标转换失败: {e}")
295
+
296
+ def _handle_icon_for_app(self, resources_dir: Path, exe_path: Path, config: Dict[str, Any]) -> str:
297
+ """
298
+ 处理应用程序包的图标文件.
299
+
300
+ Args:
301
+ resources_dir: Resources目录路径
302
+ exe_path: 可执行文件路径
303
+ config: 配置字典
304
+
305
+ Returns:
306
+ str: 最终的图标文件名(不含路径),用于Info.plist引用
307
+ """
308
+ # 1. 首先检查PyInstaller是否已经生成了图标
309
+ pyinstaller_icon = None
310
+ if exe_path.parent.name == self.config.get("name", "MyApp"):
311
+ # PyInstaller目录模式,检查_internal或Resources中的图标
312
+ possible_icon_paths = [
313
+ exe_path.parent / "_internal" / "icon-windowed.icns",
314
+ exe_path.parent / "_internal" / "icon.icns",
315
+ exe_path.parent / "icon-windowed.icns",
316
+ exe_path.parent / "icon.icns",
317
+ ]
318
+ for icon_path in possible_icon_paths:
319
+ if icon_path.exists():
320
+ pyinstaller_icon = icon_path
321
+ break
322
+
323
+ # 2. 如果找到PyInstaller生成的图标,保持原文件名
324
+ if pyinstaller_icon:
325
+ target_name = pyinstaller_icon.name
326
+ shutil.copy2(pyinstaller_icon, resources_dir / target_name)
327
+ self.progress.info(f"使用PyInstaller生成的图标: {target_name}")
328
+ return target_name
329
+
330
+ # 3. 否则使用配置中指定的图标
331
+ icon_path = config.get("icon") or self.config.get("icon")
332
+ if icon_path and os.path.exists(icon_path):
333
+ source_path = Path(icon_path)
334
+ icon_ext = source_path.suffix.lower()
335
+
336
+ if icon_ext == ".icns":
337
+ # 已经是icns格式,保持原文件名
338
+ target_name = source_path.name
339
+ shutil.copy2(icon_path, resources_dir / target_name)
340
+ self.progress.info(f"使用配置中的图标: {target_name}")
341
+ return target_name
342
+ elif icon_ext in [".png", ".jpg", ".jpeg"]:
343
+ # 需要转换格式,保持文件名但改扩展名
344
+ target_name = source_path.stem + ".icns"
345
+ target_path = resources_dir / target_name
346
+ self._convert_icon_to_icns(icon_path, target_path)
347
+ self.progress.info(f"转换图标 {source_path.name} -> {target_name}")
348
+ return target_name
349
+
350
+ self.progress.warning("未找到图标文件")
351
+ return None
352
+
353
+ def _build_create_dmg_command(
354
+ self,
355
+ config: Dict[str, Any],
356
+ app_path: Path,
357
+ output_path: Path,
358
+ create_dmg_path: str,
359
+ ) -> List[str]:
360
+ """
361
+ 构建create-dmg命令.
362
+ """
363
+ command = [create_dmg_path]
364
+
365
+ # 基本选项
366
+ volname = config.get("volname", f"{self.config.get('name', 'MyApp')} Installer")
367
+ command.extend(["--volname", volname])
368
+
369
+ # 窗口设置
370
+ if "window_size" in config:
371
+ window_size = config["window_size"]
372
+ if isinstance(window_size, list) and len(window_size) == 2:
373
+ command.extend(
374
+ ["--window-size", str(window_size[0]), str(window_size[1])]
375
+ )
376
+
377
+ if "window_pos" in config:
378
+ window_pos = config["window_pos"]
379
+ if isinstance(window_pos, list) and len(window_pos) == 2:
380
+ command.extend(["--window-pos", str(window_pos[0]), str(window_pos[1])])
381
+
382
+ # 图标设置
383
+ if "icon_size" in config:
384
+ command.extend(["--icon-size", str(config["icon_size"])])
385
+
386
+ # 背景图片
387
+ background = config.get("background")
388
+ if background and os.path.exists(background):
389
+ command.extend(["--background", background])
390
+
391
+ # 卷图标
392
+ volicon = config.get("volicon")
393
+ if volicon and os.path.exists(volicon):
394
+ command.extend(["--volicon", volicon])
395
+
396
+ # 应用程序文件夹链接
397
+ if config.get("app_drop_link", True):
398
+ app_drop_x = config.get("app_drop_x", 448)
399
+ app_drop_y = config.get("app_drop_y", 120)
400
+ command.extend(["--app-drop-link", str(app_drop_x), str(app_drop_y)])
401
+
402
+ # 图标位置
403
+ if "icon_position" in config:
404
+ icon_pos = config["icon_position"]
405
+ if isinstance(icon_pos, list) and len(icon_pos) == 2:
406
+ command.extend(
407
+ ["--icon", app_path.name, str(icon_pos[0]), str(icon_pos[1])]
408
+ )
409
+
410
+ # 格式选项
411
+ dmg_format = config.get("format", "UDZO") # UDZO是默认压缩格式
412
+ command.extend(["--format", dmg_format])
413
+
414
+ # 文件系统
415
+ filesystem = config.get("filesystem")
416
+ if filesystem:
417
+ command.extend(["--filesystem", filesystem])
418
+
419
+ # 隐藏扩展名
420
+ if config.get("hide_extension", False):
421
+ command.append("--hide-extension")
422
+
423
+ # 许可协议
424
+ eula = config.get("eula")
425
+ if eula and os.path.exists(eula):
426
+ command.extend(["--eula", eula])
427
+
428
+ # 详细输出
429
+ if config.get("verbose", False):
430
+ command.append("--hdiutil-verbose")
431
+
432
+ # 自动打开设置(默认禁用自动打开以避免干扰)
433
+ # 注意:--sandbox-safe 参数可能导致DMG创建问题,暂时移除
434
+ # if not config.get("auto_open", False):
435
+ # command.append("--sandbox-safe")
436
+
437
+ # 输出文件和源文件
438
+ command.extend([str(output_path), str(app_path)])
439
+
440
+ return command
441
+
442
+ def validate_config(self, format_type: str) -> List[str]:
443
+ """验证DMG配置.
444
+
445
+ 配置选项说明:
446
+ - auto_open (bool): 是否在创建后自动打开DMG,默认为False(禁用)
447
+ - volname (str): DMG卷名称
448
+ - window_size (list): 窗口大小 [width, height]
449
+ - window_pos (list): 窗口位置 [x, y]
450
+ - icon_size (int): 图标大小
451
+ - background (str): 背景图片路径
452
+ - volicon (str): 卷图标路径
453
+ - app_drop_link (bool): 是否添加应用程序文件夹链接
454
+ - format (str): DMG格式 (UDZO|UDBZ|ULFO|ULMO)
455
+ - filesystem (str): 文件系统 (HFS+|APFS)
456
+ - eula (str): 许可协议文件路径
457
+ """
458
+ errors = []
459
+
460
+ config = self.get_format_config("dmg")
461
+
462
+ # 检查背景图片
463
+ background = config.get("background")
464
+ if background and not os.path.exists(background):
465
+ errors.append(f"背景图片文件不存在: {background}")
466
+
467
+ # 检查卷图标
468
+ volicon = config.get("volicon")
469
+ if volicon and not os.path.exists(volicon):
470
+ errors.append(f"卷图标文件不存在: {volicon}")
471
+
472
+ # 检查许可协议文件
473
+ eula = config.get("eula")
474
+ if eula and not os.path.exists(eula):
475
+ errors.append(f"许可协议文件不存在: {eula}")
476
+
477
+ return errors