Files
docker-py-revanced/src/utils.py
T
2023-08-08 03:36:49 +00:00

100 lines
2.7 KiB
Python

"""Utilities."""
import re
import subprocess
from typing import Dict
from loguru import logger
from requests import Response
default_build = [
"youtube",
"youtube_music",
]
possible_archs = ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
def update_changelog(name: str, response: Dict[str, str]) -> None:
"""Updated Changelog."""
parent_repo = "https://github.com/nikhilbadyal/docker-py-revanced"
with open("changelog.md", "a", encoding="utf_8") as file1:
collapse_start = f"\n<details> <summary>👀 {name} </summary>\n\n"
release_version = f"**Release Version** - [{response['tag_name']}]({response['html_url']})<br>"
change_log = f"**Changelog** -<br> {response['body']}"
publish_time = f"**Published at** -<br> {response['published_at']}"
footer = f"<br><sub>Change logs generated by [Docker Py Revanced]({parent_repo})</sub>\n"
collapse_end = "</details>"
change_log = (
collapse_start
+ release_version
+ change_log
+ publish_time
+ footer
+ collapse_end
)
file1.write(change_log)
class AppNotFound(ValueError):
"""Not a valid Revanced App."""
pass
class PatcherDownloadFailed(Exception):
"""Not a valid Revanced App."""
pass
class PatchesJsonFailed(ValueError):
"""Patches failed."""
pass
def handle_response(response: Response) -> None:
"""Handle Get Request Response."""
response_code = response.status_code
if response_code != 200:
logger.error(response.text)
exit(1)
def slugify(string: str) -> str:
"""Converts a string to a slug format."""
# Convert to lowercase
string = string.lower()
# Remove special characters
string = re.sub(r"[^\w\s-]", "", string)
# Replace spaces with dashes
string = re.sub(r"\s+", "-", string)
# Remove consecutive dashes
string = re.sub(r"-+", "-", string)
# Remove leading and trailing dashes
string = string.strip("-")
return string
def check_java(dry_run: bool) -> None:
"""Check if Java>=17 is installed."""
try:
if dry_run:
return
jd = subprocess.check_output(
["java", "-version"], stderr=subprocess.STDOUT
).decode("utf-8")
jd = jd[1:-1]
if "Runtime Environment" not in jd:
raise subprocess.CalledProcessError(-1, "java -version")
if "17" not in jd and "20" not in jd:
raise subprocess.CalledProcessError(-1, "java -version")
logger.debug("Cool!! Java is available")
except subprocess.CalledProcessError:
logger.debug("Java>= 17 Must be installed")
exit(-1)