2021-07-02 16:18:35 +03:00
import sys
import os . path
import subprocess
import typing
from uds . log import logger
import UDSClient
from UDSLauncherMac import Ui_MacLauncher
from PyQt5 import QtCore , QtWidgets , QtGui
SCRIPT_NAME = ' UDSClientLauncher '
class UdsApplication ( QtWidgets . QApplication ) :
path : str
2021-07-03 13:30:46 +03:00
tunnels : typing . List [ subprocess . Popen ]
2021-07-02 16:18:35 +03:00
def __init__ ( self , argv : typing . List [ str ] ) - > None :
super ( ) . __init__ ( argv )
self . path = os . path . join ( os . path . dirname ( sys . argv [ 0 ] ) . replace ( ' Resources ' , ' MacOS ' ) , SCRIPT_NAME )
2021-07-03 13:30:46 +03:00
self . tunnels = [ ]
self . lastWindowClosed . connect ( self . closeTunnels ) # type: ignore
2021-07-02 16:18:35 +03:00
2021-07-03 13:30:46 +03:00
def cleanTunnels ( self ) - > None :
2021-07-03 22:48:38 +03:00
def isRunning ( p : subprocess . Popen ) :
try :
if p . poll ( ) is None :
return True
except Exception as e :
logger . debug ( ' Got error polling subprocess: %s ' , e )
return False
for k in [ i for i , tunnel in enumerate ( self . tunnels ) if not isRunning ( tunnel ) ] :
try :
del self . tunnels [ k ]
except Exception as e :
logger . debug ( ' Error closing tunnel: %s ' , e )
2021-07-02 16:18:35 +03:00
2021-07-03 13:30:46 +03:00
def closeTunnels ( self ) - > None :
logger . debug ( ' Closing remaining tunnels ' )
for tunnel in self . tunnels :
2021-07-03 13:58:26 +03:00
logger . debug ( ' Checking %s - " %s " ' , tunnel , tunnel . poll ( ) )
2021-07-03 13:30:46 +03:00
if tunnel . poll ( ) is None : # Running
logger . info ( ' Found running tunnel %s , closing it ' , tunnel . pid )
tunnel . kill ( )
def event ( self , evnt : QtCore . QEvent ) - > bool :
2021-07-02 16:18:35 +03:00
if evnt . type ( ) == QtCore . QEvent . FileOpen :
fe = typing . cast ( QtGui . QFileOpenEvent , evnt )
logger . debug ( ' Got url: %s ' , fe . url ( ) . url ( ) )
fe . accept ( )
logger . debug ( ' Spawning %s ' , self . path )
2021-07-03 14:02:34 +03:00
# First, remove all finished tunnel processed from check queue
self . cleanTunnels ( )
# And now add a new one
self . tunnels . append ( subprocess . Popen ( [ self . path , fe . url ( ) . url ( ) ] ) )
2021-07-02 16:18:35 +03:00
return super ( ) . event ( evnt )
def main ( args : typing . List [ str ] ) :
if len ( args ) > 1 :
UDSClient . main ( args )
else :
app = UdsApplication ( sys . argv )
window = QtWidgets . QMainWindow ( )
Ui_MacLauncher ( ) . setupUi ( window )
window . showMinimized ( )
2021-08-22 00:06:19 +03:00
sys . exit ( app . exec ( ) )
2021-07-02 16:18:35 +03:00
if __name__ == " __main__ " :
main ( args = sys . argv )