forked from Goo-aw233/MSPCManagerHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstallationFeature.py
542 lines (467 loc) · 30.1 KB
/
installationFeature.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import os
import requests
import shutil
import subprocess
import tempfile
import tkinter as tk
import webbrowser
import winreg
from tkinter import filedialog, messagebox
class InstallationFeature:
def __init__(self, translator, result_textbox=None):
self.translator = translator
self.result_textbox = result_textbox
def textbox(self, message):
self.result_textbox.config(state="normal")
self.result_textbox.insert(tk.END, message + "\n")
self.result_textbox.config(state="disable")
self.result_textbox.update_idletasks() # 刷新界面
def refresh_result_textbox(self):
pass
def download_from_winget(self):
try:
# 检查 WinGet 是否安装
result = subprocess.Popen(['where.exe', 'winget.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
stdout, stderr = result.communicate()
if result.returncode != 0:
return self.translator.translate("winget_not_installed")
# 弹出提示框询问用户是否同意 Microsoft Store 源协议
self.textbox(self.translator.translate("winget_msstore_source_agreement") + '\n')
response = messagebox.askyesnocancel(
self.translator.translate("winget_msstore_source_agreement_notice"),
self.translator.translate("ask_winget_msstore_source_agreement")
)
if response is None:
return self.translator.translate("user_canceled")
elif not response:
return self.translator.translate("disagree_winget_msstore_source_agreement")
# 使用 WinGet 搜索 Microsoft PC Manager
result = subprocess.Popen(['winget.exe', 'search', 'Microsoft PC Manager', '--source', 'msstore', '--accept-source-agreements'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
stdout, stderr = result.communicate()
if result.returncode != 0:
# 无互联网连接
if result.returncode == 2316632067:
return self.translator.translate("winget_not_internet_connection")
# 找不到结果
elif result.returncode == 2316632084:
return self.translator.translate("winget_no_results_found")
# 需要重置 msstore 源或无互联网连接
elif result.returncode == 2316632139:
return self.translator.translate("winget_source_reset_needed")
else:
stderr = stderr.strip() if stderr else self.translator.translate("winget_not_error_info")
stdout = stdout.strip() if stdout else self.translator.translate("winget_not_output")
return f"{self.translator.translate('winget_error')}\n{self.translator.translate('winget_error_info')}: {stderr}\n{self.translator.translate('winget_error_code')}: {result.returncode}\n{self.translator.translate('winget_output')}: {stdout}"
# 使用 WinGet 安装 Microsoft PC Manager
result = subprocess.Popen(['winget.exe', 'install', 'Microsoft PC Manager', '--source', 'msstore', '--accept-source-agreements', '--accept-package-agreements'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
stdout, stderr = result.communicate()
self.textbox(self.translator.translate("downloading_pc_manager_from_winget") + '\n')
if result.returncode != 0:
# Microsoft PC Manager 早已安装
if result.returncode == 2316632107:
return self.translator.translate("already_installed_pc_manager_from_winget")
else:
stderr = stderr.strip() if stderr else self.translator.translate("winget_not_error_info")
stdout = stdout.strip() if stdout else self.translator.translate("winget_not_output")
return f"{self.translator.translate('winget_error')}\n{self.translator.translate('winget_error_info')}: {stderr}\n{self.translator.translate('winget_error_code')}: {result.returncode}\n{self.translator.translate('winget_output')}: {stdout}"
return self.translator.translate("pc_manager_has_been_installed_from_winget")
except Exception as e:
return f"{self.translator.translate('winget_error')}\n{self.translator.translate('winget_error_info')}: {str(e)}\n{self.translator.translate('winget_not_error_code')}"
def download_from_msstore(self):
try:
# 检测 Microsoft Store 是否安装
result = subprocess.run(
['powershell.exe', '-Command', 'Get-AppxPackage -Name Microsoft.WindowsStore'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
if 'PackageFamilyName : Microsoft.WindowsStore_8wekyb3d8bbwe' in result.stdout:
try:
# 如果安装了 Microsoft Store,运行命令
subprocess.run(
['powershell.exe', '-Command', 'Start-Process ms-windows-store://pdp/?ProductId=9PM860492SZD'], creationflags=subprocess.CREATE_NO_WINDOW)
return self.translator.translate("download_from_msstore_app_opened")
except Exception as e:
return f"{self.translator.translate('download_from_msstore_app_error')}: {str(e)}"
else:
# 如果没有安装 Microsoft Store,打开指定的 URL
webbrowser.open("https://www.microsoft.com/store/productid/9PM860492SZD")
return self.translator.translate("download_from_msstore_site_opened")
except Exception as e:
return f"{self.translator.translate('download_from_msstore_site_error')}: {str(e)}"
def install_for_all_users(self):
# 打开文件选择对话框选择文件
all_users_application_package_file_path = filedialog.askopenfilename(
filetypes=[("Msix / MsixBundle", "*.msix;*.msixbundle"),
("*", "*")])
if not all_users_application_package_file_path:
return self.translator.translate("install_for_all_users_no_file_selected")
# 弹出提示框询问用户是否选择许可证文件
response_for_all_users_license = messagebox.askyesnocancel(
self.translator.translate("install_for_all_users_license_select_notice"),
self.translator.translate("install_for_all_users_license_select")
)
all_users_dependency_package_paths = None
if response_for_all_users_license: # 使用许可证
all_users_license_path = filedialog.askopenfilename(
filetypes=[("License.xml", "*.xml"), ("*", "*")])
if not all_users_license_path:
return self.translator.translate("install_for_all_users_no_file_selected")
elif response_for_all_users_license == None:
return self.translator.translate("user_canceled")
else:
all_users_license_path = None
# 弹出提示框询问用户是否选择依赖包
response_for_all_users_dependency = messagebox.askyesnocancel(
self.translator.translate("install_for_all_users_dependency_package_select_notice"),
self.translator.translate("install_for_all_users_dependency_package_select")
)
if response_for_all_users_dependency: # 选择依赖包
all_users_dependency_package_paths = filedialog.askopenfilenames(
filetypes=[("Msix", "*.msix"),
("*", "*")])
if not all_users_dependency_package_paths:
return self.translator.translate("install_for_all_users_no_file_selected")
elif response_for_all_users_dependency == None:
return self.translator.translate("user_canceled")
try:
# 构建 Dism.exe 命令
all_users_DISM_command = ['Dism.exe', '/Online', '/Add-ProvisionedAppxPackage',
f'/PackagePath:{all_users_application_package_file_path}']
if all_users_license_path: # 使用许可证文件
all_users_DISM_command.append(f'/LicensePath:{all_users_license_path}')
else: # 不使用许可证文件
all_users_DISM_command.append('/SkipLicense')
if all_users_dependency_package_paths: # 使用依赖包,若不使用则跳过
for dependency_path in all_users_dependency_package_paths:
all_users_DISM_command.append(f'/DependencyPackagePath:{dependency_path}')
# 使用 Dism.exe 安装应用
result = subprocess.run(all_users_DISM_command, capture_output=True, text=True,
creationflags=subprocess.CREATE_NO_WINDOW)
if result.returncode == 0:
return self.translator.translate("install_for_all_users_success")
else:
return self.translator.translate("install_for_all_users_error") + f": {result.stderr}\n{result.stdout}"
except Exception as e:
return self.translator.translate("install_for_all_users_error") + f": {str(e)}"
def install_for_current_user(self):
# 打开文件选择对话框选择文件
current_user_application_package_file_path = filedialog.askopenfilename(
filetypes=[("Msix / MsixBundle", "*.msix;*.msixbundle"),
("*", "*")])
if not current_user_application_package_file_path:
return self.translator.translate("install_for_current_user_no_file_selected")
# 弹出提示框询问用户是否选择依赖包
response_for_current_user_dependency = messagebox.askyesnocancel(
self.translator.translate("install_for_current_user_dependency_package_select_notice"),
self.translator.translate("install_for_current_user_dependency_package_select")
)
current_user_dependency_package_paths = None
if response_for_current_user_dependency: # 选择依赖包
current_user_dependency_package_paths = filedialog.askopenfilenames(
filetypes=[("Msix", "*.msix"),
("*", "*")])
if not current_user_dependency_package_paths:
return self.translator.translate("install_for_current_user_no_file_selected")
elif response_for_current_user_dependency == None:
return self.translator.translate("user_canceled")
try:
# 构建 Add-AppxPackage 命令
command = ['powershell.exe', '-Command',
f'Add-AppxPackage -Path "{current_user_application_package_file_path}"']
if current_user_dependency_package_paths: # 使用依赖包,若不使用则跳过
dependency_paths = ",".join([f'"{additional_dependency_paths}"' for additional_dependency_paths in current_user_dependency_package_paths])
command.append(f'-DependencyPath {dependency_paths}')
# 执行 Add-AppxPackage 命令安装应用
result = subprocess.run(command, capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
if result.returncode == 0:
return self.translator.translate("install_for_current_user_success")
else:
return self.translator.translate("install_for_current_user_error") + f": {result.stderr}\n{result.stdout}"
except Exception as e:
return self.translator.translate("install_for_current_user_error") + f": {str(e)}"
def reinstall_pc_manager(self):
whether_to_reinstall_for_all_users = messagebox.askyesnocancel(
self.translator.translate("ask_whether_to_reinstall_for_all_users"),
self.translator.translate("whether_to_reinstall_for_all_users")
)
if whether_to_reinstall_for_all_users is None:
return self.translator.translate("user_canceled")
if whether_to_reinstall_for_all_users:
command = [
"powershell.exe",
"-Command",
"Get-AppxPackage -AllUsers *Microsoft.MicrosoftPCManager* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register \"$($_.InstallLocation)\\AppxManifest.xml\"}"
]
else:
command = [
"powershell.exe",
"-Command",
"Get-AppxPackage *Microsoft.MicrosoftPCManager* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register \"$($_.InstallLocation)\\AppxManifest.xml\"}"
]
result = subprocess.run(command, capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
if result.returncode == 0:
# 资源正在使用
if "0x80073D02" in result.stderr:
return self.translator.translate("reintsall_pc_manager_error_code_0x80073D02") + f"\n\n{result.stdout}\n{result.stderr}"
return self.translator.translate("reinstall_pc_manager_success")
# 需要以管理员身份运行
elif result.returncode == 1:
return self.translator.translate("reintsall_pc_manager_error_code_1") + f"\n\n{result.stdout}\n{result.stderr}"
# AppxManifest.xml 损坏
elif result.returncode == 2:
return self.translator.translate("reintsall_pc_manager_error_code_2") + f"\n\n{result.stdout}\n{result.stderr}"
else:
return f"{self.translator.translate('reinstall_pc_manager_error')}\n{self.translator.translate('reinstall_pc_manager_error_code')}: {result.returncode}\n\n{result.stdout}\n{result.stderr}"
def update_from_application_package(self):
# 打开文件选择对话框选择文件
update_application_package_file_path = filedialog.askopenfilename(
filetypes=[("Msix / MsixBundle", "*.msix;*.msixbundle"),
("*", "*")])
if not update_application_package_file_path:
return self.translator.translate("update_from_application_package_no_file_selected")
# 弹出提示框询问用户是否选择依赖包
response_for_update_dependency = messagebox.askyesnocancel(
self.translator.translate("update_from_application_package_dependency_package_select_notice"),
self.translator.translate("update_from_application_package_dependency_package_select")
)
update_dependency_package_paths = None
if response_for_update_dependency: # 选择依赖包
update_dependency_package_paths = filedialog.askopenfilenames(
filetypes=[("Msix", "*.msix"),
("*", "*")])
if not update_dependency_package_paths:
return self.translator.translate("update_from_application_package_no_file_selected")
elif response_for_update_dependency == None:
return self.translator.translate("user_canceled")
try:
# 构建 Add-AppxPackage 命令
command = ['powershell.exe', '-Command',
f'Add-AppxPackage -Path "{update_application_package_file_path}"']
if update_dependency_package_paths: # 使用依赖包,若不使用则跳过
dependency_paths = ",".join([f'"{additional_dependency_paths}"' for additional_dependency_paths in
update_dependency_package_paths])
command.append(f'-DependencyPath {dependency_paths}')
# 执行 Add-AppxPackage 命令安装应用
result = subprocess.run(command, capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
if result.returncode == 0:
return self.translator.translate("update_from_application_package_success")
else:
return self.translator.translate("update_from_application_package_error") + f": {result.stderr}\n{result.stdout}"
except Exception as e:
return self.translator.translate("update_from_application_package_error") + f": {str(e)}"
def install_from_appxmanifest(self):
# 询问是否已经安装过 Microsoft PC Manager
responding_to_whether_or_not_it_was_installed = messagebox.askyesnocancel(
self.translator.translate("install_from_appxmanifest_warn_title"),
self.translator.translate("install_from_appxmanifest_warn")
)
if responding_to_whether_or_not_it_was_installed is None or not responding_to_whether_or_not_it_was_installed:
return self.translator.translate("user_canceled")
# 打开文件选择对话框选择文件
appxmanifest_file_path = filedialog.askopenfilename(
filetypes=[("Msix / MsixBundle", "*.msix;*.msixbundle"), ("*", "*")]
)
if not appxmanifest_file_path:
return self.translator.translate("user_canceled")
try:
# 创建临时目录
temp_appxmanifest_file_path = os.path.join(tempfile.gettempdir(), "MSPCManagerHelper")
if not os.path.exists(temp_appxmanifest_file_path):
os.makedirs(temp_appxmanifest_file_path)
# 复制文件到临时目录并重命名为 .zip
pc_manager_package_file_name = os.path.basename(appxmanifest_file_path)
pc_manager_zip_package_file_path = os.path.join(temp_appxmanifest_file_path, pc_manager_package_file_name + ".zip")
shutil.copyfile(appxmanifest_file_path, pc_manager_zip_package_file_path)
# 解压文件
pc_manager_unpacked_path = os.path.join(temp_appxmanifest_file_path, os.path.splitext(pc_manager_package_file_name)[0])
shutil.unpack_archive(pc_manager_zip_package_file_path, pc_manager_unpacked_path)
# 检测解压后的文件夹内是否还有 .msix 文件
pc_manager_msix_files = [f for f in os.listdir(pc_manager_unpacked_path) if f.endswith('.msix')]
if not pc_manager_msix_files:
# 写入 exclude.txt
exclude_file_path = os.path.join(temp_appxmanifest_file_path, "exclude.txt")
with open(exclude_file_path, 'w') as exclude_file:
exclude_file.write("AppxMetadata\n")
exclude_file.write("[Content_Types].xml\n")
exclude_file.write("AppxBlockMap.xml\n")
exclude_file.write("AppxSignature.p7x\n")
else:
# 检查文件名中是否带有 x64 或 arm64
x64_file = next((f for f in pc_manager_msix_files if 'x64' in f), None)
arm64_file = next((f for f in pc_manager_msix_files if 'arm64' in f), None)
if not x64_file and not arm64_file:
return self.translator.translate("install_from_appxmanifest_no_match_pc_manager_architecture")
# 读取 PROCESSOR_ARCHITECTURE 的值
processor_architecture = winreg.QueryValueEx(
winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"),
"PROCESSOR_ARCHITECTURE"
)[0]
if processor_architecture == "AMD64" and x64_file:
pc_manager_zip_package_file_path = os.path.join(temp_appxmanifest_file_path, x64_file + ".zip")
shutil.copyfile(os.path.join(pc_manager_unpacked_path, x64_file), pc_manager_zip_package_file_path)
shutil.unpack_archive(pc_manager_zip_package_file_path,
os.path.join(temp_appxmanifest_file_path, os.path.splitext(x64_file)[0]))
pc_manager_unpacked_path = os.path.join(temp_appxmanifest_file_path, os.path.splitext(x64_file)[0])
elif processor_architecture == "ARM64" and arm64_file:
pc_manager_zip_package_file_path = os.path.join(temp_appxmanifest_file_path, arm64_file + ".zip")
shutil.copyfile(os.path.join(pc_manager_unpacked_path, arm64_file), pc_manager_zip_package_file_path)
shutil.unpack_archive(pc_manager_zip_package_file_path,
os.path.join(temp_appxmanifest_file_path, os.path.splitext(arm64_file)[0]))
pc_manager_unpacked_path = os.path.join(temp_appxmanifest_file_path,
os.path.splitext(arm64_file)[0])
else:
shutil.rmtree(temp_appxmanifest_file_path)
return self.translator.translate("install_from_appxmanifest_no_match_architecture")
# 写入 exclude.txt
exclude_file_path = os.path.join(temp_appxmanifest_file_path, "exclude.txt")
with open(exclude_file_path, 'w') as exclude_file:
exclude_file.write("AppxMetadata\n")
exclude_file.write("[Content_Types].xml\n")
exclude_file.write("AppxBlockMap.xml\n")
exclude_file.write("AppxSignature.p7x\n")
# 将最后解压的文件夹复制到 %ProgramFiles% 下,除了 exclude.txt 列出的文件与文件夹
pc_manager_program_files_path = os.path.join(os.environ['ProgramFiles'], os.path.basename(pc_manager_unpacked_path))
if not os.path.exists(pc_manager_program_files_path):
os.makedirs(pc_manager_program_files_path)
with open(exclude_file_path, 'r') as exclude_file:
exclude_list = exclude_file.read().splitlines()
for root, dirs, files in os.walk(pc_manager_unpacked_path):
relative_path = os.path.relpath(root, pc_manager_unpacked_path)
if relative_path in exclude_list:
continue
dest_dir = os.path.join(pc_manager_program_files_path, relative_path)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for file in files:
if file in exclude_list:
continue
shutil.copyfile(os.path.join(root, file), os.path.join(dest_dir, file))
# 修改 AppxManifest.xml 文件
appxmanifest_path = os.path.join(pc_manager_program_files_path, "AppxManifest.xml")
with open(appxmanifest_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
with open(appxmanifest_path, 'w', encoding='utf-8') as file:
for line in lines:
if '<TargetDeviceFamily' in line and not line.strip().startswith('<!--'):
indent = line[:len(line) - len(line.lstrip())]
file.write(f"{indent}<!-- {line.strip()} -->\n")
file.write(
f"{indent}<TargetDeviceFamily Name=\"Windows.Universal\" MinVersion=\"0.0.0.0\" MaxVersionTested=\"0.0.0.0\"/>\n")
else:
file.write(line)
# 弹出提示框询问用户是否选择依赖包
response_for_dependency = messagebox.askyesnocancel(
self.translator.translate("install_from_appxmanifest_dependency_package_select_notice"),
self.translator.translate("install_from_appxmanifest_dependency_package_select")
)
if response_for_dependency: # 选择依赖包
dependency_package_paths = filedialog.askopenfilenames(
filetypes=[("Msix", "*.msix"), ("*", "*")])
if not dependency_package_paths:
return self.translator.translate("install_from_appxmanifest_no_file_selected")
for dependency_path in dependency_package_paths:
subprocess.run(
['powershell.exe', '-Command', f'Add-AppxPackage -Path "{dependency_path}"'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
# 注册 AppxManifest.xml
subprocess.run(
['powershell.exe', '-Command', f'Add-AppxPackage -Register "{pc_manager_program_files_path}\\AppxManifest.xml"'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
# 询问是否注册服务
response_for_service = messagebox.askyesno(
self.translator.translate("install_from_appxmanifest_register_svc_notice"),
self.translator.translate("install_from_appxmanifest_register_svc")
)
if response_for_service:
# 检查服务 "PCManager Service Store" 是否存在
service_check_store = subprocess.run(
['powershell.exe', '-Command', 'Get-Service -Name "PCManager Service Store"'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
# 检查服务 "PC Manager Service" 是否存在
service_check_store_old = subprocess.run(
['powershell.exe', '-Command', 'Get-Service -Name "PC Manager Service"'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
if any(check.returncode == 0 for check in [service_check_store, service_check_store_old]):
messagebox.showwarning(
self.translator.translate("install_from_appxmanifest_svc_exists_warning"),
self.translator.translate("install_from_appxmanifest_svc_exists")
)
else:
# 创建服务
service_create = subprocess.run(
['sc.exe', 'create', 'PCManager Service Store', 'binPath=',
f'"{pc_manager_program_files_path}\\PCManager\\MSPCManagerService.exe"',
'DisplayName=', '"MSPCManager Service (Store)"', 'start=', 'auto'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
if service_create.returncode == 0:
# 设置服务描述
subprocess.run(
['sc.exe', 'description', 'PCManager Service Store',
'"Microsoft PCManager Service For Store"'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
# 启动服务
subprocess.run(
['sc.exe', 'start', 'PCManager Service Store'],
capture_output=True, text=True, creationflags=subprocess.CREATE_NO_WINDOW
)
# 清理临时文件
for temporary_files in os.listdir(temp_appxmanifest_file_path):
temporary_files_path = os.path.join(temp_appxmanifest_file_path, temporary_files)
if os.path.isfile(temporary_files_path):
os.remove(temporary_files_path)
elif os.path.isdir(temporary_files_path):
shutil.rmtree(temporary_files_path)
return self.translator.translate("install_from_appxmanifest_success")
except Exception as e:
return f"{self.translator.translate('install_from_appxmanifest_error')}: {str(e)}"
def install_wv2_runtime(self, app):
wv2_installer_temp_dir = os.path.join(tempfile.gettempdir(), "MSPCManagerHelper")
installer_path = os.path.join(wv2_installer_temp_dir, "MicrosoftEdgeWebView2Setup.exe")
download_url = "https://go.microsoft.com/fwlink/p/?LinkId=2124703"
try:
# 检查临时目录是否存在
if os.path.exists(wv2_installer_temp_dir):
shutil.rmtree(wv2_installer_temp_dir) # 删除临时目录
# 创建临时目录
os.makedirs(wv2_installer_temp_dir, exist_ok=True)
# 下载文件
response = requests.get(download_url)
if response.status_code == 200:
with open(installer_path, 'wb') as file:
file.write(response.content)
else:
return self.translator.translate("wv2_download_error")
# 运行安装程序
app.current_process = subprocess.Popen([installer_path, "/install"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = app.current_process.communicate()
if app.cancelled:
return self.translator.translate("wv2_installation_cancelled")
if app.current_process.returncode == 0:
return self.translator.translate("wv2_runtime_install_success") + '\n' + self.translator.translate("wv2_runtime_installer_download_link")
# 需要以管理员身份运行
elif app.current_process.returncode == 2147747880:
return f"{self.translator.translate('wv2_installer_exit_code')}: {app.current_process.returncode}\n{self.translator.translate('wv2_runtime_already_installed')}"
# 文件夹需要删除
elif app.current_process.returncode == 2147747596:
return f"{self.translator.translate('wv2_installer_exit_code')}: {app.current_process.returncode}\n{self.translator.translate('wv2_installer_exit_code_0x8004070c')}"
# 无文件夹写入权限
elif app.current_process.returncode == 2147942583:
return f"{self.translator.translate('wv2_installer_exit_code')}: {app.current_process.returncode}\n{self.translator.translate('wv2_installer_exit_code_0x800700b7')}"
else:
return f"{self.translator.translate('wv2_installer_exit_code')}: {app.current_process.returncode}\n{self.translator.translate('wv2_installer_error')}"
except Exception as e:
return f"{self.translator.translate('wv2_download_error_info')}: {str(e)}"
finally:
# 删除临时目录
if os.path.exists(wv2_installer_temp_dir):
shutil.rmtree(wv2_installer_temp_dir)
app.current_process = None