diff --git a/.gitignore b/.gitignore index 8aae9ce..5164739 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ node_modules *srcinfo.json .mypy_cache _cache -.vscode \ No newline at end of file +.vscode +*.cache \ No newline at end of file diff --git a/app/appconfig.py b/app/appconfig.py index 15063e5..6d9a229 100644 --- a/app/appconfig.py +++ b/app/appconfig.py @@ -1,6 +1,9 @@ # Copyright 2016-2020 Christoph Reiter # SPDX-License-Identifier: MIT +from typing import Optional + + REPO_URL = "https://repo.msys2.org" REPOSITORIES = [ @@ -53,4 +56,4 @@ BUILD_STATUS_CONFIG = [ UPDATE_INTERVAL = 60 * 5 REQUEST_TIMEOUT = 60 -CACHE_LOCAL = False +CACHE_DIR: Optional[str] = None diff --git a/app/fetch.py b/app/fetch.py index c44c650..702b11b 100644 --- a/app/fetch.py +++ b/app/fetch.py @@ -31,13 +31,12 @@ def get_update_urls() -> List[str]: async def get_content_cached(url: str, *args: Any, **kwargs: Any) -> bytes: - if not appconfig.CACHE_LOCAL: + cache_dir = appconfig.CACHE_DIR + if cache_dir is None: async with httpx.AsyncClient() as client: r = await client.get(url, *args, **kwargs) return r.content - base = os.path.dirname(os.path.realpath(__file__)) - cache_dir = os.path.join(base, "_cache") os.makedirs(cache_dir, exist_ok=True) cache_fn = quote_plus( @@ -233,7 +232,7 @@ async def update_arch_versions() -> None: async def check_needs_update(_cache_key: List[str] = [""]) -> bool: """Raises RequestException""" - if appconfig.CACHE_LOCAL: + if appconfig.CACHE_DIR: return True async def get_headers(client: httpx.AsyncClient, *args: Any, **kwargs: Any) -> httpx.Headers: diff --git a/run.py b/run.py index f90ae84..9cd9105 100755 --- a/run.py +++ b/run.py @@ -1,6 +1,7 @@ # Copyright 2016-2020 Christoph Reiter # SPDX-License-Identifier: MIT +import os import sys import argparse from typing import List, Optional, Union @@ -18,7 +19,12 @@ def main(argv: List[str]) -> Optional[Union[int, str]]: help="port number") args = parser.parse_args() - appconfig.CACHE_LOCAL = args.cache + if args.cache: + base = os.path.dirname(os.path.realpath(__file__)) + cache_dir = os.path.join(base, ".app.cache") + print(f"Using cache: {repr(cache_dir)}") + appconfig.CACHE_DIR = cache_dir + uvicorn.run(app, host="127.0.0.1", port=args.port) return None