mirror of
https://github.com/kennethreitz/pipenv.git
synced 2026-06-05 22:50:18 +00:00
Add missing dependencies, fix patched pip path
- Add missing dependencies: - `zipp==0.6.0` - `more-itertools==5.0.0` - `importlib-metadata==1.3.0` - `funcsigs==1.0.2` - `contextlib2==0.6.0.post1` - Fix patched pip import paths for CLI and resolver - Update patches Signed-off-by: Dan Ryan <dan.ryan@canonical.com>
This commit is contained in:
@@ -21,7 +21,7 @@ CommandInfo = namedtuple('CommandInfo', 'module_path, class_name, summary')
|
||||
|
||||
# The ordering matters for help display.
|
||||
# Also, even though the module path starts with the same
|
||||
# "pip._internal.commands" prefix in each case, we include the full path
|
||||
# "pipenv.patched.notpip._internal.commands" prefix in each case, we include the full path
|
||||
# because it makes testing easier (specifically when modifying commands_dict
|
||||
# in test setup / teardown by adding info for a FakeCommand class defined
|
||||
# in a test-related module).
|
||||
@@ -29,59 +29,59 @@ CommandInfo = namedtuple('CommandInfo', 'module_path, class_name, summary')
|
||||
# so that the ordering won't be lost when using Python 2.7.
|
||||
commands_dict = OrderedDict([
|
||||
('install', CommandInfo(
|
||||
'pip._internal.commands.install', 'InstallCommand',
|
||||
'pipenv.patched.notpip._internal.commands.install', 'InstallCommand',
|
||||
'Install packages.',
|
||||
)),
|
||||
('download', CommandInfo(
|
||||
'pip._internal.commands.download', 'DownloadCommand',
|
||||
'pipenv.patched.notpip._internal.commands.download', 'DownloadCommand',
|
||||
'Download packages.',
|
||||
)),
|
||||
('uninstall', CommandInfo(
|
||||
'pip._internal.commands.uninstall', 'UninstallCommand',
|
||||
'pipenv.patched.notpip._internal.commands.uninstall', 'UninstallCommand',
|
||||
'Uninstall packages.',
|
||||
)),
|
||||
('freeze', CommandInfo(
|
||||
'pip._internal.commands.freeze', 'FreezeCommand',
|
||||
'pipenv.patched.notpip._internal.commands.freeze', 'FreezeCommand',
|
||||
'Output installed packages in requirements format.',
|
||||
)),
|
||||
('list', CommandInfo(
|
||||
'pip._internal.commands.list', 'ListCommand',
|
||||
'pipenv.patched.notpip._internal.commands.list', 'ListCommand',
|
||||
'List installed packages.',
|
||||
)),
|
||||
('show', CommandInfo(
|
||||
'pip._internal.commands.show', 'ShowCommand',
|
||||
'pipenv.patched.notpip._internal.commands.show', 'ShowCommand',
|
||||
'Show information about installed packages.',
|
||||
)),
|
||||
('check', CommandInfo(
|
||||
'pip._internal.commands.check', 'CheckCommand',
|
||||
'pipenv.patched.notpip._internal.commands.check', 'CheckCommand',
|
||||
'Verify installed packages have compatible dependencies.',
|
||||
)),
|
||||
('config', CommandInfo(
|
||||
'pip._internal.commands.configuration', 'ConfigurationCommand',
|
||||
'pipenv.patched.notpip._internal.commands.configuration', 'ConfigurationCommand',
|
||||
'Manage local and global configuration.',
|
||||
)),
|
||||
('search', CommandInfo(
|
||||
'pip._internal.commands.search', 'SearchCommand',
|
||||
'pipenv.patched.notpip._internal.commands.search', 'SearchCommand',
|
||||
'Search PyPI for packages.',
|
||||
)),
|
||||
('wheel', CommandInfo(
|
||||
'pip._internal.commands.wheel', 'WheelCommand',
|
||||
'pipenv.patched.notpip._internal.commands.wheel', 'WheelCommand',
|
||||
'Build wheels from your requirements.',
|
||||
)),
|
||||
('hash', CommandInfo(
|
||||
'pip._internal.commands.hash', 'HashCommand',
|
||||
'pipenv.patched.notpip._internal.commands.hash', 'HashCommand',
|
||||
'Compute hashes of package archives.',
|
||||
)),
|
||||
('completion', CommandInfo(
|
||||
'pip._internal.commands.completion', 'CompletionCommand',
|
||||
'pipenv.patched.notpip._internal.commands.completion', 'CompletionCommand',
|
||||
'A helper command used for command completion.',
|
||||
)),
|
||||
('debug', CommandInfo(
|
||||
'pip._internal.commands.debug', 'DebugCommand',
|
||||
'pipenv.patched.notpip._internal.commands.debug', 'DebugCommand',
|
||||
'Show information useful for debugging.',
|
||||
)),
|
||||
('help', CommandInfo(
|
||||
'pip._internal.commands.help', 'HelpCommand',
|
||||
'pipenv.patched.notpip._internal.commands.help', 'HelpCommand',
|
||||
'Show help for commands.',
|
||||
)),
|
||||
]) # type: OrderedDict[str, CommandInfo]
|
||||
|
||||
@@ -443,17 +443,6 @@ class Resolver(object):
|
||||
for subreq in dist.requires(available_requested):
|
||||
add_req(subreq, extras_requested=available_requested)
|
||||
|
||||
# Hack for deep-resolving extras.
|
||||
for available in available_requested:
|
||||
if hasattr(dist, '_DistInfoDistribution__dep_map'):
|
||||
for req in dist._DistInfoDistribution__dep_map[available]:
|
||||
req = self._make_install_req(
|
||||
req,
|
||||
req_to_install
|
||||
)
|
||||
|
||||
more_reqs.append(req)
|
||||
|
||||
if not req_to_install.editable and not req_to_install.satisfied_by:
|
||||
# XXX: --no-install leads this to report 'Successfully
|
||||
# downloaded' for only non-editable reqs, even though we took
|
||||
|
||||
@@ -5,6 +5,7 @@ import collections
|
||||
import copy
|
||||
import hashlib
|
||||
import os
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from functools import partial
|
||||
from shutil import rmtree
|
||||
@@ -89,7 +90,7 @@ class HashCache(SafeFileCache):
|
||||
|
||||
def _get_file_hash(self, location):
|
||||
h = hashlib.new(FAVORITE_HASH)
|
||||
with open_local_or_remote_file(location, self.session) as fp:
|
||||
with open_local_or_remote_file(location, self.session) as (fp, size):
|
||||
for chunk in iter(lambda: fp.read(8096), b""):
|
||||
h.update(chunk)
|
||||
return ":".join([FAVORITE_HASH, h.hexdigest()])
|
||||
@@ -322,12 +323,13 @@ class PyPIRepository(BaseRepository):
|
||||
if PIP_VERSION < (19, 3):
|
||||
resolver_kwargs.update(**make_install_req_kwargs)
|
||||
else:
|
||||
from pipenv.patched.notpip._internal.req.constructors import install_req_from_req_string
|
||||
from pipenv.vendor.pip_shims.shims import install_req_from_req_string
|
||||
|
||||
make_install_req = partial(
|
||||
install_req_from_req_string, **make_install_req_kwargs
|
||||
)
|
||||
resolver_kwargs["make_install_req"] = make_install_req
|
||||
del resolver_kwargs["use_pep517"]
|
||||
|
||||
if PIP_VERSION >= (20,):
|
||||
preparer_kwargs["session"] = self.session
|
||||
@@ -359,7 +361,7 @@ class PyPIRepository(BaseRepository):
|
||||
|
||||
results = set(results) if results else set()
|
||||
|
||||
return set(results)
|
||||
return results, ireq
|
||||
|
||||
def get_legacy_dependencies(self, ireq):
|
||||
"""
|
||||
|
||||
@@ -79,8 +79,8 @@ def clean_requires_python(candidates):
|
||||
if getattr(c, "requires_python", None):
|
||||
# Old specifications had people setting this to single digits
|
||||
# which is effectively the same as '>=digit,<digit+1'
|
||||
if c.requires_python.isdigit():
|
||||
c.requires_python = '>={0},<{1}'.format(c.requires_python, int(c.requires_python) + 1)
|
||||
if len(c.requires_python) == 1 and c.requires_python in ("2", "3"):
|
||||
c.requires_python = '>={0},<{1!s}'.format(c.requires_python, int(c.requires_python) + 1)
|
||||
try:
|
||||
specifierset = SpecifierSet(c.requires_python)
|
||||
except InvalidSpecifier:
|
||||
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
|
||||
|
||||
A. HISTORY OF THE SOFTWARE
|
||||
==========================
|
||||
|
||||
contextlib2 is a derivative of the contextlib module distributed by the PSF
|
||||
as part of the Python standard library. According, it is itself redistributed
|
||||
under the PSF license (reproduced in full below). As the contextlib module
|
||||
was added only in Python 2.5, the licenses for earlier Python versions are
|
||||
not applicable and have not been included.
|
||||
|
||||
Python was created in the early 1990s by Guido van Rossum at Stichting
|
||||
Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands
|
||||
as a successor of a language called ABC. Guido remains Python's
|
||||
principal author, although it includes many contributions from others.
|
||||
|
||||
In 1995, Guido continued his work on Python at the Corporation for
|
||||
National Research Initiatives (CNRI, see http://www.cnri.reston.va.us)
|
||||
in Reston, Virginia where he released several versions of the
|
||||
software.
|
||||
|
||||
In May 2000, Guido and the Python core development team moved to
|
||||
BeOpen.com to form the BeOpen PythonLabs team. In October of the same
|
||||
year, the PythonLabs team moved to Digital Creations (now Zope
|
||||
Corporation, see http://www.zope.com). In 2001, the Python Software
|
||||
Foundation (PSF, see http://www.python.org/psf/) was formed, a
|
||||
non-profit organization created specifically to own Python-related
|
||||
Intellectual Property. Zope Corporation is a sponsoring member of
|
||||
the PSF.
|
||||
|
||||
All Python releases are Open Source (see http://www.opensource.org for
|
||||
the Open Source Definition). Historically, most, but not all, Python
|
||||
releases have also been GPL-compatible; the table below summarizes
|
||||
the various releases that included the contextlib module.
|
||||
|
||||
Release Derived Year Owner GPL-
|
||||
from compatible? (1)
|
||||
|
||||
2.5 2.4 2006 PSF yes
|
||||
2.5.1 2.5 2007 PSF yes
|
||||
2.5.2 2.5.1 2008 PSF yes
|
||||
2.5.3 2.5.2 2008 PSF yes
|
||||
2.6 2.5 2008 PSF yes
|
||||
2.6.1 2.6 2008 PSF yes
|
||||
2.6.2 2.6.1 2009 PSF yes
|
||||
2.6.3 2.6.2 2009 PSF yes
|
||||
2.6.4 2.6.3 2009 PSF yes
|
||||
2.6.5 2.6.4 2010 PSF yes
|
||||
3.0 2.6 2008 PSF yes
|
||||
3.0.1 3.0 2009 PSF yes
|
||||
3.1 3.0.1 2009 PSF yes
|
||||
3.1.1 3.1 2009 PSF yes
|
||||
3.1.2 3.1.1 2010 PSF yes
|
||||
3.1.3 3.1.2 2010 PSF yes
|
||||
3.1.4 3.1.3 2011 PSF yes
|
||||
3.2 3.1 2011 PSF yes
|
||||
3.2.1 3.2 2011 PSF yes
|
||||
3.2.2 3.2.1 2011 PSF yes
|
||||
3.3 3.2 2012 PSF yes
|
||||
|
||||
Footnotes:
|
||||
|
||||
(1) GPL-compatible doesn't mean that we're distributing Python under
|
||||
the GPL. All Python licenses, unlike the GPL, let you distribute
|
||||
a modified version without making your changes open source. The
|
||||
GPL-compatible licenses make it possible to combine Python with
|
||||
other software that is released under the GPL; the others don't.
|
||||
|
||||
Thanks to the many outside volunteers who have worked under Guido's
|
||||
direction to make these releases possible.
|
||||
|
||||
|
||||
B. TERMS AND CONDITIONS FOR ACCESSING OR OTHERWISE USING PYTHON
|
||||
===============================================================
|
||||
|
||||
PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
|
||||
--------------------------------------------
|
||||
|
||||
1. This LICENSE AGREEMENT is between the Python Software Foundation
|
||||
("PSF"), and the Individual or Organization ("Licensee") accessing and
|
||||
otherwise using this software ("Python") in source or binary form and
|
||||
its associated documentation.
|
||||
|
||||
2. Subject to the terms and conditions of this License Agreement, PSF hereby
|
||||
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
|
||||
analyze, test, perform and/or display publicly, prepare derivative works,
|
||||
distribute, and otherwise use Python alone or in any derivative version,
|
||||
provided, however, that PSF's License Agreement and PSF's notice of copyright,
|
||||
i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
||||
2011 Python Software Foundation; All Rights Reserved" are retained in Python
|
||||
alone or in any derivative version prepared by Licensee.
|
||||
|
||||
3. In the event Licensee prepares a derivative work that is based on
|
||||
or incorporates Python or any part thereof, and wants to make
|
||||
the derivative work available to others as provided herein, then
|
||||
Licensee hereby agrees to include in any such work a brief summary of
|
||||
the changes made to Python.
|
||||
|
||||
4. PSF is making Python available to Licensee on an "AS IS"
|
||||
basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
|
||||
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
|
||||
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
|
||||
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT
|
||||
INFRINGE ANY THIRD PARTY RIGHTS.
|
||||
|
||||
5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
|
||||
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
|
||||
A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON,
|
||||
OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
|
||||
|
||||
6. This License Agreement will automatically terminate upon a material
|
||||
breach of its terms and conditions.
|
||||
|
||||
7. Nothing in this License Agreement shall be deemed to create any
|
||||
relationship of agency, partnership, or joint venture between PSF and
|
||||
Licensee. This License Agreement does not grant permission to use PSF
|
||||
trademarks or trade name in a trademark sense to endorse or promote
|
||||
products or services of Licensee, or any third party.
|
||||
|
||||
8. By copying, installing or otherwise using Python, Licensee
|
||||
agrees to be bound by the terms and conditions of this License
|
||||
Agreement.
|
||||
Vendored
+518
@@ -0,0 +1,518 @@
|
||||
"""contextlib2 - backports and enhancements to the contextlib module"""
|
||||
|
||||
import abc
|
||||
import sys
|
||||
import warnings
|
||||
from collections import deque
|
||||
from functools import wraps
|
||||
|
||||
__all__ = ["contextmanager", "closing", "nullcontext",
|
||||
"AbstractContextManager",
|
||||
"ContextDecorator", "ExitStack",
|
||||
"redirect_stdout", "redirect_stderr", "suppress"]
|
||||
|
||||
# Backwards compatibility
|
||||
__all__ += ["ContextStack"]
|
||||
|
||||
|
||||
# Backport abc.ABC
|
||||
if sys.version_info[:2] >= (3, 4):
|
||||
_abc_ABC = abc.ABC
|
||||
else:
|
||||
_abc_ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
|
||||
|
||||
|
||||
# Backport classic class MRO
|
||||
def _classic_mro(C, result):
|
||||
if C in result:
|
||||
return
|
||||
result.append(C)
|
||||
for B in C.__bases__:
|
||||
_classic_mro(B, result)
|
||||
return result
|
||||
|
||||
|
||||
# Backport _collections_abc._check_methods
|
||||
def _check_methods(C, *methods):
|
||||
try:
|
||||
mro = C.__mro__
|
||||
except AttributeError:
|
||||
mro = tuple(_classic_mro(C, []))
|
||||
|
||||
for method in methods:
|
||||
for B in mro:
|
||||
if method in B.__dict__:
|
||||
if B.__dict__[method] is None:
|
||||
return NotImplemented
|
||||
break
|
||||
else:
|
||||
return NotImplemented
|
||||
return True
|
||||
|
||||
|
||||
class AbstractContextManager(_abc_ABC):
|
||||
"""An abstract base class for context managers."""
|
||||
|
||||
def __enter__(self):
|
||||
"""Return `self` upon entering the runtime context."""
|
||||
return self
|
||||
|
||||
@abc.abstractmethod
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
"""Raise any exception triggered within the runtime context."""
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def __subclasshook__(cls, C):
|
||||
"""Check whether subclass is considered a subclass of this ABC."""
|
||||
if cls is AbstractContextManager:
|
||||
return _check_methods(C, "__enter__", "__exit__")
|
||||
return NotImplemented
|
||||
|
||||
|
||||
class ContextDecorator(object):
|
||||
"""A base class or mixin that enables context managers to work as decorators."""
|
||||
|
||||
def refresh_cm(self):
|
||||
"""Returns the context manager used to actually wrap the call to the
|
||||
decorated function.
|
||||
|
||||
The default implementation just returns *self*.
|
||||
|
||||
Overriding this method allows otherwise one-shot context managers
|
||||
like _GeneratorContextManager to support use as decorators via
|
||||
implicit recreation.
|
||||
|
||||
DEPRECATED: refresh_cm was never added to the standard library's
|
||||
ContextDecorator API
|
||||
"""
|
||||
warnings.warn("refresh_cm was never added to the standard library",
|
||||
DeprecationWarning)
|
||||
return self._recreate_cm()
|
||||
|
||||
def _recreate_cm(self):
|
||||
"""Return a recreated instance of self.
|
||||
|
||||
Allows an otherwise one-shot context manager like
|
||||
_GeneratorContextManager to support use as
|
||||
a decorator via implicit recreation.
|
||||
|
||||
This is a private interface just for _GeneratorContextManager.
|
||||
See issue #11647 for details.
|
||||
"""
|
||||
return self
|
||||
|
||||
def __call__(self, func):
|
||||
@wraps(func)
|
||||
def inner(*args, **kwds):
|
||||
with self._recreate_cm():
|
||||
return func(*args, **kwds)
|
||||
return inner
|
||||
|
||||
|
||||
class _GeneratorContextManager(ContextDecorator):
|
||||
"""Helper for @contextmanager decorator."""
|
||||
|
||||
def __init__(self, func, args, kwds):
|
||||
self.gen = func(*args, **kwds)
|
||||
self.func, self.args, self.kwds = func, args, kwds
|
||||
# Issue 19330: ensure context manager instances have good docstrings
|
||||
doc = getattr(func, "__doc__", None)
|
||||
if doc is None:
|
||||
doc = type(self).__doc__
|
||||
self.__doc__ = doc
|
||||
# Unfortunately, this still doesn't provide good help output when
|
||||
# inspecting the created context manager instances, since pydoc
|
||||
# currently bypasses the instance docstring and shows the docstring
|
||||
# for the class instead.
|
||||
# See http://bugs.python.org/issue19404 for more details.
|
||||
|
||||
def _recreate_cm(self):
|
||||
# _GCM instances are one-shot context managers, so the
|
||||
# CM must be recreated each time a decorated function is
|
||||
# called
|
||||
return self.__class__(self.func, self.args, self.kwds)
|
||||
|
||||
def __enter__(self):
|
||||
try:
|
||||
return next(self.gen)
|
||||
except StopIteration:
|
||||
raise RuntimeError("generator didn't yield")
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if type is None:
|
||||
try:
|
||||
next(self.gen)
|
||||
except StopIteration:
|
||||
return
|
||||
else:
|
||||
raise RuntimeError("generator didn't stop")
|
||||
else:
|
||||
if value is None:
|
||||
# Need to force instantiation so we can reliably
|
||||
# tell if we get the same exception back
|
||||
value = type()
|
||||
try:
|
||||
self.gen.throw(type, value, traceback)
|
||||
raise RuntimeError("generator didn't stop after throw()")
|
||||
except StopIteration as exc:
|
||||
# Suppress StopIteration *unless* it's the same exception that
|
||||
# was passed to throw(). This prevents a StopIteration
|
||||
# raised inside the "with" statement from being suppressed.
|
||||
return exc is not value
|
||||
except RuntimeError as exc:
|
||||
# Don't re-raise the passed in exception
|
||||
if exc is value:
|
||||
return False
|
||||
# Likewise, avoid suppressing if a StopIteration exception
|
||||
# was passed to throw() and later wrapped into a RuntimeError
|
||||
# (see PEP 479).
|
||||
if _HAVE_EXCEPTION_CHAINING and exc.__cause__ is value:
|
||||
return False
|
||||
raise
|
||||
except:
|
||||
# only re-raise if it's *not* the exception that was
|
||||
# passed to throw(), because __exit__() must not raise
|
||||
# an exception unless __exit__() itself failed. But throw()
|
||||
# has to raise the exception to signal propagation, so this
|
||||
# fixes the impedance mismatch between the throw() protocol
|
||||
# and the __exit__() protocol.
|
||||
#
|
||||
if sys.exc_info()[1] is not value:
|
||||
raise
|
||||
|
||||
|
||||
def contextmanager(func):
|
||||
"""@contextmanager decorator.
|
||||
|
||||
Typical usage:
|
||||
|
||||
@contextmanager
|
||||
def some_generator(<arguments>):
|
||||
<setup>
|
||||
try:
|
||||
yield <value>
|
||||
finally:
|
||||
<cleanup>
|
||||
|
||||
This makes this:
|
||||
|
||||
with some_generator(<arguments>) as <variable>:
|
||||
<body>
|
||||
|
||||
equivalent to this:
|
||||
|
||||
<setup>
|
||||
try:
|
||||
<variable> = <value>
|
||||
<body>
|
||||
finally:
|
||||
<cleanup>
|
||||
|
||||
"""
|
||||
@wraps(func)
|
||||
def helper(*args, **kwds):
|
||||
return _GeneratorContextManager(func, args, kwds)
|
||||
return helper
|
||||
|
||||
|
||||
class closing(object):
|
||||
"""Context to automatically close something at the end of a block.
|
||||
|
||||
Code like this:
|
||||
|
||||
with closing(<module>.open(<arguments>)) as f:
|
||||
<block>
|
||||
|
||||
is equivalent to this:
|
||||
|
||||
f = <module>.open(<arguments>)
|
||||
try:
|
||||
<block>
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
"""
|
||||
def __init__(self, thing):
|
||||
self.thing = thing
|
||||
|
||||
def __enter__(self):
|
||||
return self.thing
|
||||
|
||||
def __exit__(self, *exc_info):
|
||||
self.thing.close()
|
||||
|
||||
|
||||
class _RedirectStream(object):
|
||||
|
||||
_stream = None
|
||||
|
||||
def __init__(self, new_target):
|
||||
self._new_target = new_target
|
||||
# We use a list of old targets to make this CM re-entrant
|
||||
self._old_targets = []
|
||||
|
||||
def __enter__(self):
|
||||
self._old_targets.append(getattr(sys, self._stream))
|
||||
setattr(sys, self._stream, self._new_target)
|
||||
return self._new_target
|
||||
|
||||
def __exit__(self, exctype, excinst, exctb):
|
||||
setattr(sys, self._stream, self._old_targets.pop())
|
||||
|
||||
|
||||
class redirect_stdout(_RedirectStream):
|
||||
"""Context manager for temporarily redirecting stdout to another file.
|
||||
|
||||
# How to send help() to stderr
|
||||
with redirect_stdout(sys.stderr):
|
||||
help(dir)
|
||||
|
||||
# How to write help() to a file
|
||||
with open('help.txt', 'w') as f:
|
||||
with redirect_stdout(f):
|
||||
help(pow)
|
||||
"""
|
||||
|
||||
_stream = "stdout"
|
||||
|
||||
|
||||
class redirect_stderr(_RedirectStream):
|
||||
"""Context manager for temporarily redirecting stderr to another file."""
|
||||
|
||||
_stream = "stderr"
|
||||
|
||||
|
||||
class suppress(object):
|
||||
"""Context manager to suppress specified exceptions
|
||||
|
||||
After the exception is suppressed, execution proceeds with the next
|
||||
statement following the with statement.
|
||||
|
||||
with suppress(FileNotFoundError):
|
||||
os.remove(somefile)
|
||||
# Execution still resumes here if the file was already removed
|
||||
"""
|
||||
|
||||
def __init__(self, *exceptions):
|
||||
self._exceptions = exceptions
|
||||
|
||||
def __enter__(self):
|
||||
pass
|
||||
|
||||
def __exit__(self, exctype, excinst, exctb):
|
||||
# Unlike isinstance and issubclass, CPython exception handling
|
||||
# currently only looks at the concrete type hierarchy (ignoring
|
||||
# the instance and subclass checking hooks). While Guido considers
|
||||
# that a bug rather than a feature, it's a fairly hard one to fix
|
||||
# due to various internal implementation details. suppress provides
|
||||
# the simpler issubclass based semantics, rather than trying to
|
||||
# exactly reproduce the limitations of the CPython interpreter.
|
||||
#
|
||||
# See http://bugs.python.org/issue12029 for more details
|
||||
return exctype is not None and issubclass(exctype, self._exceptions)
|
||||
|
||||
|
||||
# Context manipulation is Python 3 only
|
||||
_HAVE_EXCEPTION_CHAINING = sys.version_info[0] >= 3
|
||||
if _HAVE_EXCEPTION_CHAINING:
|
||||
def _make_context_fixer(frame_exc):
|
||||
def _fix_exception_context(new_exc, old_exc):
|
||||
# Context may not be correct, so find the end of the chain
|
||||
while 1:
|
||||
exc_context = new_exc.__context__
|
||||
if exc_context is old_exc:
|
||||
# Context is already set correctly (see issue 20317)
|
||||
return
|
||||
if exc_context is None or exc_context is frame_exc:
|
||||
break
|
||||
new_exc = exc_context
|
||||
# Change the end of the chain to point to the exception
|
||||
# we expect it to reference
|
||||
new_exc.__context__ = old_exc
|
||||
return _fix_exception_context
|
||||
|
||||
def _reraise_with_existing_context(exc_details):
|
||||
try:
|
||||
# bare "raise exc_details[1]" replaces our carefully
|
||||
# set-up context
|
||||
fixed_ctx = exc_details[1].__context__
|
||||
raise exc_details[1]
|
||||
except BaseException:
|
||||
exc_details[1].__context__ = fixed_ctx
|
||||
raise
|
||||
else:
|
||||
# No exception context in Python 2
|
||||
def _make_context_fixer(frame_exc):
|
||||
return lambda new_exc, old_exc: None
|
||||
|
||||
# Use 3 argument raise in Python 2,
|
||||
# but use exec to avoid SyntaxError in Python 3
|
||||
def _reraise_with_existing_context(exc_details):
|
||||
exc_type, exc_value, exc_tb = exc_details
|
||||
exec("raise exc_type, exc_value, exc_tb")
|
||||
|
||||
# Handle old-style classes if they exist
|
||||
try:
|
||||
from types import InstanceType
|
||||
except ImportError:
|
||||
# Python 3 doesn't have old-style classes
|
||||
_get_type = type
|
||||
else:
|
||||
# Need to handle old-style context managers on Python 2
|
||||
def _get_type(obj):
|
||||
obj_type = type(obj)
|
||||
if obj_type is InstanceType:
|
||||
return obj.__class__ # Old-style class
|
||||
return obj_type # New-style class
|
||||
|
||||
|
||||
# Inspired by discussions on http://bugs.python.org/issue13585
|
||||
class ExitStack(object):
|
||||
"""Context manager for dynamic management of a stack of exit callbacks
|
||||
|
||||
For example:
|
||||
|
||||
with ExitStack() as stack:
|
||||
files = [stack.enter_context(open(fname)) for fname in filenames]
|
||||
# All opened files will automatically be closed at the end of
|
||||
# the with statement, even if attempts to open files later
|
||||
# in the list raise an exception
|
||||
|
||||
"""
|
||||
def __init__(self):
|
||||
self._exit_callbacks = deque()
|
||||
|
||||
def pop_all(self):
|
||||
"""Preserve the context stack by transferring it to a new instance"""
|
||||
new_stack = type(self)()
|
||||
new_stack._exit_callbacks = self._exit_callbacks
|
||||
self._exit_callbacks = deque()
|
||||
return new_stack
|
||||
|
||||
def _push_cm_exit(self, cm, cm_exit):
|
||||
"""Helper to correctly register callbacks to __exit__ methods"""
|
||||
def _exit_wrapper(*exc_details):
|
||||
return cm_exit(cm, *exc_details)
|
||||
_exit_wrapper.__self__ = cm
|
||||
self.push(_exit_wrapper)
|
||||
|
||||
def push(self, exit):
|
||||
"""Registers a callback with the standard __exit__ method signature
|
||||
|
||||
Can suppress exceptions the same way __exit__ methods can.
|
||||
|
||||
Also accepts any object with an __exit__ method (registering a call
|
||||
to the method instead of the object itself)
|
||||
"""
|
||||
# We use an unbound method rather than a bound method to follow
|
||||
# the standard lookup behaviour for special methods
|
||||
_cb_type = _get_type(exit)
|
||||
try:
|
||||
exit_method = _cb_type.__exit__
|
||||
except AttributeError:
|
||||
# Not a context manager, so assume its a callable
|
||||
self._exit_callbacks.append(exit)
|
||||
else:
|
||||
self._push_cm_exit(exit, exit_method)
|
||||
return exit # Allow use as a decorator
|
||||
|
||||
def callback(self, callback, *args, **kwds):
|
||||
"""Registers an arbitrary callback and arguments.
|
||||
|
||||
Cannot suppress exceptions.
|
||||
"""
|
||||
def _exit_wrapper(exc_type, exc, tb):
|
||||
callback(*args, **kwds)
|
||||
# We changed the signature, so using @wraps is not appropriate, but
|
||||
# setting __wrapped__ may still help with introspection
|
||||
_exit_wrapper.__wrapped__ = callback
|
||||
self.push(_exit_wrapper)
|
||||
return callback # Allow use as a decorator
|
||||
|
||||
def enter_context(self, cm):
|
||||
"""Enters the supplied context manager
|
||||
|
||||
If successful, also pushes its __exit__ method as a callback and
|
||||
returns the result of the __enter__ method.
|
||||
"""
|
||||
# We look up the special methods on the type to match the with statement
|
||||
_cm_type = _get_type(cm)
|
||||
_exit = _cm_type.__exit__
|
||||
result = _cm_type.__enter__(cm)
|
||||
self._push_cm_exit(cm, _exit)
|
||||
return result
|
||||
|
||||
def close(self):
|
||||
"""Immediately unwind the context stack"""
|
||||
self.__exit__(None, None, None)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc_details):
|
||||
received_exc = exc_details[0] is not None
|
||||
|
||||
# We manipulate the exception state so it behaves as though
|
||||
# we were actually nesting multiple with statements
|
||||
frame_exc = sys.exc_info()[1]
|
||||
_fix_exception_context = _make_context_fixer(frame_exc)
|
||||
|
||||
# Callbacks are invoked in LIFO order to match the behaviour of
|
||||
# nested context managers
|
||||
suppressed_exc = False
|
||||
pending_raise = False
|
||||
while self._exit_callbacks:
|
||||
cb = self._exit_callbacks.pop()
|
||||
try:
|
||||
if cb(*exc_details):
|
||||
suppressed_exc = True
|
||||
pending_raise = False
|
||||
exc_details = (None, None, None)
|
||||
except:
|
||||
new_exc_details = sys.exc_info()
|
||||
# simulate the stack of exceptions by setting the context
|
||||
_fix_exception_context(new_exc_details[1], exc_details[1])
|
||||
pending_raise = True
|
||||
exc_details = new_exc_details
|
||||
if pending_raise:
|
||||
_reraise_with_existing_context(exc_details)
|
||||
return received_exc and suppressed_exc
|
||||
|
||||
|
||||
# Preserve backwards compatibility
|
||||
class ContextStack(ExitStack):
|
||||
"""Backwards compatibility alias for ExitStack"""
|
||||
|
||||
def __init__(self):
|
||||
warnings.warn("ContextStack has been renamed to ExitStack",
|
||||
DeprecationWarning)
|
||||
super(ContextStack, self).__init__()
|
||||
|
||||
def register_exit(self, callback):
|
||||
return self.push(callback)
|
||||
|
||||
def register(self, callback, *args, **kwds):
|
||||
return self.callback(callback, *args, **kwds)
|
||||
|
||||
def preserve(self):
|
||||
return self.pop_all()
|
||||
|
||||
|
||||
class nullcontext(AbstractContextManager):
|
||||
"""Context manager that does no additional processing.
|
||||
Used as a stand-in for a normal context manager, when a particular
|
||||
block of code is only sometimes used with a normal context manager:
|
||||
cm = optional_cm if condition else nullcontext()
|
||||
with cm:
|
||||
# Perform operation, using optional_cm if condition is True
|
||||
"""
|
||||
|
||||
def __init__(self, enter_result=None):
|
||||
self.enter_result = enter_result
|
||||
|
||||
def __enter__(self):
|
||||
return self.enter_result
|
||||
|
||||
def __exit__(self, *excinfo):
|
||||
pass
|
||||
Vendored
+829
@@ -0,0 +1,829 @@
|
||||
# Copyright 2001-2013 Python Software Foundation; All Rights Reserved
|
||||
"""Function signature objects for callables
|
||||
|
||||
Back port of Python 3.3's function signature tools from the inspect module,
|
||||
modified to be compatible with Python 2.6, 2.7 and 3.3+.
|
||||
"""
|
||||
from __future__ import absolute_import, division, print_function
|
||||
import itertools
|
||||
import functools
|
||||
import re
|
||||
import types
|
||||
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
from ordereddict import OrderedDict
|
||||
|
||||
from funcsigs.version import __version__
|
||||
|
||||
__all__ = ['BoundArguments', 'Parameter', 'Signature', 'signature']
|
||||
|
||||
|
||||
_WrapperDescriptor = type(type.__call__)
|
||||
_MethodWrapper = type(all.__call__)
|
||||
|
||||
_NonUserDefinedCallables = (_WrapperDescriptor,
|
||||
_MethodWrapper,
|
||||
types.BuiltinFunctionType)
|
||||
|
||||
|
||||
def formatannotation(annotation, base_module=None):
|
||||
if isinstance(annotation, type):
|
||||
if annotation.__module__ in ('builtins', '__builtin__', base_module):
|
||||
return annotation.__name__
|
||||
return annotation.__module__+'.'+annotation.__name__
|
||||
return repr(annotation)
|
||||
|
||||
|
||||
def _get_user_defined_method(cls, method_name, *nested):
|
||||
try:
|
||||
if cls is type:
|
||||
return
|
||||
meth = getattr(cls, method_name)
|
||||
for name in nested:
|
||||
meth = getattr(meth, name, meth)
|
||||
except AttributeError:
|
||||
return
|
||||
else:
|
||||
if not isinstance(meth, _NonUserDefinedCallables):
|
||||
# Once '__signature__' will be added to 'C'-level
|
||||
# callables, this check won't be necessary
|
||||
return meth
|
||||
|
||||
|
||||
def signature(obj):
|
||||
'''Get a signature object for the passed callable.'''
|
||||
|
||||
if not callable(obj):
|
||||
raise TypeError('{0!r} is not a callable object'.format(obj))
|
||||
|
||||
if isinstance(obj, types.MethodType):
|
||||
sig = signature(obj.__func__)
|
||||
if obj.__self__ is None:
|
||||
# Unbound method - preserve as-is.
|
||||
return sig
|
||||
else:
|
||||
# Bound method. Eat self - if we can.
|
||||
params = tuple(sig.parameters.values())
|
||||
|
||||
if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
|
||||
raise ValueError('invalid method signature')
|
||||
|
||||
kind = params[0].kind
|
||||
if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
|
||||
# Drop first parameter:
|
||||
# '(p1, p2[, ...])' -> '(p2[, ...])'
|
||||
params = params[1:]
|
||||
else:
|
||||
if kind is not _VAR_POSITIONAL:
|
||||
# Unless we add a new parameter type we never
|
||||
# get here
|
||||
raise ValueError('invalid argument type')
|
||||
# It's a var-positional parameter.
|
||||
# Do nothing. '(*args[, ...])' -> '(*args[, ...])'
|
||||
|
||||
return sig.replace(parameters=params)
|
||||
|
||||
try:
|
||||
sig = obj.__signature__
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if sig is not None:
|
||||
return sig
|
||||
|
||||
try:
|
||||
# Was this function wrapped by a decorator?
|
||||
wrapped = obj.__wrapped__
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
return signature(wrapped)
|
||||
|
||||
if isinstance(obj, types.FunctionType):
|
||||
return Signature.from_function(obj)
|
||||
|
||||
if isinstance(obj, functools.partial):
|
||||
sig = signature(obj.func)
|
||||
|
||||
new_params = OrderedDict(sig.parameters.items())
|
||||
|
||||
partial_args = obj.args or ()
|
||||
partial_keywords = obj.keywords or {}
|
||||
try:
|
||||
ba = sig.bind_partial(*partial_args, **partial_keywords)
|
||||
except TypeError as ex:
|
||||
msg = 'partial object {0!r} has incorrect arguments'.format(obj)
|
||||
raise ValueError(msg)
|
||||
|
||||
for arg_name, arg_value in ba.arguments.items():
|
||||
param = new_params[arg_name]
|
||||
if arg_name in partial_keywords:
|
||||
# We set a new default value, because the following code
|
||||
# is correct:
|
||||
#
|
||||
# >>> def foo(a): print(a)
|
||||
# >>> print(partial(partial(foo, a=10), a=20)())
|
||||
# 20
|
||||
# >>> print(partial(partial(foo, a=10), a=20)(a=30))
|
||||
# 30
|
||||
#
|
||||
# So, with 'partial' objects, passing a keyword argument is
|
||||
# like setting a new default value for the corresponding
|
||||
# parameter
|
||||
#
|
||||
# We also mark this parameter with '_partial_kwarg'
|
||||
# flag. Later, in '_bind', the 'default' value of this
|
||||
# parameter will be added to 'kwargs', to simulate
|
||||
# the 'functools.partial' real call.
|
||||
new_params[arg_name] = param.replace(default=arg_value,
|
||||
_partial_kwarg=True)
|
||||
|
||||
elif (param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL) and
|
||||
not param._partial_kwarg):
|
||||
new_params.pop(arg_name)
|
||||
|
||||
return sig.replace(parameters=new_params.values())
|
||||
|
||||
sig = None
|
||||
if isinstance(obj, type):
|
||||
# obj is a class or a metaclass
|
||||
|
||||
# First, let's see if it has an overloaded __call__ defined
|
||||
# in its metaclass
|
||||
call = _get_user_defined_method(type(obj), '__call__')
|
||||
if call is not None:
|
||||
sig = signature(call)
|
||||
else:
|
||||
# Now we check if the 'obj' class has a '__new__' method
|
||||
new = _get_user_defined_method(obj, '__new__')
|
||||
if new is not None:
|
||||
sig = signature(new)
|
||||
else:
|
||||
# Finally, we should have at least __init__ implemented
|
||||
init = _get_user_defined_method(obj, '__init__')
|
||||
if init is not None:
|
||||
sig = signature(init)
|
||||
elif not isinstance(obj, _NonUserDefinedCallables):
|
||||
# An object with __call__
|
||||
# We also check that the 'obj' is not an instance of
|
||||
# _WrapperDescriptor or _MethodWrapper to avoid
|
||||
# infinite recursion (and even potential segfault)
|
||||
call = _get_user_defined_method(type(obj), '__call__', 'im_func')
|
||||
if call is not None:
|
||||
sig = signature(call)
|
||||
|
||||
if sig is not None:
|
||||
# For classes and objects we skip the first parameter of their
|
||||
# __call__, __new__, or __init__ methods
|
||||
return sig.replace(parameters=tuple(sig.parameters.values())[1:])
|
||||
|
||||
if isinstance(obj, types.BuiltinFunctionType):
|
||||
# Raise a nicer error message for builtins
|
||||
msg = 'no signature found for builtin function {0!r}'.format(obj)
|
||||
raise ValueError(msg)
|
||||
|
||||
raise ValueError('callable {0!r} is not supported by signature'.format(obj))
|
||||
|
||||
|
||||
class _void(object):
|
||||
'''A private marker - used in Parameter & Signature'''
|
||||
|
||||
|
||||
class _empty(object):
|
||||
pass
|
||||
|
||||
|
||||
class _ParameterKind(int):
|
||||
def __new__(self, *args, **kwargs):
|
||||
obj = int.__new__(self, *args)
|
||||
obj._name = kwargs['name']
|
||||
return obj
|
||||
|
||||
def __str__(self):
|
||||
return self._name
|
||||
|
||||
def __repr__(self):
|
||||
return '<_ParameterKind: {0!r}>'.format(self._name)
|
||||
|
||||
|
||||
_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY')
|
||||
_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD')
|
||||
_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL')
|
||||
_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY')
|
||||
_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD')
|
||||
|
||||
|
||||
class Parameter(object):
|
||||
'''Represents a parameter in a function signature.
|
||||
|
||||
Has the following public attributes:
|
||||
|
||||
* name : str
|
||||
The name of the parameter as a string.
|
||||
* default : object
|
||||
The default value for the parameter if specified. If the
|
||||
parameter has no default value, this attribute is not set.
|
||||
* annotation
|
||||
The annotation for the parameter if specified. If the
|
||||
parameter has no annotation, this attribute is not set.
|
||||
* kind : str
|
||||
Describes how argument values are bound to the parameter.
|
||||
Possible values: `Parameter.POSITIONAL_ONLY`,
|
||||
`Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
|
||||
`Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
|
||||
'''
|
||||
|
||||
__slots__ = ('_name', '_kind', '_default', '_annotation', '_partial_kwarg')
|
||||
|
||||
POSITIONAL_ONLY = _POSITIONAL_ONLY
|
||||
POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
|
||||
VAR_POSITIONAL = _VAR_POSITIONAL
|
||||
KEYWORD_ONLY = _KEYWORD_ONLY
|
||||
VAR_KEYWORD = _VAR_KEYWORD
|
||||
|
||||
empty = _empty
|
||||
|
||||
def __init__(self, name, kind, default=_empty, annotation=_empty,
|
||||
_partial_kwarg=False):
|
||||
|
||||
if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
|
||||
_VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
|
||||
raise ValueError("invalid value for 'Parameter.kind' attribute")
|
||||
self._kind = kind
|
||||
|
||||
if default is not _empty:
|
||||
if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
|
||||
msg = '{0} parameters cannot have default values'.format(kind)
|
||||
raise ValueError(msg)
|
||||
self._default = default
|
||||
self._annotation = annotation
|
||||
|
||||
if name is None:
|
||||
if kind != _POSITIONAL_ONLY:
|
||||
raise ValueError("None is not a valid name for a "
|
||||
"non-positional-only parameter")
|
||||
self._name = name
|
||||
else:
|
||||
name = str(name)
|
||||
if kind != _POSITIONAL_ONLY and not re.match(r'[a-z_]\w*$', name, re.I):
|
||||
msg = '{0!r} is not a valid parameter name'.format(name)
|
||||
raise ValueError(msg)
|
||||
self._name = name
|
||||
|
||||
self._partial_kwarg = _partial_kwarg
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def default(self):
|
||||
return self._default
|
||||
|
||||
@property
|
||||
def annotation(self):
|
||||
return self._annotation
|
||||
|
||||
@property
|
||||
def kind(self):
|
||||
return self._kind
|
||||
|
||||
def replace(self, name=_void, kind=_void, annotation=_void,
|
||||
default=_void, _partial_kwarg=_void):
|
||||
'''Creates a customized copy of the Parameter.'''
|
||||
|
||||
if name is _void:
|
||||
name = self._name
|
||||
|
||||
if kind is _void:
|
||||
kind = self._kind
|
||||
|
||||
if annotation is _void:
|
||||
annotation = self._annotation
|
||||
|
||||
if default is _void:
|
||||
default = self._default
|
||||
|
||||
if _partial_kwarg is _void:
|
||||
_partial_kwarg = self._partial_kwarg
|
||||
|
||||
return type(self)(name, kind, default=default, annotation=annotation,
|
||||
_partial_kwarg=_partial_kwarg)
|
||||
|
||||
def __str__(self):
|
||||
kind = self.kind
|
||||
|
||||
formatted = self._name
|
||||
if kind == _POSITIONAL_ONLY:
|
||||
if formatted is None:
|
||||
formatted = ''
|
||||
formatted = '<{0}>'.format(formatted)
|
||||
|
||||
# Add annotation and default value
|
||||
if self._annotation is not _empty:
|
||||
formatted = '{0}:{1}'.format(formatted,
|
||||
formatannotation(self._annotation))
|
||||
|
||||
if self._default is not _empty:
|
||||
formatted = '{0}={1}'.format(formatted, repr(self._default))
|
||||
|
||||
if kind == _VAR_POSITIONAL:
|
||||
formatted = '*' + formatted
|
||||
elif kind == _VAR_KEYWORD:
|
||||
formatted = '**' + formatted
|
||||
|
||||
return formatted
|
||||
|
||||
def __repr__(self):
|
||||
return '<{0} at {1:#x} {2!r}>'.format(self.__class__.__name__,
|
||||
id(self), self.name)
|
||||
|
||||
def __hash__(self):
|
||||
msg = "unhashable type: '{0}'".format(self.__class__.__name__)
|
||||
raise TypeError(msg)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (issubclass(other.__class__, Parameter) and
|
||||
self._name == other._name and
|
||||
self._kind == other._kind and
|
||||
self._default == other._default and
|
||||
self._annotation == other._annotation)
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
|
||||
class BoundArguments(object):
|
||||
'''Result of `Signature.bind` call. Holds the mapping of arguments
|
||||
to the function's parameters.
|
||||
|
||||
Has the following public attributes:
|
||||
|
||||
* arguments : OrderedDict
|
||||
An ordered mutable mapping of parameters' names to arguments' values.
|
||||
Does not contain arguments' default values.
|
||||
* signature : Signature
|
||||
The Signature object that created this instance.
|
||||
* args : tuple
|
||||
Tuple of positional arguments values.
|
||||
* kwargs : dict
|
||||
Dict of keyword arguments values.
|
||||
'''
|
||||
|
||||
def __init__(self, signature, arguments):
|
||||
self.arguments = arguments
|
||||
self._signature = signature
|
||||
|
||||
@property
|
||||
def signature(self):
|
||||
return self._signature
|
||||
|
||||
@property
|
||||
def args(self):
|
||||
args = []
|
||||
for param_name, param in self._signature.parameters.items():
|
||||
if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
|
||||
param._partial_kwarg):
|
||||
# Keyword arguments mapped by 'functools.partial'
|
||||
# (Parameter._partial_kwarg is True) are mapped
|
||||
# in 'BoundArguments.kwargs', along with VAR_KEYWORD &
|
||||
# KEYWORD_ONLY
|
||||
break
|
||||
|
||||
try:
|
||||
arg = self.arguments[param_name]
|
||||
except KeyError:
|
||||
# We're done here. Other arguments
|
||||
# will be mapped in 'BoundArguments.kwargs'
|
||||
break
|
||||
else:
|
||||
if param.kind == _VAR_POSITIONAL:
|
||||
# *args
|
||||
args.extend(arg)
|
||||
else:
|
||||
# plain argument
|
||||
args.append(arg)
|
||||
|
||||
return tuple(args)
|
||||
|
||||
@property
|
||||
def kwargs(self):
|
||||
kwargs = {}
|
||||
kwargs_started = False
|
||||
for param_name, param in self._signature.parameters.items():
|
||||
if not kwargs_started:
|
||||
if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
|
||||
param._partial_kwarg):
|
||||
kwargs_started = True
|
||||
else:
|
||||
if param_name not in self.arguments:
|
||||
kwargs_started = True
|
||||
continue
|
||||
|
||||
if not kwargs_started:
|
||||
continue
|
||||
|
||||
try:
|
||||
arg = self.arguments[param_name]
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
if param.kind == _VAR_KEYWORD:
|
||||
# **kwargs
|
||||
kwargs.update(arg)
|
||||
else:
|
||||
# plain keyword argument
|
||||
kwargs[param_name] = arg
|
||||
|
||||
return kwargs
|
||||
|
||||
def __hash__(self):
|
||||
msg = "unhashable type: '{0}'".format(self.__class__.__name__)
|
||||
raise TypeError(msg)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (issubclass(other.__class__, BoundArguments) and
|
||||
self.signature == other.signature and
|
||||
self.arguments == other.arguments)
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
|
||||
class Signature(object):
|
||||
'''A Signature object represents the overall signature of a function.
|
||||
It stores a Parameter object for each parameter accepted by the
|
||||
function, as well as information specific to the function itself.
|
||||
|
||||
A Signature object has the following public attributes and methods:
|
||||
|
||||
* parameters : OrderedDict
|
||||
An ordered mapping of parameters' names to the corresponding
|
||||
Parameter objects (keyword-only arguments are in the same order
|
||||
as listed in `code.co_varnames`).
|
||||
* return_annotation : object
|
||||
The annotation for the return type of the function if specified.
|
||||
If the function has no annotation for its return type, this
|
||||
attribute is not set.
|
||||
* bind(*args, **kwargs) -> BoundArguments
|
||||
Creates a mapping from positional and keyword arguments to
|
||||
parameters.
|
||||
* bind_partial(*args, **kwargs) -> BoundArguments
|
||||
Creates a partial mapping from positional and keyword arguments
|
||||
to parameters (simulating 'functools.partial' behavior.)
|
||||
'''
|
||||
|
||||
__slots__ = ('_return_annotation', '_parameters')
|
||||
|
||||
_parameter_cls = Parameter
|
||||
_bound_arguments_cls = BoundArguments
|
||||
|
||||
empty = _empty
|
||||
|
||||
def __init__(self, parameters=None, return_annotation=_empty,
|
||||
__validate_parameters__=True):
|
||||
'''Constructs Signature from the given list of Parameter
|
||||
objects and 'return_annotation'. All arguments are optional.
|
||||
'''
|
||||
|
||||
if parameters is None:
|
||||
params = OrderedDict()
|
||||
else:
|
||||
if __validate_parameters__:
|
||||
params = OrderedDict()
|
||||
top_kind = _POSITIONAL_ONLY
|
||||
|
||||
for idx, param in enumerate(parameters):
|
||||
kind = param.kind
|
||||
if kind < top_kind:
|
||||
msg = 'wrong parameter order: {0} before {1}'
|
||||
msg = msg.format(top_kind, param.kind)
|
||||
raise ValueError(msg)
|
||||
else:
|
||||
top_kind = kind
|
||||
|
||||
name = param.name
|
||||
if name is None:
|
||||
name = str(idx)
|
||||
param = param.replace(name=name)
|
||||
|
||||
if name in params:
|
||||
msg = 'duplicate parameter name: {0!r}'.format(name)
|
||||
raise ValueError(msg)
|
||||
params[name] = param
|
||||
else:
|
||||
params = OrderedDict(((param.name, param)
|
||||
for param in parameters))
|
||||
|
||||
self._parameters = params
|
||||
self._return_annotation = return_annotation
|
||||
|
||||
@classmethod
|
||||
def from_function(cls, func):
|
||||
'''Constructs Signature for the given python function'''
|
||||
|
||||
if not isinstance(func, types.FunctionType):
|
||||
raise TypeError('{0!r} is not a Python function'.format(func))
|
||||
|
||||
Parameter = cls._parameter_cls
|
||||
|
||||
# Parameter information.
|
||||
func_code = func.__code__
|
||||
pos_count = func_code.co_argcount
|
||||
arg_names = func_code.co_varnames
|
||||
positional = tuple(arg_names[:pos_count])
|
||||
keyword_only_count = getattr(func_code, 'co_kwonlyargcount', 0)
|
||||
keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
|
||||
annotations = getattr(func, '__annotations__', {})
|
||||
defaults = func.__defaults__
|
||||
kwdefaults = getattr(func, '__kwdefaults__', None)
|
||||
|
||||
if defaults:
|
||||
pos_default_count = len(defaults)
|
||||
else:
|
||||
pos_default_count = 0
|
||||
|
||||
parameters = []
|
||||
|
||||
# Non-keyword-only parameters w/o defaults.
|
||||
non_default_count = pos_count - pos_default_count
|
||||
for name in positional[:non_default_count]:
|
||||
annotation = annotations.get(name, _empty)
|
||||
parameters.append(Parameter(name, annotation=annotation,
|
||||
kind=_POSITIONAL_OR_KEYWORD))
|
||||
|
||||
# ... w/ defaults.
|
||||
for offset, name in enumerate(positional[non_default_count:]):
|
||||
annotation = annotations.get(name, _empty)
|
||||
parameters.append(Parameter(name, annotation=annotation,
|
||||
kind=_POSITIONAL_OR_KEYWORD,
|
||||
default=defaults[offset]))
|
||||
|
||||
# *args
|
||||
if func_code.co_flags & 0x04:
|
||||
name = arg_names[pos_count + keyword_only_count]
|
||||
annotation = annotations.get(name, _empty)
|
||||
parameters.append(Parameter(name, annotation=annotation,
|
||||
kind=_VAR_POSITIONAL))
|
||||
|
||||
# Keyword-only parameters.
|
||||
for name in keyword_only:
|
||||
default = _empty
|
||||
if kwdefaults is not None:
|
||||
default = kwdefaults.get(name, _empty)
|
||||
|
||||
annotation = annotations.get(name, _empty)
|
||||
parameters.append(Parameter(name, annotation=annotation,
|
||||
kind=_KEYWORD_ONLY,
|
||||
default=default))
|
||||
# **kwargs
|
||||
if func_code.co_flags & 0x08:
|
||||
index = pos_count + keyword_only_count
|
||||
if func_code.co_flags & 0x04:
|
||||
index += 1
|
||||
|
||||
name = arg_names[index]
|
||||
annotation = annotations.get(name, _empty)
|
||||
parameters.append(Parameter(name, annotation=annotation,
|
||||
kind=_VAR_KEYWORD))
|
||||
|
||||
return cls(parameters,
|
||||
return_annotation=annotations.get('return', _empty),
|
||||
__validate_parameters__=False)
|
||||
|
||||
@property
|
||||
def parameters(self):
|
||||
try:
|
||||
return types.MappingProxyType(self._parameters)
|
||||
except AttributeError:
|
||||
return OrderedDict(self._parameters.items())
|
||||
|
||||
@property
|
||||
def return_annotation(self):
|
||||
return self._return_annotation
|
||||
|
||||
def replace(self, parameters=_void, return_annotation=_void):
|
||||
'''Creates a customized copy of the Signature.
|
||||
Pass 'parameters' and/or 'return_annotation' arguments
|
||||
to override them in the new copy.
|
||||
'''
|
||||
|
||||
if parameters is _void:
|
||||
parameters = self.parameters.values()
|
||||
|
||||
if return_annotation is _void:
|
||||
return_annotation = self._return_annotation
|
||||
|
||||
return type(self)(parameters,
|
||||
return_annotation=return_annotation)
|
||||
|
||||
def __hash__(self):
|
||||
msg = "unhashable type: '{0}'".format(self.__class__.__name__)
|
||||
raise TypeError(msg)
|
||||
|
||||
def __eq__(self, other):
|
||||
if (not issubclass(type(other), Signature) or
|
||||
self.return_annotation != other.return_annotation or
|
||||
len(self.parameters) != len(other.parameters)):
|
||||
return False
|
||||
|
||||
other_positions = dict((param, idx)
|
||||
for idx, param in enumerate(other.parameters.keys()))
|
||||
|
||||
for idx, (param_name, param) in enumerate(self.parameters.items()):
|
||||
if param.kind == _KEYWORD_ONLY:
|
||||
try:
|
||||
other_param = other.parameters[param_name]
|
||||
except KeyError:
|
||||
return False
|
||||
else:
|
||||
if param != other_param:
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
other_idx = other_positions[param_name]
|
||||
except KeyError:
|
||||
return False
|
||||
else:
|
||||
if (idx != other_idx or
|
||||
param != other.parameters[param_name]):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def _bind(self, args, kwargs, partial=False):
|
||||
'''Private method. Don't use directly.'''
|
||||
|
||||
arguments = OrderedDict()
|
||||
|
||||
parameters = iter(self.parameters.values())
|
||||
parameters_ex = ()
|
||||
arg_vals = iter(args)
|
||||
|
||||
if partial:
|
||||
# Support for binding arguments to 'functools.partial' objects.
|
||||
# See 'functools.partial' case in 'signature()' implementation
|
||||
# for details.
|
||||
for param_name, param in self.parameters.items():
|
||||
if (param._partial_kwarg and param_name not in kwargs):
|
||||
# Simulating 'functools.partial' behavior
|
||||
kwargs[param_name] = param.default
|
||||
|
||||
while True:
|
||||
# Let's iterate through the positional arguments and corresponding
|
||||
# parameters
|
||||
try:
|
||||
arg_val = next(arg_vals)
|
||||
except StopIteration:
|
||||
# No more positional arguments
|
||||
try:
|
||||
param = next(parameters)
|
||||
except StopIteration:
|
||||
# No more parameters. That's it. Just need to check that
|
||||
# we have no `kwargs` after this while loop
|
||||
break
|
||||
else:
|
||||
if param.kind == _VAR_POSITIONAL:
|
||||
# That's OK, just empty *args. Let's start parsing
|
||||
# kwargs
|
||||
break
|
||||
elif param.name in kwargs:
|
||||
if param.kind == _POSITIONAL_ONLY:
|
||||
msg = '{arg!r} parameter is positional only, ' \
|
||||
'but was passed as a keyword'
|
||||
msg = msg.format(arg=param.name)
|
||||
raise TypeError(msg)
|
||||
parameters_ex = (param,)
|
||||
break
|
||||
elif (param.kind == _VAR_KEYWORD or
|
||||
param.default is not _empty):
|
||||
# That's fine too - we have a default value for this
|
||||
# parameter. So, lets start parsing `kwargs`, starting
|
||||
# with the current parameter
|
||||
parameters_ex = (param,)
|
||||
break
|
||||
else:
|
||||
if partial:
|
||||
parameters_ex = (param,)
|
||||
break
|
||||
else:
|
||||
msg = '{arg!r} parameter lacking default value'
|
||||
msg = msg.format(arg=param.name)
|
||||
raise TypeError(msg)
|
||||
else:
|
||||
# We have a positional argument to process
|
||||
try:
|
||||
param = next(parameters)
|
||||
except StopIteration:
|
||||
raise TypeError('too many positional arguments')
|
||||
else:
|
||||
if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
|
||||
# Looks like we have no parameter for this positional
|
||||
# argument
|
||||
raise TypeError('too many positional arguments')
|
||||
|
||||
if param.kind == _VAR_POSITIONAL:
|
||||
# We have an '*args'-like argument, let's fill it with
|
||||
# all positional arguments we have left and move on to
|
||||
# the next phase
|
||||
values = [arg_val]
|
||||
values.extend(arg_vals)
|
||||
arguments[param.name] = tuple(values)
|
||||
break
|
||||
|
||||
if param.name in kwargs:
|
||||
raise TypeError('multiple values for argument '
|
||||
'{arg!r}'.format(arg=param.name))
|
||||
|
||||
arguments[param.name] = arg_val
|
||||
|
||||
# Now, we iterate through the remaining parameters to process
|
||||
# keyword arguments
|
||||
kwargs_param = None
|
||||
for param in itertools.chain(parameters_ex, parameters):
|
||||
if param.kind == _POSITIONAL_ONLY:
|
||||
# This should never happen in case of a properly built
|
||||
# Signature object (but let's have this check here
|
||||
# to ensure correct behaviour just in case)
|
||||
raise TypeError('{arg!r} parameter is positional only, '
|
||||
'but was passed as a keyword'. \
|
||||
format(arg=param.name))
|
||||
|
||||
if param.kind == _VAR_KEYWORD:
|
||||
# Memorize that we have a '**kwargs'-like parameter
|
||||
kwargs_param = param
|
||||
continue
|
||||
|
||||
param_name = param.name
|
||||
try:
|
||||
arg_val = kwargs.pop(param_name)
|
||||
except KeyError:
|
||||
# We have no value for this parameter. It's fine though,
|
||||
# if it has a default value, or it is an '*args'-like
|
||||
# parameter, left alone by the processing of positional
|
||||
# arguments.
|
||||
if (not partial and param.kind != _VAR_POSITIONAL and
|
||||
param.default is _empty):
|
||||
raise TypeError('{arg!r} parameter lacking default value'. \
|
||||
format(arg=param_name))
|
||||
|
||||
else:
|
||||
arguments[param_name] = arg_val
|
||||
|
||||
if kwargs:
|
||||
if kwargs_param is not None:
|
||||
# Process our '**kwargs'-like parameter
|
||||
arguments[kwargs_param.name] = kwargs
|
||||
else:
|
||||
raise TypeError('too many keyword arguments %r' % kwargs)
|
||||
|
||||
return self._bound_arguments_cls(self, arguments)
|
||||
|
||||
def bind(*args, **kwargs):
|
||||
'''Get a BoundArguments object, that maps the passed `args`
|
||||
and `kwargs` to the function's signature. Raises `TypeError`
|
||||
if the passed arguments can not be bound.
|
||||
'''
|
||||
return args[0]._bind(args[1:], kwargs)
|
||||
|
||||
def bind_partial(self, *args, **kwargs):
|
||||
'''Get a BoundArguments object, that partially maps the
|
||||
passed `args` and `kwargs` to the function's signature.
|
||||
Raises `TypeError` if the passed arguments can not be bound.
|
||||
'''
|
||||
return self._bind(args, kwargs, partial=True)
|
||||
|
||||
def __str__(self):
|
||||
result = []
|
||||
render_kw_only_separator = True
|
||||
for idx, param in enumerate(self.parameters.values()):
|
||||
formatted = str(param)
|
||||
|
||||
kind = param.kind
|
||||
if kind == _VAR_POSITIONAL:
|
||||
# OK, we have an '*args'-like parameter, so we won't need
|
||||
# a '*' to separate keyword-only arguments
|
||||
render_kw_only_separator = False
|
||||
elif kind == _KEYWORD_ONLY and render_kw_only_separator:
|
||||
# We have a keyword-only parameter to render and we haven't
|
||||
# rendered an '*args'-like parameter before, so add a '*'
|
||||
# separator to the parameters list ("foo(arg1, *, arg2)" case)
|
||||
result.append('*')
|
||||
# This condition should be only triggered once, so
|
||||
# reset the flag
|
||||
render_kw_only_separator = False
|
||||
|
||||
result.append(formatted)
|
||||
|
||||
rendered = '({0})'.format(', '.join(result))
|
||||
|
||||
if self.return_annotation is not _empty:
|
||||
anno = formatannotation(self.return_annotation)
|
||||
rendered += ' -> {0}'.format(anno)
|
||||
|
||||
return rendered
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
__version__ = "1.0.2"
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
Copyright 2017-2019 Jason R. Coombs, Barry Warsaw
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+554
@@ -0,0 +1,554 @@
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import abc
|
||||
import csv
|
||||
import sys
|
||||
import zipp
|
||||
import operator
|
||||
import functools
|
||||
import itertools
|
||||
import collections
|
||||
|
||||
from ._compat import (
|
||||
install,
|
||||
NullFinder,
|
||||
ConfigParser,
|
||||
suppress,
|
||||
map,
|
||||
FileNotFoundError,
|
||||
IsADirectoryError,
|
||||
NotADirectoryError,
|
||||
PermissionError,
|
||||
pathlib,
|
||||
PYPY_OPEN_BUG,
|
||||
ModuleNotFoundError,
|
||||
MetaPathFinder,
|
||||
email_message_from_string,
|
||||
PyPy_repr,
|
||||
)
|
||||
from importlib import import_module
|
||||
from itertools import starmap
|
||||
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
__all__ = [
|
||||
'Distribution',
|
||||
'DistributionFinder',
|
||||
'PackageNotFoundError',
|
||||
'distribution',
|
||||
'distributions',
|
||||
'entry_points',
|
||||
'files',
|
||||
'metadata',
|
||||
'requires',
|
||||
'version',
|
||||
]
|
||||
|
||||
|
||||
class PackageNotFoundError(ModuleNotFoundError):
|
||||
"""The package was not found."""
|
||||
|
||||
|
||||
class EntryPoint(
|
||||
PyPy_repr,
|
||||
collections.namedtuple('EntryPointBase', 'name value group')):
|
||||
"""An entry point as defined by Python packaging conventions.
|
||||
|
||||
See `the packaging docs on entry points
|
||||
<https://packaging.python.org/specifications/entry-points/>`_
|
||||
for more information.
|
||||
"""
|
||||
|
||||
pattern = re.compile(
|
||||
r'(?P<module>[\w.]+)\s*'
|
||||
r'(:\s*(?P<attr>[\w.]+))?\s*'
|
||||
r'(?P<extras>\[.*\])?\s*$'
|
||||
)
|
||||
"""
|
||||
A regular expression describing the syntax for an entry point,
|
||||
which might look like:
|
||||
|
||||
- module
|
||||
- package.module
|
||||
- package.module:attribute
|
||||
- package.module:object.attribute
|
||||
- package.module:attr [extra1, extra2]
|
||||
|
||||
Other combinations are possible as well.
|
||||
|
||||
The expression is lenient about whitespace around the ':',
|
||||
following the attr, and following any extras.
|
||||
"""
|
||||
|
||||
def load(self):
|
||||
"""Load the entry point from its definition. If only a module
|
||||
is indicated by the value, return that module. Otherwise,
|
||||
return the named object.
|
||||
"""
|
||||
match = self.pattern.match(self.value)
|
||||
module = import_module(match.group('module'))
|
||||
attrs = filter(None, (match.group('attr') or '').split('.'))
|
||||
return functools.reduce(getattr, attrs, module)
|
||||
|
||||
@property
|
||||
def extras(self):
|
||||
match = self.pattern.match(self.value)
|
||||
return list(re.finditer(r'\w+', match.group('extras') or ''))
|
||||
|
||||
@classmethod
|
||||
def _from_config(cls, config):
|
||||
return [
|
||||
cls(name, value, group)
|
||||
for group in config.sections()
|
||||
for name, value in config.items(group)
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _from_text(cls, text):
|
||||
config = ConfigParser(delimiters='=')
|
||||
# case sensitive: https://stackoverflow.com/q/1611799/812183
|
||||
config.optionxform = str
|
||||
try:
|
||||
config.read_string(text)
|
||||
except AttributeError: # pragma: nocover
|
||||
# Python 2 has no read_string
|
||||
config.readfp(io.StringIO(text))
|
||||
return EntryPoint._from_config(config)
|
||||
|
||||
def __iter__(self):
|
||||
"""
|
||||
Supply iter so one may construct dicts of EntryPoints easily.
|
||||
"""
|
||||
return iter((self.name, self))
|
||||
|
||||
def __reduce__(self):
|
||||
return (
|
||||
self.__class__,
|
||||
(self.name, self.value, self.group),
|
||||
)
|
||||
|
||||
|
||||
class PackagePath(pathlib.PurePosixPath):
|
||||
"""A reference to a path in a package"""
|
||||
|
||||
def read_text(self, encoding='utf-8'):
|
||||
with self.locate().open(encoding=encoding) as stream:
|
||||
return stream.read()
|
||||
|
||||
def read_binary(self):
|
||||
with self.locate().open('rb') as stream:
|
||||
return stream.read()
|
||||
|
||||
def locate(self):
|
||||
"""Return a path-like object for this path"""
|
||||
return self.dist.locate_file(self)
|
||||
|
||||
|
||||
class FileHash:
|
||||
def __init__(self, spec):
|
||||
self.mode, _, self.value = spec.partition('=')
|
||||
|
||||
def __repr__(self):
|
||||
return '<FileHash mode: {} value: {}>'.format(self.mode, self.value)
|
||||
|
||||
|
||||
class Distribution:
|
||||
"""A Python distribution package."""
|
||||
|
||||
@abc.abstractmethod
|
||||
def read_text(self, filename):
|
||||
"""Attempt to load metadata file given by the name.
|
||||
|
||||
:param filename: The name of the file in the distribution info.
|
||||
:return: The text if found, otherwise None.
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def locate_file(self, path):
|
||||
"""
|
||||
Given a path to a file in this distribution, return a path
|
||||
to it.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def from_name(cls, name):
|
||||
"""Return the Distribution for the given package name.
|
||||
|
||||
:param name: The name of the distribution package to search for.
|
||||
:return: The Distribution instance (or subclass thereof) for the named
|
||||
package, if found.
|
||||
:raises PackageNotFoundError: When the named package's distribution
|
||||
metadata cannot be found.
|
||||
"""
|
||||
for resolver in cls._discover_resolvers():
|
||||
dists = resolver(DistributionFinder.Context(name=name))
|
||||
dist = next(dists, None)
|
||||
if dist is not None:
|
||||
return dist
|
||||
else:
|
||||
raise PackageNotFoundError(name)
|
||||
|
||||
@classmethod
|
||||
def discover(cls, **kwargs):
|
||||
"""Return an iterable of Distribution objects for all packages.
|
||||
|
||||
Pass a ``context`` or pass keyword arguments for constructing
|
||||
a context.
|
||||
|
||||
:context: A ``DistributionFinder.Context`` object.
|
||||
:return: Iterable of Distribution objects for all packages.
|
||||
"""
|
||||
context = kwargs.pop('context', None)
|
||||
if context and kwargs:
|
||||
raise ValueError("cannot accept context and kwargs")
|
||||
context = context or DistributionFinder.Context(**kwargs)
|
||||
return itertools.chain.from_iterable(
|
||||
resolver(context)
|
||||
for resolver in cls._discover_resolvers()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def at(path):
|
||||
"""Return a Distribution for the indicated metadata path
|
||||
|
||||
:param path: a string or path-like object
|
||||
:return: a concrete Distribution instance for the path
|
||||
"""
|
||||
return PathDistribution(pathlib.Path(path))
|
||||
|
||||
@staticmethod
|
||||
def _discover_resolvers():
|
||||
"""Search the meta_path for resolvers."""
|
||||
declared = (
|
||||
getattr(finder, 'find_distributions', None)
|
||||
for finder in sys.meta_path
|
||||
)
|
||||
return filter(None, declared)
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
"""Return the parsed metadata for this Distribution.
|
||||
|
||||
The returned object will have keys that name the various bits of
|
||||
metadata. See PEP 566 for details.
|
||||
"""
|
||||
text = (
|
||||
self.read_text('METADATA')
|
||||
or self.read_text('PKG-INFO')
|
||||
# This last clause is here to support old egg-info files. Its
|
||||
# effect is to just end up using the PathDistribution's self._path
|
||||
# (which points to the egg-info file) attribute unchanged.
|
||||
or self.read_text('')
|
||||
)
|
||||
return email_message_from_string(text)
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
"""Return the 'Version' metadata for the distribution package."""
|
||||
return self.metadata['Version']
|
||||
|
||||
@property
|
||||
def entry_points(self):
|
||||
return EntryPoint._from_text(self.read_text('entry_points.txt'))
|
||||
|
||||
@property
|
||||
def files(self):
|
||||
"""Files in this distribution.
|
||||
|
||||
:return: List of PackagePath for this distribution or None
|
||||
|
||||
Result is `None` if the metadata file that enumerates files
|
||||
(i.e. RECORD for dist-info or SOURCES.txt for egg-info) is
|
||||
missing.
|
||||
Result may be empty if the metadata exists but is empty.
|
||||
"""
|
||||
file_lines = self._read_files_distinfo() or self._read_files_egginfo()
|
||||
|
||||
def make_file(name, hash=None, size_str=None):
|
||||
result = PackagePath(name)
|
||||
result.hash = FileHash(hash) if hash else None
|
||||
result.size = int(size_str) if size_str else None
|
||||
result.dist = self
|
||||
return result
|
||||
|
||||
return file_lines and list(starmap(make_file, csv.reader(file_lines)))
|
||||
|
||||
def _read_files_distinfo(self):
|
||||
"""
|
||||
Read the lines of RECORD
|
||||
"""
|
||||
text = self.read_text('RECORD')
|
||||
return text and text.splitlines()
|
||||
|
||||
def _read_files_egginfo(self):
|
||||
"""
|
||||
SOURCES.txt might contain literal commas, so wrap each line
|
||||
in quotes.
|
||||
"""
|
||||
text = self.read_text('SOURCES.txt')
|
||||
return text and map('"{}"'.format, text.splitlines())
|
||||
|
||||
@property
|
||||
def requires(self):
|
||||
"""Generated requirements specified for this Distribution"""
|
||||
reqs = self._read_dist_info_reqs() or self._read_egg_info_reqs()
|
||||
return reqs and list(reqs)
|
||||
|
||||
def _read_dist_info_reqs(self):
|
||||
return self.metadata.get_all('Requires-Dist')
|
||||
|
||||
def _read_egg_info_reqs(self):
|
||||
source = self.read_text('requires.txt')
|
||||
return source and self._deps_from_requires_text(source)
|
||||
|
||||
@classmethod
|
||||
def _deps_from_requires_text(cls, source):
|
||||
section_pairs = cls._read_sections(source.splitlines())
|
||||
sections = {
|
||||
section: list(map(operator.itemgetter('line'), results))
|
||||
for section, results in
|
||||
itertools.groupby(section_pairs, operator.itemgetter('section'))
|
||||
}
|
||||
return cls._convert_egg_info_reqs_to_simple_reqs(sections)
|
||||
|
||||
@staticmethod
|
||||
def _read_sections(lines):
|
||||
section = None
|
||||
for line in filter(None, lines):
|
||||
section_match = re.match(r'\[(.*)\]$', line)
|
||||
if section_match:
|
||||
section = section_match.group(1)
|
||||
continue
|
||||
yield locals()
|
||||
|
||||
@staticmethod
|
||||
def _convert_egg_info_reqs_to_simple_reqs(sections):
|
||||
"""
|
||||
Historically, setuptools would solicit and store 'extra'
|
||||
requirements, including those with environment markers,
|
||||
in separate sections. More modern tools expect each
|
||||
dependency to be defined separately, with any relevant
|
||||
extras and environment markers attached directly to that
|
||||
requirement. This method converts the former to the
|
||||
latter. See _test_deps_from_requires_text for an example.
|
||||
"""
|
||||
def make_condition(name):
|
||||
return name and 'extra == "{name}"'.format(name=name)
|
||||
|
||||
def parse_condition(section):
|
||||
section = section or ''
|
||||
extra, sep, markers = section.partition(':')
|
||||
if extra and markers:
|
||||
markers = '({markers})'.format(markers=markers)
|
||||
conditions = list(filter(None, [markers, make_condition(extra)]))
|
||||
return '; ' + ' and '.join(conditions) if conditions else ''
|
||||
|
||||
for section, deps in sections.items():
|
||||
for dep in deps:
|
||||
yield dep + parse_condition(section)
|
||||
|
||||
|
||||
class DistributionFinder(MetaPathFinder):
|
||||
"""
|
||||
A MetaPathFinder capable of discovering installed distributions.
|
||||
"""
|
||||
|
||||
class Context:
|
||||
"""
|
||||
Keyword arguments presented by the caller to
|
||||
``distributions()`` or ``Distribution.discover()``
|
||||
to narrow the scope of a search for distributions
|
||||
in all DistributionFinders.
|
||||
|
||||
Each DistributionFinder may expect any parameters
|
||||
and should attempt to honor the canonical
|
||||
parameters defined below when appropriate.
|
||||
"""
|
||||
|
||||
name = None
|
||||
"""
|
||||
Specific name for which a distribution finder should match.
|
||||
A name of ``None`` matches all distributions.
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
vars(self).update(kwargs)
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
"""
|
||||
The path that a distribution finder should search.
|
||||
|
||||
Typically refers to Python package paths and defaults
|
||||
to ``sys.path``.
|
||||
"""
|
||||
return vars(self).get('path', sys.path)
|
||||
|
||||
@property
|
||||
def pattern(self):
|
||||
return '.*' if self.name is None else re.escape(self.name)
|
||||
|
||||
@abc.abstractmethod
|
||||
def find_distributions(self, context=Context()):
|
||||
"""
|
||||
Find distributions.
|
||||
|
||||
Return an iterable of all Distribution instances capable of
|
||||
loading the metadata for packages matching the ``context``,
|
||||
a DistributionFinder.Context instance.
|
||||
"""
|
||||
|
||||
|
||||
@install
|
||||
class MetadataPathFinder(NullFinder, DistributionFinder):
|
||||
"""A degenerate finder for distribution packages on the file system.
|
||||
|
||||
This finder supplies only a find_distributions() method for versions
|
||||
of Python that do not have a PathFinder find_distributions().
|
||||
"""
|
||||
|
||||
def find_distributions(self, context=DistributionFinder.Context()):
|
||||
"""
|
||||
Find distributions.
|
||||
|
||||
Return an iterable of all Distribution instances capable of
|
||||
loading the metadata for packages matching ``context.name``
|
||||
(or all names if ``None`` indicated) along the paths in the list
|
||||
of directories ``context.path``.
|
||||
"""
|
||||
found = self._search_paths(context.pattern, context.path)
|
||||
return map(PathDistribution, found)
|
||||
|
||||
@classmethod
|
||||
def _search_paths(cls, pattern, paths):
|
||||
"""Find metadata directories in paths heuristically."""
|
||||
return itertools.chain.from_iterable(
|
||||
cls._search_path(path, pattern)
|
||||
for path in map(cls._switch_path, paths)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _switch_path(path):
|
||||
if not PYPY_OPEN_BUG or os.path.isfile(path): # pragma: no branch
|
||||
with suppress(Exception):
|
||||
return zipp.Path(path)
|
||||
return pathlib.Path(path)
|
||||
|
||||
@classmethod
|
||||
def _matches_info(cls, normalized, item):
|
||||
template = r'{pattern}(-.*)?\.(dist|egg)-info'
|
||||
manifest = template.format(pattern=normalized)
|
||||
return re.match(manifest, item.name, flags=re.IGNORECASE)
|
||||
|
||||
@classmethod
|
||||
def _matches_legacy(cls, normalized, item):
|
||||
template = r'{pattern}-.*\.egg[\\/]EGG-INFO'
|
||||
manifest = template.format(pattern=normalized)
|
||||
return re.search(manifest, str(item), flags=re.IGNORECASE)
|
||||
|
||||
@classmethod
|
||||
def _search_path(cls, root, pattern):
|
||||
if not root.is_dir():
|
||||
return ()
|
||||
normalized = pattern.replace('-', '_')
|
||||
return (item for item in root.iterdir()
|
||||
if cls._matches_info(normalized, item)
|
||||
or cls._matches_legacy(normalized, item))
|
||||
|
||||
|
||||
class PathDistribution(Distribution):
|
||||
def __init__(self, path):
|
||||
"""Construct a distribution from a path to the metadata directory.
|
||||
|
||||
:param path: A pathlib.Path or similar object supporting
|
||||
.joinpath(), __div__, .parent, and .read_text().
|
||||
"""
|
||||
self._path = path
|
||||
|
||||
def read_text(self, filename):
|
||||
with suppress(FileNotFoundError, IsADirectoryError, KeyError,
|
||||
NotADirectoryError, PermissionError):
|
||||
return self._path.joinpath(filename).read_text(encoding='utf-8')
|
||||
read_text.__doc__ = Distribution.read_text.__doc__
|
||||
|
||||
def locate_file(self, path):
|
||||
return self._path.parent / path
|
||||
|
||||
|
||||
def distribution(distribution_name):
|
||||
"""Get the ``Distribution`` instance for the named package.
|
||||
|
||||
:param distribution_name: The name of the distribution package as a string.
|
||||
:return: A ``Distribution`` instance (or subclass thereof).
|
||||
"""
|
||||
return Distribution.from_name(distribution_name)
|
||||
|
||||
|
||||
def distributions(**kwargs):
|
||||
"""Get all ``Distribution`` instances in the current environment.
|
||||
|
||||
:return: An iterable of ``Distribution`` instances.
|
||||
"""
|
||||
return Distribution.discover(**kwargs)
|
||||
|
||||
|
||||
def metadata(distribution_name):
|
||||
"""Get the metadata for the named package.
|
||||
|
||||
:param distribution_name: The name of the distribution package to query.
|
||||
:return: An email.Message containing the parsed metadata.
|
||||
"""
|
||||
return Distribution.from_name(distribution_name).metadata
|
||||
|
||||
|
||||
def version(distribution_name):
|
||||
"""Get the version string for the named package.
|
||||
|
||||
:param distribution_name: The name of the distribution package to query.
|
||||
:return: The version string for the package as defined in the package's
|
||||
"Version" metadata key.
|
||||
"""
|
||||
return distribution(distribution_name).version
|
||||
|
||||
|
||||
def entry_points():
|
||||
"""Return EntryPoint objects for all installed packages.
|
||||
|
||||
:return: EntryPoint objects for all installed packages.
|
||||
"""
|
||||
eps = itertools.chain.from_iterable(
|
||||
dist.entry_points for dist in distributions())
|
||||
by_group = operator.attrgetter('group')
|
||||
ordered = sorted(eps, key=by_group)
|
||||
grouped = itertools.groupby(ordered, by_group)
|
||||
return {
|
||||
group: tuple(eps)
|
||||
for group, eps in grouped
|
||||
}
|
||||
|
||||
|
||||
def files(distribution_name):
|
||||
"""Return a list of files for the named package.
|
||||
|
||||
:param distribution_name: The name of the distribution package to query.
|
||||
:return: List of files composing the distribution.
|
||||
"""
|
||||
return distribution(distribution_name).files
|
||||
|
||||
|
||||
def requires(distribution_name):
|
||||
"""
|
||||
Return a list of requirements for the named package.
|
||||
|
||||
:return: An iterator of requirements, suitable for
|
||||
packaging.requirement.Requirement.
|
||||
"""
|
||||
return distribution(distribution_name).requires
|
||||
|
||||
|
||||
__version__ = version(__name__)
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import io
|
||||
import abc
|
||||
import sys
|
||||
import email
|
||||
|
||||
|
||||
if sys.version_info > (3,): # pragma: nocover
|
||||
import builtins
|
||||
from configparser import ConfigParser
|
||||
from contextlib import suppress
|
||||
FileNotFoundError = builtins.FileNotFoundError
|
||||
IsADirectoryError = builtins.IsADirectoryError
|
||||
NotADirectoryError = builtins.NotADirectoryError
|
||||
PermissionError = builtins.PermissionError
|
||||
map = builtins.map
|
||||
else: # pragma: nocover
|
||||
from backports.configparser import ConfigParser
|
||||
from itertools import imap as map # type: ignore
|
||||
from contextlib2 import suppress # noqa
|
||||
FileNotFoundError = IOError, OSError
|
||||
IsADirectoryError = IOError, OSError
|
||||
NotADirectoryError = IOError, OSError
|
||||
PermissionError = IOError, OSError
|
||||
|
||||
if sys.version_info > (3, 5): # pragma: nocover
|
||||
import pathlib
|
||||
else: # pragma: nocover
|
||||
import pathlib2 as pathlib
|
||||
|
||||
try:
|
||||
ModuleNotFoundError = builtins.FileNotFoundError
|
||||
except (NameError, AttributeError): # pragma: nocover
|
||||
ModuleNotFoundError = ImportError # type: ignore
|
||||
|
||||
|
||||
if sys.version_info >= (3,): # pragma: nocover
|
||||
from importlib.abc import MetaPathFinder
|
||||
else: # pragma: nocover
|
||||
class MetaPathFinder(object):
|
||||
__metaclass__ = abc.ABCMeta
|
||||
|
||||
|
||||
__metaclass__ = type
|
||||
__all__ = [
|
||||
'install', 'NullFinder', 'MetaPathFinder', 'ModuleNotFoundError',
|
||||
'pathlib', 'ConfigParser', 'map', 'suppress', 'FileNotFoundError',
|
||||
'NotADirectoryError', 'email_message_from_string',
|
||||
]
|
||||
|
||||
|
||||
def install(cls):
|
||||
"""
|
||||
Class decorator for installation on sys.meta_path.
|
||||
|
||||
Adds the backport DistributionFinder to sys.meta_path and
|
||||
attempts to disable the finder functionality of the stdlib
|
||||
DistributionFinder.
|
||||
"""
|
||||
sys.meta_path.append(cls())
|
||||
disable_stdlib_finder()
|
||||
return cls
|
||||
|
||||
|
||||
def disable_stdlib_finder():
|
||||
"""
|
||||
Give the backport primacy for discovering path-based distributions
|
||||
by monkey-patching the stdlib O_O.
|
||||
|
||||
See #91 for more background for rationale on this sketchy
|
||||
behavior.
|
||||
"""
|
||||
def matches(finder):
|
||||
return (
|
||||
finder.__module__ == '_frozen_importlib_external'
|
||||
and hasattr(finder, 'find_distributions')
|
||||
)
|
||||
for finder in filter(matches, sys.meta_path): # pragma: nocover
|
||||
del finder.find_distributions
|
||||
|
||||
|
||||
class NullFinder:
|
||||
"""
|
||||
A "Finder" (aka "MetaClassFinder") that never finds any modules,
|
||||
but may find distributions.
|
||||
"""
|
||||
@staticmethod
|
||||
def find_spec(*args, **kwargs):
|
||||
return None
|
||||
|
||||
# In Python 2, the import system requires finders
|
||||
# to have a find_module() method, but this usage
|
||||
# is deprecated in Python 3 in favor of find_spec().
|
||||
# For the purposes of this finder (i.e. being present
|
||||
# on sys.meta_path but having no other import
|
||||
# system functionality), the two methods are identical.
|
||||
find_module = find_spec
|
||||
|
||||
|
||||
def py2_message_from_string(text): # nocoverpy3
|
||||
# Work around https://bugs.python.org/issue25545 where
|
||||
# email.message_from_string cannot handle Unicode on Python 2.
|
||||
io_buffer = io.StringIO(text)
|
||||
return email.message_from_file(io_buffer)
|
||||
|
||||
|
||||
email_message_from_string = (
|
||||
py2_message_from_string
|
||||
if sys.version_info < (3,) else
|
||||
email.message_from_string
|
||||
)
|
||||
|
||||
# https://bitbucket.org/pypy/pypy/issues/3021/ioopen-directory-leaks-a-file-descriptor
|
||||
PYPY_OPEN_BUG = getattr(sys, 'pypy_version_info', (9, 9, 9))[:3] <= (7, 1, 1)
|
||||
|
||||
|
||||
class PyPy_repr:
|
||||
"""
|
||||
Override repr for EntryPoint objects on PyPy to avoid __iter__ access.
|
||||
Ref #97, #102.
|
||||
"""
|
||||
affected = hasattr(sys, 'pypy_version_info')
|
||||
|
||||
def __compat_repr__(self): # pragma: nocover
|
||||
def make_param(name):
|
||||
value = getattr(self, name)
|
||||
return '{name}={value!r}'.format(**locals())
|
||||
params = ', '.join(map(make_param, self._fields))
|
||||
return 'EntryPoint({params})'.format(**locals())
|
||||
|
||||
if affected: # pragma: nocover
|
||||
__repr__ = __compat_repr__
|
||||
del affected
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
=========================
|
||||
importlib_metadata NEWS
|
||||
=========================
|
||||
|
||||
v1.3.0
|
||||
======
|
||||
|
||||
* Improve custom finders documentation. Closes #105.
|
||||
|
||||
v1.2.0
|
||||
======
|
||||
|
||||
* Once again, drop support for Python 3.4. Ref #104.
|
||||
|
||||
v1.1.3
|
||||
======
|
||||
|
||||
* Restored support for Python 3.4 due to improper version
|
||||
compatibility declarations in the v1.1.0 and v1.1.1
|
||||
releases. Closes #104.
|
||||
|
||||
v1.1.2
|
||||
======
|
||||
|
||||
* Repaired project metadata to correctly declare the
|
||||
``python_requires`` directive. Closes #103.
|
||||
|
||||
v1.1.1
|
||||
======
|
||||
|
||||
* Fixed ``repr(EntryPoint)`` on PyPy 3 also. Closes #102.
|
||||
|
||||
v1.1.0
|
||||
======
|
||||
|
||||
* Dropped support for Python 3.4.
|
||||
* EntryPoints are now pickleable. Closes #96.
|
||||
* Fixed ``repr(EntryPoint)`` on PyPy 2. Closes #97.
|
||||
|
||||
v1.0.0
|
||||
======
|
||||
|
||||
* Project adopts semver for versioning.
|
||||
|
||||
* Removed compatibility shim introduced in 0.23.
|
||||
|
||||
* For better compatibility with the stdlib implementation and to
|
||||
avoid the same distributions being discovered by the stdlib and
|
||||
backport implementations, the backport now disables the
|
||||
stdlib DistributionFinder during initialization (import time).
|
||||
Closes #91 and closes #100.
|
||||
|
||||
0.23
|
||||
====
|
||||
* Added a compatibility shim to prevent failures on beta releases
|
||||
of Python before the signature changed to accept the
|
||||
"context" parameter on find_distributions. This workaround
|
||||
will have a limited lifespan, not to extend beyond release of
|
||||
Python 3.8 final.
|
||||
|
||||
0.22
|
||||
====
|
||||
* Renamed ``package`` parameter to ``distribution_name``
|
||||
as `recommended <https://bugs.python.org/issue34632#msg349423>`_
|
||||
in the following functions: ``distribution``, ``metadata``,
|
||||
``version``, ``files``, and ``requires``. This
|
||||
backward-incompatible change is expected to have little impact
|
||||
as these functions are assumed to be primarily used with
|
||||
positional parameters.
|
||||
|
||||
0.21
|
||||
====
|
||||
* ``importlib.metadata`` now exposes the ``DistributionFinder``
|
||||
metaclass and references it in the docs for extending the
|
||||
search algorithm.
|
||||
* Add ``Distribution.at`` for constructing a Distribution object
|
||||
from a known metadata directory on the file system. Closes #80.
|
||||
* Distribution finders now receive a context object that
|
||||
supplies ``.path`` and ``.name`` properties. This change
|
||||
introduces a fundamental backward incompatibility for
|
||||
any projects implementing a ``find_distributions`` method
|
||||
on a ``MetaPathFinder``. This new layer of abstraction
|
||||
allows this context to be supplied directly or constructed
|
||||
on demand and opens the opportunity for a
|
||||
``find_distributions`` method to solicit additional
|
||||
context from the caller. Closes #85.
|
||||
|
||||
0.20
|
||||
====
|
||||
* Clarify in the docs that calls to ``.files`` could return
|
||||
``None`` when the metadata is not present. Closes #69.
|
||||
* Return all requirements and not just the first for dist-info
|
||||
packages. Closes #67.
|
||||
|
||||
0.19
|
||||
====
|
||||
* Restrain over-eager egg metadata resolution.
|
||||
* Add support for entry points with colons in the name. Closes #75.
|
||||
|
||||
0.18
|
||||
====
|
||||
* Parse entry points case sensitively. Closes #68
|
||||
* Add a version constraint on the backport configparser package. Closes #66
|
||||
|
||||
0.17
|
||||
====
|
||||
* Fix a permission problem in the tests on Windows.
|
||||
|
||||
0.16
|
||||
====
|
||||
* Don't crash if there exists an EGG-INFO directory on sys.path.
|
||||
|
||||
0.15
|
||||
====
|
||||
* Fix documentation.
|
||||
|
||||
0.14
|
||||
====
|
||||
* Removed ``local_distribution`` function from the API.
|
||||
**This backward-incompatible change removes this
|
||||
behavior summarily**. Projects should remove their
|
||||
reliance on this behavior. A replacement behavior is
|
||||
under review in the `pep517 project
|
||||
<https://github.com/pypa/pep517>`_. Closes #42.
|
||||
|
||||
0.13
|
||||
====
|
||||
* Update docstrings to match PEP 8. Closes #63.
|
||||
* Merged modules into one module. Closes #62.
|
||||
|
||||
0.12
|
||||
====
|
||||
* Add support for eggs. !65; Closes #19.
|
||||
|
||||
0.11
|
||||
====
|
||||
* Support generic zip files (not just wheels). Closes #59
|
||||
* Support zip files with multiple distributions in them. Closes #60
|
||||
* Fully expose the public API in ``importlib_metadata.__all__``.
|
||||
|
||||
0.10
|
||||
====
|
||||
* The ``Distribution`` ABC is now officially part of the public API.
|
||||
Closes #37.
|
||||
* Fixed support for older single file egg-info formats. Closes #43.
|
||||
* Fixed a testing bug when ``$CWD`` has spaces in the path. Closes #50.
|
||||
* Add Python 3.8 to the ``tox`` testing matrix.
|
||||
|
||||
0.9
|
||||
===
|
||||
* Fixed issue where entry points without an attribute would raise an
|
||||
Exception. Closes #40.
|
||||
* Removed unused ``name`` parameter from ``entry_points()``. Closes #44.
|
||||
* ``DistributionFinder`` classes must now be instantiated before
|
||||
being placed on ``sys.meta_path``.
|
||||
|
||||
0.8
|
||||
===
|
||||
* This library can now discover/enumerate all installed packages. **This
|
||||
backward-incompatible change alters the protocol finders must
|
||||
implement to support distribution package discovery.** Closes #24.
|
||||
* The signature of ``find_distributions()`` on custom installer finders
|
||||
should now accept two parameters, ``name`` and ``path`` and
|
||||
these parameters must supply defaults.
|
||||
* The ``entry_points()`` method no longer accepts a package name
|
||||
but instead returns all entry points in a dictionary keyed by the
|
||||
``EntryPoint.group``. The ``resolve`` method has been removed. Instead,
|
||||
call ``EntryPoint.load()``, which has the same semantics as
|
||||
``pkg_resources`` and ``entrypoints``. **This is a backward incompatible
|
||||
change.**
|
||||
* Metadata is now always returned as Unicode text regardless of
|
||||
Python version. Closes #29.
|
||||
* This library can now discover metadata for a 'local' package (found
|
||||
in the current-working directory). Closes #27.
|
||||
* Added ``files()`` function for resolving files from a distribution.
|
||||
* Added a new ``requires()`` function, which returns the requirements
|
||||
for a package suitable for parsing by
|
||||
``packaging.requirements.Requirement``. Closes #18.
|
||||
* The top-level ``read_text()`` function has been removed. Use
|
||||
``PackagePath.read_text()`` on instances returned by the ``files()``
|
||||
function. **This is a backward incompatible change.**
|
||||
* Release dates are now automatically injected into the changelog
|
||||
based on SCM tags.
|
||||
|
||||
0.7
|
||||
===
|
||||
* Fixed issue where packages with dashes in their names would
|
||||
not be discovered. Closes #21.
|
||||
* Distribution lookup is now case-insensitive. Closes #20.
|
||||
* Wheel distributions can no longer be discovered by their module
|
||||
name. Like Path distributions, they must be indicated by their
|
||||
distribution package name.
|
||||
|
||||
0.6
|
||||
===
|
||||
* Removed ``importlib_metadata.distribution`` function. Now
|
||||
the public interface is primarily the utility functions exposed
|
||||
in ``importlib_metadata.__all__``. Closes #14.
|
||||
* Added two new utility functions ``read_text`` and
|
||||
``metadata``.
|
||||
|
||||
0.5
|
||||
===
|
||||
* Updated README and removed details about Distribution
|
||||
class, now considered private. Closes #15.
|
||||
* Added test suite support for Python 3.4+.
|
||||
* Fixed SyntaxErrors on Python 3.4 and 3.5. !12
|
||||
* Fixed errors on Windows joining Path elements. !15
|
||||
|
||||
0.4
|
||||
===
|
||||
* Housekeeping.
|
||||
|
||||
0.3
|
||||
===
|
||||
* Added usage documentation. Closes #8
|
||||
* Add support for getting metadata from wheels on ``sys.path``. Closes #9
|
||||
|
||||
0.2
|
||||
===
|
||||
* Added ``importlib_metadata.entry_points()``. Closes #1
|
||||
* Added ``importlib_metadata.resolve()``. Closes #12
|
||||
* Add support for Python 2.7. Closes #4
|
||||
|
||||
0.1
|
||||
===
|
||||
* Initial release.
|
||||
|
||||
|
||||
..
|
||||
Local Variables:
|
||||
mode: change-log-mode
|
||||
indent-tabs-mode: nil
|
||||
sentence-end-double-space: t
|
||||
fill-column: 78
|
||||
coding: utf-8
|
||||
End:
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# importlib_metadata documentation build configuration file, created by
|
||||
# sphinx-quickstart on Thu Nov 30 10:21:00 2017.
|
||||
#
|
||||
# This file is execfile()d with the current directory set to its
|
||||
# containing dir.
|
||||
#
|
||||
# Note that not all possible configuration values are present in this
|
||||
# autogenerated file.
|
||||
#
|
||||
# All configuration values have a default; values that are commented out
|
||||
# serve to show the default.
|
||||
|
||||
# If extensions (or modules to document with autodoc) are in another directory,
|
||||
# add these directories to sys.path here. If the directory is relative to the
|
||||
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||
#
|
||||
# import os
|
||||
# import sys
|
||||
# sys.path.insert(0, os.path.abspath('.'))
|
||||
|
||||
|
||||
# -- General configuration ------------------------------------------------
|
||||
|
||||
# If your documentation needs a minimal Sphinx version, state it here.
|
||||
#
|
||||
# needs_sphinx = '1.0'
|
||||
|
||||
# Add any Sphinx extension module names here, as strings. They can be
|
||||
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||
# ones.
|
||||
extensions = [
|
||||
'rst.linker',
|
||||
'sphinx.ext.autodoc',
|
||||
'sphinx.ext.coverage',
|
||||
'sphinx.ext.doctest',
|
||||
'sphinx.ext.intersphinx',
|
||||
'sphinx.ext.viewcode',
|
||||
]
|
||||
|
||||
# Add any paths that contain templates here, relative to this directory.
|
||||
templates_path = ['_templates']
|
||||
|
||||
# The suffix(es) of source filenames.
|
||||
# You can specify multiple suffix as a list of string:
|
||||
#
|
||||
# source_suffix = ['.rst', '.md']
|
||||
source_suffix = '.rst'
|
||||
|
||||
# The master toctree document.
|
||||
master_doc = 'index'
|
||||
|
||||
# General information about the project.
|
||||
project = 'importlib_metadata'
|
||||
copyright = '2017-2019, Jason R. Coombs, Barry Warsaw'
|
||||
author = 'Jason R. Coombs, Barry Warsaw'
|
||||
|
||||
# The version info for the project you're documenting, acts as replacement for
|
||||
# |version| and |release|, also used in various other places throughout the
|
||||
# built documents.
|
||||
#
|
||||
# The short X.Y version.
|
||||
version = '0.1'
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = '0.1'
|
||||
|
||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||
# for a list of supported languages.
|
||||
#
|
||||
# This is also used if you do content translation via gettext catalogs.
|
||||
# Usually you set "language" from the command line for these cases.
|
||||
language = None
|
||||
|
||||
# List of patterns, relative to source directory, that match files and
|
||||
# directories to ignore when looking for source files.
|
||||
# This patterns also effect to html_static_path and html_extra_path
|
||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||
|
||||
# The name of the Pygments (syntax highlighting) style to use.
|
||||
pygments_style = 'sphinx'
|
||||
|
||||
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
||||
todo_include_todos = False
|
||||
|
||||
|
||||
# -- Options for HTML output ----------------------------------------------
|
||||
|
||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
#
|
||||
html_theme = 'default'
|
||||
|
||||
# Custom sidebar templates, must be a dictionary that maps document names
|
||||
# to template names.
|
||||
#
|
||||
# This is required for the alabaster theme
|
||||
# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars
|
||||
html_sidebars = {
|
||||
'**': [
|
||||
'relations.html', # needs 'show_related': True theme option to display
|
||||
'searchbox.html',
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
# -- Options for HTMLHelp output ------------------------------------------
|
||||
|
||||
# Output file base name for HTML help builder.
|
||||
htmlhelp_basename = 'importlib_metadatadoc'
|
||||
|
||||
|
||||
# -- Options for LaTeX output ---------------------------------------------
|
||||
|
||||
latex_elements = {
|
||||
# The paper size ('letterpaper' or 'a4paper').
|
||||
#
|
||||
# 'papersize': 'letterpaper',
|
||||
|
||||
# The font size ('10pt', '11pt' or '12pt').
|
||||
#
|
||||
# 'pointsize': '10pt',
|
||||
|
||||
# Additional stuff for the LaTeX preamble.
|
||||
#
|
||||
# 'preamble': '',
|
||||
|
||||
# Latex figure (float) alignment
|
||||
#
|
||||
# 'figure_align': 'htbp',
|
||||
}
|
||||
|
||||
# Grouping the document tree into LaTeX files. List of tuples
|
||||
# (source start file, target name, title,
|
||||
# author, documentclass [howto, manual, or own class]).
|
||||
latex_documents = [
|
||||
(master_doc, 'importlib_metadata.tex',
|
||||
'importlib\\_metadata Documentation',
|
||||
'Brett Cannon, Barry Warsaw', 'manual'),
|
||||
]
|
||||
|
||||
|
||||
# -- Options for manual page output ---------------------------------------
|
||||
|
||||
# One entry per manual page. List of tuples
|
||||
# (source start file, name, description, authors, manual section).
|
||||
man_pages = [
|
||||
(master_doc, 'importlib_metadata', 'importlib_metadata Documentation',
|
||||
[author], 1)
|
||||
]
|
||||
|
||||
|
||||
# -- Options for Texinfo output -------------------------------------------
|
||||
|
||||
# Grouping the document tree into Texinfo files. List of tuples
|
||||
# (source start file, target name, title, author,
|
||||
# dir menu entry, description, category)
|
||||
texinfo_documents = [
|
||||
(master_doc, 'importlib_metadata', 'importlib_metadata Documentation',
|
||||
author, 'importlib_metadata', 'One line description of project.',
|
||||
'Miscellaneous'),
|
||||
]
|
||||
|
||||
|
||||
# Example configuration for intersphinx: refer to the Python standard library.
|
||||
intersphinx_mapping = {
|
||||
'python': ('https://docs.python.org/3', None),
|
||||
}
|
||||
|
||||
|
||||
# For rst.linker, inject release dates into changelog.rst
|
||||
link_files = {
|
||||
'changelog.rst': dict(
|
||||
replace=[
|
||||
dict(
|
||||
pattern=r'^(?m)((?P<scm_version>v?\d+(\.\d+){1,2}))\n[-=]+\n',
|
||||
with_scm='{text}\n{rev[timestamp]:%Y-%m-%d}\n\n',
|
||||
),
|
||||
],
|
||||
),
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
===============================
|
||||
Welcome to importlib_metadata
|
||||
===============================
|
||||
|
||||
``importlib_metadata`` is a library which provides an API for accessing an
|
||||
installed package's `metadata`_, such as its entry points or its top-level
|
||||
name. This functionality intends to replace most uses of ``pkg_resources``
|
||||
`entry point API`_ and `metadata API`_. Along with ``importlib.resources`` in
|
||||
`Python 3.7 and newer`_ (backported as `importlib_resources`_ for older
|
||||
versions of Python), this can eliminate the need to use the older and less
|
||||
efficient ``pkg_resources`` package.
|
||||
|
||||
``importlib_metadata`` is a backport of Python 3.8's standard library
|
||||
`importlib.metadata`_ module for Python 2.7, and 3.4 through 3.7. Users of
|
||||
Python 3.8 and beyond are encouraged to use the standard library module.
|
||||
When imported on Python 3.8 and later, ``importlib_metadata`` replaces the
|
||||
DistributionFinder behavior from the stdlib, but leaves the API in tact.
|
||||
Developers looking for detailed API descriptions should refer to the Python
|
||||
3.8 standard library documentation.
|
||||
|
||||
The documentation here includes a general :ref:`usage <using>` guide.
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
:caption: Contents:
|
||||
|
||||
using.rst
|
||||
changelog (links).rst
|
||||
|
||||
|
||||
Project details
|
||||
===============
|
||||
|
||||
* Project home: https://gitlab.com/python-devs/importlib_metadata
|
||||
* Report bugs at: https://gitlab.com/python-devs/importlib_metadata/issues
|
||||
* Code hosting: https://gitlab.com/python-devs/importlib_metadata.git
|
||||
* Documentation: http://importlib_metadata.readthedocs.io/
|
||||
|
||||
|
||||
Indices and tables
|
||||
==================
|
||||
|
||||
* :ref:`genindex`
|
||||
* :ref:`modindex`
|
||||
* :ref:`search`
|
||||
|
||||
|
||||
.. _`metadata`: https://www.python.org/dev/peps/pep-0566/
|
||||
.. _`entry point API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
|
||||
.. _`metadata API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api
|
||||
.. _`Python 3.7 and newer`: https://docs.python.org/3/library/importlib.html#module-importlib.resources
|
||||
.. _`importlib_resources`: https://importlib-resources.readthedocs.io/en/latest/index.html
|
||||
.. _`importlib.metadata`: https://docs.python.org/3/library/importlib.metadata.html
|
||||
+259
@@ -0,0 +1,259 @@
|
||||
.. _using:
|
||||
|
||||
==========================
|
||||
Using importlib_metadata
|
||||
==========================
|
||||
|
||||
``importlib_metadata`` is a library that provides for access to installed
|
||||
package metadata. Built in part on Python's import system, this library
|
||||
intends to replace similar functionality in the `entry point
|
||||
API`_ and `metadata API`_ of ``pkg_resources``. Along with
|
||||
``importlib.resources`` in `Python 3.7
|
||||
and newer`_ (backported as `importlib_resources`_ for older versions of
|
||||
Python), this can eliminate the need to use the older and less efficient
|
||||
``pkg_resources`` package.
|
||||
|
||||
By "installed package" we generally mean a third-party package installed into
|
||||
Python's ``site-packages`` directory via tools such as `pip
|
||||
<https://pypi.org/project/pip/>`_. Specifically,
|
||||
it means a package with either a discoverable ``dist-info`` or ``egg-info``
|
||||
directory, and metadata defined by `PEP 566`_ or its older specifications.
|
||||
By default, package metadata can live on the file system or in zip archives on
|
||||
``sys.path``. Through an extension mechanism, the metadata can live almost
|
||||
anywhere.
|
||||
|
||||
|
||||
Overview
|
||||
========
|
||||
|
||||
Let's say you wanted to get the version string for a package you've installed
|
||||
using ``pip``. We start by creating a virtual environment and installing
|
||||
something into it::
|
||||
|
||||
$ python3 -m venv example
|
||||
$ source example/bin/activate
|
||||
(example) $ pip install importlib_metadata
|
||||
(example) $ pip install wheel
|
||||
|
||||
You can get the version string for ``wheel`` by running the following::
|
||||
|
||||
(example) $ python
|
||||
>>> from importlib_metadata import version
|
||||
>>> version('wheel')
|
||||
'0.32.3'
|
||||
|
||||
You can also get the set of entry points keyed by group, such as
|
||||
``console_scripts``, ``distutils.commands`` and others. Each group contains a
|
||||
sequence of :ref:`EntryPoint <entry-points>` objects.
|
||||
|
||||
You can get the :ref:`metadata for a distribution <metadata>`::
|
||||
|
||||
>>> list(metadata('wheel'))
|
||||
['Metadata-Version', 'Name', 'Version', 'Summary', 'Home-page', 'Author', 'Author-email', 'Maintainer', 'Maintainer-email', 'License', 'Project-URL', 'Project-URL', 'Project-URL', 'Keywords', 'Platform', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Requires-Python', 'Provides-Extra', 'Requires-Dist', 'Requires-Dist']
|
||||
|
||||
You can also get a :ref:`distribution's version number <version>`, list its
|
||||
:ref:`constituent files <files>`, and get a list of the distribution's
|
||||
:ref:`requirements`.
|
||||
|
||||
|
||||
Functional API
|
||||
==============
|
||||
|
||||
This package provides the following functionality via its public API.
|
||||
|
||||
|
||||
.. _entry-points:
|
||||
|
||||
Entry points
|
||||
------------
|
||||
|
||||
The ``entry_points()`` function returns a dictionary of all entry points,
|
||||
keyed by group. Entry points are represented by ``EntryPoint`` instances;
|
||||
each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
|
||||
a ``.load()`` method to resolve the value::
|
||||
|
||||
>>> eps = entry_points()
|
||||
>>> list(eps)
|
||||
['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']
|
||||
>>> scripts = eps['console_scripts']
|
||||
>>> wheel = [ep for ep in scripts if ep.name == 'wheel'][0]
|
||||
>>> wheel
|
||||
EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts')
|
||||
>>> main = wheel.load()
|
||||
>>> main
|
||||
<function main at 0x103528488>
|
||||
|
||||
The ``group`` and ``name`` are arbitrary values defined by the package author
|
||||
and usually a client will wish to resolve all entry points for a particular
|
||||
group. Read `the setuptools docs
|
||||
<https://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins>`_
|
||||
for more information on entrypoints, their definition, and usage.
|
||||
|
||||
|
||||
.. _metadata:
|
||||
|
||||
Distribution metadata
|
||||
---------------------
|
||||
|
||||
Every distribution includes some metadata, which you can extract using the
|
||||
``metadata()`` function::
|
||||
|
||||
>>> wheel_metadata = metadata('wheel')
|
||||
|
||||
The keys of the returned data structure [#f1]_ name the metadata keywords, and
|
||||
their values are returned unparsed from the distribution metadata::
|
||||
|
||||
>>> wheel_metadata['Requires-Python']
|
||||
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
|
||||
|
||||
|
||||
.. _version:
|
||||
|
||||
Distribution versions
|
||||
---------------------
|
||||
|
||||
The ``version()`` function is the quickest way to get a distribution's version
|
||||
number, as a string::
|
||||
|
||||
>>> version('wheel')
|
||||
'0.32.3'
|
||||
|
||||
|
||||
.. _files:
|
||||
|
||||
Distribution files
|
||||
------------------
|
||||
|
||||
You can also get the full set of files contained within a distribution. The
|
||||
``files()`` function takes a distribution package name and returns all of the
|
||||
files installed by this distribution. Each file object returned is a
|
||||
``PackagePath``, a `pathlib.Path`_ derived object with additional ``dist``,
|
||||
``size``, and ``hash`` properties as indicated by the metadata. For example::
|
||||
|
||||
>>> util = [p for p in files('wheel') if 'util.py' in str(p)][0]
|
||||
>>> util
|
||||
PackagePath('wheel/util.py')
|
||||
>>> util.size
|
||||
859
|
||||
>>> util.dist
|
||||
<importlib_metadata._hooks.PathDistribution object at 0x101e0cef0>
|
||||
>>> util.hash
|
||||
<FileHash mode: sha256 value: bYkw5oMccfazVCoYQwKkkemoVyMAFoR34mmKBx8R1NI>
|
||||
|
||||
Once you have the file, you can also read its contents::
|
||||
|
||||
>>> print(util.read_text())
|
||||
import base64
|
||||
import sys
|
||||
...
|
||||
def as_bytes(s):
|
||||
if isinstance(s, text_type):
|
||||
return s.encode('utf-8')
|
||||
return s
|
||||
|
||||
In the case where the metadata file listing files
|
||||
(RECORD or SOURCES.txt) is missing, ``files()`` will
|
||||
return ``None``. The caller may wish to wrap calls to
|
||||
``files()`` in `always_iterable
|
||||
<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable>`_
|
||||
or otherwise guard against this condition if the target
|
||||
distribution is not known to have the metadata present.
|
||||
|
||||
.. _requirements:
|
||||
|
||||
Distribution requirements
|
||||
-------------------------
|
||||
|
||||
To get the full set of requirements for a distribution, use the ``requires()``
|
||||
function::
|
||||
|
||||
>>> requires('wheel')
|
||||
["pytest (>=3.0.0) ; extra == 'test'", "pytest-cov ; extra == 'test'"]
|
||||
|
||||
|
||||
Distributions
|
||||
=============
|
||||
|
||||
While the above API is the most common and convenient usage, you can get all
|
||||
of that information from the ``Distribution`` class. A ``Distribution`` is an
|
||||
abstract object that represents the metadata for a Python package. You can
|
||||
get the ``Distribution`` instance::
|
||||
|
||||
>>> from importlib_metadata import distribution
|
||||
>>> dist = distribution('wheel')
|
||||
|
||||
Thus, an alternative way to get the version number is through the
|
||||
``Distribution`` instance::
|
||||
|
||||
>>> dist.version
|
||||
'0.32.3'
|
||||
|
||||
There are all kinds of additional metadata available on the ``Distribution``
|
||||
instance::
|
||||
|
||||
>>> d.metadata['Requires-Python']
|
||||
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
|
||||
>>> d.metadata['License']
|
||||
'MIT'
|
||||
|
||||
The full set of available metadata is not described here. See `PEP 566
|
||||
<https://www.python.org/dev/peps/pep-0566/>`_ for additional details.
|
||||
|
||||
|
||||
Extending the search algorithm
|
||||
==============================
|
||||
|
||||
Because package metadata is not available through ``sys.path`` searches, or
|
||||
package loaders directly, the metadata for a package is found through import
|
||||
system `finders`_. To find a distribution package's metadata,
|
||||
``importlib_metadata`` queries the list of `meta path finders`_ on
|
||||
`sys.meta_path`_.
|
||||
|
||||
By default ``importlib_metadata`` installs a finder for distribution packages
|
||||
found on the file system. This finder doesn't actually find any *packages*,
|
||||
but it can find the packages' metadata.
|
||||
|
||||
The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
|
||||
interface expected of finders by Python's import system.
|
||||
``importlib_metadata`` extends this protocol by looking for an optional
|
||||
``find_distributions`` callable on the finders from
|
||||
``sys.meta_path`` and presents this extended interface as the
|
||||
``DistributionFinder`` abstract base class, which defines this abstract
|
||||
method::
|
||||
|
||||
@abc.abstractmethod
|
||||
def find_distributions(context=DistributionFinder.Context()):
|
||||
"""Return an iterable of all Distribution instances capable of
|
||||
loading the metadata for packages for the indicated ``context``.
|
||||
"""
|
||||
|
||||
The ``DistributionFinder.Context`` object provides ``.path`` and ``.name``
|
||||
properties indicating the path to search and names to match and may
|
||||
supply other relevant context.
|
||||
|
||||
What this means in practice is that to support finding distribution package
|
||||
metadata in locations other than the file system, subclass
|
||||
``Distribution`` and implement the abstract methods. Then from
|
||||
a custom finder, return instances of this derived ``Distribution`` in the
|
||||
``find_distributions()`` method.
|
||||
|
||||
|
||||
.. _`entry point API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
|
||||
.. _`metadata API`: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#metadata-api
|
||||
.. _`Python 3.7 and newer`: https://docs.python.org/3/library/importlib.html#module-importlib.resources
|
||||
.. _`importlib_resources`: https://importlib-resources.readthedocs.io/en/latest/index.html
|
||||
.. _`PEP 566`: https://www.python.org/dev/peps/pep-0566/
|
||||
.. _`finders`: https://docs.python.org/3/reference/import.html#finders-and-loaders
|
||||
.. _`meta path finders`: https://docs.python.org/3/glossary.html#term-meta-path-finder
|
||||
.. _`sys.meta_path`: https://docs.python.org/3/library/sys.html#sys.meta_path
|
||||
.. _`pathlib.Path`: https://docs.python.org/3/library/pathlib.html#pathlib.Path
|
||||
|
||||
|
||||
.. rubric:: Footnotes
|
||||
|
||||
.. [#f1] Technically, the returned distribution metadata object is an
|
||||
`email.message.Message
|
||||
<https://docs.python.org/3/library/email.message.html#email.message.EmailMessage>`_
|
||||
instance, but this is an implementation detail, and not part of the
|
||||
stable API. You should only use dictionary-like methods and syntax
|
||||
to access the metadata contents.
|
||||
Binary file not shown.
+200
@@ -0,0 +1,200 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import tempfile
|
||||
import textwrap
|
||||
import contextlib
|
||||
|
||||
try:
|
||||
from contextlib import ExitStack
|
||||
except ImportError:
|
||||
from contextlib2 import ExitStack
|
||||
|
||||
try:
|
||||
import pathlib
|
||||
except ImportError:
|
||||
import pathlib2 as pathlib
|
||||
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def tempdir():
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
yield pathlib.Path(tmpdir)
|
||||
finally:
|
||||
shutil.rmtree(tmpdir)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def save_cwd():
|
||||
orig = os.getcwd()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
os.chdir(orig)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def tempdir_as_cwd():
|
||||
with tempdir() as tmp:
|
||||
with save_cwd():
|
||||
os.chdir(str(tmp))
|
||||
yield tmp
|
||||
|
||||
|
||||
class SiteDir:
|
||||
def setUp(self):
|
||||
self.fixtures = ExitStack()
|
||||
self.addCleanup(self.fixtures.close)
|
||||
self.site_dir = self.fixtures.enter_context(tempdir())
|
||||
|
||||
|
||||
class OnSysPath:
|
||||
@staticmethod
|
||||
@contextlib.contextmanager
|
||||
def add_sys_path(dir):
|
||||
sys.path[:0] = [str(dir)]
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
sys.path.remove(str(dir))
|
||||
|
||||
def setUp(self):
|
||||
super(OnSysPath, self).setUp()
|
||||
self.fixtures.enter_context(self.add_sys_path(self.site_dir))
|
||||
|
||||
|
||||
class DistInfoPkg(OnSysPath, SiteDir):
|
||||
files = {
|
||||
"distinfo_pkg-1.0.0.dist-info": {
|
||||
"METADATA": """
|
||||
Name: distinfo-pkg
|
||||
Author: Steven Ma
|
||||
Version: 1.0.0
|
||||
Requires-Dist: wheel >= 1.0
|
||||
Requires-Dist: pytest; extra == 'test'
|
||||
""",
|
||||
"RECORD": "mod.py,sha256=abc,20\n",
|
||||
"entry_points.txt": """
|
||||
[entries]
|
||||
main = mod:main
|
||||
ns:sub = mod:main
|
||||
"""
|
||||
},
|
||||
"mod.py": """
|
||||
def main():
|
||||
print("hello world")
|
||||
""",
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
super(DistInfoPkg, self).setUp()
|
||||
build_files(DistInfoPkg.files, self.site_dir)
|
||||
|
||||
|
||||
class DistInfoPkgOffPath(SiteDir):
|
||||
def setUp(self):
|
||||
super(DistInfoPkgOffPath, self).setUp()
|
||||
build_files(DistInfoPkg.files, self.site_dir)
|
||||
|
||||
|
||||
class EggInfoPkg(OnSysPath, SiteDir):
|
||||
files = {
|
||||
"egginfo_pkg.egg-info": {
|
||||
"PKG-INFO": """
|
||||
Name: egginfo-pkg
|
||||
Author: Steven Ma
|
||||
License: Unknown
|
||||
Version: 1.0.0
|
||||
Classifier: Intended Audience :: Developers
|
||||
Classifier: Topic :: Software Development :: Libraries
|
||||
""",
|
||||
"SOURCES.txt": """
|
||||
mod.py
|
||||
egginfo_pkg.egg-info/top_level.txt
|
||||
""",
|
||||
"entry_points.txt": """
|
||||
[entries]
|
||||
main = mod:main
|
||||
""",
|
||||
"requires.txt": """
|
||||
wheel >= 1.0; python_version >= "2.7"
|
||||
[test]
|
||||
pytest
|
||||
""",
|
||||
"top_level.txt": "mod\n"
|
||||
},
|
||||
"mod.py": """
|
||||
def main():
|
||||
print("hello world")
|
||||
""",
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
super(EggInfoPkg, self).setUp()
|
||||
build_files(EggInfoPkg.files, prefix=self.site_dir)
|
||||
|
||||
|
||||
class EggInfoFile(OnSysPath, SiteDir):
|
||||
files = {
|
||||
"egginfo_file.egg-info": """
|
||||
Metadata-Version: 1.0
|
||||
Name: egginfo_file
|
||||
Version: 0.1
|
||||
Summary: An example package
|
||||
Home-page: www.example.com
|
||||
Author: Eric Haffa-Vee
|
||||
Author-email: eric@example.coms
|
||||
License: UNKNOWN
|
||||
Description: UNKNOWN
|
||||
Platform: UNKNOWN
|
||||
""",
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
super(EggInfoFile, self).setUp()
|
||||
build_files(EggInfoFile.files, prefix=self.site_dir)
|
||||
|
||||
|
||||
def build_files(file_defs, prefix=pathlib.Path()):
|
||||
"""Build a set of files/directories, as described by the
|
||||
|
||||
file_defs dictionary. Each key/value pair in the dictionary is
|
||||
interpreted as a filename/contents pair. If the contents value is a
|
||||
dictionary, a directory is created, and the dictionary interpreted
|
||||
as the files within it, recursively.
|
||||
|
||||
For example:
|
||||
|
||||
{"README.txt": "A README file",
|
||||
"foo": {
|
||||
"__init__.py": "",
|
||||
"bar": {
|
||||
"__init__.py": "",
|
||||
},
|
||||
"baz.py": "# Some code",
|
||||
}
|
||||
}
|
||||
"""
|
||||
for name, contents in file_defs.items():
|
||||
full_name = prefix / name
|
||||
if isinstance(contents, dict):
|
||||
full_name.mkdir()
|
||||
build_files(contents, prefix=full_name)
|
||||
else:
|
||||
if isinstance(contents, bytes):
|
||||
with full_name.open('wb') as f:
|
||||
f.write(contents)
|
||||
else:
|
||||
with full_name.open('w') as f:
|
||||
f.write(DALS(contents))
|
||||
|
||||
|
||||
def DALS(str):
|
||||
"Dedent and left-strip"
|
||||
return textwrap.dedent(str).lstrip()
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
import re
|
||||
import textwrap
|
||||
import unittest
|
||||
|
||||
from . import fixtures
|
||||
from .. import (
|
||||
Distribution, PackageNotFoundError, __version__, distribution,
|
||||
entry_points, files, metadata, requires, version,
|
||||
)
|
||||
|
||||
try:
|
||||
from collections.abc import Iterator
|
||||
except ImportError:
|
||||
from collections import Iterator # noqa: F401
|
||||
|
||||
try:
|
||||
from builtins import str as text
|
||||
except ImportError:
|
||||
from __builtin__ import unicode as text
|
||||
|
||||
|
||||
class APITests(
|
||||
fixtures.EggInfoPkg,
|
||||
fixtures.DistInfoPkg,
|
||||
fixtures.EggInfoFile,
|
||||
unittest.TestCase):
|
||||
|
||||
version_pattern = r'\d+\.\d+(\.\d)?'
|
||||
|
||||
def test_retrieves_version_of_self(self):
|
||||
pkg_version = version('egginfo-pkg')
|
||||
assert isinstance(pkg_version, text)
|
||||
assert re.match(self.version_pattern, pkg_version)
|
||||
|
||||
def test_retrieves_version_of_distinfo_pkg(self):
|
||||
pkg_version = version('distinfo-pkg')
|
||||
assert isinstance(pkg_version, text)
|
||||
assert re.match(self.version_pattern, pkg_version)
|
||||
|
||||
def test_for_name_does_not_exist(self):
|
||||
with self.assertRaises(PackageNotFoundError):
|
||||
distribution('does-not-exist')
|
||||
|
||||
def test_for_top_level(self):
|
||||
self.assertEqual(
|
||||
distribution('egginfo-pkg').read_text('top_level.txt').strip(),
|
||||
'mod')
|
||||
|
||||
def test_read_text(self):
|
||||
top_level = [
|
||||
path for path in files('egginfo-pkg')
|
||||
if path.name == 'top_level.txt'
|
||||
][0]
|
||||
self.assertEqual(top_level.read_text(), 'mod\n')
|
||||
|
||||
def test_entry_points(self):
|
||||
entries = dict(entry_points()['entries'])
|
||||
ep = entries['main']
|
||||
self.assertEqual(ep.value, 'mod:main')
|
||||
self.assertEqual(ep.extras, [])
|
||||
|
||||
def test_metadata_for_this_package(self):
|
||||
md = metadata('egginfo-pkg')
|
||||
assert md['author'] == 'Steven Ma'
|
||||
assert md['LICENSE'] == 'Unknown'
|
||||
assert md['Name'] == 'egginfo-pkg'
|
||||
classifiers = md.get_all('Classifier')
|
||||
assert 'Topic :: Software Development :: Libraries' in classifiers
|
||||
|
||||
def test_importlib_metadata_version(self):
|
||||
assert re.match(self.version_pattern, __version__)
|
||||
|
||||
@staticmethod
|
||||
def _test_files(files):
|
||||
root = files[0].root
|
||||
for file in files:
|
||||
assert file.root == root
|
||||
assert not file.hash or file.hash.value
|
||||
assert not file.hash or file.hash.mode == 'sha256'
|
||||
assert not file.size or file.size >= 0
|
||||
assert file.locate().exists()
|
||||
assert isinstance(file.read_binary(), bytes)
|
||||
if file.name.endswith('.py'):
|
||||
file.read_text()
|
||||
|
||||
def test_file_hash_repr(self):
|
||||
try:
|
||||
assertRegex = self.assertRegex
|
||||
except AttributeError:
|
||||
# Python 2
|
||||
assertRegex = self.assertRegexpMatches
|
||||
|
||||
util = [
|
||||
p for p in files('distinfo-pkg')
|
||||
if p.name == 'mod.py'
|
||||
][0]
|
||||
assertRegex(
|
||||
repr(util.hash),
|
||||
'<FileHash mode: sha256 value: .*>')
|
||||
|
||||
def test_files_dist_info(self):
|
||||
self._test_files(files('distinfo-pkg'))
|
||||
|
||||
def test_files_egg_info(self):
|
||||
self._test_files(files('egginfo-pkg'))
|
||||
|
||||
def test_version_egg_info_file(self):
|
||||
self.assertEqual(version('egginfo-file'), '0.1')
|
||||
|
||||
def test_requires_egg_info_file(self):
|
||||
requirements = requires('egginfo-file')
|
||||
self.assertIsNone(requirements)
|
||||
|
||||
def test_requires_egg_info(self):
|
||||
deps = requires('egginfo-pkg')
|
||||
assert len(deps) == 2
|
||||
assert any(
|
||||
dep == 'wheel >= 1.0; python_version >= "2.7"'
|
||||
for dep in deps
|
||||
)
|
||||
|
||||
def test_requires_dist_info(self):
|
||||
deps = requires('distinfo-pkg')
|
||||
assert len(deps) == 2
|
||||
assert all(deps)
|
||||
assert 'wheel >= 1.0' in deps
|
||||
assert "pytest; extra == 'test'" in deps
|
||||
|
||||
def test_more_complex_deps_requires_text(self):
|
||||
requires = textwrap.dedent("""
|
||||
dep1
|
||||
dep2
|
||||
|
||||
[:python_version < "3"]
|
||||
dep3
|
||||
|
||||
[extra1]
|
||||
dep4
|
||||
|
||||
[extra2:python_version < "3"]
|
||||
dep5
|
||||
""")
|
||||
deps = sorted(Distribution._deps_from_requires_text(requires))
|
||||
expected = [
|
||||
'dep1',
|
||||
'dep2',
|
||||
'dep3; python_version < "3"',
|
||||
'dep4; extra == "extra1"',
|
||||
'dep5; (python_version < "3") and extra == "extra2"',
|
||||
]
|
||||
# It's important that the environment marker expression be
|
||||
# wrapped in parentheses to avoid the following 'and' binding more
|
||||
# tightly than some other part of the environment expression.
|
||||
|
||||
assert deps == expected
|
||||
|
||||
|
||||
class OffSysPathTests(fixtures.DistInfoPkgOffPath, unittest.TestCase):
|
||||
def test_find_distributions_specified_path(self):
|
||||
dists = Distribution.discover(path=[str(self.site_dir)])
|
||||
assert any(
|
||||
dist.metadata['Name'] == 'distinfo-pkg'
|
||||
for dist in dists
|
||||
)
|
||||
|
||||
def test_distribution_at_pathlib(self):
|
||||
"""Demonstrate how to load metadata direct from a directory.
|
||||
"""
|
||||
dist_info_path = self.site_dir / 'distinfo_pkg-1.0.0.dist-info'
|
||||
dist = Distribution.at(dist_info_path)
|
||||
assert dist.version == '1.0.0'
|
||||
|
||||
def test_distribution_at_str(self):
|
||||
dist_info_path = self.site_dir / 'distinfo_pkg-1.0.0.dist-info'
|
||||
dist = Distribution.at(str(dist_info_path))
|
||||
assert dist.version == '1.0.0'
|
||||
@@ -0,0 +1,22 @@
|
||||
import unittest
|
||||
import packaging.requirements
|
||||
import packaging.version
|
||||
|
||||
from . import fixtures
|
||||
from .. import version
|
||||
|
||||
|
||||
class IntegrationTests(fixtures.DistInfoPkg, unittest.TestCase):
|
||||
|
||||
def test_package_spec_installed(self):
|
||||
"""
|
||||
Illustrate the recommended procedure to determine if
|
||||
a specified version of a package is installed.
|
||||
"""
|
||||
def is_installed(package_spec):
|
||||
req = packaging.requirements.Requirement(package_spec)
|
||||
return version(req.name) in req.specifier
|
||||
|
||||
assert is_installed('distinfo-pkg==1.0')
|
||||
assert is_installed('distinfo-pkg>=1.0,<2.0')
|
||||
assert not is_installed('distinfo-pkg<1.0')
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
import json
|
||||
import pickle
|
||||
import textwrap
|
||||
import unittest
|
||||
import importlib
|
||||
import importlib_metadata
|
||||
|
||||
from . import fixtures
|
||||
from .. import (
|
||||
Distribution, EntryPoint, MetadataPathFinder,
|
||||
PackageNotFoundError, distributions,
|
||||
entry_points, metadata, version,
|
||||
)
|
||||
|
||||
try:
|
||||
from builtins import str as text
|
||||
except ImportError:
|
||||
from __builtin__ import unicode as text
|
||||
|
||||
|
||||
class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
|
||||
version_pattern = r'\d+\.\d+(\.\d)?'
|
||||
|
||||
def test_retrieves_version_of_self(self):
|
||||
dist = Distribution.from_name('distinfo-pkg')
|
||||
assert isinstance(dist.version, text)
|
||||
assert re.match(self.version_pattern, dist.version)
|
||||
|
||||
def test_for_name_does_not_exist(self):
|
||||
with self.assertRaises(PackageNotFoundError):
|
||||
Distribution.from_name('does-not-exist')
|
||||
|
||||
def test_new_style_classes(self):
|
||||
self.assertIsInstance(Distribution, type)
|
||||
self.assertIsInstance(MetadataPathFinder, type)
|
||||
|
||||
|
||||
class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
|
||||
def test_import_nonexistent_module(self):
|
||||
# Ensure that the MetadataPathFinder does not crash an import of a
|
||||
# non-existent module.
|
||||
with self.assertRaises(ImportError):
|
||||
importlib.import_module('does_not_exist')
|
||||
|
||||
def test_resolve(self):
|
||||
entries = dict(entry_points()['entries'])
|
||||
ep = entries['main']
|
||||
self.assertEqual(ep.load().__name__, "main")
|
||||
|
||||
def test_entrypoint_with_colon_in_name(self):
|
||||
entries = dict(entry_points()['entries'])
|
||||
ep = entries['ns:sub']
|
||||
self.assertEqual(ep.value, 'mod:main')
|
||||
|
||||
def test_resolve_without_attr(self):
|
||||
ep = EntryPoint(
|
||||
name='ep',
|
||||
value='importlib_metadata',
|
||||
group='grp',
|
||||
)
|
||||
assert ep.load() is importlib_metadata
|
||||
|
||||
|
||||
class NameNormalizationTests(
|
||||
fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||
@staticmethod
|
||||
def pkg_with_dashes(site_dir):
|
||||
"""
|
||||
Create minimal metadata for a package with dashes
|
||||
in the name (and thus underscores in the filename).
|
||||
"""
|
||||
metadata_dir = site_dir / 'my_pkg.dist-info'
|
||||
metadata_dir.mkdir()
|
||||
metadata = metadata_dir / 'METADATA'
|
||||
with metadata.open('w') as strm:
|
||||
strm.write('Version: 1.0\n')
|
||||
return 'my-pkg'
|
||||
|
||||
def test_dashes_in_dist_name_found_as_underscores(self):
|
||||
"""
|
||||
For a package with a dash in the name, the dist-info metadata
|
||||
uses underscores in the name. Ensure the metadata loads.
|
||||
"""
|
||||
pkg_name = self.pkg_with_dashes(self.site_dir)
|
||||
assert version(pkg_name) == '1.0'
|
||||
|
||||
@staticmethod
|
||||
def pkg_with_mixed_case(site_dir):
|
||||
"""
|
||||
Create minimal metadata for a package with mixed case
|
||||
in the name.
|
||||
"""
|
||||
metadata_dir = site_dir / 'CherryPy.dist-info'
|
||||
metadata_dir.mkdir()
|
||||
metadata = metadata_dir / 'METADATA'
|
||||
with metadata.open('w') as strm:
|
||||
strm.write('Version: 1.0\n')
|
||||
return 'CherryPy'
|
||||
|
||||
def test_dist_name_found_as_any_case(self):
|
||||
"""
|
||||
Ensure the metadata loads when queried with any case.
|
||||
"""
|
||||
pkg_name = self.pkg_with_mixed_case(self.site_dir)
|
||||
assert version(pkg_name) == '1.0'
|
||||
assert version(pkg_name.lower()) == '1.0'
|
||||
assert version(pkg_name.upper()) == '1.0'
|
||||
|
||||
|
||||
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||
@staticmethod
|
||||
def pkg_with_non_ascii_description(site_dir):
|
||||
"""
|
||||
Create minimal metadata for a package with non-ASCII in
|
||||
the description.
|
||||
"""
|
||||
metadata_dir = site_dir / 'portend.dist-info'
|
||||
metadata_dir.mkdir()
|
||||
metadata = metadata_dir / 'METADATA'
|
||||
with metadata.open('w', encoding='utf-8') as fp:
|
||||
fp.write('Description: pôrˈtend\n')
|
||||
return 'portend'
|
||||
|
||||
@staticmethod
|
||||
def pkg_with_non_ascii_description_egg_info(site_dir):
|
||||
"""
|
||||
Create minimal metadata for an egg-info package with
|
||||
non-ASCII in the description.
|
||||
"""
|
||||
metadata_dir = site_dir / 'portend.dist-info'
|
||||
metadata_dir.mkdir()
|
||||
metadata = metadata_dir / 'METADATA'
|
||||
with metadata.open('w', encoding='utf-8') as fp:
|
||||
fp.write(textwrap.dedent("""
|
||||
Name: portend
|
||||
|
||||
pôrˈtend
|
||||
""").lstrip())
|
||||
return 'portend'
|
||||
|
||||
def test_metadata_loads(self):
|
||||
pkg_name = self.pkg_with_non_ascii_description(self.site_dir)
|
||||
meta = metadata(pkg_name)
|
||||
assert meta['Description'] == 'pôrˈtend'
|
||||
|
||||
def test_metadata_loads_egg_info(self):
|
||||
pkg_name = self.pkg_with_non_ascii_description_egg_info(self.site_dir)
|
||||
meta = metadata(pkg_name)
|
||||
assert meta.get_payload() == 'pôrˈtend\n'
|
||||
|
||||
|
||||
class DiscoveryTests(fixtures.EggInfoPkg,
|
||||
fixtures.DistInfoPkg,
|
||||
unittest.TestCase):
|
||||
|
||||
def test_package_discovery(self):
|
||||
dists = list(distributions())
|
||||
assert all(
|
||||
isinstance(dist, Distribution)
|
||||
for dist in dists
|
||||
)
|
||||
assert any(
|
||||
dist.metadata['Name'] == 'egginfo-pkg'
|
||||
for dist in dists
|
||||
)
|
||||
assert any(
|
||||
dist.metadata['Name'] == 'distinfo-pkg'
|
||||
for dist in dists
|
||||
)
|
||||
|
||||
def test_invalid_usage(self):
|
||||
with self.assertRaises(ValueError):
|
||||
list(distributions(context='something', name='else'))
|
||||
|
||||
|
||||
class DirectoryTest(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||
def test_egg_info(self):
|
||||
# make an `EGG-INFO` directory that's unrelated
|
||||
self.site_dir.joinpath('EGG-INFO').mkdir()
|
||||
# used to crash with `IsADirectoryError`
|
||||
with self.assertRaises(PackageNotFoundError):
|
||||
version('unknown-package')
|
||||
|
||||
def test_egg(self):
|
||||
egg = self.site_dir.joinpath('foo-3.6.egg')
|
||||
egg.mkdir()
|
||||
with self.add_sys_path(egg):
|
||||
with self.assertRaises(PackageNotFoundError):
|
||||
version('foo')
|
||||
|
||||
|
||||
class TestEntryPoints(unittest.TestCase):
|
||||
def __init__(self, *args):
|
||||
super(TestEntryPoints, self).__init__(*args)
|
||||
self.ep = importlib_metadata.EntryPoint('name', 'value', 'group')
|
||||
|
||||
def test_entry_point_pickleable(self):
|
||||
revived = pickle.loads(pickle.dumps(self.ep))
|
||||
assert revived == self.ep
|
||||
|
||||
def test_immutable(self):
|
||||
"""EntryPoints should be immutable"""
|
||||
with self.assertRaises(AttributeError):
|
||||
self.ep.name = 'badactor'
|
||||
|
||||
def test_repr(self):
|
||||
assert 'EntryPoint' in repr(self.ep)
|
||||
assert 'name=' in repr(self.ep)
|
||||
assert "'name'" in repr(self.ep)
|
||||
|
||||
def test_hashable(self):
|
||||
"""EntryPoints should be hashable"""
|
||||
hash(self.ep)
|
||||
|
||||
def test_json_dump(self):
|
||||
"""
|
||||
json should not expect to be able to dump an EntryPoint
|
||||
"""
|
||||
with self.assertRaises(Exception):
|
||||
json.dumps(self.ep)
|
||||
@@ -0,0 +1,70 @@
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from .. import distribution, entry_points, files, PackageNotFoundError, version
|
||||
|
||||
try:
|
||||
from importlib.resources import path
|
||||
except ImportError:
|
||||
from importlib_resources import path
|
||||
|
||||
try:
|
||||
from contextlib import ExitStack
|
||||
except ImportError:
|
||||
from contextlib2 import ExitStack
|
||||
|
||||
|
||||
class TestZip(unittest.TestCase):
|
||||
root = 'importlib_metadata.tests.data'
|
||||
|
||||
def setUp(self):
|
||||
# Find the path to the example-*.whl so we can add it to the front of
|
||||
# sys.path, where we'll then try to find the metadata thereof.
|
||||
self.resources = ExitStack()
|
||||
self.addCleanup(self.resources.close)
|
||||
wheel = self.resources.enter_context(
|
||||
path(self.root, 'example-21.12-py3-none-any.whl'))
|
||||
sys.path.insert(0, str(wheel))
|
||||
self.resources.callback(sys.path.pop, 0)
|
||||
|
||||
def test_zip_version(self):
|
||||
self.assertEqual(version('example'), '21.12')
|
||||
|
||||
def test_zip_version_does_not_match(self):
|
||||
with self.assertRaises(PackageNotFoundError):
|
||||
version('definitely-not-installed')
|
||||
|
||||
def test_zip_entry_points(self):
|
||||
scripts = dict(entry_points()['console_scripts'])
|
||||
entry_point = scripts['example']
|
||||
self.assertEqual(entry_point.value, 'example:main')
|
||||
entry_point = scripts['Example']
|
||||
self.assertEqual(entry_point.value, 'example:main')
|
||||
|
||||
def test_missing_metadata(self):
|
||||
self.assertIsNone(distribution('example').read_text('does not exist'))
|
||||
|
||||
def test_case_insensitive(self):
|
||||
self.assertEqual(version('Example'), '21.12')
|
||||
|
||||
def test_files(self):
|
||||
for file in files('example'):
|
||||
path = str(file.dist.locate_file(file))
|
||||
assert '.whl/' in path, path
|
||||
|
||||
|
||||
class TestEgg(TestZip):
|
||||
def setUp(self):
|
||||
# Find the path to the example-*.egg so we can add it to the front of
|
||||
# sys.path, where we'll then try to find the metadata thereof.
|
||||
self.resources = ExitStack()
|
||||
self.addCleanup(self.resources.close)
|
||||
egg = self.resources.enter_context(
|
||||
path(self.root, 'example-21.12-py3.6.egg'))
|
||||
sys.path.insert(0, str(egg))
|
||||
self.resources.callback(sys.path.pop, 0)
|
||||
|
||||
def test_files(self):
|
||||
for file in files('example'):
|
||||
path = str(file.dist.locate_file(file))
|
||||
assert '.egg/' in path, path
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2012 Erik Rose
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
from more_itertools.more import * # noqa
|
||||
from more_itertools.recipes import * # noqa
|
||||
Vendored
+2333
File diff suppressed because it is too large
Load Diff
+577
@@ -0,0 +1,577 @@
|
||||
"""Imported from the recipes section of the itertools documentation.
|
||||
|
||||
All functions taken from the recipes section of the itertools library docs
|
||||
[1]_.
|
||||
Some backward-compatible usability improvements have been made.
|
||||
|
||||
.. [1] http://docs.python.org/library/itertools.html#recipes
|
||||
|
||||
"""
|
||||
from collections import deque
|
||||
from itertools import (
|
||||
chain, combinations, count, cycle, groupby, islice, repeat, starmap, tee
|
||||
)
|
||||
import operator
|
||||
from random import randrange, sample, choice
|
||||
|
||||
from six import PY2
|
||||
from six.moves import filter, filterfalse, map, range, zip, zip_longest
|
||||
|
||||
__all__ = [
|
||||
'accumulate',
|
||||
'all_equal',
|
||||
'consume',
|
||||
'dotproduct',
|
||||
'first_true',
|
||||
'flatten',
|
||||
'grouper',
|
||||
'iter_except',
|
||||
'ncycles',
|
||||
'nth',
|
||||
'nth_combination',
|
||||
'padnone',
|
||||
'pairwise',
|
||||
'partition',
|
||||
'powerset',
|
||||
'prepend',
|
||||
'quantify',
|
||||
'random_combination_with_replacement',
|
||||
'random_combination',
|
||||
'random_permutation',
|
||||
'random_product',
|
||||
'repeatfunc',
|
||||
'roundrobin',
|
||||
'tabulate',
|
||||
'tail',
|
||||
'take',
|
||||
'unique_everseen',
|
||||
'unique_justseen',
|
||||
]
|
||||
|
||||
|
||||
def accumulate(iterable, func=operator.add):
|
||||
"""
|
||||
Return an iterator whose items are the accumulated results of a function
|
||||
(specified by the optional *func* argument) that takes two arguments.
|
||||
By default, returns accumulated sums with :func:`operator.add`.
|
||||
|
||||
>>> list(accumulate([1, 2, 3, 4, 5])) # Running sum
|
||||
[1, 3, 6, 10, 15]
|
||||
>>> list(accumulate([1, 2, 3], func=operator.mul)) # Running product
|
||||
[1, 2, 6]
|
||||
>>> list(accumulate([0, 1, -1, 2, 3, 2], func=max)) # Running maximum
|
||||
[0, 1, 1, 2, 3, 3]
|
||||
|
||||
This function is available in the ``itertools`` module for Python 3.2 and
|
||||
greater.
|
||||
|
||||
"""
|
||||
it = iter(iterable)
|
||||
try:
|
||||
total = next(it)
|
||||
except StopIteration:
|
||||
return
|
||||
else:
|
||||
yield total
|
||||
|
||||
for element in it:
|
||||
total = func(total, element)
|
||||
yield total
|
||||
|
||||
|
||||
def take(n, iterable):
|
||||
"""Return first *n* items of the iterable as a list.
|
||||
|
||||
>>> take(3, range(10))
|
||||
[0, 1, 2]
|
||||
>>> take(5, range(3))
|
||||
[0, 1, 2]
|
||||
|
||||
Effectively a short replacement for ``next`` based iterator consumption
|
||||
when you want more than one item, but less than the whole iterator.
|
||||
|
||||
"""
|
||||
return list(islice(iterable, n))
|
||||
|
||||
|
||||
def tabulate(function, start=0):
|
||||
"""Return an iterator over the results of ``func(start)``,
|
||||
``func(start + 1)``, ``func(start + 2)``...
|
||||
|
||||
*func* should be a function that accepts one integer argument.
|
||||
|
||||
If *start* is not specified it defaults to 0. It will be incremented each
|
||||
time the iterator is advanced.
|
||||
|
||||
>>> square = lambda x: x ** 2
|
||||
>>> iterator = tabulate(square, -3)
|
||||
>>> take(4, iterator)
|
||||
[9, 4, 1, 0]
|
||||
|
||||
"""
|
||||
return map(function, count(start))
|
||||
|
||||
|
||||
def tail(n, iterable):
|
||||
"""Return an iterator over the last *n* items of *iterable*.
|
||||
|
||||
>>> t = tail(3, 'ABCDEFG')
|
||||
>>> list(t)
|
||||
['E', 'F', 'G']
|
||||
|
||||
"""
|
||||
return iter(deque(iterable, maxlen=n))
|
||||
|
||||
|
||||
def consume(iterator, n=None):
|
||||
"""Advance *iterable* by *n* steps. If *n* is ``None``, consume it
|
||||
entirely.
|
||||
|
||||
Efficiently exhausts an iterator without returning values. Defaults to
|
||||
consuming the whole iterator, but an optional second argument may be
|
||||
provided to limit consumption.
|
||||
|
||||
>>> i = (x for x in range(10))
|
||||
>>> next(i)
|
||||
0
|
||||
>>> consume(i, 3)
|
||||
>>> next(i)
|
||||
4
|
||||
>>> consume(i)
|
||||
>>> next(i)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
StopIteration
|
||||
|
||||
If the iterator has fewer items remaining than the provided limit, the
|
||||
whole iterator will be consumed.
|
||||
|
||||
>>> i = (x for x in range(3))
|
||||
>>> consume(i, 5)
|
||||
>>> next(i)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
StopIteration
|
||||
|
||||
"""
|
||||
# Use functions that consume iterators at C speed.
|
||||
if n is None:
|
||||
# feed the entire iterator into a zero-length deque
|
||||
deque(iterator, maxlen=0)
|
||||
else:
|
||||
# advance to the empty slice starting at position n
|
||||
next(islice(iterator, n, n), None)
|
||||
|
||||
|
||||
def nth(iterable, n, default=None):
|
||||
"""Returns the nth item or a default value.
|
||||
|
||||
>>> l = range(10)
|
||||
>>> nth(l, 3)
|
||||
3
|
||||
>>> nth(l, 20, "zebra")
|
||||
'zebra'
|
||||
|
||||
"""
|
||||
return next(islice(iterable, n, None), default)
|
||||
|
||||
|
||||
def all_equal(iterable):
|
||||
"""
|
||||
Returns ``True`` if all the elements are equal to each other.
|
||||
|
||||
>>> all_equal('aaaa')
|
||||
True
|
||||
>>> all_equal('aaab')
|
||||
False
|
||||
|
||||
"""
|
||||
g = groupby(iterable)
|
||||
return next(g, True) and not next(g, False)
|
||||
|
||||
|
||||
def quantify(iterable, pred=bool):
|
||||
"""Return the how many times the predicate is true.
|
||||
|
||||
>>> quantify([True, False, True])
|
||||
2
|
||||
|
||||
"""
|
||||
return sum(map(pred, iterable))
|
||||
|
||||
|
||||
def padnone(iterable):
|
||||
"""Returns the sequence of elements and then returns ``None`` indefinitely.
|
||||
|
||||
>>> take(5, padnone(range(3)))
|
||||
[0, 1, 2, None, None]
|
||||
|
||||
Useful for emulating the behavior of the built-in :func:`map` function.
|
||||
|
||||
See also :func:`padded`.
|
||||
|
||||
"""
|
||||
return chain(iterable, repeat(None))
|
||||
|
||||
|
||||
def ncycles(iterable, n):
|
||||
"""Returns the sequence elements *n* times
|
||||
|
||||
>>> list(ncycles(["a", "b"], 3))
|
||||
['a', 'b', 'a', 'b', 'a', 'b']
|
||||
|
||||
"""
|
||||
return chain.from_iterable(repeat(tuple(iterable), n))
|
||||
|
||||
|
||||
def dotproduct(vec1, vec2):
|
||||
"""Returns the dot product of the two iterables.
|
||||
|
||||
>>> dotproduct([10, 10], [20, 20])
|
||||
400
|
||||
|
||||
"""
|
||||
return sum(map(operator.mul, vec1, vec2))
|
||||
|
||||
|
||||
def flatten(listOfLists):
|
||||
"""Return an iterator flattening one level of nesting in a list of lists.
|
||||
|
||||
>>> list(flatten([[0, 1], [2, 3]]))
|
||||
[0, 1, 2, 3]
|
||||
|
||||
See also :func:`collapse`, which can flatten multiple levels of nesting.
|
||||
|
||||
"""
|
||||
return chain.from_iterable(listOfLists)
|
||||
|
||||
|
||||
def repeatfunc(func, times=None, *args):
|
||||
"""Call *func* with *args* repeatedly, returning an iterable over the
|
||||
results.
|
||||
|
||||
If *times* is specified, the iterable will terminate after that many
|
||||
repetitions:
|
||||
|
||||
>>> from operator import add
|
||||
>>> times = 4
|
||||
>>> args = 3, 5
|
||||
>>> list(repeatfunc(add, times, *args))
|
||||
[8, 8, 8, 8]
|
||||
|
||||
If *times* is ``None`` the iterable will not terminate:
|
||||
|
||||
>>> from random import randrange
|
||||
>>> times = None
|
||||
>>> args = 1, 11
|
||||
>>> take(6, repeatfunc(randrange, times, *args)) # doctest:+SKIP
|
||||
[2, 4, 8, 1, 8, 4]
|
||||
|
||||
"""
|
||||
if times is None:
|
||||
return starmap(func, repeat(args))
|
||||
return starmap(func, repeat(args, times))
|
||||
|
||||
|
||||
def pairwise(iterable):
|
||||
"""Returns an iterator of paired items, overlapping, from the original
|
||||
|
||||
>>> take(4, pairwise(count()))
|
||||
[(0, 1), (1, 2), (2, 3), (3, 4)]
|
||||
|
||||
"""
|
||||
a, b = tee(iterable)
|
||||
next(b, None)
|
||||
return zip(a, b)
|
||||
|
||||
|
||||
def grouper(n, iterable, fillvalue=None):
|
||||
"""Collect data into fixed-length chunks or blocks.
|
||||
|
||||
>>> list(grouper(3, 'ABCDEFG', 'x'))
|
||||
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]
|
||||
|
||||
"""
|
||||
args = [iter(iterable)] * n
|
||||
return zip_longest(fillvalue=fillvalue, *args)
|
||||
|
||||
|
||||
def roundrobin(*iterables):
|
||||
"""Yields an item from each iterable, alternating between them.
|
||||
|
||||
>>> list(roundrobin('ABC', 'D', 'EF'))
|
||||
['A', 'D', 'E', 'B', 'F', 'C']
|
||||
|
||||
This function produces the same output as :func:`interleave_longest`, but
|
||||
may perform better for some inputs (in particular when the number of
|
||||
iterables is small).
|
||||
|
||||
"""
|
||||
# Recipe credited to George Sakkis
|
||||
pending = len(iterables)
|
||||
if PY2:
|
||||
nexts = cycle(iter(it).next for it in iterables)
|
||||
else:
|
||||
nexts = cycle(iter(it).__next__ for it in iterables)
|
||||
while pending:
|
||||
try:
|
||||
for next in nexts:
|
||||
yield next()
|
||||
except StopIteration:
|
||||
pending -= 1
|
||||
nexts = cycle(islice(nexts, pending))
|
||||
|
||||
|
||||
def partition(pred, iterable):
|
||||
"""
|
||||
Returns a 2-tuple of iterables derived from the input iterable.
|
||||
The first yields the items that have ``pred(item) == False``.
|
||||
The second yields the items that have ``pred(item) == True``.
|
||||
|
||||
>>> is_odd = lambda x: x % 2 != 0
|
||||
>>> iterable = range(10)
|
||||
>>> even_items, odd_items = partition(is_odd, iterable)
|
||||
>>> list(even_items), list(odd_items)
|
||||
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
|
||||
|
||||
"""
|
||||
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
|
||||
t1, t2 = tee(iterable)
|
||||
return filterfalse(pred, t1), filter(pred, t2)
|
||||
|
||||
|
||||
def powerset(iterable):
|
||||
"""Yields all possible subsets of the iterable.
|
||||
|
||||
>>> list(powerset([1, 2, 3]))
|
||||
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
|
||||
|
||||
:func:`powerset` will operate on iterables that aren't :class:`set`
|
||||
instances, so repeated elements in the input will produce repeated elements
|
||||
in the output. Use :func:`unique_everseen` on the input to avoid generating
|
||||
duplicates:
|
||||
|
||||
>>> seq = [1, 1, 0]
|
||||
>>> list(powerset(seq))
|
||||
[(), (1,), (1,), (0,), (1, 1), (1, 0), (1, 0), (1, 1, 0)]
|
||||
>>> from more_itertools import unique_everseen
|
||||
>>> list(powerset(unique_everseen(seq)))
|
||||
[(), (1,), (0,), (1, 0)]
|
||||
|
||||
"""
|
||||
s = list(iterable)
|
||||
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
|
||||
|
||||
|
||||
def unique_everseen(iterable, key=None):
|
||||
"""
|
||||
Yield unique elements, preserving order.
|
||||
|
||||
>>> list(unique_everseen('AAAABBBCCDAABBB'))
|
||||
['A', 'B', 'C', 'D']
|
||||
>>> list(unique_everseen('ABBCcAD', str.lower))
|
||||
['A', 'B', 'C', 'D']
|
||||
|
||||
Sequences with a mix of hashable and unhashable items can be used.
|
||||
The function will be slower (i.e., `O(n^2)`) for unhashable items.
|
||||
|
||||
"""
|
||||
seenset = set()
|
||||
seenset_add = seenset.add
|
||||
seenlist = []
|
||||
seenlist_add = seenlist.append
|
||||
if key is None:
|
||||
for element in iterable:
|
||||
try:
|
||||
if element not in seenset:
|
||||
seenset_add(element)
|
||||
yield element
|
||||
except TypeError:
|
||||
if element not in seenlist:
|
||||
seenlist_add(element)
|
||||
yield element
|
||||
else:
|
||||
for element in iterable:
|
||||
k = key(element)
|
||||
try:
|
||||
if k not in seenset:
|
||||
seenset_add(k)
|
||||
yield element
|
||||
except TypeError:
|
||||
if k not in seenlist:
|
||||
seenlist_add(k)
|
||||
yield element
|
||||
|
||||
|
||||
def unique_justseen(iterable, key=None):
|
||||
"""Yields elements in order, ignoring serial duplicates
|
||||
|
||||
>>> list(unique_justseen('AAAABBBCCDAABBB'))
|
||||
['A', 'B', 'C', 'D', 'A', 'B']
|
||||
>>> list(unique_justseen('ABBCcAD', str.lower))
|
||||
['A', 'B', 'C', 'A', 'D']
|
||||
|
||||
"""
|
||||
return map(next, map(operator.itemgetter(1), groupby(iterable, key)))
|
||||
|
||||
|
||||
def iter_except(func, exception, first=None):
|
||||
"""Yields results from a function repeatedly until an exception is raised.
|
||||
|
||||
Converts a call-until-exception interface to an iterator interface.
|
||||
Like ``iter(func, sentinel)``, but uses an exception instead of a sentinel
|
||||
to end the loop.
|
||||
|
||||
>>> l = [0, 1, 2]
|
||||
>>> list(iter_except(l.pop, IndexError))
|
||||
[2, 1, 0]
|
||||
|
||||
"""
|
||||
try:
|
||||
if first is not None:
|
||||
yield first()
|
||||
while 1:
|
||||
yield func()
|
||||
except exception:
|
||||
pass
|
||||
|
||||
|
||||
def first_true(iterable, default=None, pred=None):
|
||||
"""
|
||||
Returns the first true value in the iterable.
|
||||
|
||||
If no true value is found, returns *default*
|
||||
|
||||
If *pred* is not None, returns the first item for which
|
||||
``pred(item) == True`` .
|
||||
|
||||
>>> first_true(range(10))
|
||||
1
|
||||
>>> first_true(range(10), pred=lambda x: x > 5)
|
||||
6
|
||||
>>> first_true(range(10), default='missing', pred=lambda x: x > 9)
|
||||
'missing'
|
||||
|
||||
"""
|
||||
return next(filter(pred, iterable), default)
|
||||
|
||||
|
||||
def random_product(*args, **kwds):
|
||||
"""Draw an item at random from each of the input iterables.
|
||||
|
||||
>>> random_product('abc', range(4), 'XYZ') # doctest:+SKIP
|
||||
('c', 3, 'Z')
|
||||
|
||||
If *repeat* is provided as a keyword argument, that many items will be
|
||||
drawn from each iterable.
|
||||
|
||||
>>> random_product('abcd', range(4), repeat=2) # doctest:+SKIP
|
||||
('a', 2, 'd', 3)
|
||||
|
||||
This equivalent to taking a random selection from
|
||||
``itertools.product(*args, **kwarg)``.
|
||||
|
||||
"""
|
||||
pools = [tuple(pool) for pool in args] * kwds.get('repeat', 1)
|
||||
return tuple(choice(pool) for pool in pools)
|
||||
|
||||
|
||||
def random_permutation(iterable, r=None):
|
||||
"""Return a random *r* length permutation of the elements in *iterable*.
|
||||
|
||||
If *r* is not specified or is ``None``, then *r* defaults to the length of
|
||||
*iterable*.
|
||||
|
||||
>>> random_permutation(range(5)) # doctest:+SKIP
|
||||
(3, 4, 0, 1, 2)
|
||||
|
||||
This equivalent to taking a random selection from
|
||||
``itertools.permutations(iterable, r)``.
|
||||
|
||||
"""
|
||||
pool = tuple(iterable)
|
||||
r = len(pool) if r is None else r
|
||||
return tuple(sample(pool, r))
|
||||
|
||||
|
||||
def random_combination(iterable, r):
|
||||
"""Return a random *r* length subsequence of the elements in *iterable*.
|
||||
|
||||
>>> random_combination(range(5), 3) # doctest:+SKIP
|
||||
(2, 3, 4)
|
||||
|
||||
This equivalent to taking a random selection from
|
||||
``itertools.combinations(iterable, r)``.
|
||||
|
||||
"""
|
||||
pool = tuple(iterable)
|
||||
n = len(pool)
|
||||
indices = sorted(sample(range(n), r))
|
||||
return tuple(pool[i] for i in indices)
|
||||
|
||||
|
||||
def random_combination_with_replacement(iterable, r):
|
||||
"""Return a random *r* length subsequence of elements in *iterable*,
|
||||
allowing individual elements to be repeated.
|
||||
|
||||
>>> random_combination_with_replacement(range(3), 5) # doctest:+SKIP
|
||||
(0, 0, 1, 2, 2)
|
||||
|
||||
This equivalent to taking a random selection from
|
||||
``itertools.combinations_with_replacement(iterable, r)``.
|
||||
|
||||
"""
|
||||
pool = tuple(iterable)
|
||||
n = len(pool)
|
||||
indices = sorted(randrange(n) for i in range(r))
|
||||
return tuple(pool[i] for i in indices)
|
||||
|
||||
|
||||
def nth_combination(iterable, r, index):
|
||||
"""Equivalent to ``list(combinations(iterable, r))[index]``.
|
||||
|
||||
The subsequences of *iterable* that are of length *r* can be ordered
|
||||
lexicographically. :func:`nth_combination` computes the subsequence at
|
||||
sort position *index* directly, without computing the previous
|
||||
subsequences.
|
||||
|
||||
"""
|
||||
pool = tuple(iterable)
|
||||
n = len(pool)
|
||||
if (r < 0) or (r > n):
|
||||
raise ValueError
|
||||
|
||||
c = 1
|
||||
k = min(r, n - r)
|
||||
for i in range(1, k + 1):
|
||||
c = c * (n - k + i) // i
|
||||
|
||||
if index < 0:
|
||||
index += c
|
||||
|
||||
if (index < 0) or (index >= c):
|
||||
raise IndexError
|
||||
|
||||
result = []
|
||||
while r:
|
||||
c, n, r = c * r // n, n - 1, r - 1
|
||||
while index >= c:
|
||||
index -= c
|
||||
c, n = c * (n - r) // n, n - 1
|
||||
result.append(pool[-1 - n])
|
||||
|
||||
return tuple(result)
|
||||
|
||||
|
||||
def prepend(value, iterator):
|
||||
"""Yield *value*, followed by the elements in *iterator*.
|
||||
|
||||
>>> value = '0'
|
||||
>>> iterator = ['1', '2', '3']
|
||||
>>> list(prepend(value, iterator))
|
||||
['0', '1', '2', '3']
|
||||
|
||||
To prepend multiple values, see :func:`itertools.chain`.
|
||||
|
||||
"""
|
||||
return chain([value], iterator)
|
||||
+2313
File diff suppressed because it is too large
Load Diff
+616
@@ -0,0 +1,616 @@
|
||||
from doctest import DocTestSuite
|
||||
from unittest import TestCase
|
||||
|
||||
from itertools import combinations
|
||||
from six.moves import range
|
||||
|
||||
import more_itertools as mi
|
||||
|
||||
|
||||
def load_tests(loader, tests, ignore):
|
||||
# Add the doctests
|
||||
tests.addTests(DocTestSuite('more_itertools.recipes'))
|
||||
return tests
|
||||
|
||||
|
||||
class AccumulateTests(TestCase):
|
||||
"""Tests for ``accumulate()``"""
|
||||
|
||||
def test_empty(self):
|
||||
"""Test that an empty input returns an empty output"""
|
||||
self.assertEqual(list(mi.accumulate([])), [])
|
||||
|
||||
def test_default(self):
|
||||
"""Test accumulate with the default function (addition)"""
|
||||
self.assertEqual(list(mi.accumulate([1, 2, 3])), [1, 3, 6])
|
||||
|
||||
def test_bogus_function(self):
|
||||
"""Test accumulate with an invalid function"""
|
||||
with self.assertRaises(TypeError):
|
||||
list(mi.accumulate([1, 2, 3], func=lambda x: x))
|
||||
|
||||
def test_custom_function(self):
|
||||
"""Test accumulate with a custom function"""
|
||||
self.assertEqual(
|
||||
list(mi.accumulate((1, 2, 3, 2, 1), func=max)), [1, 2, 3, 3, 3]
|
||||
)
|
||||
|
||||
|
||||
class TakeTests(TestCase):
|
||||
"""Tests for ``take()``"""
|
||||
|
||||
def test_simple_take(self):
|
||||
"""Test basic usage"""
|
||||
t = mi.take(5, range(10))
|
||||
self.assertEqual(t, [0, 1, 2, 3, 4])
|
||||
|
||||
def test_null_take(self):
|
||||
"""Check the null case"""
|
||||
t = mi.take(0, range(10))
|
||||
self.assertEqual(t, [])
|
||||
|
||||
def test_negative_take(self):
|
||||
"""Make sure taking negative items results in a ValueError"""
|
||||
self.assertRaises(ValueError, lambda: mi.take(-3, range(10)))
|
||||
|
||||
def test_take_too_much(self):
|
||||
"""Taking more than an iterator has remaining should return what the
|
||||
iterator has remaining.
|
||||
|
||||
"""
|
||||
t = mi.take(10, range(5))
|
||||
self.assertEqual(t, [0, 1, 2, 3, 4])
|
||||
|
||||
|
||||
class TabulateTests(TestCase):
|
||||
"""Tests for ``tabulate()``"""
|
||||
|
||||
def test_simple_tabulate(self):
|
||||
"""Test the happy path"""
|
||||
t = mi.tabulate(lambda x: x)
|
||||
f = tuple([next(t) for _ in range(3)])
|
||||
self.assertEqual(f, (0, 1, 2))
|
||||
|
||||
def test_count(self):
|
||||
"""Ensure tabulate accepts specific count"""
|
||||
t = mi.tabulate(lambda x: 2 * x, -1)
|
||||
f = (next(t), next(t), next(t))
|
||||
self.assertEqual(f, (-2, 0, 2))
|
||||
|
||||
|
||||
class TailTests(TestCase):
|
||||
"""Tests for ``tail()``"""
|
||||
|
||||
def test_greater(self):
|
||||
"""Length of iterable is greater than requested tail"""
|
||||
self.assertEqual(list(mi.tail(3, 'ABCDEFG')), ['E', 'F', 'G'])
|
||||
|
||||
def test_equal(self):
|
||||
"""Length of iterable is equal to the requested tail"""
|
||||
self.assertEqual(
|
||||
list(mi.tail(7, 'ABCDEFG')), ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
||||
)
|
||||
|
||||
def test_less(self):
|
||||
"""Length of iterable is less than requested tail"""
|
||||
self.assertEqual(
|
||||
list(mi.tail(8, 'ABCDEFG')), ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
||||
)
|
||||
|
||||
|
||||
class ConsumeTests(TestCase):
|
||||
"""Tests for ``consume()``"""
|
||||
|
||||
def test_sanity(self):
|
||||
"""Test basic functionality"""
|
||||
r = (x for x in range(10))
|
||||
mi.consume(r, 3)
|
||||
self.assertEqual(3, next(r))
|
||||
|
||||
def test_null_consume(self):
|
||||
"""Check the null case"""
|
||||
r = (x for x in range(10))
|
||||
mi.consume(r, 0)
|
||||
self.assertEqual(0, next(r))
|
||||
|
||||
def test_negative_consume(self):
|
||||
"""Check that negative consumsion throws an error"""
|
||||
r = (x for x in range(10))
|
||||
self.assertRaises(ValueError, lambda: mi.consume(r, -1))
|
||||
|
||||
def test_total_consume(self):
|
||||
"""Check that iterator is totally consumed by default"""
|
||||
r = (x for x in range(10))
|
||||
mi.consume(r)
|
||||
self.assertRaises(StopIteration, lambda: next(r))
|
||||
|
||||
|
||||
class NthTests(TestCase):
|
||||
"""Tests for ``nth()``"""
|
||||
|
||||
def test_basic(self):
|
||||
"""Make sure the nth item is returned"""
|
||||
l = range(10)
|
||||
for i, v in enumerate(l):
|
||||
self.assertEqual(mi.nth(l, i), v)
|
||||
|
||||
def test_default(self):
|
||||
"""Ensure a default value is returned when nth item not found"""
|
||||
l = range(3)
|
||||
self.assertEqual(mi.nth(l, 100, "zebra"), "zebra")
|
||||
|
||||
def test_negative_item_raises(self):
|
||||
"""Ensure asking for a negative item raises an exception"""
|
||||
self.assertRaises(ValueError, lambda: mi.nth(range(10), -3))
|
||||
|
||||
|
||||
class AllEqualTests(TestCase):
|
||||
"""Tests for ``all_equal()``"""
|
||||
|
||||
def test_true(self):
|
||||
"""Everything is equal"""
|
||||
self.assertTrue(mi.all_equal('aaaaaa'))
|
||||
self.assertTrue(mi.all_equal([0, 0, 0, 0]))
|
||||
|
||||
def test_false(self):
|
||||
"""Not everything is equal"""
|
||||
self.assertFalse(mi.all_equal('aaaaab'))
|
||||
self.assertFalse(mi.all_equal([0, 0, 0, 1]))
|
||||
|
||||
def test_tricky(self):
|
||||
"""Not everything is identical, but everything is equal"""
|
||||
items = [1, complex(1, 0), 1.0]
|
||||
self.assertTrue(mi.all_equal(items))
|
||||
|
||||
def test_empty(self):
|
||||
"""Return True if the iterable is empty"""
|
||||
self.assertTrue(mi.all_equal(''))
|
||||
self.assertTrue(mi.all_equal([]))
|
||||
|
||||
def test_one(self):
|
||||
"""Return True if the iterable is singular"""
|
||||
self.assertTrue(mi.all_equal('0'))
|
||||
self.assertTrue(mi.all_equal([0]))
|
||||
|
||||
|
||||
class QuantifyTests(TestCase):
|
||||
"""Tests for ``quantify()``"""
|
||||
|
||||
def test_happy_path(self):
|
||||
"""Make sure True count is returned"""
|
||||
q = [True, False, True]
|
||||
self.assertEqual(mi.quantify(q), 2)
|
||||
|
||||
def test_custom_predicate(self):
|
||||
"""Ensure non-default predicates return as expected"""
|
||||
q = range(10)
|
||||
self.assertEqual(mi.quantify(q, lambda x: x % 2 == 0), 5)
|
||||
|
||||
|
||||
class PadnoneTests(TestCase):
|
||||
"""Tests for ``padnone()``"""
|
||||
|
||||
def test_happy_path(self):
|
||||
"""wrapper iterator should return None indefinitely"""
|
||||
r = range(2)
|
||||
p = mi.padnone(r)
|
||||
self.assertEqual([0, 1, None, None], [next(p) for _ in range(4)])
|
||||
|
||||
|
||||
class NcyclesTests(TestCase):
|
||||
"""Tests for ``nyclces()``"""
|
||||
|
||||
def test_happy_path(self):
|
||||
"""cycle a sequence three times"""
|
||||
r = ["a", "b", "c"]
|
||||
n = mi.ncycles(r, 3)
|
||||
self.assertEqual(
|
||||
["a", "b", "c", "a", "b", "c", "a", "b", "c"],
|
||||
list(n)
|
||||
)
|
||||
|
||||
def test_null_case(self):
|
||||
"""asking for 0 cycles should return an empty iterator"""
|
||||
n = mi.ncycles(range(100), 0)
|
||||
self.assertRaises(StopIteration, lambda: next(n))
|
||||
|
||||
def test_pathalogical_case(self):
|
||||
"""asking for negative cycles should return an empty iterator"""
|
||||
n = mi.ncycles(range(100), -10)
|
||||
self.assertRaises(StopIteration, lambda: next(n))
|
||||
|
||||
|
||||
class DotproductTests(TestCase):
|
||||
"""Tests for ``dotproduct()``'"""
|
||||
|
||||
def test_happy_path(self):
|
||||
"""simple dotproduct example"""
|
||||
self.assertEqual(400, mi.dotproduct([10, 10], [20, 20]))
|
||||
|
||||
|
||||
class FlattenTests(TestCase):
|
||||
"""Tests for ``flatten()``"""
|
||||
|
||||
def test_basic_usage(self):
|
||||
"""ensure list of lists is flattened one level"""
|
||||
f = [[0, 1, 2], [3, 4, 5]]
|
||||
self.assertEqual(list(range(6)), list(mi.flatten(f)))
|
||||
|
||||
def test_single_level(self):
|
||||
"""ensure list of lists is flattened only one level"""
|
||||
f = [[0, [1, 2]], [[3, 4], 5]]
|
||||
self.assertEqual([0, [1, 2], [3, 4], 5], list(mi.flatten(f)))
|
||||
|
||||
|
||||
class RepeatfuncTests(TestCase):
|
||||
"""Tests for ``repeatfunc()``"""
|
||||
|
||||
def test_simple_repeat(self):
|
||||
"""test simple repeated functions"""
|
||||
r = mi.repeatfunc(lambda: 5)
|
||||
self.assertEqual([5, 5, 5, 5, 5], [next(r) for _ in range(5)])
|
||||
|
||||
def test_finite_repeat(self):
|
||||
"""ensure limited repeat when times is provided"""
|
||||
r = mi.repeatfunc(lambda: 5, times=5)
|
||||
self.assertEqual([5, 5, 5, 5, 5], list(r))
|
||||
|
||||
def test_added_arguments(self):
|
||||
"""ensure arguments are applied to the function"""
|
||||
r = mi.repeatfunc(lambda x: x, 2, 3)
|
||||
self.assertEqual([3, 3], list(r))
|
||||
|
||||
def test_null_times(self):
|
||||
"""repeat 0 should return an empty iterator"""
|
||||
r = mi.repeatfunc(range, 0, 3)
|
||||
self.assertRaises(StopIteration, lambda: next(r))
|
||||
|
||||
|
||||
class PairwiseTests(TestCase):
|
||||
"""Tests for ``pairwise()``"""
|
||||
|
||||
def test_base_case(self):
|
||||
"""ensure an iterable will return pairwise"""
|
||||
p = mi.pairwise([1, 2, 3])
|
||||
self.assertEqual([(1, 2), (2, 3)], list(p))
|
||||
|
||||
def test_short_case(self):
|
||||
"""ensure an empty iterator if there's not enough values to pair"""
|
||||
p = mi.pairwise("a")
|
||||
self.assertRaises(StopIteration, lambda: next(p))
|
||||
|
||||
|
||||
class GrouperTests(TestCase):
|
||||
"""Tests for ``grouper()``"""
|
||||
|
||||
def test_even(self):
|
||||
"""Test when group size divides evenly into the length of
|
||||
the iterable.
|
||||
|
||||
"""
|
||||
self.assertEqual(
|
||||
list(mi.grouper(3, 'ABCDEF')), [('A', 'B', 'C'), ('D', 'E', 'F')]
|
||||
)
|
||||
|
||||
def test_odd(self):
|
||||
"""Test when group size does not divide evenly into the length of the
|
||||
iterable.
|
||||
|
||||
"""
|
||||
self.assertEqual(
|
||||
list(mi.grouper(3, 'ABCDE')), [('A', 'B', 'C'), ('D', 'E', None)]
|
||||
)
|
||||
|
||||
def test_fill_value(self):
|
||||
"""Test that the fill value is used to pad the final group"""
|
||||
self.assertEqual(
|
||||
list(mi.grouper(3, 'ABCDE', 'x')),
|
||||
[('A', 'B', 'C'), ('D', 'E', 'x')]
|
||||
)
|
||||
|
||||
|
||||
class RoundrobinTests(TestCase):
|
||||
"""Tests for ``roundrobin()``"""
|
||||
|
||||
def test_even_groups(self):
|
||||
"""Ensure ordered output from evenly populated iterables"""
|
||||
self.assertEqual(
|
||||
list(mi.roundrobin('ABC', [1, 2, 3], range(3))),
|
||||
['A', 1, 0, 'B', 2, 1, 'C', 3, 2]
|
||||
)
|
||||
|
||||
def test_uneven_groups(self):
|
||||
"""Ensure ordered output from unevenly populated iterables"""
|
||||
self.assertEqual(
|
||||
list(mi.roundrobin('ABCD', [1, 2], range(0))),
|
||||
['A', 1, 'B', 2, 'C', 'D']
|
||||
)
|
||||
|
||||
|
||||
class PartitionTests(TestCase):
|
||||
"""Tests for ``partition()``"""
|
||||
|
||||
def test_bool(self):
|
||||
"""Test when pred() returns a boolean"""
|
||||
lesser, greater = mi.partition(lambda x: x > 5, range(10))
|
||||
self.assertEqual(list(lesser), [0, 1, 2, 3, 4, 5])
|
||||
self.assertEqual(list(greater), [6, 7, 8, 9])
|
||||
|
||||
def test_arbitrary(self):
|
||||
"""Test when pred() returns an integer"""
|
||||
divisibles, remainders = mi.partition(lambda x: x % 3, range(10))
|
||||
self.assertEqual(list(divisibles), [0, 3, 6, 9])
|
||||
self.assertEqual(list(remainders), [1, 2, 4, 5, 7, 8])
|
||||
|
||||
|
||||
class PowersetTests(TestCase):
|
||||
"""Tests for ``powerset()``"""
|
||||
|
||||
def test_combinatorics(self):
|
||||
"""Ensure a proper enumeration"""
|
||||
p = mi.powerset([1, 2, 3])
|
||||
self.assertEqual(
|
||||
list(p),
|
||||
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
|
||||
)
|
||||
|
||||
|
||||
class UniqueEverseenTests(TestCase):
|
||||
"""Tests for ``unique_everseen()``"""
|
||||
|
||||
def test_everseen(self):
|
||||
"""ensure duplicate elements are ignored"""
|
||||
u = mi.unique_everseen('AAAABBBBCCDAABBB')
|
||||
self.assertEqual(
|
||||
['A', 'B', 'C', 'D'],
|
||||
list(u)
|
||||
)
|
||||
|
||||
def test_custom_key(self):
|
||||
"""ensure the custom key comparison works"""
|
||||
u = mi.unique_everseen('aAbACCc', key=str.lower)
|
||||
self.assertEqual(list('abC'), list(u))
|
||||
|
||||
def test_unhashable(self):
|
||||
"""ensure things work for unhashable items"""
|
||||
iterable = ['a', [1, 2, 3], [1, 2, 3], 'a']
|
||||
u = mi.unique_everseen(iterable)
|
||||
self.assertEqual(list(u), ['a', [1, 2, 3]])
|
||||
|
||||
def test_unhashable_key(self):
|
||||
"""ensure things work for unhashable items with a custom key"""
|
||||
iterable = ['a', [1, 2, 3], [1, 2, 3], 'a']
|
||||
u = mi.unique_everseen(iterable, key=lambda x: x)
|
||||
self.assertEqual(list(u), ['a', [1, 2, 3]])
|
||||
|
||||
|
||||
class UniqueJustseenTests(TestCase):
|
||||
"""Tests for ``unique_justseen()``"""
|
||||
|
||||
def test_justseen(self):
|
||||
"""ensure only last item is remembered"""
|
||||
u = mi.unique_justseen('AAAABBBCCDABB')
|
||||
self.assertEqual(list('ABCDAB'), list(u))
|
||||
|
||||
def test_custom_key(self):
|
||||
"""ensure the custom key comparison works"""
|
||||
u = mi.unique_justseen('AABCcAD', str.lower)
|
||||
self.assertEqual(list('ABCAD'), list(u))
|
||||
|
||||
|
||||
class IterExceptTests(TestCase):
|
||||
"""Tests for ``iter_except()``"""
|
||||
|
||||
def test_exact_exception(self):
|
||||
"""ensure the exact specified exception is caught"""
|
||||
l = [1, 2, 3]
|
||||
i = mi.iter_except(l.pop, IndexError)
|
||||
self.assertEqual(list(i), [3, 2, 1])
|
||||
|
||||
def test_generic_exception(self):
|
||||
"""ensure the generic exception can be caught"""
|
||||
l = [1, 2]
|
||||
i = mi.iter_except(l.pop, Exception)
|
||||
self.assertEqual(list(i), [2, 1])
|
||||
|
||||
def test_uncaught_exception_is_raised(self):
|
||||
"""ensure a non-specified exception is raised"""
|
||||
l = [1, 2, 3]
|
||||
i = mi.iter_except(l.pop, KeyError)
|
||||
self.assertRaises(IndexError, lambda: list(i))
|
||||
|
||||
def test_first(self):
|
||||
"""ensure first is run before the function"""
|
||||
l = [1, 2, 3]
|
||||
f = lambda: 25
|
||||
i = mi.iter_except(l.pop, IndexError, f)
|
||||
self.assertEqual(list(i), [25, 3, 2, 1])
|
||||
|
||||
|
||||
class FirstTrueTests(TestCase):
|
||||
"""Tests for ``first_true()``"""
|
||||
|
||||
def test_something_true(self):
|
||||
"""Test with no keywords"""
|
||||
self.assertEqual(mi.first_true(range(10)), 1)
|
||||
|
||||
def test_nothing_true(self):
|
||||
"""Test default return value."""
|
||||
self.assertIsNone(mi.first_true([0, 0, 0]))
|
||||
|
||||
def test_default(self):
|
||||
"""Test with a default keyword"""
|
||||
self.assertEqual(mi.first_true([0, 0, 0], default='!'), '!')
|
||||
|
||||
def test_pred(self):
|
||||
"""Test with a custom predicate"""
|
||||
self.assertEqual(
|
||||
mi.first_true([2, 4, 6], pred=lambda x: x % 3 == 0), 6
|
||||
)
|
||||
|
||||
|
||||
class RandomProductTests(TestCase):
|
||||
"""Tests for ``random_product()``
|
||||
|
||||
Since random.choice() has different results with the same seed across
|
||||
python versions 2.x and 3.x, these tests use highly probably events to
|
||||
create predictable outcomes across platforms.
|
||||
"""
|
||||
|
||||
def test_simple_lists(self):
|
||||
"""Ensure that one item is chosen from each list in each pair.
|
||||
Also ensure that each item from each list eventually appears in
|
||||
the chosen combinations.
|
||||
|
||||
Odds are roughly 1 in 7.1 * 10e16 that one item from either list will
|
||||
not be chosen after 100 samplings of one item from each list. Just to
|
||||
be safe, better use a known random seed, too.
|
||||
|
||||
"""
|
||||
nums = [1, 2, 3]
|
||||
lets = ['a', 'b', 'c']
|
||||
n, m = zip(*[mi.random_product(nums, lets) for _ in range(100)])
|
||||
n, m = set(n), set(m)
|
||||
self.assertEqual(n, set(nums))
|
||||
self.assertEqual(m, set(lets))
|
||||
self.assertEqual(len(n), len(nums))
|
||||
self.assertEqual(len(m), len(lets))
|
||||
|
||||
def test_list_with_repeat(self):
|
||||
"""ensure multiple items are chosen, and that they appear to be chosen
|
||||
from one list then the next, in proper order.
|
||||
|
||||
"""
|
||||
nums = [1, 2, 3]
|
||||
lets = ['a', 'b', 'c']
|
||||
r = list(mi.random_product(nums, lets, repeat=100))
|
||||
self.assertEqual(2 * 100, len(r))
|
||||
n, m = set(r[::2]), set(r[1::2])
|
||||
self.assertEqual(n, set(nums))
|
||||
self.assertEqual(m, set(lets))
|
||||
self.assertEqual(len(n), len(nums))
|
||||
self.assertEqual(len(m), len(lets))
|
||||
|
||||
|
||||
class RandomPermutationTests(TestCase):
|
||||
"""Tests for ``random_permutation()``"""
|
||||
|
||||
def test_full_permutation(self):
|
||||
"""ensure every item from the iterable is returned in a new ordering
|
||||
|
||||
15 elements have a 1 in 1.3 * 10e12 of appearing in sorted order, so
|
||||
we fix a seed value just to be sure.
|
||||
|
||||
"""
|
||||
i = range(15)
|
||||
r = mi.random_permutation(i)
|
||||
self.assertEqual(set(i), set(r))
|
||||
if i == r:
|
||||
raise AssertionError("Values were not permuted")
|
||||
|
||||
def test_partial_permutation(self):
|
||||
"""ensure all returned items are from the iterable, that the returned
|
||||
permutation is of the desired length, and that all items eventually
|
||||
get returned.
|
||||
|
||||
Sampling 100 permutations of length 5 from a set of 15 leaves a
|
||||
(2/3)^100 chance that an item will not be chosen. Multiplied by 15
|
||||
items, there is a 1 in 2.6e16 chance that at least 1 item will not
|
||||
show up in the resulting output. Using a random seed will fix that.
|
||||
|
||||
"""
|
||||
items = range(15)
|
||||
item_set = set(items)
|
||||
all_items = set()
|
||||
for _ in range(100):
|
||||
permutation = mi.random_permutation(items, 5)
|
||||
self.assertEqual(len(permutation), 5)
|
||||
permutation_set = set(permutation)
|
||||
self.assertLessEqual(permutation_set, item_set)
|
||||
all_items |= permutation_set
|
||||
self.assertEqual(all_items, item_set)
|
||||
|
||||
|
||||
class RandomCombinationTests(TestCase):
|
||||
"""Tests for ``random_combination()``"""
|
||||
|
||||
def test_pseudorandomness(self):
|
||||
"""ensure different subsets of the iterable get returned over many
|
||||
samplings of random combinations"""
|
||||
items = range(15)
|
||||
all_items = set()
|
||||
for _ in range(50):
|
||||
combination = mi.random_combination(items, 5)
|
||||
all_items |= set(combination)
|
||||
self.assertEqual(all_items, set(items))
|
||||
|
||||
def test_no_replacement(self):
|
||||
"""ensure that elements are sampled without replacement"""
|
||||
items = range(15)
|
||||
for _ in range(50):
|
||||
combination = mi.random_combination(items, len(items))
|
||||
self.assertEqual(len(combination), len(set(combination)))
|
||||
self.assertRaises(
|
||||
ValueError, lambda: mi.random_combination(items, len(items) + 1)
|
||||
)
|
||||
|
||||
|
||||
class RandomCombinationWithReplacementTests(TestCase):
|
||||
"""Tests for ``random_combination_with_replacement()``"""
|
||||
|
||||
def test_replacement(self):
|
||||
"""ensure that elements are sampled with replacement"""
|
||||
items = range(5)
|
||||
combo = mi.random_combination_with_replacement(items, len(items) * 2)
|
||||
self.assertEqual(2 * len(items), len(combo))
|
||||
if len(set(combo)) == len(combo):
|
||||
raise AssertionError("Combination contained no duplicates")
|
||||
|
||||
def test_pseudorandomness(self):
|
||||
"""ensure different subsets of the iterable get returned over many
|
||||
samplings of random combinations"""
|
||||
items = range(15)
|
||||
all_items = set()
|
||||
for _ in range(50):
|
||||
combination = mi.random_combination_with_replacement(items, 5)
|
||||
all_items |= set(combination)
|
||||
self.assertEqual(all_items, set(items))
|
||||
|
||||
|
||||
class NthCombinationTests(TestCase):
|
||||
def test_basic(self):
|
||||
iterable = 'abcdefg'
|
||||
r = 4
|
||||
for index, expected in enumerate(combinations(iterable, r)):
|
||||
actual = mi.nth_combination(iterable, r, index)
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_long(self):
|
||||
actual = mi.nth_combination(range(180), 4, 2000000)
|
||||
expected = (2, 12, 35, 126)
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_invalid_r(self):
|
||||
for r in (-1, 3):
|
||||
with self.assertRaises(ValueError):
|
||||
mi.nth_combination([], r, 0)
|
||||
|
||||
def test_invalid_index(self):
|
||||
with self.assertRaises(IndexError):
|
||||
mi.nth_combination('abcdefg', 3, -36)
|
||||
|
||||
|
||||
class PrependTests(TestCase):
|
||||
def test_basic(self):
|
||||
value = 'a'
|
||||
iterator = iter('bcdefg')
|
||||
actual = list(mi.prepend(value, iterator))
|
||||
expected = list('abcdefg')
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_multiple(self):
|
||||
value = 'ab'
|
||||
iterator = iter('cdefg')
|
||||
actual = tuple(mi.prepend(value, iterator))
|
||||
expected = ('ab',) + tuple('cdefg')
|
||||
self.assertEqual(actual, expected)
|
||||
+17
-88
@@ -11,7 +11,6 @@ import packaging.markers
|
||||
import packaging.version
|
||||
import pip_shims.shims
|
||||
import requests
|
||||
from first import first
|
||||
from packaging.utils import canonicalize_name
|
||||
from vistir.compat import JSONDecodeError, fs_str
|
||||
from vistir.contextmanagers import cd, temp_environ
|
||||
@@ -20,6 +19,7 @@ from vistir.path import create_tracked_tempdir
|
||||
from ..environment import MYPY_RUNNING
|
||||
from ..utils import _ensure_dir, prepare_pip_source_args
|
||||
from .cache import CACHE_DIR, DependencyCache
|
||||
from .setup_info import SetupInfo
|
||||
from .utils import (
|
||||
clean_requires_python,
|
||||
fix_requires_python_marker,
|
||||
@@ -139,9 +139,9 @@ class AbstractDependency(object):
|
||||
:rtype: set(str)
|
||||
"""
|
||||
|
||||
if len(self.candidates) == 1 and first(self.candidates).editable:
|
||||
if len(self.candidates) == 1 and next(iter(self.candidates)).editable:
|
||||
return self
|
||||
elif len(other.candidates) == 1 and first(other.candidates).editable:
|
||||
elif len(other.candidates) == 1 and next(iter(other.candidates)).editable:
|
||||
return other
|
||||
return self.version_set & other.version_set
|
||||
|
||||
@@ -158,9 +158,9 @@ class AbstractDependency(object):
|
||||
|
||||
from .requirements import Requirement
|
||||
|
||||
if len(self.candidates) == 1 and first(self.candidates).editable:
|
||||
if len(self.candidates) == 1 and next(iter(self.candidates)).editable:
|
||||
return self
|
||||
elif len(other.candidates) == 1 and first(other.candidates).editable:
|
||||
elif len(other.candidates) == 1 and next(iter(other.candidates)).editable:
|
||||
return other
|
||||
new_specifiers = self.specifiers & other.specifiers
|
||||
markers = set(self.markers) if self.markers else set()
|
||||
@@ -475,90 +475,19 @@ def get_dependencies_from_index(dep, sources=None, pip_options=None, wheel_cache
|
||||
if not wheel_cache:
|
||||
wheel_cache = WHEEL_CACHE
|
||||
dep.is_direct = True
|
||||
reqset = pip_shims.shims.RequirementSet()
|
||||
reqset.add_requirement(dep)
|
||||
requirements = None
|
||||
setup_requires = {}
|
||||
with temp_environ(), start_resolver(
|
||||
finder=finder, session=session, wheel_cache=wheel_cache
|
||||
) as resolver:
|
||||
with temp_environ():
|
||||
os.environ["PIP_EXISTS_ACTION"] = "i"
|
||||
dist = None
|
||||
if dep.editable and not dep.prepared and not dep.req:
|
||||
with cd(dep.setup_py_dir):
|
||||
from setuptools.dist import distutils
|
||||
|
||||
try:
|
||||
dist = distutils.core.run_setup(dep.setup_py)
|
||||
except (ImportError, TypeError, AttributeError):
|
||||
dist = None
|
||||
else:
|
||||
setup_requires[dist.get_name()] = dist.setup_requires
|
||||
if not dist:
|
||||
try:
|
||||
dist = dep.get_dist()
|
||||
except (TypeError, ValueError, AttributeError):
|
||||
pass
|
||||
else:
|
||||
setup_requires[dist.get_name()] = dist.setup_requires
|
||||
resolver.require_hashes = False
|
||||
try:
|
||||
results = resolver._resolve_one(reqset, dep)
|
||||
except Exception:
|
||||
# FIXME: Needs to bubble the exception somehow to the user.
|
||||
results = []
|
||||
finally:
|
||||
try:
|
||||
wheel_cache.cleanup()
|
||||
except AttributeError:
|
||||
pass
|
||||
resolver_requires_python = getattr(resolver, "requires_python", None)
|
||||
requires_python = getattr(reqset, "requires_python", resolver_requires_python)
|
||||
if requires_python:
|
||||
add_marker = fix_requires_python_marker(requires_python)
|
||||
reqset.remove(dep)
|
||||
if dep.req.marker:
|
||||
dep.req.marker._markers.extend(["and"].extend(add_marker._markers))
|
||||
else:
|
||||
dep.req.marker = add_marker
|
||||
reqset.add(dep)
|
||||
requirements = set()
|
||||
for r in results:
|
||||
if requires_python:
|
||||
if r.req.marker:
|
||||
r.req.marker._markers.extend(["and"].extend(add_marker._markers))
|
||||
else:
|
||||
r.req.marker = add_marker
|
||||
requirements.add(format_requirement(r))
|
||||
for section in setup_requires:
|
||||
python_version = section
|
||||
not_python = not is_python(section)
|
||||
|
||||
# This is for cleaning up :extras: formatted markers
|
||||
# by adding them to the results of the resolver
|
||||
# since any such extra would have been returned as a result anyway
|
||||
for value in setup_requires[section]:
|
||||
|
||||
# This is a marker.
|
||||
if is_python(section):
|
||||
python_version = value[1:-1]
|
||||
else:
|
||||
not_python = True
|
||||
|
||||
if ":" not in value and not_python:
|
||||
try:
|
||||
requirement_str = "{0}{1}".format(value, python_version).replace(
|
||||
":", ";"
|
||||
)
|
||||
requirements.add(
|
||||
format_requirement(
|
||||
make_install_requirement(requirement_str).ireq
|
||||
)
|
||||
)
|
||||
# Anything could go wrong here -- can't be too careful.
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
setup_info = SetupInfo.from_ireq(dep)
|
||||
results = setup_info.get_info()
|
||||
setup_requires.update(results["setup_requires"])
|
||||
requirements = set(results["requires"].values())
|
||||
else:
|
||||
results = pip_shims.shims.resolve(dep)
|
||||
requirements = [v for v in results.values() if v.name != dep.name]
|
||||
requirements = set([format_requirement(r) for r in requirements])
|
||||
if not dep.editable and is_pinned_requirement(dep) and requirements is not None:
|
||||
DEPENDENCY_CACHE[dep] = list(requirements)
|
||||
return requirements
|
||||
@@ -685,10 +614,10 @@ def get_grouped_dependencies(constraints):
|
||||
# then we take the loose match (which _is_ flexible) and start moving backwards in
|
||||
# versions by popping them off of a stack and checking for the conflicting package
|
||||
for _, ireqs in full_groupby(constraints, key=key_from_ireq):
|
||||
ireqs = list(ireqs)
|
||||
editable_ireq = first(ireqs, key=lambda ireq: ireq.editable)
|
||||
ireqs = sorted(ireqs, key=lambda ireq: ireq.editable)
|
||||
editable_ireq = next(iter(ireq for ireq in ireqs if ireq.editable), None)
|
||||
if editable_ireq:
|
||||
yield editable_ireq # ignore all the other specs: the editable one is the one that counts
|
||||
yield editable_ireq # only the editable match mattters, ignore all others
|
||||
continue
|
||||
ireqs = iter(ireqs)
|
||||
# deepcopy the accumulator so as to not modify the self.our_constraints invariant
|
||||
|
||||
+83
-31
@@ -11,9 +11,9 @@ from packaging.specifiers import Specifier, SpecifierSet
|
||||
from vistir.compat import Mapping, Set, lru_cache
|
||||
from vistir.misc import dedup
|
||||
|
||||
from .utils import filter_none, validate_markers
|
||||
from ..environment import MYPY_RUNNING
|
||||
from ..exceptions import RequirementError
|
||||
from .utils import filter_none, validate_markers
|
||||
|
||||
from six.moves import reduce # isort:skip
|
||||
|
||||
@@ -24,7 +24,8 @@ if MYPY_RUNNING:
|
||||
STRING_TYPE = Union[str, bytes, Text]
|
||||
|
||||
|
||||
MAX_VERSIONS = {2: 7, 3: 10}
|
||||
MAX_VERSIONS = {2: 7, 3: 11, 4: 0}
|
||||
DEPRECATED_VERSIONS = ["3.0", "3.1", "3.2", "3.3"]
|
||||
|
||||
|
||||
def is_instance(item, cls):
|
||||
@@ -147,9 +148,8 @@ def _format_pyspec(specifier):
|
||||
version = getattr(specifier, "version", specifier).rstrip()
|
||||
if version and version.endswith("*"):
|
||||
if version.endswith(".*"):
|
||||
version = version.rstrip(".*")
|
||||
else:
|
||||
version = version.rstrip("*")
|
||||
version = version[:-2]
|
||||
version = version.rstrip("*")
|
||||
specifier = Specifier("{0}{1}".format(specifier.operator, version))
|
||||
try:
|
||||
op = REPLACE_RANGES[specifier.operator]
|
||||
@@ -196,6 +196,7 @@ def _get_specs(specset):
|
||||
return sorted(result, key=operator.itemgetter(1))
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful
|
||||
def _group_by_op(specs):
|
||||
# type: (Union[Set[Specifier], SpecifierSet]) -> Iterator
|
||||
specs = [_get_specs(x) for x in list(specs)]
|
||||
@@ -205,6 +206,7 @@ def _group_by_op(specs):
|
||||
return grouping
|
||||
|
||||
|
||||
# TODO: rename this to something meaningful
|
||||
def normalize_specifier_set(specs):
|
||||
# type: (Union[str, SpecifierSet]) -> Optional[Set[Specifier]]
|
||||
"""Given a specifier set, a string, or an iterable, normalize the specifiers
|
||||
@@ -227,14 +229,16 @@ def normalize_specifier_set(specs):
|
||||
return {_format_pyspec(spec) for spec in specs}
|
||||
spec_list = []
|
||||
for spec in specs.split(","):
|
||||
spec = spec.strip()
|
||||
if spec.endswith(".*"):
|
||||
spec = spec.rstrip(".*")
|
||||
elif spec.endswith("*"):
|
||||
spec = spec.rstrip("*")
|
||||
spec = spec[:-2]
|
||||
spec = spec.rstrip("*")
|
||||
spec_list.append(spec)
|
||||
return normalize_specifier_set(SpecifierSet(",".join(spec_list)))
|
||||
|
||||
|
||||
# TODO: Check if this is used by anything public otherwise make it private
|
||||
# And rename it to something meaningful
|
||||
def get_sorted_version_string(version_set):
|
||||
# type: (Set[AnyStr]) -> AnyStr
|
||||
version_list = sorted(
|
||||
@@ -244,6 +248,9 @@ def get_sorted_version_string(version_set):
|
||||
return version
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful
|
||||
# TODO: Add a deprecation decorator and deprecate this -- i'm sure it's used
|
||||
# in other libraries
|
||||
@lru_cache(maxsize=1024)
|
||||
def cleanup_pyspecs(specs, joiner="or"):
|
||||
specs = normalize_specifier_set(specs)
|
||||
@@ -288,6 +295,7 @@ def cleanup_pyspecs(specs, joiner="or"):
|
||||
return sorted([(k, v) for k, v in results.items()], key=operator.itemgetter(1))
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful
|
||||
@lru_cache(maxsize=1024)
|
||||
def fix_version_tuple(version_tuple):
|
||||
# type: (Tuple[AnyStr, AnyStr]) -> Tuple[AnyStr, AnyStr]
|
||||
@@ -302,6 +310,7 @@ def fix_version_tuple(version_tuple):
|
||||
return (op, version)
|
||||
|
||||
|
||||
# TODO: Rename this to something meaningful, deprecate it (See prior function)
|
||||
@lru_cache(maxsize=128)
|
||||
def get_versions(specset, group_by_operator=True):
|
||||
# type: (Union[Set[Specifier], SpecifierSet], bool) -> List[Tuple[STRING_TYPE, STRING_TYPE]]
|
||||
@@ -528,39 +537,69 @@ def contains_pyversion(marker):
|
||||
return _markers_contains_pyversion(marker._markers)
|
||||
|
||||
|
||||
def _split_specifierset_str(specset_str, prefix="=="):
|
||||
# type: (str, str) -> Set[Specifier]
|
||||
"""
|
||||
Take a specifierset string and split it into a list to join for specifier sets
|
||||
|
||||
:param str specset_str: A string containing python versions, often comma separated
|
||||
:param str prefix: A prefix to use when generating the specifier set
|
||||
:return: A list of :class:`Specifier` instances generated with the provided prefix
|
||||
:rtype: Set[Specifier]
|
||||
"""
|
||||
specifiers = set()
|
||||
if "," not in specset_str and " " in specset_str:
|
||||
values = [v.strip() for v in specset_str.split()]
|
||||
else:
|
||||
values = [v.strip() for v in specset_str.split(",")]
|
||||
if prefix == "!=" and any(v in values for v in DEPRECATED_VERSIONS):
|
||||
values = DEPRECATED_VERSIONS[:]
|
||||
for value in sorted(values):
|
||||
specifiers.add(Specifier("{0}{1}".format(prefix, value)))
|
||||
return specifiers
|
||||
|
||||
|
||||
def _get_specifiers_from_markers(marker_item):
|
||||
"""
|
||||
Given a marker item, get specifiers from the version marker
|
||||
|
||||
:param :class:`~packaging.markers.Marker` marker_sequence: A marker describing a version constraint
|
||||
:return: A set of specifiers corresponding to the marker constraint
|
||||
:rtype: Set[Specifier]
|
||||
"""
|
||||
specifiers = set()
|
||||
if isinstance(marker_item, tuple):
|
||||
variable, op, value = marker_item
|
||||
if variable.value != "python_version":
|
||||
return specifiers
|
||||
if op.value == "in":
|
||||
specifiers.update(_split_specifierset_str(value.value, prefix="=="))
|
||||
elif op.value == "not in":
|
||||
specifiers.update(_split_specifierset_str(value.value, prefix="!="))
|
||||
else:
|
||||
specifiers.add(Specifier("{0}{1}".format(op.value, value.value)))
|
||||
elif isinstance(marker_item, list):
|
||||
parts = get_specset(marker_item)
|
||||
if parts:
|
||||
specifiers.update(parts)
|
||||
return specifiers
|
||||
|
||||
|
||||
def get_specset(marker_list):
|
||||
# type: (List) -> Optional[SpecifierSet]
|
||||
specset = set()
|
||||
_last_str = "and"
|
||||
for marker_parts in marker_list:
|
||||
if isinstance(marker_parts, tuple):
|
||||
variable, op, value = marker_parts
|
||||
if variable.value != "python_version":
|
||||
continue
|
||||
if op.value == "in":
|
||||
values = [v.strip() for v in value.value.split(",")]
|
||||
specset.update(Specifier("=={0}".format(v)) for v in values)
|
||||
elif op.value == "not in":
|
||||
values = [v.strip() for v in value.value.split(",")]
|
||||
bad_versions = ["3.0", "3.1", "3.2", "3.3"]
|
||||
if len(values) >= 2 and any(v in values for v in bad_versions):
|
||||
values = bad_versions
|
||||
specset.update(
|
||||
Specifier("!={0}".format(v.strip())) for v in sorted(bad_versions)
|
||||
)
|
||||
else:
|
||||
specset.add(Specifier("{0}{1}".format(op.value, value.value)))
|
||||
elif isinstance(marker_parts, list):
|
||||
parts = get_specset(marker_parts)
|
||||
if parts:
|
||||
specset.update(parts)
|
||||
elif isinstance(marker_parts, str):
|
||||
_last_str = marker_parts
|
||||
if isinstance(marker_parts, str):
|
||||
_last_str = marker_parts # noqa
|
||||
else:
|
||||
specset.update(_get_specifiers_from_markers(marker_parts))
|
||||
specifiers = SpecifierSet()
|
||||
specifiers._specs = frozenset(specset)
|
||||
return specifiers
|
||||
|
||||
|
||||
# TODO: Refactor this (reduce complexity)
|
||||
def parse_marker_dict(marker_dict):
|
||||
op = marker_dict["op"]
|
||||
lhs = marker_dict["lhs"]
|
||||
@@ -670,3 +709,16 @@ def marker_from_specifier(spec):
|
||||
marker_segments.append(format_pyversion(marker_segment))
|
||||
marker_str = " and ".join(marker_segments).replace('"', "'")
|
||||
return Marker(marker_str)
|
||||
|
||||
|
||||
def merge_markers(m1, m2):
|
||||
# type: (Marker, Marker) -> Optional[Marker]
|
||||
if not all((m1, m2)):
|
||||
return next(iter(v for v in (m1, m2) if v), None)
|
||||
m1 = _ensure_marker(m1)
|
||||
m2 = _ensure_marker(m2)
|
||||
_markers = [] # type: List[Marker]
|
||||
for marker in (m1, m2):
|
||||
_markers.append(str(marker))
|
||||
marker_str = " and ".join([normalize_marker_str(m) for m in _markers if m])
|
||||
return _ensure_marker(normalize_marker_str(marker_str))
|
||||
|
||||
+46
-38
@@ -15,7 +15,6 @@ import pip_shims
|
||||
import six
|
||||
import vistir
|
||||
from cached_property import cached_property
|
||||
from first import first
|
||||
from packaging.markers import Marker
|
||||
from packaging.requirements import Requirement as PackagingRequirement
|
||||
from packaging.specifiers import (
|
||||
@@ -793,27 +792,25 @@ class Line(object):
|
||||
|
||||
def get_setup_info(self):
|
||||
# type: () -> SetupInfo
|
||||
setup_info = SetupInfo.from_ireq(self.ireq)
|
||||
if not setup_info.name:
|
||||
setup_info.get_info()
|
||||
setup_info = None
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
setup_info = SetupInfo.from_ireq(self.ireq)
|
||||
if not setup_info.name:
|
||||
setup_info.get_info()
|
||||
return setup_info
|
||||
|
||||
@property
|
||||
def setup_info(self):
|
||||
# type: () -> Optional[SetupInfo]
|
||||
if self._setup_info is None and not self.is_named and not self.is_wheel:
|
||||
if self._setup_info:
|
||||
if not self._setup_info.name:
|
||||
self._setup_info.get_info()
|
||||
else:
|
||||
# make two attempts at this before failing to allow for stale data
|
||||
if not self._setup_info and not self.is_named and not self.is_wheel:
|
||||
# make two attempts at this before failing to allow for stale data
|
||||
try:
|
||||
self.setup_info = self.get_setup_info()
|
||||
except FileNotFoundError:
|
||||
try:
|
||||
self.setup_info = self.get_setup_info()
|
||||
except FileNotFoundError:
|
||||
try:
|
||||
self.setup_info = self.get_setup_info()
|
||||
except FileNotFoundError:
|
||||
raise
|
||||
raise
|
||||
return self._setup_info
|
||||
|
||||
@setup_info.setter
|
||||
@@ -863,12 +860,16 @@ class Line(object):
|
||||
@cached_property
|
||||
def parsed_setup_cfg(self):
|
||||
# type: () -> Dict[Any, Any]
|
||||
if self.is_local and self.path and is_installable_dir(self.path):
|
||||
setup_content = read_source(self.setup_cfg)
|
||||
base_dir = os.path.dirname(os.path.abspath(self.setup_cfg))
|
||||
if self.setup_cfg:
|
||||
return parse_setup_cfg(setup_content, base_dir)
|
||||
return {}
|
||||
if not (
|
||||
self.is_local
|
||||
and self.path
|
||||
and is_installable_dir(self.path)
|
||||
and self.setup_cfg
|
||||
):
|
||||
return {}
|
||||
base_dir = os.path.dirname(os.path.abspath(self.setup_cfg))
|
||||
setup_content = read_source(self.setup_cfg)
|
||||
return parse_setup_cfg(setup_content, base_dir)
|
||||
|
||||
@cached_property
|
||||
def parsed_setup_py(self):
|
||||
@@ -886,7 +887,7 @@ class Line(object):
|
||||
wheel_kwargs = self.wheel_kwargs.copy()
|
||||
wheel_kwargs["src_dir"] = repo.checkout_directory
|
||||
ireq.ensure_has_source_dir(wheel_kwargs["src_dir"])
|
||||
with temp_path():
|
||||
with pip_shims.shims.global_tempdir_manager(), temp_path():
|
||||
sys.path = [repo.checkout_directory, "", ".", get_python_lib(plat_specific=0)]
|
||||
setupinfo = SetupInfo.create(
|
||||
repo.checkout_directory,
|
||||
@@ -1061,7 +1062,7 @@ class Line(object):
|
||||
# type: () -> "Line"
|
||||
if self._name is None:
|
||||
self.parse_name()
|
||||
if not self._name and not self.is_vcs and not self.is_named:
|
||||
if not any([self._name, self.is_vcs, self.is_named]):
|
||||
if self.setup_info and self.setup_info.name:
|
||||
self._name = self.setup_info.name
|
||||
name, extras, url = self.requirement_info
|
||||
@@ -1558,16 +1559,18 @@ class FileRequirement(object):
|
||||
self._parsed_line._setup_info
|
||||
and not self._parsed_line._setup_info.name
|
||||
):
|
||||
self._parsed_line._setup_info.get_info()
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
self._parsed_line._setup_info.get_info()
|
||||
self._setup_info = self.parsed_line._setup_info
|
||||
elif self.parsed_line and (
|
||||
self.parsed_line.ireq and not self.parsed_line.is_wheel
|
||||
):
|
||||
self._setup_info = SetupInfo.from_ireq(self.parsed_line.ireq)
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
self._setup_info = SetupInfo.from_ireq(self.parsed_line.ireq)
|
||||
else:
|
||||
if self.link and not self.link.is_wheel:
|
||||
self._setup_info = Line(self.line_part).setup_info
|
||||
if self._setup_info:
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
self._setup_info.get_info()
|
||||
return self._setup_info
|
||||
|
||||
@@ -1954,20 +1957,23 @@ class VCSRequirement(FileRequirement):
|
||||
def setup_info(self):
|
||||
if self._parsed_line and self._parsed_line.setup_info:
|
||||
if not self._parsed_line.setup_info.name:
|
||||
self._parsed_line._setup_info.get_info()
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
self._parsed_line._setup_info.get_info()
|
||||
return self._parsed_line.setup_info
|
||||
if self._repo:
|
||||
from .setup_info import SetupInfo
|
||||
|
||||
self._setup_info = SetupInfo.from_ireq(
|
||||
Line(self._repo.checkout_directory).ireq
|
||||
)
|
||||
self._setup_info.get_info()
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
self._setup_info = SetupInfo.from_ireq(
|
||||
Line(self._repo.checkout_directory).ireq
|
||||
)
|
||||
self._setup_info.get_info()
|
||||
return self._setup_info
|
||||
ireq = self.parsed_line.ireq
|
||||
from .setup_info import SetupInfo
|
||||
|
||||
self._setup_info = SetupInfo.from_ireq(ireq)
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
self._setup_info = SetupInfo.from_ireq(ireq)
|
||||
return self._setup_info
|
||||
|
||||
@setup_info.setter
|
||||
@@ -2271,7 +2277,7 @@ class VCSRequirement(FileRequirement):
|
||||
alt_type = "" # type: Optional[STRING_TYPE]
|
||||
vcs_value = "" # type: STRING_TYPE
|
||||
if src_keys:
|
||||
chosen_key = first(src_keys)
|
||||
chosen_key = next(iter(src_keys))
|
||||
vcs_type = pipfile.pop("vcs")
|
||||
if chosen_key in pipfile:
|
||||
vcs_value = pipfile[chosen_key]
|
||||
@@ -2561,7 +2567,8 @@ class Requirement(object):
|
||||
if self.req is not None and (
|
||||
not isinstance(self.req, NamedRequirement) and self.req.is_local
|
||||
):
|
||||
setup_info = self.run_requires()
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
setup_info = self.run_requires()
|
||||
build_backend = setup_info.get("build_backend")
|
||||
return build_backend
|
||||
return "setuptools.build_meta"
|
||||
@@ -2673,7 +2680,7 @@ class Requirement(object):
|
||||
if hasattr(pipfile, "keys"):
|
||||
_pipfile = dict(pipfile).copy()
|
||||
_pipfile["version"] = get_version(pipfile)
|
||||
vcs = first([vcs for vcs in VCS_LIST if vcs in _pipfile])
|
||||
vcs = next(iter([vcs for vcs in VCS_LIST if vcs in _pipfile]), None)
|
||||
if vcs:
|
||||
_pipfile["vcs"] = vcs
|
||||
r = VCSRequirement.from_pipfile(name, pipfile)
|
||||
@@ -2955,10 +2962,11 @@ class Requirement(object):
|
||||
from .dependencies import get_finder
|
||||
|
||||
finder = get_finder(sources=sources)
|
||||
info = SetupInfo.from_requirement(self, finder=finder)
|
||||
if info is None:
|
||||
return {}
|
||||
info_dict = info.get_info()
|
||||
with pip_shims.shims.global_tempdir_manager():
|
||||
info = SetupInfo.from_requirement(self, finder=finder)
|
||||
if info is None:
|
||||
return {}
|
||||
info_dict = info.get_info()
|
||||
if self.req and not self.req.setup_info:
|
||||
self.req._setup_info = info
|
||||
if self.req._has_hashed_name and info_dict.get("name"):
|
||||
|
||||
+66
-16
@@ -5,12 +5,15 @@ import ast
|
||||
import atexit
|
||||
import contextlib
|
||||
import importlib
|
||||
import io
|
||||
import operator
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
import attr
|
||||
import chardet
|
||||
import packaging.specifiers
|
||||
import packaging.utils
|
||||
import packaging.version
|
||||
@@ -46,6 +49,10 @@ except ImportError:
|
||||
import distutils
|
||||
from distutils.core import Distribution
|
||||
|
||||
try:
|
||||
from contextlib import ExitStack
|
||||
except ImportError:
|
||||
from contextlib2 import ExitStack
|
||||
|
||||
try:
|
||||
from os import scandir
|
||||
@@ -294,7 +301,11 @@ def parse_setup_cfg(setup_cfg_contents, base_dir):
|
||||
},
|
||||
}
|
||||
parser = configparser.ConfigParser(default_opts)
|
||||
parser.read_string(setup_cfg_contents)
|
||||
if six.PY2:
|
||||
buff = io.BytesIO(setup_cfg_contents)
|
||||
parser.readfp(buff)
|
||||
else:
|
||||
parser.read_string(setup_cfg_contents)
|
||||
results = {}
|
||||
package_dir = get_package_dir_from_setupcfg(parser, base_dir=base_dir)
|
||||
name, version = get_name_and_version_from_setupcfg(parser, package_dir)
|
||||
@@ -629,6 +640,20 @@ def get_metadata_from_dist(dist):
|
||||
|
||||
|
||||
class Analyzer(ast.NodeVisitor):
|
||||
OP_MAP = {
|
||||
ast.Add: operator.add,
|
||||
ast.Sub: operator.sub,
|
||||
ast.Mult: operator.mul,
|
||||
ast.Div: operator.floordiv,
|
||||
ast.Mod: operator.mod,
|
||||
ast.Pow: operator.pow,
|
||||
ast.LShift: operator.lshift,
|
||||
ast.RShift: operator.rshift,
|
||||
ast.BitAnd: operator.and_,
|
||||
ast.BitOr: operator.or_,
|
||||
ast.BitXor: operator.xor
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.name_types = []
|
||||
self.function_map = {} # type: Dict[Any, Any]
|
||||
@@ -654,8 +679,10 @@ class Analyzer(ast.NodeVisitor):
|
||||
def visit_BinOp(self, node):
|
||||
left = ast_unparse(node.left, initial_mapping=True)
|
||||
right = ast_unparse(node.right, initial_mapping=True)
|
||||
op = ast_unparse(node.op, initial_mapping=True)
|
||||
node.left = left
|
||||
node.right = right
|
||||
node.op = op
|
||||
self.binOps.append(node)
|
||||
|
||||
def unmap_binops(self):
|
||||
@@ -691,25 +718,35 @@ def ast_unparse(item, initial_mapping=False, analyzer=None, recurse=True): # no
|
||||
elif isinstance(item, ast.BinOp):
|
||||
if analyzer and item in analyzer.binOps_map:
|
||||
unparsed = analyzer.binOps_map[item]
|
||||
elif isinstance(item.op, ast.Add):
|
||||
else:
|
||||
right_item = unparse(item.right)
|
||||
left_item = unparse(item.left)
|
||||
if type(item.op) in Analyzer.OP_MAP:
|
||||
item.op = Analyzer.OP_MAP[type(item.op)]
|
||||
if not initial_mapping:
|
||||
right_item = unparse(item.right)
|
||||
left_item = unparse(item.left)
|
||||
if not all(
|
||||
isinstance(side, (six.string_types, int, float, list, tuple))
|
||||
for side in (left_item, right_item)
|
||||
):
|
||||
item.left = left_item
|
||||
item.right = right_item
|
||||
unparsed = item
|
||||
if type(item.op) in Analyzer.OP_MAP:
|
||||
item = Analyzer.OP_MAP[type(item.op)](left_item, right_item)
|
||||
else:
|
||||
item.left = left_item
|
||||
item.right = right_item
|
||||
item.op = unparse(item.op)
|
||||
try:
|
||||
unparsed = item.op(left_item, right_item)
|
||||
except Exception:
|
||||
unparsed = item
|
||||
else:
|
||||
unparsed = left_item + right_item
|
||||
if type(item.op) in Analyzer.OP_MAP:
|
||||
item.op = Analyzer.OP_MAP[type(item.op)]
|
||||
try:
|
||||
unparsed = item.op(left_item, right_item)
|
||||
except Exception:
|
||||
unparsed = item
|
||||
else:
|
||||
unparsed = item
|
||||
elif isinstance(item.op, ast.Sub):
|
||||
unparsed = unparse(item.left) - unparse(item.right)
|
||||
else:
|
||||
unparsed = item
|
||||
elif isinstance(item, ast.Name):
|
||||
if not initial_mapping:
|
||||
unparsed = item.id
|
||||
@@ -747,10 +784,13 @@ def ast_unparse(item, initial_mapping=False, analyzer=None, recurse=True): # no
|
||||
unparsed = name if not unparsed else unparsed
|
||||
elif isinstance(item, ast.Call):
|
||||
unparsed = {}
|
||||
if isinstance(item.func, ast.Name):
|
||||
func_name = unparse(item.func)
|
||||
elif isinstance(item.func, ast.Attribute):
|
||||
if isinstance(item.func, (ast.Name, ast.Attribute)):
|
||||
func_name = unparse(item.func)
|
||||
else:
|
||||
try:
|
||||
func_name = unparse(item.func)
|
||||
except Exception:
|
||||
func_name = None
|
||||
if func_name:
|
||||
unparsed[func_name] = {}
|
||||
for keyword in item.keywords:
|
||||
@@ -809,7 +849,15 @@ def ast_parse_attribute_from_file(path, attribute):
|
||||
|
||||
def ast_parse_file(path):
|
||||
# type: (S) -> Analyzer
|
||||
tree = ast.parse(read_source(path))
|
||||
try:
|
||||
tree = ast.parse(read_source(path))
|
||||
except SyntaxError:
|
||||
# The source may be encoded strangely, e.g. azure-storage
|
||||
# which has a setup.py encoded with utf-8-sig
|
||||
with open(path, "rb") as fh:
|
||||
contents = fh.read()
|
||||
encoding = chardet.detect(contents)["encoding"]
|
||||
tree = ast.parse(contents.decode(encoding))
|
||||
ast_analyzer = Analyzer()
|
||||
ast_analyzer.visit(tree)
|
||||
return ast_analyzer
|
||||
@@ -1111,6 +1159,8 @@ class SetupInfo(object):
|
||||
try:
|
||||
parsed = setuptools_parse_setup_cfg(self.setup_cfg.as_posix())
|
||||
except Exception:
|
||||
if six.PY2:
|
||||
contents = self.setup_cfg.read_bytes()
|
||||
parsed = parse_setup_cfg(contents, base_dir)
|
||||
if not parsed:
|
||||
return {}
|
||||
|
||||
+11
-6
@@ -12,7 +12,6 @@ from itertools import chain, groupby
|
||||
import six
|
||||
import tomlkit
|
||||
from attr import validators
|
||||
from first import first
|
||||
from packaging.markers import InvalidMarker, Marker, Op, Value, Variable
|
||||
from packaging.specifiers import InvalidSpecifier, Specifier, SpecifierSet
|
||||
from packaging.version import parse as parse_version
|
||||
@@ -103,6 +102,11 @@ def filter_none(k, v):
|
||||
return False
|
||||
|
||||
|
||||
def filter_dict(dict_):
|
||||
# type: (Dict[AnyStr, Any]) -> Dict[AnyStr, Any]
|
||||
return {k: v for k, v in dict_.items() if filter_none(k, v)}
|
||||
|
||||
|
||||
def optional_instance_of(cls):
|
||||
# type: (Any) -> _ValidatorType[Optional[_T]]
|
||||
return validators.optional(validators.instance_of(cls))
|
||||
@@ -548,8 +552,9 @@ def split_vcs_method_from_uri(uri):
|
||||
# type: (AnyStr) -> Tuple[Optional[STRING_TYPE], STRING_TYPE]
|
||||
"""Split a vcs+uri formatted uri into (vcs, uri)"""
|
||||
vcs_start = "{0}+"
|
||||
vcs = None # type: Optional[STRING_TYPE]
|
||||
vcs = first([vcs for vcs in VCS_LIST if uri.startswith(vcs_start.format(vcs))])
|
||||
vcs = next(
|
||||
iter([vcs for vcs in VCS_LIST if uri.startswith(vcs_start.format(vcs))]), None
|
||||
)
|
||||
if vcs:
|
||||
vcs, uri = uri.split("+", 1)
|
||||
return vcs, uri
|
||||
@@ -718,7 +723,7 @@ def get_pinned_version(ireq):
|
||||
except AttributeError:
|
||||
raise TypeError("Expected InstallRequirement, not {}".format(type(ireq).__name__))
|
||||
|
||||
if ireq.editable:
|
||||
if getattr(ireq, "editable", False):
|
||||
raise ValueError("InstallRequirement is editable")
|
||||
if not specifier:
|
||||
raise ValueError("InstallRequirement has no version specification")
|
||||
@@ -766,7 +771,7 @@ def as_tuple(ireq):
|
||||
raise TypeError("Expected a pinned InstallRequirement, got {}".format(ireq))
|
||||
|
||||
name = key_from_req(ireq.req)
|
||||
version = first(ireq.specifier._specs)._spec[1]
|
||||
version = next(iter(ireq.specifier._specs))._spec[1]
|
||||
extras = tuple(sorted(ireq.extras))
|
||||
return name, version, extras
|
||||
|
||||
@@ -906,7 +911,7 @@ def version_from_ireq(ireq):
|
||||
:rtype: str
|
||||
"""
|
||||
|
||||
return first(ireq.specifier._specs).version
|
||||
return next(iter(ireq.specifier._specs)).version
|
||||
|
||||
|
||||
def _get_requires_python(candidate):
|
||||
|
||||
Vendored
+5
@@ -40,6 +40,8 @@ toml==0.10.0
|
||||
cached-property==1.5.1
|
||||
vistir==0.4.3
|
||||
pip-shims==0.4.0
|
||||
contextlib2==0.6.0.post1
|
||||
funcsigs==1.0.2
|
||||
enum34==1.1.6
|
||||
# yaspin==0.15.0
|
||||
yaspin==0.14.3
|
||||
@@ -47,5 +49,8 @@ cerberus==1.3.2
|
||||
resolvelib==0.2.2
|
||||
backports.functools_lru_cache==1.5
|
||||
pep517==0.8.1
|
||||
zipp==0.6.0
|
||||
importlib_metadata==1.3.0
|
||||
more-itertools==5.0.0
|
||||
git+https://github.com/sarugaku/passa.git@master#egg=passa
|
||||
orderedmultidict==1.0.1
|
||||
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
Copyright Jason R. Coombs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
Vendored
+220
@@ -0,0 +1,220 @@
|
||||
# coding: utf-8
|
||||
|
||||
from __future__ import division
|
||||
|
||||
import io
|
||||
import sys
|
||||
import posixpath
|
||||
import zipfile
|
||||
import functools
|
||||
import itertools
|
||||
|
||||
import more_itertools
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
def _parents(path):
|
||||
"""
|
||||
Given a path with elements separated by
|
||||
posixpath.sep, generate all parents of that path.
|
||||
|
||||
>>> list(_parents('b/d'))
|
||||
['b']
|
||||
>>> list(_parents('/b/d/'))
|
||||
['/b']
|
||||
>>> list(_parents('b/d/f/'))
|
||||
['b/d', 'b']
|
||||
>>> list(_parents('b'))
|
||||
[]
|
||||
>>> list(_parents(''))
|
||||
[]
|
||||
"""
|
||||
return itertools.islice(_ancestry(path), 1, None)
|
||||
|
||||
|
||||
def _ancestry(path):
|
||||
"""
|
||||
Given a path with elements separated by
|
||||
posixpath.sep, generate all elements of that path
|
||||
|
||||
>>> list(_ancestry('b/d'))
|
||||
['b/d', 'b']
|
||||
>>> list(_ancestry('/b/d/'))
|
||||
['/b/d', '/b']
|
||||
>>> list(_ancestry('b/d/f/'))
|
||||
['b/d/f', 'b/d', 'b']
|
||||
>>> list(_ancestry('b'))
|
||||
['b']
|
||||
>>> list(_ancestry(''))
|
||||
[]
|
||||
"""
|
||||
path = path.rstrip(posixpath.sep)
|
||||
while path and path != posixpath.sep:
|
||||
yield path
|
||||
path, tail = posixpath.split(path)
|
||||
|
||||
|
||||
class Path:
|
||||
"""
|
||||
A pathlib-compatible interface for zip files.
|
||||
|
||||
Consider a zip file with this structure::
|
||||
|
||||
.
|
||||
├── a.txt
|
||||
└── b
|
||||
├── c.txt
|
||||
└── d
|
||||
└── e.txt
|
||||
|
||||
>>> data = io.BytesIO()
|
||||
>>> zf = zipfile.ZipFile(data, 'w')
|
||||
>>> zf.writestr('a.txt', 'content of a')
|
||||
>>> zf.writestr('b/c.txt', 'content of c')
|
||||
>>> zf.writestr('b/d/e.txt', 'content of e')
|
||||
>>> zf.filename = 'abcde.zip'
|
||||
|
||||
Path accepts the zipfile object itself or a filename
|
||||
|
||||
>>> root = Path(zf)
|
||||
|
||||
From there, several path operations are available.
|
||||
|
||||
Directory iteration (including the zip file itself):
|
||||
|
||||
>>> a, b = root.iterdir()
|
||||
>>> a
|
||||
Path('abcde.zip', 'a.txt')
|
||||
>>> b
|
||||
Path('abcde.zip', 'b/')
|
||||
|
||||
name property:
|
||||
|
||||
>>> b.name
|
||||
'b'
|
||||
|
||||
join with divide operator:
|
||||
|
||||
>>> c = b / 'c.txt'
|
||||
>>> c
|
||||
Path('abcde.zip', 'b/c.txt')
|
||||
>>> c.name
|
||||
'c.txt'
|
||||
|
||||
Read text:
|
||||
|
||||
>>> c.read_text()
|
||||
'content of c'
|
||||
|
||||
existence:
|
||||
|
||||
>>> c.exists()
|
||||
True
|
||||
>>> (b / 'missing.txt').exists()
|
||||
False
|
||||
|
||||
Coercion to string:
|
||||
|
||||
>>> str(c)
|
||||
'abcde.zip/b/c.txt'
|
||||
"""
|
||||
|
||||
__repr = "{self.__class__.__name__}({self.root.filename!r}, {self.at!r})"
|
||||
|
||||
def __init__(self, root, at=""):
|
||||
self.root = (
|
||||
root
|
||||
if isinstance(root, zipfile.ZipFile)
|
||||
else zipfile.ZipFile(self._pathlib_compat(root))
|
||||
)
|
||||
self.at = at
|
||||
|
||||
@staticmethod
|
||||
def _pathlib_compat(path):
|
||||
"""
|
||||
For path-like objects, convert to a filename for compatibility
|
||||
on Python 3.6.1 and earlier.
|
||||
"""
|
||||
try:
|
||||
return path.__fspath__()
|
||||
except AttributeError:
|
||||
return str(path)
|
||||
|
||||
@property
|
||||
def open(self):
|
||||
return functools.partial(self.root.open, self.at)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return posixpath.basename(self.at.rstrip("/"))
|
||||
|
||||
def read_text(self, *args, **kwargs):
|
||||
with self.open() as strm:
|
||||
return io.TextIOWrapper(strm, *args, **kwargs).read()
|
||||
|
||||
def read_bytes(self):
|
||||
with self.open() as strm:
|
||||
return strm.read()
|
||||
|
||||
def _is_child(self, path):
|
||||
return posixpath.dirname(path.at.rstrip("/")) == self.at.rstrip("/")
|
||||
|
||||
def _next(self, at):
|
||||
return Path(self.root, at)
|
||||
|
||||
def is_dir(self):
|
||||
return not self.at or self.at.endswith("/")
|
||||
|
||||
def is_file(self):
|
||||
return not self.is_dir()
|
||||
|
||||
def exists(self):
|
||||
return self.at in self._names()
|
||||
|
||||
def iterdir(self):
|
||||
if not self.is_dir():
|
||||
raise ValueError("Can't listdir a file")
|
||||
subs = map(self._next, self._names())
|
||||
return filter(self._is_child, subs)
|
||||
|
||||
def __str__(self):
|
||||
return posixpath.join(self.root.filename, self.at)
|
||||
|
||||
def __repr__(self):
|
||||
return self.__repr.format(self=self)
|
||||
|
||||
def joinpath(self, add):
|
||||
add = self._pathlib_compat(add)
|
||||
next = posixpath.join(self.at, add)
|
||||
next_dir = posixpath.join(self.at, add, "")
|
||||
names = self._names()
|
||||
return self._next(next_dir if next not in names and next_dir in names else next)
|
||||
|
||||
__truediv__ = joinpath
|
||||
|
||||
@staticmethod
|
||||
def _implied_dirs(names):
|
||||
return more_itertools.unique_everseen(
|
||||
parent + "/"
|
||||
for name in names
|
||||
for parent in _parents(name)
|
||||
if parent + "/" not in names
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _add_implied_dirs(cls, names):
|
||||
return names + list(cls._implied_dirs(names))
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
parent_at = posixpath.dirname(self.at.rstrip('/'))
|
||||
if parent_at:
|
||||
parent_at += '/'
|
||||
return self._next(parent_at)
|
||||
|
||||
def _names(self):
|
||||
return self._add_implied_dirs(self.root.namelist())
|
||||
|
||||
if sys.version_info < (3,):
|
||||
__div__ = __truediv__
|
||||
@@ -267,24 +267,6 @@ index c24158f4..37c3197f 100644
|
||||
)
|
||||
|
||||
if not self.ignore_dependencies:
|
||||
@@ -423,6 +443,17 @@ class Resolver(object):
|
||||
for subreq in dist.requires(available_requested):
|
||||
add_req(subreq, extras_requested=available_requested)
|
||||
|
||||
+ # Hack for deep-resolving extras.
|
||||
+ for available in available_requested:
|
||||
+ if hasattr(dist, '_DistInfoDistribution__dep_map'):
|
||||
+ for req in dist._DistInfoDistribution__dep_map[available]:
|
||||
+ req = self._make_install_req(
|
||||
+ req,
|
||||
+ req_to_install
|
||||
+ )
|
||||
+
|
||||
+ more_reqs.append(req)
|
||||
+
|
||||
if not req_to_install.editable and not req_to_install.satisfied_by:
|
||||
# XXX: --no-install leads this to report 'Successfully
|
||||
# downloaded' for only non-editable reqs, even though we took
|
||||
diff --git a/pipenv/patched/pip/_internal/models/candidate.py b/pipenv/patched/pip/_internal/models/candidate.py
|
||||
index 4d49604d..cdfe65aa 100644
|
||||
--- a/pipenv/patched/pip/_internal/models/candidate.py
|
||||
@@ -522,3 +504,90 @@ index 77d40be6..8a32cf2d 100644
|
||||
- return path
|
||||
+ return path
|
||||
\ No newline at end of file
|
||||
diff --git a/pipenv/patched/notpip/_internal/commands/__init__.py b/pipenv/patched/notpip/_internal/commands/__init__.py
|
||||
index abcafa55..ca155a94 100644
|
||||
--- a/pipenv/patched/notpip/_internal/commands/__init__.py
|
||||
+++ b/pipenv/patched/notpip/_internal/commands/__init__.py
|
||||
@@ -21,7 +21,7 @@ CommandInfo = namedtuple('CommandInfo', 'module_path, class_name, summary')
|
||||
|
||||
# The ordering matters for help display.
|
||||
# Also, even though the module path starts with the same
|
||||
-# "pip._internal.commands" prefix in each case, we include the full path
|
||||
+# "pipenv.patched.notpip._internal.commands" prefix in each case, we include the full path
|
||||
# because it makes testing easier (specifically when modifying commands_dict
|
||||
# in test setup / teardown by adding info for a FakeCommand class defined
|
||||
# in a test-related module).
|
||||
@@ -29,59 +29,59 @@ CommandInfo = namedtuple('CommandInfo', 'module_path, class_name, summary')
|
||||
# so that the ordering won't be lost when using Python 2.7.
|
||||
commands_dict = OrderedDict([
|
||||
('install', CommandInfo(
|
||||
- 'pip._internal.commands.install', 'InstallCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.install', 'InstallCommand',
|
||||
'Install packages.',
|
||||
)),
|
||||
('download', CommandInfo(
|
||||
- 'pip._internal.commands.download', 'DownloadCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.download', 'DownloadCommand',
|
||||
'Download packages.',
|
||||
)),
|
||||
('uninstall', CommandInfo(
|
||||
- 'pip._internal.commands.uninstall', 'UninstallCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.uninstall', 'UninstallCommand',
|
||||
'Uninstall packages.',
|
||||
)),
|
||||
('freeze', CommandInfo(
|
||||
- 'pip._internal.commands.freeze', 'FreezeCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.freeze', 'FreezeCommand',
|
||||
'Output installed packages in requirements format.',
|
||||
)),
|
||||
('list', CommandInfo(
|
||||
- 'pip._internal.commands.list', 'ListCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.list', 'ListCommand',
|
||||
'List installed packages.',
|
||||
)),
|
||||
('show', CommandInfo(
|
||||
- 'pip._internal.commands.show', 'ShowCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.show', 'ShowCommand',
|
||||
'Show information about installed packages.',
|
||||
)),
|
||||
('check', CommandInfo(
|
||||
- 'pip._internal.commands.check', 'CheckCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.check', 'CheckCommand',
|
||||
'Verify installed packages have compatible dependencies.',
|
||||
)),
|
||||
('config', CommandInfo(
|
||||
- 'pip._internal.commands.configuration', 'ConfigurationCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.configuration', 'ConfigurationCommand',
|
||||
'Manage local and global configuration.',
|
||||
)),
|
||||
('search', CommandInfo(
|
||||
- 'pip._internal.commands.search', 'SearchCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.search', 'SearchCommand',
|
||||
'Search PyPI for packages.',
|
||||
)),
|
||||
('wheel', CommandInfo(
|
||||
- 'pip._internal.commands.wheel', 'WheelCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.wheel', 'WheelCommand',
|
||||
'Build wheels from your requirements.',
|
||||
)),
|
||||
('hash', CommandInfo(
|
||||
- 'pip._internal.commands.hash', 'HashCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.hash', 'HashCommand',
|
||||
'Compute hashes of package archives.',
|
||||
)),
|
||||
('completion', CommandInfo(
|
||||
- 'pip._internal.commands.completion', 'CompletionCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.completion', 'CompletionCommand',
|
||||
'A helper command used for command completion.',
|
||||
)),
|
||||
('debug', CommandInfo(
|
||||
- 'pip._internal.commands.debug', 'DebugCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.debug', 'DebugCommand',
|
||||
'Show information useful for debugging.',
|
||||
)),
|
||||
('help', CommandInfo(
|
||||
- 'pip._internal.commands.help', 'HelpCommand',
|
||||
+ 'pipenv.patched.notpip._internal.commands.help', 'HelpCommand',
|
||||
'Show help for commands.',
|
||||
)),
|
||||
]) # type: OrderedDict[str, CommandInfo]
|
||||
|
||||
@@ -155,7 +155,7 @@ index f389784..c1bcf9d 100644
|
||||
else:
|
||||
return self.repository.find_best_match(ireq, prereleases)
|
||||
diff --git a/pipenv/patched/piptools/repositories/pypi.py b/pipenv/patched/piptools/repositories/pypi.py
|
||||
index acbd680..c9a23ad 100644
|
||||
index acbd680..4bd3e22 100644
|
||||
--- a/pipenv/patched/piptools/repositories/pypi.py
|
||||
+++ b/pipenv/patched/piptools/repositories/pypi.py
|
||||
@@ -2,21 +2,29 @@
|
||||
@@ -400,7 +400,7 @@ index acbd680..c9a23ad 100644
|
||||
|
||||
preparer_kwargs = {
|
||||
"build_dir": self.build_dir,
|
||||
@@ -186,9 +311,11 @@ class PyPIRepository(BaseRepository):
|
||||
@@ -186,21 +311,24 @@ class PyPIRepository(BaseRepository):
|
||||
"upgrade_strategy": "to-satisfy-only",
|
||||
"force_reinstall": False,
|
||||
"ignore_dependencies": False,
|
||||
@@ -413,7 +413,21 @@ index acbd680..c9a23ad 100644
|
||||
}
|
||||
make_install_req_kwargs = {"isolated": False, "wheel_cache": wheel_cache}
|
||||
|
||||
@@ -208,6 +335,7 @@ class PyPIRepository(BaseRepository):
|
||||
if PIP_VERSION < (19, 3):
|
||||
resolver_kwargs.update(**make_install_req_kwargs)
|
||||
else:
|
||||
- from pip._internal.req.constructors import install_req_from_req_string
|
||||
+ from pipenv.vendor.pip_shims.shims import install_req_from_req_string
|
||||
|
||||
make_install_req = partial(
|
||||
install_req_from_req_string, **make_install_req_kwargs
|
||||
)
|
||||
resolver_kwargs["make_install_req"] = make_install_req
|
||||
+ del resolver_kwargs["use_pep517"]
|
||||
|
||||
if PIP_VERSION >= (20,):
|
||||
preparer_kwargs["session"] = self.session
|
||||
@@ -208,6 +336,7 @@ class PyPIRepository(BaseRepository):
|
||||
|
||||
resolver = None
|
||||
preparer = None
|
||||
@@ -421,7 +435,7 @@ index acbd680..c9a23ad 100644
|
||||
with RequirementTracker() as req_tracker:
|
||||
# Pip 18 uses a requirement tracker to prevent fork bombs
|
||||
if req_tracker:
|
||||
@@ -216,7 +344,6 @@ class PyPIRepository(BaseRepository):
|
||||
@@ -216,7 +345,6 @@ class PyPIRepository(BaseRepository):
|
||||
resolver_kwargs["preparer"] = preparer
|
||||
reqset = RequirementSet()
|
||||
ireq.is_direct = True
|
||||
@@ -429,7 +443,7 @@ index acbd680..c9a23ad 100644
|
||||
|
||||
resolver = PipResolver(**resolver_kwargs)
|
||||
require_hashes = False
|
||||
@@ -225,12 +352,16 @@ class PyPIRepository(BaseRepository):
|
||||
@@ -225,12 +353,16 @@ class PyPIRepository(BaseRepository):
|
||||
results = resolver._resolve_one(reqset, ireq)
|
||||
else:
|
||||
results = resolver._resolve_one(reqset, ireq, require_hashes)
|
||||
@@ -441,14 +455,15 @@ index acbd680..c9a23ad 100644
|
||||
- reqset.cleanup_files()
|
||||
+ results = set(results) if results else set()
|
||||
|
||||
return set(results)
|
||||
- return set(results)
|
||||
+ return results, ireq
|
||||
|
||||
- def get_dependencies(self, ireq):
|
||||
+ def get_legacy_dependencies(self, ireq):
|
||||
"""
|
||||
Given a pinned, URL, or editable InstallRequirement, returns a set of
|
||||
dependencies (also InstallRequirements, but not necessarily pinned).
|
||||
@@ -265,9 +396,8 @@ class PyPIRepository(BaseRepository):
|
||||
@@ -265,9 +397,8 @@ class PyPIRepository(BaseRepository):
|
||||
wheel_cache = WheelCache(CACHE_DIR, self.options.format_control)
|
||||
prev_tracker = os.environ.get("PIP_REQ_TRACKER")
|
||||
try:
|
||||
@@ -460,7 +475,7 @@ index acbd680..c9a23ad 100644
|
||||
finally:
|
||||
if "PIP_REQ_TRACKER" in os.environ:
|
||||
if prev_tracker:
|
||||
@@ -313,12 +443,10 @@ class PyPIRepository(BaseRepository):
|
||||
@@ -313,12 +444,10 @@ class PyPIRepository(BaseRepository):
|
||||
# We need to get all of the candidates that match our current version
|
||||
# pin, these will represent all of the files that could possibly
|
||||
# satisfy this constraint.
|
||||
@@ -476,7 +491,7 @@ index acbd680..c9a23ad 100644
|
||||
|
||||
log.debug(" {}".format(ireq.name))
|
||||
|
||||
@@ -328,30 +456,11 @@ class PyPIRepository(BaseRepository):
|
||||
@@ -328,30 +457,11 @@ class PyPIRepository(BaseRepository):
|
||||
return candidate.link
|
||||
|
||||
return {
|
||||
@@ -571,7 +586,7 @@ index fc53f18..c056665 100644
|
||||
]
|
||||
return self.dependency_cache.reverse_dependencies(non_editable)
|
||||
diff --git a/pipenv/patched/piptools/utils.py b/pipenv/patched/piptools/utils.py
|
||||
index 8727f1e..1f4c10a 100644
|
||||
index 8727f1e..c9f53f7 100644
|
||||
--- a/pipenv/patched/piptools/utils.py
|
||||
+++ b/pipenv/patched/piptools/utils.py
|
||||
@@ -1,6 +1,7 @@
|
||||
@@ -648,8 +663,8 @@ index 8727f1e..1f4c10a 100644
|
||||
+ if getattr(c, "requires_python", None):
|
||||
+ # Old specifications had people setting this to single digits
|
||||
+ # which is effectively the same as '>=digit,<digit+1'
|
||||
+ if c.requires_python.isdigit():
|
||||
+ c.requires_python = '>={0},<{1}'.format(c.requires_python, int(c.requires_python) + 1)
|
||||
+ if len(c.requires_python) == 1 and c.requires_python in ("2", "3"):
|
||||
+ c.requires_python = '>={0},<{1!s}'.format(c.requires_python, int(c.requires_python) + 1)
|
||||
+ try:
|
||||
+ specifierset = SpecifierSet(c.requires_python)
|
||||
+ except InvalidSpecifier:
|
||||
|
||||
Reference in New Issue
Block a user