From 25aff69cf6df92662d41b99a94658e8e989b23d7 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sat, 15 Feb 2020 11:01:16 -0700 Subject: [PATCH] fix app directory validation --- hyperglass/__init__.py | 38 ++----------------------- hyperglass/configuration/__init__.py | 4 ++- hyperglass/util.py | 42 +++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/hyperglass/__init__.py b/hyperglass/__init__.py index 2e0fff1..196c842 100644 --- a/hyperglass/__init__.py +++ b/hyperglass/__init__.py @@ -37,15 +37,13 @@ POSSIBILITY OF SUCH DAMAGE. """ # Standard Library -import os import sys -import getpass -from pathlib import Path # Third Party import uvloop # Project +from hyperglass.util import set_app_path from hyperglass.constants import METADATA try: @@ -59,39 +57,7 @@ else: _style = "plaintext" stackprinter.set_excepthook(style=_style) -config_path = None - -_CONFIG_PATHS = (Path.home() / "hyperglass", Path("/etc/hyperglass/")) - -for path in _CONFIG_PATHS: - try: - if not isinstance(path, Path): - path = Path(path) - - if path.exists(): - tmp = path / "test.tmp" - tmp.touch() - if tmp.exists(): - config_path = path - tmp.unlink() - break - except Exception: - config_path = None - -if config_path is None: - raise RuntimeError( - """ -No configuration directories were determined to both exist and be readable -by hyperglass. hyperglass is running as user '{un}' (UID '{uid}'), and tried to access -the following directories: -{dir}""".format( - un=getpass.getuser(), - uid=os.getuid(), - dir="\n".join([" - " + str(p) for p in _CONFIG_PATHS]), - ) - ) - -os.environ["hyperglass_directory"] = str(config_path) +set_app_path() uvloop.install() diff --git a/hyperglass/configuration/__init__.py b/hyperglass/configuration/__init__.py index 768375d..6a012bb 100644 --- a/hyperglass/configuration/__init__.py +++ b/hyperglass/configuration/__init__.py @@ -14,7 +14,7 @@ from aiofile import AIOFile from pydantic import ValidationError # Project -from hyperglass.util import log, check_path +from hyperglass.util import log, check_path, set_app_path from hyperglass.constants import ( CREDIT, LOG_LEVELS, @@ -31,6 +31,8 @@ from hyperglass.configuration.models import routers as _routers from hyperglass.configuration.models import commands as _commands from hyperglass.configuration.markdown import get_markdown +set_app_path() + CONFIG_PATH = Path(os.environ["hyperglass_directory"]) log.info("Configuration directory: {d}", d=str(CONFIG_PATH)) diff --git a/hyperglass/util.py b/hyperglass/util.py index 73d07f9..9bb7330 100644 --- a/hyperglass/util.py +++ b/hyperglass/util.py @@ -365,7 +365,7 @@ async def build_frontend( # noqa: C901 Compare repository's static assets with build directory's assets. If the contents don't match, re-copy the files. """ - asset_dir = Path(__file__).parent.parent / "assets" + asset_dir = Path(__file__).parent.parent / "images" target_dir = app_path / "static" / "images" comparison = dircmp(asset_dir, target_dir, ignore=[".DS_Store"]) @@ -381,3 +381,43 @@ async def build_frontend( # noqa: C901 raise RuntimeError(str(e)) return True + + +def set_app_path(required=False): + """Find app directory and set value to environment variable.""" + import os + from pathlib import Path + from getpass import getuser + + matched_path = None + + config_paths = (Path.home() / "hyperglass", Path("/etc/hyperglass/")) + + for path in config_paths: + try: + if path.exists(): + tmp = path / "test.tmp" + tmp.touch() + if tmp.exists(): + matched_path = path + tmp.unlink() + break + except Exception: + matched_path = None + + if required and matched_path is None: + # Only raise an error if required is True + raise RuntimeError( + """ + No configuration directories were determined to both exist and be readable + by hyperglass. hyperglass is running as user '{un}' (UID '{uid}'), and tried + to access the following directories: + {dir}""".format( + un=getuser(), + uid=os.getuid(), + dir="\n".join([" - " + str(p) for p in config_paths]), + ) + ) + + os.environ["hyperglass_directory"] = str(matched_path) + return True