🐛 Handle a case when user enters invalid app for rebuilding

This commit is contained in:
Nikhil Badyal
2022-11-11 23:20:29 +05:30
parent e5c0702c34
commit f42381b37c
5 changed files with 57 additions and 30 deletions
+37 -20
View File
@@ -8,6 +8,7 @@ from src.config import RevancedConfig
from src.downloader import Downloader
from src.parser import Parser
from src.patches import Patches
from src.utils import AppNotFound
def main() -> None:
@@ -25,35 +26,51 @@ def main() -> None:
logger.info("Trying to build %s" % app)
app_all_patches, version, is_experimental = patcher.get_app_configs(app)
version = downloader.download_apk_to_patch(version, app)
config.app_versions[app] = version
patcher.include_exclude_patch(app, parser, app_all_patches)
logger.info(f"Downloaded {app}, version {version}")
parser.patch_app(app=app, version=version, is_experimental=is_experimental)
except AppNotFound as e:
logger.info(f"Invalid app requested to build {e}")
except Exception as e:
logger.exception(f"Failed to build {app} because of {e}")
if len(config.alternative_youtube_patches):
if len(config.alternative_youtube_patches) and "youtube" in config.apps:
for alternative_patch in config.alternative_youtube_patches:
logger.info(f"Rebuilding youtube with inverted ${alternative_patch} patch.")
_, version, is_experimental = patcher.get_app_configs("youtube")
parser.invert_patch(alternative_patch)
parser.patch_app(
app="youtube",
version=version,
is_experimental=is_experimental,
output_prefix="-" + alternative_patch + "-",
)
if len(config.alternative_youtube_music_patches):
was_inverted = parser.invert_patch(alternative_patch)
if was_inverted:
logger.info(
f"Rebuilding youtube with inverted {alternative_patch} patch."
)
parser.patch_app(
app="youtube",
version=config.app_versions.get("youtube", "latest"),
is_experimental=is_experimental,
output_prefix="-" + alternative_patch + "-",
)
else:
logger.info(
f"Skipping Rebuilding youtube as {alternative_patch} patch was not found."
)
if len(config.alternative_youtube_music_patches) and "youtube_music" in config.apps:
for alternative_patch in config.alternative_youtube_music_patches:
logger.info(
f"Rebuilding youtube music with inverted ${alternative_patch} patch."
)
_, version, is_experimental = patcher.get_app_configs("youtube_music")
parser.invert_patch(alternative_patch)
parser.patch_app(
app="youtube_music",
version=version,
is_experimental=is_experimental,
output_prefix="-" + alternative_patch + "-",
)
was_inverted = parser.invert_patch(alternative_patch)
if was_inverted:
logger.info(
f"Rebuilding youtube music with inverted {alternative_patch} patch."
)
parser.patch_app(
app="youtube_music",
version=config.app_versions.get("youtube_music", "latest"),
is_experimental=is_experimental,
output_prefix="-" + alternative_patch + "-",
)
else:
logger.info(
f"Skipping Rebuilding youtube music as {alternative_patch} patch was not found."
)
if __name__ == "__main__":
+2 -1
View File
@@ -1,6 +1,6 @@
"""Revanced Configurations."""
from pathlib import Path
from typing import List
from typing import Dict, List
from environs import Env
from requests import Session
@@ -12,6 +12,7 @@ class RevancedConfig:
"""Revanced Configurations."""
def __init__(self, env: Env) -> None:
self.app_versions: Dict[str, str] = {}
self.env = env
self.temp_folder = Path("apks")
self.session = Session()
+10 -6
View File
@@ -49,16 +49,20 @@ class Parser(object):
"""
return self._PATCHES
def invert_patch(self, name: str) -> None:
def invert_patch(self, name: str) -> bool:
"""
Getter to get all excluded patches
:return: List of excluded patches
"""
patch_index = self._PATCHES.index(name)
if self._PATCHES[patch_index - 1] == "-e":
self._PATCHES[patch_index - 1] = "-i"
else:
self._PATCHES[patch_index - 1] = "-e"
try:
patch_index = self._PATCHES.index(name)
if self._PATCHES[patch_index - 1] == "-e":
self._PATCHES[patch_index - 1] = "-i"
else:
self._PATCHES[patch_index - 1] = "-e"
return True
except ValueError:
return False
# noinspection IncorrectFormatting
def patch_app(
+2 -3
View File
@@ -1,12 +1,12 @@
"""Revanced Patches."""
import subprocess
import sys
from typing import Any, Dict, List, Tuple
from loguru import logger
from requests import Session
from src.config import RevancedConfig
from src.utils import AppNotFound
class Patches(object):
@@ -118,8 +118,7 @@ class Patches(object):
"nyx-music-player": "_nyx",
}
if not (app_name := app_names.get(app)):
logger.debug("Invalid app name")
sys.exit(-1)
raise AppNotFound(app_name)
patches = getattr(self, app_name)
version = ""
try:
+6
View File
@@ -31,3 +31,9 @@ def update_changelog(name: str, response: Dict[str, str]) -> None:
)
file1.write(change_log)
file1.close()
class AppNotFound(ValueError):
"""Not a valid Revanced App."""
pass