From ce480e80938550eeed954e34e1dc1539e70fa53f Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sat, 15 Feb 2020 21:56:40 -0700 Subject: [PATCH] fix asset directory syncing --- hyperglass/cli/util.py | 10 +++---- hyperglass/util.py | 59 +++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/hyperglass/cli/util.py b/hyperglass/cli/util.py index 5fe2573..cc397c8 100644 --- a/hyperglass/cli/util.py +++ b/hyperglass/cli/util.py @@ -201,17 +201,15 @@ def build_ui(): def create_dir(path, **kwargs): """Validate and attempt to create a directory, if it does not exist.""" + # If input path is not a path object, try to make it one if not isinstance(path, Path): - # If input path is not a path object, try to make it one - try: path = Path(path) except TypeError: error("{p} is not a valid path", p=path) + # If path does not exist, try to create it if not path.exists(): - # If path does not exist, try to create it - try: path.mkdir(**kwargs) except PermissionError: @@ -221,12 +219,12 @@ def create_dir(path, **kwargs): p=path, ) + # Verify the path was actually created if path.exists(): - # Verify the path was actually created success("Created {p}", p=path) + # If the path already exists, inform the user elif path.exists(): - # If the path already exists, inform the user info("{p} already exists", p=path) return True diff --git a/hyperglass/util.py b/hyperglass/util.py index 183354a..cc17f77 100644 --- a/hyperglass/util.py +++ b/hyperglass/util.py @@ -256,10 +256,7 @@ async def move_files(src, dst, files): # noqa: C901 def migrate_static_assets(app_path): - """Compare repository's static assets with build directory's assets. - - If the contents don't match, re-copy the files. - """ + """Synchronize the project assets with the installation assets.""" import shutil from pathlib import Path from filecmp import dircmp @@ -267,14 +264,26 @@ def migrate_static_assets(app_path): asset_dir = Path(__file__).parent.parent / "images" target_dir = app_path / "static" / "images" - if not target_dir.exists(): - target_dir.mkdir(parents=True) + target_exists = target_dir.exists() - comparison = dircmp(asset_dir, target_dir, ignore=[".DS_Store"]) - - if not comparison.left_list == comparison.right_list: + if not target_exists: shutil.copytree(asset_dir, target_dir) - if not comparison.left_list == comparison.right_list: + + # Compare the contents of the project's asset directory (considered + # the source of truth) with the installation directory. If they do + # not match, delete the installation directory's asset directory and + # re-copy it. + compare_initial = dircmp(asset_dir, target_dir, ignore=[".DS_Store"]) + + if not compare_initial.left_list == compare_initial.right_list: + shutil.rmtree(target_dir) + shutil.copytree(asset_dir, target_dir) + + # Re-compare the source and destination directory contents to + # ensure they match. + compare_post = dircmp(asset_dir, target_dir, ignore=[".DS_Store"]) + + if not compare_post.left_list == compare_post.right_list: return ( False, "Files in {a} do not match files in {b}", @@ -383,19 +392,15 @@ async def build_frontend( # noqa: C901 env_vars = {"_HYPERGLASS_CONFIG_": params} - """ - Set NextJS production/development mode and base URL based on - developer_mode setting. - """ + # Set NextJS production/development mode and base URL based on + # developer_mode setting. if dev_mode: env_vars.update({"NODE_ENV": "development", "_HYPERGLASS_URL_": dev_url}) else: env_vars.update({"NODE_ENV": "production", "_HYPERGLASS_URL_": prod_url}) - """ - Check if hyperglass/ui/node_modules has been initialized. If not, - initialize it. - """ + # Check if hyperglass/ui/node_modules has been initialized. If not, + # initialize it. initialized = await check_node_modules() if initialized: log.debug("node_modules is already initialized") @@ -407,17 +412,13 @@ async def build_frontend( # noqa: C901 try: env_json = json.dumps(env_vars) - """ - Create SHA256 hash from all parameters passed to UI, use as - build identifier. - """ + # Create SHA256 hash from all parameters passed to UI, use as + # build identifier. build_id = hashlib.sha256(env_json.encode()).hexdigest() + # Read hard-coded environment file from last build. If build ID + # matches this build's ID, don't run a new build. if env_file.exists() and not force: - """ - Read hard-coded environment file from last build. If build - ID matches this build's ID, don't run a new build. - """ async with AIOFile(env_file, "r") as ef: ef_json = await ef.read() ef_id = json.loads(ef_json).get("buildId", "empty") @@ -430,10 +431,8 @@ async def build_frontend( # noqa: C901 ) return True - """ - Create temporary file. json file extension is added for easy - webpack JSON parsing. - """ + # Create temporary file. json file extension is added for easy + # webpack JSON parsing. temp_file = tempfile.NamedTemporaryFile( mode="w+", prefix="hyperglass_", suffix=".json", delete=not dev_mode )