File size: 1,604 Bytes
d7be147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)