|
| 1 | +# Copyright 2022 iiPython |
| 2 | +# Build the installer.py file |
| 3 | + |
| 4 | +# Modules |
| 5 | +import os |
| 6 | +import io |
| 7 | +import shutil |
| 8 | +import zipfile |
| 9 | +from base64 import b85encode |
| 10 | + |
| 11 | +try: |
| 12 | + from main import __version__ as x2_version |
| 13 | + |
| 14 | +except ImportError: |
| 15 | + x2_version = input("x2 version: ") |
| 16 | + |
| 17 | +# Initialization |
| 18 | +to_include = [f for f in os.listdir() if f not in [ |
| 19 | + ".git", "__pycache__", "md", "scripts", ".xtconfig", "main.xt", |
| 20 | + "build", "dist", "installer.py", "installer.spec" |
| 21 | +]] |
| 22 | +print("Including all of the following:\n", "\n\t- ".join([""] + to_include).lstrip("\n")) |
| 23 | +if not input("Is this correct (y/N)? ").lower() == "y": |
| 24 | + exit(1) |
| 25 | + |
| 26 | +# Build our zip file into memory |
| 27 | +print("Zipping to memory ...") |
| 28 | +bio = io.BytesIO() |
| 29 | +with zipfile.ZipFile(bio, "w") as zf: |
| 30 | + for fn in to_include: |
| 31 | + if os.path.isfile(fn): |
| 32 | + zf.write(fn, fn) |
| 33 | + |
| 34 | + else: |
| 35 | + for path, _, files in os.walk(fn): |
| 36 | + if "__pycache__" in path: |
| 37 | + continue |
| 38 | + |
| 39 | + for file in files: |
| 40 | + fp = os.path.join(path, file) |
| 41 | + zf.write(fp, fp) |
| 42 | + |
| 43 | +print(" ..done!") |
| 44 | + |
| 45 | +# Create installer.py |
| 46 | +print("Writing to installer.py ...") |
| 47 | +installer_raw = f"""# Copyright 2022 iiPython |
| 48 | +# This file is autogenerated, please do not modify it. |
| 49 | +
|
| 50 | +# Modules |
| 51 | +import os |
| 52 | +import io |
| 53 | +import shutil |
| 54 | +import zipfile |
| 55 | +from base64 import b85decode |
| 56 | +
|
| 57 | +# Pre-install notice |
| 58 | +print("x2 Installer for {x2_version}\\n") |
| 59 | +print("Please note: this installer uses UP TO 2GB OF RAM!") |
| 60 | +print("Ensure you have 2gb of RAM available, then type 'proceed' to continue.") |
| 61 | +
|
| 62 | +if input("> ") != "proceed": |
| 63 | + exit(0) |
| 64 | +
|
| 65 | +print() |
| 66 | +
|
| 67 | +# Handle installation |
| 68 | +x2_dir = os.path.join(os.path.expanduser("~"), "x2") |
| 69 | +if os.path.isdir(x2_dir): |
| 70 | + if (input("x2 is already installed, upgrade (y/N)? ") or "n").lower() != "y": |
| 71 | + exit(0) |
| 72 | +
|
| 73 | + shutil.rmtree(x2_dir) |
| 74 | +
|
| 75 | +with zipfile.ZipFile(io.BytesIO(b85decode({b85encode(bio.getvalue())})), "r") as zf: |
| 76 | + zf.extractall(x2_dir) |
| 77 | +
|
| 78 | +shutil.move(os.path.join(x2_dir, "main.py"), os.path.join(x2_dir, "x2.py")) |
| 79 | +
|
| 80 | +# Create batch launcher |
| 81 | +with open(os.path.join(x2_dir, "x2.bat"), "w+") as launcher: |
| 82 | + launcher.write("@echo off\\ntitle x2\\n\\"%~dp0python\python.exe\\" \\"%~dp0x2.py\\" %*") |
| 83 | +
|
| 84 | +if (input("Add x2 to PATH (Y/n)? ") or "y").lower() == "y": |
| 85 | + os.system(f"setx PATH \\"%PATH%;{{x2_dir}}\\"") |
| 86 | +
|
| 87 | +# Finished with install |
| 88 | +print("\\nInstallation finished successfully.\\nMake sure you reopen all terminals for PATH to take effect.") |
| 89 | +input() |
| 90 | +""" # noqa |
| 91 | +del bio |
| 92 | + |
| 93 | +with open("installer.py", "w+") as installer: |
| 94 | + installer.write(installer_raw) |
| 95 | + |
| 96 | +print(" ..done!") |
| 97 | + |
| 98 | +# Build with pyinstaller |
| 99 | +if shutil.which("pyinstaller"): |
| 100 | + if input("pyinstaller was found, build now (y/N)? ").lower() == "y": |
| 101 | + os.system("pyinstaller -i scripts/assets/x2.ico -F installer.py") |
0 commit comments