software / pyimageserverrun.py
shethjenil's picture
Upload pyimageserverrun.py
4411845 verified
# import sys
# import ctypes
# if not ctypes.windll.shell32.IsUserAnAdmin():
# ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
# sys.exit()
from argparse import ArgumentParser
from zipfile import ZipFile
from subprocess import Popen, PIPE
from shutil import rmtree
import atexit
argparser = ArgumentParser()
argparser.add_argument("-i", "--image", type=str, help="app image", required=True)
argparser.add_argument("-p", "--port", type=int, default=12345, help="port number")
argparser.add_argument("-f", "--folder", type=str, default="appfolder", help="app folder")
argparser.add_argument("-a", "--address", type=str, default="0.0.0.0", help="address to listen on")
args = argparser.parse_args()
folder = args.folder
rmtree(folder, ignore_errors=True)
atexit.register(lambda: rmtree(folder, ignore_errors=True))
ZipFile(args.image).extractall(folder)
try:
process = Popen([folder + "\\main", "-p", str(args.port), "-a", str(args.address)], stdout=PIPE, stderr=PIPE, shell=True)
while True:
output = process.stdout.readline()
if output == b"" and process.poll() is not None:
break
if output:
if "Running a app" in output.decode():
print("Setup complete")
process.wait()
except Exception as e:
print(f"An error occurred: {e}")
finally:
rmtree(folder)