Python pkg_resources.ResourceManager() Examples

The following are 14 code examples of pkg_resources.ResourceManager(). 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: test_pkg_resources.py    From pkg_resources with MIT License 6 votes vote down vote up
def test_resource_filename_rewrites_on_change(self):
        """
        If a previous call to get_resource_filename has saved the file, but
        the file has been subsequently mutated with different file of the
        same size and modification time, it should not be overwritten on a
        subsequent call to get_resource_filename.
        """
        import mod
        manager = pkg_resources.ResourceManager()
        zp = pkg_resources.ZipProvider(mod)
        filename = zp.get_resource_filename(manager, 'data.dat')
        actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
        assert actual == self.ref_time
        f = open(filename, 'w')
        f.write('hello, world?')
        f.close()
        ts = timestamp(self.ref_time)
        os.utime(filename, (ts, ts))
        filename = zp.get_resource_filename(manager, 'data.dat')
        with open(filename) as f:
            assert f.read() == 'hello, world!'
        manager.cleanup_resources() 
Example #2
Source File: test_pkg_resources.py    From datafari with Apache License 2.0 6 votes vote down vote up
def test_resource_filename_rewrites_on_change(self):
        """
        If a previous call to get_resource_filename has saved the file, but
        the file has been subsequently mutated with different file of the
        same size and modification time, it should not be overwritten on a
        subsequent call to get_resource_filename.
        """
        import mod
        manager = pkg_resources.ResourceManager()
        zp = pkg_resources.ZipProvider(mod)
        filename = zp.get_resource_filename(manager, 'data.dat')
        actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
        assert actual == self.ref_time
        f = open(filename, 'w')
        f.write('hello, world?')
        f.close()
        ts = timestamp(self.ref_time)
        os.utime(filename, (ts, ts))
        filename = zp.get_resource_filename(manager, 'data.dat')
        f = open(filename)
        assert f.read() == 'hello, world!'
        manager.cleanup_resources() 
Example #3
Source File: test_pkg_resources.py    From setuptools with MIT License 6 votes vote down vote up
def test_resource_filename_rewrites_on_change(self):
        """
        If a previous call to get_resource_filename has saved the file, but
        the file has been subsequently mutated with different file of the
        same size and modification time, it should not be overwritten on a
        subsequent call to get_resource_filename.
        """
        import mod
        manager = pkg_resources.ResourceManager()
        zp = pkg_resources.ZipProvider(mod)
        filename = zp.get_resource_filename(manager, 'data.dat')
        actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
        assert actual == self.ref_time
        f = open(filename, 'w')
        f.write('hello, world?')
        f.close()
        ts = timestamp(self.ref_time)
        os.utime(filename, (ts, ts))
        filename = zp.get_resource_filename(manager, 'data.dat')
        with open(filename) as f:
            assert f.read() == 'hello, world!'
        manager.cleanup_resources() 
Example #4
Source File: shared_data.py    From recruit with Apache License 2.0 5 votes vote down vote up
def get_package_loader(self, package, package_path):
        from pkg_resources import DefaultProvider, ResourceManager, get_provider

        loadtime = datetime.utcnow()
        provider = get_provider(package)
        manager = ResourceManager()
        filesystem_bound = isinstance(provider, DefaultProvider)

        def loader(path):
            if path is None:
                return None, None

            path = posixpath.join(package_path, path)

            if not provider.has_resource(path):
                return None, None

            basename = posixpath.basename(path)

            if filesystem_bound:
                return (
                    basename,
                    self._opener(provider.get_resource_filename(manager, path)),
                )

            s = provider.get_resource_string(manager, path)
            return basename, lambda: (BytesIO(s), loadtime, len(s))

        return loader 
Example #5
Source File: urlparser.py    From mishkal with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, egg_or_spec, resource_name, manager=None, root_resource=None):
        if pkg_resources is None:
            raise NotImplementedError("This class requires pkg_resources.")
        if isinstance(egg_or_spec, (str, unicode)):
            self.egg = pkg_resources.get_distribution(egg_or_spec)
        else:
            self.egg = egg_or_spec
        self.resource_name = resource_name
        if manager is None:
            manager = pkg_resources.ResourceManager()
        self.manager = manager
        if root_resource is None:
            root_resource = resource_name
        self.root_resource = os.path.normpath(root_resource) 
Example #6
Source File: test_pkg_resources.py    From pkg_resources with MIT License 5 votes vote down vote up
def test_get_cache_path(self):
        mgr = pkg_resources.ResourceManager()
        path = mgr.get_cache_path('foo')
        type_ = str(type(path))
        message = "Unexpected type from get_cache_path: " + type_
        assert isinstance(path, (unicode, str)), message 
Example #7
Source File: loaders.py    From pipenv with MIT License 5 votes vote down vote up
def __init__(self, package_name, package_path="templates", encoding="utf-8"):
        from pkg_resources import DefaultProvider
        from pkg_resources import get_provider
        from pkg_resources import ResourceManager

        provider = get_provider(package_name)
        self.encoding = encoding
        self.manager = ResourceManager()
        self.filesystem_bound = isinstance(provider, DefaultProvider)
        self.provider = provider
        self.package_path = package_path 
Example #8
Source File: shared_data.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def get_package_loader(self, package, package_path):
        from pkg_resources import DefaultProvider, ResourceManager, get_provider

        loadtime = datetime.utcnow()
        provider = get_provider(package)
        manager = ResourceManager()
        filesystem_bound = isinstance(provider, DefaultProvider)

        def loader(path):
            if path is None:
                return None, None

            path = posixpath.join(package_path, path)

            if not provider.has_resource(path):
                return None, None

            basename = posixpath.basename(path)

            if filesystem_bound:
                return (
                    basename,
                    self._opener(provider.get_resource_filename(manager, path)),
                )

            s = provider.get_resource_string(manager, path)
            return basename, lambda: (BytesIO(s), loadtime, len(s))

        return loader 
Example #9
Source File: shared_data.py    From Building-Recommendation-Systems-with-Python with MIT License 5 votes vote down vote up
def get_package_loader(self, package, package_path):
        from pkg_resources import DefaultProvider, ResourceManager, get_provider

        loadtime = datetime.utcnow()
        provider = get_provider(package)
        manager = ResourceManager()
        filesystem_bound = isinstance(provider, DefaultProvider)

        def loader(path):
            if path is None:
                return None, None

            path = posixpath.join(package_path, path)

            if not provider.has_resource(path):
                return None, None

            basename = posixpath.basename(path)

            if filesystem_bound:
                return (
                    basename,
                    self._opener(provider.get_resource_filename(manager, path)),
                )

            s = provider.get_resource_string(manager, path)
            return basename, lambda: (BytesIO(s), loadtime, len(s))

        return loader 
Example #10
Source File: shared_data.py    From scylla with Apache License 2.0 5 votes vote down vote up
def get_package_loader(self, package, package_path):
        from pkg_resources import DefaultProvider, ResourceManager, get_provider

        loadtime = datetime.utcnow()
        provider = get_provider(package)
        manager = ResourceManager()
        filesystem_bound = isinstance(provider, DefaultProvider)

        def loader(path):
            if path is None:
                return None, None

            path = posixpath.join(package_path, path)

            if not provider.has_resource(path):
                return None, None

            basename = posixpath.basename(path)

            if filesystem_bound:
                return (
                    basename,
                    self._opener(provider.get_resource_filename(manager, path)),
                )

            s = provider.get_resource_string(manager, path)
            return basename, lambda: (BytesIO(s), loadtime, len(s))

        return loader 
Example #11
Source File: config.py    From websnort with GNU General Public License v3.0 5 votes vote down vote up
def installed_location(filename):
    """
    Returns the full path for the given installed file or None if not found.

    :param filename: The filename to search for.
    :returns: The absolute filepath for the file or None if not installed.
    """
    try:
        return ResourceManager().resource_filename(Requirement.parse("websnort"), filename)
    except DistributionNotFound:
        return None 
Example #12
Source File: test_pkg_resources.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_get_cache_path(self):
        mgr = pkg_resources.ResourceManager()
        path = mgr.get_cache_path('foo')
        type_ = str(type(path))
        message = "Unexpected type from get_cache_path: " + type_
        assert isinstance(path, (unicode, str)), message 
Example #13
Source File: test_pkg_resources.py    From setuptools with MIT License 5 votes vote down vote up
def test_get_cache_path(self):
        mgr = pkg_resources.ResourceManager()
        path = mgr.get_cache_path('foo')
        type_ = str(type(path))
        message = "Unexpected type from get_cache_path: " + type_
        assert isinstance(path, string_types), message 
Example #14
Source File: test_pkg_resources.py    From setuptools with MIT License 5 votes vote down vote up
def test_get_cache_path_race(self, tmpdir):
        # Patch to os.path.isdir to create a race condition
        def patched_isdir(dirname, unpatched_isdir=pkg_resources.isdir):
            patched_isdir.dirnames.append(dirname)

            was_dir = unpatched_isdir(dirname)
            if not was_dir:
                os.makedirs(dirname)
            return was_dir

        patched_isdir.dirnames = []

        # Get a cache path with a "race condition"
        mgr = pkg_resources.ResourceManager()
        mgr.set_extraction_path(str(tmpdir))

        archive_name = os.sep.join(('foo', 'bar', 'baz'))
        with mock.patch.object(pkg_resources, 'isdir', new=patched_isdir):
            mgr.get_cache_path(archive_name)

        # Because this test relies on the implementation details of this
        # function, these assertions are a sentinel to ensure that the
        # test suite will not fail silently if the implementation changes.
        called_dirnames = patched_isdir.dirnames
        assert len(called_dirnames) == 2
        assert called_dirnames[0].split(os.sep)[-2:] == ['foo', 'bar']
        assert called_dirnames[1].split(os.sep)[-1:] == ['foo']