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