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
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)
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
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
def tree_command(args, packages=None, exit_on_failure=True): """Display dependency tree for a single package or the entire environment :param args: Command arguments :param packages: Collection of packages :param exit_on_failure: Enable/disable exiting application on failure :return: None """ package_key = args.package 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)) package_string = '{package} [{installed}: {version}]' requirement_string = ( '{spacing}{package} [{installed}: {version} | {required}: {spec}]') packages = (packages or dependency_list(ignore_list=ignore_list)) if package_key: package = packages.get(key=package_key) if not package: printer.error( messages.PACKAGE_NOT_FOUND.format(package=package_key)) sys.exit(1) tree = {package: package_dependency_tree(dependency=package)} else: tree = dependency_tree( packages=packages, requirements=requirements if requirements else None) if not tree: printer.info(messages.PACKAGES_NOT_FOUND) def print_dependency_tree(requirements_list, indent=0): spacing = ' ' * indent for requirement in requirements_list: printer.info( requirement_string.format( spacing=spacing, package=printer.colored_message( message=requirement.key, message_color=printer.color_package), installed=messages.INSTALLED, version=requirement.version_id, required=messages.REQUIRED, spec=requirement.specified_version), ) print_dependency_tree(requirements_list[requirement], indent + 2) for dependency in tree: printer.info( package_string.format(package=printer.colored_message( message=dependency.key, message_color=printer.color_package), installed=messages.INSTALLED, version=dependency.version)) print_dependency_tree(tree[dependency], indent=2) return True
def validate_command(args, packages=None, exit_on_failure=True): """Runs requirement file validation :param args: Command arguments :param packages: Collection of packages :param exit_on_failure: Enable/disable exiting application on failure :return: None """ strict = args.strict or False ignore_list = ( args.ignore or Config.ignore_list or [] ) requirements_files = ( args.requirements or Config.requirements_files or [] ) lock_files = ( args.lock or Config.lock_files or [] ) printer = Printer() if not validate_files( files=requirements_files, printer=printer, exit_on_failure=exit_on_failure ) or not validate_files( files=lock_files, printer=printer, exit_on_failure=exit_on_failure): return False try: requirements = RequirementCollection.from_files( filepaths=requirements_files ) locked = RequirementCollection.from_files(filepaths=lock_files) except Exception as e: # Always exit on invalid requirements printer.error('{}: {}'.format(messages.REQUIREMENTS_PARSING_ERROR, e)) sys.exit(1) checks_ok = [] packages = ( packages or dependency_list(ignore_list=ignore_list) ) checks_ok.append(check_unlocked_requirements( requirements=requirements, printer=printer, )) checks_ok.append(check_unset_locks( requirements=requirements, locked=locked, printer=printer, )) checks_ok.append(check_package_version_mismatch( packages=packages, locked=locked, printer=printer, )) checks_ok.append(check_requirement_version_mismatch( requirements=requirements, locked=locked, printer=printer, )) if strict: checks_ok.append(check_unnecessary_packages( packages=packages, requirements=requirements, locked=locked, printer=printer, )) checks_ok.append(check_unnecessary_locks( requirements=requirements, locked=locked, ignore_list=ignore_list, printer=printer, )) return ( sys.exit(1) if exit_on_failure else False if not all(checks_ok) else True )