我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pkg_resources.parse_requirements()。
def _move_install_requirements_markers(self): """ Move requirements in `install_requires` that are using environment markers `extras_require`. """ # divide the install_requires into two sets, simple ones still # handled by install_requires and more complex ones handled # by extras_require. def is_simple_req(req): return not req.marker spec_inst_reqs = getattr(self, 'install_requires', None) or () inst_reqs = list(pkg_resources.parse_requirements(spec_inst_reqs)) simple_reqs = filter(is_simple_req, inst_reqs) complex_reqs = filterfalse(is_simple_req, inst_reqs) self.install_requires = list(map(str, simple_reqs)) for r in complex_reqs: self._tmp_extras_require[':' + str(r.marker)].append(r) self.extras_require = dict( (k, [str(r) for r in map(self._clean_req, v)]) for k, v in self._tmp_extras_require.items() )
def _finalize_requires(self): """ Move requirements in `install_requires` that are using environment markers to `extras_require`. """ if not self.install_requires: return extras_require = defaultdict(list, ( (k, list(pkg_resources.parse_requirements(v))) for k, v in (self.extras_require or {}).items() )) install_requires = [] for r in pkg_resources.parse_requirements(self.install_requires): marker = r.marker if not marker: install_requires.append(r) continue r.marker = None extras_require[':' + str(marker)].append(r) self.extras_require = dict( (k, [str(r) for r in v]) for k, v in extras_require.items() ) self.install_requires = [str(r) for r in install_requires]
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 _check_extra(extra, reqs): name, sep, marker = extra.partition(':') if marker and pkg_resources.invalid_marker(marker): raise DistutilsSetupError("Invalid environment marker: " + marker) list(pkg_resources.parse_requirements(reqs))
def check_requirements(dist, attr, value): """Verify that install_requires is a valid requirements list""" try: list(pkg_resources.parse_requirements(value)) except (TypeError, ValueError) as error: tmpl = ( "{attr!r} must be a string or list of strings " "containing valid project/version requirement specifiers; {error}" ) raise DistutilsSetupError(tmpl.format(attr=attr, error=error))
def _convert_extras_requirements(self): """ Convert requirements in `extras_require` of the form `"extra": ["barbazquux; {marker}"]` to `"extra:{marker}": ["barbazquux"]`. """ spec_ext_reqs = getattr(self, 'extras_require', None) or {} self._tmp_extras_require = defaultdict(list) for section, v in spec_ext_reqs.items(): # Do not strip empty sections. self._tmp_extras_require[section] for r in pkg_resources.parse_requirements(v): suffix = self._suffix_for(r) self._tmp_extras_require[section + suffix].append(r)
def fetch_build_eggs(self, requires): """Resolve pre-setup requirements""" resolved_dists = pkg_resources.working_set.resolve( pkg_resources.parse_requirements(requires), installer=self.fetch_build_egg, replace_conflicting=True, ) for dist in resolved_dists: pkg_resources.working_set.add(dist, replace=True) return resolved_dists
def get_raw_requirements(requirements_file): """Get requirements from a requirements.txt file. -r is not supported""" unparsed_requirements_lines = get_lines_from_file(requirements_file) return pkg_resources.parse_requirements( '\n'.join(unparsed_requirements_lines) )
def check_extras(dist, attr, value): """Verify that extras_require mapping is valid""" try: for k,v in value.items(): if ':' in k: k,m = k.split(':',1) if pkg_resources.invalid_marker(m): raise DistutilsSetupError("Invalid environment marker: "+m) list(pkg_resources.parse_requirements(v)) except (TypeError,ValueError,AttributeError): raise DistutilsSetupError( "'extras_require' must be a dictionary whose values are " "strings or lists of strings containing valid project/version " "requirement specifiers." )
def check_requirements(dist, attr, value): """Verify that install_requires is a valid requirements list""" try: list(pkg_resources.parse_requirements(value)) except (TypeError,ValueError): raise DistutilsSetupError( "%r must be a string or list of strings " "containing valid project/version requirement specifiers" % (attr,) )
def fetch_build_eggs(self, requires): """Resolve pre-setup requirements""" from pkg_resources import working_set, parse_requirements for dist in working_set.resolve( parse_requirements(requires), installer=self.fetch_build_egg, replace_conflicting=True ): working_set.add(dist, replace=True)
def check_extras(dist, attr, value): """Verify that extras_require mapping is valid""" try: for k, v in value.items(): if ':' in k: k, m = k.split(':', 1) if pkg_resources.invalid_marker(m): raise DistutilsSetupError("Invalid environment marker: " + m) list(pkg_resources.parse_requirements(v)) except (TypeError, ValueError, AttributeError): raise DistutilsSetupError( "'extras_require' must be a dictionary whose values are " "strings or lists of strings containing valid project/version " "requirement specifiers." )
def fetch_build_eggs(self, requires): """Resolve pre-setup requirements""" resolved_dists = pkg_resources.working_set.resolve( pkg_resources.parse_requirements(requires), installer=self.fetch_build_egg, replace_conflicting=True, ) for dist in resolved_dists: pkg_resources.working_set.add(dist, replace=True)