Source code for lupkg.lupli_cli

# -*- coding: utf-8 -*-
"""Console script for lupli."""

import logging
import os
import sys

import click
import click_completion
import click_log

from frkl import load_string_from_url_or_path
from frutils import SUPPORTED_OUTPUT_FORMATS, flatten_lists
from frutils.frutils_cli import CursorOff, output
from .exceptions import LupkgMetadataException, LupkgProcessingException
from .lupkg_list import LupList
from .lupkg_config import LUPKG_INDEXES
from .utils import ensure_base_path, handle_metadata_exception

log = logging.getLogger("lucify")
click_log.basic_config(log)

# optional shell completion
click_completion.init()

DEFAULT_TARGET_PATH = os.path.expanduser("~/.local/bin")


[docs]def click_stdout(message): click.echo(message)
@click.group() @click.option( "--index", "-i", multiple=True, default=["default"], help="the lupkg index to use" ) @click.option( "--target-path", "-t", default=None, multiple=False, help="the local target folder" ) @click.option( "--output-format", "-o", help="output format", default="default", required=False, type=click.Choice(["default"] + SUPPORTED_OUTPUT_FORMATS), ) @click_log.simple_verbosity_option(log, "--verbosity", default="WARN") @click.pass_context def cli(ctx, index, target_path, output_format): """lupli manages lists of lupkgs """ click.echo("") ctx.obj = {} ctx.obj["output_format"] = output_format if not index: index = "default" try: ctx.obj["index"] = LUPKG_INDEXES.get_index(index) except (Exception) as e: # raise click.ClickException(e) log.debug(e, exc_info=True) raise e ctx.obj["output"] = click_stdout if target_path is None: target_path = DEFAULT_TARGET_PATH ctx.obj["target_path"] = target_path # if metadata is None: # metadata = os.path.join( # os.path.realpath(os.path.expanduser(target_path)), # LUPKG_FOLDER_METADATA_FILE_NAME, # ) # ctx.obj["metadata_path"] = metadata @cli.command(name="install", short_help="installs all packages in a list") @click.option("--force", "-f", help="overwrite existing files", is_flag=True) @click.option( "--property", "-p", help="a key/value pair describing an additional property to be used to select the right version of this package (e.g. 'arch Linux64bit')", metavar="KEY VALUE", nargs=2, multiple=True, ) @click.argument("luplists", metavar="PKG_LIST", nargs=-1) @click.pass_context def install(ctx, luplists, force, property): index = ctx.obj["index"] output_format = ctx.obj["output_format"] properties = {} for p in property: properties[p[0]] = p[1] target_path = ctx.obj["target_path"] target_path = ensure_base_path(target_path, create_folder=True) pl = load_string_from_url_or_path(luplists, create_python_object=True) pl = flatten_lists(pl) meta = {"path": target_path, "index": index} luplist = LupList(pl, meta=meta) click.echo() result = [] result_files = [] with CursorOff(): for lupkg_item in luplist.tasklist: try: if output_format == "default": click.echo("- installing package: {}".format(lupkg_item.pkg_name)) install_result = lupkg_item.install( pkg_properties=properties, install_properties={"force": force} ) if install_result: result.append(install_result) result_files.extend(install_result.get("installed_files", [])) except (LupkgProcessingException) as epe: click.echo("Error installing package: {}".format(lupkg_item.pkg_name)) click.echo(" => {}".format(epe.msg)) sys.exit(1) except (LupkgMetadataException) as eme: click.echo("\nError installing package: ", nl=False) click.secho(lupkg_item.pkg_name, bold=True) handle_metadata_exception(eme) if result_files: if output_format == "default": click.echo("\nInstalled:") for r in result_files: click.echo(" - {}".format(r)) else: output(result_files, output_type=output_format) else: if output_format == "default": click.echo("Nothing to do.") else: output([], output_type=output_format) if __name__ == "__main__": sys.exit(cli()) # pragma: no cover