mirror of
https://github.com/FAUSheppy/homelab_gamevault
synced 2025-12-06 06:51:36 +01:00
Compare commits
63 Commits
linux-supp
...
dev/update
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
010e19e6b4 | ||
|
|
366080afa2 | ||
|
|
3a99e0195b | ||
|
|
1b11b46723 | ||
| 472d9cfca2 | |||
| cf55f6f387 | |||
|
|
2e8a5facfd | ||
| 0dea7a55f4 | |||
|
|
94aaf97a4d | ||
|
|
3579948407 | ||
|
|
597a471949 | ||
|
|
27d32e147b | ||
|
|
ac8e8ad495 | ||
|
|
3368048dd7 | ||
|
|
7f608019ed | ||
|
|
839efae1a3 | ||
|
|
36e5cc3842 | ||
|
|
5ab17e6f4c | ||
|
|
2e5676d5a6 | ||
|
|
a2702d7f70 | ||
|
|
344d32901e | ||
|
|
aefab57bb0 | ||
|
|
d3840c216c | ||
|
|
3912c66bb3 | ||
|
|
8e5bfd9ae3 | ||
|
|
4e9b85ee6d | ||
|
|
8e9e4db3fa | ||
|
|
47f9912dc7 | ||
|
|
c256a8ae3b | ||
|
|
554b4ece7a | ||
|
|
a01d8992c0 | ||
|
|
e35803ce1a | ||
|
|
0d4fd8852d | ||
|
|
7719764604 | ||
|
|
b0741929a3 | ||
|
|
d6f8e6ac4d | ||
|
|
c2d3a0f37a | ||
|
|
8c0e65c194 | ||
|
|
c13725cd84 | ||
|
|
4785517b6c | ||
|
|
1c48b033d3 | ||
|
|
68f14c0831 | ||
|
|
de0627bbcd | ||
|
|
3545ecbaa8 | ||
|
|
311483df19 | ||
|
|
df3ea69efb | ||
|
|
f46fce1824 | ||
| 0d35e9c095 | |||
| 1f3b489094 | |||
| 6f4f658393 | |||
| 1cd0516e64 | |||
| ff26fe9bb1 | |||
| 6c6cafe1db | |||
| b515d32d06 | |||
| f3342ac5aa | |||
| 0f573cba74 | |||
| e4c4f17d09 | |||
| 05d2833344 | |||
| 8c2057d8ac | |||
| e1ec605183 | |||
| 2eddfb2d45 | |||
| cc5d9685fa | |||
|
|
a36c737668 |
43
.github/workflows/release.yaml
vendored
Normal file
43
.github/workflows/release.yaml
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
name: Build and Release EXE
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install pyinstaller
|
||||
pip install -r requirements.txt
|
||||
|
||||
- name: Build EXE
|
||||
run: pyinstaller client.py
|
||||
|
||||
- name: Copy helper scripts
|
||||
run: |
|
||||
cp .\dist\run.bat .\dist\client\
|
||||
cp .\dist\run.ps1 .\dist\client\
|
||||
cp .\windows_run_as_admin.ps1 .\dist\client\
|
||||
|
||||
- name: Archive EXE
|
||||
run: Compress-Archive -Path dist\ -DestinationPath release.zip
|
||||
|
||||
- name: Release
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: gh release create ${{ github.ref_name }} "release.zip" --generate-notes --title "release-${{ github.ref_name }}"
|
||||
|
||||
37
.github/workflows/server.yaml
vendored
Normal file
37
.github/workflows/server.yaml
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "master"
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
environment:
|
||||
name: prod
|
||||
steps:
|
||||
-
|
||||
name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
-
|
||||
name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v2
|
||||
-
|
||||
name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
-
|
||||
name: Login to Docker Registry
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
registry: ${{ secrets.REGISTRY }}
|
||||
username: ${{ secrets.REGISTRY_USER }}
|
||||
password: ${{ secrets.REGISTRY_PASS }}
|
||||
-
|
||||
name: Build and push async-icinga image
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
context: server
|
||||
platforms: linux/amd64
|
||||
push: true
|
||||
tags: "${{ secrets.REGISTRY }}/atlantishq/gamevault-server:latest"
|
||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -6,7 +6,13 @@ cache/
|
||||
install/
|
||||
*.json
|
||||
install-dir/
|
||||
|
||||
*.spec
|
||||
*.exe
|
||||
dist/
|
||||
%APPDATA%/
|
||||
example_software_root/
|
||||
|
||||
# windows dir on linux ä
|
||||
**\\install-dir/
|
||||
server/data/
|
||||
database.db
|
||||
|
||||
20
README.md
20
README.md
@@ -1,8 +1,18 @@
|
||||
# Example Game
|
||||
You can download the GNU GPL Licensed game "FreeDink" from [here](https://nextcloud.atlantishq.de/s/9T62K9WjpEt3AQ7) and put it into `example_software_root/FreeDink/main_dir" to test it out.
|
||||
|
||||
# TODO
|
||||
- check local cache before loading from remote
|
||||
- better image resizing, prevent stretching etc.
|
||||
- async load from remote, currently the who GUI blocks during the loading process
|
||||
- unpacking progress
|
||||
# Linux
|
||||
|
||||
sudo mkdir -pm755 /etc/apt/keyrings
|
||||
sudo wget -O /etc/apt/keyrings/winehq-archive.key https://dl.winehq.org/wine-builds/winehq.key
|
||||
chmod a+r /etc/apt/keyrings/*
|
||||
sudo wget -NP /etc/apt/sources.list.d/ https://dl.winehq.org/wine-builds/debian/dists/bullseye/winehq-bullseye.sources\n
|
||||
sudo apt update
|
||||
sudo dpkg --add-architecture i386 \n
|
||||
sudo apt install --install-recommends winehq-staging
|
||||
sudo apt install libgl1:i386 nvidia-driver-libs:i386
|
||||
sudo apt install python3 python3-pip python3-tk
|
||||
|
||||
# non sudo in project #
|
||||
python -m pip install -r requirements.txt
|
||||
python client.py
|
||||
|
||||
31
check_release.py
Normal file
31
check_release.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import requests
|
||||
import os
|
||||
|
||||
REPO = "FAUSheppy/homelab_gamevault"
|
||||
API_URL = f"https://api.github.com/repos/{REPO}/releases/latest"
|
||||
VERSION_FILE = ".gamevault_version"
|
||||
|
||||
def get_latest_release():
|
||||
response = requests.get(API_URL)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
version = data['tag_name']
|
||||
zip_url = data['zipball_url']
|
||||
return version, zip_url
|
||||
|
||||
|
||||
def read_local_version():
|
||||
if not os.path.exists(VERSION_FILE):
|
||||
return None
|
||||
with open(VERSION_FILE, 'r') as f:
|
||||
return f.read().strip()
|
||||
|
||||
def update_updater():
|
||||
pass # TODO
|
||||
# download updater
|
||||
# replace updater
|
||||
|
||||
def execute_updater(new_version):
|
||||
# TODO
|
||||
# os.system(["updater.exe", new_version])
|
||||
pass
|
||||
137
client.py
137
client.py
@@ -8,6 +8,11 @@ import json
|
||||
import os
|
||||
import cache_utils
|
||||
import imagetools
|
||||
import webbrowser
|
||||
import statekeeper
|
||||
import infowidget
|
||||
import requests
|
||||
import tkinter
|
||||
|
||||
customtkinter.set_appearance_mode("dark")
|
||||
customtkinter.set_default_color_theme("blue")
|
||||
@@ -17,15 +22,21 @@ app = customtkinter.CTk()
|
||||
app.geometry("1490x700")
|
||||
last_geometry = app.winfo_geometry()
|
||||
|
||||
scrollable_frame = customtkinter.CTkScrollableFrame(app, width=app.winfo_width(), height=app.winfo_height())
|
||||
|
||||
buttons = []
|
||||
details_elements = []
|
||||
|
||||
non_disabled_entry_color = None
|
||||
all_metadata = None
|
||||
|
||||
infowidget_window = None
|
||||
|
||||
db = None # app data-backend (i.e. LocalFS or FTP)
|
||||
|
||||
CONFIG_FILE = "gamevault_config.json"
|
||||
|
||||
|
||||
def close_input_window(input_window):
|
||||
'''Close the config window and save the settings'''
|
||||
|
||||
@@ -66,6 +77,14 @@ def dropdown_changed(dropdown_var, user_entry, password_entry, server_path_entry
|
||||
user_entry.configure(fg_color="#CCCCCC")
|
||||
server_path_entry.delete(0, customtkinter.END)
|
||||
server_path_entry.insert(0, "C:/path/to/game/mount/")
|
||||
elif dropdown_var == "FTP/FTPS":
|
||||
user_entry.configure(state=customtkinter.NORMAL)
|
||||
password_entry.configure(state=customtkinter.NORMAL)
|
||||
if non_disabled_entry_color: # else first run and nothing to do
|
||||
password_entry.configure(fg_color=non_disabled_entry_color)
|
||||
user_entry.configure(fg_color=non_disabled_entry_color)
|
||||
server_path_entry.delete(0, customtkinter.END)
|
||||
server_path_entry.insert(0, "ftp://server:port/path or ftps://server:port/path")
|
||||
else:
|
||||
user_entry.configure(state=customtkinter.NORMAL)
|
||||
password_entry.configure(state=customtkinter.NORMAL)
|
||||
@@ -73,7 +92,7 @@ def dropdown_changed(dropdown_var, user_entry, password_entry, server_path_entry
|
||||
password_entry.configure(fg_color=non_disabled_entry_color)
|
||||
user_entry.configure(fg_color=non_disabled_entry_color)
|
||||
server_path_entry.delete(0, customtkinter.END)
|
||||
server_path_entry.insert(0, "ftp://server/path::port or ftps://server/path:port")
|
||||
|
||||
|
||||
install_dir_entry.delete(0, customtkinter.END)
|
||||
install_dir_entry.insert(0, "./install-dir")
|
||||
@@ -112,10 +131,10 @@ def get_config_inputs():
|
||||
install_dir_entry.grid(row=4, column=1, padx=10, pady=5, sticky="ew", columnspan=2)
|
||||
|
||||
# Dropdown
|
||||
dropdown_var = customtkinter.StringVar(value="Local Filesystem")
|
||||
dropdown_var = customtkinter.StringVar(value="HTTP/HTTPS")
|
||||
dropdown_label = customtkinter.CTkLabel(input_window, text="Select option:")
|
||||
dropdown_label.grid(row=0, column=0, sticky="w", padx=10, pady=5)
|
||||
dropdown = customtkinter.CTkOptionMenu(input_window, variable=dropdown_var, values=["FTP/FTPS", "Local Filesystem"],
|
||||
dropdown = customtkinter.CTkOptionMenu(input_window, variable=dropdown_var, values=["HTTP/HTTPS", "FTP/FTPS", "Local Filesystem"],
|
||||
command=lambda dropdown_var=dropdown_var, user_entry=user_entry, password_entry=password_entry,
|
||||
server_path_entry=server_path_entry, install_dir_entry=install_dir_entry:
|
||||
dropdown_changed(dropdown_var, user_entry, password_entry, server_path_entry, install_dir_entry))
|
||||
@@ -124,13 +143,14 @@ def get_config_inputs():
|
||||
dropdown_changed(dropdown_var, user_entry, password_entry, server_path_entry, install_dir_entry)
|
||||
dropdown.grid(row=0, column=1, padx=10, pady=5)
|
||||
|
||||
# Button to abort & close #
|
||||
abort_button = customtkinter.CTkButton(input_window, text="Abort", command=lambda: input_window.quit(), fg_color="red")
|
||||
abort_button.grid(row=5, column=0, padx=10, pady=20)
|
||||
|
||||
# Button to save & close #
|
||||
save_button = customtkinter.CTkButton(input_window, text="Save & Close", command=lambda: close_input_window(input_window))
|
||||
save_button.grid(row=5, column=0, padx=10, pady=20)
|
||||
save_button.grid(row=5, column=2, padx=10, pady=20)
|
||||
|
||||
# Button to abort & close #
|
||||
abort_button = customtkinter.CTkButton(input_window, text="Abort", command=lambda: input_window.quit())
|
||||
abort_button.grid(row=5, column=2, padx=10, pady=20)
|
||||
|
||||
input_window.update()
|
||||
input_window.mainloop()
|
||||
@@ -165,18 +185,44 @@ def switch_to_game_details(software):
|
||||
def load_main():
|
||||
'''Load the main page overview'''
|
||||
|
||||
global all_metadata
|
||||
global infowidget_window
|
||||
|
||||
app.title("Lan Vault: Overview")
|
||||
|
||||
if not infowidget_window:
|
||||
infowidget_window = infowidget.ProgressBarApp(app, data_backend=db)
|
||||
|
||||
infowidget_window.root.grid(row=1, column=0, sticky="n")
|
||||
|
||||
# navbar should not expand when window is resized
|
||||
app.grid_rowconfigure(0, weight=0)
|
||||
# buttongrid (scrollable frame) should expand when window is resized
|
||||
app.grid_rowconfigure(1, weight=1)
|
||||
app.grid_columnconfigure(1, weight=1)
|
||||
# place scrollable frame
|
||||
scrollable_frame.grid(row=1, column=1, sticky="nsew", columnspan=2)
|
||||
|
||||
|
||||
# create tiles from meta files #
|
||||
cache_dir_size = 0
|
||||
for software in db.find_all_metadata():
|
||||
create_main_window_tile(software)
|
||||
if not all_metadata:
|
||||
all_metadata = db.find_all_metadata()
|
||||
|
||||
for software in all_metadata:
|
||||
|
||||
print("Software:", software)
|
||||
create_main_window_tile(software, scrollable_frame)
|
||||
|
||||
# retrieve cache dir from any software #
|
||||
if not cache_dir_size:
|
||||
cache_dir_size = cache_utils.get_cache_size()
|
||||
label = customtkinter.CTkLabel(app, text="Cache Size: {:.2f} GB".format(cache_dir_size))
|
||||
GITHUB_URL = "https://github.com/FAUSheppy/homelab_gamevault"
|
||||
github = customtkinter.CTkButton(app, text="Star on Github",
|
||||
command=lambda: webbrowser.open_new(GITHUB_URL))
|
||||
label.grid(row=0, column=0)
|
||||
github.grid(row=0, column=1)
|
||||
|
||||
# set update listener & update positions #
|
||||
update_button_positions()
|
||||
@@ -186,7 +232,7 @@ def destroy_main():
|
||||
'''Destroy all elements in the main view'''
|
||||
|
||||
global buttons
|
||||
|
||||
scrollable_frame.grid_remove()
|
||||
app.unbind("<Configure>")
|
||||
for b in buttons:
|
||||
b.destroy()
|
||||
@@ -200,29 +246,42 @@ def load_details(app, software):
|
||||
global details_elements
|
||||
|
||||
app.title("Lan Vault: {}".format(software.title))
|
||||
details_elements = client_details.create_details_page(app, software, switch_to_main)
|
||||
details_elements = client_details.create_details_page(app, software, switch_to_main, infowidget_window)
|
||||
|
||||
def create_main_window_tile(software):
|
||||
def create_main_window_tile(software, parent):
|
||||
'''Create the main window tile'''
|
||||
|
||||
if software.get_thumbnail():
|
||||
try:
|
||||
print("Loading thumbnail:", software.get_thumbnail())
|
||||
img = PIL.Image.open(software.get_thumbnail())
|
||||
img = imagetools.smart_resize(img, 200, 300)
|
||||
except PIL.UnidentifiedImageError:
|
||||
print("Failed to load thumbnail:", software.get_thumbnail())
|
||||
img = PIL.Image.new('RGB', (200, 300))
|
||||
else:
|
||||
img = PIL.Image.new('RGB', (200, 300))
|
||||
|
||||
img = PIL.ImageTk.PhotoImage(img)
|
||||
button = customtkinter.CTkButton(app, image=img,
|
||||
imgTk = PIL.ImageTk.PhotoImage(img)
|
||||
button = customtkinter.CTkButton(parent, image=imgTk,
|
||||
width=200, height=300,
|
||||
command=lambda: switch_to_game_details(software),
|
||||
border_width=0, corner_radius=0, border_spacing=0,
|
||||
text=software.title,
|
||||
fg_color="transparent", compound="top", anchor="s")
|
||||
|
||||
|
||||
def callback_update_thumbnail():
|
||||
|
||||
# TODO: bind button & software into this callback
|
||||
|
||||
try:
|
||||
target_file = software.get_thumbnail()
|
||||
if not target_file:
|
||||
return
|
||||
print("Loading thumbnail (async):", target_file)
|
||||
img = PIL.Image.open(target_file)
|
||||
img = imagetools.smart_resize(img, 200, 300)
|
||||
except PIL.UnidentifiedImageError:
|
||||
print("Failed to load thumbnail:", target_file)
|
||||
img = PIL.Image.new('RGB', (200, 300))
|
||||
|
||||
button.configure(image=PIL.ImageTk.PhotoImage(img))
|
||||
|
||||
# register the update task for the image #
|
||||
statekeeper.add_to_task_queue(callback_update_thumbnail)
|
||||
|
||||
# cache button and return #
|
||||
buttons.append(button)
|
||||
return button
|
||||
|
||||
@@ -230,16 +289,16 @@ def update_button_positions(event=None):
|
||||
'''Sets the tile positions initially and on resize'''
|
||||
|
||||
global last_geometry
|
||||
|
||||
# check vs old location #
|
||||
new_geometry = app.winfo_geometry()
|
||||
if last_geometry[0] == new_geometry[0] and last_geometry[1] == new_geometry[1]:
|
||||
return
|
||||
else:
|
||||
last_geometry = new_geometry
|
||||
scrollable_frame.configure(width=app.winfo_width(), height=app.winfo_height())
|
||||
|
||||
# Calculate the number of columns based on the current width of the window #
|
||||
num_columns = app.winfo_width() // 201 # Adjust 100 as needed for button width
|
||||
num_columns = app.winfo_width() // 301 # Adjust 100 as needed for button width
|
||||
|
||||
# window became too small #
|
||||
if num_columns == 0:
|
||||
@@ -257,11 +316,14 @@ def update_button_positions(event=None):
|
||||
continue
|
||||
else:
|
||||
DOWNSHIFT = 1 # FIXME make real navbar
|
||||
button.grid(row=(i // num_columns)+ DOWNSHIFT, column=i % num_columns, sticky="we")
|
||||
RIGHTSHIFT = 1 # first column for loading stuff
|
||||
button.grid(row=(i // num_columns)+ DOWNSHIFT, column=i % num_columns + RIGHTSHIFT, sticky="we")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# run updater #
|
||||
|
||||
pgw = pgwrapper.ProgressBarWrapper()
|
||||
pgw.new(app)
|
||||
|
||||
@@ -272,7 +334,6 @@ if __name__ == "__main__":
|
||||
|
||||
if not os.path.isfile(CONFIG_FILE):
|
||||
get_config_inputs()
|
||||
print("wtf")
|
||||
|
||||
# load config #
|
||||
with open(CONFIG_FILE) as f:
|
||||
@@ -284,6 +345,8 @@ if __name__ == "__main__":
|
||||
password = config_loaded.get("Password:")
|
||||
install_dir = config_loaded["Install dir:"]
|
||||
backend_type = config_loaded["Select option:"]
|
||||
hide_above_age = config_loaded.get("hide_above_age") or 100
|
||||
|
||||
|
||||
# fix abs path if not set #
|
||||
if os.path.abspath(install_dir):
|
||||
@@ -293,6 +356,14 @@ if __name__ == "__main__":
|
||||
if backend_type == "FTP/FTPS":
|
||||
remote_root_dir = "/" + config_loaded["Server/Path:"].split("://")[1].split("/", 1)[1]
|
||||
server = config_loaded["Server/Path:"][:-len(remote_root_dir)]
|
||||
elif backend_type == "HTTP/HTTPS":
|
||||
server = config_loaded["Server/Path:"]
|
||||
remote_root_dir = None
|
||||
if not any(server.startswith(s) for s in ["http://", "https://"]):
|
||||
server = "http://" + server
|
||||
#if not ":" in server.split("://")[1]:
|
||||
# server = server + ":5000"
|
||||
print(server)
|
||||
elif backend_type == "Local Filesystem":
|
||||
remote_root_dir = config_loaded["Server/Path:"]
|
||||
server = None
|
||||
@@ -303,7 +374,11 @@ if __name__ == "__main__":
|
||||
print(user, password, install_dir, remote_root_dir, server, config_loaded["Server/Path:"])
|
||||
|
||||
# add db backend #
|
||||
if backend_type == "FTP/FTPS":
|
||||
if backend_type == "HTTP/HTTPS":
|
||||
db = data_backend.HTTP(user, password, install_dir,
|
||||
remote_root_dir="./", server=server, progress_bar_wrapper=pgw,
|
||||
tkinter_root=app, hide_above_age=hide_above_age)
|
||||
elif backend_type == "FTP/FTPS":
|
||||
db = data_backend.FTP(user, password, install_dir, server=server,
|
||||
remote_root_dir=remote_root_dir, progress_bar_wrapper=pgw, tkinter_root=app)
|
||||
elif backend_type == "Local Filesystem":
|
||||
@@ -316,5 +391,9 @@ if __name__ == "__main__":
|
||||
app.update()
|
||||
|
||||
# fill and run app #
|
||||
try:
|
||||
load_main() # TODO add button to reopen config # TODO add button to purge cache/purge cache window # TODO show game size on remote
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
app.withdraw()
|
||||
tkinter.messagebox.showerror("There was a connection problem", str(e))
|
||||
app.mainloop()
|
||||
|
||||
@@ -2,7 +2,8 @@ import PIL
|
||||
import tkinter
|
||||
import customtkinter
|
||||
import imagetools
|
||||
|
||||
import os
|
||||
import statekeeper
|
||||
|
||||
def show_large_picture(app, path):
|
||||
'''Show a full-window version of the clicked picture'''
|
||||
@@ -21,11 +22,14 @@ def show_large_picture(app, path):
|
||||
large_image = customtkinter.CTkButton(app, text="", image=img, width=x-2*30, height=y-2*30,
|
||||
fg_color="transparent", hover_color="black", corner_radius=0, border_width=0, border_spacing=0,
|
||||
command=lambda: large_image.destroy())
|
||||
|
||||
large_image.place(x=30, y=30)
|
||||
|
||||
def create_details_page(app, software, backswitch_function):
|
||||
def create_details_page(app, software, backswitch_function, infowidget_window):
|
||||
'''Create the details page for a software and return its elements for later destruction'''
|
||||
|
||||
infowidget_window.root.grid(row=1, column=0, sticky="n")
|
||||
|
||||
elements = []
|
||||
|
||||
if software.get_thumbnail():
|
||||
@@ -38,23 +42,32 @@ def create_details_page(app, software, backswitch_function):
|
||||
|
||||
img = PIL.ImageTk.PhotoImage(img)
|
||||
|
||||
# navbar & progress bar #
|
||||
# navbar #
|
||||
navbar = customtkinter.CTkFrame(app, fg_color="transparent")
|
||||
navbar.grid(column=0, row=0, padx=10, pady=5, sticky="ew")
|
||||
back_button = customtkinter.CTkButton(navbar, text="Back",
|
||||
command=backswitch_function)
|
||||
navbar.grid(column=1, row=0, padx=10, pady=5, sticky="ew")
|
||||
|
||||
# back button
|
||||
back_button = customtkinter.CTkButton(navbar, text="Back", command=backswitch_function)
|
||||
back_button.pack(anchor="nw", side="left")
|
||||
progress_bar = software.progress_bar_wrapper.new(navbar)
|
||||
progress_bar.pack(anchor="nw", side="left", padx=20, pady=5)
|
||||
|
||||
# progress bar #
|
||||
#progress_bar = software.progress_bar_wrapper.new(navbar)
|
||||
#progress_bar.pack(anchor="nw", side="left", padx=20, pady=5)
|
||||
|
||||
# progress bar text #
|
||||
#progress_text = software.progress_bar_wrapper.new_text(navbar)
|
||||
#progress_text.pack(anchor="nw", side="left", padx=20, pady=5)
|
||||
|
||||
elements.append(navbar)
|
||||
elements.append(back_button)
|
||||
elements.append(progress_bar)
|
||||
#elements.append(progress_bar)
|
||||
#elements.append(progress_text)
|
||||
|
||||
# thumbnail image #
|
||||
thumbnail_image = customtkinter.CTkButton(app, text="", image=img, width=500, height=700,
|
||||
fg_color="transparent", hover_color="black", corner_radius=0,
|
||||
command=lambda path=path: show_large_picture(app, path))
|
||||
thumbnail_image.grid(column=0, row=1, padx=10)
|
||||
thumbnail_image.grid(column=1, row=1, padx=10)
|
||||
elements.append(thumbnail_image)
|
||||
|
||||
# fonts #
|
||||
@@ -63,7 +76,7 @@ def create_details_page(app, software, backswitch_function):
|
||||
|
||||
# info box #
|
||||
info_frame = customtkinter.CTkFrame(app, width=500)
|
||||
info_frame.grid(column=1, row=1, sticky="nswe", padx=10)
|
||||
info_frame.grid(column=2, row=1, sticky="nswe", padx=10)
|
||||
elements.append(info_frame)
|
||||
|
||||
# title #
|
||||
@@ -118,10 +131,10 @@ def create_details_page(app, software, backswitch_function):
|
||||
remove_text = "Remove Manually"
|
||||
else:
|
||||
install_text = "Install"
|
||||
remove_text = "Remove"
|
||||
remove_text = "Remove (not implemented)" # FIXME: change text once implemented
|
||||
|
||||
install_button = customtkinter.CTkButton(button_frame, text=install_text,
|
||||
command=lambda: software.install())
|
||||
command=lambda: software.install_async())
|
||||
|
||||
# add remove button #
|
||||
remove_button = customtkinter.CTkButton(button_frame, text=remove_text,
|
||||
@@ -131,6 +144,9 @@ def create_details_page(app, software, backswitch_function):
|
||||
remove_button.configure(state=tkinter.DISABLED)
|
||||
remove_button.configure(fg_color="gray")
|
||||
|
||||
# FIXME: disable remove button until implemented
|
||||
remove_button.configure(state=tkinter.DISABLED)
|
||||
|
||||
remove_button.pack(padx=10, pady=15, anchor="sw", side="left")
|
||||
|
||||
|
||||
@@ -138,9 +154,13 @@ def create_details_page(app, software, backswitch_function):
|
||||
run_button = customtkinter.CTkButton(button_frame, text="Run",
|
||||
command=lambda: software.run())
|
||||
run_button.pack(padx=10, pady=15, anchor="sw", side="left")
|
||||
software.run_button = run_button
|
||||
|
||||
# install button #
|
||||
if not software.run_exe:
|
||||
print(software.run_exe)
|
||||
print(os.path.join(software.backend.install_dir, software.run_exe or ""))
|
||||
if not software.run_exe or not (os.path.isfile(software.run_exe)
|
||||
or os.path.isfile(os.path.join(software.backend.install_dir, software.title, software.run_exe))):
|
||||
run_button.configure(state=tkinter.DISABLED)
|
||||
run_button.configure(fg_color="gray")
|
||||
|
||||
@@ -154,12 +174,17 @@ def create_details_page(app, software, backswitch_function):
|
||||
# add other pictures #
|
||||
if software.pictures:
|
||||
|
||||
def callback_add_pictures():
|
||||
|
||||
picture_frame = customtkinter.CTkScrollableFrame(info_frame, height=200, width=300, orientation="horizontal", fg_color="transparent")
|
||||
picture_frame.grid(column=0, row=7, sticky="we")
|
||||
|
||||
i = 0
|
||||
print("Software pictures in callback:", software.pictures[1:])
|
||||
for path in software.pictures[1:]:
|
||||
img = PIL.Image.open(path)
|
||||
|
||||
print("Doing picture:", path)
|
||||
img = PIL.Image.open(software.backend.get(path))
|
||||
img = imagetools.smart_resize(img, 180, 180)
|
||||
img = PIL.ImageTk.PhotoImage(img)
|
||||
extra_pic_button = customtkinter.CTkButton(picture_frame, text="", image=img, command=lambda path=path: show_large_picture(app, path),
|
||||
@@ -171,4 +196,6 @@ def create_details_page(app, software, backswitch_function):
|
||||
|
||||
elements.append(picture_frame)
|
||||
|
||||
statekeeper.add_to_task_queue(callback_add_pictures)
|
||||
|
||||
return elements
|
||||
|
||||
277
data_backend.py
277
data_backend.py
@@ -6,19 +6,9 @@ import ftplib
|
||||
import tqdm
|
||||
import ssl
|
||||
import concurrent.futures
|
||||
import statekeeper
|
||||
import requests
|
||||
|
||||
class SESSION_REUSE_FTP_TLS(ftplib.FTP_TLS):
|
||||
"""Explicit FTPS, with shared TLS session"""
|
||||
|
||||
def ntransfercmd(self, cmd, rest=None):
|
||||
|
||||
conn, size = ftplib.FTP.ntransfercmd(self, cmd, rest)
|
||||
if self._prot_p:
|
||||
conn = self.context.wrap_socket(
|
||||
conn,
|
||||
server_hostname=self.host,
|
||||
session=self.sock.session) # this is the fix
|
||||
return conn, size
|
||||
|
||||
class DataBackend:
|
||||
|
||||
@@ -26,17 +16,18 @@ class DataBackend:
|
||||
os.makedirs(cache_dir, exist_ok=True)
|
||||
|
||||
def __init__(self, user, password, install_dir, server=None, remote_root_dir=None,
|
||||
progress_bar_wrapper=None, tkinter_root=None):
|
||||
progress_bar_wrapper=None, tkinter_root=None, hide_above_age=100):
|
||||
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.auth = (self.user, self.password)
|
||||
self.remote_root_dir = remote_root_dir
|
||||
self.server = server
|
||||
self.install_dir = install_dir
|
||||
self.progress_bar_wrapper = progress_bar_wrapper
|
||||
self.root = tkinter_root
|
||||
self.cache_dir = "./cache/"
|
||||
self.ftp = None # ftp connection object
|
||||
self.hide_above_age = hide_above_age
|
||||
|
||||
def get(self, path, return_content=False):
|
||||
'''Return the contents of this path'''
|
||||
@@ -103,69 +94,45 @@ class LocalFS(DataBackend):
|
||||
|
||||
return list(filter(lambda x: not x.invalid, meta_info_list))
|
||||
|
||||
class FTP(DataBackend):
|
||||
class HTTP(DataBackend):
|
||||
|
||||
paths_listed = {}
|
||||
REMOTE_PATH = "/get-path"
|
||||
|
||||
def _connect(self, individual_connection=False):
|
||||
def _get_url(self):
|
||||
#print(self.server + HTTP.REMOTE_PATH)
|
||||
return self.server + HTTP.REMOTE_PATH
|
||||
|
||||
if self.ftp and not individual_connection:
|
||||
try:
|
||||
self.ftp.voidcmd("NOOP")
|
||||
return self.ftp
|
||||
except ssl.SSLError:
|
||||
pass # reconnect
|
||||
def get_local_target(self, path):
|
||||
|
||||
if self.server.startswith("ftp://"):
|
||||
tls = False
|
||||
elif self.server.startswith("ftps://"):
|
||||
tls = True
|
||||
else:
|
||||
raise ValueError("FTP Server must start with ftp:// or ftps://")
|
||||
# prepend root dir if not given #
|
||||
fullpath = path
|
||||
if self.remote_root_dir and not path.startswith(self.remote_root_dir):
|
||||
fullpath = os.path.join(self.remote_root_dir, path)
|
||||
|
||||
# build connection parameters #
|
||||
server = self.server.split("://")[1]
|
||||
port = None
|
||||
try:
|
||||
server = server.split(":")[0]
|
||||
except (IndexError, ValueError):
|
||||
port = 0
|
||||
fullpath = fullpath.replace("\\", "/")
|
||||
local_file = os.path.join(self.cache_dir, fullpath)
|
||||
print("Local Target is", local_file)
|
||||
return local_file
|
||||
|
||||
# try extract server #
|
||||
try:
|
||||
server = server.split(":")[0]
|
||||
except (IndexError, ValueError):
|
||||
server = self.server
|
||||
def local_delete_cache_file(self, path, cache_dir=None):
|
||||
'''Delete a local cache file'''
|
||||
|
||||
print("Connecting to:", server, "on port:", port, "ssl =", tls)
|
||||
print("WARNING: removing:", self.get_local_target(path))
|
||||
os.remove(self.get_local_target(path))
|
||||
|
||||
# connect #
|
||||
if not tls:
|
||||
ftp = ftplib.FTP()
|
||||
else:
|
||||
ftp = SESSION_REUSE_FTP_TLS()
|
||||
ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
|
||||
def get(self, path, cache_dir=None, return_content=False, wait=False):
|
||||
|
||||
ftp.connect(server, port=port or 0)
|
||||
print("Getting", path, "cache dir", cache_dir, "return content:", return_content)
|
||||
|
||||
if self.user:
|
||||
ftp.login(self.user, self.password)
|
||||
else:
|
||||
ftp.login()
|
||||
if cache_dir is None:
|
||||
print("Setting cache dir from backend default", "cur:", cache_dir, "new (default):", self.cache_dir)
|
||||
cache_dir = self.cache_dir
|
||||
|
||||
# open a secure session for tls #
|
||||
if tls:
|
||||
ftp.prot_p()
|
||||
|
||||
# cache dir is automatically set #
|
||||
self.cache_dir = None
|
||||
|
||||
if not individual_connection:
|
||||
self.ftp = ftp
|
||||
return ftp
|
||||
|
||||
|
||||
def get(self, path, cache_dir=None, return_content=False, new_connection=False):
|
||||
# fix cache path reuse #
|
||||
if path.startswith(cache_dir):
|
||||
path = path[len(cache_dir):]
|
||||
print("Fixed path to not duble include cache dir, path:", path)
|
||||
|
||||
# check the load cache dir #
|
||||
if cache_dir:
|
||||
@@ -173,129 +140,139 @@ class FTP(DataBackend):
|
||||
elif not cache_dir and not return_content:
|
||||
AssertionError("Need to set either cache_dir or return_content!")
|
||||
|
||||
# prepend root dir if not given #
|
||||
fullpath = path
|
||||
if self.remote_root_dir and not path.startswith(self.remote_root_dir):
|
||||
fullpath = os.path.join(self.remote_root_dir, path)
|
||||
#print(self.remote_root_dir, path, fullpath)
|
||||
fullpath = fullpath.replace("\\", "/")
|
||||
local_file = os.path.join(cache_dir, os.path.basename(path))
|
||||
local_file = self.get_local_target(path)
|
||||
local_dir = os.path.dirname(local_file)
|
||||
|
||||
# print("Cachedir:", cache_dir, os.path.basename(path), local_file)
|
||||
|
||||
if not os.path.isfile(local_file):
|
||||
ftp = self._connect(individual_connection=True)
|
||||
ftp.sendcmd('TYPE I')
|
||||
|
||||
# load the file on remote #
|
||||
if not new_connection:
|
||||
total_size = ftp.size(fullpath)
|
||||
print(total_size)
|
||||
self.progress_bar_wrapper.get_pb()["maximum"] = total_size
|
||||
|
||||
print(local_file, "not in cache, retriving..")
|
||||
with open(local_file, "w") as f:
|
||||
f.write(local_file)
|
||||
with open(local_file, 'wb') as local_file_open, tqdm.tqdm(
|
||||
desc="Downloading",
|
||||
total=total_size,
|
||||
unit='B',
|
||||
unit_scale=True
|
||||
) as cmd_progress_bar:
|
||||
|
||||
# Define a callback function to update the progress bar #
|
||||
def callback(data):
|
||||
local_file_open.write(data)
|
||||
if new_connection: # return if parralell
|
||||
return
|
||||
self.root.update_idletasks() # Update the GUI
|
||||
self.progress_bar_wrapper.get_pb().set(
|
||||
self.progress_bar_wrapper.get_pb().get() + len(data)/total_size)
|
||||
cmd_progress_bar.update(len(data))
|
||||
|
||||
# run with callback #
|
||||
ftp.retrbinary('RETR ' + fullpath, callback)
|
||||
# sanity check and create directory #
|
||||
if not local_dir.startswith(cache_dir):
|
||||
raise AssertionError("Local Dir does not start with cache dir:" + local_dir)
|
||||
else:
|
||||
with open(local_file, 'wb') as fp:
|
||||
ftp.retrbinary('RETR ' + fullpath, fp.write)
|
||||
os.makedirs(local_dir, exist_ok=True)
|
||||
|
||||
if new_connection:
|
||||
ftp.close()
|
||||
print("Requiring:", path)
|
||||
|
||||
if return_content:
|
||||
with open(local_file, encoding="utf-8") as fr:
|
||||
return fr.read()
|
||||
if not os.path.isfile(local_file) or os.stat(local_file).st_size == 0:
|
||||
|
||||
return local_file
|
||||
if return_content or wait:
|
||||
|
||||
def list(self, path, fullpaths=False, new_connection=False):
|
||||
print("Sync Requested")
|
||||
# the content is needed for the UI now and not cached, it's needs to be downloaded synchroniously #
|
||||
# as there cannot be a meaningful UI-draw without it. #
|
||||
# THIS IS THE OLD WAY
|
||||
# r = requests.get(self._get_url(), params={ "path" : path, "as_string": True })
|
||||
|
||||
# prepend root dir if not given #
|
||||
fullpath = path
|
||||
if self.remote_root_dir and not path.startswith(self.remote_root_dir):
|
||||
fullpath = os.path.join(self.remote_root_dir, path)
|
||||
fullpath = fullpath.replace("\\", "/")
|
||||
#print(fullpath)
|
||||
# # cache the download imediatelly #
|
||||
# with open(local_file, encoding="utf-8", mode="w") as f:
|
||||
# f.write(r.text)
|
||||
|
||||
# if not os.path.isdir(fullpath):
|
||||
# return []
|
||||
# this is with streaming
|
||||
chunk_size = 1024 * 1024 * 5 # 5MB
|
||||
r = requests.get(self._get_url(), params={"path": path, "as_string": True}, stream=True, auth=(self.user, self.password))
|
||||
r.raise_for_status()
|
||||
|
||||
if path.endswith(".txt"):
|
||||
TYPE = "w"
|
||||
else:
|
||||
TYPE = "wb"
|
||||
|
||||
with open(local_file, TYPE) as f:
|
||||
count = 0
|
||||
for chunk in r.iter_content(chunk_size=chunk_size, decode_unicode=True):
|
||||
|
||||
print(f"Doing chunk.. {chunk_size*count}")
|
||||
if chunk:
|
||||
|
||||
try:
|
||||
f.write(chunk)
|
||||
except TypeError as e:
|
||||
print("Cannot write:", chunk, "..to ", path, " because it is the wrong type.", e)
|
||||
raise e
|
||||
f.flush()
|
||||
|
||||
count += 1
|
||||
|
||||
if return_content:
|
||||
print("Content for", path, ":", r.text)
|
||||
return r.text
|
||||
else:
|
||||
return local_file
|
||||
|
||||
else:
|
||||
print("Async Requested for:", local_file)
|
||||
statekeeper.add_to_download_queue(self._get_url(), path, auth=(self.user, self.password))
|
||||
return local_file
|
||||
|
||||
elif return_content:
|
||||
print("Returning Cached file:", local_file)
|
||||
with open(local_file, encoding="utf-8") as fr:
|
||||
return fr.read()
|
||||
else:
|
||||
print("Already present:", local_file)
|
||||
return local_file
|
||||
|
||||
def list(self, path, fullpaths=False):
|
||||
|
||||
fullpath = path
|
||||
if self.remote_root_dir and not path.startswith(self.remote_root_dir):
|
||||
fullpath = os.path.join(self.remote_root_dir, path)
|
||||
fullpath = fullpath.replace("\\", "/")
|
||||
|
||||
# retrieve session cached paths #
|
||||
if fullpath in self.paths_listed:
|
||||
paths = self.paths_listed[fullpath]
|
||||
#print("Retrieved paths from cache:", fullpath, paths)
|
||||
else:
|
||||
ftp = self._connect(individual_connection=new_connection)
|
||||
print("Listing previously unlisted path: {}".format(fullpath))
|
||||
self.paths_listed.update({fullpath: []}) # in case dir does not exit
|
||||
paths = ftp.nlst(fullpath)
|
||||
self.paths_listed.update({fullpath: paths})
|
||||
|
||||
if new_connection: # close individual connections
|
||||
ftp.close()
|
||||
r = requests.get(self._get_url(), params={ "path" : path }, auth=(self.user, self.password))
|
||||
r.raise_for_status()
|
||||
#print(r, r.status_code, r.content)
|
||||
paths = r.json()["contents"]
|
||||
|
||||
if not fullpaths:
|
||||
return paths
|
||||
|
||||
return [ os.path.join(path, filename).replace("\\", "/") for filename in paths ]
|
||||
|
||||
except ftplib.error_perm as e:
|
||||
if "550 No files found" in str(e):
|
||||
print("No files in this directory: {}".format(fullpath))
|
||||
return []
|
||||
elif "550 No such file or directory" in str(e):
|
||||
print("File or dir does not exist: {}".format(fullpath))
|
||||
return []
|
||||
else:
|
||||
raise e
|
||||
return [ os.path.join(path, filename).replace("\\", "/") for filename in paths ]
|
||||
|
||||
def find_all_metadata(self):
|
||||
|
||||
local_meta_file_list = []
|
||||
|
||||
root_elements = self.list(self.remote_root_dir)
|
||||
print("root elements:", root_elements)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()*5) as executor:
|
||||
|
||||
software_dir_contents = list(executor.map(
|
||||
lambda s: self.list(s, fullpaths=True, new_connection=True), root_elements))
|
||||
lambda s: self.list(s, fullpaths=True), root_elements))
|
||||
|
||||
# this caches the paths, done remove it #
|
||||
cache_list = [os.path.join(s, "registry_files") for s in root_elements ]
|
||||
cache_list += [os.path.join(s, "pictures") for s in root_elements ]
|
||||
|
||||
# THIS PRELOAD IMAGES-paths, DO NOT REMOVE IT #
|
||||
picture_contents_async_cache = list(executor.map(
|
||||
lambda s: self.list(s, fullpaths=True, new_connection=True), cache_list))
|
||||
lambda s: self.list(s, fullpaths=True), cache_list))
|
||||
|
||||
for files in software_dir_contents:
|
||||
#print(s)
|
||||
#files = self.list(s, fullpaths=True)
|
||||
print(files)
|
||||
for f in files:
|
||||
if f.endswith("meta.yaml"):
|
||||
meta_file_content = self.get(f, cache_dir="cache", return_content=True)
|
||||
#print(meta_file_content)
|
||||
local_meta_file_list.append(f)
|
||||
|
||||
|
||||
print("local meta:", local_meta_file_list)
|
||||
software_list = None
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()*5) as executor:
|
||||
software_list = executor.map(lambda meta_file: software.Software(meta_file, self, self.progress_bar_wrapper), local_meta_file_list)
|
||||
return list(filter(lambda x: not x.invalid, software_list))
|
||||
|
||||
# evaluate
|
||||
software_list = list(software_list)
|
||||
print("Software List:", software_list)
|
||||
print("Invalid:", list(filter(lambda x: x.invalid, software_list)))
|
||||
print("Valid:", list(filter(lambda x: not x.invalid, software_list)))
|
||||
|
||||
# filter valid #
|
||||
results_valid = list(filter(lambda x: not x.invalid, software_list))
|
||||
|
||||
# filer age #
|
||||
print("Age limit set to", self.hide_above_age, "games have", [x.age_limit for x in software_list])
|
||||
results_with_age = list(filter(lambda x: x.age_limit <= self.hide_above_age, results_valid))
|
||||
|
||||
return results_with_age
|
||||
|
||||
48
db.py
Normal file
48
db.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from sqlalchemy import Column, String, Integer, Boolean, create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, scoped_session
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
import logging
|
||||
logging.basicConfig()
|
||||
logging.getLogger('sqlalchemy').setLevel(logging.ERROR)
|
||||
class Download(Base):
|
||||
|
||||
__tablename__ = 'files'
|
||||
|
||||
path = Column(String, primary_key=True)
|
||||
local_path = Column(String)
|
||||
url = Column(String)
|
||||
size = Column(Integer)
|
||||
count = Column(Integer) # extraction only
|
||||
type = Column(String)
|
||||
finished = Column(Boolean)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.path == other.path
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.path)
|
||||
|
||||
class Database:
|
||||
|
||||
def __init__(self, db_url="sqlite:///database.db"):
|
||||
|
||||
self.engine = create_engine(db_url, echo=False)
|
||||
self.session_factory = sessionmaker(bind=self.engine)
|
||||
self.Session = scoped_session(self.session_factory) # Thread-safe sessions
|
||||
|
||||
# Automatically create tables
|
||||
Base.metadata.create_all(self.engine)
|
||||
|
||||
def session(self):
|
||||
"""Returns a new session (or an existing one if in the same thread)."""
|
||||
return self.Session()
|
||||
|
||||
def close_session(self):
|
||||
"""Closes the current session."""
|
||||
self.Session.remove()
|
||||
|
||||
# Singleton instance of Database
|
||||
db = Database()
|
||||
146
infowidget.py
Normal file
146
infowidget.py
Normal file
@@ -0,0 +1,146 @@
|
||||
# in the background queue write filename in sqlite db
|
||||
# get filename for sqlitedb
|
||||
# get the size with info=1 from server
|
||||
# only display for size>10M
|
||||
# make a list of pregressbars
|
||||
|
||||
# start background thread with main list and main widget
|
||||
# update list and widget
|
||||
|
||||
# update list and widget
|
||||
import customtkinter as ctk
|
||||
from tkinter import ttk
|
||||
import threading
|
||||
import random
|
||||
import time
|
||||
import string
|
||||
import statekeeper
|
||||
import os
|
||||
|
||||
class ProgressBarApp:
|
||||
def __init__(self, parent, data_backend):
|
||||
|
||||
self.data_backend = data_backend
|
||||
self.parent = parent
|
||||
self.root = ctk.CTkFrame(parent)
|
||||
#self.root.title("Dynamic Progress Bars")
|
||||
|
||||
self.delete_all_button = ctk.CTkLabel(self.root, text="Downloads")
|
||||
self.delete_all_button.pack(pady=5)
|
||||
|
||||
self.delete_all_button = ctk.CTkButton(self.root, text="Delete All Finished", command=self.delete_all_finished, state=ctk.DISABLED)
|
||||
self.delete_all_button.pack(pady=5)
|
||||
|
||||
self.frame = ctk.CTkFrame(self.root)
|
||||
self.frame.pack(pady=10)
|
||||
|
||||
self.progress_bars = [] # Store tuples of (progressbar, frame, duration, delete_button)
|
||||
|
||||
self.running = True
|
||||
self.root.after(0, self.start_tracking_progress_bars)
|
||||
|
||||
def start_tracking_progress_bars(self):
|
||||
|
||||
self.already_tracked = set()
|
||||
self.check_for_new_progress_bars()
|
||||
|
||||
def check_for_new_progress_bars(self):
|
||||
|
||||
downloads = set(statekeeper.get_download())
|
||||
new = downloads - self.already_tracked
|
||||
self.already_tracked |= downloads
|
||||
|
||||
for element in new:
|
||||
frame = ctk.CTkFrame(self.frame)
|
||||
frame.pack(fill=ctk.X, pady=2)
|
||||
|
||||
progress = ttk.Progressbar(frame, length=200, mode='determinate')
|
||||
progress.pack(side=ctk.LEFT, padx=5)
|
||||
|
||||
delete_button = ctk.CTkButton(frame, text="Delete", command=lambda f=frame: self.delete_progress(f), state=ctk.DISABLED)
|
||||
delete_button.pack(side=ctk.LEFT, padx=5)
|
||||
|
||||
label = ctk.CTkLabel(frame, text=os.path.basename(element.path))
|
||||
label.pack(side=ctk.LEFT, padx=5)
|
||||
|
||||
self.progress_bars.insert(0, (progress, frame, delete_button)) # Insert at the top
|
||||
frame.pack(fill=ctk.X, pady=2, before=self.frame.winfo_children()[-1] if self.frame.winfo_children() else None)
|
||||
|
||||
print("Starting tracker for", element.path)
|
||||
threading.Thread(target=self.fill_progress, args=(progress, element.path, frame, delete_button), daemon=True).start()
|
||||
|
||||
# Schedule the next check in 2 seconds
|
||||
if self.running:
|
||||
self.root.after(2000, self.check_for_new_progress_bars)
|
||||
|
||||
def fill_progress(self, progress, path, frame, delete_button):
|
||||
|
||||
fail_count = 0
|
||||
same_size_count = 0
|
||||
prev_precent = 0
|
||||
while True:
|
||||
|
||||
print("Checking download progress..")
|
||||
|
||||
if not progress.winfo_exists(): # Check if progress bar still exists
|
||||
return
|
||||
|
||||
try:
|
||||
percent_filled = statekeeper.get_percent_filled(path, self.data_backend.auth)
|
||||
except OSError as e:
|
||||
fail_count += 1
|
||||
if fail_count > 6:
|
||||
raise e
|
||||
else:
|
||||
time.sleep(1)
|
||||
continue
|
||||
|
||||
print("Percent filled:", percent_filled, path)
|
||||
if percent_filled >= 99.9:
|
||||
self.root.after(0, progress.configure, { "value" : 100 })
|
||||
print("Finished", path)
|
||||
break
|
||||
else:
|
||||
self.root.after(0, progress.configure, { "value" : percent_filled })
|
||||
time.sleep(0.5)
|
||||
|
||||
# check for stuck downloads #
|
||||
print("same size count", same_size_count)
|
||||
if prev_precent == percent_filled:
|
||||
same_size_count += 1
|
||||
else:
|
||||
same_size_count = 0
|
||||
if same_size_count > 100:
|
||||
self.root.after(0, delete_button.configure, {"state": ctk.NORMAL, "text": "Failed - Delete file manually!"})
|
||||
self.progress_bars.append((progress, frame, path, delete_button))
|
||||
self.update_delete_all_button()
|
||||
statekeeper.log_end_download(path)
|
||||
self.data_backend.local_delete_cache_file(path)
|
||||
break
|
||||
prev_precent = percent_filled
|
||||
|
||||
# handle finished download #
|
||||
self.root.after(0, delete_button.configure, {"state": ctk.NORMAL})
|
||||
self.progress_bars.append((progress, frame, path, delete_button))
|
||||
self.update_delete_all_button()
|
||||
|
||||
def delete_progress(self, frame):
|
||||
frame.destroy()
|
||||
self.progress_bars = [(p, f, d) for p, f, d in self.progress_bars if f != frame]
|
||||
self.update_delete_all_button()
|
||||
|
||||
def delete_all_finished(self):
|
||||
for _, frame, _, _ in self.progress_bars:
|
||||
frame.destroy()
|
||||
self.progress_bars.clear()
|
||||
self.update_delete_all_button()
|
||||
|
||||
def update_delete_all_button(self):
|
||||
if self.progress_bars:
|
||||
self.delete_all_button.configure(state=ctk.NORMAL)
|
||||
else:
|
||||
self.delete_all_button.configure(state=ctk.DISABLED)
|
||||
|
||||
def on_close(self):
|
||||
self.running = False
|
||||
self.root.destroy()
|
||||
@@ -11,6 +11,7 @@ def render_path(path, install_location, game_directory,):
|
||||
result_path = path[:-len(".j2")]
|
||||
|
||||
# prepare template #
|
||||
print("JINJA-> cwd: ", os.getcwd(), "path:", path)
|
||||
input_content = ""
|
||||
with open(path, encoding="utf-16") as f:
|
||||
input_content = f.read()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
# windows imports #
|
||||
if os.name == "nt":
|
||||
@@ -16,7 +17,7 @@ def unpack_software(software_cache_path, target_path):
|
||||
'''Unpack a downloaded software to the target location'''
|
||||
pass
|
||||
|
||||
def install_registry_file(registry_file, game_path=None):
|
||||
def install_registry_file(registry_file):
|
||||
'''Install a given registy file'''
|
||||
|
||||
# test path:
|
||||
@@ -26,13 +27,6 @@ def install_registry_file(registry_file, game_path=None):
|
||||
p = subprocess.Popen(["wine64", "start", "regedit", registry_file],
|
||||
subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||
print("Running regedit for wine..")
|
||||
else:
|
||||
# windows sucky sucky #
|
||||
if not os.path.isabs(registry_file):
|
||||
registry_file = os.path.join(os.getcwd(), registry_file)
|
||||
|
||||
p = subprocess.Popen(["python", "regedit.py", registry_file],
|
||||
subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||
|
||||
print(p.communicate())
|
||||
|
||||
@@ -52,6 +46,15 @@ def resolve_lnk(lnk_file_path):
|
||||
def run_exe(path, synchronous=False):
|
||||
'''Launches a given software'''
|
||||
|
||||
if type(path) == str:
|
||||
paths = [path]
|
||||
else:
|
||||
paths = path
|
||||
|
||||
# sanity check path is list #
|
||||
if not type(paths) == list:
|
||||
raise AssertionError("ERROR: run_exe could not build a list of paths")
|
||||
|
||||
if os.name != "nt":
|
||||
if ".lnk" in path:
|
||||
subprocess.Popen(["wine64", "start", path])
|
||||
@@ -62,18 +65,23 @@ def run_exe(path, synchronous=False):
|
||||
if synchronous:
|
||||
raise NotImplementedError("SYNC not yet implemented")
|
||||
|
||||
if path.endswith(".lnk"):
|
||||
path = resolve_lnk(path)
|
||||
paths = [resolve_lnk(p) if p.endswith(".lnk") else p for p in paths]
|
||||
|
||||
print("Executing:", path)
|
||||
print("Executing:", paths)
|
||||
|
||||
try:
|
||||
subprocess.Popen(path, cwd=os.path.dirname(path))
|
||||
if paths[0].endswith(".reg"):
|
||||
raise OSError("WinError 740")
|
||||
subprocess.Popen(path, cwd=os.path.dirname(paths[0])) # TODO fix this BS
|
||||
except OSError as e:
|
||||
if "WinError 740" in str(e):
|
||||
p = subprocess.Popen(["python", "adminrun.py", path],
|
||||
p = subprocess.Popen(["powershell", "-ExecutionPolicy", "Bypass", "-File",
|
||||
"windows_run_as_admin.ps1", json.dumps(paths)],
|
||||
subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||
try:
|
||||
print(p.communicate())
|
||||
except UnicodeDecodeError as e:
|
||||
print("WARNING: cannot show you ERROR from exe because output contained illegal characters. This maybe because your refused the admin prompt.")
|
||||
else:
|
||||
raise e
|
||||
|
||||
|
||||
26
pgwrapper.py
26
pgwrapper.py
@@ -6,16 +6,42 @@ class ProgressBarWrapper:
|
||||
in the DataBackend and Software Objects'''
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.progress_bar = None
|
||||
self.progress_text = None
|
||||
self.tk_parent = None
|
||||
|
||||
def update(self):
|
||||
|
||||
if self.tk_parent:
|
||||
self.tk_parent.update_idletasks()
|
||||
|
||||
def new(self, tk_parent):
|
||||
|
||||
self.tk_parent = tk_parent
|
||||
self.progress_bar = customtkinter.CTkProgressBar(tk_parent, height=20, width=200)
|
||||
self.progress_bar["maximum"] = 10000
|
||||
self.progress_bar.set(0)
|
||||
|
||||
return self.progress_bar
|
||||
|
||||
def new_text(self, tk_parent):
|
||||
|
||||
self.tk_parent = tk_parent
|
||||
self.progress_text = customtkinter.CTkLabel(tk_parent, height=20, width=130, text="")
|
||||
return self.progress_text
|
||||
|
||||
def get_pb(self):
|
||||
|
||||
if self.progress_bar:
|
||||
return self.progress_bar
|
||||
else:
|
||||
raise AssertionError("No progress bar in this wrapper created")
|
||||
|
||||
def set_text(self, text):
|
||||
|
||||
if self.progress_text:
|
||||
self.progress_text.configure(text=text)
|
||||
else:
|
||||
pass
|
||||
#FIXME raise AssertionError("No progress text in this wrapper created")
|
||||
|
||||
14
regedit.py
14
regedit.py
@@ -1,14 +0,0 @@
|
||||
from pyuac.main_decorator import main_requires_admin
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
@main_requires_admin(return_output=True)
|
||||
def main(registry_file):
|
||||
p = subprocess.Popen(["regedit", registry_file],
|
||||
subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||
stdout, stderr = p.communicate()
|
||||
|
||||
if __name__ == '__main__':
|
||||
registry_file = sys.argv[-1]
|
||||
rv = main(registry_file)
|
||||
print(rv)
|
||||
@@ -1,7 +1,9 @@
|
||||
pyuac
|
||||
Pillow
|
||||
Pillow>=10.0.3
|
||||
customtkinter
|
||||
tqdm
|
||||
Jinja2
|
||||
pyyaml
|
||||
pywin32==<version>; platform_system=="Windows"
|
||||
pywin32==306; platform_system=="Windows"
|
||||
requests
|
||||
sqlalchemy
|
||||
17
server/Dockerfile
Normal file
17
server/Dockerfile
Normal file
@@ -0,0 +1,17 @@
|
||||
FROM alpine
|
||||
|
||||
RUN apk add --no-cache py3-pip
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN python3 -m pip install --no-cache-dir --break-system-packages waitress
|
||||
|
||||
COPY req.txt .
|
||||
RUN python3 -m pip install --no-cache-dir --break-system-packages -r req.txt
|
||||
|
||||
COPY ./ .
|
||||
|
||||
EXPOSE 5000/tcp
|
||||
|
||||
ENTRYPOINT ["waitress-serve"]
|
||||
CMD ["--host", "0.0.0.0", "--port", "5000", "--call", "app:createApp"]
|
||||
5
server/app.py
Normal file
5
server/app.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import main as server
|
||||
def createApp(envivorment=None, start_response=None):
|
||||
with server.app.app_context():
|
||||
server.create_app()
|
||||
return server.app
|
||||
69
server/main.py
Normal file
69
server/main.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from flask import Flask, request, jsonify, send_file, abort
|
||||
import os
|
||||
import sys
|
||||
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Base directory constraint
|
||||
BASE_DIR = os.path.abspath("./data")
|
||||
|
||||
@app.route('/get-path', methods=['GET'])
|
||||
def get_path():
|
||||
|
||||
# Get the "path" and "info" arguments from the URL
|
||||
path = request.args.get('path')
|
||||
|
||||
# replace windows paths
|
||||
path = path.replace("\\", "/")
|
||||
if path.startswith("/"):
|
||||
path = path[1:]
|
||||
|
||||
print("path", path, file=sys.stderr)
|
||||
|
||||
info = request.args.get('info')
|
||||
|
||||
if not path:
|
||||
return jsonify({"error": "Missing 'path' parameter."}), 400
|
||||
|
||||
# Ensure the path is secure and resolve it within the BASE_DIR
|
||||
#secure_path = secure_filename(path)
|
||||
full_path = os.path.abspath(os.path.join(BASE_DIR, path))
|
||||
|
||||
if not full_path.startswith(BASE_DIR):
|
||||
return jsonify({"error": "Access to the specified path is not allowed."}), 403
|
||||
|
||||
print(full_path, file=sys.stderr)
|
||||
|
||||
# Check if the path exists
|
||||
if not os.path.exists(full_path):
|
||||
print("missing file", file=sys.stderr)
|
||||
return jsonify({"contents": list()})
|
||||
|
||||
# If the path is a directory, return a JSON list of its contents
|
||||
if os.path.isdir(full_path):
|
||||
contents = filter(lambda x: not x.startswith("."), os.listdir(full_path))
|
||||
return jsonify({"contents": list(contents)})
|
||||
|
||||
# If the path is a file
|
||||
if os.path.isfile(full_path):
|
||||
if info == '1':
|
||||
# Return the file size if 'info=1' is specified
|
||||
file_size = os.path.getsize(full_path)
|
||||
return jsonify({"size": file_size})
|
||||
else:
|
||||
# Return the file as a download
|
||||
try:
|
||||
return send_file(full_path, as_attachment=True)
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
# If the path is neither a file nor a directory, return an error
|
||||
return jsonify({"error": "Invalid path type."}), 400
|
||||
|
||||
def create_app():
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
1
server/req.txt
Normal file
1
server/req.txt
Normal file
@@ -0,0 +1 @@
|
||||
flask
|
||||
95
software.py
95
software.py
@@ -1,4 +1,5 @@
|
||||
import yaml
|
||||
import tkinter
|
||||
import os
|
||||
import localaction
|
||||
import zipfile
|
||||
@@ -7,6 +8,11 @@ import pathlib
|
||||
import tqdm
|
||||
import webbrowser
|
||||
import jinja_helper
|
||||
import threading
|
||||
import sys
|
||||
import tkinter
|
||||
import statekeeper
|
||||
from tkinter import messagebox
|
||||
|
||||
class Software:
|
||||
|
||||
@@ -15,6 +21,7 @@ class Software:
|
||||
self.meta_file = meta_file
|
||||
self.directory = os.path.dirname(meta_file)
|
||||
self.backend = backend
|
||||
self.run_button = None
|
||||
print("Software Directory:", self.directory)
|
||||
|
||||
self.cache_dir = backend.cache_dir or os.path.join("cache", self.directory.lstrip("/").lstrip("\\"))
|
||||
@@ -24,13 +31,19 @@ class Software:
|
||||
self.invalid = False
|
||||
self._load_from_yaml()
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
raise e
|
||||
self.invalid = True
|
||||
|
||||
self.progress_bar_wrapper = progress_bar_wrapper
|
||||
# if not progress_bar_wrapper:
|
||||
# raise AssertionError()
|
||||
|
||||
# self.progress_bar_wrapper = progress_bar_wrapper
|
||||
|
||||
def _load_from_yaml(self):
|
||||
|
||||
content = self.backend.get(self.meta_file, self.cache_dir, return_content=True, new_connection=True)
|
||||
content = self.backend.get(self.meta_file, self.cache_dir, return_content=True)
|
||||
# print("Meta-Content:", content)
|
||||
|
||||
meta = yaml.safe_load(content)
|
||||
if not meta:
|
||||
@@ -45,11 +58,18 @@ class Software:
|
||||
self.extra_files = meta.get("extra_files")
|
||||
self.run_exe = meta.get("run_exe")
|
||||
self.installer = meta.get("installer")
|
||||
self.installer_no_admin = meta.get("installer_no_admin")
|
||||
self.age_limit = meta.get("age_limit") or 20
|
||||
|
||||
self.pictures = [ self.backend.get(pp, self.cache_dir, new_connection=True) for pp in
|
||||
self.backend.list(os.path.join(self.directory, "pictures"), fullpaths=True, new_connection=True) ]
|
||||
self.pictures = [ self.backend.get(pp, self.cache_dir) for pp in
|
||||
self.backend.list(os.path.join(self.directory, "pictures"), fullpaths=True) ]
|
||||
|
||||
self.reg_files = self.backend.list(os.path.join(self.directory, "registry_files"), fullpaths=True, new_connection=True)
|
||||
if any([x is None for x in self.pictures]):
|
||||
raise AssertionError("None Entries in self.pictures: " + str(self.pictures))
|
||||
|
||||
self.reg_files = self.backend.list(os.path.join(self.directory, "registry_files"), fullpaths=True)
|
||||
|
||||
print("Finished Init for", self.title)
|
||||
|
||||
|
||||
def get_thumbnail(self):
|
||||
@@ -64,27 +84,51 @@ class Software:
|
||||
'''Extract a cached, downloaded zip to the target location'''
|
||||
|
||||
software_path = os.path.join(target, self.title)
|
||||
|
||||
if os.path.isdir(software_path):
|
||||
return # TODO better skip
|
||||
overwrite = messagebox.askyesno(
|
||||
"Overwrite Existing Directory",
|
||||
f"The directory '{software_path}' already exists.\nDo you want to overwrite it?"
|
||||
)
|
||||
if not overwrite:
|
||||
print("Skipping install as instructed by user...")
|
||||
return
|
||||
|
||||
os.makedirs(software_path, exist_ok=True)
|
||||
|
||||
# beginn progress tracking #
|
||||
with zipfile.ZipFile(cache_src, 'r') as zip_ref:
|
||||
|
||||
statekeeper.log_begin_download(local_path=cache_src, path=cache_src, url=None, type="extraction", start_size=len(zip_ref.infolist()))
|
||||
total_count = zip_ref.infolist()
|
||||
count = 0
|
||||
for member in tqdm.tqdm(total_count, desc='Extracting '):
|
||||
try:
|
||||
zip_ref.extract(member, software_path)
|
||||
count += 1
|
||||
self.progress_bar_wrapper.get_pb().set(count/len(total_count))
|
||||
self.progress_bar_wrapper.get_pb().update_idletasks()
|
||||
|
||||
# update progress #
|
||||
statekeeper.set_extraction_status(cache_src, count)
|
||||
|
||||
|
||||
except zipfile.error as e:
|
||||
print(e)
|
||||
pass # TODO ???
|
||||
#zip_ref.extractall(software_path)
|
||||
|
||||
# finish extraction tracking #
|
||||
statekeeper.log_end_download(cache_src, type="extraction")
|
||||
|
||||
def install_async(self):
|
||||
|
||||
thread = threading.Thread(target=self.install)
|
||||
thread.start()
|
||||
|
||||
def install(self):
|
||||
'''Install this software from the backend'''
|
||||
|
||||
# things to execute #
|
||||
admin_run_list = []
|
||||
|
||||
print("Installing:", self.title, self.directory)
|
||||
|
||||
# handle link-only software #
|
||||
@@ -92,6 +136,8 @@ class Software:
|
||||
webbrowser.open(self.link_only)
|
||||
return
|
||||
|
||||
#self.progress_bar_wrapper.set_text(text="Please wait..")
|
||||
#self.progress_bar_wrapper.tk_parent.update_idletasks()
|
||||
path = os.path.join(self.directory, "main_dir")
|
||||
|
||||
try:
|
||||
@@ -99,25 +145,35 @@ class Software:
|
||||
except IndexError:
|
||||
print("No main_dir:", path)
|
||||
raise AssertionError("No main_dir for this software")
|
||||
local_file = self.backend.get(remote_file, self.cache_dir)
|
||||
|
||||
statekeeper.log_begin_download(remote_file, self.backend.get_local_target(remote_file), self.backend._get_url())
|
||||
local_file = self.backend.get(remote_file, self.cache_dir, wait=True)
|
||||
statekeeper.log_end_download(remote_file)
|
||||
|
||||
print("Deciding on installer...")
|
||||
|
||||
# execute or unpack #
|
||||
if local_file.endswith(".exe"):
|
||||
print("Target is an executable.. running as installer.")
|
||||
if os.name != "nt" and not os.path.isabs(local_file):
|
||||
# need abs path for wine #
|
||||
local_file = os.path.join(os.getcwd(), local_file)
|
||||
localaction.run_exe(local_file)
|
||||
elif local_file.endswith(".zip"):
|
||||
print("Target is a zip.. unpacking first.")
|
||||
self._extract_to_target(local_file, self.backend.install_dir)
|
||||
|
||||
# download & install registry files #
|
||||
for rf in self.reg_files:
|
||||
path = self.backend.get(rf, cache_dir=self.cache_dir)
|
||||
path = self.backend.get(rf, cache_dir=self.cache_dir, wait=True)
|
||||
if path.endswith(".j2"):
|
||||
target_install_dir = os.path.join(self.backend.install_dir, self.title)
|
||||
print("Install dir Registry:", target_install_dir)
|
||||
path = jinja_helper.render_path(path, target_install_dir, self.directory)
|
||||
|
||||
if sys.platform == "win32":
|
||||
admin_run_list.append(path)
|
||||
else:
|
||||
localaction.install_registry_file(path)
|
||||
|
||||
# install dependencies #
|
||||
@@ -136,16 +192,31 @@ class Software:
|
||||
|
||||
|
||||
print("Running installer:", installer_path)
|
||||
if not self.installer_no_admin:
|
||||
admin_run_list.append(installer_path)
|
||||
else:
|
||||
localaction.run_exe(installer_path)
|
||||
|
||||
if admin_run_list:
|
||||
print("admin list", admin_run_list)
|
||||
localaction.run_exe(admin_run_list)
|
||||
|
||||
# install gamefiles #
|
||||
if self.extra_files:
|
||||
for src, dest in self.extra_files.items():
|
||||
tmp = self.backend.get(os.path.join(self.directory, "extra_files", src), self.cache_dir)
|
||||
tmp = self.backend.get(os.path.join(self.directory, "extra_files", src), self.cache_dir, wait=True)
|
||||
dest_dir = os.path.expandvars(dest)
|
||||
os.makedirs(dest_dir, exist_ok=True)
|
||||
shutil.copy(tmp, dest_dir)
|
||||
|
||||
#self.progress_bar_wrapper.set_text(text="")
|
||||
try:
|
||||
if self.run_button:
|
||||
self.run_button.configure(state=tkinter.NORMAL)
|
||||
self.run_button.configure(fg_color="green")
|
||||
except tkinter.TclError:
|
||||
print("INFO: No longer in installation view - no button to update")
|
||||
|
||||
def run(self):
|
||||
'''Run the configured exe for this software'''
|
||||
|
||||
|
||||
151
statekeeper.py
Normal file
151
statekeeper.py
Normal file
@@ -0,0 +1,151 @@
|
||||
import requests
|
||||
import os
|
||||
import sqlalchemy
|
||||
import threading
|
||||
from db import db, Download
|
||||
from sqlalchemy import or_, and_
|
||||
|
||||
def _bytes_to_mb(size):
|
||||
return size / (1024*1024)
|
||||
|
||||
def add_to_download_queue(url, path, auth):
|
||||
'''The download is added to the global queue and downloaded eventually'''
|
||||
#_download(url, path)
|
||||
thread = threading.Thread(target=_download, args=(url, path, auth))
|
||||
thread.start()
|
||||
|
||||
def add_to_task_queue(task):
|
||||
'''Add a callback to background execution queue'''
|
||||
#print("Executing tasks", task)
|
||||
thread = threading.Thread(target=task)
|
||||
thread.start()
|
||||
#task()
|
||||
|
||||
def _download(url, path, auth):
|
||||
|
||||
response = requests.get(url + "?path=" + path, stream=True, auth=auth)
|
||||
|
||||
# Check if the request was successful
|
||||
if response.status_code == 200:
|
||||
|
||||
# Save the file locally
|
||||
local_filename = os.path.join("./cache", path)
|
||||
|
||||
with open(local_filename, 'wb') as f:
|
||||
for chunk in response.iter_content(chunk_size=8192): # Download in chunks
|
||||
f.write(chunk)
|
||||
|
||||
print(f"File downloaded successfully as {local_filename}")
|
||||
|
||||
else:
|
||||
|
||||
raise AssertionError("Non-200 Response for:", url, path, response.status_code, response.text)
|
||||
|
||||
def log_begin_download(path, local_path, url, type="download", start_size=-1):
|
||||
|
||||
if type == "extraction":
|
||||
print("Extraction path:", path)
|
||||
else:
|
||||
print("Download path", path)
|
||||
|
||||
session = db.session()
|
||||
path_exists = session.query(Download).filter(and_(Download.path==path, Download.finished==False, Download.type==type)).first()
|
||||
|
||||
if path_exists and False: # TODO FIX THIS
|
||||
print("DAFUG", path_exists)
|
||||
print("WTF", path_exists.path)
|
||||
raise AssertionError("ERROR: {} is already downloading.".format(path))
|
||||
else:
|
||||
print("Adding to download log:", path)
|
||||
session.merge(Download(path=path, size=start_size, type=type, local_path=local_path, url=url, finished=False, count=1))
|
||||
session.commit()
|
||||
|
||||
db.close_session()
|
||||
|
||||
def set_extraction_status(path, count):
|
||||
|
||||
session = db.session()
|
||||
obj = session.query(Download).filter(and_(Download.path==path, Download.type=="extraction")).first()
|
||||
if not obj:
|
||||
print("ERROR: {} is not currently extraction, cannot set status.".format(path))
|
||||
else:
|
||||
obj.count = count
|
||||
session.merge(obj)
|
||||
session.commit()
|
||||
|
||||
db.close_session()
|
||||
|
||||
def log_end_download(path, type="download"):
|
||||
|
||||
print("Downlod end logged", path)
|
||||
session = db.session()
|
||||
obj = session.query(Download).filter(and_(Download.path==path, Download.type==type)).first()
|
||||
if not obj:
|
||||
raise AssertionError("ERROR: {} is not downloading/cannot remove.".format(path))
|
||||
else:
|
||||
print("Removing from download log:", path)
|
||||
obj.finished = True
|
||||
session.merge(obj)
|
||||
session.commit()
|
||||
|
||||
db.close_session()
|
||||
|
||||
def get_download_size(path, auth):
|
||||
|
||||
session = db.session()
|
||||
obj = session.query(Download).filter(Download.path==path).first()
|
||||
|
||||
if not obj :
|
||||
print("Warning: Download-Object does no longe exist in DB. Returning -1")
|
||||
return -1
|
||||
elif obj.size != -1:
|
||||
session.close()
|
||||
return obj.size
|
||||
|
||||
# query size #
|
||||
r = requests.get(obj.url, params={"path": path, "info": 1}, auth=auth)
|
||||
r.raise_for_status()
|
||||
|
||||
size = r.json()["size"]
|
||||
obj.size = _bytes_to_mb(size)
|
||||
session.merge(obj)
|
||||
session.commit()
|
||||
session.close()
|
||||
|
||||
return size
|
||||
|
||||
def get_percent_filled(path, auth):
|
||||
|
||||
session = db.session()
|
||||
obj = session.query(Download).filter(Download.path==path, Download.finished==False).first()
|
||||
|
||||
if not obj:
|
||||
return 100
|
||||
|
||||
if obj.type == "extraction":
|
||||
return obj.count / obj.size * 100
|
||||
|
||||
if not obj:
|
||||
return 100 # means its finished
|
||||
|
||||
size = _bytes_to_mb(os.stat(obj.local_path).st_size)
|
||||
total_size = get_download_size(obj.path, auth)
|
||||
session.close()
|
||||
|
||||
if total_size == 0:
|
||||
return 0
|
||||
|
||||
print("Current filled:", size / total_size * 100)
|
||||
return size / total_size * 100
|
||||
|
||||
def get_download(path=None):
|
||||
|
||||
session = db.session()
|
||||
if path:
|
||||
MIN_SIZE_PGBAR_LIMIT = 1024*1024*100 # 100mb
|
||||
downloads = session.query(Download).filter(Download.size>MIN_SIZE_PGBAR_LIMIT, Download.finished==False).all()
|
||||
else:
|
||||
downloads = session.query(Download).filter(Download.finished==False).all()
|
||||
|
||||
session.close()
|
||||
return downloads
|
||||
12
todo.txt
Normal file
12
todo.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
# important
|
||||
## downloaded file hash sum check
|
||||
## fix initial startup pictures not loading
|
||||
## implement flush download cache button
|
||||
## fix Call of duty installation chain
|
||||
## test on fresh windows
|
||||
## prepare bf2
|
||||
|
||||
nice to have
|
||||
# implement full remove
|
||||
# player name templating (e.g. in regex or game file folder)
|
||||
# oauth login server
|
||||
2
updater/requirements.txt
Normal file
2
updater/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
tkinter
|
||||
requests
|
||||
51
updater/updater.py
Normal file
51
updater/updater.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import requests
|
||||
import zipfile
|
||||
import io
|
||||
import shutil
|
||||
import tkinter as tk
|
||||
|
||||
|
||||
CLIENT_DIR = os.path.join("client")
|
||||
INTERNAL_DIR = os.path.join(CLIENT_DIR, "_internal")
|
||||
|
||||
def prompt_user(version):
|
||||
root = tk.Tk()
|
||||
root.withdraw() # Hide main window
|
||||
result = tk.messagebox.askyesno("Update Available", f"New version {version} available. Download and install?")
|
||||
root.destroy()
|
||||
return result
|
||||
|
||||
def download_and_extract(zip_url):
|
||||
print("Downloading...")
|
||||
response = requests.get(zip_url)
|
||||
response.raise_for_status()
|
||||
with zipfile.ZipFile(io.BytesIO(response.content)) as z:
|
||||
temp_dir = "_temp_extracted"
|
||||
z.extractall(temp_dir)
|
||||
top_folder = next(os.scandir(temp_dir)).path
|
||||
|
||||
# Replace _internal
|
||||
source_internal = os.path.join(top_folder, "client", "_internal")
|
||||
if os.path.exists(INTERNAL_DIR):
|
||||
shutil.rmtree(INTERNAL_DIR)
|
||||
shutil.copytree(source_internal, INTERNAL_DIR)
|
||||
|
||||
# Replace client.exe
|
||||
source_exe = os.path.join(top_folder, "client", "client.exe")
|
||||
target_exe = os.path.join(CLIENT_DIR, "client.exe")
|
||||
shutil.copy2(source_exe, target_exe)
|
||||
|
||||
shutil.rmtree(temp_dir)
|
||||
print("Update complete.")
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
|
||||
if prompt_user():
|
||||
download_and_extract()
|
||||
# TODO: run main file again
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
65
windows_run_as_admin.ps1
Normal file
65
windows_run_as_admin.ps1
Normal file
@@ -0,0 +1,65 @@
|
||||
param (
|
||||
[string]$PathsJson
|
||||
)
|
||||
|
||||
try {
|
||||
$Paths = $PathsJson | ConvertFrom-Json -ErrorAction Stop
|
||||
} catch {
|
||||
# If it fails, assume it's Base64-encoded and decode it
|
||||
try {
|
||||
$DecodedJson = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($PathsJson))
|
||||
$Paths = $DecodedJson | ConvertFrom-Json -ErrorAction Stop
|
||||
} catch {
|
||||
Write-Error "Failed to parse PathsJson as JSON or Base64-encoded JSON."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Paths"
|
||||
Write-Host $PathsJson
|
||||
Write-Host $PWD
|
||||
|
||||
# Check if running as administrator
|
||||
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
$Principal = New-Object System.Security.Principal.WindowsPrincipal($CurrentUser)
|
||||
$IsAdmin = $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
|
||||
|
||||
if (-not $IsAdmin) {
|
||||
# Relaunch the script with elevated privileges
|
||||
$EncodedJson = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($PathsJson))
|
||||
$TempFile = New-TemporaryFile
|
||||
Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -Command `"Set-Location -Path '$PWD'; & '$PSCommandPath' '$EncodedJson' *> '$TempFile'`" " -Verb RunAs -Wait
|
||||
|
||||
Start-Sleep 1
|
||||
if (Test-Path $TempFile) {
|
||||
# Output the captured output
|
||||
Get-Content $TempFile | Write-Host
|
||||
Remove-Item $TempFile
|
||||
}else{
|
||||
Write-Host "No output captured. Check if the script produces any output."
|
||||
}
|
||||
|
||||
exit
|
||||
}
|
||||
|
||||
|
||||
foreach($Path in $Paths){
|
||||
|
||||
Write-Host "Running: $Path"
|
||||
|
||||
# Run the process and capture output
|
||||
try {
|
||||
|
||||
if ($Path -match '\.reg$') {
|
||||
# Run regedit silently to merge the .reg file
|
||||
$Process = Start-Process -FilePath "regedit.exe" -ArgumentList "/s `"$Path`"" -NoNewWindow -PassThru
|
||||
} else {
|
||||
# Run the file normally
|
||||
$Process = Start-Process -FilePath $Path -NoNewWindow -PassThru
|
||||
}
|
||||
$Process.WaitForExit()
|
||||
|
||||
} catch {
|
||||
Write-Host "Error running the process: $_"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user