我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用pkg_resources.to_filename()。
def pre_run(self): self.egg_name = pkg_resources.safe_name(self.distribution.get_name()) self.egg_info = "%s.egg-info" % pkg_resources.to_filename( self.egg_name) if (not os.path.exists(self.egg_info) or _newer_requires_files(self.egg_info)): ei_cmd = self.get_finalized_command('egg_info') ei_cmd.run() self.install_test_requirements() _copy_test_requires_to(self.egg_info)
def _get_egg_info(metadata_config, packages_root): egg_name = metadata_config['name'] egg_base = packages_root egg_info = to_filename(egg_name) + '.egg-info' if egg_base != os.curdir: egg_info = os.path.join(egg_base, egg_info) return egg_info
def read(self): import sys import glob import warnings opj = os.path.join if self.package is not None: package = self.package.__package__ if package is None: package = self.package.__name__ project = pkg_resources.to_filename(pkg_resources.safe_name(self.entry_point.dist.project_name)) package_pattern = '%s*.egg-info' % package project_pattern = '%s*.egg-info' % project file = getattr(self.package, '__file__', None) if file is not None: candidates = [] def _add_candidate(where): candidates.extend(glob.glob(where)) for entry in sys.path: if file.startswith(entry): _add_candidate(opj(entry, 'EGG-INFO')) # egg? for pattern in (package_pattern, project_pattern): # dist-installed? _add_candidate(opj(entry, pattern)) dir, name = os.path.split(self.package.__file__) for pattern in (package_pattern, project_pattern): _add_candidate(opj(dir, pattern)) _add_candidate(opj(dir, '..', pattern)) for candidate in candidates: if os.path.isdir(candidate): path = opj(candidate, 'PKG-INFO') else: path = candidate if os.path.exists(path): with open(path) as f: return f.read() warnings.warn('No PKG-INFO found for package: %s' % self.package_name)