python#

Common Python package format utilities.

Classes#

PythonDistribution

Base object describing a python distribution based on path to anchor file.

PythonInstalledDistribution

Python distribution installed via distutils.

PythonEggInfoDistribution

Python distribution installed via setuptools.

PythonEggLinkDistribution

Python distribution installed via setuptools.

PythonDistributionMetadata

Object representing the metada of a Python Distribution given by anchor

Evaluator

This class is used to evaluate marker expressions.

Functions#

norm_package_name(name)

pypi_name_to_conda_name(pypi_name)

norm_package_version(version)

Normalize a version by removing extra spaces and parentheses.

split_spec(spec, sep)

Split a spec by separator and return stripped start and end parts.

parse_specification(spec)

Parse a requirement from a python distribution metadata and return a

get_site_packages_anchor_files(site_packages_path, ...)

Get all the anchor files for the site packages directory.

get_dist_file_from_egg_link(egg_link_file, prefix_path)

Return the egg info file path following an egg link.

parse_marker(marker_string)

Parse marker string and return a dictionary containing a marker expression.

_is_literal(o)

get_default_marker_context()

Return the default context dictionary to use when parsing markers.

interpret(marker[, execution_context])

Interpret a marker and return a result depending on environment.

Attributes#

PYPI_TO_CONDA

PYPI_CONDA_DEPS

PARTIAL_PYPI_SPEC_PATTERN

PY_FILE_RE

PySpec

IDENTIFIER

VERSION_IDENTIFIER

COMPARE_OP

MARKER_OP

OR

AND

NON_SPACE

STRING_CHUNK

DEFAULT_MARKER_CONTEXT

evaluator

PYPI_TO_CONDA#
PYPI_CONDA_DEPS#
PARTIAL_PYPI_SPEC_PATTERN#
PY_FILE_RE#
PySpec#
exception MetadataWarning#

Bases: Warning

Base class for warning categories.

class PythonDistribution(anchor_full_path, python_version)#

Base object describing a python distribution based on path to anchor file.

property name#
property norm_name#
property conda_name#
property version#
MANIFEST_FILES = ()#
REQUIRES_FILES = ()#
MANDATORY_FILES = ()#
ENTRY_POINTS_FILES = ('entry_points.txt',)#
static init(prefix_path, anchor_file, python_version)#
_check_files()#

Check the existence of mandatory files for a given distribution.

_check_path_data(path, checksum, size)#

Normalizes record data content and format.

static _parse_requires_file_data(data, global_section='__global__')#
static _parse_entries_file_data(data)#
_load_requires_provides_file()#
manifest_full_path()#
get_paths()#

Read the list of installed paths from record or source file.

Example

[(u'skdata/__init__.py', u'sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU', 0),

(u'skdata/diabetes.py', None, None), ...

]

get_dist_requirements()#
get_python_requirements()#
get_external_requirements()#
get_extra_provides()#
get_conda_dependencies()#

Process metadata fields providing dependency information.

This includes normalizing fields, and evaluating environment markers.

abstract get_optional_dependencies()#
get_entry_points()#
class PythonInstalledDistribution(prefix_path, anchor_file, python_version)#

Bases: PythonDistribution

Python distribution installed via distutils.

Notes

MANIFEST_FILES = ('RECORD',)#
REQUIRES_FILES = ()#
MANDATORY_FILES = ('METADATA',)#
ENTRY_POINTS_FILES = ()#
is_manageable = True#
class PythonEggInfoDistribution(anchor_full_path, python_version, sp_reference)#

Bases: PythonDistribution

Python distribution installed via setuptools.

Notes

property is_manageable#
MANIFEST_FILES = ('installed-files.txt', 'SOURCES', 'SOURCES.txt')#
REQUIRES_FILES = ('requires.txt', 'depends.txt')#
MANDATORY_FILES = ()#
ENTRY_POINTS_FILES = ('entry_points.txt',)#
class PythonEggLinkDistribution(prefix_path, anchor_file, python_version)#

Bases: PythonEggInfoDistribution

Python distribution installed via setuptools.

Notes

is_manageable = False#
class PythonDistributionMetadata(path)#

Object representing the metada of a Python Distribution given by anchor file (or directory) path.

This metadata is extracted from a single file. Python distributions might create additional files that complement this metadata information, but that is handled at the python distribution level.

Notes

property name#
property version#
FILE_NAMES = ('METADATA', 'PKG-INFO')#
SINGLE_USE_KEYS#
MULTIPLE_USE_KEYS#
static _process_path(path, metadata_filenames)#

Find metadata file inside dist-info folder, or check direct file.

classmethod _message_to_dict(message)#

Convert the RFC-822 headers data into a dictionary.

message is an email.parser.Message instance.

The canonical method to transform metadata fields into such a data structure is as follows:

  • The original key-value format should be read with email.parser.HeaderParser

  • All transformed keys should be reduced to lower case. Hyphens should be replaced with underscores, but otherwise should retain all other characters

  • The transformed value for any field marked with "(Multiple-use") should be a single list containing all the original values for the given key

  • The Keywords field should be converted to a list by splitting the original value on whitespace characters

  • The message body, if present, should be set to the value of the description key.

  • The result should be stored as a string-keyed dictionary.

classmethod _read_metadata(fpath)#

Read the original format which is stored as RFC-822 headers.

_get_multiple_data(keys)#

Helper method to get multiple data values by keys.

Keys is an iterable including the preferred key in order, to include values of key that might have been replaced (deprecated), for example keys can be ['requires_dist', 'requires'], where the key 'requires' is deprecated and replaced by 'requires_dist'.

get_dist_requirements()#

Changed in version 2.1: The field format specification was relaxed to accept the syntax used by popular publishing tools.

Each entry contains a string naming some other distutils project required by this distribution.

The format of a requirement string contains from one to four parts:
  • A project name, in the same format as the Name: field. The only mandatory part.

  • A comma-separated list of ‘extra’ names. These are defined by the required project, referring to specific features which may need extra dependencies.

  • A version specifier. Tools parsing the format should accept optional parentheses around this, but tools generating it should not use parentheses.

  • An environment marker after a semicolon. This means that the requirement is only needed in the specified conditions.

This field may be followed by an environment marker after a semicolon.

Example

frozenset(['pkginfo', 'PasteDeploy', 'zope.interface (>3.5.0)',

'pywin32 >1.0; sys_platform == "win32"'])

Return 'Requires' if 'Requires-Dist' is empty.

get_python_requirements()#

New in version 1.2.

This field specifies the Python version(s) that the distribution is guaranteed to be compatible with. Installation tools may look at this when picking which version of a project to install.

The value must be in the format specified in Version specifiers.

This field may be followed by an environment marker after a semicolon.

Example

frozenset(['>=3', '>2.6,!=3.0.*,!=3.1.*', '~=2.6',

'>=3; sys_platform == "win32"'])

get_external_requirements()#

Changed in version 2.1: The field format specification was relaxed to accept the syntax used by popular publishing tools.

Each entry contains a string describing some dependency in the system that the distribution is to be used. This field is intended to serve as a hint to downstream project maintainers, and has no semantics which are meaningful to the distutils distribution.

The format of a requirement string is a name of an external dependency, optionally followed by a version declaration within parentheses.

This field may be followed by an environment marker after a semicolon.

Because they refer to non-Python software releases, version numbers for this field are not required to conform to the format specified in PEP 440: they should correspond to the version scheme used by the external dependency.

Notice that there’s is no particular rule on the strings to be used!

Example

frozenset(['C', 'libpng (>=1.5)', 'make; sys_platform != "win32"'])

get_extra_provides()#

New in version 2.1.

A string containing the name of an optional feature. Must be a valid Python identifier. May be used to make a dependency conditional on hether the optional feature has been requested.

Example

frozenset(['pdf', 'doc', 'test'])

get_dist_provides()#

New in version 1.2.

Changed in version 2.1: The field format specification was relaxed to accept the syntax used by popular publishing tools.

Each entry contains a string naming a Distutils project which is contained within this distribution. This field must include the project identified in the Name field, followed by the version : Name (Version).

A distribution may provide additional names, e.g. to indicate that multiple projects have been bundled together. For instance, source distributions of the ZODB project have historically included the transaction project, which is now available as a separate distribution. Installing such a source distribution satisfies requirements for both ZODB and transaction.

A distribution may also provide a “virtual” project name, which does not correspond to any separately-distributed project: such a name might be used to indicate an abstract capability which could be supplied by one of multiple projects. E.g., multiple projects might supply RDBMS bindings for use by a given ORM: each project might declare that it provides ORM-bindings, allowing other projects to depend only on having at most one of them installed.

A version declaration may be supplied and must follow the rules described in Version specifiers. The distribution’s version number will be implied if none is specified.

This field may be followed by an environment marker after a semicolon.

Return Provides in case Provides-Dist is empty.

get_dist_obsolete()#

New in version 1.2.

Changed in version 2.1: The field format specification was relaxed to accept the syntax used by popular publishing tools.

Each entry contains a string describing a distutils project’s distribution which this distribution renders obsolete, meaning that the two projects should not be installed at the same time.

Version declarations can be supplied. Version numbers must be in the format specified in Version specifiers [1].

The most common use of this field will be in case a project name changes, e.g. Gorgon 2.3 gets subsumed into Torqued Python 1.0. When you install Torqued Python, the Gorgon distribution should be removed.

This field may be followed by an environment marker after a semicolon.

Return Obsoletes in case Obsoletes-Dist is empty.

Example

frozenset(['Gorgon', "OtherProject (<3.0) ; python_version == '2.7'"])

Notes

get_classifiers()#

Classifiers are described in PEP 301, and the Python Package Index publishes a dynamic list of currently defined classifiers.

This field may be followed by an environment marker after a semicolon.

Example

frozenset(['Development Status :: 4 - Beta',

"Environment :: Console (Text Based) ; os_name == "posix"])

norm_package_name(name)#
pypi_name_to_conda_name(pypi_name)#
norm_package_version(version)#

Normalize a version by removing extra spaces and parentheses.

split_spec(spec, sep)#

Split a spec by separator and return stripped start and end parts.

parse_specification(spec)#

Parse a requirement from a python distribution metadata and return a namedtuple with name, extras, constraints, marker and url components.

This method does not enforce strict specifications but extracts the information which is assumed to be correct. As such no errors are raised.

Example

PySpec(name='requests', extras=['security'], constraints='>=3.3.0',

marker='foo >= 2.7 or bar == 1', url=''])

get_site_packages_anchor_files(site_packages_path, site_packages_dir)#

Get all the anchor files for the site packages directory.

Return the egg info file path following an egg link.

parse_marker(marker_string)#

Parse marker string and return a dictionary containing a marker expression.

The dictionary will contain keys "op", "lhs" and "rhs" for non-terminals in the expression grammar, or strings. A string contained in quotes is to be interpreted as a literal string, and a string not contained in quotes is a variable (such as os_name).

IDENTIFIER#
VERSION_IDENTIFIER#
COMPARE_OP#
MARKER_OP#
OR#
AND#
NON_SPACE#
STRING_CHUNK#
_is_literal(o)#
class Evaluator#

This class is used to evaluate marker expressions.

operations#
evaluate(expr, context)#

Evaluate a marker expression returned by the parse_requirement() function in the specified context.

get_default_marker_context()#

Return the default context dictionary to use when parsing markers.

DEFAULT_MARKER_CONTEXT#
evaluator#
interpret(marker, execution_context=None)#

Interpret a marker and return a result depending on environment.

Parameters:
  • marker (str) -- The marker to interpret.

  • execution_context (mapping) -- The context used for name lookup.