|
| 1 | +""" This module is the installer for the Subconscious release distributable. """ |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import shutil |
| 5 | +import ctypes |
| 6 | +import zipfile |
| 7 | +import requests |
| 8 | +import tempfile |
| 9 | +import flet as ft |
| 10 | + |
| 11 | +from titlebar import TitleBar |
| 12 | + |
| 13 | + |
| 14 | +def is_admin(): |
| 15 | + try: |
| 16 | + return ctypes.windll.shell32.IsUserAnAdmin() |
| 17 | + except: |
| 18 | + return False |
| 19 | + |
| 20 | +def download_latest_release(download_dir, pb): |
| 21 | + """ Download the latest release of the Subconscious app """ |
| 22 | + # GitHub API URL for the latest release |
| 23 | + url = "https://api.github.com/repos/baebranch/subconscious/releases/latest" |
| 24 | + |
| 25 | + # Send a GET request to the GitHub API |
| 26 | + response = requests.get(url) |
| 27 | + response.raise_for_status() # Raise an exception for HTTP errors |
| 28 | + file_size = int(response.headers.get('Content-Length', 0)) |
| 29 | + |
| 30 | + # Parse the JSON response |
| 31 | + release_data = response.json() |
| 32 | + |
| 33 | + # Get the URL of the asset to download (assuming the first asset) |
| 34 | + asset = release_data['assets'][0] |
| 35 | + download_url = asset['browser_download_url'] |
| 36 | + asset_name = asset['name'] |
| 37 | + |
| 38 | + # Download the asset |
| 39 | + download_response = requests.get(download_url, stream=True) |
| 40 | + download_response.raise_for_status() |
| 41 | + |
| 42 | + # Save the asset to the specified directory |
| 43 | + file_path = os.path.join(download_dir, asset_name) |
| 44 | + with open(file_path, 'wb') as file: |
| 45 | + for chunk in download_response.iter_content(chunk_size=8192): |
| 46 | + file.write(chunk) |
| 47 | + |
| 48 | + return file_path |
| 49 | + |
| 50 | + |
| 51 | +def extract_to_program_files(zip_path, program_files_dir): |
| 52 | + """ Extract the downloaded zip file to the Program Files directory """ |
| 53 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 54 | + zip_ref.extractall(program_files_dir) |
| 55 | + |
| 56 | + |
| 57 | +def main(page: ft.Page): |
| 58 | + """ Main function to initiate the installer UI """ |
| 59 | + # Window size |
| 60 | + page.visible = False |
| 61 | + page.window.width = page.window.min_width = page.window.max_width = 450 |
| 62 | + page.window.height = page.window.min_height = page.window.max_height = 250 |
| 63 | + page.window.center() |
| 64 | + page.padding = 0 |
| 65 | + page.spacing = 0 |
| 66 | + page.window.frameless = False |
| 67 | + page.window.title_bar_hidden = True |
| 68 | + page.theme_mode = ft.ThemeMode.SYSTEM |
| 69 | + |
| 70 | + # UI Config |
| 71 | + done = ft.TextButton("Done", on_click=lambda _: page.window.close(), style=ft.ButtonStyle(shape=ft.RoundedRectangleBorder(radius=5)), visible=False) |
| 72 | + step = ft.Text("Installing...", size=15) |
| 73 | + pb = ft.ProgressBar(width=430, bar_height=5, height=5) |
| 74 | + |
| 75 | + # Layout |
| 76 | + page.add(TitleBar(page, title="Subconscious Installer")) |
| 77 | + page.add(ft.Container(content= |
| 78 | + ft.Column( |
| 79 | + [ |
| 80 | + ft.Row([ |
| 81 | + ft.Text("Subconscious", size=25, color=ft.Colors.PRIMARY), |
| 82 | + ], alignment="left", spacing=0), |
| 83 | + step, |
| 84 | + pb, |
| 85 | + ft.Row([ |
| 86 | + done |
| 87 | + ], expand=True) |
| 88 | + ], |
| 89 | + spacing=20, expand=True, alignment=ft.alignment.top_center |
| 90 | + ), |
| 91 | + padding=ft.padding.only(20, 20, 20, 20)) |
| 92 | + ) |
| 93 | + page.visible = True |
| 94 | + page.update() |
| 95 | + |
| 96 | + try: |
| 97 | + # Create a temporary directory |
| 98 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 99 | + # Download the latest release to the temporary directory |
| 100 | + step.value = "Downloading..." |
| 101 | + pb.value = 0.0 |
| 102 | + page.update() |
| 103 | + zip_path = download_latest_release(temp_dir, pb) |
| 104 | + |
| 105 | + # Define the Program Files directory |
| 106 | + program_files_dir = os.path.join(os.environ['ProgramFiles'], 'Subconscious') |
| 107 | + step.value = f"Installing to {program_files_dir}..." |
| 108 | + pb.value = 0.50 |
| 109 | + page.update() |
| 110 | + |
| 111 | + # Ensure the Program Files directory exists |
| 112 | + os.makedirs(program_files_dir, exist_ok=True) |
| 113 | + |
| 114 | + # Extract the downloaded zip file to the Program Files directory |
| 115 | + step.value = "Extracting files..." |
| 116 | + pb.value = 0.75 |
| 117 | + page.update() |
| 118 | + extract_to_program_files(zip_path, program_files_dir) |
| 119 | + done.visible = True |
| 120 | + step.value = "Installation completed successfully!" |
| 121 | + pb.value = 1.0 |
| 122 | + page.update() |
| 123 | + except Exception as e: |
| 124 | + pb.value = 0 |
| 125 | + done.visible = True |
| 126 | + step.value = "An error occurred, could not install" |
| 127 | + page.update() |
| 128 | + print(e) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + if is_admin(): |
| 133 | + print("Already running as administrator.") |
| 134 | + ft.app(target=main) |
| 135 | + else: |
| 136 | + print("Attempting to run as administrator...") |
| 137 | + ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) |
0 commit comments