예제 #1
0
def create_tree_of_packages_dependencies(dist_tree, packages_names, req_file_path):
    """Create packages dependencies tree
    :param dict tree: the package tree
    :param set packages_names: set of select packages to be shown in the output.
    :param req_file_path: the path to requirements.txt file
    :rtype: dict
    """
    DEPENDENCIES = 'dependencies'
    FROM = 'from'
    VERSION = 'version'
    NAME = 'name'
    VERSION_SEPARATOR = '@'
    DIR_VERSION = '0.0.0'
    PACKAGE_FORMAT_VERSION = 'packageFormatVersion'

    tree = utils.sorted_tree(dist_tree)
    nodes = tree.keys()
    key_tree = dict((k.key, v) for k, v in tree.items())

    def get_children(n): return key_tree.get(n.key, [])
    packages_as_dist_obj = [
        p for p in nodes if p.key in packages_names or p.project_name in packages_names]

    def create_children_recursive(root_package, key_tree):
        children_packages_as_dist = key_tree[root_package[NAME].lower()]
        for child_dist in children_packages_as_dist:
            child_package = {NAME: child_dist.project_name, VERSION: child_dist.installed_version,
                             FROM: root_package[FROM] +
                             [child_dist.key + VERSION_SEPARATOR +
                              child_dist.installed_version]}
            create_children_recursive(child_package, key_tree)
            root_package[DEPENDENCIES] = {
                child_dist.project_name: child_package}
        return root_package

    def create_dir_as_root():
        name = os.path.basename(os.path.dirname(os.path.abspath(req_file_path)))
        dir_as_root = { NAME: name, VERSION: DIR_VERSION,
                       FROM: [name + VERSION_SEPARATOR + DIR_VERSION], DEPENDENCIES: {},
                       PACKAGE_FORMAT_VERSION: 'pip:0.0.1'}
        return dir_as_root

    def create_package_as_root(package, dir_as_root):
        package_as_root = {NAME: package.project_name.lower(), VERSION: package._obj._version,
                           FROM: ["{}{}{}".format(dir_as_root[NAME], VERSION_SEPARATOR, dir_as_root[VERSION])] +
                           ["{}{}{}".format(package.project_name.lower(),
                                            VERSION_SEPARATOR, package._obj._version)]}
        return package_as_root
    dir_as_root = create_dir_as_root()
    for package in packages_as_dist_obj:
        package_as_root = create_package_as_root(package, dir_as_root)
        package_tree = create_children_recursive(package_as_root, key_tree)
        dir_as_root[DEPENDENCIES][package_as_root[NAME]] = package_tree
    return dir_as_root
예제 #2
0
def create_tree_of_packages_dependencies(dist_tree,
                                         packages_names,
                                         req_file_path,
                                         allow_missing=False):
    """Create packages dependencies tree
    :param dict tree: the package tree
    :param set packages_names: set of select packages to be shown in the output.
    :param req_file_path: the path to the dependencies file
                          (e.g. requirements.txt)
    :rtype: dict
    """
    DEPENDENCIES = 'dependencies'
    VERSION = 'version'
    NAME = 'name'
    VERSION_SEPARATOR = '@'
    DIR_VERSION = '0.0.0'
    PACKAGE_FORMAT_VERSION = 'packageFormatVersion'

    tree = utils.sorted_tree(dist_tree)
    nodes = tree.keys()
    key_tree = dict((k.key, v) for k, v in tree.items())

    def get_children(n):
        return key_tree.get(n.key, [])

    lowercase_pkgs_names = [p.lower() for p in packages_names]
    packages_as_dist_obj = [
        p for p in nodes if p.key.lower() in lowercase_pkgs_names or (
            p.project_name and p.project_name.lower()) in lowercase_pkgs_names
    ]

    def create_children_recursive(root_package, key_tree, ancestors):
        root_name = root_package[NAME].lower()
        if root_name not in key_tree:
            msg = 'Required package missing: ' + root_name
            if allow_missing:
                sys.stderr.write(msg + "\n")
                return
            else:
                sys.exit(msg)

        ancestors = ancestors.copy()
        ancestors.add(root_name)
        children_packages_as_dist = key_tree[root_name]
        for child_dist in children_packages_as_dist:
            if child_dist.project_name.lower() in ancestors:
                continue

            child_package = {
                NAME: child_dist.project_name,
                VERSION: child_dist.installed_version,
            }

            create_children_recursive(child_package, key_tree, ancestors)
            if DEPENDENCIES not in root_package:
                root_package[DEPENDENCIES] = {}
            root_package[DEPENDENCIES][child_dist.project_name] = child_package
        return root_package

    def create_dir_as_root():
        name = os.path.basename(os.path.dirname(
            os.path.abspath(req_file_path)))
        dir_as_root = {
            NAME: name,
            VERSION: DIR_VERSION,
            DEPENDENCIES: {},
            PACKAGE_FORMAT_VERSION: 'pip:0.0.1'
        }
        return dir_as_root

    def create_package_as_root(package, dir_as_root):
        package_as_root = {
            NAME: package.project_name.lower(),
            VERSION: package._obj._version,
        }
        return package_as_root

    dir_as_root = create_dir_as_root()
    for package in packages_as_dist_obj:
        package_as_root = create_package_as_root(package, dir_as_root)
        package_tree = create_children_recursive(package_as_root, key_tree,
                                                 set([]))
        dir_as_root[DEPENDENCIES][package_as_root[NAME]] = package_tree
    return dir_as_root
예제 #3
0
def create_tree_of_packages_dependencies(dist_tree,
                                         top_level_requirements,
                                         req_file_path,
                                         allow_missing=False,
                                         only_provenance=False):
    """Create packages dependencies tree
    :param dict tree: the package tree
    :param set packages_names: set of select packages to be shown in the output.
    :param req_file_path: the path to the dependencies file
                          (e.g. requirements.txt)
    :rtype: dict
    """
    DEPENDENCIES = 'dependencies'
    VERSION = 'version'
    NAME = 'name'
    DIR_VERSION = '0.0.0'
    PACKAGE_FORMAT_VERSION = 'packageFormatVersion'
    LABELS = 'labels'
    PROVENANCE = 'provenance'

    tree = utils.sorted_tree(dist_tree)
    nodes = tree.keys()
    key_tree = dict((k.key, v) for k, v in tree.items())

    lowercase_pkgs_names = [p.name.lower() for p in top_level_requirements]
    tlr_by_key = dict(
        (tlr.name.lower(), tlr) for tlr in top_level_requirements)
    packages_as_dist_obj = [
        p for p in nodes if p.key.lower() in lowercase_pkgs_names or (
            p.project_name and p.project_name.lower()) in lowercase_pkgs_names
    ]

    def create_children_recursive(root_package, key_tree, ancestors):
        root_name = root_package[NAME].lower()
        if root_name not in key_tree:
            msg = 'Required packages missing: ' + root_name
            if allow_missing:
                sys.stderr.write(msg + "\n")
                return
            else:
                sys.exit(msg)

        ancestors = ancestors.copy()
        ancestors.add(root_name)
        children_packages_as_dist = key_tree[root_name]
        for child_dist in children_packages_as_dist:
            if child_dist.project_name.lower() in ancestors:
                continue

            child_package = {
                NAME: child_dist.project_name,
                VERSION: child_dist.installed_version,
            }

            create_children_recursive(child_package, key_tree, ancestors)
            if DEPENDENCIES not in root_package:
                root_package[DEPENDENCIES] = {}
            root_package[DEPENDENCIES][child_dist.project_name] = child_package
        return root_package

    def create_dir_as_root():
        name, version = None, None
        if os.path.basename(req_file_path) == 'setup.py':
            with open(req_file_path, "r") as setup_py_file:
                name, version = setup_file.parse_name_and_version(
                    setup_py_file.read())

        dir_as_root = {
            NAME:
            name or os.path.basename(
                os.path.dirname(os.path.abspath(req_file_path))),
            VERSION:
            version or DIR_VERSION,
            DEPENDENCIES: {},
            PACKAGE_FORMAT_VERSION:
            'pip:0.0.1'
        }
        return dir_as_root

    def create_package_as_root(package, dir_as_root):
        package_as_root = {
            NAME: package.project_name.lower(),
            # Note: _version is a private field.
            VERSION: package._obj._version,
        }
        return package_as_root

    dir_as_root = create_dir_as_root()
    for package in packages_as_dist_obj:
        package_as_root = create_package_as_root(package, dir_as_root)
        if only_provenance:
            package_as_root[LABELS] = {
                PROVENANCE:
                format_provenance_label(
                    tlr_by_key[package_as_root[NAME]].provenance)
            }
            dir_as_root[DEPENDENCIES][package_as_root[NAME]] = package_as_root
        else:
            package_tree = create_children_recursive(package_as_root, key_tree,
                                                     set([]))
            dir_as_root[DEPENDENCIES][package_as_root[NAME]] = package_tree
    return dir_as_root
예제 #4
0
def create_tree_of_packages_dependencies(dist_tree, packages_names, req_file_path, allow_missing=False):
    """Create packages dependencies tree
    :param dict tree: the package tree
    :param set packages_names: set of select packages to be shown in the output.
    :param req_file_path: the path to requirements.txt file
    :rtype: dict
    """
    DEPENDENCIES = 'dependencies'
    FROM = 'from'
    VERSION = 'version'
    NAME = 'name'
    VERSION_SEPARATOR = '@'
    DIR_VERSION = '0.0.0'
    PACKAGE_FORMAT_VERSION = 'packageFormatVersion'

    tree = utils.sorted_tree(dist_tree)
    nodes = tree.keys()
    key_tree = dict((k.key, v) for k, v in tree.items())

    def get_children(n): return key_tree.get(n.key, [])
    lowercase_pkgs_names = [p.lower() for p in packages_names]
    packages_as_dist_obj = [
        p for p in nodes if
            p.key.lower() in lowercase_pkgs_names or
            (p.project_name and p.project_name.lower()) in lowercase_pkgs_names]

    def create_children_recursive(root_package, key_tree, ancestors):
        root_name = root_package[NAME].lower()
        if root_name not in key_tree:
            msg = 'Required package missing: ' + root_name
            if allow_missing:
                sys.stderr.write(msg + "\n")
                return
            else:
                sys.exit(msg)

        ancestors = ancestors.copy()
        ancestors.add(root_name)
        children_packages_as_dist = key_tree[root_name]
        for child_dist in children_packages_as_dist:
            if child_dist.project_name.lower() in ancestors:
                continue

            child_package = {
                NAME: child_dist.project_name,
                VERSION: child_dist.installed_version,
                FROM: root_package[FROM] +
                      [child_dist.key + VERSION_SEPARATOR + child_dist.installed_version]
            }

            create_children_recursive(child_package, key_tree, ancestors)
            if DEPENDENCIES not in root_package:
                root_package[DEPENDENCIES] = {}
            root_package[DEPENDENCIES][child_dist.project_name] = child_package
        return root_package

    def create_dir_as_root():
        name = os.path.basename(os.path.dirname(os.path.abspath(req_file_path)))
        dir_as_root = {
            NAME: name,
            VERSION: DIR_VERSION,
            DEPENDENCIES: {},
            FROM: [name + VERSION_SEPARATOR + DIR_VERSION], DEPENDENCIES: {},
            PACKAGE_FORMAT_VERSION: 'pip:0.0.1'
        }
        return dir_as_root

    def create_package_as_root(package, dir_as_root):
        package_as_root = {
            NAME: package.project_name.lower(),
            VERSION: package._obj._version,
            FROM: ["{}{}{}".format(
                dir_as_root[NAME], VERSION_SEPARATOR, dir_as_root[VERSION])] +
                  ["{}{}{}".format(
                package.project_name.lower(), VERSION_SEPARATOR, package._obj._version)]
        }
        return package_as_root
    dir_as_root = create_dir_as_root()
    for package in packages_as_dist_obj:
        package_as_root = create_package_as_root(package, dir_as_root)
        package_tree = create_children_recursive(package_as_root, key_tree, set([]))
        dir_as_root[DEPENDENCIES][package_as_root[NAME]] = package_tree
    return dir_as_root