Example #1
0
def test_unset_locks():
    # preconditions
    requirement_file_path = str(PACKAGES_DIR / 'requirements-unset.txt')
    lock_file_path = str(PACKAGES_DIR / 'requirements-unset.lock')
    requirements = RequirementCollection.from_file(
        filepath=requirement_file_path)
    locked = RequirementCollection.from_file(filepath=lock_file_path)

    # action
    unset = operations.unset_locks(requirements=requirements, locked=locked)

    # verification
    assert ['package2', 'package3'] == unset.keys()
Example #2
0
def test_lock_version_mismatch(virtualenv, mocker):
    # preconditions
    mismatch_package1_path = PACKAGES_DIR / 'mismatch' / 'mismatch-package1'
    mismatch_package2_path = PACKAGES_DIR / 'mismatch' / 'mismatch-package2'
    mismatch_package3_path = PACKAGES_DIR / 'mismatch' / 'mismatch-package3'
    requirements_file_path = PACKAGES_DIR / 'requirements-mismatch.lock'

    packages = prepare_packages(venv=virtualenv,
                                mocker=mocker,
                                packages=[
                                    mismatch_package1_path,
                                    mismatch_package2_path,
                                    mismatch_package3_path,
                                ])

    expected_mismatch = [
        (packages.get('mismatch-package2'), '==2.0.0'),
        (packages.get('mismatch-package3'), '>=3.0.0'),
    ]

    requirements = RequirementCollection.from_file(
        filepath=requirements_file_path)

    # action
    mismatch = operations.lock_version_mismatch(packages=packages,
                                                locked=requirements)

    # verification
    assert expected_mismatch == mismatch
Example #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
Example #4
0
File: list.py Project: sbg/dante
def list_command(args, packages=None, exit_on_failure=True):
    """List installed dependencies with configurable filters
    :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))

    requirements = requirements if requirements else None
    packages = (packages or dependency_list(ignore_list=ignore_list,
                                            requirements=requirements))

    headers = [messages.PACKAGE, messages.INSTALLED]
    tabular_data = [[
        printer.colored_message(message=package.key,
                                message_color=printer.color_package),
        package.version_id
    ] for package in packages]

    if tabular_data:
        printer.table(headers=headers, tabular_data=tabular_data)
    else:
        printer.info(messages.PACKAGES_NOT_FOUND)

    return True
Example #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
Example #6
0
File: tree.py Project: sbg/dante
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