diff --git a/beat/editor/scripts/editor_cli.py b/beat/editor/scripts/editor_cli.py index 26ad706dfa46888b1a407788c4d30a2859e115ae..bf229e5cd3d04eb2ac93f09840ef0ed3094fac62 100644 --- a/beat/editor/scripts/editor_cli.py +++ b/beat/editor/scripts/editor_cli.py @@ -39,6 +39,8 @@ from click_plugins import with_plugins from PyQt5.QtWidgets import QApplication from PyQt5.QtCore import QCoreApplication +from beat.core.dock import Host + from beat.cmdline.click_helper import AliasedGroup from beat.cmdline.decorators import raise_on_error from beat.cmdline.decorators import verbosity_option @@ -53,6 +55,32 @@ from .. import version global logger logger = None + +def dump_environments(environments_file_path): + logger.info("Generating environments information") + Host(images_cache=environments_file_path, raise_on_errors=False) + logger.info("Done") + + +def setup_environment_cache(ctx, param, value): + """Click option callback to setup environment cache""" + + if not value: + environments = ctx.meta["environments"] + if not os.path.exists(environments): + dump_environments(environments) + + +refresh_environment_cache_flag = click.option( + "--no-check-env", + is_flag=True, + expose_value=False, + default=False, + help="Do not check for environment cache", + callback=setup_environment_cache, +) + + START_EPILOG = """\b Example: $ beat editor start @@ -62,14 +90,26 @@ Example: @with_plugins(pkg_resources.iter_entry_points("beat.editor.cli")) @click.group(cls=AliasedGroup) +@click.option( + "--environments", + "-e", + help="Overrides the path to the environment file. If not set use the value from your RC file [default: /.environments.json]", + type=click.STRING, + default=".environments.json", +) @verbosity_option() @click.pass_context -def editor(ctx): +def editor(ctx, environments): """beat.editor commands.""" config = ctx.meta["config"] config.set("prefix", os.path.abspath(config.path)) + if not os.path.isabs(environments): + environments = os.path.abspath(os.path.join(config.prefix, environments)) + + ctx.meta["environments"] = environments + global logger logger = setup_logger("beat.editor", ctx.meta["verbosity"]) logger.info("BEAT prefix set to `%s'", ctx.meta["config"].path) @@ -82,6 +122,7 @@ def editor(ctx): @editor.command(epilog=START_EPILOG) @click.pass_context +@refresh_environment_cache_flag @raise_on_error def start(ctx): """Start the beat editor""" @@ -106,6 +147,7 @@ Example: @click.argument("asset_type") @click.argument("asset_name") @click.pass_context +@refresh_environment_cache_flag @raise_on_error def edit(ctx, asset_type, asset_name): """Edit one specific asset""" @@ -117,7 +159,7 @@ def edit(ctx, asset_type, asset_name): asset_path = os.path.join( ctx.meta["config"].path, - AssetType.path(AssetType[asset_type.upper()]), + AssetType[asset_type.upper()].path, asset_name + ".json", ) @@ -125,3 +167,22 @@ def edit(ctx, asset_type, asset_name): asset_widget.show() return app.exec_() + + +ENV_REFRESH_EPILOG = """\b +Example: + $ beat editor refresh_env + +""" + + +@editor.command(epilog=ENV_REFRESH_EPILOG) +@click.pass_context +@raise_on_error +def refresh_env(ctx): + """Update environments cache""" + + environments_file_path = ctx.meta["environments"] + if os.path.exists(environments_file_path): + os.remove(environments_file_path) + dump_environments(environments_file_path) diff --git a/beat/editor/test/conftest.py b/beat/editor/test/conftest.py index 7a98125ced5fe456093e855931deb5cf34eae719..a6ad00e97ff7c8fcf1c68eb5575676cffc49ffce 100644 --- a/beat/editor/test/conftest.py +++ b/beat/editor/test/conftest.py @@ -28,6 +28,7 @@ import sys import tempfile import pytest import pkg_resources +import simplejson as json import subprocess import shutil import importlib @@ -45,6 +46,7 @@ else: prefix = os.path.join(prefix_folder, "prefix") +environment_file = os.path.join(prefix, "environment.json") @pytest.fixture( @@ -64,6 +66,16 @@ def test_prefix(): for path in prefixes: subprocess.check_call(["rsync", "-arz", path, prefix_folder]) + with open(environment_file, "wt") as env_file: + env_file.write( + json.dumps( + { + "Test Env": {"name": "Python 2.7", "version": "1.3.0"}, + "Another test Env": {"name": "another test", "version": "1.1.1"}, + } + ) + ) + yield prefix shutil.rmtree(prefix_folder) @@ -84,7 +96,13 @@ def test_prefix(): @pytest.fixture(scope="module") def beat_context(test_prefix): Context = namedtuple("Context", ["meta"]) - context = Context(meta={"--user": "user", "--prefix": test_prefix}) + context = Context( + meta={ + "--user": "user", + "--prefix": test_prefix, + "environments": environment_file, + } + ) config = Configuration(context.meta) context.meta["config"] = config