61 lines
1.4 KiB
Python
61 lines
1.4 KiB
Python
import os
|
|
import sys
|
|
import zipfile
|
|
import tempfile
|
|
import subprocess
|
|
import shutil
|
|
|
|
def resource_path(relative_path: str) -> str:
|
|
"""
|
|
Get absolute path to resource, works for dev and for PyInstaller bundle.
|
|
"""
|
|
if hasattr(sys, "_MEIPASS"):
|
|
return os.path.join(sys._MEIPASS, relative_path)
|
|
return os.path.join(os.path.abspath("."), relative_path)
|
|
|
|
def extract_and_run():
|
|
zip_name = "app.zip"
|
|
exe_name = "app.exe"
|
|
|
|
zip_path = resource_path(zip_name)
|
|
|
|
if not os.path.isfile(zip_path):
|
|
print("Payload app.zip not found!")
|
|
return 1
|
|
|
|
temp_dir = tempfile.mkdtemp(prefix="self_extract_")
|
|
|
|
try:
|
|
with zipfile.ZipFile(zip_path, "r") as z:
|
|
z.extractall(temp_dir)
|
|
|
|
exe_path = os.path.join(temp_dir, exe_name)
|
|
|
|
if not os.path.isfile(exe_path):
|
|
print("app.exe not found inside extracted payload!")
|
|
return 2
|
|
|
|
# Run the extracted executable
|
|
process = subprocess.Popen(
|
|
[exe_path],
|
|
cwd=temp_dir,
|
|
shell=False
|
|
)
|
|
|
|
process.wait()
|
|
return process.returncode
|
|
|
|
finally:
|
|
# Cleanup after execution
|
|
try:
|
|
shutil.rmtree(temp_dir, ignore_errors=True)
|
|
except Exception:
|
|
pass
|
|
|
|
def main():
|
|
code = extract_and_run()
|
|
sys.exit(code if code is not None else 0)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|