software / RUNAPP.py
shethjenil's picture
Update RUNAPP.py
d7be147 verified
from zipfile import ZipFile
from subprocess import Popen
from os import path , getenv , kill
from shutil import rmtree
import argparse
from tkinter.filedialog import askopenfilename
import socket
import atexit
def check_port(port:int,host:str) -> bool:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.settimeout(1) # Adjust timeout as needed
s.connect((host, port))
s.close()
return True
except (socket.timeout, ConnectionRefusedError):
return False
argparser = argparse.ArgumentParser()
argparser.add_argument("-p","--port",type=int,default=12345,help="port number")
argparser.add_argument("-a","--host",type=str,default="localhost",help="host address")
argparser.add_argument("-i","--image",type=str,help="app image")
args = argparser.parse_args()
folder = getenv("LOCALAPPDATA")+"\\"+f"JenilSoftware_{args.host}_{args.port}"
process = None
def kill_process():
if process:
kill(process.pid)
rmtree(folder)
atexit.register(kill_process)
try:
if path.exists(folder):
rmtree(folder)
image = None
if not check_port(args.port,args.host):
if args.image:
image = args.image
else:
image = askopenfilename(filetypes=[("Application Image",["*.png"])],defaultextension=".png")
ZipFile(image).extractall(folder)
process = Popen([folder+r"\main","-p",str(args.port),"-a",str(args.host),"-m","2"],shell=True)
process.wait()
else:
raise Exception("port is not available for app")
except Exception as e:
print(e)
rmtree(folder)