예제 #1
0
def venv_fixture(pickle_file):
    """Loads required virtualenv pkg data from a pickle file

    :param pickle_file: path to a .pickle file
    :returns: a tuple of pkgs, pkg_index, req_map
    :rtype: tuple

    """
    with open(pickle_file, 'rb') as f:
        pkgs = pickle.load(f)
        dist_index = build_dist_index(pkgs)
        tree = construct_tree(dist_index)
        return pkgs, dist_index, tree
예제 #2
0
def venv_fixture(pickle_file):
    """Loads required virtualenv pkg data from a pickle file

    :param pickle_file: path to a .pickle file
    :returns: a tuple of pkgs, pkg_index, req_map
    :rtype: tuple

    """
    with open(pickle_file, 'rb') as f:
        pkgs = pickle.load(f)
        dist_index = build_dist_index(pkgs)
        tree = construct_tree(dist_index)
        return pkgs, dist_index, tree
예제 #3
0
def build_dep_list(package_name, local_only=True):
    """Builds a dependency list for the given package, assuming the given package has already been
    installed. The dependency list is provided in the order in which it needs to be installed.

    Args:
        package_name: The name of the package for which to build a dependency tree.
        local_only: Look in local distribution? Default: True.

    Returns:
        A Python list containing information about the dependencies for that package.
    """
    ## Dealing with pip 10.* api chagnes
    ## The solution is from:
    ## https://github.com/naiquevin/pipdeptree/blob/master/pipdeptree.py
    try:
        from pip._internal.utils.misc import get_installed_distributions
    except ImportError:
        from pip import get_installed_distributions
    packages = get_installed_distributions(local_only=local_only)
    dist_index = build_dist_index(packages)
    tree = sorted_tree(construct_tree(dist_index))
    nodes = tree.keys()
    # filter by our desired package only
    nodes = [p for p in nodes if p.key == package_name or p.project_name == package_name]
    if len(nodes) == 0:
        raise PackageNotFoundError(package_name)
    if len(nodes) > 1:
        raise MultiplePackagesFoundError(package_name)

    key_tree = dict((k.key, v) for k, v in iteritems(tree))
    deps = recursive_extract_dep_list(key_tree, package_name)

    unique_deps = []
    seen_deps = set()
    for dep in deps:
        if dep.key not in seen_deps:
            unique_deps.append(dep.as_dict())
            seen_deps.add(dep.key)
        else:
            logger.debug("Duplicate dependency found: %s" % dep.project_name)
    return unique_deps
예제 #4
0
def get_virtual_dependencies_of_packages():
    """
    Return information about installed dependencies in the virtualenv of application

    example of json result :

    {"dependencies": [{"required_version": null,
        "installed_version": "1.8.13", "package_name": "Django", "key": "django"}],
    "package_name": "django-ckeditor"}
    """
    packages = pkg_resources.working_set
    dist_index = pipdeptree.build_dist_index(packages)
    tree = pipdeptree.construct_tree(dist_index)

    packages_dict = {}
    locations = []
    for p in packages:
        packages_dict[p.key] = p
        locations.append(p.location)

    most_common = Counter(locations).most_common(1)[0][0]
    print("most common loc")
    print(most_common)

    ret = [{
        'package_name': k.as_dict()['key'],
        'dependencies': [v.as_dict() for v in vs]
    } for k, vs in tree.items()]
    for r in ret:
        r["package"] = packages_dict[r["package_name"]]
        r["dependency_for"] = []
        if r["package"].location == most_common:
            r["default_location"] = True
        for rr in ret:
            if r['package'].key in [d['key'] for d in rr["dependencies"]]:
                r["dependency_for"].append(rr["package_name"])

    sortedret = sorted(ret,
                       key=(lambda x:
                            (len(x["dependencies"]), x["package_name"])))
    return most_common, sortedret
예제 #5
0
def get_package_tree(ignore_list=None, include_only=None):
    """Returns dependency package tree
    :param ignore_list: list of dependencies to exclude from tree
    :param include_only: list of dependencies to include if
    :return: dictionary of top level packages with their dependencies
    """
    ignore_list = [i.lower() for i in ignore_list] if ignore_list else []
    include_only = [i.lower() for i in include_only] if include_only else []
    packages = [
        package for package in pip.get_installed_distributions()
        if package.key not in ignore_list
    ]

    # if include_only is set, remove other packages
    if include_only:
        packages = [
            package for package in packages if package.key in include_only
        ]

    dist_index = pipdeptree.build_dist_index(pkgs=packages)
    tree = pipdeptree.construct_tree(index=dist_index)
    return tree
예제 #6
0
def read_installed(verbose=True):
    cwd = os.getcwd()
    if verbose:
        try:
            # virtual environment with venv in python 3.3+
            from site import getsitepackages
            site_package_dirs = getsitepackages()
        except ImportError:
            # virtual environment with virtualenv
            # https://github.com/pypa/virtualenv/issues/228
            from distutils.sysconfig import get_python_lib
            site_package_dirs = [get_python_lib()]
        print('reading installed from:\n\t{}'.format('\n\t'.join(
            [os.path.relpath(x, cwd) for x in site_package_dirs])))
    pkgs = get_installed_distributions()
    dist_index = build_dist_index(pkgs)
    tree = construct_tree(dist_index)
    branch_keys = set(r.key for r in flatten(tree.values()))
    nodes = [p for p in tree.keys() if p.key not in branch_keys]
    project_names = set(normalize_package_name(p.project_name) for p in nodes)
    editable_packages = set(
        normalize_package_name(p.project_name) for p in nodes
        if dist_is_editable(p._obj))
    return set(project_names), editable_packages, tree
예제 #7
0
import pipdeptree
import json

pkgs = pipdeptree.get_installed_distributions()
dist_index = pipdeptree.build_dist_index(pkgs)
tree = pipdeptree.construct_tree(dist_index)
json_tree = json.loads(pipdeptree.render_json_tree(tree, indent=0))
print(json_tree)
print([
    package for package in json_tree if package['package_name'] == 'tornado'
][0])