From 12f86de92a55f8b2b253d65d562d778db56563e2 Mon Sep 17 00:00:00 2001 From: Ananas Date: Sun, 4 Aug 2024 15:15:40 +0200 Subject: [PATCH] feat: show head commit of local agac repo --- api/agac.py | 22 ++++++++++++++++------ api/constants.py | 6 ++++-- api/main.py | 5 +++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/api/agac.py b/api/agac.py index 6eec464..985617b 100644 --- a/api/agac.py +++ b/api/agac.py @@ -10,7 +10,7 @@ import subprocess from .image import Image -from .constants import GIT_PATH, ALLOWED_FILE_EXTENSIONS +from .constants import GIT_PATH, ALLOWED_FILE_EXTENSIONS __all__ = ("Agac",) @@ -19,7 +19,7 @@ class Agac: def __init__(self) -> None: self._repo_path = Path(GIT_PATH) - self.__update_repo() + self.head_commit = self.__update_repo() self.images, self.categories = self.__phrase_images() def get_random(self, categorie: str = None) -> Image: @@ -42,7 +42,7 @@ def __update_repo(self): ) process = subprocess.Popen( - ["git", "pull"], + ["git", "pull"], text = True, stdout = subprocess.PIPE, cwd = self._repo_path @@ -51,10 +51,20 @@ def __update_repo(self): process.wait() output, _ = process.communicate() - if not process.returncode == 0: - print("Git Error") + if not process.returncode == 0: + print(f"ERROR: {output}") - print(f"Git Output: {output}") + hash_process = subprocess.Popen( + ["git", "rev-parse", "HEAD"], + text = True, + stdout = subprocess.PIPE, + cwd = self._repo_path + ) + + hash_process.wait() + hash, _ = hash_process.communicate() + + return hash.replace("\n", "") def __phrase_images(self) -> Union[List[Image], Dict[str, List[Image]]]: images = [] diff --git a/api/constants.py b/api/constants.py index aeceaee..3e2d56e 100644 --- a/api/constants.py +++ b/api/constants.py @@ -3,10 +3,12 @@ __all__ = ( "GIT_PATH", "GIT_REPO_URL", - "ALLOWED_FILE_EXTENSIONS" + "CACHE_PATH", + "ALLOWED_FILE_EXTENSIONS", + "RATE_LIMIT" ) -ALLOWED_FILE_EXTENSIONS = [".png", ".jpeg", ".jpg"] +ALLOWED_FILE_EXTENSIONS = [".png", ".jpeg", ".jpg", ".webp"] GIT_PATH = config("GIT_PATH", default = "./assets/repo", cast = str) CACHE_PATH = config("CACHE_PATH", default = "./assets/cache", cast = str) GIT_REPO_URL = config("GIT_REPO_URL", default = "https://github.com/THEGOLDENPRO/anime-girls-and-computers", cast = str) diff --git a/api/main.py b/api/main.py index 4866ec5..2de5dc1 100644 --- a/api/main.py +++ b/api/main.py @@ -17,7 +17,7 @@ from .constants import RATE_LIMIT from . import __version__, errors -ROOT_PATH = (lambda x: x if x is not None else "")(environ.get("ROOT_PATH")) +ROOT_PATH = environ.get("ROOT_PATH", "") TAGS_METADATA = [ { @@ -79,7 +79,8 @@ async def info(): """Returns repository information like image count and etc.""" return { "version": __version__, - "image_count": len(agac.images) + "image_count": len(agac.images), + "agac_head_commit": agac.head_commit } @app.get(