Python pkg_resources.find_distributions() Examples

The following are 30 code examples of pkg_resources.find_distributions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkg_resources , or try the search function .
Example #1
Source File: package.py    From django-leonardo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def check_package_exists(package, lib_dir):
    """Check if a package is installed globally or in lib_dir.

    Returns True when the requirement is met.
    Returns False when the package is not installed or doesn't meet req.
    """
    try:
        req = pkg_resources.Requirement.parse(package)
    except ValueError:
        # This is a zip file
        req = pkg_resources.Requirement.parse(urlparse(package).fragment)

    # Check packages from lib dir
    if lib_dir is not None:
        if any(dist in req for dist in
               pkg_resources.find_distributions(lib_dir)):
            return True

    # Check packages from global + virtual environment
    # pylint: disable=not-an-iterable
    return any(dist in req for dist in pkg_resources.working_set) 
Example #2
Source File: test_setup_keywords.py    From setuptools-odoo with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_odoo_addon1(self):
        addon1_dir = os.path.join(DATA_DIR, "setup_reusable_addons", "addon1")
        subprocess.check_call([sys.executable, "setup.py", "egg_info"], cwd=addon1_dir)
        egg_info_dir = os.path.join(addon1_dir, "odoo8_addon_addon1.egg-info")
        assert os.path.isdir(egg_info_dir)
        try:
            dist = next(pkg_resources.find_distributions(addon1_dir))
            self.assertEqual(dist.key, "odoo8-addon-addon1")
            self.assertEqual(
                dist.requires(),
                [pkg_resources.Requirement.parse(r) for r in ["odoo>=8.0a,<9.0a"]],
            )
            self.assertTrue(dist.has_metadata("not-zip-safe"))
            self.assertEqual(dist.version, "8.0.1.0.0.99.dev4")
        finally:
            shutil.rmtree(egg_info_dir) 
Example #3
Source File: test_setup_keywords.py    From setuptools-odoo with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_odoo_addon2(self):
        addon2_dir = os.path.join(DATA_DIR, "setup_reusable_addons", "addon2")
        subprocess.check_call([sys.executable, "setup.py", "egg_info"], cwd=addon2_dir)
        egg_info_dir = os.path.join(addon2_dir, "odoo8_addon_addon2.egg-info")
        assert os.path.isdir(egg_info_dir)
        try:
            dist = next(pkg_resources.find_distributions(addon2_dir))
            self.assertEqual(dist.key, "odoo8-addon-addon2")
            self.assertEqual(
                dist.requires(),
                [
                    pkg_resources.Requirement.parse(r)
                    for r in [
                        "odoo8-addon-addon1",
                        "odoo>=8.0a,<9.0a",
                        "python-dateutil",
                    ]
                ],
            )
            self.assertTrue(dist.has_metadata("not-zip-safe"))
            self.assertEqual(dist.version, "8.0.1.0.1")
        finally:
            shutil.rmtree(egg_info_dir) 
Example #4
Source File: test_setup_keywords.py    From setuptools-odoo with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_odoo_addon4(self):
        addon4_dir = os.path.join(DATA_DIR, "setup_reusable_addons", "addon4")
        subprocess.check_call([sys.executable, "setup.py", "egg_info"], cwd=addon4_dir)
        egg_info_dir = os.path.join(addon4_dir, "odoo8_addon_addon4.egg-info")
        assert os.path.isdir(egg_info_dir)
        try:
            dist = next(pkg_resources.find_distributions(addon4_dir))
            self.assertEqual(dist.key, "odoo8-addon-addon4")
            self.assertEqual(
                dist.requires(),
                [
                    pkg_resources.Requirement.parse(r)
                    for r in [
                        "astropy>=1.0",
                        "odoo8-addon-addon1>=8.0.3.0.0",
                        "odoo>=8.0a,<9.0a",
                        "python-dateutil",
                    ]
                ],
            )
            self.assertTrue(dist.has_metadata("not-zip-safe"))
            self.assertEqual(dist.version, "8.0.2.0.0")
        finally:
            shutil.rmtree(egg_info_dir) 
Example #5
Source File: pundle.py    From pundler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def parse_directory(self):
        if not op.exists(self.directory):
            return {}
        dists = [
            # this magic takes first element or None
            next(iter(
                pkg_resources.find_distributions(op.join(self.directory, item), True)
            ), None)
            for item in os.listdir(self.directory) if '-' in item
        ]
        dists.extend(
            VCSDist(op.join(self.directory, item))
            for item in os.listdir(self.directory) if '+' in item
        )
        dists = filter(None, dists)
        result = defaultdict(list)
        for dist in dists:
            result[dist.key].append(dist)
        return result 
Example #6
Source File: ah_bootstrap.py    From pydis with MIT License 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                with _silence():
                    run_setup(os.path.join(path, 'setup.py'),
                              ['egg_info'])

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #7
Source File: ah_bootstrap.py    From specidentify with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                with _silence():
                    run_setup(os.path.join(path, 'setup.py'),
                              ['egg_info'])

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #8
Source File: ah_bootstrap.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                # We use subprocess instead of run_setup from setuptools to
                # avoid segmentation faults - see the following for more details:
                # https://github.com/cython/cython/issues/2104
                sp.check_output([sys.executable, 'setup.py', 'egg_info'], cwd=path)

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #9
Source File: test_find_distributions.py    From setuptools with MIT License 5 votes vote down vote up
def test_non_egg_dir_named_egg(self, target_dir):
        dists = pkg_resources.find_distributions(str(target_dir))
        assert not list(dists) 
Example #10
Source File: test_find_distributions.py    From setuptools with MIT License 5 votes vote down vote up
def test_standalone_egg_directory(self, target_dir):
        (TESTS_DATA_DIR / 'my-test-package_unpacked-egg').copy(target_dir)
        dists = pkg_resources.find_distributions(str(target_dir))
        assert [dist.project_name for dist in dists] == ['my-test-package']
        dists = pkg_resources.find_distributions(str(target_dir), only=True)
        assert not list(dists) 
Example #11
Source File: test_find_distributions.py    From setuptools with MIT License 5 votes vote down vote up
def test_zipped_egg(self, target_dir):
        (TESTS_DATA_DIR / 'my-test-package_zipped-egg').copy(target_dir)
        dists = pkg_resources.find_distributions(str(target_dir))
        assert [dist.project_name for dist in dists] == ['my-test-package']
        dists = pkg_resources.find_distributions(str(target_dir), only=True)
        assert not list(dists) 
Example #12
Source File: test_dist_info.py    From setuptools with MIT License 5 votes vote down vote up
def test_distinfo(self, metadata):
        dists = dict(
            (d.project_name, d)
            for d in pkg_resources.find_distributions(metadata)
        )

        assert len(dists) == 2, dists

        unversioned = dists['UnversionedDistribution']
        versioned = dists['VersionedDistribution']

        assert versioned.version == '2.718'  # from filename
        assert unversioned.version == '0.3'  # from METADATA 
Example #13
Source File: test_dist_info.py    From setuptools with MIT License 5 votes vote down vote up
def test_conditional_dependencies(self, metadata):
        specs = 'splort==4', 'quux>=1.1'
        requires = list(map(pkg_resources.Requirement.parse, specs))

        for d in pkg_resources.find_distributions(metadata):
            assert d.requires() == requires[:1]
            assert d.requires(extras=('baz',)) == [
                requires[0],
                pkg_resources.Requirement.parse('quux>=1.1;extra=="baz"'),
            ]
            assert d.extras == ['baz'] 
Example #14
Source File: setup_info.py    From pipenv with MIT License 5 votes vote down vote up
def get_distinfo_dist(path, pkg_name=None):
    # type: (S, Optional[S]) -> Optional[DistInfoDistribution]
    import pkg_resources

    dist_dir = next(iter(find_distinfo(path, pkg_name=pkg_name)), None)
    if dist_dir is not None:
        metadata_dir = dist_dir.path
        base_dir = os.path.dirname(metadata_dir)
        dist = next(iter(pkg_resources.find_distributions(base_dir)), None)
        if dist is not None:
            return dist
    return None 
Example #15
Source File: setup_info.py    From requirementslib with MIT License 5 votes vote down vote up
def get_distinfo_dist(path, pkg_name=None):
    # type: (S, Optional[S]) -> Optional[DistInfoDistribution]
    import pkg_resources

    dist_dir = next(iter(find_distinfo(path, pkg_name=pkg_name)), None)
    if dist_dir is not None:
        metadata_dir = dist_dir.path
        base_dir = os.path.dirname(metadata_dir)
        dist = next(iter(pkg_resources.find_distributions(base_dir)), None)
        if dist is not None:
            return dist
    return None 
Example #16
Source File: pip_faster.py    From venv-update with MIT License 5 votes vote down vote up
def fresh_working_set():
    """return a pkg_resources "working set", representing the *currently* installed packages"""
    class WorkingSetPlusEditableInstalls(pkg_resources.WorkingSet):

        def __init__(self, *args, **kwargs):
            self._normalized_name_mapping = {}
            super(WorkingSetPlusEditableInstalls, self).__init__(*args, **kwargs)

        def add_entry(self, entry):
            """Same as the original .add_entry, but sets only=False, so that egg-links are honored."""
            logger.debug('working-set entry: %r', entry)
            self.entry_keys.setdefault(entry, [])
            self.entries.append(entry)
            for dist in pkg_resources.find_distributions(entry, False):

                # eggs override anything that's installed normally
                # fun fact: pkg_resources.working_set's results depend on the
                # ordering of os.listdir since the order of os.listdir is
                # entirely arbitrary (an implemenation detail of file system),
                # without calling site.main(), an .egg-link file may or may not
                # be honored, depending on the filesystem
                replace = (dist.precedence == pkg_resources.EGG_DIST)
                self._normalized_name_mapping[normalize_name(dist.key)] = dist.key
                self.add(dist, entry, False, replace=replace)

        def find_normalized(self, req):
            req = _package_req_to_pkg_resources_req(str(req))
            req.key = self._normalized_name_mapping.get(normalize_name(req.key), req.key)
            return self.find(req)

    return WorkingSetPlusEditableInstalls() 
Example #17
Source File: test_dist_info.py    From Flask with Apache License 2.0 5 votes vote down vote up
def test_distinfo(self):
        dists = {}
        for d in pkg_resources.find_distributions(self.tmpdir):
            dists[d.project_name] = d

        assert len(dists) == 2, dists

        unversioned = dists['UnversionedDistribution']
        versioned = dists['VersionedDistribution']

        assert versioned.version == '2.718' # from filename
        assert unversioned.version == '0.3' # from METADATA 
Example #18
Source File: test_dist_info.py    From Flask with Apache License 2.0 5 votes vote down vote up
def test_conditional_dependencies(self):
        requires = [pkg_resources.Requirement.parse('splort==4'),
                    pkg_resources.Requirement.parse('quux>=1.1')]

        for d in pkg_resources.find_distributions(self.tmpdir):
            self.assertEqual(d.requires(), requires[:1])
            self.assertEqual(d.requires(extras=('baz',)), requires)
            self.assertEqual(d.extras, ['baz']) 
Example #19
Source File: test_dist_info.py    From Flask with Apache License 2.0 5 votes vote down vote up
def test_distinfo(self):
        dists = {}
        for d in pkg_resources.find_distributions(self.tmpdir):
            dists[d.project_name] = d

        assert len(dists) == 2, dists

        unversioned = dists['UnversionedDistribution']
        versioned = dists['VersionedDistribution']

        assert versioned.version == '2.718' # from filename
        assert unversioned.version == '0.3' # from METADATA 
Example #20
Source File: test_dist_info.py    From Flask with Apache License 2.0 5 votes vote down vote up
def test_conditional_dependencies(self):
        requires = [pkg_resources.Requirement.parse('splort==4'),
                    pkg_resources.Requirement.parse('quux>=1.1')]

        for d in pkg_resources.find_distributions(self.tmpdir):
            self.assertEqual(d.requires(), requires[:1])
            self.assertEqual(d.requires(extras=('baz',)), requires)
            self.assertEqual(d.extras, ['baz']) 
Example #21
Source File: ah_bootstrap.py    From grizli with MIT License 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                # We use subprocess instead of run_setup from setuptools to
                # avoid segmentation faults - see the following for more details:
                # https://github.com/cython/cython/issues/2104
                sp.check_output([sys.executable, 'setup.py', 'egg_info'], cwd=path)

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #22
Source File: test_dist_info.py    From pledgeservice with Apache License 2.0 5 votes vote down vote up
def test_distinfo(self):
        dists = {}
        for d in pkg_resources.find_distributions(self.tmpdir):
            dists[d.project_name] = d

        assert len(dists) == 2, dists

        unversioned = dists['UnversionedDistribution']
        versioned = dists['VersionedDistribution']

        assert versioned.version == '2.718' # from filename
        assert unversioned.version == '0.3' # from METADATA 
Example #23
Source File: environment.py    From pipenv with MIT License 5 votes vote down vote up
def get_distributions(self):
        # type: () -> Generator[pkg_resources.Distribution, None, None]
        """
        Retrives the distributions installed on the library path of the environment

        :return: A set of distributions found on the library path
        :rtype: iterator
        """

        pkg_resources = self.safe_import("pkg_resources")
        libdirs = self.base_paths["libdirs"].split(os.pathsep)
        dists = (pkg_resources.find_distributions(libdir) for libdir in libdirs)
        for dist in itertools.chain.from_iterable(dists):
            yield dist 
Example #24
Source File: test_dist_info.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_conditional_dependencies(self):
        requires = [pkg_resources.Requirement.parse('splort==4'),
                    pkg_resources.Requirement.parse('quux>=1.1')]

        for d in pkg_resources.find_distributions(self.tmpdir):
            self.assertEqual(d.requires(), requires[:1])
            self.assertEqual(d.requires(extras=('baz',)), requires)
            self.assertEqual(d.extras, ['baz']) 
Example #25
Source File: test_dist_info.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_distinfo(self):
        dists = {}
        for d in pkg_resources.find_distributions(self.tmpdir):
            dists[d.project_name] = d

        assert len(dists) == 2, dists

        unversioned = dists['UnversionedDistribution']
        versioned = dists['VersionedDistribution']

        assert versioned.version == '2.718' # from filename
        assert unversioned.version == '0.3' # from METADATA 
Example #26
Source File: hunch_publisher.py    From Hunch with Apache License 2.0 5 votes vote down vote up
def create_package_from_setup_py(self, path_to_setup_py, custom_package_name):
        """
        Creates a package from Setup.py specified in the the given path
        :param path_to_setup_py:
        :return: Package Information: Contains package name, version and path
        """
        if os.path.isfile(path_to_setup_py):
            path_to_setup_py = os.path.abspath(path_to_setup_py)
            setup_py_directory = os.path.dirname(path_to_setup_py)
        elif os.path.isdir(path_to_setup_py):
            setup_py_directory = os.path.abspath(path_to_setup_py)
            path_to_setup_py = setup_py_directory + '/setup.py'
        else:
            raise Exception("Given path/file: " + path_to_setup_py + " doesn't exist")

        if not os.path.exists(path_to_setup_py):
            raise Exception("setup.py doesn't exist in the given path: " + path_to_setup_py)

        sandbox.run_setup(path_to_setup_py, ['sdist'])
        eggs = find_distributions(setup_py_directory)

        version = None
        for egg in eggs:
            if custom_package_name in egg.egg_name():
                version = egg.version

        if not version:
            raise Exception("{package_name} not found".format(package_name=custom_package_name))

        package_info = dict()
        package_info['path'] = setup_py_directory + "/dist/" + custom_package_name + "-" + version + ".tar.gz"
        package_info['version'] = version
        package_info['name'] = custom_package_name
        return package_info 
Example #27
Source File: pundle.py    From pundler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, directory):
        self.dir = directory
        name = op.split(directory)[-1]
        key, encoded = name.split('+', 1)
        self.key = key.lower()
        self.line = b64decode(encoded).decode('utf-8')
        egg, req, version = parse_vcs_requirement(self.line)
        version = version or '0.0.0'
        self.hashcmp = (pkg_resources_parse_version(version), -1, egg, self.dir)
        self.version = self.line
        self.pkg_resource = next(iter(pkg_resources.find_distributions(self.dir, True)), None)
        self.location = self.pkg_resource.location 
Example #28
Source File: test_find_distributions.py    From pkg_resources with MIT License 5 votes vote down vote up
def test_zipped_egg(self, project_dir, target_dir):
        # install this distro as an unpacked egg:
        args = [
            sys.executable,
            '-c', 'from setuptools.command.easy_install import main; main()',
            '-mNx',
            '-d', target_dir,
            '--zip-ok',
            project_dir,
        ]
        subprocess.check_call(args)
        dists = pkg_resources.find_distributions(target_dir)
        assert [dist.project_name for dist in dists] == ['my-test-package']
        dists = pkg_resources.find_distributions(target_dir, only=True)
        assert not list(dists) 
Example #29
Source File: test_find_distributions.py    From pkg_resources with MIT License 5 votes vote down vote up
def test_standalone_egg_directory(self, project_dir, target_dir):
        # install this distro as an unpacked egg:
        args = [
            sys.executable,
            '-c', 'from setuptools.command.easy_install import main; main()',
            '-mNx',
            '-d', target_dir,
            '--always-unzip',
            project_dir,
        ]
        subprocess.check_call(args)
        dists = pkg_resources.find_distributions(target_dir)
        assert [dist.project_name for dist in dists] == ['my-test-package']
        dists = pkg_resources.find_distributions(target_dir, only=True)
        assert not list(dists) 
Example #30
Source File: test_find_distributions.py    From pkg_resources with MIT License 5 votes vote down vote up
def test_non_egg_dir_named_egg(self, target_dir):
        dists = pkg_resources.find_distributions(target_dir)
        assert not list(dists)