def setup_global_engine(engine_override): """ Always make sure the global engine is installed and that we have at least one engine installed """ engine_store.load_engines(locations=["installed", "global"]) try: engine_store.get_engine(ENGINE_GLOBAL_NAME, locations=["installed"]) except (HamletEngineInvalidVersion, EngineStoreMissingEngineException) as e: # If the global engine is old then we need to force it to be the latest if isinstance(e, HamletEngineInvalidVersion): engine_store.clean_engine(ENGINE_GLOBAL_NAME) engine_store.get_engine(ENGINE_GLOBAL_NAME, locations=["global"]).install() engine_store.load_engines(locations=["installed", "global"], refresh=True) # Force an update to the global engine if required if (engine_store.get_engine(ENGINE_GLOBAL_NAME, locations=[ "installed" ]).digest != engine_store.get_engine(ENGINE_GLOBAL_NAME, locations=["global"]).digest): engine_store.get_engines(ENGINE_GLOBAL_NAME, locations=["global"]).install() engine_store.load_engines(locations=["installed"], refresh=True) if engine_store.global_engine is None: default_engine = (engine_override if engine_override is not None else ENGINE_DEFAULT_GLOBAL_ENGINE) try: engine_store.get_engine(default_engine, locations=["installed"]) except EngineStoreMissingEngineException: engine_store.load_engines(locations=["local", "remote", "hidden"]) engine_store.get_engine(default_engine, locations=["local", "remote", "hidden"]).install() engine_store.load_engines(locations=["installed"], refresh=True) engine_store.global_engine = default_engine
def get_engine_env(engine_override): """ Inject the hamlet engine environment variables into the context """ engine_name = engine_override if engine_override else ENGINE_GLOBAL_NAME engine = engine_store.get_engine(engine_name, locations=["installed"]) HAMLET_GLOBAL_CONFIG.engine_environment = engine.environment
def set_engine(name): """ Sets the global engine """ engine = engine_store.get_engine(name, locations=["installed"]) engine_store.global_engine = engine.name click.echo(f"[*] global engine set to {engine.name}")
def install_engine(opts, name, location, update): """ Install an engine """ engine_store.load_engines(locations=location, refresh=True) if name is None and opts.engine: name = opts.engine if name is None: raise click.exceptions.BadParameter( "Engine name missing - provide name with HAMLET_ENGINE or as an argument" ) try: engine = engine_store.get_engine(name, locations=location) except HamletEngineInvalidVersion as e: click.secho( ( "[!] The state of this engine is not compatible with the cli\n" f"[!] Remove the engine using\n" f" hamlet engine clean-engine {name}\n" "[!] then install the engine again" ), err=True, fg="red", ) raise e try: installed_engine = engine_store.get_engine(name, locations=["installed"]) except EngineStoreMissingEngineException: installed_engine = False pass if installed_engine and update: engine.install() click.echo(f"[*] updating engine | {name} | digest: {engine.digest}") elif not installed_engine: engine.install() click.echo(f"[*] installing engine | {name} | digest: {engine.digest}") else: click.echo(f"[*] skipping engine update | {name} | digest: {engine.digest}")
def env(opts, environment_variable): """ Get the environment variables for the current engine """ engine_name = opts.engine if opts.engine else ENGINE_GLOBAL_NAME engine = engine_store.get_engine(engine_name, locations=["installed"]) if environment_variable is None: click.echo("# run eval $(hamlet engine env) to set variables") for k, v in engine.environment.items(): click.echo(f'export {k}="{v}"') else: try: click.echo(engine.environment[environment_variable]) except KeyError: click.echo("")
def get_engine(opts): """ Gets the current global engine """ name = opts.engine if opts.engine else engine_store.global_engine click.echo(engine_store.get_engine(name, locations=["installed"]).name)
def describe_engine(opts, name, location): """ Provides a detailed description of an engine """ location = None if not location else [location] if name: engine_name = name elif opts.engine: engine_name = opts.engine location = ["installed"] else: engine_name = engine_store.global_engine location = ["installed"] engine_store.load_engines(locations=location, refresh=True) engines = [ engine for engine in engine_store.get_engines(locations=location) if engine.name == engine_name ] if len(engines) > 1: extra_engines = "\n".join( [ f" - name: {engine.name} - location: {engine.location}" for engine in engines ] ) raise click.exceptions.UsageError( ( f"Multiple Engines found for the provided name: {engine_name}\n" f"{extra_engines}\n" "run the command with the --location option to pick an engine" ) ) else: engine = engine_store.get_engine(engine_name, locations=location) engine_details = { "engine": { "name": engine.name, "description": engine.description, "location": engine.location, "engine_dir": engine.engine_dir, "digest": engine.digest, }, "environment": engine.environment, "install_state": engine.install_state, } sources = [] for source in engine.sources: try: source_digest = source.digest except BaseException as e: click.secho( f"[!] Source check failed {engine.name} - {source.name}", fg="red", err=True, ) click.secho(f"[!] {e}", fg="red", err=True) source_digest = None pass sources.append( { "name": source.name, "description": source.description, "latest_digest": source_digest, } ) parts = [] for part in engine.parts: parts.append( { "type": part.type, "description": part.description, "source_path": part.source_path, "source_name": part.source_name, } ) engine_details["sources"] = sources engine_details["parts"] = parts click.echo(json.dumps(engine_details, indent=2))