1
1
import asyncio
2
2
import os
3
+ import subprocess
3
4
import time
4
5
from datetime import datetime
5
6
from threading import Event
6
7
7
- import clr
8
8
from pypdl import Pypdl
9
9
from PyQt6 .QtCore import QThreadPool
10
10
from PyQt6 .QtGui import QIcon
15
15
from utls import Worker , default_logger
16
16
17
17
script_dir = os .path .dirname (os .path .abspath (__file__ ))
18
- clr .AddReference (rf"{ script_dir } \data\System.Management.Automation.dll" )
19
18
20
19
21
20
class internal_func (Ui_MainProgram ):
@@ -105,7 +104,7 @@ def log_error():
105
104
current_time = datetime .now ().strftime ("[%d-%m-%Y %H:%M:%S]" )
106
105
f .write (f"[python logs] \n { current_time } \n \n " )
107
106
f .write (n [2 ])
108
- f .write (f' { 82 * "-" } \n ' )
107
+ f .write (f" { 82 * '-' } \n " )
109
108
110
109
# if normal show a simple popup
111
110
if normal :
@@ -322,58 +321,45 @@ async def new_url_gen():
322
321
self .threadpool .start (worker )
323
322
324
323
def install (self , arg , ** kwargs ):
325
- # importing the system management.Automation dlls powershell funcs
326
- from System .Management .Automation import PowerShell
327
-
328
324
self .stop_btn .hide ()
329
325
self .pushButton .show ()
330
326
331
327
def install_thread (path , progress_current , progress_main , uwp , val = True ):
328
+ def run (command ):
329
+ output = subprocess .run (
330
+ [
331
+ "C:\\ WINDOWS\\ system32\\ WindowsPowerShell\\ v1.0\\ powershell.exe" ,
332
+ command ,
333
+ ],
334
+ creationflags = subprocess .CREATE_NO_WINDOW ,
335
+ capture_output = True ,
336
+ text = True ,
337
+ )
338
+ return output
339
+
332
340
flag = 0
333
341
main_prog_error = 0
334
342
part = int ((100 - self .mainprogressBar .value ()) / len (path ))
335
343
336
- # helper func for getting progress from powershell
337
- def Progress (source , e ):
338
- prog = int (source [e .Index ].PercentComplete )
339
- # to remove -1 from the progress bar
340
- progress_current .emit (prog if prog > 0 else 0 )
341
- if not val :
342
- progress_main .emit (prog if prog > 0 else 0 )
343
-
344
- # helper func for getting error from powershell
345
- def error (source , e ):
346
- nonlocal flag , main_prog_error
347
-
348
- flag = 1
349
- if path [s_path ] == 1 :
350
- main_prog_error = 1
351
-
352
- with open (f"{ script_dir } /log.txt" , "a" ) as f :
353
- current_time = datetime .now ().strftime ("[%d-%m-%Y %H:%M:%S]" )
354
- f .write (f"[powershell logs] \n { current_time } \n \n " )
355
- f .write (f'Package Name: { s_path .split ("/" )[- 1 ]} \n \n ' )
356
- f .write (str (source [e .Index ].Exception .Message ))
357
- f .write (f'{ 82 * "-" } \n ' )
358
-
359
344
for s_path in path .keys ():
360
- # C# command run using pythonnet via system.management.automation dll
361
- ps = PowerShell .Create ()
362
- ps .Streams .Progress .DataAdded += Progress
363
- ps .Streams .Error .DataAdded += error
364
-
345
+ progress_current .emit (10 )
365
346
if uwp :
366
- ps .AddCommand ("Add-AppxPackage" )
367
- ps .AddParameter ("Path" , s_path )
347
+ output = run (f'Add-AppPackage "{ s_path } "' )
368
348
else :
369
- ps .AddCommand ("Start-Process" )
370
- ps .AddParameter ("FilePath" , s_path )
371
-
372
- try :
373
- ps .Invoke ()
374
- except Exception as e :
375
- print (e )
376
-
349
+ output = run (f'Start-Process "{ s_path } "' )
350
+
351
+ if not output .returncode == 0 :
352
+ flag = 1
353
+ if path [s_path ] == 1 :
354
+ main_prog_error = 1
355
+
356
+ with open (f"{ script_dir } /log.txt" , "a" ) as f :
357
+ current_time = datetime .now ().strftime ("[%d-%m-%Y %H:%M:%S]" )
358
+ f .write (f"[powershell logs] \n { current_time } \n \n " )
359
+ f .write (f"command: { output .args [1 ]} \n \n " )
360
+ f .write (output .stderr )
361
+ f .write (f"{ 82 * '-' } \n " )
362
+ progress_current .emit (100 )
377
363
time .sleep (0.3 )
378
364
progress_main .emit (part )
379
365
@@ -391,7 +377,6 @@ def error(source, e):
391
377
detail_msg += "if the app is not installed, Enable [Advanced --> Dependencies --> Ignore Version], "
392
378
detail_msg += "If the problem still exists Enable [Advanced --> Dependencies --> Ignore All Filters]"
393
379
endresult = (msg , detail_msg , "Warning" )
394
-
395
380
return endresult
396
381
return 0
397
382
0 commit comments