Commit 1a5a2f29 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[scripts][editor_cli] Add environment caching

In order to avoid having the reload again and again the docker
environment available, create a cache file if not existing on
startup.

This also add a command to manually refresh the cache if needed.
parent 04ff532c
......@@ -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,13 @@ 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")
START_EPILOG = """\b
Example:
$ beat editor start
......@@ -62,18 +71,33 @@ 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: <prefix>/.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)
if not os.path.exists(environments):
dump_environments(environments)
QCoreApplication.setApplicationName("beat.editor")
QCoreApplication.setOrganizationName("Idiap")
QCoreApplication.setOrganizationDomain("ch.idiap")
......@@ -117,7 +141,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 +149,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)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment