我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用pkg_resources.RequirementParseError()。
def _get_cached_requirements(self): import os.path import pkg_resources path = os.path.join(self._cache_dir, 'requirements.txt') if not os.path.exists(path): # No cached requirements. The empty set will always trigger a cache # refresh because the current requirements will, at minimum, # contain q2cli. return set() else: with open(path, 'r') as fh: contents = fh.read() try: return set(pkg_resources.parse_requirements(contents)) except pkg_resources.RequirementParseError: # Unreadable cached requirements, trigger a cache refresh. return set()
def version_info(module): """Get version of a standard python module. Args: module (module): python module object to get version info for. Returns: dict: dictionary of version info. """ if hasattr(module, '__version__'): version = module.__version__ elif hasattr(module, 'VERSION'): version = module.VERSION else: pkgname = module.__name__.split('.')[0] try: info = pkg_resources.get_distribution(pkgname) except (pkg_resources.DistributionNotFound, pkg_resources.RequirementParseError): version = None log.warning( 'version information not found for %s -- what package is this from?' % module.__name__) else: version = info.version return {'version': version}
def check_setuptools(): import pkg_resources try: list(pkg_resources.parse_requirements('foo;platform_system!="Windows"')) except pkg_resources.RequirementParseError: exit('Your Python is using an outdated version of `setuptools`. Please ' 'run `pip install -U setuptools` and try again.')
def _is_same_dep(cls, dep1, dep2): """ Returns True if both dependency definitions refer to the same package, even if versions differ. """ dep1_name = pkg_resources.Requirement.parse(dep1).project_name try: dep2_name = pkg_resources.Requirement.parse(dep2).project_name except pkg_resources.RequirementParseError: # we couldn't parse a version, probably a URL return False return dep1_name == dep2_name
def try_parse_requirements(lines: typed_list(str)): """ Yields all package requirements parseable from the given lines. :param lines: An iterable of lines from a requirements file. """ for line in lines: try: yield from pkg_resources.parse_requirements(line) except pkg_resources.RequirementParseError: # unsupported requirement specification pass
def _gen_requirements_file(self, requirements_list) -> str: modules = {} for req in requirements_list: name, req_spec = self._parse_line(req) if name is None: name = req url = None version = None try: # this will fail is an url is supplied parsed_req = list(pkg_resources.parse_requirements(req_spec)) if len(parsed_req) > 0: item = parsed_req[0] if hasattr(item, "name"): name = item.name elif hasattr(item, "unsafe_name"): name = item.unsafe_name version = item.specs if hasattr(item, "url"): url = item.url except pkg_resources.RequirementParseError: url = req_spec if name not in modules: modules[name] = {"name": name, "version": []} if version is not None: modules[name]["version"].extend(version) if url is not None: modules[name]["url"] = url requirements_file = "" for module, info in modules.items(): version_spec = "" if len(info["version"]) > 0: version_spec = " " + (", ".join(["%s %s" % (a, b) for a, b in info["version"]])) if "url" in info: module = info["url"] requirements_file += module + version_spec + "\n" return requirements_file