requirementslib.models.utils module

requirementslib.models.utils.as_tuple(ireq)[source]

Pulls out the (name: str, version:str, extras:(str)) tuple from the pinned InstallRequirement.

requirementslib.models.utils.build_vcs_uri(vcs, uri, name=None, ref=None, subdirectory=None, extras=None)[source]
requirementslib.models.utils.clean_requires_python(candidates)[source]

Get a cleaned list of all the candidates with valid specifiers in the requires_python attributes.

requirementslib.models.utils.convert_direct_url_to_url(direct_url)[source]

Converts direct URLs to standard, link-style URLs

Given a direct url as defined by PEP 508, convert to a Link compatible URL by moving the name and extras into an egg_fragment.

Parameters:direct_url (str) – A pep-508 compliant direct url.
Returns:A reformatted URL for use with Link objects and InstallRequirement objects.
Return type:AnyStr
requirementslib.models.utils.convert_url_to_direct_url(url, name=None)[source]

Converts normal link-style URLs to direct urls.

Given a Link compatible URL, convert to a direct url as defined by PEP 508 by extracting the name and extras from the egg_fragment.

Parameters:
  • url (AnyStr) – A InstallRequirement compliant URL.
  • name (Optiona[AnyStr]) – A name to use in case the supplied URL doesn’t provide one.
Returns:

A pep-508 compliant direct url.

Return type:

AnyStr

Raises:
  • ValueError – Raised when the URL can’t be parsed or a name can’t be found.
  • TypeError – When a non-string input is provided.
requirementslib.models.utils.expand_env_variables(line)[source]

Expand the env vars in a line following pip’s standard. https://pip.pypa.io/en/stable/reference/pip_install/#id10

Matches environment variable-style values in ‘${MY_VARIABLE_1}’ with the variable name consisting of only uppercase letters, digits or the ‘_’

requirementslib.models.utils.extras_to_string(extras)[source]

Turn a list of extras into a string

Parameters:extras (List[str]]) – a list of extras to format
Returns:A string of extras
Return type:str
requirementslib.models.utils.filter_dict(dict_)[source]
requirementslib.models.utils.filter_none(k, v)[source]
requirementslib.models.utils.fix_requires_python_marker(requires_python)[source]
requirementslib.models.utils.flat_map(fn, collection)[source]

Map a function over a collection and flatten the result by one-level

requirementslib.models.utils.format_requirement(ireq)[source]

Formats an InstallRequirement instance as a string.

Generic formatter for pretty printing InstallRequirements to the terminal in a less verbose way than using its __str__ method.

:param InstallRequirement ireq: A pip InstallRequirement instance. :return: A formatted string for prettyprinting :rtype: str

requirementslib.models.utils.format_specifier(ireq)[source]

Generic formatter for pretty printing specifiers.

Pretty-prints specifiers from InstallRequirements for output to terminal.

:param InstallRequirement ireq: A pip InstallRequirement instance. :return: A string of specifiers in the given install requirement or <any> :rtype: str

requirementslib.models.utils.full_groupby(iterable, key=None)[source]

Like groupby(), but sorts the input on the group key first.

requirementslib.models.utils.get_default_pyproject_backend()[source]
requirementslib.models.utils.get_name_variants(pkg)[source]

Given a packager name, get the variants of its name for both the canonicalized and “safe” forms.

Parameters:pkg (AnyStr) – The package to lookup
Returns:A list of names.
Return type:Set
requirementslib.models.utils.get_pinned_version(ireq)[source]

Get the pinned version of an InstallRequirement.

An InstallRequirement is considered pinned if:

  • Is not editable
  • It has exactly one specifier
  • That specifier is “==”
  • The version does not contain a wildcard
Examples:
django==1.8 # pinned django>1.8 # NOT pinned django~=1.8 # NOT pinned django==1.* # NOT pinned

Raises TypeError if the input is not a valid InstallRequirement, or ValueError if the InstallRequirement is not pinned.

requirementslib.models.utils.get_pyproject(path)[source]

Given a base path, look for the corresponding pyproject.toml file and return its build_requires and build_backend.

Parameters:path (AnyStr) – The root path of the project, should be a directory (will be truncated)
Returns:A 2 tuple of build requirements and the build backend
Return type:Optional[Tuple[List[AnyStr], AnyStr]]
requirementslib.models.utils.get_setuptools_version[source]
requirementslib.models.utils.get_url_name(url)[source]

Given a url, derive an appropriate name to use in a pipfile.

Parameters:url (str) – A url to derive a string from
Returns:The name of the corresponding pipfile entry
Return type:Text
requirementslib.models.utils.get_version(pipfile_entry)[source]
requirementslib.models.utils.init_requirement(name)[source]
requirementslib.models.utils.is_pinned_requirement(ireq)[source]

Returns whether an InstallRequirement is a “pinned” requirement.

An InstallRequirement is considered pinned if:

  • Is not editable
  • It has exactly one specifier
  • That specifier is “==”
  • The version does not contain a wildcard
Examples:
django==1.8 # pinned django>1.8 # NOT pinned django~=1.8 # NOT pinned django==1.* # NOT pinned
requirementslib.models.utils.key_from_ireq(ireq)[source]

Get a standardized key for an InstallRequirement.

requirementslib.models.utils.key_from_req(req)[source]

Get an all-lowercase version of the requirement’s name.

requirementslib.models.utils.lookup_table(values, key=None, keyval=None, unique=False, use_lists=False)[source]

Builds a dict-based lookup table (index) elegantly.

Supports building normal and unique lookup tables. For example:

>>> assert lookup_table(
...     ['foo', 'bar', 'baz', 'qux', 'quux'], lambda s: s[0]) == {
...     'b': {'bar', 'baz'},
...     'f': {'foo'},
...     'q': {'quux', 'qux'}
... }

For key functions that uniquely identify values, set unique=True:

>>> assert lookup_table(
...     ['foo', 'bar', 'baz', 'qux', 'quux'], lambda s: s[0], unique=True) == {
...     'b': 'baz',
...     'f': 'foo',
...     'q': 'quux'
... }

The values of the resulting lookup table will be values, not sets.

For extra power, you can even change the values while building up the LUT. To do so, use the keyval function instead of the key arg:

>>> assert lookup_table(
...     ['foo', 'bar', 'baz', 'qux', 'quux'],
...     keyval=lambda s: (s[0], s[1:])) == {
...     'b': {'ar', 'az'},
...     'f': {'oo'},
...     'q': {'uux', 'ux'}
... }
requirementslib.models.utils.make_install_requirement(name, version=None, extras=None, markers=None, constraint=False)[source]

Generates an InstallRequirement.

Create an InstallRequirement from the supplied metadata.

Parameters:
  • name (str) – The requirement’s name.
  • version (str.) – The requirement version (must be pinned).
  • extras (list[str]) – The desired extras.
  • markers (str) – The desired markers, without a preceding semicolon.
  • constraint – Whether to flag the requirement as a constraint, defaults to False.
  • constraint – bool, optional
Returns:

A generated InstallRequirement

Return type:

InstallRequirement

requirementslib.models.utils.name_from_req(req)[source]

Get the name of the requirement

requirementslib.models.utils.normalize_name(pkg)[source]

Given a package name, return its normalized, non-canonicalized form.

Parameters:pkg (AnyStr) – The name of a package
Returns:A normalized package name
Return type:AnyStr
requirementslib.models.utils.optional_instance_of(cls)[source]
requirementslib.models.utils.parse_extras(extras_str)[source]

Turn a string of extras into a parsed extras list

Parameters:extras_str (str) – An extras string
Returns:A sorted list of extras
Return type:List[str]
requirementslib.models.utils.read_source(path, encoding='utf-8')[source]

Read a source file and get the contents with proper encoding for Python 2/3.

Parameters:
  • path (AnyStr) – the file path
  • encoding (AnyStr) – the encoding that defaults to UTF-8
Returns:

The contents of the source file

Return type:

AnyStr

requirementslib.models.utils.specs_to_string(specs)[source]

Turn a list of specifier tuples into a string

Parameters:str]] specs (List[Union[Specifier,) – a list of specifiers to format
Returns:A string of specifiers
Return type:str
requirementslib.models.utils.split_markers_from_line(line)[source]

Split markers from a dependency

requirementslib.models.utils.split_ref_from_uri(uri)[source]

Given a path or URI, check for a ref and split it from the path if it is present, returning a tuple of the original input and the ref or None.

Parameters:uri (AnyStr) – The path or URI to split
Returns:A 2-tuple of the path or URI and the ref
Return type:Tuple[AnyStr, Optional[AnyStr]]
requirementslib.models.utils.split_vcs_method_from_uri(uri)[source]

Split a vcs+uri formatted uri into (vcs, uri)

requirementslib.models.utils.strip_extras_markers_from_requirement(req)[source]

Strips extras markers from requirement instances.

Given a Requirement instance with markers defining extra == ‘name’, strip out the extras from the markers and return the cleaned requirement

Parameters:req (PackagingRequirement) – A packaging requirement to clean
Returns:A cleaned requirement
Return type:PackagingRequirement
requirementslib.models.utils.tomlkit_dict_to_python(toml_dict)[source]
requirementslib.models.utils.tomlkit_value_to_python(toml_value)[source]
requirementslib.models.utils.validate_markers(instance, attr_, value)[source]
requirementslib.models.utils.validate_path(instance, attr_, value)[source]
requirementslib.models.utils.validate_specifiers(instance, attr_, value)[source]
requirementslib.models.utils.validate_vcs(instance, attr_, value)[source]
requirementslib.models.utils.version_from_ireq(ireq)[source]

version_from_ireq Extract the version from a supplied InstallRequirement

Parameters:ireq (InstallRequirement) – An InstallRequirement
Returns:The version of the InstallRequirement.
Return type:str