Ejemplo n.º 1
0
Archivo: cyclic.py Proyecto: sbg/dante
def cyclic_command(args, packages=None, exit_on_failure=True):
    """Runs detection of cyclical dependencies
    :param args: Command arguments
    :param packages: Collection of packages
    :param exit_on_failure: Enable/disable exiting application on failure
    :return: None
    """
    ignore_list = (
        args.ignore or Config.ignore_list or []
    )

    printer = Printer()
    packages = (
        packages or dependency_list(ignore_list=ignore_list)
    )

    cyclic_paths = cyclic_dependencies(packages=packages)

    if cyclic_paths:
        printer.error(messages.CYCLIC_FOUND)
        for path in cyclic_paths:
            if path:
                path.append(path[0])
                path_message = ' -> '.join(map(lambda p: p.key, path))
                printer.info(message=path_message)
        if exit_on_failure:
            sys.exit(1)
        return False

    printer.success(messages.CYCLIC_OK)
    return True
Ejemplo n.º 2
0
def graph_command(args, packages=None, exit_on_failure=True):
    """Return or render a dependency graph
    :param args: Command arguments
    :param packages: Collection of packages
    :param exit_on_failure: Enable/disable exiting application on failure
    :return: None
    """
    ignore_list = (args.ignore or Config.ignore_list or [])

    name = args.name or Config.graph_name
    filename = args.filename or Config.graph_filename
    file_format = args.format or Config.graph_format
    engine = args.engine or Config.graph_engine
    strict = args.strict or Config.graph_strict
    graph_attr = args.graph_attr or Config.graph_attributes
    node_attr = args.node_attr or Config.graph_node_attributes
    edge_attr = args.edge_attr or Config.graph_edge_attributes
    view = args.view or False
    render = args.render or False

    printer = Printer()
    packages = (packages or dependency_list(ignore_list=ignore_list))

    graph = get_graph(
        packages=packages,
        name=name,
        filename=filename,
        file_format=file_format,
        engine=engine,
        strict=strict,
        graph_attr=graph_attr,
        node_attr=node_attr,
        edge_attr=edge_attr,
    )

    if render:
        try:
            filepath = render_graph(graph=graph, view=view)
            printer.success(
                messages.GRAPH_EXPORTED.format(file_path=filepath,
                                               format=file_format))
        except Exception as e:
            printer.error(messages.GRAPH_FAILED_TO_RENDER.format(error=e))
            return sys.exit(1) if exit_on_failure else ''

    else:
        printer.print_message(message=graph)
Ejemplo n.º 3
0
def lock_command(args, packages=None, exit_on_failure=True):
    """Display or save locked requirements for current environment
    :param args: Command arguments
    :param packages: Collection of packages
    :param exit_on_failure: Enable/disable exiting application on failure
    :return: None
    """
    requirements_files = (args.requirements or Config.requirements_files or [])
    ignore_list = (args.ignore or Config.ignore_list or [])
    save_lock = args.save or False
    lock_filepath = args.file or Config.lock_file_path

    printer = Printer()
    if not validate_files(files=requirements_files,
                          printer=printer,
                          exit_on_failure=exit_on_failure):
        return False

    requirements = RequirementCollection()
    for requirements_file in requirements_files:
        requirements.extend(
            RequirementCollection.from_file(filepath=requirements_file))

    lock_ignore_list = [
        key for key in ignore_list if key not in requirements.keys()
    ]

    packages = (packages or dependency_list(ignore_list=lock_ignore_list))

    locked = locked_requirements(packages=packages, requirements=requirements)

    if save_lock:
        locked.save_lock_file(filepath=lock_filepath)
        printer.success(message=messages.LOCK_EXPORTED.format(
            file_path=lock_filepath))
    else:
        for item in locked:
            printer.info("{}=={}".format(
                printer.colored_message(item.key, color.DEFAULT_PACKAGE),
                item.version_id))

    return True
Ejemplo n.º 4
0
def conflicts_command(args, packages=None, exit_on_failure=True):
    """Runs detection of dependency conflicts
    :param args: Command arguments
    :param packages: Collection of packages
    :param exit_on_failure: Enable/disable exiting application on failure
    :return: None
    """
    ignore_list = (args.ignore or Config.ignore_list or [])

    printer = Printer()
    packages = (packages or dependency_list(ignore_list=ignore_list))

    conflicting = [
        (package, required_by)
        for package, required_by in conflicting_dependencies(packages=packages)
    ]

    headers = [
        messages.PACKAGE,
        messages.INSTALLED,
        messages.REQUIRED,
        messages.REQUIRED_BY,
    ]

    tabular_data = [[
        printer.colored_message(message=package.key,
                                message_color=printer.color_package),
        package.version_id,
        required_version,
        required_by.key,
    ] for package, requirement in conflicting
                    for required_by, required_version in requirement]

    if tabular_data:
        printer.error(messages.CONFLICTS_FOUND)
        printer.table(headers=headers, tabular_data=tabular_data)
        if exit_on_failure:
            sys.exit(1)
        return False

    printer.success(messages.CONFLICTS_OK)
    return True
Ejemplo n.º 5
0
def missing_requirements_command(args, packages=None, exit_on_failure=True):
    """Runs detection of required packages that are not installed
    :param args: Command arguments
    :param packages: Collection of packages
    :param exit_on_failure: Enable/disable exiting application on failure
    :return: None
    """
    requirements_files = (
        args.requirements or Config.requirements_files or []
    )
    ignore_list = (
        args.ignore or Config.ignore_list or []
    )

    printer = Printer()
    if not validate_files(
            files=requirements_files,
            printer=printer,
            exit_on_failure=exit_on_failure):
        return False

    requirements = RequirementCollection()
    for requirements_file in requirements_files:
        requirements.extend(
            RequirementCollection.from_file(filepath=requirements_file)
        )

    packages = (
        packages or dependency_list(ignore_list=ignore_list)
    )

    missing = [
        (package, required_by)
        for package, required_by
        in missing_requirements(
            packages=packages,
            requirements=requirements,
            ignore_list=ignore_list
        )
    ]

    headers = [
        messages.PACKAGE,
        messages.REQUIRED,
        messages.REQUIRED_BY,
    ]

    tabular_data = []
    for package, requirers in missing:
        if requirers:
            for required_by, required_version in requirers:
                tabular_data.append([
                    printer.colored_message(
                        message=package.key,
                        message_color=printer.color_package
                    ),
                    required_version,
                    required_by.key,
                ])
        else:
            tabular_data.append([
                printer.colored_message(
                    message=package.key,
                    message_color=printer.color_package
                ),
                package.version.specifier,
                "Requirements",
            ])

    if tabular_data:
        printer.error(messages.MISSING_FOUND)
        printer.table(headers=headers, tabular_data=tabular_data)
        if exit_on_failure:
            sys.exit(1)
        return False

    printer.success(messages.MISSING_OK)
    return True