Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

376 use branch info to make decisions in qs #377

Merged
merged 3 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ logs/
# Ignore macOS system files
.DS_Store

# Ignore uploads
# Ignore these direct
uploads/

.vscode/
config/
json-schema/
38 changes: 0 additions & 38 deletions json-schema/README.md

This file was deleted.

92 changes: 91 additions & 1 deletion modules/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import os
import re
import requests
Expand All @@ -15,6 +16,91 @@
}


JSON_SCHEMA_DIR = "json-schema"
FILES_TO_DOWNLOAD = ["prototype_config.yml", "config-schema.json"]
GITHUB_BASE_URL = "https://raw.githubusercontent.com/Kometa-Team/Kometa"

HASH_FILE = os.path.join(
JSON_SCHEMA_DIR, "file_hashes.txt"
) # Stores previous file hashes


def get_kometa_branch():
"""Fetch the correct branch (master or nightly)."""
from .helpers import check_for_update # Prevent circular import

version_info = check_for_update()
return version_info.get("kometa_branch", "nightly") # Default to nightly


def calculate_hash(content):
"""Compute the SHA256 hash of the given content."""
return hashlib.sha256(content.encode("utf-8")).hexdigest()


def load_previous_hashes():
"""Load the last known hashes of schema files."""
if not os.path.exists(HASH_FILE):
return {}

hashes = {}
with open(HASH_FILE, "r", encoding="utf-8") as f:
for line in f:
filename, file_hash = line.strip().split(":", 1)
hashes[filename] = file_hash
return hashes


def save_hashes(hashes):
"""Save updated hashes to the hash file."""
with open(HASH_FILE, "w", encoding="utf-8") as f:
for filename, file_hash in hashes.items():
f.write(f"{filename}:{file_hash}\n")


def ensure_json_schema():
"""Ensure json-schema files exist and are up-to-date based on hash checks."""
branch = get_kometa_branch()

if not os.path.exists(JSON_SCHEMA_DIR):
os.makedirs(JSON_SCHEMA_DIR)

previous_hashes = load_previous_hashes()
new_hashes = {}

for filename in FILES_TO_DOWNLOAD:
file_path = os.path.join(JSON_SCHEMA_DIR, filename)
url = f"{GITHUB_BASE_URL}/{branch}/json-schema/{filename}"

# print(f"[INFO] Checking for updates: {filename}...")

try:
response = requests.get(url, timeout=10)
response.raise_for_status()
new_content = response.text
new_hash = calculate_hash(new_content)

# Compare hash with previous version
if filename in previous_hashes and previous_hashes[filename] == new_hash:
# print(f"[INFO] No changes detected for {filename}. Skipping download.")
new_hashes[filename] = new_hash # Keep existing hash
continue

# Save the new file if hash has changed
# print(f"[INFO] Changes detected in {filename}. Downloading new version...")
with open(file_path, "w", encoding="utf-8") as f:
f.write(new_content)

new_hashes[filename] = new_hash

except requests.RequestException as e:
print(f"[ERROR] Failed to download {filename}: {e}")
continue # Skip to the next file

# Save updated hashes
save_hashes(new_hashes)


def get_local_version():
"""Read the local VERSION file and determine the branch."""
version_file = "VERSION"
Expand Down Expand Up @@ -43,16 +129,20 @@ def get_remote_version(branch):


def check_for_update():
"""Compare the local version with the remote version."""
"""Compare the local version with the remote version and determine Kometa branch."""
local_version, branch = get_local_version()
remote_version = get_remote_version(branch)

update_available = remote_version and remote_version != local_version

# Determine Kometa branch
kometa_branch = "master" if branch == "master" else "nightly"

return {
"local_version": local_version,
"remote_version": remote_version,
"branch": branch,
"kometa_branch": kometa_branch,
"update_available": update_available,
}

Expand Down
13 changes: 11 additions & 2 deletions modules/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
build_config_dict,
get_template_list,
get_bits,
check_for_update,
enforce_string_fields,
ensure_json_schema,
STRING_FIELDS,
)

Expand Down Expand Up @@ -406,12 +408,19 @@ def build_config(header_style="ascii"):
yaml.default_flow_style = False
yaml.sort_keys = False

ensure_json_schema()

with open("json-schema/config-schema.json", "r") as file:
schema = yaml.load(file)

# Prepare the final YAML content
# Fetch kometa_branch dynamically
version_info = check_for_update()
kometa_branch = version_info.get(
"kometa_branch", "nightly"
) # Default to nightly if not found

yaml_content = (
"# yaml-language-server: $schema=https://raw.githubusercontent.com/Kometa-Team/Kometa/nightly/json-schema/config-schema.json\n\n"
f"# yaml-language-server: $schema=https://raw.githubusercontent.com/Kometa-Team/Kometa/{kometa_branch}/json-schema/config-schema.json\n\n"
f"{section_heading('KOMETA') if header_style == 'ascii' else ('#==================== KOMETA ====================#' if header_style == 'divider' else '')}\n\n"
f"{header_comment}\n\n"
)
Expand Down
11 changes: 10 additions & 1 deletion modules/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
from ruamel.yaml import YAML
from flask import current_app as app

from .helpers import build_config_dict, get_template_list, get_bits, booler
from .helpers import (
build_config_dict,
get_template_list,
get_bits,
booler,
ensure_json_schema,
)
from .iso_639_1 import iso_639_1_languages # Importing the languages list
from .iso_639_2 import iso_639_2_languages # Importing the languages list
from .iso_3166_1 import iso_3166_1_regions # Importing the regions list
Expand Down Expand Up @@ -202,6 +208,9 @@ def retrieve_status(target):
def get_dummy_data(target):

yaml = YAML(typ="safe", pure=True)

ensure_json_schema()

with open("json-schema/prototype_config.yml", "r") as file:
base_config = yaml.load(file)

Expand Down
10 changes: 7 additions & 3 deletions quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
redact_sensitive_data,
check_for_update,
update_checker_loop,
booler,
is_default_image,
ensure_json_schema,
)
from modules.persistence import (
save_settings,
Expand All @@ -51,9 +54,7 @@
flush_session_storage,
notification_systems_available,
)
from modules.database import reset_data
from modules.database import get_unique_config_names
from modules.helpers import booler, is_default_image
from modules.database import reset_data, get_unique_config_names

from PIL import Image, ImageDraw

Expand Down Expand Up @@ -111,6 +112,9 @@ def inject_version_info():

server_session = Session(app)

# Ensure json-schema files are up to date at startup
ensure_json_schema()


@app.route("/check_base_images", methods=["GET"])
def check_base_images():
Expand Down
6 changes: 3 additions & 3 deletions templates/001-navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ <h2 class="fw-bold text-white">{{ page_info['title'] }}</h2>

<!-- Previous Button (Hidden on Start Page) -->
{% if page_info['template_name'] != '001-start' %}
<button type="submit" class="btn nav-button d-none d-md-inline-flex" onclick="loading('previous')"
<button type="submit" class="btn nav-button d-none d-md-inline-flex" onclick="loading('prev')"
title="Previous Step: {{ page_info['prev_page_name'] }}"
formaction="/step/{{ page_info['prev_page'] }}">
<i class="fa fa-arrow-left"></i> Previous
<i id="prev-spinner-icon" class="fa fa-arrow-left"></i> Previous
</button>
{% endif %}

Expand Down Expand Up @@ -63,7 +63,7 @@ <h2 class="fw-bold text-white">{{ page_info['title'] }}</h2>
<button type="submit" class="btn nav-button d-none d-md-inline-flex" onclick="loading('next')"
title="Next Step: {{ page_info['next_page_name'] }}"
formaction="/step/{{ page_info['next_page'] }}">
Next <i class="fa fa-arrow-right"></i>
Next <i id="next-spinner-icon" class="fa fa-arrow-right"></i>
</button>
{% endif %}

Expand Down
Loading
Loading