Python pkg_resources.resource_isdir() Examples
The following are 16
code examples of pkg_resources.resource_isdir().
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: classloader.py From aries-cloudagent-python with Apache License 2.0 | 6 votes |
def scan_subpackages(cls, package: str) -> Sequence[str]: """Return a list of sub-packages defined under a named package.""" # FIXME use importlib.resources in python 3.7 if "." in package: package, sub_pkg = package.split(".", 1) else: sub_pkg = "." if not pkg_resources.resource_isdir(package, sub_pkg): raise ModuleLoadError(f"Undefined package {package}") found = [] joiner = "" if sub_pkg == "." else f"{sub_pkg}." for sub_path in pkg_resources.resource_listdir(package, sub_pkg): if pkg_resources.resource_exists( package, f"{sub_pkg}/{sub_path}/__init__.py" ): found.append(f"{package}.{joiner}{sub_path}") return found
Example #2
Source File: pkgutil.py From incubator-retired-cotton with Apache License 2.0 | 6 votes |
def _unpack_assets(output_dir, module, asset_root, execute, current_path): """ The internal helper function for unpack_assets(...) recursion. :param current_path: Records the current """ for asset in pkg_resources.resource_listdir(module, current_path): asset_target = os.path.join(os.path.relpath(current_path, asset_root), asset) if pkg_resources.resource_isdir(module, os.path.join(current_path, asset)): safe_mkdir(os.path.join(output_dir, asset_target)) _unpack_assets(output_dir, module, asset_root, execute, os.path.join(current_path, asset)) else: output_file = os.path.join(output_dir, asset_target) with open(output_file, 'wb') as fp: fp.write(pkg_resources.resource_string( module, os.path.join(asset_root, asset_target))) execute(output_file)
Example #3
Source File: resource.py From canari3 with GNU General Public License v3.0 | 6 votes |
def image_resources(package=None, directory='resources'): """ Returns all images under the directory relative to a package path. If no directory or package is specified then the resources module of the calling package will be used. Images are recursively discovered. :param package: package name in dotted format. :param directory: path relative to package path of the resources directory. :return: a list of images under the specified resources path. """ if not package: package = calling_package() package_dir = '.'.join([package, directory]) images = [] for i in resource_listdir(package, directory): if i.startswith('__') or i.endswith('.egg-info'): continue fname = resource_filename(package_dir, i) if resource_isdir(package_dir, i): images.extend(image_resources(package_dir, i)) elif what(fname): images.append(fname) return images # etc
Example #4
Source File: install.py From treadmill with Apache License 2.0 | 6 votes |
def _is_scan_dir(package, src_dir, dst_dir): """Check if working on a scan dir. """ if os.path.exists(os.path.join(dst_dir, _CONTROL_DIR_NAME)): return True package_name = package.__name__ if pkg_resources.resource_isdir(package_name, os.path.join(src_dir, _CONTROL_DIR_NAME)): return True if pkg_resources.resource_exists(package_name, os.path.join(src_dir, _CONTROL_DIR_FILE)): return True return False
Example #5
Source File: plugins.py From datasette with Apache License 2.0 | 5 votes |
def get_plugins(): plugins = [] plugin_to_distinfo = dict(pm.list_plugin_distinfo()) for plugin in pm.get_plugins(): static_path = None templates_path = None if plugin.__name__ not in DEFAULT_PLUGINS: try: if pkg_resources.resource_isdir(plugin.__name__, "static"): static_path = pkg_resources.resource_filename( plugin.__name__, "static" ) if pkg_resources.resource_isdir(plugin.__name__, "templates"): templates_path = pkg_resources.resource_filename( plugin.__name__, "templates" ) except (KeyError, ImportError): # Caused by --plugins_dir= plugins - KeyError/ImportError thrown in Py3.5 pass plugin_info = { "name": plugin.__name__, "static_path": static_path, "templates_path": templates_path, "hooks": [h.name for h in pm.get_hookcallers(plugin)], } distinfo = plugin_to_distinfo.get(plugin) if distinfo: plugin_info["version"] = distinfo.version plugin_info["name"] = distinfo.project_name plugins.append(plugin_info) return plugins
Example #6
Source File: resources.py From virtme with GNU General Public License v2.0 | 5 votes |
def find_guest_tools(): """Return the path of the guest tools installed with the running virtme. """ if pkg_resources.resource_isdir(__name__, 'guest'): return pkg_resources.resource_filename(__name__, 'guest') # No luck. This is somewhat surprising. return None
Example #7
Source File: scripts.py From assembly with MIT License | 5 votes |
def copy_resource_dir(src, dest): """ To copy package data directory to destination """ package_name = "assembly" dest = (dest + "/" + os.path.basename(src)).rstrip("/") if pkg_resources.resource_isdir(package_name, src): if not os.path.isdir(dest): os.makedirs(dest) for res in pkg_resources.resource_listdir(about.__name__, src): copy_resource_dir(src + "/" + res, dest) else: if not os.path.isfile(dest) and os.path.splitext(src)[1] not in [".pyc"]: copy_resource_file(src, dest)
Example #8
Source File: __init__.py From udata with GNU Affero General Public License v3.0 | 5 votes |
def _iter(plugin, module): ''' Iterate over migrations for a given plugin module Yield tuples in the form (plugin_name, module_name, filename) ''' module_name = module if isinstance(module, str) else module.__name__ if not resource_isdir(module_name, 'migrations'): return for filename in resource_listdir(module_name, 'migrations'): if filename.endswith('.py') and not filename.startswith('__'): yield Migration(plugin, filename, module_name)
Example #9
Source File: test_android.py From chaquopy with MIT License | 5 votes |
def test_pr_resources(self): import pkg_resources as pr # App ZIP pkg = "android1" names = ["subdir", "__init__.py", "a.txt", "b.so", "mod1.py"] self.assertCountEqual(names, pr.resource_listdir(pkg, "")) for name in names: with self.subTest(name=name): self.assertTrue(pr.resource_exists(pkg, name)) self.assertEqual(pr.resource_isdir(pkg, name), name == "subdir") self.assertFalse(pr.resource_exists(pkg, "nonexistent")) self.assertFalse(pr.resource_isdir(pkg, "nonexistent")) self.assertCountEqual(["c.txt"], pr.resource_listdir(pkg, "subdir")) self.assertTrue(pr.resource_exists(pkg, "subdir/c.txt")) self.assertFalse(pr.resource_isdir(pkg, "subdir/c.txt")) self.assertFalse(pr.resource_exists(pkg, "subdir/nonexistent.txt")) self.check_pr_resource(APP_ZIP, pkg, "__init__.py", b"# This package is") self.check_pr_resource(APP_ZIP, pkg, "a.txt", b"alpha\n") self.check_pr_resource(APP_ZIP, pkg, "b.so", b"bravo\n") self.check_pr_resource(APP_ZIP, pkg, "subdir/c.txt", b"charlie\n") # Requirements ZIP self.reset_package("murmurhash") self.assertCountEqual(["include", "tests", "__init__.pxd", "__init__.pyc", "about.pyc", "mrmr.pxd", "mrmr.pyx", "mrmr.so"], pr.resource_listdir("murmurhash", "")) self.assertCountEqual(["MurmurHash2.h", "MurmurHash3.h"], pr.resource_listdir("murmurhash", "include/murmurhash")) self.check_pr_resource(REQS_COMMON_ZIP, "murmurhash", "__init__.pyc", MAGIC_NUMBER) self.check_pr_resource(REQS_COMMON_ZIP, "murmurhash", "mrmr.pxd", b"from libc.stdint") self.check_pr_resource(REQS_ABI_ZIP, "murmurhash", "mrmr.so", b"\x7fELF")
Example #10
Source File: cluster.py From pentagon with Apache License 2.0 | 5 votes |
def _files_directory(self): _template_path = 'files/public_cluster' if pkg_resources.resource_isdir(__name__, _template_path): return pkg_resources.resource_filename(__name__, _template_path) else: raise StandardError( 'Could not find template path ({})'.format(_template_path))
Example #11
Source File: __init__.py From dusty with MIT License | 5 votes |
def resources_for_test_config(test_config): resources = {} for key in [constants.CONFIG_BUNDLES_KEY, 'apps', 'libs', 'services']: key_path = 'test_configs/{}/{}'.format(test_config, key) if resource_isdir(__name__, key_path): resources[key] = {resource_name: resource_string(__name__, '{}/{}'.format(key_path, resource_name)) for resource_name in resource_listdir(__name__, key_path)} return resources
Example #12
Source File: neuromodel.py From NeuroNER with MIT License | 5 votes |
def _fetch(name, content_type=None): """ Load data or models from the package folder. Args: name (str): name of the resource content_type (str): either "data" or "trained_models" Returns: fileset (dict): dictionary containing the file content """ package_name = 'neuroner' resource_path = '/'.join((content_type, name)) # get dirs root_dir = os.path.dirname(pkg_resources.resource_filename(package_name, '__init__.py')) src_dir = os.path.join(root_dir, resource_path) dest_dir = os.path.join('.', content_type, name) if pkg_resources.resource_isdir(package_name, resource_path): # copy from package to dest dir if os.path.isdir(dest_dir): msg = "Directory '{}' already exists.".format(dest_dir) print(msg) else: shutil.copytree(src_dir, dest_dir) msg = "Directory created: '{}'.".format(dest_dir) print(msg) else: msg = "{} not found in {} package.".format(name,package_name) print(msg)
Example #13
Source File: project.py From cclyzer with MIT License | 4 votes |
def __init__(self): """Initialize the project manager.""" _logger.info("Initializing project manager") self._projects = {} metadata = {} # a dict from internal names to project name, dependencies logic_resource_pkg = settings.LOGIC_RESOURCE_PKG # Iterate over all project files for project in resource_listdir(logic_resource_pkg, '.'): # Skip empty resource paths (apparently, that can happen!!) if not project: continue # Skip ordinary files if not resource_isdir(logic_resource_pkg, project): continue # Construct project path project_dir = project # Find project file for resource in resource_listdir(logic_resource_pkg, project_dir): if resource.endswith(".project"): # Compute path to resource path_to_resource = path.join(project_dir, resource) path_to_file = FileManager().mktemp() # Read contents of project file with open(path_to_file, 'w') as f: # Copy contents from resource stream for byte in resource_stream(logic_resource_pkg, path_to_resource): f.write(byte) # Extract metadata from project file internal_name, deps = self.__extract_metadata(path_to_file) metadata[internal_name] = Metadata(project, deps) break # 2nd pass to create and store projects. This way the internal # names are entirely hidden from the user. for (i, (project, deps)) in metadata.iteritems(): p = Project(project, *[metadata[d].project for d in deps]) p.internal_name = i self._projects[project] = p setattr(self, project.replace('-', '_'), p) _logger.info("Found project %s that depends on: %s", project, p.dependencies)
Example #14
Source File: resource.py From cclyzer with MIT License | 4 votes |
def __enter__(self): # Compute resource path project = self._project cached_logic_dir = runtime.FileManager().getpath('logic') cached_proj_dir = path.join(cached_logic_dir, project) # Check if project has been extracted before if path.exists(cached_proj_dir): # Compare signatures disk_signature = resource_stream( self._pkg, path.join(project, 'checksum') ).read().strip() cached_signature = open( path.join(cached_proj_dir, 'checksum'), 'rb' ).read().strip() # Project hasn't changed; don't overwrite if disk_signature == cached_signature: return cached_proj_dir # remove stale cached project shutil.rmtree(cached_proj_dir) _logger.info("Extracting project %s to %s", project, cached_proj_dir) resource_dirs = [project] # Iterate over all project files while resource_dirs: # Pop next resource directory res_dir = resource_dirs.pop(0) # Process its files for resource in resource_listdir(self._pkg, res_dir): # Skip empty resource paths (apparently, that can happen!!) if not resource: continue # Compute path to resource path_to_resource = path.join(res_dir, resource) path_to_file = path.join(cached_logic_dir, path_to_resource) # Process resource directories recursively if resource_isdir(self._pkg, path_to_resource): resource_dirs.append(path_to_resource) continue _logger.debug("Extracting project file %s", path_to_resource) # Create parent directory parent_dir = path.dirname(path_to_file) if not path.exists(parent_dir): makedirs(parent_dir) with open(path_to_file, 'w') as f: # Copy contents from resource stream for byte in resource_stream(self._pkg, path_to_resource): f.write(byte) return cached_proj_dir
Example #15
Source File: __init__.py From pecan with BSD 3-Clause "New" or "Revised" License | 4 votes |
def copy_dir(source, dest, variables, out_=sys.stdout, i=0): """ Copies the ``source`` directory to the ``dest`` directory, where ``source`` is some tuple representing an installed package and a subdirectory in the package, e.g., ('pecan', os.path.join('scaffolds', 'base')) ('pecan_extension', os.path.join('scaffolds', 'scaffold_name')) ``variables``: A dictionary of variables to use in any substitutions. Substitution is performed via ``string.Template``. ``out_``: File object to write to (default is sys.stdout). """ def out(msg): out_.write('%s%s' % (' ' * (i * 2), msg)) out_.write('\n') out_.flush() names = sorted(pkg_resources.resource_listdir(source[0], source[1])) if not os.path.exists(dest): out('Creating %s' % dest) makedirs(dest) else: out('%s already exists' % dest) return for name in names: full = '/'.join([source[1], name]) dest_full = os.path.join(dest, substitute_filename(name, variables)) sub_file = False if dest_full.endswith('_tmpl'): dest_full = dest_full[:-5] sub_file = True if pkg_resources.resource_isdir(source[0], full): out('Recursing into %s' % os.path.basename(full)) copy_dir((source[0], full), dest_full, variables, out_, i + 1) continue else: content = pkg_resources.resource_string(source[0], full) if sub_file: content = render_template(content, variables) if content is None: continue # pragma: no cover out('Copying %s to %s' % (full, dest_full)) f = open(dest_full, 'wb') f.write(content) f.close()
Example #16
Source File: install.py From treadmill with Apache License 2.0 | 4 votes |
def _install_scan_dir(package, src_dir, dst_dir, params, prefix_len, rec=None): """Interpolate source directory as a scan directory containing service definitions. """ package_name = package.__name__ src_control_dir = os.path.join(src_dir, _CONTROL_DIR_NAME) src_control_dir_file = os.path.join(src_dir, _CONTROL_DIR_FILE) dst_path = os.path.join(dst_dir, src_dir[prefix_len:]) dst_control_dir = os.path.join(dst_path, _CONTROL_DIR_NAME) scan_dir = None if not os.path.exists(dst_control_dir): fs.mkdir_safe(dst_control_dir) if rec: rec.write('%s\n' % os.path.join(dst_control_dir, '')) if pkg_resources.resource_isdir(package_name, src_control_dir): _install(package, src_control_dir, dst_dir, params, prefix_len=prefix_len, rec=rec) elif pkg_resources.resource_exists(package_name, src_control_dir_file): _LOGGER.info('Expand control dir: %s => %s', src_control_dir_file, dst_control_dir) svscan_conf_file = pkg_resources.resource_string( package_name, src_control_dir_file ) if svscan_conf_file: svscan_conf = yaml.load(svscan_conf_file.decode('utf8')) else: svscan_conf = {} scan_dir = supervisor.create_scan_dir( dst_path, svscan_conf.get('finish_timeout', 0), wait_cgroups=svscan_conf.get('wait_cgroups', None), kill_svc=svscan_conf.get('kill_svc', None) ) scan_dir.write() if not scan_dir: scan_dir = supervisor.ScanDir(dst_path) _install_services( scan_dir, package, src_dir, dst_dir, params, prefix_len=prefix_len, rec=rec )