Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +6 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__init__.py +29 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/__init__.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/_cmd.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/adapter.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/cache.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/controller.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/filewrapper.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/heuristics.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/serialize.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/wrapper.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/_cmd.py +70 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/adapter.py +168 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/cache.py +75 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__init__.py +8 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__pycache__/__init__.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__pycache__/file_cache.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__pycache__/redis_cache.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/file_cache.py +145 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/redis_cache.py +48 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/controller.py +511 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/filewrapper.py +119 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/heuristics.py +157 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/py.typed +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/serialize.py +146 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/wrapper.py +43 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__init__.py +33 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/__init__.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/compat.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/database.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/index.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/locators.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/manifest.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/markers.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/metadata.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/resources.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/scripts.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/util.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/version.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/wheel.cpython-310.pyc +0 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/compat.py +1137 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/database.py +1329 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/index.py +508 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/locators.py +1295 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/manifest.py +384 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/markers.py +162 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/metadata.py +1031 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/resources.py +358 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/scripts.py +447 -0
- ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/t32.exe +0 -0
.gitattributes
CHANGED
|
@@ -105,3 +105,9 @@ ACE_plus/flashenv/lib/python3.10/site-packages/setuptools/gui-arm64.exe filter=l
|
|
| 105 |
ACE_plus/flashenv/lib/python3.10/site-packages/setuptools/_vendor/more_itertools/__pycache__/more.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 106 |
ACE_plus/flashenv/lib/python3.10/site-packages/setuptools/_vendor/pyparsing/__pycache__/core.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 107 |
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/__pycache__/typing_extensions.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
ACE_plus/flashenv/lib/python3.10/site-packages/setuptools/_vendor/more_itertools/__pycache__/more.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 106 |
ACE_plus/flashenv/lib/python3.10/site-packages/setuptools/_vendor/pyparsing/__pycache__/core.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 107 |
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/__pycache__/typing_extensions.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 108 |
+
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/uts46data.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 109 |
+
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/idna/__pycache__/idnadata.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 110 |
+
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/w64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
| 111 |
+
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/t64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
| 112 |
+
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/t64.exe filter=lfs diff=lfs merge=lfs -text
|
| 113 |
+
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/w64.exe filter=lfs diff=lfs merge=lfs -text
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
|
| 5 |
+
"""CacheControl import Interface.
|
| 6 |
+
|
| 7 |
+
Make it easy to import from cachecontrol without long namespaces.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
__author__ = "Eric Larson"
|
| 11 |
+
__email__ = "eric@ionrock.org"
|
| 12 |
+
__version__ = "0.14.2"
|
| 13 |
+
|
| 14 |
+
from pip._vendor.cachecontrol.adapter import CacheControlAdapter
|
| 15 |
+
from pip._vendor.cachecontrol.controller import CacheController
|
| 16 |
+
from pip._vendor.cachecontrol.wrapper import CacheControl
|
| 17 |
+
|
| 18 |
+
__all__ = [
|
| 19 |
+
"__author__",
|
| 20 |
+
"__email__",
|
| 21 |
+
"__version__",
|
| 22 |
+
"CacheControlAdapter",
|
| 23 |
+
"CacheController",
|
| 24 |
+
"CacheControl",
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
import logging
|
| 28 |
+
|
| 29 |
+
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (773 Bytes). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/_cmd.cpython-310.pyc
ADDED
|
Binary file (1.84 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/adapter.cpython-310.pyc
ADDED
|
Binary file (4.53 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/cache.cpython-310.pyc
ADDED
|
Binary file (3.29 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/controller.cpython-310.pyc
ADDED
|
Binary file (10.3 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/filewrapper.cpython-310.pyc
ADDED
|
Binary file (3.23 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/heuristics.cpython-310.pyc
ADDED
|
Binary file (5.41 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/serialize.cpython-310.pyc
ADDED
|
Binary file (3.36 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/__pycache__/wrapper.cpython-310.pyc
ADDED
|
Binary file (1.48 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/_cmd.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import logging
|
| 7 |
+
from argparse import ArgumentParser
|
| 8 |
+
from typing import TYPE_CHECKING
|
| 9 |
+
|
| 10 |
+
from pip._vendor import requests
|
| 11 |
+
|
| 12 |
+
from pip._vendor.cachecontrol.adapter import CacheControlAdapter
|
| 13 |
+
from pip._vendor.cachecontrol.cache import DictCache
|
| 14 |
+
from pip._vendor.cachecontrol.controller import logger
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from argparse import Namespace
|
| 18 |
+
|
| 19 |
+
from pip._vendor.cachecontrol.controller import CacheController
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def setup_logging() -> None:
|
| 23 |
+
logger.setLevel(logging.DEBUG)
|
| 24 |
+
handler = logging.StreamHandler()
|
| 25 |
+
logger.addHandler(handler)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def get_session() -> requests.Session:
|
| 29 |
+
adapter = CacheControlAdapter(
|
| 30 |
+
DictCache(), cache_etags=True, serializer=None, heuristic=None
|
| 31 |
+
)
|
| 32 |
+
sess = requests.Session()
|
| 33 |
+
sess.mount("http://", adapter)
|
| 34 |
+
sess.mount("https://", adapter)
|
| 35 |
+
|
| 36 |
+
sess.cache_controller = adapter.controller # type: ignore[attr-defined]
|
| 37 |
+
return sess
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_args() -> Namespace:
|
| 41 |
+
parser = ArgumentParser()
|
| 42 |
+
parser.add_argument("url", help="The URL to try and cache")
|
| 43 |
+
return parser.parse_args()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def main() -> None:
|
| 47 |
+
args = get_args()
|
| 48 |
+
sess = get_session()
|
| 49 |
+
|
| 50 |
+
# Make a request to get a response
|
| 51 |
+
resp = sess.get(args.url)
|
| 52 |
+
|
| 53 |
+
# Turn on logging
|
| 54 |
+
setup_logging()
|
| 55 |
+
|
| 56 |
+
# try setting the cache
|
| 57 |
+
cache_controller: CacheController = (
|
| 58 |
+
sess.cache_controller # type: ignore[attr-defined]
|
| 59 |
+
)
|
| 60 |
+
cache_controller.cache_response(resp.request, resp.raw)
|
| 61 |
+
|
| 62 |
+
# Now try to get it
|
| 63 |
+
if cache_controller.cached_request(resp.request):
|
| 64 |
+
print("Cached!")
|
| 65 |
+
else:
|
| 66 |
+
print("Not cached :(")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
main()
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/adapter.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import functools
|
| 7 |
+
import types
|
| 8 |
+
import weakref
|
| 9 |
+
import zlib
|
| 10 |
+
from typing import TYPE_CHECKING, Any, Collection, Mapping
|
| 11 |
+
|
| 12 |
+
from pip._vendor.requests.adapters import HTTPAdapter
|
| 13 |
+
|
| 14 |
+
from pip._vendor.cachecontrol.cache import DictCache
|
| 15 |
+
from pip._vendor.cachecontrol.controller import PERMANENT_REDIRECT_STATUSES, CacheController
|
| 16 |
+
from pip._vendor.cachecontrol.filewrapper import CallbackFileWrapper
|
| 17 |
+
|
| 18 |
+
if TYPE_CHECKING:
|
| 19 |
+
from pip._vendor.requests import PreparedRequest, Response
|
| 20 |
+
from pip._vendor.urllib3 import HTTPResponse
|
| 21 |
+
|
| 22 |
+
from pip._vendor.cachecontrol.cache import BaseCache
|
| 23 |
+
from pip._vendor.cachecontrol.heuristics import BaseHeuristic
|
| 24 |
+
from pip._vendor.cachecontrol.serialize import Serializer
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class CacheControlAdapter(HTTPAdapter):
|
| 28 |
+
invalidating_methods = {"PUT", "PATCH", "DELETE"}
|
| 29 |
+
|
| 30 |
+
def __init__(
|
| 31 |
+
self,
|
| 32 |
+
cache: BaseCache | None = None,
|
| 33 |
+
cache_etags: bool = True,
|
| 34 |
+
controller_class: type[CacheController] | None = None,
|
| 35 |
+
serializer: Serializer | None = None,
|
| 36 |
+
heuristic: BaseHeuristic | None = None,
|
| 37 |
+
cacheable_methods: Collection[str] | None = None,
|
| 38 |
+
*args: Any,
|
| 39 |
+
**kw: Any,
|
| 40 |
+
) -> None:
|
| 41 |
+
super().__init__(*args, **kw)
|
| 42 |
+
self.cache = DictCache() if cache is None else cache
|
| 43 |
+
self.heuristic = heuristic
|
| 44 |
+
self.cacheable_methods = cacheable_methods or ("GET",)
|
| 45 |
+
|
| 46 |
+
controller_factory = controller_class or CacheController
|
| 47 |
+
self.controller = controller_factory(
|
| 48 |
+
self.cache, cache_etags=cache_etags, serializer=serializer
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
def send(
|
| 52 |
+
self,
|
| 53 |
+
request: PreparedRequest,
|
| 54 |
+
stream: bool = False,
|
| 55 |
+
timeout: None | float | tuple[float, float] | tuple[float, None] = None,
|
| 56 |
+
verify: bool | str = True,
|
| 57 |
+
cert: (None | bytes | str | tuple[bytes | str, bytes | str]) = None,
|
| 58 |
+
proxies: Mapping[str, str] | None = None,
|
| 59 |
+
cacheable_methods: Collection[str] | None = None,
|
| 60 |
+
) -> Response:
|
| 61 |
+
"""
|
| 62 |
+
Send a request. Use the request information to see if it
|
| 63 |
+
exists in the cache and cache the response if we need to and can.
|
| 64 |
+
"""
|
| 65 |
+
cacheable = cacheable_methods or self.cacheable_methods
|
| 66 |
+
if request.method in cacheable:
|
| 67 |
+
try:
|
| 68 |
+
cached_response = self.controller.cached_request(request)
|
| 69 |
+
except zlib.error:
|
| 70 |
+
cached_response = None
|
| 71 |
+
if cached_response:
|
| 72 |
+
return self.build_response(request, cached_response, from_cache=True)
|
| 73 |
+
|
| 74 |
+
# check for etags and add headers if appropriate
|
| 75 |
+
request.headers.update(self.controller.conditional_headers(request))
|
| 76 |
+
|
| 77 |
+
resp = super().send(request, stream, timeout, verify, cert, proxies)
|
| 78 |
+
|
| 79 |
+
return resp
|
| 80 |
+
|
| 81 |
+
def build_response( # type: ignore[override]
|
| 82 |
+
self,
|
| 83 |
+
request: PreparedRequest,
|
| 84 |
+
response: HTTPResponse,
|
| 85 |
+
from_cache: bool = False,
|
| 86 |
+
cacheable_methods: Collection[str] | None = None,
|
| 87 |
+
) -> Response:
|
| 88 |
+
"""
|
| 89 |
+
Build a response by making a request or using the cache.
|
| 90 |
+
|
| 91 |
+
This will end up calling send and returning a potentially
|
| 92 |
+
cached response
|
| 93 |
+
"""
|
| 94 |
+
cacheable = cacheable_methods or self.cacheable_methods
|
| 95 |
+
if not from_cache and request.method in cacheable:
|
| 96 |
+
# Check for any heuristics that might update headers
|
| 97 |
+
# before trying to cache.
|
| 98 |
+
if self.heuristic:
|
| 99 |
+
response = self.heuristic.apply(response)
|
| 100 |
+
|
| 101 |
+
# apply any expiration heuristics
|
| 102 |
+
if response.status == 304:
|
| 103 |
+
# We must have sent an ETag request. This could mean
|
| 104 |
+
# that we've been expired already or that we simply
|
| 105 |
+
# have an etag. In either case, we want to try and
|
| 106 |
+
# update the cache if that is the case.
|
| 107 |
+
cached_response = self.controller.update_cached_response(
|
| 108 |
+
request, response
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
if cached_response is not response:
|
| 112 |
+
from_cache = True
|
| 113 |
+
|
| 114 |
+
# We are done with the server response, read a
|
| 115 |
+
# possible response body (compliant servers will
|
| 116 |
+
# not return one, but we cannot be 100% sure) and
|
| 117 |
+
# release the connection back to the pool.
|
| 118 |
+
response.read(decode_content=False)
|
| 119 |
+
response.release_conn()
|
| 120 |
+
|
| 121 |
+
response = cached_response
|
| 122 |
+
|
| 123 |
+
# We always cache the 301 responses
|
| 124 |
+
elif int(response.status) in PERMANENT_REDIRECT_STATUSES:
|
| 125 |
+
self.controller.cache_response(request, response)
|
| 126 |
+
else:
|
| 127 |
+
# Wrap the response file with a wrapper that will cache the
|
| 128 |
+
# response when the stream has been consumed.
|
| 129 |
+
response._fp = CallbackFileWrapper( # type: ignore[assignment]
|
| 130 |
+
response._fp, # type: ignore[arg-type]
|
| 131 |
+
functools.partial(
|
| 132 |
+
self.controller.cache_response, request, weakref.ref(response)
|
| 133 |
+
),
|
| 134 |
+
)
|
| 135 |
+
if response.chunked:
|
| 136 |
+
super_update_chunk_length = response.__class__._update_chunk_length
|
| 137 |
+
|
| 138 |
+
def _update_chunk_length(
|
| 139 |
+
weak_self: weakref.ReferenceType[HTTPResponse],
|
| 140 |
+
) -> None:
|
| 141 |
+
self = weak_self()
|
| 142 |
+
if self is None:
|
| 143 |
+
return
|
| 144 |
+
|
| 145 |
+
super_update_chunk_length(self)
|
| 146 |
+
if self.chunk_left == 0:
|
| 147 |
+
self._fp._close() # type: ignore[union-attr]
|
| 148 |
+
|
| 149 |
+
response._update_chunk_length = functools.partial( # type: ignore[method-assign]
|
| 150 |
+
_update_chunk_length, weakref.ref(response)
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
resp: Response = super().build_response(request, response)
|
| 154 |
+
|
| 155 |
+
# See if we should invalidate the cache.
|
| 156 |
+
if request.method in self.invalidating_methods and resp.ok:
|
| 157 |
+
assert request.url is not None
|
| 158 |
+
cache_url = self.controller.cache_url(request.url)
|
| 159 |
+
self.cache.delete(cache_url)
|
| 160 |
+
|
| 161 |
+
# Give the request a from_cache attr to let people use it
|
| 162 |
+
resp.from_cache = from_cache # type: ignore[attr-defined]
|
| 163 |
+
|
| 164 |
+
return resp
|
| 165 |
+
|
| 166 |
+
def close(self) -> None:
|
| 167 |
+
self.cache.close()
|
| 168 |
+
super().close() # type: ignore[no-untyped-call]
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/cache.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
|
| 5 |
+
"""
|
| 6 |
+
The cache object API for implementing caches. The default is a thread
|
| 7 |
+
safe in-memory dictionary.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
from threading import Lock
|
| 13 |
+
from typing import IO, TYPE_CHECKING, MutableMapping
|
| 14 |
+
|
| 15 |
+
if TYPE_CHECKING:
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class BaseCache:
|
| 20 |
+
def get(self, key: str) -> bytes | None:
|
| 21 |
+
raise NotImplementedError()
|
| 22 |
+
|
| 23 |
+
def set(
|
| 24 |
+
self, key: str, value: bytes, expires: int | datetime | None = None
|
| 25 |
+
) -> None:
|
| 26 |
+
raise NotImplementedError()
|
| 27 |
+
|
| 28 |
+
def delete(self, key: str) -> None:
|
| 29 |
+
raise NotImplementedError()
|
| 30 |
+
|
| 31 |
+
def close(self) -> None:
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class DictCache(BaseCache):
|
| 36 |
+
def __init__(self, init_dict: MutableMapping[str, bytes] | None = None) -> None:
|
| 37 |
+
self.lock = Lock()
|
| 38 |
+
self.data = init_dict or {}
|
| 39 |
+
|
| 40 |
+
def get(self, key: str) -> bytes | None:
|
| 41 |
+
return self.data.get(key, None)
|
| 42 |
+
|
| 43 |
+
def set(
|
| 44 |
+
self, key: str, value: bytes, expires: int | datetime | None = None
|
| 45 |
+
) -> None:
|
| 46 |
+
with self.lock:
|
| 47 |
+
self.data.update({key: value})
|
| 48 |
+
|
| 49 |
+
def delete(self, key: str) -> None:
|
| 50 |
+
with self.lock:
|
| 51 |
+
if key in self.data:
|
| 52 |
+
self.data.pop(key)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class SeparateBodyBaseCache(BaseCache):
|
| 56 |
+
"""
|
| 57 |
+
In this variant, the body is not stored mixed in with the metadata, but is
|
| 58 |
+
passed in (as a bytes-like object) in a separate call to ``set_body()``.
|
| 59 |
+
|
| 60 |
+
That is, the expected interaction pattern is::
|
| 61 |
+
|
| 62 |
+
cache.set(key, serialized_metadata)
|
| 63 |
+
cache.set_body(key)
|
| 64 |
+
|
| 65 |
+
Similarly, the body should be loaded separately via ``get_body()``.
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
def set_body(self, key: str, body: bytes) -> None:
|
| 69 |
+
raise NotImplementedError()
|
| 70 |
+
|
| 71 |
+
def get_body(self, key: str) -> IO[bytes] | None:
|
| 72 |
+
"""
|
| 73 |
+
Return the body as file-like object.
|
| 74 |
+
"""
|
| 75 |
+
raise NotImplementedError()
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
|
| 5 |
+
from pip._vendor.cachecontrol.caches.file_cache import FileCache, SeparateBodyFileCache
|
| 6 |
+
from pip._vendor.cachecontrol.caches.redis_cache import RedisCache
|
| 7 |
+
|
| 8 |
+
__all__ = ["FileCache", "SeparateBodyFileCache", "RedisCache"]
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (436 Bytes). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__pycache__/file_cache.cpython-310.pyc
ADDED
|
Binary file (5.25 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/__pycache__/redis_cache.cpython-310.pyc
ADDED
|
Binary file (2.05 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/file_cache.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import hashlib
|
| 7 |
+
import os
|
| 8 |
+
import tempfile
|
| 9 |
+
from textwrap import dedent
|
| 10 |
+
from typing import IO, TYPE_CHECKING
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
from pip._vendor.cachecontrol.cache import BaseCache, SeparateBodyBaseCache
|
| 14 |
+
from pip._vendor.cachecontrol.controller import CacheController
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from datetime import datetime
|
| 18 |
+
|
| 19 |
+
from filelock import BaseFileLock
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class _FileCacheMixin:
|
| 23 |
+
"""Shared implementation for both FileCache variants."""
|
| 24 |
+
|
| 25 |
+
def __init__(
|
| 26 |
+
self,
|
| 27 |
+
directory: str | Path,
|
| 28 |
+
forever: bool = False,
|
| 29 |
+
filemode: int = 0o0600,
|
| 30 |
+
dirmode: int = 0o0700,
|
| 31 |
+
lock_class: type[BaseFileLock] | None = None,
|
| 32 |
+
) -> None:
|
| 33 |
+
try:
|
| 34 |
+
if lock_class is None:
|
| 35 |
+
from filelock import FileLock
|
| 36 |
+
|
| 37 |
+
lock_class = FileLock
|
| 38 |
+
except ImportError:
|
| 39 |
+
notice = dedent(
|
| 40 |
+
"""
|
| 41 |
+
NOTE: In order to use the FileCache you must have
|
| 42 |
+
filelock installed. You can install it via pip:
|
| 43 |
+
pip install cachecontrol[filecache]
|
| 44 |
+
"""
|
| 45 |
+
)
|
| 46 |
+
raise ImportError(notice)
|
| 47 |
+
|
| 48 |
+
self.directory = directory
|
| 49 |
+
self.forever = forever
|
| 50 |
+
self.filemode = filemode
|
| 51 |
+
self.dirmode = dirmode
|
| 52 |
+
self.lock_class = lock_class
|
| 53 |
+
|
| 54 |
+
@staticmethod
|
| 55 |
+
def encode(x: str) -> str:
|
| 56 |
+
return hashlib.sha224(x.encode()).hexdigest()
|
| 57 |
+
|
| 58 |
+
def _fn(self, name: str) -> str:
|
| 59 |
+
# NOTE: This method should not change as some may depend on it.
|
| 60 |
+
# See: https://github.com/ionrock/cachecontrol/issues/63
|
| 61 |
+
hashed = self.encode(name)
|
| 62 |
+
parts = list(hashed[:5]) + [hashed]
|
| 63 |
+
return os.path.join(self.directory, *parts)
|
| 64 |
+
|
| 65 |
+
def get(self, key: str) -> bytes | None:
|
| 66 |
+
name = self._fn(key)
|
| 67 |
+
try:
|
| 68 |
+
with open(name, "rb") as fh:
|
| 69 |
+
return fh.read()
|
| 70 |
+
|
| 71 |
+
except FileNotFoundError:
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def set(
|
| 75 |
+
self, key: str, value: bytes, expires: int | datetime | None = None
|
| 76 |
+
) -> None:
|
| 77 |
+
name = self._fn(key)
|
| 78 |
+
self._write(name, value)
|
| 79 |
+
|
| 80 |
+
def _write(self, path: str, data: bytes) -> None:
|
| 81 |
+
"""
|
| 82 |
+
Safely write the data to the given path.
|
| 83 |
+
"""
|
| 84 |
+
# Make sure the directory exists
|
| 85 |
+
dirname = os.path.dirname(path)
|
| 86 |
+
os.makedirs(dirname, self.dirmode, exist_ok=True)
|
| 87 |
+
|
| 88 |
+
with self.lock_class(path + ".lock"):
|
| 89 |
+
# Write our actual file
|
| 90 |
+
(fd, name) = tempfile.mkstemp(dir=dirname)
|
| 91 |
+
try:
|
| 92 |
+
os.write(fd, data)
|
| 93 |
+
finally:
|
| 94 |
+
os.close(fd)
|
| 95 |
+
os.chmod(name, self.filemode)
|
| 96 |
+
os.replace(name, path)
|
| 97 |
+
|
| 98 |
+
def _delete(self, key: str, suffix: str) -> None:
|
| 99 |
+
name = self._fn(key) + suffix
|
| 100 |
+
if not self.forever:
|
| 101 |
+
try:
|
| 102 |
+
os.remove(name)
|
| 103 |
+
except FileNotFoundError:
|
| 104 |
+
pass
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
class FileCache(_FileCacheMixin, BaseCache):
|
| 108 |
+
"""
|
| 109 |
+
Traditional FileCache: body is stored in memory, so not suitable for large
|
| 110 |
+
downloads.
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
def delete(self, key: str) -> None:
|
| 114 |
+
self._delete(key, "")
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class SeparateBodyFileCache(_FileCacheMixin, SeparateBodyBaseCache):
|
| 118 |
+
"""
|
| 119 |
+
Memory-efficient FileCache: body is stored in a separate file, reducing
|
| 120 |
+
peak memory usage.
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
def get_body(self, key: str) -> IO[bytes] | None:
|
| 124 |
+
name = self._fn(key) + ".body"
|
| 125 |
+
try:
|
| 126 |
+
return open(name, "rb")
|
| 127 |
+
except FileNotFoundError:
|
| 128 |
+
return None
|
| 129 |
+
|
| 130 |
+
def set_body(self, key: str, body: bytes) -> None:
|
| 131 |
+
name = self._fn(key) + ".body"
|
| 132 |
+
self._write(name, body)
|
| 133 |
+
|
| 134 |
+
def delete(self, key: str) -> None:
|
| 135 |
+
self._delete(key, "")
|
| 136 |
+
self._delete(key, ".body")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def url_to_file_path(url: str, filecache: FileCache) -> str:
|
| 140 |
+
"""Return the file cache path based on the URL.
|
| 141 |
+
|
| 142 |
+
This does not ensure the file exists!
|
| 143 |
+
"""
|
| 144 |
+
key = CacheController.cache_url(url)
|
| 145 |
+
return filecache._fn(key)
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/redis_cache.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from datetime import datetime, timezone
|
| 8 |
+
from typing import TYPE_CHECKING
|
| 9 |
+
|
| 10 |
+
from pip._vendor.cachecontrol.cache import BaseCache
|
| 11 |
+
|
| 12 |
+
if TYPE_CHECKING:
|
| 13 |
+
from redis import Redis
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class RedisCache(BaseCache):
|
| 17 |
+
def __init__(self, conn: Redis[bytes]) -> None:
|
| 18 |
+
self.conn = conn
|
| 19 |
+
|
| 20 |
+
def get(self, key: str) -> bytes | None:
|
| 21 |
+
return self.conn.get(key)
|
| 22 |
+
|
| 23 |
+
def set(
|
| 24 |
+
self, key: str, value: bytes, expires: int | datetime | None = None
|
| 25 |
+
) -> None:
|
| 26 |
+
if not expires:
|
| 27 |
+
self.conn.set(key, value)
|
| 28 |
+
elif isinstance(expires, datetime):
|
| 29 |
+
now_utc = datetime.now(timezone.utc)
|
| 30 |
+
if expires.tzinfo is None:
|
| 31 |
+
now_utc = now_utc.replace(tzinfo=None)
|
| 32 |
+
delta = expires - now_utc
|
| 33 |
+
self.conn.setex(key, int(delta.total_seconds()), value)
|
| 34 |
+
else:
|
| 35 |
+
self.conn.setex(key, expires, value)
|
| 36 |
+
|
| 37 |
+
def delete(self, key: str) -> None:
|
| 38 |
+
self.conn.delete(key)
|
| 39 |
+
|
| 40 |
+
def clear(self) -> None:
|
| 41 |
+
"""Helper for clearing all the keys in a database. Use with
|
| 42 |
+
caution!"""
|
| 43 |
+
for key in self.conn.keys():
|
| 44 |
+
self.conn.delete(key)
|
| 45 |
+
|
| 46 |
+
def close(self) -> None:
|
| 47 |
+
"""Redis uses connection pooling, no need to close the connection."""
|
| 48 |
+
pass
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/controller.py
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
|
| 5 |
+
"""
|
| 6 |
+
The httplib2 algorithms ported for use with requests.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import calendar
|
| 12 |
+
import logging
|
| 13 |
+
import re
|
| 14 |
+
import time
|
| 15 |
+
import weakref
|
| 16 |
+
from email.utils import parsedate_tz
|
| 17 |
+
from typing import TYPE_CHECKING, Collection, Mapping
|
| 18 |
+
|
| 19 |
+
from pip._vendor.requests.structures import CaseInsensitiveDict
|
| 20 |
+
|
| 21 |
+
from pip._vendor.cachecontrol.cache import DictCache, SeparateBodyBaseCache
|
| 22 |
+
from pip._vendor.cachecontrol.serialize import Serializer
|
| 23 |
+
|
| 24 |
+
if TYPE_CHECKING:
|
| 25 |
+
from typing import Literal
|
| 26 |
+
|
| 27 |
+
from pip._vendor.requests import PreparedRequest
|
| 28 |
+
from pip._vendor.urllib3 import HTTPResponse
|
| 29 |
+
|
| 30 |
+
from pip._vendor.cachecontrol.cache import BaseCache
|
| 31 |
+
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
URI = re.compile(r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?")
|
| 35 |
+
|
| 36 |
+
PERMANENT_REDIRECT_STATUSES = (301, 308)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def parse_uri(uri: str) -> tuple[str, str, str, str, str]:
|
| 40 |
+
"""Parses a URI using the regex given in Appendix B of RFC 3986.
|
| 41 |
+
|
| 42 |
+
(scheme, authority, path, query, fragment) = parse_uri(uri)
|
| 43 |
+
"""
|
| 44 |
+
match = URI.match(uri)
|
| 45 |
+
assert match is not None
|
| 46 |
+
groups = match.groups()
|
| 47 |
+
return (groups[1], groups[3], groups[4], groups[6], groups[8])
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class CacheController:
|
| 51 |
+
"""An interface to see if request should cached or not."""
|
| 52 |
+
|
| 53 |
+
def __init__(
|
| 54 |
+
self,
|
| 55 |
+
cache: BaseCache | None = None,
|
| 56 |
+
cache_etags: bool = True,
|
| 57 |
+
serializer: Serializer | None = None,
|
| 58 |
+
status_codes: Collection[int] | None = None,
|
| 59 |
+
):
|
| 60 |
+
self.cache = DictCache() if cache is None else cache
|
| 61 |
+
self.cache_etags = cache_etags
|
| 62 |
+
self.serializer = serializer or Serializer()
|
| 63 |
+
self.cacheable_status_codes = status_codes or (200, 203, 300, 301, 308)
|
| 64 |
+
|
| 65 |
+
@classmethod
|
| 66 |
+
def _urlnorm(cls, uri: str) -> str:
|
| 67 |
+
"""Normalize the URL to create a safe key for the cache"""
|
| 68 |
+
(scheme, authority, path, query, fragment) = parse_uri(uri)
|
| 69 |
+
if not scheme or not authority:
|
| 70 |
+
raise Exception("Only absolute URIs are allowed. uri = %s" % uri)
|
| 71 |
+
|
| 72 |
+
scheme = scheme.lower()
|
| 73 |
+
authority = authority.lower()
|
| 74 |
+
|
| 75 |
+
if not path:
|
| 76 |
+
path = "/"
|
| 77 |
+
|
| 78 |
+
# Could do syntax based normalization of the URI before
|
| 79 |
+
# computing the digest. See Section 6.2.2 of Std 66.
|
| 80 |
+
request_uri = query and "?".join([path, query]) or path
|
| 81 |
+
defrag_uri = scheme + "://" + authority + request_uri
|
| 82 |
+
|
| 83 |
+
return defrag_uri
|
| 84 |
+
|
| 85 |
+
@classmethod
|
| 86 |
+
def cache_url(cls, uri: str) -> str:
|
| 87 |
+
return cls._urlnorm(uri)
|
| 88 |
+
|
| 89 |
+
def parse_cache_control(self, headers: Mapping[str, str]) -> dict[str, int | None]:
|
| 90 |
+
known_directives = {
|
| 91 |
+
# https://tools.ietf.org/html/rfc7234#section-5.2
|
| 92 |
+
"max-age": (int, True),
|
| 93 |
+
"max-stale": (int, False),
|
| 94 |
+
"min-fresh": (int, True),
|
| 95 |
+
"no-cache": (None, False),
|
| 96 |
+
"no-store": (None, False),
|
| 97 |
+
"no-transform": (None, False),
|
| 98 |
+
"only-if-cached": (None, False),
|
| 99 |
+
"must-revalidate": (None, False),
|
| 100 |
+
"public": (None, False),
|
| 101 |
+
"private": (None, False),
|
| 102 |
+
"proxy-revalidate": (None, False),
|
| 103 |
+
"s-maxage": (int, True),
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
cc_headers = headers.get("cache-control", headers.get("Cache-Control", ""))
|
| 107 |
+
|
| 108 |
+
retval: dict[str, int | None] = {}
|
| 109 |
+
|
| 110 |
+
for cc_directive in cc_headers.split(","):
|
| 111 |
+
if not cc_directive.strip():
|
| 112 |
+
continue
|
| 113 |
+
|
| 114 |
+
parts = cc_directive.split("=", 1)
|
| 115 |
+
directive = parts[0].strip()
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
typ, required = known_directives[directive]
|
| 119 |
+
except KeyError:
|
| 120 |
+
logger.debug("Ignoring unknown cache-control directive: %s", directive)
|
| 121 |
+
continue
|
| 122 |
+
|
| 123 |
+
if not typ or not required:
|
| 124 |
+
retval[directive] = None
|
| 125 |
+
if typ:
|
| 126 |
+
try:
|
| 127 |
+
retval[directive] = typ(parts[1].strip())
|
| 128 |
+
except IndexError:
|
| 129 |
+
if required:
|
| 130 |
+
logger.debug(
|
| 131 |
+
"Missing value for cache-control " "directive: %s",
|
| 132 |
+
directive,
|
| 133 |
+
)
|
| 134 |
+
except ValueError:
|
| 135 |
+
logger.debug(
|
| 136 |
+
"Invalid value for cache-control directive " "%s, must be %s",
|
| 137 |
+
directive,
|
| 138 |
+
typ.__name__,
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
return retval
|
| 142 |
+
|
| 143 |
+
def _load_from_cache(self, request: PreparedRequest) -> HTTPResponse | None:
|
| 144 |
+
"""
|
| 145 |
+
Load a cached response, or return None if it's not available.
|
| 146 |
+
"""
|
| 147 |
+
# We do not support caching of partial content: so if the request contains a
|
| 148 |
+
# Range header then we don't want to load anything from the cache.
|
| 149 |
+
if "Range" in request.headers:
|
| 150 |
+
return None
|
| 151 |
+
|
| 152 |
+
cache_url = request.url
|
| 153 |
+
assert cache_url is not None
|
| 154 |
+
cache_data = self.cache.get(cache_url)
|
| 155 |
+
if cache_data is None:
|
| 156 |
+
logger.debug("No cache entry available")
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
if isinstance(self.cache, SeparateBodyBaseCache):
|
| 160 |
+
body_file = self.cache.get_body(cache_url)
|
| 161 |
+
else:
|
| 162 |
+
body_file = None
|
| 163 |
+
|
| 164 |
+
result = self.serializer.loads(request, cache_data, body_file)
|
| 165 |
+
if result is None:
|
| 166 |
+
logger.warning("Cache entry deserialization failed, entry ignored")
|
| 167 |
+
return result
|
| 168 |
+
|
| 169 |
+
def cached_request(self, request: PreparedRequest) -> HTTPResponse | Literal[False]:
|
| 170 |
+
"""
|
| 171 |
+
Return a cached response if it exists in the cache, otherwise
|
| 172 |
+
return False.
|
| 173 |
+
"""
|
| 174 |
+
assert request.url is not None
|
| 175 |
+
cache_url = self.cache_url(request.url)
|
| 176 |
+
logger.debug('Looking up "%s" in the cache', cache_url)
|
| 177 |
+
cc = self.parse_cache_control(request.headers)
|
| 178 |
+
|
| 179 |
+
# Bail out if the request insists on fresh data
|
| 180 |
+
if "no-cache" in cc:
|
| 181 |
+
logger.debug('Request header has "no-cache", cache bypassed')
|
| 182 |
+
return False
|
| 183 |
+
|
| 184 |
+
if "max-age" in cc and cc["max-age"] == 0:
|
| 185 |
+
logger.debug('Request header has "max_age" as 0, cache bypassed')
|
| 186 |
+
return False
|
| 187 |
+
|
| 188 |
+
# Check whether we can load the response from the cache:
|
| 189 |
+
resp = self._load_from_cache(request)
|
| 190 |
+
if not resp:
|
| 191 |
+
return False
|
| 192 |
+
|
| 193 |
+
# If we have a cached permanent redirect, return it immediately. We
|
| 194 |
+
# don't need to test our response for other headers b/c it is
|
| 195 |
+
# intrinsically "cacheable" as it is Permanent.
|
| 196 |
+
#
|
| 197 |
+
# See:
|
| 198 |
+
# https://tools.ietf.org/html/rfc7231#section-6.4.2
|
| 199 |
+
#
|
| 200 |
+
# Client can try to refresh the value by repeating the request
|
| 201 |
+
# with cache busting headers as usual (ie no-cache).
|
| 202 |
+
if int(resp.status) in PERMANENT_REDIRECT_STATUSES:
|
| 203 |
+
msg = (
|
| 204 |
+
"Returning cached permanent redirect response "
|
| 205 |
+
"(ignoring date and etag information)"
|
| 206 |
+
)
|
| 207 |
+
logger.debug(msg)
|
| 208 |
+
return resp
|
| 209 |
+
|
| 210 |
+
headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(resp.headers)
|
| 211 |
+
if not headers or "date" not in headers:
|
| 212 |
+
if "etag" not in headers:
|
| 213 |
+
# Without date or etag, the cached response can never be used
|
| 214 |
+
# and should be deleted.
|
| 215 |
+
logger.debug("Purging cached response: no date or etag")
|
| 216 |
+
self.cache.delete(cache_url)
|
| 217 |
+
logger.debug("Ignoring cached response: no date")
|
| 218 |
+
return False
|
| 219 |
+
|
| 220 |
+
now = time.time()
|
| 221 |
+
time_tuple = parsedate_tz(headers["date"])
|
| 222 |
+
assert time_tuple is not None
|
| 223 |
+
date = calendar.timegm(time_tuple[:6])
|
| 224 |
+
current_age = max(0, now - date)
|
| 225 |
+
logger.debug("Current age based on date: %i", current_age)
|
| 226 |
+
|
| 227 |
+
# TODO: There is an assumption that the result will be a
|
| 228 |
+
# urllib3 response object. This may not be best since we
|
| 229 |
+
# could probably avoid instantiating or constructing the
|
| 230 |
+
# response until we know we need it.
|
| 231 |
+
resp_cc = self.parse_cache_control(headers)
|
| 232 |
+
|
| 233 |
+
# determine freshness
|
| 234 |
+
freshness_lifetime = 0
|
| 235 |
+
|
| 236 |
+
# Check the max-age pragma in the cache control header
|
| 237 |
+
max_age = resp_cc.get("max-age")
|
| 238 |
+
if max_age is not None:
|
| 239 |
+
freshness_lifetime = max_age
|
| 240 |
+
logger.debug("Freshness lifetime from max-age: %i", freshness_lifetime)
|
| 241 |
+
|
| 242 |
+
# If there isn't a max-age, check for an expires header
|
| 243 |
+
elif "expires" in headers:
|
| 244 |
+
expires = parsedate_tz(headers["expires"])
|
| 245 |
+
if expires is not None:
|
| 246 |
+
expire_time = calendar.timegm(expires[:6]) - date
|
| 247 |
+
freshness_lifetime = max(0, expire_time)
|
| 248 |
+
logger.debug("Freshness lifetime from expires: %i", freshness_lifetime)
|
| 249 |
+
|
| 250 |
+
# Determine if we are setting freshness limit in the
|
| 251 |
+
# request. Note, this overrides what was in the response.
|
| 252 |
+
max_age = cc.get("max-age")
|
| 253 |
+
if max_age is not None:
|
| 254 |
+
freshness_lifetime = max_age
|
| 255 |
+
logger.debug(
|
| 256 |
+
"Freshness lifetime from request max-age: %i", freshness_lifetime
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
min_fresh = cc.get("min-fresh")
|
| 260 |
+
if min_fresh is not None:
|
| 261 |
+
# adjust our current age by our min fresh
|
| 262 |
+
current_age += min_fresh
|
| 263 |
+
logger.debug("Adjusted current age from min-fresh: %i", current_age)
|
| 264 |
+
|
| 265 |
+
# Return entry if it is fresh enough
|
| 266 |
+
if freshness_lifetime > current_age:
|
| 267 |
+
logger.debug('The response is "fresh", returning cached response')
|
| 268 |
+
logger.debug("%i > %i", freshness_lifetime, current_age)
|
| 269 |
+
return resp
|
| 270 |
+
|
| 271 |
+
# we're not fresh. If we don't have an Etag, clear it out
|
| 272 |
+
if "etag" not in headers:
|
| 273 |
+
logger.debug('The cached response is "stale" with no etag, purging')
|
| 274 |
+
self.cache.delete(cache_url)
|
| 275 |
+
|
| 276 |
+
# return the original handler
|
| 277 |
+
return False
|
| 278 |
+
|
| 279 |
+
def conditional_headers(self, request: PreparedRequest) -> dict[str, str]:
|
| 280 |
+
resp = self._load_from_cache(request)
|
| 281 |
+
new_headers = {}
|
| 282 |
+
|
| 283 |
+
if resp:
|
| 284 |
+
headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(resp.headers)
|
| 285 |
+
|
| 286 |
+
if "etag" in headers:
|
| 287 |
+
new_headers["If-None-Match"] = headers["ETag"]
|
| 288 |
+
|
| 289 |
+
if "last-modified" in headers:
|
| 290 |
+
new_headers["If-Modified-Since"] = headers["Last-Modified"]
|
| 291 |
+
|
| 292 |
+
return new_headers
|
| 293 |
+
|
| 294 |
+
def _cache_set(
|
| 295 |
+
self,
|
| 296 |
+
cache_url: str,
|
| 297 |
+
request: PreparedRequest,
|
| 298 |
+
response: HTTPResponse,
|
| 299 |
+
body: bytes | None = None,
|
| 300 |
+
expires_time: int | None = None,
|
| 301 |
+
) -> None:
|
| 302 |
+
"""
|
| 303 |
+
Store the data in the cache.
|
| 304 |
+
"""
|
| 305 |
+
if isinstance(self.cache, SeparateBodyBaseCache):
|
| 306 |
+
# We pass in the body separately; just put a placeholder empty
|
| 307 |
+
# string in the metadata.
|
| 308 |
+
self.cache.set(
|
| 309 |
+
cache_url,
|
| 310 |
+
self.serializer.dumps(request, response, b""),
|
| 311 |
+
expires=expires_time,
|
| 312 |
+
)
|
| 313 |
+
# body is None can happen when, for example, we're only updating
|
| 314 |
+
# headers, as is the case in update_cached_response().
|
| 315 |
+
if body is not None:
|
| 316 |
+
self.cache.set_body(cache_url, body)
|
| 317 |
+
else:
|
| 318 |
+
self.cache.set(
|
| 319 |
+
cache_url,
|
| 320 |
+
self.serializer.dumps(request, response, body),
|
| 321 |
+
expires=expires_time,
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
def cache_response(
|
| 325 |
+
self,
|
| 326 |
+
request: PreparedRequest,
|
| 327 |
+
response_or_ref: HTTPResponse | weakref.ReferenceType[HTTPResponse],
|
| 328 |
+
body: bytes | None = None,
|
| 329 |
+
status_codes: Collection[int] | None = None,
|
| 330 |
+
) -> None:
|
| 331 |
+
"""
|
| 332 |
+
Algorithm for caching requests.
|
| 333 |
+
|
| 334 |
+
This assumes a requests Response object.
|
| 335 |
+
"""
|
| 336 |
+
if isinstance(response_or_ref, weakref.ReferenceType):
|
| 337 |
+
response = response_or_ref()
|
| 338 |
+
if response is None:
|
| 339 |
+
# The weakref can be None only in case the user used streamed request
|
| 340 |
+
# and did not consume or close it, and holds no reference to requests.Response.
|
| 341 |
+
# In such case, we don't want to cache the response.
|
| 342 |
+
return
|
| 343 |
+
else:
|
| 344 |
+
response = response_or_ref
|
| 345 |
+
|
| 346 |
+
# From httplib2: Don't cache 206's since we aren't going to
|
| 347 |
+
# handle byte range requests
|
| 348 |
+
cacheable_status_codes = status_codes or self.cacheable_status_codes
|
| 349 |
+
if response.status not in cacheable_status_codes:
|
| 350 |
+
logger.debug(
|
| 351 |
+
"Status code %s not in %s", response.status, cacheable_status_codes
|
| 352 |
+
)
|
| 353 |
+
return
|
| 354 |
+
|
| 355 |
+
response_headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(
|
| 356 |
+
response.headers
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
if "date" in response_headers:
|
| 360 |
+
time_tuple = parsedate_tz(response_headers["date"])
|
| 361 |
+
assert time_tuple is not None
|
| 362 |
+
date = calendar.timegm(time_tuple[:6])
|
| 363 |
+
else:
|
| 364 |
+
date = 0
|
| 365 |
+
|
| 366 |
+
# If we've been given a body, our response has a Content-Length, that
|
| 367 |
+
# Content-Length is valid then we can check to see if the body we've
|
| 368 |
+
# been given matches the expected size, and if it doesn't we'll just
|
| 369 |
+
# skip trying to cache it.
|
| 370 |
+
if (
|
| 371 |
+
body is not None
|
| 372 |
+
and "content-length" in response_headers
|
| 373 |
+
and response_headers["content-length"].isdigit()
|
| 374 |
+
and int(response_headers["content-length"]) != len(body)
|
| 375 |
+
):
|
| 376 |
+
return
|
| 377 |
+
|
| 378 |
+
cc_req = self.parse_cache_control(request.headers)
|
| 379 |
+
cc = self.parse_cache_control(response_headers)
|
| 380 |
+
|
| 381 |
+
assert request.url is not None
|
| 382 |
+
cache_url = self.cache_url(request.url)
|
| 383 |
+
logger.debug('Updating cache with response from "%s"', cache_url)
|
| 384 |
+
|
| 385 |
+
# Delete it from the cache if we happen to have it stored there
|
| 386 |
+
no_store = False
|
| 387 |
+
if "no-store" in cc:
|
| 388 |
+
no_store = True
|
| 389 |
+
logger.debug('Response header has "no-store"')
|
| 390 |
+
if "no-store" in cc_req:
|
| 391 |
+
no_store = True
|
| 392 |
+
logger.debug('Request header has "no-store"')
|
| 393 |
+
if no_store and self.cache.get(cache_url):
|
| 394 |
+
logger.debug('Purging existing cache entry to honor "no-store"')
|
| 395 |
+
self.cache.delete(cache_url)
|
| 396 |
+
if no_store:
|
| 397 |
+
return
|
| 398 |
+
|
| 399 |
+
# https://tools.ietf.org/html/rfc7234#section-4.1:
|
| 400 |
+
# A Vary header field-value of "*" always fails to match.
|
| 401 |
+
# Storing such a response leads to a deserialization warning
|
| 402 |
+
# during cache lookup and is not allowed to ever be served,
|
| 403 |
+
# so storing it can be avoided.
|
| 404 |
+
if "*" in response_headers.get("vary", ""):
|
| 405 |
+
logger.debug('Response header has "Vary: *"')
|
| 406 |
+
return
|
| 407 |
+
|
| 408 |
+
# If we've been given an etag, then keep the response
|
| 409 |
+
if self.cache_etags and "etag" in response_headers:
|
| 410 |
+
expires_time = 0
|
| 411 |
+
if response_headers.get("expires"):
|
| 412 |
+
expires = parsedate_tz(response_headers["expires"])
|
| 413 |
+
if expires is not None:
|
| 414 |
+
expires_time = calendar.timegm(expires[:6]) - date
|
| 415 |
+
|
| 416 |
+
expires_time = max(expires_time, 14 * 86400)
|
| 417 |
+
|
| 418 |
+
logger.debug(f"etag object cached for {expires_time} seconds")
|
| 419 |
+
logger.debug("Caching due to etag")
|
| 420 |
+
self._cache_set(cache_url, request, response, body, expires_time)
|
| 421 |
+
|
| 422 |
+
# Add to the cache any permanent redirects. We do this before looking
|
| 423 |
+
# that the Date headers.
|
| 424 |
+
elif int(response.status) in PERMANENT_REDIRECT_STATUSES:
|
| 425 |
+
logger.debug("Caching permanent redirect")
|
| 426 |
+
self._cache_set(cache_url, request, response, b"")
|
| 427 |
+
|
| 428 |
+
# Add to the cache if the response headers demand it. If there
|
| 429 |
+
# is no date header then we can't do anything about expiring
|
| 430 |
+
# the cache.
|
| 431 |
+
elif "date" in response_headers:
|
| 432 |
+
time_tuple = parsedate_tz(response_headers["date"])
|
| 433 |
+
assert time_tuple is not None
|
| 434 |
+
date = calendar.timegm(time_tuple[:6])
|
| 435 |
+
# cache when there is a max-age > 0
|
| 436 |
+
max_age = cc.get("max-age")
|
| 437 |
+
if max_age is not None and max_age > 0:
|
| 438 |
+
logger.debug("Caching b/c date exists and max-age > 0")
|
| 439 |
+
expires_time = max_age
|
| 440 |
+
self._cache_set(
|
| 441 |
+
cache_url,
|
| 442 |
+
request,
|
| 443 |
+
response,
|
| 444 |
+
body,
|
| 445 |
+
expires_time,
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
# If the request can expire, it means we should cache it
|
| 449 |
+
# in the meantime.
|
| 450 |
+
elif "expires" in response_headers:
|
| 451 |
+
if response_headers["expires"]:
|
| 452 |
+
expires = parsedate_tz(response_headers["expires"])
|
| 453 |
+
if expires is not None:
|
| 454 |
+
expires_time = calendar.timegm(expires[:6]) - date
|
| 455 |
+
else:
|
| 456 |
+
expires_time = None
|
| 457 |
+
|
| 458 |
+
logger.debug(
|
| 459 |
+
"Caching b/c of expires header. expires in {} seconds".format(
|
| 460 |
+
expires_time
|
| 461 |
+
)
|
| 462 |
+
)
|
| 463 |
+
self._cache_set(
|
| 464 |
+
cache_url,
|
| 465 |
+
request,
|
| 466 |
+
response,
|
| 467 |
+
body,
|
| 468 |
+
expires_time,
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
def update_cached_response(
|
| 472 |
+
self, request: PreparedRequest, response: HTTPResponse
|
| 473 |
+
) -> HTTPResponse:
|
| 474 |
+
"""On a 304 we will get a new set of headers that we want to
|
| 475 |
+
update our cached value with, assuming we have one.
|
| 476 |
+
|
| 477 |
+
This should only ever be called when we've sent an ETag and
|
| 478 |
+
gotten a 304 as the response.
|
| 479 |
+
"""
|
| 480 |
+
assert request.url is not None
|
| 481 |
+
cache_url = self.cache_url(request.url)
|
| 482 |
+
cached_response = self._load_from_cache(request)
|
| 483 |
+
|
| 484 |
+
if not cached_response:
|
| 485 |
+
# we didn't have a cached response
|
| 486 |
+
return response
|
| 487 |
+
|
| 488 |
+
# Lets update our headers with the headers from the new request:
|
| 489 |
+
# http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-26#section-4.1
|
| 490 |
+
#
|
| 491 |
+
# The server isn't supposed to send headers that would make
|
| 492 |
+
# the cached body invalid. But... just in case, we'll be sure
|
| 493 |
+
# to strip out ones we know that might be problmatic due to
|
| 494 |
+
# typical assumptions.
|
| 495 |
+
excluded_headers = ["content-length"]
|
| 496 |
+
|
| 497 |
+
cached_response.headers.update(
|
| 498 |
+
{
|
| 499 |
+
k: v
|
| 500 |
+
for k, v in response.headers.items()
|
| 501 |
+
if k.lower() not in excluded_headers
|
| 502 |
+
}
|
| 503 |
+
)
|
| 504 |
+
|
| 505 |
+
# we want a 200 b/c we have content via the cache
|
| 506 |
+
cached_response.status = 200
|
| 507 |
+
|
| 508 |
+
# update our cache
|
| 509 |
+
self._cache_set(cache_url, request, cached_response)
|
| 510 |
+
|
| 511 |
+
return cached_response
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/filewrapper.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import mmap
|
| 7 |
+
from tempfile import NamedTemporaryFile
|
| 8 |
+
from typing import TYPE_CHECKING, Any, Callable
|
| 9 |
+
|
| 10 |
+
if TYPE_CHECKING:
|
| 11 |
+
from http.client import HTTPResponse
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class CallbackFileWrapper:
|
| 15 |
+
"""
|
| 16 |
+
Small wrapper around a fp object which will tee everything read into a
|
| 17 |
+
buffer, and when that file is closed it will execute a callback with the
|
| 18 |
+
contents of that buffer.
|
| 19 |
+
|
| 20 |
+
All attributes are proxied to the underlying file object.
|
| 21 |
+
|
| 22 |
+
This class uses members with a double underscore (__) leading prefix so as
|
| 23 |
+
not to accidentally shadow an attribute.
|
| 24 |
+
|
| 25 |
+
The data is stored in a temporary file until it is all available. As long
|
| 26 |
+
as the temporary files directory is disk-based (sometimes it's a
|
| 27 |
+
memory-backed-``tmpfs`` on Linux), data will be unloaded to disk if memory
|
| 28 |
+
pressure is high. For small files the disk usually won't be used at all,
|
| 29 |
+
it'll all be in the filesystem memory cache, so there should be no
|
| 30 |
+
performance impact.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
def __init__(
|
| 34 |
+
self, fp: HTTPResponse, callback: Callable[[bytes], None] | None
|
| 35 |
+
) -> None:
|
| 36 |
+
self.__buf = NamedTemporaryFile("rb+", delete=True)
|
| 37 |
+
self.__fp = fp
|
| 38 |
+
self.__callback = callback
|
| 39 |
+
|
| 40 |
+
def __getattr__(self, name: str) -> Any:
|
| 41 |
+
# The vagaries of garbage collection means that self.__fp is
|
| 42 |
+
# not always set. By using __getattribute__ and the private
|
| 43 |
+
# name[0] allows looking up the attribute value and raising an
|
| 44 |
+
# AttributeError when it doesn't exist. This stop things from
|
| 45 |
+
# infinitely recursing calls to getattr in the case where
|
| 46 |
+
# self.__fp hasn't been set.
|
| 47 |
+
#
|
| 48 |
+
# [0] https://docs.python.org/2/reference/expressions.html#atom-identifiers
|
| 49 |
+
fp = self.__getattribute__("_CallbackFileWrapper__fp")
|
| 50 |
+
return getattr(fp, name)
|
| 51 |
+
|
| 52 |
+
def __is_fp_closed(self) -> bool:
|
| 53 |
+
try:
|
| 54 |
+
return self.__fp.fp is None
|
| 55 |
+
|
| 56 |
+
except AttributeError:
|
| 57 |
+
pass
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
closed: bool = self.__fp.closed
|
| 61 |
+
return closed
|
| 62 |
+
|
| 63 |
+
except AttributeError:
|
| 64 |
+
pass
|
| 65 |
+
|
| 66 |
+
# We just don't cache it then.
|
| 67 |
+
# TODO: Add some logging here...
|
| 68 |
+
return False
|
| 69 |
+
|
| 70 |
+
def _close(self) -> None:
|
| 71 |
+
if self.__callback:
|
| 72 |
+
if self.__buf.tell() == 0:
|
| 73 |
+
# Empty file:
|
| 74 |
+
result = b""
|
| 75 |
+
else:
|
| 76 |
+
# Return the data without actually loading it into memory,
|
| 77 |
+
# relying on Python's buffer API and mmap(). mmap() just gives
|
| 78 |
+
# a view directly into the filesystem's memory cache, so it
|
| 79 |
+
# doesn't result in duplicate memory use.
|
| 80 |
+
self.__buf.seek(0, 0)
|
| 81 |
+
result = memoryview(
|
| 82 |
+
mmap.mmap(self.__buf.fileno(), 0, access=mmap.ACCESS_READ)
|
| 83 |
+
)
|
| 84 |
+
self.__callback(result)
|
| 85 |
+
|
| 86 |
+
# We assign this to None here, because otherwise we can get into
|
| 87 |
+
# really tricky problems where the CPython interpreter dead locks
|
| 88 |
+
# because the callback is holding a reference to something which
|
| 89 |
+
# has a __del__ method. Setting this to None breaks the cycle
|
| 90 |
+
# and allows the garbage collector to do it's thing normally.
|
| 91 |
+
self.__callback = None
|
| 92 |
+
|
| 93 |
+
# Closing the temporary file releases memory and frees disk space.
|
| 94 |
+
# Important when caching big files.
|
| 95 |
+
self.__buf.close()
|
| 96 |
+
|
| 97 |
+
def read(self, amt: int | None = None) -> bytes:
|
| 98 |
+
data: bytes = self.__fp.read(amt)
|
| 99 |
+
if data:
|
| 100 |
+
# We may be dealing with b'', a sign that things are over:
|
| 101 |
+
# it's passed e.g. after we've already closed self.__buf.
|
| 102 |
+
self.__buf.write(data)
|
| 103 |
+
if self.__is_fp_closed():
|
| 104 |
+
self._close()
|
| 105 |
+
|
| 106 |
+
return data
|
| 107 |
+
|
| 108 |
+
def _safe_read(self, amt: int) -> bytes:
|
| 109 |
+
data: bytes = self.__fp._safe_read(amt) # type: ignore[attr-defined]
|
| 110 |
+
if amt == 2 and data == b"\r\n":
|
| 111 |
+
# urllib executes this read to toss the CRLF at the end
|
| 112 |
+
# of the chunk.
|
| 113 |
+
return data
|
| 114 |
+
|
| 115 |
+
self.__buf.write(data)
|
| 116 |
+
if self.__is_fp_closed():
|
| 117 |
+
self._close()
|
| 118 |
+
|
| 119 |
+
return data
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/heuristics.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import calendar
|
| 7 |
+
import time
|
| 8 |
+
from datetime import datetime, timedelta, timezone
|
| 9 |
+
from email.utils import formatdate, parsedate, parsedate_tz
|
| 10 |
+
from typing import TYPE_CHECKING, Any, Mapping
|
| 11 |
+
|
| 12 |
+
if TYPE_CHECKING:
|
| 13 |
+
from pip._vendor.urllib3 import HTTPResponse
|
| 14 |
+
|
| 15 |
+
TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def expire_after(delta: timedelta, date: datetime | None = None) -> datetime:
|
| 19 |
+
date = date or datetime.now(timezone.utc)
|
| 20 |
+
return date + delta
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def datetime_to_header(dt: datetime) -> str:
|
| 24 |
+
return formatdate(calendar.timegm(dt.timetuple()))
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class BaseHeuristic:
|
| 28 |
+
def warning(self, response: HTTPResponse) -> str | None:
|
| 29 |
+
"""
|
| 30 |
+
Return a valid 1xx warning header value describing the cache
|
| 31 |
+
adjustments.
|
| 32 |
+
|
| 33 |
+
The response is provided too allow warnings like 113
|
| 34 |
+
http://tools.ietf.org/html/rfc7234#section-5.5.4 where we need
|
| 35 |
+
to explicitly say response is over 24 hours old.
|
| 36 |
+
"""
|
| 37 |
+
return '110 - "Response is Stale"'
|
| 38 |
+
|
| 39 |
+
def update_headers(self, response: HTTPResponse) -> dict[str, str]:
|
| 40 |
+
"""Update the response headers with any new headers.
|
| 41 |
+
|
| 42 |
+
NOTE: This SHOULD always include some Warning header to
|
| 43 |
+
signify that the response was cached by the client, not
|
| 44 |
+
by way of the provided headers.
|
| 45 |
+
"""
|
| 46 |
+
return {}
|
| 47 |
+
|
| 48 |
+
def apply(self, response: HTTPResponse) -> HTTPResponse:
|
| 49 |
+
updated_headers = self.update_headers(response)
|
| 50 |
+
|
| 51 |
+
if updated_headers:
|
| 52 |
+
response.headers.update(updated_headers)
|
| 53 |
+
warning_header_value = self.warning(response)
|
| 54 |
+
if warning_header_value is not None:
|
| 55 |
+
response.headers.update({"Warning": warning_header_value})
|
| 56 |
+
|
| 57 |
+
return response
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class OneDayCache(BaseHeuristic):
|
| 61 |
+
"""
|
| 62 |
+
Cache the response by providing an expires 1 day in the
|
| 63 |
+
future.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
def update_headers(self, response: HTTPResponse) -> dict[str, str]:
|
| 67 |
+
headers = {}
|
| 68 |
+
|
| 69 |
+
if "expires" not in response.headers:
|
| 70 |
+
date = parsedate(response.headers["date"])
|
| 71 |
+
expires = expire_after(
|
| 72 |
+
timedelta(days=1),
|
| 73 |
+
date=datetime(*date[:6], tzinfo=timezone.utc), # type: ignore[index,misc]
|
| 74 |
+
)
|
| 75 |
+
headers["expires"] = datetime_to_header(expires)
|
| 76 |
+
headers["cache-control"] = "public"
|
| 77 |
+
return headers
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class ExpiresAfter(BaseHeuristic):
|
| 81 |
+
"""
|
| 82 |
+
Cache **all** requests for a defined time period.
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
def __init__(self, **kw: Any) -> None:
|
| 86 |
+
self.delta = timedelta(**kw)
|
| 87 |
+
|
| 88 |
+
def update_headers(self, response: HTTPResponse) -> dict[str, str]:
|
| 89 |
+
expires = expire_after(self.delta)
|
| 90 |
+
return {"expires": datetime_to_header(expires), "cache-control": "public"}
|
| 91 |
+
|
| 92 |
+
def warning(self, response: HTTPResponse) -> str | None:
|
| 93 |
+
tmpl = "110 - Automatically cached for %s. Response might be stale"
|
| 94 |
+
return tmpl % self.delta
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class LastModified(BaseHeuristic):
|
| 98 |
+
"""
|
| 99 |
+
If there is no Expires header already, fall back on Last-Modified
|
| 100 |
+
using the heuristic from
|
| 101 |
+
http://tools.ietf.org/html/rfc7234#section-4.2.2
|
| 102 |
+
to calculate a reasonable value.
|
| 103 |
+
|
| 104 |
+
Firefox also does something like this per
|
| 105 |
+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ
|
| 106 |
+
http://lxr.mozilla.org/mozilla-release/source/netwerk/protocol/http/nsHttpResponseHead.cpp#397
|
| 107 |
+
Unlike mozilla we limit this to 24-hr.
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
cacheable_by_default_statuses = {
|
| 111 |
+
200,
|
| 112 |
+
203,
|
| 113 |
+
204,
|
| 114 |
+
206,
|
| 115 |
+
300,
|
| 116 |
+
301,
|
| 117 |
+
404,
|
| 118 |
+
405,
|
| 119 |
+
410,
|
| 120 |
+
414,
|
| 121 |
+
501,
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
def update_headers(self, resp: HTTPResponse) -> dict[str, str]:
|
| 125 |
+
headers: Mapping[str, str] = resp.headers
|
| 126 |
+
|
| 127 |
+
if "expires" in headers:
|
| 128 |
+
return {}
|
| 129 |
+
|
| 130 |
+
if "cache-control" in headers and headers["cache-control"] != "public":
|
| 131 |
+
return {}
|
| 132 |
+
|
| 133 |
+
if resp.status not in self.cacheable_by_default_statuses:
|
| 134 |
+
return {}
|
| 135 |
+
|
| 136 |
+
if "date" not in headers or "last-modified" not in headers:
|
| 137 |
+
return {}
|
| 138 |
+
|
| 139 |
+
time_tuple = parsedate_tz(headers["date"])
|
| 140 |
+
assert time_tuple is not None
|
| 141 |
+
date = calendar.timegm(time_tuple[:6])
|
| 142 |
+
last_modified = parsedate(headers["last-modified"])
|
| 143 |
+
if last_modified is None:
|
| 144 |
+
return {}
|
| 145 |
+
|
| 146 |
+
now = time.time()
|
| 147 |
+
current_age = max(0, now - date)
|
| 148 |
+
delta = date - calendar.timegm(last_modified)
|
| 149 |
+
freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
|
| 150 |
+
if freshness_lifetime <= current_age:
|
| 151 |
+
return {}
|
| 152 |
+
|
| 153 |
+
expires = date + freshness_lifetime
|
| 154 |
+
return {"expires": time.strftime(TIME_FMT, time.gmtime(expires))}
|
| 155 |
+
|
| 156 |
+
def warning(self, resp: HTTPResponse) -> str | None:
|
| 157 |
+
return None
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/py.typed
ADDED
|
File without changes
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/serialize.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import io
|
| 7 |
+
from typing import IO, TYPE_CHECKING, Any, Mapping, cast
|
| 8 |
+
|
| 9 |
+
from pip._vendor import msgpack
|
| 10 |
+
from pip._vendor.requests.structures import CaseInsensitiveDict
|
| 11 |
+
from pip._vendor.urllib3 import HTTPResponse
|
| 12 |
+
|
| 13 |
+
if TYPE_CHECKING:
|
| 14 |
+
from pip._vendor.requests import PreparedRequest
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Serializer:
|
| 18 |
+
serde_version = "4"
|
| 19 |
+
|
| 20 |
+
def dumps(
|
| 21 |
+
self,
|
| 22 |
+
request: PreparedRequest,
|
| 23 |
+
response: HTTPResponse,
|
| 24 |
+
body: bytes | None = None,
|
| 25 |
+
) -> bytes:
|
| 26 |
+
response_headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(
|
| 27 |
+
response.headers
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if body is None:
|
| 31 |
+
# When a body isn't passed in, we'll read the response. We
|
| 32 |
+
# also update the response with a new file handler to be
|
| 33 |
+
# sure it acts as though it was never read.
|
| 34 |
+
body = response.read(decode_content=False)
|
| 35 |
+
response._fp = io.BytesIO(body) # type: ignore[assignment]
|
| 36 |
+
response.length_remaining = len(body)
|
| 37 |
+
|
| 38 |
+
data = {
|
| 39 |
+
"response": {
|
| 40 |
+
"body": body, # Empty bytestring if body is stored separately
|
| 41 |
+
"headers": {str(k): str(v) for k, v in response.headers.items()},
|
| 42 |
+
"status": response.status,
|
| 43 |
+
"version": response.version,
|
| 44 |
+
"reason": str(response.reason),
|
| 45 |
+
"decode_content": response.decode_content,
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Construct our vary headers
|
| 50 |
+
data["vary"] = {}
|
| 51 |
+
if "vary" in response_headers:
|
| 52 |
+
varied_headers = response_headers["vary"].split(",")
|
| 53 |
+
for header in varied_headers:
|
| 54 |
+
header = str(header).strip()
|
| 55 |
+
header_value = request.headers.get(header, None)
|
| 56 |
+
if header_value is not None:
|
| 57 |
+
header_value = str(header_value)
|
| 58 |
+
data["vary"][header] = header_value
|
| 59 |
+
|
| 60 |
+
return b",".join([f"cc={self.serde_version}".encode(), self.serialize(data)])
|
| 61 |
+
|
| 62 |
+
def serialize(self, data: dict[str, Any]) -> bytes:
|
| 63 |
+
return cast(bytes, msgpack.dumps(data, use_bin_type=True))
|
| 64 |
+
|
| 65 |
+
def loads(
|
| 66 |
+
self,
|
| 67 |
+
request: PreparedRequest,
|
| 68 |
+
data: bytes,
|
| 69 |
+
body_file: IO[bytes] | None = None,
|
| 70 |
+
) -> HTTPResponse | None:
|
| 71 |
+
# Short circuit if we've been given an empty set of data
|
| 72 |
+
if not data:
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
# Previous versions of this library supported other serialization
|
| 76 |
+
# formats, but these have all been removed.
|
| 77 |
+
if not data.startswith(f"cc={self.serde_version},".encode()):
|
| 78 |
+
return None
|
| 79 |
+
|
| 80 |
+
data = data[5:]
|
| 81 |
+
return self._loads_v4(request, data, body_file)
|
| 82 |
+
|
| 83 |
+
def prepare_response(
|
| 84 |
+
self,
|
| 85 |
+
request: PreparedRequest,
|
| 86 |
+
cached: Mapping[str, Any],
|
| 87 |
+
body_file: IO[bytes] | None = None,
|
| 88 |
+
) -> HTTPResponse | None:
|
| 89 |
+
"""Verify our vary headers match and construct a real urllib3
|
| 90 |
+
HTTPResponse object.
|
| 91 |
+
"""
|
| 92 |
+
# Special case the '*' Vary value as it means we cannot actually
|
| 93 |
+
# determine if the cached response is suitable for this request.
|
| 94 |
+
# This case is also handled in the controller code when creating
|
| 95 |
+
# a cache entry, but is left here for backwards compatibility.
|
| 96 |
+
if "*" in cached.get("vary", {}):
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
# Ensure that the Vary headers for the cached response match our
|
| 100 |
+
# request
|
| 101 |
+
for header, value in cached.get("vary", {}).items():
|
| 102 |
+
if request.headers.get(header, None) != value:
|
| 103 |
+
return None
|
| 104 |
+
|
| 105 |
+
body_raw = cached["response"].pop("body")
|
| 106 |
+
|
| 107 |
+
headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(
|
| 108 |
+
data=cached["response"]["headers"]
|
| 109 |
+
)
|
| 110 |
+
if headers.get("transfer-encoding", "") == "chunked":
|
| 111 |
+
headers.pop("transfer-encoding")
|
| 112 |
+
|
| 113 |
+
cached["response"]["headers"] = headers
|
| 114 |
+
|
| 115 |
+
try:
|
| 116 |
+
body: IO[bytes]
|
| 117 |
+
if body_file is None:
|
| 118 |
+
body = io.BytesIO(body_raw)
|
| 119 |
+
else:
|
| 120 |
+
body = body_file
|
| 121 |
+
except TypeError:
|
| 122 |
+
# This can happen if cachecontrol serialized to v1 format (pickle)
|
| 123 |
+
# using Python 2. A Python 2 str(byte string) will be unpickled as
|
| 124 |
+
# a Python 3 str (unicode string), which will cause the above to
|
| 125 |
+
# fail with:
|
| 126 |
+
#
|
| 127 |
+
# TypeError: 'str' does not support the buffer interface
|
| 128 |
+
body = io.BytesIO(body_raw.encode("utf8"))
|
| 129 |
+
|
| 130 |
+
# Discard any `strict` parameter serialized by older version of cachecontrol.
|
| 131 |
+
cached["response"].pop("strict", None)
|
| 132 |
+
|
| 133 |
+
return HTTPResponse(body=body, preload_content=False, **cached["response"])
|
| 134 |
+
|
| 135 |
+
def _loads_v4(
|
| 136 |
+
self,
|
| 137 |
+
request: PreparedRequest,
|
| 138 |
+
data: bytes,
|
| 139 |
+
body_file: IO[bytes] | None = None,
|
| 140 |
+
) -> HTTPResponse | None:
|
| 141 |
+
try:
|
| 142 |
+
cached = msgpack.loads(data, raw=False)
|
| 143 |
+
except ValueError:
|
| 144 |
+
return None
|
| 145 |
+
|
| 146 |
+
return self.prepare_response(request, cached, body_file)
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/cachecontrol/wrapper.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: 2015 Eric Larson
|
| 2 |
+
#
|
| 3 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
from typing import TYPE_CHECKING, Collection
|
| 7 |
+
|
| 8 |
+
from pip._vendor.cachecontrol.adapter import CacheControlAdapter
|
| 9 |
+
from pip._vendor.cachecontrol.cache import DictCache
|
| 10 |
+
|
| 11 |
+
if TYPE_CHECKING:
|
| 12 |
+
from pip._vendor import requests
|
| 13 |
+
|
| 14 |
+
from pip._vendor.cachecontrol.cache import BaseCache
|
| 15 |
+
from pip._vendor.cachecontrol.controller import CacheController
|
| 16 |
+
from pip._vendor.cachecontrol.heuristics import BaseHeuristic
|
| 17 |
+
from pip._vendor.cachecontrol.serialize import Serializer
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def CacheControl(
|
| 21 |
+
sess: requests.Session,
|
| 22 |
+
cache: BaseCache | None = None,
|
| 23 |
+
cache_etags: bool = True,
|
| 24 |
+
serializer: Serializer | None = None,
|
| 25 |
+
heuristic: BaseHeuristic | None = None,
|
| 26 |
+
controller_class: type[CacheController] | None = None,
|
| 27 |
+
adapter_class: type[CacheControlAdapter] | None = None,
|
| 28 |
+
cacheable_methods: Collection[str] | None = None,
|
| 29 |
+
) -> requests.Session:
|
| 30 |
+
cache = DictCache() if cache is None else cache
|
| 31 |
+
adapter_class = adapter_class or CacheControlAdapter
|
| 32 |
+
adapter = adapter_class(
|
| 33 |
+
cache,
|
| 34 |
+
cache_etags=cache_etags,
|
| 35 |
+
serializer=serializer,
|
| 36 |
+
heuristic=heuristic,
|
| 37 |
+
controller_class=controller_class,
|
| 38 |
+
cacheable_methods=cacheable_methods,
|
| 39 |
+
)
|
| 40 |
+
sess.mount("http://", adapter)
|
| 41 |
+
sess.mount("https://", adapter)
|
| 42 |
+
|
| 43 |
+
return sess
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2012-2023 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
__version__ = '0.3.9'
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class DistlibException(Exception):
|
| 13 |
+
pass
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
from logging import NullHandler
|
| 18 |
+
except ImportError: # pragma: no cover
|
| 19 |
+
|
| 20 |
+
class NullHandler(logging.Handler):
|
| 21 |
+
|
| 22 |
+
def handle(self, record):
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
def emit(self, record):
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
def createLock(self):
|
| 29 |
+
self.lock = None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
logger.addHandler(NullHandler())
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.07 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/compat.cpython-310.pyc
ADDED
|
Binary file (31.4 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/database.cpython-310.pyc
ADDED
|
Binary file (43.1 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/index.cpython-310.pyc
ADDED
|
Binary file (17.3 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/locators.cpython-310.pyc
ADDED
|
Binary file (38.3 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/manifest.cpython-310.pyc
ADDED
|
Binary file (10.2 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/markers.cpython-310.pyc
ADDED
|
Binary file (5.31 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/metadata.cpython-310.pyc
ADDED
|
Binary file (26.9 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/resources.cpython-310.pyc
ADDED
|
Binary file (11 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/scripts.cpython-310.pyc
ADDED
|
Binary file (11.7 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/util.cpython-310.pyc
ADDED
|
Binary file (52.1 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/version.cpython-310.pyc
ADDED
|
Binary file (20.3 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/__pycache__/wheel.cpython-310.pyc
ADDED
|
Binary file (28.5 kB). View file
|
|
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/compat.py
ADDED
|
@@ -0,0 +1,1137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2013-2017 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
from __future__ import absolute_import
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import re
|
| 11 |
+
import shutil
|
| 12 |
+
import sys
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
import ssl
|
| 16 |
+
except ImportError: # pragma: no cover
|
| 17 |
+
ssl = None
|
| 18 |
+
|
| 19 |
+
if sys.version_info[0] < 3: # pragma: no cover
|
| 20 |
+
from StringIO import StringIO
|
| 21 |
+
string_types = basestring,
|
| 22 |
+
text_type = unicode
|
| 23 |
+
from types import FileType as file_type
|
| 24 |
+
import __builtin__ as builtins
|
| 25 |
+
import ConfigParser as configparser
|
| 26 |
+
from urlparse import urlparse, urlunparse, urljoin, urlsplit, urlunsplit
|
| 27 |
+
from urllib import (urlretrieve, quote as _quote, unquote, url2pathname,
|
| 28 |
+
pathname2url, ContentTooShortError, splittype)
|
| 29 |
+
|
| 30 |
+
def quote(s):
|
| 31 |
+
if isinstance(s, unicode):
|
| 32 |
+
s = s.encode('utf-8')
|
| 33 |
+
return _quote(s)
|
| 34 |
+
|
| 35 |
+
import urllib2
|
| 36 |
+
from urllib2 import (Request, urlopen, URLError, HTTPError,
|
| 37 |
+
HTTPBasicAuthHandler, HTTPPasswordMgr, HTTPHandler,
|
| 38 |
+
HTTPRedirectHandler, build_opener)
|
| 39 |
+
if ssl:
|
| 40 |
+
from urllib2 import HTTPSHandler
|
| 41 |
+
import httplib
|
| 42 |
+
import xmlrpclib
|
| 43 |
+
import Queue as queue
|
| 44 |
+
from HTMLParser import HTMLParser
|
| 45 |
+
import htmlentitydefs
|
| 46 |
+
raw_input = raw_input
|
| 47 |
+
from itertools import ifilter as filter
|
| 48 |
+
from itertools import ifilterfalse as filterfalse
|
| 49 |
+
|
| 50 |
+
# Leaving this around for now, in case it needs resurrecting in some way
|
| 51 |
+
# _userprog = None
|
| 52 |
+
# def splituser(host):
|
| 53 |
+
# """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'."""
|
| 54 |
+
# global _userprog
|
| 55 |
+
# if _userprog is None:
|
| 56 |
+
# import re
|
| 57 |
+
# _userprog = re.compile('^(.*)@(.*)$')
|
| 58 |
+
|
| 59 |
+
# match = _userprog.match(host)
|
| 60 |
+
# if match: return match.group(1, 2)
|
| 61 |
+
# return None, host
|
| 62 |
+
|
| 63 |
+
else: # pragma: no cover
|
| 64 |
+
from io import StringIO
|
| 65 |
+
string_types = str,
|
| 66 |
+
text_type = str
|
| 67 |
+
from io import TextIOWrapper as file_type
|
| 68 |
+
import builtins
|
| 69 |
+
import configparser
|
| 70 |
+
from urllib.parse import (urlparse, urlunparse, urljoin, quote, unquote,
|
| 71 |
+
urlsplit, urlunsplit, splittype)
|
| 72 |
+
from urllib.request import (urlopen, urlretrieve, Request, url2pathname,
|
| 73 |
+
pathname2url, HTTPBasicAuthHandler,
|
| 74 |
+
HTTPPasswordMgr, HTTPHandler,
|
| 75 |
+
HTTPRedirectHandler, build_opener)
|
| 76 |
+
if ssl:
|
| 77 |
+
from urllib.request import HTTPSHandler
|
| 78 |
+
from urllib.error import HTTPError, URLError, ContentTooShortError
|
| 79 |
+
import http.client as httplib
|
| 80 |
+
import urllib.request as urllib2
|
| 81 |
+
import xmlrpc.client as xmlrpclib
|
| 82 |
+
import queue
|
| 83 |
+
from html.parser import HTMLParser
|
| 84 |
+
import html.entities as htmlentitydefs
|
| 85 |
+
raw_input = input
|
| 86 |
+
from itertools import filterfalse
|
| 87 |
+
filter = filter
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
from ssl import match_hostname, CertificateError
|
| 91 |
+
except ImportError: # pragma: no cover
|
| 92 |
+
|
| 93 |
+
class CertificateError(ValueError):
|
| 94 |
+
pass
|
| 95 |
+
|
| 96 |
+
def _dnsname_match(dn, hostname, max_wildcards=1):
|
| 97 |
+
"""Matching according to RFC 6125, section 6.4.3
|
| 98 |
+
|
| 99 |
+
http://tools.ietf.org/html/rfc6125#section-6.4.3
|
| 100 |
+
"""
|
| 101 |
+
pats = []
|
| 102 |
+
if not dn:
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
parts = dn.split('.')
|
| 106 |
+
leftmost, remainder = parts[0], parts[1:]
|
| 107 |
+
|
| 108 |
+
wildcards = leftmost.count('*')
|
| 109 |
+
if wildcards > max_wildcards:
|
| 110 |
+
# Issue #17980: avoid denials of service by refusing more
|
| 111 |
+
# than one wildcard per fragment. A survey of established
|
| 112 |
+
# policy among SSL implementations showed it to be a
|
| 113 |
+
# reasonable choice.
|
| 114 |
+
raise CertificateError(
|
| 115 |
+
"too many wildcards in certificate DNS name: " + repr(dn))
|
| 116 |
+
|
| 117 |
+
# speed up common case w/o wildcards
|
| 118 |
+
if not wildcards:
|
| 119 |
+
return dn.lower() == hostname.lower()
|
| 120 |
+
|
| 121 |
+
# RFC 6125, section 6.4.3, subitem 1.
|
| 122 |
+
# The client SHOULD NOT attempt to match a presented identifier in which
|
| 123 |
+
# the wildcard character comprises a label other than the left-most label.
|
| 124 |
+
if leftmost == '*':
|
| 125 |
+
# When '*' is a fragment by itself, it matches a non-empty dotless
|
| 126 |
+
# fragment.
|
| 127 |
+
pats.append('[^.]+')
|
| 128 |
+
elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
|
| 129 |
+
# RFC 6125, section 6.4.3, subitem 3.
|
| 130 |
+
# The client SHOULD NOT attempt to match a presented identifier
|
| 131 |
+
# where the wildcard character is embedded within an A-label or
|
| 132 |
+
# U-label of an internationalized domain name.
|
| 133 |
+
pats.append(re.escape(leftmost))
|
| 134 |
+
else:
|
| 135 |
+
# Otherwise, '*' matches any dotless string, e.g. www*
|
| 136 |
+
pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
|
| 137 |
+
|
| 138 |
+
# add the remaining fragments, ignore any wildcards
|
| 139 |
+
for frag in remainder:
|
| 140 |
+
pats.append(re.escape(frag))
|
| 141 |
+
|
| 142 |
+
pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
|
| 143 |
+
return pat.match(hostname)
|
| 144 |
+
|
| 145 |
+
def match_hostname(cert, hostname):
|
| 146 |
+
"""Verify that *cert* (in decoded format as returned by
|
| 147 |
+
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
|
| 148 |
+
rules are followed, but IP addresses are not accepted for *hostname*.
|
| 149 |
+
|
| 150 |
+
CertificateError is raised on failure. On success, the function
|
| 151 |
+
returns nothing.
|
| 152 |
+
"""
|
| 153 |
+
if not cert:
|
| 154 |
+
raise ValueError("empty or no certificate, match_hostname needs a "
|
| 155 |
+
"SSL socket or SSL context with either "
|
| 156 |
+
"CERT_OPTIONAL or CERT_REQUIRED")
|
| 157 |
+
dnsnames = []
|
| 158 |
+
san = cert.get('subjectAltName', ())
|
| 159 |
+
for key, value in san:
|
| 160 |
+
if key == 'DNS':
|
| 161 |
+
if _dnsname_match(value, hostname):
|
| 162 |
+
return
|
| 163 |
+
dnsnames.append(value)
|
| 164 |
+
if not dnsnames:
|
| 165 |
+
# The subject is only checked when there is no dNSName entry
|
| 166 |
+
# in subjectAltName
|
| 167 |
+
for sub in cert.get('subject', ()):
|
| 168 |
+
for key, value in sub:
|
| 169 |
+
# XXX according to RFC 2818, the most specific Common Name
|
| 170 |
+
# must be used.
|
| 171 |
+
if key == 'commonName':
|
| 172 |
+
if _dnsname_match(value, hostname):
|
| 173 |
+
return
|
| 174 |
+
dnsnames.append(value)
|
| 175 |
+
if len(dnsnames) > 1:
|
| 176 |
+
raise CertificateError("hostname %r "
|
| 177 |
+
"doesn't match either of %s" %
|
| 178 |
+
(hostname, ', '.join(map(repr, dnsnames))))
|
| 179 |
+
elif len(dnsnames) == 1:
|
| 180 |
+
raise CertificateError("hostname %r "
|
| 181 |
+
"doesn't match %r" %
|
| 182 |
+
(hostname, dnsnames[0]))
|
| 183 |
+
else:
|
| 184 |
+
raise CertificateError("no appropriate commonName or "
|
| 185 |
+
"subjectAltName fields were found")
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
from types import SimpleNamespace as Container
|
| 190 |
+
except ImportError: # pragma: no cover
|
| 191 |
+
|
| 192 |
+
class Container(object):
|
| 193 |
+
"""
|
| 194 |
+
A generic container for when multiple values need to be returned
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
def __init__(self, **kwargs):
|
| 198 |
+
self.__dict__.update(kwargs)
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
try:
|
| 202 |
+
from shutil import which
|
| 203 |
+
except ImportError: # pragma: no cover
|
| 204 |
+
# Implementation from Python 3.3
|
| 205 |
+
def which(cmd, mode=os.F_OK | os.X_OK, path=None):
|
| 206 |
+
"""Given a command, mode, and a PATH string, return the path which
|
| 207 |
+
conforms to the given mode on the PATH, or None if there is no such
|
| 208 |
+
file.
|
| 209 |
+
|
| 210 |
+
`mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
|
| 211 |
+
of os.environ.get("PATH"), or can be overridden with a custom search
|
| 212 |
+
path.
|
| 213 |
+
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
# Check that a given file can be accessed with the correct mode.
|
| 217 |
+
# Additionally check that `file` is not a directory, as on Windows
|
| 218 |
+
# directories pass the os.access check.
|
| 219 |
+
def _access_check(fn, mode):
|
| 220 |
+
return (os.path.exists(fn) and os.access(fn, mode) and not os.path.isdir(fn))
|
| 221 |
+
|
| 222 |
+
# If we're given a path with a directory part, look it up directly rather
|
| 223 |
+
# than referring to PATH directories. This includes checking relative to the
|
| 224 |
+
# current directory, e.g. ./script
|
| 225 |
+
if os.path.dirname(cmd):
|
| 226 |
+
if _access_check(cmd, mode):
|
| 227 |
+
return cmd
|
| 228 |
+
return None
|
| 229 |
+
|
| 230 |
+
if path is None:
|
| 231 |
+
path = os.environ.get("PATH", os.defpath)
|
| 232 |
+
if not path:
|
| 233 |
+
return None
|
| 234 |
+
path = path.split(os.pathsep)
|
| 235 |
+
|
| 236 |
+
if sys.platform == "win32":
|
| 237 |
+
# The current directory takes precedence on Windows.
|
| 238 |
+
if os.curdir not in path:
|
| 239 |
+
path.insert(0, os.curdir)
|
| 240 |
+
|
| 241 |
+
# PATHEXT is necessary to check on Windows.
|
| 242 |
+
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
|
| 243 |
+
# See if the given file matches any of the expected path extensions.
|
| 244 |
+
# This will allow us to short circuit when given "python.exe".
|
| 245 |
+
# If it does match, only test that one, otherwise we have to try
|
| 246 |
+
# others.
|
| 247 |
+
if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
|
| 248 |
+
files = [cmd]
|
| 249 |
+
else:
|
| 250 |
+
files = [cmd + ext for ext in pathext]
|
| 251 |
+
else:
|
| 252 |
+
# On other platforms you don't have things like PATHEXT to tell you
|
| 253 |
+
# what file suffixes are executable, so just pass on cmd as-is.
|
| 254 |
+
files = [cmd]
|
| 255 |
+
|
| 256 |
+
seen = set()
|
| 257 |
+
for dir in path:
|
| 258 |
+
normdir = os.path.normcase(dir)
|
| 259 |
+
if normdir not in seen:
|
| 260 |
+
seen.add(normdir)
|
| 261 |
+
for thefile in files:
|
| 262 |
+
name = os.path.join(dir, thefile)
|
| 263 |
+
if _access_check(name, mode):
|
| 264 |
+
return name
|
| 265 |
+
return None
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
# ZipFile is a context manager in 2.7, but not in 2.6
|
| 269 |
+
|
| 270 |
+
from zipfile import ZipFile as BaseZipFile
|
| 271 |
+
|
| 272 |
+
if hasattr(BaseZipFile, '__enter__'): # pragma: no cover
|
| 273 |
+
ZipFile = BaseZipFile
|
| 274 |
+
else: # pragma: no cover
|
| 275 |
+
from zipfile import ZipExtFile as BaseZipExtFile
|
| 276 |
+
|
| 277 |
+
class ZipExtFile(BaseZipExtFile):
|
| 278 |
+
|
| 279 |
+
def __init__(self, base):
|
| 280 |
+
self.__dict__.update(base.__dict__)
|
| 281 |
+
|
| 282 |
+
def __enter__(self):
|
| 283 |
+
return self
|
| 284 |
+
|
| 285 |
+
def __exit__(self, *exc_info):
|
| 286 |
+
self.close()
|
| 287 |
+
# return None, so if an exception occurred, it will propagate
|
| 288 |
+
|
| 289 |
+
class ZipFile(BaseZipFile):
|
| 290 |
+
|
| 291 |
+
def __enter__(self):
|
| 292 |
+
return self
|
| 293 |
+
|
| 294 |
+
def __exit__(self, *exc_info):
|
| 295 |
+
self.close()
|
| 296 |
+
# return None, so if an exception occurred, it will propagate
|
| 297 |
+
|
| 298 |
+
def open(self, *args, **kwargs):
|
| 299 |
+
base = BaseZipFile.open(self, *args, **kwargs)
|
| 300 |
+
return ZipExtFile(base)
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
try:
|
| 304 |
+
from platform import python_implementation
|
| 305 |
+
except ImportError: # pragma: no cover
|
| 306 |
+
|
| 307 |
+
def python_implementation():
|
| 308 |
+
"""Return a string identifying the Python implementation."""
|
| 309 |
+
if 'PyPy' in sys.version:
|
| 310 |
+
return 'PyPy'
|
| 311 |
+
if os.name == 'java':
|
| 312 |
+
return 'Jython'
|
| 313 |
+
if sys.version.startswith('IronPython'):
|
| 314 |
+
return 'IronPython'
|
| 315 |
+
return 'CPython'
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
import sysconfig
|
| 319 |
+
|
| 320 |
+
try:
|
| 321 |
+
callable = callable
|
| 322 |
+
except NameError: # pragma: no cover
|
| 323 |
+
from collections.abc import Callable
|
| 324 |
+
|
| 325 |
+
def callable(obj):
|
| 326 |
+
return isinstance(obj, Callable)
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
try:
|
| 330 |
+
fsencode = os.fsencode
|
| 331 |
+
fsdecode = os.fsdecode
|
| 332 |
+
except AttributeError: # pragma: no cover
|
| 333 |
+
# Issue #99: on some systems (e.g. containerised),
|
| 334 |
+
# sys.getfilesystemencoding() returns None, and we need a real value,
|
| 335 |
+
# so fall back to utf-8. From the CPython 2.7 docs relating to Unix and
|
| 336 |
+
# sys.getfilesystemencoding(): the return value is "the user’s preference
|
| 337 |
+
# according to the result of nl_langinfo(CODESET), or None if the
|
| 338 |
+
# nl_langinfo(CODESET) failed."
|
| 339 |
+
_fsencoding = sys.getfilesystemencoding() or 'utf-8'
|
| 340 |
+
if _fsencoding == 'mbcs':
|
| 341 |
+
_fserrors = 'strict'
|
| 342 |
+
else:
|
| 343 |
+
_fserrors = 'surrogateescape'
|
| 344 |
+
|
| 345 |
+
def fsencode(filename):
|
| 346 |
+
if isinstance(filename, bytes):
|
| 347 |
+
return filename
|
| 348 |
+
elif isinstance(filename, text_type):
|
| 349 |
+
return filename.encode(_fsencoding, _fserrors)
|
| 350 |
+
else:
|
| 351 |
+
raise TypeError("expect bytes or str, not %s" %
|
| 352 |
+
type(filename).__name__)
|
| 353 |
+
|
| 354 |
+
def fsdecode(filename):
|
| 355 |
+
if isinstance(filename, text_type):
|
| 356 |
+
return filename
|
| 357 |
+
elif isinstance(filename, bytes):
|
| 358 |
+
return filename.decode(_fsencoding, _fserrors)
|
| 359 |
+
else:
|
| 360 |
+
raise TypeError("expect bytes or str, not %s" %
|
| 361 |
+
type(filename).__name__)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
try:
|
| 365 |
+
from tokenize import detect_encoding
|
| 366 |
+
except ImportError: # pragma: no cover
|
| 367 |
+
from codecs import BOM_UTF8, lookup
|
| 368 |
+
|
| 369 |
+
cookie_re = re.compile(r"coding[:=]\s*([-\w.]+)")
|
| 370 |
+
|
| 371 |
+
def _get_normal_name(orig_enc):
|
| 372 |
+
"""Imitates get_normal_name in tokenizer.c."""
|
| 373 |
+
# Only care about the first 12 characters.
|
| 374 |
+
enc = orig_enc[:12].lower().replace("_", "-")
|
| 375 |
+
if enc == "utf-8" or enc.startswith("utf-8-"):
|
| 376 |
+
return "utf-8"
|
| 377 |
+
if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \
|
| 378 |
+
enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")):
|
| 379 |
+
return "iso-8859-1"
|
| 380 |
+
return orig_enc
|
| 381 |
+
|
| 382 |
+
def detect_encoding(readline):
|
| 383 |
+
"""
|
| 384 |
+
The detect_encoding() function is used to detect the encoding that should
|
| 385 |
+
be used to decode a Python source file. It requires one argument, readline,
|
| 386 |
+
in the same way as the tokenize() generator.
|
| 387 |
+
|
| 388 |
+
It will call readline a maximum of twice, and return the encoding used
|
| 389 |
+
(as a string) and a list of any lines (left as bytes) it has read in.
|
| 390 |
+
|
| 391 |
+
It detects the encoding from the presence of a utf-8 bom or an encoding
|
| 392 |
+
cookie as specified in pep-0263. If both a bom and a cookie are present,
|
| 393 |
+
but disagree, a SyntaxError will be raised. If the encoding cookie is an
|
| 394 |
+
invalid charset, raise a SyntaxError. Note that if a utf-8 bom is found,
|
| 395 |
+
'utf-8-sig' is returned.
|
| 396 |
+
|
| 397 |
+
If no encoding is specified, then the default of 'utf-8' will be returned.
|
| 398 |
+
"""
|
| 399 |
+
try:
|
| 400 |
+
filename = readline.__self__.name
|
| 401 |
+
except AttributeError:
|
| 402 |
+
filename = None
|
| 403 |
+
bom_found = False
|
| 404 |
+
encoding = None
|
| 405 |
+
default = 'utf-8'
|
| 406 |
+
|
| 407 |
+
def read_or_stop():
|
| 408 |
+
try:
|
| 409 |
+
return readline()
|
| 410 |
+
except StopIteration:
|
| 411 |
+
return b''
|
| 412 |
+
|
| 413 |
+
def find_cookie(line):
|
| 414 |
+
try:
|
| 415 |
+
# Decode as UTF-8. Either the line is an encoding declaration,
|
| 416 |
+
# in which case it should be pure ASCII, or it must be UTF-8
|
| 417 |
+
# per default encoding.
|
| 418 |
+
line_string = line.decode('utf-8')
|
| 419 |
+
except UnicodeDecodeError:
|
| 420 |
+
msg = "invalid or missing encoding declaration"
|
| 421 |
+
if filename is not None:
|
| 422 |
+
msg = '{} for {!r}'.format(msg, filename)
|
| 423 |
+
raise SyntaxError(msg)
|
| 424 |
+
|
| 425 |
+
matches = cookie_re.findall(line_string)
|
| 426 |
+
if not matches:
|
| 427 |
+
return None
|
| 428 |
+
encoding = _get_normal_name(matches[0])
|
| 429 |
+
try:
|
| 430 |
+
codec = lookup(encoding)
|
| 431 |
+
except LookupError:
|
| 432 |
+
# This behaviour mimics the Python interpreter
|
| 433 |
+
if filename is None:
|
| 434 |
+
msg = "unknown encoding: " + encoding
|
| 435 |
+
else:
|
| 436 |
+
msg = "unknown encoding for {!r}: {}".format(
|
| 437 |
+
filename, encoding)
|
| 438 |
+
raise SyntaxError(msg)
|
| 439 |
+
|
| 440 |
+
if bom_found:
|
| 441 |
+
if codec.name != 'utf-8':
|
| 442 |
+
# This behaviour mimics the Python interpreter
|
| 443 |
+
if filename is None:
|
| 444 |
+
msg = 'encoding problem: utf-8'
|
| 445 |
+
else:
|
| 446 |
+
msg = 'encoding problem for {!r}: utf-8'.format(
|
| 447 |
+
filename)
|
| 448 |
+
raise SyntaxError(msg)
|
| 449 |
+
encoding += '-sig'
|
| 450 |
+
return encoding
|
| 451 |
+
|
| 452 |
+
first = read_or_stop()
|
| 453 |
+
if first.startswith(BOM_UTF8):
|
| 454 |
+
bom_found = True
|
| 455 |
+
first = first[3:]
|
| 456 |
+
default = 'utf-8-sig'
|
| 457 |
+
if not first:
|
| 458 |
+
return default, []
|
| 459 |
+
|
| 460 |
+
encoding = find_cookie(first)
|
| 461 |
+
if encoding:
|
| 462 |
+
return encoding, [first]
|
| 463 |
+
|
| 464 |
+
second = read_or_stop()
|
| 465 |
+
if not second:
|
| 466 |
+
return default, [first]
|
| 467 |
+
|
| 468 |
+
encoding = find_cookie(second)
|
| 469 |
+
if encoding:
|
| 470 |
+
return encoding, [first, second]
|
| 471 |
+
|
| 472 |
+
return default, [first, second]
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
# For converting & <-> & etc.
|
| 476 |
+
try:
|
| 477 |
+
from html import escape
|
| 478 |
+
except ImportError:
|
| 479 |
+
from cgi import escape
|
| 480 |
+
if sys.version_info[:2] < (3, 4):
|
| 481 |
+
unescape = HTMLParser().unescape
|
| 482 |
+
else:
|
| 483 |
+
from html import unescape
|
| 484 |
+
|
| 485 |
+
try:
|
| 486 |
+
from collections import ChainMap
|
| 487 |
+
except ImportError: # pragma: no cover
|
| 488 |
+
from collections import MutableMapping
|
| 489 |
+
|
| 490 |
+
try:
|
| 491 |
+
from reprlib import recursive_repr as _recursive_repr
|
| 492 |
+
except ImportError:
|
| 493 |
+
|
| 494 |
+
def _recursive_repr(fillvalue='...'):
|
| 495 |
+
'''
|
| 496 |
+
Decorator to make a repr function return fillvalue for a recursive
|
| 497 |
+
call
|
| 498 |
+
'''
|
| 499 |
+
|
| 500 |
+
def decorating_function(user_function):
|
| 501 |
+
repr_running = set()
|
| 502 |
+
|
| 503 |
+
def wrapper(self):
|
| 504 |
+
key = id(self), get_ident()
|
| 505 |
+
if key in repr_running:
|
| 506 |
+
return fillvalue
|
| 507 |
+
repr_running.add(key)
|
| 508 |
+
try:
|
| 509 |
+
result = user_function(self)
|
| 510 |
+
finally:
|
| 511 |
+
repr_running.discard(key)
|
| 512 |
+
return result
|
| 513 |
+
|
| 514 |
+
# Can't use functools.wraps() here because of bootstrap issues
|
| 515 |
+
wrapper.__module__ = getattr(user_function, '__module__')
|
| 516 |
+
wrapper.__doc__ = getattr(user_function, '__doc__')
|
| 517 |
+
wrapper.__name__ = getattr(user_function, '__name__')
|
| 518 |
+
wrapper.__annotations__ = getattr(user_function,
|
| 519 |
+
'__annotations__', {})
|
| 520 |
+
return wrapper
|
| 521 |
+
|
| 522 |
+
return decorating_function
|
| 523 |
+
|
| 524 |
+
class ChainMap(MutableMapping):
|
| 525 |
+
'''
|
| 526 |
+
A ChainMap groups multiple dicts (or other mappings) together
|
| 527 |
+
to create a single, updateable view.
|
| 528 |
+
|
| 529 |
+
The underlying mappings are stored in a list. That list is public and can
|
| 530 |
+
accessed or updated using the *maps* attribute. There is no other state.
|
| 531 |
+
|
| 532 |
+
Lookups search the underlying mappings successively until a key is found.
|
| 533 |
+
In contrast, writes, updates, and deletions only operate on the first
|
| 534 |
+
mapping.
|
| 535 |
+
'''
|
| 536 |
+
|
| 537 |
+
def __init__(self, *maps):
|
| 538 |
+
'''Initialize a ChainMap by setting *maps* to the given mappings.
|
| 539 |
+
If no mappings are provided, a single empty dictionary is used.
|
| 540 |
+
|
| 541 |
+
'''
|
| 542 |
+
self.maps = list(maps) or [{}] # always at least one map
|
| 543 |
+
|
| 544 |
+
def __missing__(self, key):
|
| 545 |
+
raise KeyError(key)
|
| 546 |
+
|
| 547 |
+
def __getitem__(self, key):
|
| 548 |
+
for mapping in self.maps:
|
| 549 |
+
try:
|
| 550 |
+
return mapping[
|
| 551 |
+
key] # can't use 'key in mapping' with defaultdict
|
| 552 |
+
except KeyError:
|
| 553 |
+
pass
|
| 554 |
+
return self.__missing__(
|
| 555 |
+
key) # support subclasses that define __missing__
|
| 556 |
+
|
| 557 |
+
def get(self, key, default=None):
|
| 558 |
+
return self[key] if key in self else default
|
| 559 |
+
|
| 560 |
+
def __len__(self):
|
| 561 |
+
return len(set().union(
|
| 562 |
+
*self.maps)) # reuses stored hash values if possible
|
| 563 |
+
|
| 564 |
+
def __iter__(self):
|
| 565 |
+
return iter(set().union(*self.maps))
|
| 566 |
+
|
| 567 |
+
def __contains__(self, key):
|
| 568 |
+
return any(key in m for m in self.maps)
|
| 569 |
+
|
| 570 |
+
def __bool__(self):
|
| 571 |
+
return any(self.maps)
|
| 572 |
+
|
| 573 |
+
@_recursive_repr()
|
| 574 |
+
def __repr__(self):
|
| 575 |
+
return '{0.__class__.__name__}({1})'.format(
|
| 576 |
+
self, ', '.join(map(repr, self.maps)))
|
| 577 |
+
|
| 578 |
+
@classmethod
|
| 579 |
+
def fromkeys(cls, iterable, *args):
|
| 580 |
+
'Create a ChainMap with a single dict created from the iterable.'
|
| 581 |
+
return cls(dict.fromkeys(iterable, *args))
|
| 582 |
+
|
| 583 |
+
def copy(self):
|
| 584 |
+
'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]'
|
| 585 |
+
return self.__class__(self.maps[0].copy(), *self.maps[1:])
|
| 586 |
+
|
| 587 |
+
__copy__ = copy
|
| 588 |
+
|
| 589 |
+
def new_child(self): # like Django's Context.push()
|
| 590 |
+
'New ChainMap with a new dict followed by all previous maps.'
|
| 591 |
+
return self.__class__({}, *self.maps)
|
| 592 |
+
|
| 593 |
+
@property
|
| 594 |
+
def parents(self): # like Django's Context.pop()
|
| 595 |
+
'New ChainMap from maps[1:].'
|
| 596 |
+
return self.__class__(*self.maps[1:])
|
| 597 |
+
|
| 598 |
+
def __setitem__(self, key, value):
|
| 599 |
+
self.maps[0][key] = value
|
| 600 |
+
|
| 601 |
+
def __delitem__(self, key):
|
| 602 |
+
try:
|
| 603 |
+
del self.maps[0][key]
|
| 604 |
+
except KeyError:
|
| 605 |
+
raise KeyError(
|
| 606 |
+
'Key not found in the first mapping: {!r}'.format(key))
|
| 607 |
+
|
| 608 |
+
def popitem(self):
|
| 609 |
+
'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.'
|
| 610 |
+
try:
|
| 611 |
+
return self.maps[0].popitem()
|
| 612 |
+
except KeyError:
|
| 613 |
+
raise KeyError('No keys found in the first mapping.')
|
| 614 |
+
|
| 615 |
+
def pop(self, key, *args):
|
| 616 |
+
'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].'
|
| 617 |
+
try:
|
| 618 |
+
return self.maps[0].pop(key, *args)
|
| 619 |
+
except KeyError:
|
| 620 |
+
raise KeyError(
|
| 621 |
+
'Key not found in the first mapping: {!r}'.format(key))
|
| 622 |
+
|
| 623 |
+
def clear(self):
|
| 624 |
+
'Clear maps[0], leaving maps[1:] intact.'
|
| 625 |
+
self.maps[0].clear()
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
try:
|
| 629 |
+
from importlib.util import cache_from_source # Python >= 3.4
|
| 630 |
+
except ImportError: # pragma: no cover
|
| 631 |
+
|
| 632 |
+
def cache_from_source(path, debug_override=None):
|
| 633 |
+
assert path.endswith('.py')
|
| 634 |
+
if debug_override is None:
|
| 635 |
+
debug_override = __debug__
|
| 636 |
+
if debug_override:
|
| 637 |
+
suffix = 'c'
|
| 638 |
+
else:
|
| 639 |
+
suffix = 'o'
|
| 640 |
+
return path + suffix
|
| 641 |
+
|
| 642 |
+
|
| 643 |
+
try:
|
| 644 |
+
from collections import OrderedDict
|
| 645 |
+
except ImportError: # pragma: no cover
|
| 646 |
+
# {{{ http://code.activestate.com/recipes/576693/ (r9)
|
| 647 |
+
# Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy.
|
| 648 |
+
# Passes Python2.7's test suite and incorporates all the latest updates.
|
| 649 |
+
try:
|
| 650 |
+
from thread import get_ident as _get_ident
|
| 651 |
+
except ImportError:
|
| 652 |
+
from dummy_thread import get_ident as _get_ident
|
| 653 |
+
|
| 654 |
+
try:
|
| 655 |
+
from _abcoll import KeysView, ValuesView, ItemsView
|
| 656 |
+
except ImportError:
|
| 657 |
+
pass
|
| 658 |
+
|
| 659 |
+
class OrderedDict(dict):
|
| 660 |
+
'Dictionary that remembers insertion order'
|
| 661 |
+
|
| 662 |
+
# An inherited dict maps keys to values.
|
| 663 |
+
# The inherited dict provides __getitem__, __len__, __contains__, and get.
|
| 664 |
+
# The remaining methods are order-aware.
|
| 665 |
+
# Big-O running times for all methods are the same as for regular dictionaries.
|
| 666 |
+
|
| 667 |
+
# The internal self.__map dictionary maps keys to links in a doubly linked list.
|
| 668 |
+
# The circular doubly linked list starts and ends with a sentinel element.
|
| 669 |
+
# The sentinel element never gets deleted (this simplifies the algorithm).
|
| 670 |
+
# Each link is stored as a list of length three: [PREV, NEXT, KEY].
|
| 671 |
+
|
| 672 |
+
def __init__(self, *args, **kwds):
|
| 673 |
+
'''Initialize an ordered dictionary. Signature is the same as for
|
| 674 |
+
regular dictionaries, but keyword arguments are not recommended
|
| 675 |
+
because their insertion order is arbitrary.
|
| 676 |
+
|
| 677 |
+
'''
|
| 678 |
+
if len(args) > 1:
|
| 679 |
+
raise TypeError('expected at most 1 arguments, got %d' %
|
| 680 |
+
len(args))
|
| 681 |
+
try:
|
| 682 |
+
self.__root
|
| 683 |
+
except AttributeError:
|
| 684 |
+
self.__root = root = [] # sentinel node
|
| 685 |
+
root[:] = [root, root, None]
|
| 686 |
+
self.__map = {}
|
| 687 |
+
self.__update(*args, **kwds)
|
| 688 |
+
|
| 689 |
+
def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
|
| 690 |
+
'od.__setitem__(i, y) <==> od[i]=y'
|
| 691 |
+
# Setting a new item creates a new link which goes at the end of the linked
|
| 692 |
+
# list, and the inherited dictionary is updated with the new key/value pair.
|
| 693 |
+
if key not in self:
|
| 694 |
+
root = self.__root
|
| 695 |
+
last = root[0]
|
| 696 |
+
last[1] = root[0] = self.__map[key] = [last, root, key]
|
| 697 |
+
dict_setitem(self, key, value)
|
| 698 |
+
|
| 699 |
+
def __delitem__(self, key, dict_delitem=dict.__delitem__):
|
| 700 |
+
'od.__delitem__(y) <==> del od[y]'
|
| 701 |
+
# Deleting an existing item uses self.__map to find the link which is
|
| 702 |
+
# then removed by updating the links in the predecessor and successor nodes.
|
| 703 |
+
dict_delitem(self, key)
|
| 704 |
+
link_prev, link_next, key = self.__map.pop(key)
|
| 705 |
+
link_prev[1] = link_next
|
| 706 |
+
link_next[0] = link_prev
|
| 707 |
+
|
| 708 |
+
def __iter__(self):
|
| 709 |
+
'od.__iter__() <==> iter(od)'
|
| 710 |
+
root = self.__root
|
| 711 |
+
curr = root[1]
|
| 712 |
+
while curr is not root:
|
| 713 |
+
yield curr[2]
|
| 714 |
+
curr = curr[1]
|
| 715 |
+
|
| 716 |
+
def __reversed__(self):
|
| 717 |
+
'od.__reversed__() <==> reversed(od)'
|
| 718 |
+
root = self.__root
|
| 719 |
+
curr = root[0]
|
| 720 |
+
while curr is not root:
|
| 721 |
+
yield curr[2]
|
| 722 |
+
curr = curr[0]
|
| 723 |
+
|
| 724 |
+
def clear(self):
|
| 725 |
+
'od.clear() -> None. Remove all items from od.'
|
| 726 |
+
try:
|
| 727 |
+
for node in self.__map.itervalues():
|
| 728 |
+
del node[:]
|
| 729 |
+
root = self.__root
|
| 730 |
+
root[:] = [root, root, None]
|
| 731 |
+
self.__map.clear()
|
| 732 |
+
except AttributeError:
|
| 733 |
+
pass
|
| 734 |
+
dict.clear(self)
|
| 735 |
+
|
| 736 |
+
def popitem(self, last=True):
|
| 737 |
+
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
|
| 738 |
+
Pairs are returned in LIFO order if last is true or FIFO order if false.
|
| 739 |
+
|
| 740 |
+
'''
|
| 741 |
+
if not self:
|
| 742 |
+
raise KeyError('dictionary is empty')
|
| 743 |
+
root = self.__root
|
| 744 |
+
if last:
|
| 745 |
+
link = root[0]
|
| 746 |
+
link_prev = link[0]
|
| 747 |
+
link_prev[1] = root
|
| 748 |
+
root[0] = link_prev
|
| 749 |
+
else:
|
| 750 |
+
link = root[1]
|
| 751 |
+
link_next = link[1]
|
| 752 |
+
root[1] = link_next
|
| 753 |
+
link_next[0] = root
|
| 754 |
+
key = link[2]
|
| 755 |
+
del self.__map[key]
|
| 756 |
+
value = dict.pop(self, key)
|
| 757 |
+
return key, value
|
| 758 |
+
|
| 759 |
+
# -- the following methods do not depend on the internal structure --
|
| 760 |
+
|
| 761 |
+
def keys(self):
|
| 762 |
+
'od.keys() -> list of keys in od'
|
| 763 |
+
return list(self)
|
| 764 |
+
|
| 765 |
+
def values(self):
|
| 766 |
+
'od.values() -> list of values in od'
|
| 767 |
+
return [self[key] for key in self]
|
| 768 |
+
|
| 769 |
+
def items(self):
|
| 770 |
+
'od.items() -> list of (key, value) pairs in od'
|
| 771 |
+
return [(key, self[key]) for key in self]
|
| 772 |
+
|
| 773 |
+
def iterkeys(self):
|
| 774 |
+
'od.iterkeys() -> an iterator over the keys in od'
|
| 775 |
+
return iter(self)
|
| 776 |
+
|
| 777 |
+
def itervalues(self):
|
| 778 |
+
'od.itervalues -> an iterator over the values in od'
|
| 779 |
+
for k in self:
|
| 780 |
+
yield self[k]
|
| 781 |
+
|
| 782 |
+
def iteritems(self):
|
| 783 |
+
'od.iteritems -> an iterator over the (key, value) items in od'
|
| 784 |
+
for k in self:
|
| 785 |
+
yield (k, self[k])
|
| 786 |
+
|
| 787 |
+
def update(*args, **kwds):
|
| 788 |
+
'''od.update(E, **F) -> None. Update od from dict/iterable E and F.
|
| 789 |
+
|
| 790 |
+
If E is a dict instance, does: for k in E: od[k] = E[k]
|
| 791 |
+
If E has a .keys() method, does: for k in E.keys(): od[k] = E[k]
|
| 792 |
+
Or if E is an iterable of items, does: for k, v in E: od[k] = v
|
| 793 |
+
In either case, this is followed by: for k, v in F.items(): od[k] = v
|
| 794 |
+
|
| 795 |
+
'''
|
| 796 |
+
if len(args) > 2:
|
| 797 |
+
raise TypeError('update() takes at most 2 positional '
|
| 798 |
+
'arguments (%d given)' % (len(args), ))
|
| 799 |
+
elif not args:
|
| 800 |
+
raise TypeError('update() takes at least 1 argument (0 given)')
|
| 801 |
+
self = args[0]
|
| 802 |
+
# Make progressively weaker assumptions about "other"
|
| 803 |
+
other = ()
|
| 804 |
+
if len(args) == 2:
|
| 805 |
+
other = args[1]
|
| 806 |
+
if isinstance(other, dict):
|
| 807 |
+
for key in other:
|
| 808 |
+
self[key] = other[key]
|
| 809 |
+
elif hasattr(other, 'keys'):
|
| 810 |
+
for key in other.keys():
|
| 811 |
+
self[key] = other[key]
|
| 812 |
+
else:
|
| 813 |
+
for key, value in other:
|
| 814 |
+
self[key] = value
|
| 815 |
+
for key, value in kwds.items():
|
| 816 |
+
self[key] = value
|
| 817 |
+
|
| 818 |
+
__update = update # let subclasses override update without breaking __init__
|
| 819 |
+
|
| 820 |
+
__marker = object()
|
| 821 |
+
|
| 822 |
+
def pop(self, key, default=__marker):
|
| 823 |
+
'''od.pop(k[,d]) -> v, remove specified key and return the corresponding value.
|
| 824 |
+
If key is not found, d is returned if given, otherwise KeyError is raised.
|
| 825 |
+
|
| 826 |
+
'''
|
| 827 |
+
if key in self:
|
| 828 |
+
result = self[key]
|
| 829 |
+
del self[key]
|
| 830 |
+
return result
|
| 831 |
+
if default is self.__marker:
|
| 832 |
+
raise KeyError(key)
|
| 833 |
+
return default
|
| 834 |
+
|
| 835 |
+
def setdefault(self, key, default=None):
|
| 836 |
+
'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
|
| 837 |
+
if key in self:
|
| 838 |
+
return self[key]
|
| 839 |
+
self[key] = default
|
| 840 |
+
return default
|
| 841 |
+
|
| 842 |
+
def __repr__(self, _repr_running=None):
|
| 843 |
+
'od.__repr__() <==> repr(od)'
|
| 844 |
+
if not _repr_running:
|
| 845 |
+
_repr_running = {}
|
| 846 |
+
call_key = id(self), _get_ident()
|
| 847 |
+
if call_key in _repr_running:
|
| 848 |
+
return '...'
|
| 849 |
+
_repr_running[call_key] = 1
|
| 850 |
+
try:
|
| 851 |
+
if not self:
|
| 852 |
+
return '%s()' % (self.__class__.__name__, )
|
| 853 |
+
return '%s(%r)' % (self.__class__.__name__, self.items())
|
| 854 |
+
finally:
|
| 855 |
+
del _repr_running[call_key]
|
| 856 |
+
|
| 857 |
+
def __reduce__(self):
|
| 858 |
+
'Return state information for pickling'
|
| 859 |
+
items = [[k, self[k]] for k in self]
|
| 860 |
+
inst_dict = vars(self).copy()
|
| 861 |
+
for k in vars(OrderedDict()):
|
| 862 |
+
inst_dict.pop(k, None)
|
| 863 |
+
if inst_dict:
|
| 864 |
+
return (self.__class__, (items, ), inst_dict)
|
| 865 |
+
return self.__class__, (items, )
|
| 866 |
+
|
| 867 |
+
def copy(self):
|
| 868 |
+
'od.copy() -> a shallow copy of od'
|
| 869 |
+
return self.__class__(self)
|
| 870 |
+
|
| 871 |
+
@classmethod
|
| 872 |
+
def fromkeys(cls, iterable, value=None):
|
| 873 |
+
'''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
|
| 874 |
+
and values equal to v (which defaults to None).
|
| 875 |
+
|
| 876 |
+
'''
|
| 877 |
+
d = cls()
|
| 878 |
+
for key in iterable:
|
| 879 |
+
d[key] = value
|
| 880 |
+
return d
|
| 881 |
+
|
| 882 |
+
def __eq__(self, other):
|
| 883 |
+
'''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
|
| 884 |
+
while comparison to a regular mapping is order-insensitive.
|
| 885 |
+
|
| 886 |
+
'''
|
| 887 |
+
if isinstance(other, OrderedDict):
|
| 888 |
+
return len(self) == len(
|
| 889 |
+
other) and self.items() == other.items()
|
| 890 |
+
return dict.__eq__(self, other)
|
| 891 |
+
|
| 892 |
+
def __ne__(self, other):
|
| 893 |
+
return not self == other
|
| 894 |
+
|
| 895 |
+
# -- the following methods are only used in Python 2.7 --
|
| 896 |
+
|
| 897 |
+
def viewkeys(self):
|
| 898 |
+
"od.viewkeys() -> a set-like object providing a view on od's keys"
|
| 899 |
+
return KeysView(self)
|
| 900 |
+
|
| 901 |
+
def viewvalues(self):
|
| 902 |
+
"od.viewvalues() -> an object providing a view on od's values"
|
| 903 |
+
return ValuesView(self)
|
| 904 |
+
|
| 905 |
+
def viewitems(self):
|
| 906 |
+
"od.viewitems() -> a set-like object providing a view on od's items"
|
| 907 |
+
return ItemsView(self)
|
| 908 |
+
|
| 909 |
+
|
| 910 |
+
try:
|
| 911 |
+
from logging.config import BaseConfigurator, valid_ident
|
| 912 |
+
except ImportError: # pragma: no cover
|
| 913 |
+
IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
|
| 914 |
+
|
| 915 |
+
def valid_ident(s):
|
| 916 |
+
m = IDENTIFIER.match(s)
|
| 917 |
+
if not m:
|
| 918 |
+
raise ValueError('Not a valid Python identifier: %r' % s)
|
| 919 |
+
return True
|
| 920 |
+
|
| 921 |
+
# The ConvertingXXX classes are wrappers around standard Python containers,
|
| 922 |
+
# and they serve to convert any suitable values in the container. The
|
| 923 |
+
# conversion converts base dicts, lists and tuples to their wrapped
|
| 924 |
+
# equivalents, whereas strings which match a conversion format are converted
|
| 925 |
+
# appropriately.
|
| 926 |
+
#
|
| 927 |
+
# Each wrapper should have a configurator attribute holding the actual
|
| 928 |
+
# configurator to use for conversion.
|
| 929 |
+
|
| 930 |
+
class ConvertingDict(dict):
|
| 931 |
+
"""A converting dictionary wrapper."""
|
| 932 |
+
|
| 933 |
+
def __getitem__(self, key):
|
| 934 |
+
value = dict.__getitem__(self, key)
|
| 935 |
+
result = self.configurator.convert(value)
|
| 936 |
+
# If the converted value is different, save for next time
|
| 937 |
+
if value is not result:
|
| 938 |
+
self[key] = result
|
| 939 |
+
if type(result) in (ConvertingDict, ConvertingList,
|
| 940 |
+
ConvertingTuple):
|
| 941 |
+
result.parent = self
|
| 942 |
+
result.key = key
|
| 943 |
+
return result
|
| 944 |
+
|
| 945 |
+
def get(self, key, default=None):
|
| 946 |
+
value = dict.get(self, key, default)
|
| 947 |
+
result = self.configurator.convert(value)
|
| 948 |
+
# If the converted value is different, save for next time
|
| 949 |
+
if value is not result:
|
| 950 |
+
self[key] = result
|
| 951 |
+
if type(result) in (ConvertingDict, ConvertingList,
|
| 952 |
+
ConvertingTuple):
|
| 953 |
+
result.parent = self
|
| 954 |
+
result.key = key
|
| 955 |
+
return result
|
| 956 |
+
|
| 957 |
+
def pop(self, key, default=None):
|
| 958 |
+
value = dict.pop(self, key, default)
|
| 959 |
+
result = self.configurator.convert(value)
|
| 960 |
+
if value is not result:
|
| 961 |
+
if type(result) in (ConvertingDict, ConvertingList,
|
| 962 |
+
ConvertingTuple):
|
| 963 |
+
result.parent = self
|
| 964 |
+
result.key = key
|
| 965 |
+
return result
|
| 966 |
+
|
| 967 |
+
class ConvertingList(list):
|
| 968 |
+
"""A converting list wrapper."""
|
| 969 |
+
|
| 970 |
+
def __getitem__(self, key):
|
| 971 |
+
value = list.__getitem__(self, key)
|
| 972 |
+
result = self.configurator.convert(value)
|
| 973 |
+
# If the converted value is different, save for next time
|
| 974 |
+
if value is not result:
|
| 975 |
+
self[key] = result
|
| 976 |
+
if type(result) in (ConvertingDict, ConvertingList,
|
| 977 |
+
ConvertingTuple):
|
| 978 |
+
result.parent = self
|
| 979 |
+
result.key = key
|
| 980 |
+
return result
|
| 981 |
+
|
| 982 |
+
def pop(self, idx=-1):
|
| 983 |
+
value = list.pop(self, idx)
|
| 984 |
+
result = self.configurator.convert(value)
|
| 985 |
+
if value is not result:
|
| 986 |
+
if type(result) in (ConvertingDict, ConvertingList,
|
| 987 |
+
ConvertingTuple):
|
| 988 |
+
result.parent = self
|
| 989 |
+
return result
|
| 990 |
+
|
| 991 |
+
class ConvertingTuple(tuple):
|
| 992 |
+
"""A converting tuple wrapper."""
|
| 993 |
+
|
| 994 |
+
def __getitem__(self, key):
|
| 995 |
+
value = tuple.__getitem__(self, key)
|
| 996 |
+
result = self.configurator.convert(value)
|
| 997 |
+
if value is not result:
|
| 998 |
+
if type(result) in (ConvertingDict, ConvertingList,
|
| 999 |
+
ConvertingTuple):
|
| 1000 |
+
result.parent = self
|
| 1001 |
+
result.key = key
|
| 1002 |
+
return result
|
| 1003 |
+
|
| 1004 |
+
class BaseConfigurator(object):
|
| 1005 |
+
"""
|
| 1006 |
+
The configurator base class which defines some useful defaults.
|
| 1007 |
+
"""
|
| 1008 |
+
|
| 1009 |
+
CONVERT_PATTERN = re.compile(r'^(?P<prefix>[a-z]+)://(?P<suffix>.*)$')
|
| 1010 |
+
|
| 1011 |
+
WORD_PATTERN = re.compile(r'^\s*(\w+)\s*')
|
| 1012 |
+
DOT_PATTERN = re.compile(r'^\.\s*(\w+)\s*')
|
| 1013 |
+
INDEX_PATTERN = re.compile(r'^\[\s*(\w+)\s*\]\s*')
|
| 1014 |
+
DIGIT_PATTERN = re.compile(r'^\d+$')
|
| 1015 |
+
|
| 1016 |
+
value_converters = {
|
| 1017 |
+
'ext': 'ext_convert',
|
| 1018 |
+
'cfg': 'cfg_convert',
|
| 1019 |
+
}
|
| 1020 |
+
|
| 1021 |
+
# We might want to use a different one, e.g. importlib
|
| 1022 |
+
importer = staticmethod(__import__)
|
| 1023 |
+
|
| 1024 |
+
def __init__(self, config):
|
| 1025 |
+
self.config = ConvertingDict(config)
|
| 1026 |
+
self.config.configurator = self
|
| 1027 |
+
|
| 1028 |
+
def resolve(self, s):
|
| 1029 |
+
"""
|
| 1030 |
+
Resolve strings to objects using standard import and attribute
|
| 1031 |
+
syntax.
|
| 1032 |
+
"""
|
| 1033 |
+
name = s.split('.')
|
| 1034 |
+
used = name.pop(0)
|
| 1035 |
+
try:
|
| 1036 |
+
found = self.importer(used)
|
| 1037 |
+
for frag in name:
|
| 1038 |
+
used += '.' + frag
|
| 1039 |
+
try:
|
| 1040 |
+
found = getattr(found, frag)
|
| 1041 |
+
except AttributeError:
|
| 1042 |
+
self.importer(used)
|
| 1043 |
+
found = getattr(found, frag)
|
| 1044 |
+
return found
|
| 1045 |
+
except ImportError:
|
| 1046 |
+
e, tb = sys.exc_info()[1:]
|
| 1047 |
+
v = ValueError('Cannot resolve %r: %s' % (s, e))
|
| 1048 |
+
v.__cause__, v.__traceback__ = e, tb
|
| 1049 |
+
raise v
|
| 1050 |
+
|
| 1051 |
+
def ext_convert(self, value):
|
| 1052 |
+
"""Default converter for the ext:// protocol."""
|
| 1053 |
+
return self.resolve(value)
|
| 1054 |
+
|
| 1055 |
+
def cfg_convert(self, value):
|
| 1056 |
+
"""Default converter for the cfg:// protocol."""
|
| 1057 |
+
rest = value
|
| 1058 |
+
m = self.WORD_PATTERN.match(rest)
|
| 1059 |
+
if m is None:
|
| 1060 |
+
raise ValueError("Unable to convert %r" % value)
|
| 1061 |
+
else:
|
| 1062 |
+
rest = rest[m.end():]
|
| 1063 |
+
d = self.config[m.groups()[0]]
|
| 1064 |
+
while rest:
|
| 1065 |
+
m = self.DOT_PATTERN.match(rest)
|
| 1066 |
+
if m:
|
| 1067 |
+
d = d[m.groups()[0]]
|
| 1068 |
+
else:
|
| 1069 |
+
m = self.INDEX_PATTERN.match(rest)
|
| 1070 |
+
if m:
|
| 1071 |
+
idx = m.groups()[0]
|
| 1072 |
+
if not self.DIGIT_PATTERN.match(idx):
|
| 1073 |
+
d = d[idx]
|
| 1074 |
+
else:
|
| 1075 |
+
try:
|
| 1076 |
+
n = int(
|
| 1077 |
+
idx
|
| 1078 |
+
) # try as number first (most likely)
|
| 1079 |
+
d = d[n]
|
| 1080 |
+
except TypeError:
|
| 1081 |
+
d = d[idx]
|
| 1082 |
+
if m:
|
| 1083 |
+
rest = rest[m.end():]
|
| 1084 |
+
else:
|
| 1085 |
+
raise ValueError('Unable to convert '
|
| 1086 |
+
'%r at %r' % (value, rest))
|
| 1087 |
+
# rest should be empty
|
| 1088 |
+
return d
|
| 1089 |
+
|
| 1090 |
+
def convert(self, value):
|
| 1091 |
+
"""
|
| 1092 |
+
Convert values to an appropriate type. dicts, lists and tuples are
|
| 1093 |
+
replaced by their converting alternatives. Strings are checked to
|
| 1094 |
+
see if they have a conversion format and are converted if they do.
|
| 1095 |
+
"""
|
| 1096 |
+
if not isinstance(value, ConvertingDict) and isinstance(
|
| 1097 |
+
value, dict):
|
| 1098 |
+
value = ConvertingDict(value)
|
| 1099 |
+
value.configurator = self
|
| 1100 |
+
elif not isinstance(value, ConvertingList) and isinstance(
|
| 1101 |
+
value, list):
|
| 1102 |
+
value = ConvertingList(value)
|
| 1103 |
+
value.configurator = self
|
| 1104 |
+
elif not isinstance(value, ConvertingTuple) and isinstance(value, tuple):
|
| 1105 |
+
value = ConvertingTuple(value)
|
| 1106 |
+
value.configurator = self
|
| 1107 |
+
elif isinstance(value, string_types):
|
| 1108 |
+
m = self.CONVERT_PATTERN.match(value)
|
| 1109 |
+
if m:
|
| 1110 |
+
d = m.groupdict()
|
| 1111 |
+
prefix = d['prefix']
|
| 1112 |
+
converter = self.value_converters.get(prefix, None)
|
| 1113 |
+
if converter:
|
| 1114 |
+
suffix = d['suffix']
|
| 1115 |
+
converter = getattr(self, converter)
|
| 1116 |
+
value = converter(suffix)
|
| 1117 |
+
return value
|
| 1118 |
+
|
| 1119 |
+
def configure_custom(self, config):
|
| 1120 |
+
"""Configure an object with a user-supplied factory."""
|
| 1121 |
+
c = config.pop('()')
|
| 1122 |
+
if not callable(c):
|
| 1123 |
+
c = self.resolve(c)
|
| 1124 |
+
props = config.pop('.', None)
|
| 1125 |
+
# Check for valid identifiers
|
| 1126 |
+
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
|
| 1127 |
+
result = c(**kwargs)
|
| 1128 |
+
if props:
|
| 1129 |
+
for name, value in props.items():
|
| 1130 |
+
setattr(result, name, value)
|
| 1131 |
+
return result
|
| 1132 |
+
|
| 1133 |
+
def as_tuple(self, value):
|
| 1134 |
+
"""Utility function which converts lists to tuples."""
|
| 1135 |
+
if isinstance(value, list):
|
| 1136 |
+
value = tuple(value)
|
| 1137 |
+
return value
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/database.py
ADDED
|
@@ -0,0 +1,1329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 2 |
+
#
|
| 3 |
+
# Copyright (C) 2012-2023 The Python Software Foundation.
|
| 4 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 5 |
+
#
|
| 6 |
+
"""PEP 376 implementation."""
|
| 7 |
+
|
| 8 |
+
from __future__ import unicode_literals
|
| 9 |
+
|
| 10 |
+
import base64
|
| 11 |
+
import codecs
|
| 12 |
+
import contextlib
|
| 13 |
+
import hashlib
|
| 14 |
+
import logging
|
| 15 |
+
import os
|
| 16 |
+
import posixpath
|
| 17 |
+
import sys
|
| 18 |
+
import zipimport
|
| 19 |
+
|
| 20 |
+
from . import DistlibException, resources
|
| 21 |
+
from .compat import StringIO
|
| 22 |
+
from .version import get_scheme, UnsupportedVersionError
|
| 23 |
+
from .metadata import (Metadata, METADATA_FILENAME, WHEEL_METADATA_FILENAME, LEGACY_METADATA_FILENAME)
|
| 24 |
+
from .util import (parse_requirement, cached_property, parse_name_and_version, read_exports, write_exports, CSVReader,
|
| 25 |
+
CSVWriter)
|
| 26 |
+
|
| 27 |
+
__all__ = [
|
| 28 |
+
'Distribution', 'BaseInstalledDistribution', 'InstalledDistribution', 'EggInfoDistribution', 'DistributionPath'
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
logger = logging.getLogger(__name__)
|
| 32 |
+
|
| 33 |
+
EXPORTS_FILENAME = 'pydist-exports.json'
|
| 34 |
+
COMMANDS_FILENAME = 'pydist-commands.json'
|
| 35 |
+
|
| 36 |
+
DIST_FILES = ('INSTALLER', METADATA_FILENAME, 'RECORD', 'REQUESTED', 'RESOURCES', EXPORTS_FILENAME, 'SHARED')
|
| 37 |
+
|
| 38 |
+
DISTINFO_EXT = '.dist-info'
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class _Cache(object):
|
| 42 |
+
"""
|
| 43 |
+
A simple cache mapping names and .dist-info paths to distributions
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
def __init__(self):
|
| 47 |
+
"""
|
| 48 |
+
Initialise an instance. There is normally one for each DistributionPath.
|
| 49 |
+
"""
|
| 50 |
+
self.name = {}
|
| 51 |
+
self.path = {}
|
| 52 |
+
self.generated = False
|
| 53 |
+
|
| 54 |
+
def clear(self):
|
| 55 |
+
"""
|
| 56 |
+
Clear the cache, setting it to its initial state.
|
| 57 |
+
"""
|
| 58 |
+
self.name.clear()
|
| 59 |
+
self.path.clear()
|
| 60 |
+
self.generated = False
|
| 61 |
+
|
| 62 |
+
def add(self, dist):
|
| 63 |
+
"""
|
| 64 |
+
Add a distribution to the cache.
|
| 65 |
+
:param dist: The distribution to add.
|
| 66 |
+
"""
|
| 67 |
+
if dist.path not in self.path:
|
| 68 |
+
self.path[dist.path] = dist
|
| 69 |
+
self.name.setdefault(dist.key, []).append(dist)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class DistributionPath(object):
|
| 73 |
+
"""
|
| 74 |
+
Represents a set of distributions installed on a path (typically sys.path).
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
def __init__(self, path=None, include_egg=False):
|
| 78 |
+
"""
|
| 79 |
+
Create an instance from a path, optionally including legacy (distutils/
|
| 80 |
+
setuptools/distribute) distributions.
|
| 81 |
+
:param path: The path to use, as a list of directories. If not specified,
|
| 82 |
+
sys.path is used.
|
| 83 |
+
:param include_egg: If True, this instance will look for and return legacy
|
| 84 |
+
distributions as well as those based on PEP 376.
|
| 85 |
+
"""
|
| 86 |
+
if path is None:
|
| 87 |
+
path = sys.path
|
| 88 |
+
self.path = path
|
| 89 |
+
self._include_dist = True
|
| 90 |
+
self._include_egg = include_egg
|
| 91 |
+
|
| 92 |
+
self._cache = _Cache()
|
| 93 |
+
self._cache_egg = _Cache()
|
| 94 |
+
self._cache_enabled = True
|
| 95 |
+
self._scheme = get_scheme('default')
|
| 96 |
+
|
| 97 |
+
def _get_cache_enabled(self):
|
| 98 |
+
return self._cache_enabled
|
| 99 |
+
|
| 100 |
+
def _set_cache_enabled(self, value):
|
| 101 |
+
self._cache_enabled = value
|
| 102 |
+
|
| 103 |
+
cache_enabled = property(_get_cache_enabled, _set_cache_enabled)
|
| 104 |
+
|
| 105 |
+
def clear_cache(self):
|
| 106 |
+
"""
|
| 107 |
+
Clears the internal cache.
|
| 108 |
+
"""
|
| 109 |
+
self._cache.clear()
|
| 110 |
+
self._cache_egg.clear()
|
| 111 |
+
|
| 112 |
+
def _yield_distributions(self):
|
| 113 |
+
"""
|
| 114 |
+
Yield .dist-info and/or .egg(-info) distributions.
|
| 115 |
+
"""
|
| 116 |
+
# We need to check if we've seen some resources already, because on
|
| 117 |
+
# some Linux systems (e.g. some Debian/Ubuntu variants) there are
|
| 118 |
+
# symlinks which alias other files in the environment.
|
| 119 |
+
seen = set()
|
| 120 |
+
for path in self.path:
|
| 121 |
+
finder = resources.finder_for_path(path)
|
| 122 |
+
if finder is None:
|
| 123 |
+
continue
|
| 124 |
+
r = finder.find('')
|
| 125 |
+
if not r or not r.is_container:
|
| 126 |
+
continue
|
| 127 |
+
rset = sorted(r.resources)
|
| 128 |
+
for entry in rset:
|
| 129 |
+
r = finder.find(entry)
|
| 130 |
+
if not r or r.path in seen:
|
| 131 |
+
continue
|
| 132 |
+
try:
|
| 133 |
+
if self._include_dist and entry.endswith(DISTINFO_EXT):
|
| 134 |
+
possible_filenames = [METADATA_FILENAME, WHEEL_METADATA_FILENAME, LEGACY_METADATA_FILENAME]
|
| 135 |
+
for metadata_filename in possible_filenames:
|
| 136 |
+
metadata_path = posixpath.join(entry, metadata_filename)
|
| 137 |
+
pydist = finder.find(metadata_path)
|
| 138 |
+
if pydist:
|
| 139 |
+
break
|
| 140 |
+
else:
|
| 141 |
+
continue
|
| 142 |
+
|
| 143 |
+
with contextlib.closing(pydist.as_stream()) as stream:
|
| 144 |
+
metadata = Metadata(fileobj=stream, scheme='legacy')
|
| 145 |
+
logger.debug('Found %s', r.path)
|
| 146 |
+
seen.add(r.path)
|
| 147 |
+
yield new_dist_class(r.path, metadata=metadata, env=self)
|
| 148 |
+
elif self._include_egg and entry.endswith(('.egg-info', '.egg')):
|
| 149 |
+
logger.debug('Found %s', r.path)
|
| 150 |
+
seen.add(r.path)
|
| 151 |
+
yield old_dist_class(r.path, self)
|
| 152 |
+
except Exception as e:
|
| 153 |
+
msg = 'Unable to read distribution at %s, perhaps due to bad metadata: %s'
|
| 154 |
+
logger.warning(msg, r.path, e)
|
| 155 |
+
import warnings
|
| 156 |
+
warnings.warn(msg % (r.path, e), stacklevel=2)
|
| 157 |
+
|
| 158 |
+
def _generate_cache(self):
|
| 159 |
+
"""
|
| 160 |
+
Scan the path for distributions and populate the cache with
|
| 161 |
+
those that are found.
|
| 162 |
+
"""
|
| 163 |
+
gen_dist = not self._cache.generated
|
| 164 |
+
gen_egg = self._include_egg and not self._cache_egg.generated
|
| 165 |
+
if gen_dist or gen_egg:
|
| 166 |
+
for dist in self._yield_distributions():
|
| 167 |
+
if isinstance(dist, InstalledDistribution):
|
| 168 |
+
self._cache.add(dist)
|
| 169 |
+
else:
|
| 170 |
+
self._cache_egg.add(dist)
|
| 171 |
+
|
| 172 |
+
if gen_dist:
|
| 173 |
+
self._cache.generated = True
|
| 174 |
+
if gen_egg:
|
| 175 |
+
self._cache_egg.generated = True
|
| 176 |
+
|
| 177 |
+
@classmethod
|
| 178 |
+
def distinfo_dirname(cls, name, version):
|
| 179 |
+
"""
|
| 180 |
+
The *name* and *version* parameters are converted into their
|
| 181 |
+
filename-escaped form, i.e. any ``'-'`` characters are replaced
|
| 182 |
+
with ``'_'`` other than the one in ``'dist-info'`` and the one
|
| 183 |
+
separating the name from the version number.
|
| 184 |
+
|
| 185 |
+
:parameter name: is converted to a standard distribution name by replacing
|
| 186 |
+
any runs of non- alphanumeric characters with a single
|
| 187 |
+
``'-'``.
|
| 188 |
+
:type name: string
|
| 189 |
+
:parameter version: is converted to a standard version string. Spaces
|
| 190 |
+
become dots, and all other non-alphanumeric characters
|
| 191 |
+
(except dots) become dashes, with runs of multiple
|
| 192 |
+
dashes condensed to a single dash.
|
| 193 |
+
:type version: string
|
| 194 |
+
:returns: directory name
|
| 195 |
+
:rtype: string"""
|
| 196 |
+
name = name.replace('-', '_')
|
| 197 |
+
return '-'.join([name, version]) + DISTINFO_EXT
|
| 198 |
+
|
| 199 |
+
def get_distributions(self):
|
| 200 |
+
"""
|
| 201 |
+
Provides an iterator that looks for distributions and returns
|
| 202 |
+
:class:`InstalledDistribution` or
|
| 203 |
+
:class:`EggInfoDistribution` instances for each one of them.
|
| 204 |
+
|
| 205 |
+
:rtype: iterator of :class:`InstalledDistribution` and
|
| 206 |
+
:class:`EggInfoDistribution` instances
|
| 207 |
+
"""
|
| 208 |
+
if not self._cache_enabled:
|
| 209 |
+
for dist in self._yield_distributions():
|
| 210 |
+
yield dist
|
| 211 |
+
else:
|
| 212 |
+
self._generate_cache()
|
| 213 |
+
|
| 214 |
+
for dist in self._cache.path.values():
|
| 215 |
+
yield dist
|
| 216 |
+
|
| 217 |
+
if self._include_egg:
|
| 218 |
+
for dist in self._cache_egg.path.values():
|
| 219 |
+
yield dist
|
| 220 |
+
|
| 221 |
+
def get_distribution(self, name):
|
| 222 |
+
"""
|
| 223 |
+
Looks for a named distribution on the path.
|
| 224 |
+
|
| 225 |
+
This function only returns the first result found, as no more than one
|
| 226 |
+
value is expected. If nothing is found, ``None`` is returned.
|
| 227 |
+
|
| 228 |
+
:rtype: :class:`InstalledDistribution`, :class:`EggInfoDistribution`
|
| 229 |
+
or ``None``
|
| 230 |
+
"""
|
| 231 |
+
result = None
|
| 232 |
+
name = name.lower()
|
| 233 |
+
if not self._cache_enabled:
|
| 234 |
+
for dist in self._yield_distributions():
|
| 235 |
+
if dist.key == name:
|
| 236 |
+
result = dist
|
| 237 |
+
break
|
| 238 |
+
else:
|
| 239 |
+
self._generate_cache()
|
| 240 |
+
|
| 241 |
+
if name in self._cache.name:
|
| 242 |
+
result = self._cache.name[name][0]
|
| 243 |
+
elif self._include_egg and name in self._cache_egg.name:
|
| 244 |
+
result = self._cache_egg.name[name][0]
|
| 245 |
+
return result
|
| 246 |
+
|
| 247 |
+
def provides_distribution(self, name, version=None):
|
| 248 |
+
"""
|
| 249 |
+
Iterates over all distributions to find which distributions provide *name*.
|
| 250 |
+
If a *version* is provided, it will be used to filter the results.
|
| 251 |
+
|
| 252 |
+
This function only returns the first result found, since no more than
|
| 253 |
+
one values are expected. If the directory is not found, returns ``None``.
|
| 254 |
+
|
| 255 |
+
:parameter version: a version specifier that indicates the version
|
| 256 |
+
required, conforming to the format in ``PEP-345``
|
| 257 |
+
|
| 258 |
+
:type name: string
|
| 259 |
+
:type version: string
|
| 260 |
+
"""
|
| 261 |
+
matcher = None
|
| 262 |
+
if version is not None:
|
| 263 |
+
try:
|
| 264 |
+
matcher = self._scheme.matcher('%s (%s)' % (name, version))
|
| 265 |
+
except ValueError:
|
| 266 |
+
raise DistlibException('invalid name or version: %r, %r' % (name, version))
|
| 267 |
+
|
| 268 |
+
for dist in self.get_distributions():
|
| 269 |
+
# We hit a problem on Travis where enum34 was installed and doesn't
|
| 270 |
+
# have a provides attribute ...
|
| 271 |
+
if not hasattr(dist, 'provides'):
|
| 272 |
+
logger.debug('No "provides": %s', dist)
|
| 273 |
+
else:
|
| 274 |
+
provided = dist.provides
|
| 275 |
+
|
| 276 |
+
for p in provided:
|
| 277 |
+
p_name, p_ver = parse_name_and_version(p)
|
| 278 |
+
if matcher is None:
|
| 279 |
+
if p_name == name:
|
| 280 |
+
yield dist
|
| 281 |
+
break
|
| 282 |
+
else:
|
| 283 |
+
if p_name == name and matcher.match(p_ver):
|
| 284 |
+
yield dist
|
| 285 |
+
break
|
| 286 |
+
|
| 287 |
+
def get_file_path(self, name, relative_path):
|
| 288 |
+
"""
|
| 289 |
+
Return the path to a resource file.
|
| 290 |
+
"""
|
| 291 |
+
dist = self.get_distribution(name)
|
| 292 |
+
if dist is None:
|
| 293 |
+
raise LookupError('no distribution named %r found' % name)
|
| 294 |
+
return dist.get_resource_path(relative_path)
|
| 295 |
+
|
| 296 |
+
def get_exported_entries(self, category, name=None):
|
| 297 |
+
"""
|
| 298 |
+
Return all of the exported entries in a particular category.
|
| 299 |
+
|
| 300 |
+
:param category: The category to search for entries.
|
| 301 |
+
:param name: If specified, only entries with that name are returned.
|
| 302 |
+
"""
|
| 303 |
+
for dist in self.get_distributions():
|
| 304 |
+
r = dist.exports
|
| 305 |
+
if category in r:
|
| 306 |
+
d = r[category]
|
| 307 |
+
if name is not None:
|
| 308 |
+
if name in d:
|
| 309 |
+
yield d[name]
|
| 310 |
+
else:
|
| 311 |
+
for v in d.values():
|
| 312 |
+
yield v
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
class Distribution(object):
|
| 316 |
+
"""
|
| 317 |
+
A base class for distributions, whether installed or from indexes.
|
| 318 |
+
Either way, it must have some metadata, so that's all that's needed
|
| 319 |
+
for construction.
|
| 320 |
+
"""
|
| 321 |
+
|
| 322 |
+
build_time_dependency = False
|
| 323 |
+
"""
|
| 324 |
+
Set to True if it's known to be only a build-time dependency (i.e.
|
| 325 |
+
not needed after installation).
|
| 326 |
+
"""
|
| 327 |
+
|
| 328 |
+
requested = False
|
| 329 |
+
"""A boolean that indicates whether the ``REQUESTED`` metadata file is
|
| 330 |
+
present (in other words, whether the package was installed by user
|
| 331 |
+
request or it was installed as a dependency)."""
|
| 332 |
+
|
| 333 |
+
def __init__(self, metadata):
|
| 334 |
+
"""
|
| 335 |
+
Initialise an instance.
|
| 336 |
+
:param metadata: The instance of :class:`Metadata` describing this
|
| 337 |
+
distribution.
|
| 338 |
+
"""
|
| 339 |
+
self.metadata = metadata
|
| 340 |
+
self.name = metadata.name
|
| 341 |
+
self.key = self.name.lower() # for case-insensitive comparisons
|
| 342 |
+
self.version = metadata.version
|
| 343 |
+
self.locator = None
|
| 344 |
+
self.digest = None
|
| 345 |
+
self.extras = None # additional features requested
|
| 346 |
+
self.context = None # environment marker overrides
|
| 347 |
+
self.download_urls = set()
|
| 348 |
+
self.digests = {}
|
| 349 |
+
|
| 350 |
+
@property
|
| 351 |
+
def source_url(self):
|
| 352 |
+
"""
|
| 353 |
+
The source archive download URL for this distribution.
|
| 354 |
+
"""
|
| 355 |
+
return self.metadata.source_url
|
| 356 |
+
|
| 357 |
+
download_url = source_url # Backward compatibility
|
| 358 |
+
|
| 359 |
+
@property
|
| 360 |
+
def name_and_version(self):
|
| 361 |
+
"""
|
| 362 |
+
A utility property which displays the name and version in parentheses.
|
| 363 |
+
"""
|
| 364 |
+
return '%s (%s)' % (self.name, self.version)
|
| 365 |
+
|
| 366 |
+
@property
|
| 367 |
+
def provides(self):
|
| 368 |
+
"""
|
| 369 |
+
A set of distribution names and versions provided by this distribution.
|
| 370 |
+
:return: A set of "name (version)" strings.
|
| 371 |
+
"""
|
| 372 |
+
plist = self.metadata.provides
|
| 373 |
+
s = '%s (%s)' % (self.name, self.version)
|
| 374 |
+
if s not in plist:
|
| 375 |
+
plist.append(s)
|
| 376 |
+
return plist
|
| 377 |
+
|
| 378 |
+
def _get_requirements(self, req_attr):
|
| 379 |
+
md = self.metadata
|
| 380 |
+
reqts = getattr(md, req_attr)
|
| 381 |
+
logger.debug('%s: got requirements %r from metadata: %r', self.name, req_attr, reqts)
|
| 382 |
+
return set(md.get_requirements(reqts, extras=self.extras, env=self.context))
|
| 383 |
+
|
| 384 |
+
@property
|
| 385 |
+
def run_requires(self):
|
| 386 |
+
return self._get_requirements('run_requires')
|
| 387 |
+
|
| 388 |
+
@property
|
| 389 |
+
def meta_requires(self):
|
| 390 |
+
return self._get_requirements('meta_requires')
|
| 391 |
+
|
| 392 |
+
@property
|
| 393 |
+
def build_requires(self):
|
| 394 |
+
return self._get_requirements('build_requires')
|
| 395 |
+
|
| 396 |
+
@property
|
| 397 |
+
def test_requires(self):
|
| 398 |
+
return self._get_requirements('test_requires')
|
| 399 |
+
|
| 400 |
+
@property
|
| 401 |
+
def dev_requires(self):
|
| 402 |
+
return self._get_requirements('dev_requires')
|
| 403 |
+
|
| 404 |
+
def matches_requirement(self, req):
|
| 405 |
+
"""
|
| 406 |
+
Say if this instance matches (fulfills) a requirement.
|
| 407 |
+
:param req: The requirement to match.
|
| 408 |
+
:rtype req: str
|
| 409 |
+
:return: True if it matches, else False.
|
| 410 |
+
"""
|
| 411 |
+
# Requirement may contain extras - parse to lose those
|
| 412 |
+
# from what's passed to the matcher
|
| 413 |
+
r = parse_requirement(req)
|
| 414 |
+
scheme = get_scheme(self.metadata.scheme)
|
| 415 |
+
try:
|
| 416 |
+
matcher = scheme.matcher(r.requirement)
|
| 417 |
+
except UnsupportedVersionError:
|
| 418 |
+
# XXX compat-mode if cannot read the version
|
| 419 |
+
logger.warning('could not read version %r - using name only', req)
|
| 420 |
+
name = req.split()[0]
|
| 421 |
+
matcher = scheme.matcher(name)
|
| 422 |
+
|
| 423 |
+
name = matcher.key # case-insensitive
|
| 424 |
+
|
| 425 |
+
result = False
|
| 426 |
+
for p in self.provides:
|
| 427 |
+
p_name, p_ver = parse_name_and_version(p)
|
| 428 |
+
if p_name != name:
|
| 429 |
+
continue
|
| 430 |
+
try:
|
| 431 |
+
result = matcher.match(p_ver)
|
| 432 |
+
break
|
| 433 |
+
except UnsupportedVersionError:
|
| 434 |
+
pass
|
| 435 |
+
return result
|
| 436 |
+
|
| 437 |
+
def __repr__(self):
|
| 438 |
+
"""
|
| 439 |
+
Return a textual representation of this instance,
|
| 440 |
+
"""
|
| 441 |
+
if self.source_url:
|
| 442 |
+
suffix = ' [%s]' % self.source_url
|
| 443 |
+
else:
|
| 444 |
+
suffix = ''
|
| 445 |
+
return '<Distribution %s (%s)%s>' % (self.name, self.version, suffix)
|
| 446 |
+
|
| 447 |
+
def __eq__(self, other):
|
| 448 |
+
"""
|
| 449 |
+
See if this distribution is the same as another.
|
| 450 |
+
:param other: The distribution to compare with. To be equal to one
|
| 451 |
+
another. distributions must have the same type, name,
|
| 452 |
+
version and source_url.
|
| 453 |
+
:return: True if it is the same, else False.
|
| 454 |
+
"""
|
| 455 |
+
if type(other) is not type(self):
|
| 456 |
+
result = False
|
| 457 |
+
else:
|
| 458 |
+
result = (self.name == other.name and self.version == other.version and self.source_url == other.source_url)
|
| 459 |
+
return result
|
| 460 |
+
|
| 461 |
+
def __hash__(self):
|
| 462 |
+
"""
|
| 463 |
+
Compute hash in a way which matches the equality test.
|
| 464 |
+
"""
|
| 465 |
+
return hash(self.name) + hash(self.version) + hash(self.source_url)
|
| 466 |
+
|
| 467 |
+
|
| 468 |
+
class BaseInstalledDistribution(Distribution):
|
| 469 |
+
"""
|
| 470 |
+
This is the base class for installed distributions (whether PEP 376 or
|
| 471 |
+
legacy).
|
| 472 |
+
"""
|
| 473 |
+
|
| 474 |
+
hasher = None
|
| 475 |
+
|
| 476 |
+
def __init__(self, metadata, path, env=None):
|
| 477 |
+
"""
|
| 478 |
+
Initialise an instance.
|
| 479 |
+
:param metadata: An instance of :class:`Metadata` which describes the
|
| 480 |
+
distribution. This will normally have been initialised
|
| 481 |
+
from a metadata file in the ``path``.
|
| 482 |
+
:param path: The path of the ``.dist-info`` or ``.egg-info``
|
| 483 |
+
directory for the distribution.
|
| 484 |
+
:param env: This is normally the :class:`DistributionPath`
|
| 485 |
+
instance where this distribution was found.
|
| 486 |
+
"""
|
| 487 |
+
super(BaseInstalledDistribution, self).__init__(metadata)
|
| 488 |
+
self.path = path
|
| 489 |
+
self.dist_path = env
|
| 490 |
+
|
| 491 |
+
def get_hash(self, data, hasher=None):
|
| 492 |
+
"""
|
| 493 |
+
Get the hash of some data, using a particular hash algorithm, if
|
| 494 |
+
specified.
|
| 495 |
+
|
| 496 |
+
:param data: The data to be hashed.
|
| 497 |
+
:type data: bytes
|
| 498 |
+
:param hasher: The name of a hash implementation, supported by hashlib,
|
| 499 |
+
or ``None``. Examples of valid values are ``'sha1'``,
|
| 500 |
+
``'sha224'``, ``'sha384'``, '``sha256'``, ``'md5'`` and
|
| 501 |
+
``'sha512'``. If no hasher is specified, the ``hasher``
|
| 502 |
+
attribute of the :class:`InstalledDistribution` instance
|
| 503 |
+
is used. If the hasher is determined to be ``None``, MD5
|
| 504 |
+
is used as the hashing algorithm.
|
| 505 |
+
:returns: The hash of the data. If a hasher was explicitly specified,
|
| 506 |
+
the returned hash will be prefixed with the specified hasher
|
| 507 |
+
followed by '='.
|
| 508 |
+
:rtype: str
|
| 509 |
+
"""
|
| 510 |
+
if hasher is None:
|
| 511 |
+
hasher = self.hasher
|
| 512 |
+
if hasher is None:
|
| 513 |
+
hasher = hashlib.md5
|
| 514 |
+
prefix = ''
|
| 515 |
+
else:
|
| 516 |
+
hasher = getattr(hashlib, hasher)
|
| 517 |
+
prefix = '%s=' % self.hasher
|
| 518 |
+
digest = hasher(data).digest()
|
| 519 |
+
digest = base64.urlsafe_b64encode(digest).rstrip(b'=').decode('ascii')
|
| 520 |
+
return '%s%s' % (prefix, digest)
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
class InstalledDistribution(BaseInstalledDistribution):
|
| 524 |
+
"""
|
| 525 |
+
Created with the *path* of the ``.dist-info`` directory provided to the
|
| 526 |
+
constructor. It reads the metadata contained in ``pydist.json`` when it is
|
| 527 |
+
instantiated., or uses a passed in Metadata instance (useful for when
|
| 528 |
+
dry-run mode is being used).
|
| 529 |
+
"""
|
| 530 |
+
|
| 531 |
+
hasher = 'sha256'
|
| 532 |
+
|
| 533 |
+
def __init__(self, path, metadata=None, env=None):
|
| 534 |
+
self.modules = []
|
| 535 |
+
self.finder = finder = resources.finder_for_path(path)
|
| 536 |
+
if finder is None:
|
| 537 |
+
raise ValueError('finder unavailable for %s' % path)
|
| 538 |
+
if env and env._cache_enabled and path in env._cache.path:
|
| 539 |
+
metadata = env._cache.path[path].metadata
|
| 540 |
+
elif metadata is None:
|
| 541 |
+
r = finder.find(METADATA_FILENAME)
|
| 542 |
+
# Temporary - for Wheel 0.23 support
|
| 543 |
+
if r is None:
|
| 544 |
+
r = finder.find(WHEEL_METADATA_FILENAME)
|
| 545 |
+
# Temporary - for legacy support
|
| 546 |
+
if r is None:
|
| 547 |
+
r = finder.find(LEGACY_METADATA_FILENAME)
|
| 548 |
+
if r is None:
|
| 549 |
+
raise ValueError('no %s found in %s' % (METADATA_FILENAME, path))
|
| 550 |
+
with contextlib.closing(r.as_stream()) as stream:
|
| 551 |
+
metadata = Metadata(fileobj=stream, scheme='legacy')
|
| 552 |
+
|
| 553 |
+
super(InstalledDistribution, self).__init__(metadata, path, env)
|
| 554 |
+
|
| 555 |
+
if env and env._cache_enabled:
|
| 556 |
+
env._cache.add(self)
|
| 557 |
+
|
| 558 |
+
r = finder.find('REQUESTED')
|
| 559 |
+
self.requested = r is not None
|
| 560 |
+
p = os.path.join(path, 'top_level.txt')
|
| 561 |
+
if os.path.exists(p):
|
| 562 |
+
with open(p, 'rb') as f:
|
| 563 |
+
data = f.read().decode('utf-8')
|
| 564 |
+
self.modules = data.splitlines()
|
| 565 |
+
|
| 566 |
+
def __repr__(self):
|
| 567 |
+
return '<InstalledDistribution %r %s at %r>' % (self.name, self.version, self.path)
|
| 568 |
+
|
| 569 |
+
def __str__(self):
|
| 570 |
+
return "%s %s" % (self.name, self.version)
|
| 571 |
+
|
| 572 |
+
def _get_records(self):
|
| 573 |
+
"""
|
| 574 |
+
Get the list of installed files for the distribution
|
| 575 |
+
:return: A list of tuples of path, hash and size. Note that hash and
|
| 576 |
+
size might be ``None`` for some entries. The path is exactly
|
| 577 |
+
as stored in the file (which is as in PEP 376).
|
| 578 |
+
"""
|
| 579 |
+
results = []
|
| 580 |
+
r = self.get_distinfo_resource('RECORD')
|
| 581 |
+
with contextlib.closing(r.as_stream()) as stream:
|
| 582 |
+
with CSVReader(stream=stream) as record_reader:
|
| 583 |
+
# Base location is parent dir of .dist-info dir
|
| 584 |
+
# base_location = os.path.dirname(self.path)
|
| 585 |
+
# base_location = os.path.abspath(base_location)
|
| 586 |
+
for row in record_reader:
|
| 587 |
+
missing = [None for i in range(len(row), 3)]
|
| 588 |
+
path, checksum, size = row + missing
|
| 589 |
+
# if not os.path.isabs(path):
|
| 590 |
+
# path = path.replace('/', os.sep)
|
| 591 |
+
# path = os.path.join(base_location, path)
|
| 592 |
+
results.append((path, checksum, size))
|
| 593 |
+
return results
|
| 594 |
+
|
| 595 |
+
@cached_property
|
| 596 |
+
def exports(self):
|
| 597 |
+
"""
|
| 598 |
+
Return the information exported by this distribution.
|
| 599 |
+
:return: A dictionary of exports, mapping an export category to a dict
|
| 600 |
+
of :class:`ExportEntry` instances describing the individual
|
| 601 |
+
export entries, and keyed by name.
|
| 602 |
+
"""
|
| 603 |
+
result = {}
|
| 604 |
+
r = self.get_distinfo_resource(EXPORTS_FILENAME)
|
| 605 |
+
if r:
|
| 606 |
+
result = self.read_exports()
|
| 607 |
+
return result
|
| 608 |
+
|
| 609 |
+
def read_exports(self):
|
| 610 |
+
"""
|
| 611 |
+
Read exports data from a file in .ini format.
|
| 612 |
+
|
| 613 |
+
:return: A dictionary of exports, mapping an export category to a list
|
| 614 |
+
of :class:`ExportEntry` instances describing the individual
|
| 615 |
+
export entries.
|
| 616 |
+
"""
|
| 617 |
+
result = {}
|
| 618 |
+
r = self.get_distinfo_resource(EXPORTS_FILENAME)
|
| 619 |
+
if r:
|
| 620 |
+
with contextlib.closing(r.as_stream()) as stream:
|
| 621 |
+
result = read_exports(stream)
|
| 622 |
+
return result
|
| 623 |
+
|
| 624 |
+
def write_exports(self, exports):
|
| 625 |
+
"""
|
| 626 |
+
Write a dictionary of exports to a file in .ini format.
|
| 627 |
+
:param exports: A dictionary of exports, mapping an export category to
|
| 628 |
+
a list of :class:`ExportEntry` instances describing the
|
| 629 |
+
individual export entries.
|
| 630 |
+
"""
|
| 631 |
+
rf = self.get_distinfo_file(EXPORTS_FILENAME)
|
| 632 |
+
with open(rf, 'w') as f:
|
| 633 |
+
write_exports(exports, f)
|
| 634 |
+
|
| 635 |
+
def get_resource_path(self, relative_path):
|
| 636 |
+
"""
|
| 637 |
+
NOTE: This API may change in the future.
|
| 638 |
+
|
| 639 |
+
Return the absolute path to a resource file with the given relative
|
| 640 |
+
path.
|
| 641 |
+
|
| 642 |
+
:param relative_path: The path, relative to .dist-info, of the resource
|
| 643 |
+
of interest.
|
| 644 |
+
:return: The absolute path where the resource is to be found.
|
| 645 |
+
"""
|
| 646 |
+
r = self.get_distinfo_resource('RESOURCES')
|
| 647 |
+
with contextlib.closing(r.as_stream()) as stream:
|
| 648 |
+
with CSVReader(stream=stream) as resources_reader:
|
| 649 |
+
for relative, destination in resources_reader:
|
| 650 |
+
if relative == relative_path:
|
| 651 |
+
return destination
|
| 652 |
+
raise KeyError('no resource file with relative path %r '
|
| 653 |
+
'is installed' % relative_path)
|
| 654 |
+
|
| 655 |
+
def list_installed_files(self):
|
| 656 |
+
"""
|
| 657 |
+
Iterates over the ``RECORD`` entries and returns a tuple
|
| 658 |
+
``(path, hash, size)`` for each line.
|
| 659 |
+
|
| 660 |
+
:returns: iterator of (path, hash, size)
|
| 661 |
+
"""
|
| 662 |
+
for result in self._get_records():
|
| 663 |
+
yield result
|
| 664 |
+
|
| 665 |
+
def write_installed_files(self, paths, prefix, dry_run=False):
|
| 666 |
+
"""
|
| 667 |
+
Writes the ``RECORD`` file, using the ``paths`` iterable passed in. Any
|
| 668 |
+
existing ``RECORD`` file is silently overwritten.
|
| 669 |
+
|
| 670 |
+
prefix is used to determine when to write absolute paths.
|
| 671 |
+
"""
|
| 672 |
+
prefix = os.path.join(prefix, '')
|
| 673 |
+
base = os.path.dirname(self.path)
|
| 674 |
+
base_under_prefix = base.startswith(prefix)
|
| 675 |
+
base = os.path.join(base, '')
|
| 676 |
+
record_path = self.get_distinfo_file('RECORD')
|
| 677 |
+
logger.info('creating %s', record_path)
|
| 678 |
+
if dry_run:
|
| 679 |
+
return None
|
| 680 |
+
with CSVWriter(record_path) as writer:
|
| 681 |
+
for path in paths:
|
| 682 |
+
if os.path.isdir(path) or path.endswith(('.pyc', '.pyo')):
|
| 683 |
+
# do not put size and hash, as in PEP-376
|
| 684 |
+
hash_value = size = ''
|
| 685 |
+
else:
|
| 686 |
+
size = '%d' % os.path.getsize(path)
|
| 687 |
+
with open(path, 'rb') as fp:
|
| 688 |
+
hash_value = self.get_hash(fp.read())
|
| 689 |
+
if path.startswith(base) or (base_under_prefix and path.startswith(prefix)):
|
| 690 |
+
path = os.path.relpath(path, base)
|
| 691 |
+
writer.writerow((path, hash_value, size))
|
| 692 |
+
|
| 693 |
+
# add the RECORD file itself
|
| 694 |
+
if record_path.startswith(base):
|
| 695 |
+
record_path = os.path.relpath(record_path, base)
|
| 696 |
+
writer.writerow((record_path, '', ''))
|
| 697 |
+
return record_path
|
| 698 |
+
|
| 699 |
+
def check_installed_files(self):
|
| 700 |
+
"""
|
| 701 |
+
Checks that the hashes and sizes of the files in ``RECORD`` are
|
| 702 |
+
matched by the files themselves. Returns a (possibly empty) list of
|
| 703 |
+
mismatches. Each entry in the mismatch list will be a tuple consisting
|
| 704 |
+
of the path, 'exists', 'size' or 'hash' according to what didn't match
|
| 705 |
+
(existence is checked first, then size, then hash), the expected
|
| 706 |
+
value and the actual value.
|
| 707 |
+
"""
|
| 708 |
+
mismatches = []
|
| 709 |
+
base = os.path.dirname(self.path)
|
| 710 |
+
record_path = self.get_distinfo_file('RECORD')
|
| 711 |
+
for path, hash_value, size in self.list_installed_files():
|
| 712 |
+
if not os.path.isabs(path):
|
| 713 |
+
path = os.path.join(base, path)
|
| 714 |
+
if path == record_path:
|
| 715 |
+
continue
|
| 716 |
+
if not os.path.exists(path):
|
| 717 |
+
mismatches.append((path, 'exists', True, False))
|
| 718 |
+
elif os.path.isfile(path):
|
| 719 |
+
actual_size = str(os.path.getsize(path))
|
| 720 |
+
if size and actual_size != size:
|
| 721 |
+
mismatches.append((path, 'size', size, actual_size))
|
| 722 |
+
elif hash_value:
|
| 723 |
+
if '=' in hash_value:
|
| 724 |
+
hasher = hash_value.split('=', 1)[0]
|
| 725 |
+
else:
|
| 726 |
+
hasher = None
|
| 727 |
+
|
| 728 |
+
with open(path, 'rb') as f:
|
| 729 |
+
actual_hash = self.get_hash(f.read(), hasher)
|
| 730 |
+
if actual_hash != hash_value:
|
| 731 |
+
mismatches.append((path, 'hash', hash_value, actual_hash))
|
| 732 |
+
return mismatches
|
| 733 |
+
|
| 734 |
+
@cached_property
|
| 735 |
+
def shared_locations(self):
|
| 736 |
+
"""
|
| 737 |
+
A dictionary of shared locations whose keys are in the set 'prefix',
|
| 738 |
+
'purelib', 'platlib', 'scripts', 'headers', 'data' and 'namespace'.
|
| 739 |
+
The corresponding value is the absolute path of that category for
|
| 740 |
+
this distribution, and takes into account any paths selected by the
|
| 741 |
+
user at installation time (e.g. via command-line arguments). In the
|
| 742 |
+
case of the 'namespace' key, this would be a list of absolute paths
|
| 743 |
+
for the roots of namespace packages in this distribution.
|
| 744 |
+
|
| 745 |
+
The first time this property is accessed, the relevant information is
|
| 746 |
+
read from the SHARED file in the .dist-info directory.
|
| 747 |
+
"""
|
| 748 |
+
result = {}
|
| 749 |
+
shared_path = os.path.join(self.path, 'SHARED')
|
| 750 |
+
if os.path.isfile(shared_path):
|
| 751 |
+
with codecs.open(shared_path, 'r', encoding='utf-8') as f:
|
| 752 |
+
lines = f.read().splitlines()
|
| 753 |
+
for line in lines:
|
| 754 |
+
key, value = line.split('=', 1)
|
| 755 |
+
if key == 'namespace':
|
| 756 |
+
result.setdefault(key, []).append(value)
|
| 757 |
+
else:
|
| 758 |
+
result[key] = value
|
| 759 |
+
return result
|
| 760 |
+
|
| 761 |
+
def write_shared_locations(self, paths, dry_run=False):
|
| 762 |
+
"""
|
| 763 |
+
Write shared location information to the SHARED file in .dist-info.
|
| 764 |
+
:param paths: A dictionary as described in the documentation for
|
| 765 |
+
:meth:`shared_locations`.
|
| 766 |
+
:param dry_run: If True, the action is logged but no file is actually
|
| 767 |
+
written.
|
| 768 |
+
:return: The path of the file written to.
|
| 769 |
+
"""
|
| 770 |
+
shared_path = os.path.join(self.path, 'SHARED')
|
| 771 |
+
logger.info('creating %s', shared_path)
|
| 772 |
+
if dry_run:
|
| 773 |
+
return None
|
| 774 |
+
lines = []
|
| 775 |
+
for key in ('prefix', 'lib', 'headers', 'scripts', 'data'):
|
| 776 |
+
path = paths[key]
|
| 777 |
+
if os.path.isdir(paths[key]):
|
| 778 |
+
lines.append('%s=%s' % (key, path))
|
| 779 |
+
for ns in paths.get('namespace', ()):
|
| 780 |
+
lines.append('namespace=%s' % ns)
|
| 781 |
+
|
| 782 |
+
with codecs.open(shared_path, 'w', encoding='utf-8') as f:
|
| 783 |
+
f.write('\n'.join(lines))
|
| 784 |
+
return shared_path
|
| 785 |
+
|
| 786 |
+
def get_distinfo_resource(self, path):
|
| 787 |
+
if path not in DIST_FILES:
|
| 788 |
+
raise DistlibException('invalid path for a dist-info file: '
|
| 789 |
+
'%r at %r' % (path, self.path))
|
| 790 |
+
finder = resources.finder_for_path(self.path)
|
| 791 |
+
if finder is None:
|
| 792 |
+
raise DistlibException('Unable to get a finder for %s' % self.path)
|
| 793 |
+
return finder.find(path)
|
| 794 |
+
|
| 795 |
+
def get_distinfo_file(self, path):
|
| 796 |
+
"""
|
| 797 |
+
Returns a path located under the ``.dist-info`` directory. Returns a
|
| 798 |
+
string representing the path.
|
| 799 |
+
|
| 800 |
+
:parameter path: a ``'/'``-separated path relative to the
|
| 801 |
+
``.dist-info`` directory or an absolute path;
|
| 802 |
+
If *path* is an absolute path and doesn't start
|
| 803 |
+
with the ``.dist-info`` directory path,
|
| 804 |
+
a :class:`DistlibException` is raised
|
| 805 |
+
:type path: str
|
| 806 |
+
:rtype: str
|
| 807 |
+
"""
|
| 808 |
+
# Check if it is an absolute path # XXX use relpath, add tests
|
| 809 |
+
if path.find(os.sep) >= 0:
|
| 810 |
+
# it's an absolute path?
|
| 811 |
+
distinfo_dirname, path = path.split(os.sep)[-2:]
|
| 812 |
+
if distinfo_dirname != self.path.split(os.sep)[-1]:
|
| 813 |
+
raise DistlibException('dist-info file %r does not belong to the %r %s '
|
| 814 |
+
'distribution' % (path, self.name, self.version))
|
| 815 |
+
|
| 816 |
+
# The file must be relative
|
| 817 |
+
if path not in DIST_FILES:
|
| 818 |
+
raise DistlibException('invalid path for a dist-info file: '
|
| 819 |
+
'%r at %r' % (path, self.path))
|
| 820 |
+
|
| 821 |
+
return os.path.join(self.path, path)
|
| 822 |
+
|
| 823 |
+
def list_distinfo_files(self):
|
| 824 |
+
"""
|
| 825 |
+
Iterates over the ``RECORD`` entries and returns paths for each line if
|
| 826 |
+
the path is pointing to a file located in the ``.dist-info`` directory
|
| 827 |
+
or one of its subdirectories.
|
| 828 |
+
|
| 829 |
+
:returns: iterator of paths
|
| 830 |
+
"""
|
| 831 |
+
base = os.path.dirname(self.path)
|
| 832 |
+
for path, checksum, size in self._get_records():
|
| 833 |
+
# XXX add separator or use real relpath algo
|
| 834 |
+
if not os.path.isabs(path):
|
| 835 |
+
path = os.path.join(base, path)
|
| 836 |
+
if path.startswith(self.path):
|
| 837 |
+
yield path
|
| 838 |
+
|
| 839 |
+
def __eq__(self, other):
|
| 840 |
+
return (isinstance(other, InstalledDistribution) and self.path == other.path)
|
| 841 |
+
|
| 842 |
+
# See http://docs.python.org/reference/datamodel#object.__hash__
|
| 843 |
+
__hash__ = object.__hash__
|
| 844 |
+
|
| 845 |
+
|
| 846 |
+
class EggInfoDistribution(BaseInstalledDistribution):
|
| 847 |
+
"""Created with the *path* of the ``.egg-info`` directory or file provided
|
| 848 |
+
to the constructor. It reads the metadata contained in the file itself, or
|
| 849 |
+
if the given path happens to be a directory, the metadata is read from the
|
| 850 |
+
file ``PKG-INFO`` under that directory."""
|
| 851 |
+
|
| 852 |
+
requested = True # as we have no way of knowing, assume it was
|
| 853 |
+
shared_locations = {}
|
| 854 |
+
|
| 855 |
+
def __init__(self, path, env=None):
|
| 856 |
+
|
| 857 |
+
def set_name_and_version(s, n, v):
|
| 858 |
+
s.name = n
|
| 859 |
+
s.key = n.lower() # for case-insensitive comparisons
|
| 860 |
+
s.version = v
|
| 861 |
+
|
| 862 |
+
self.path = path
|
| 863 |
+
self.dist_path = env
|
| 864 |
+
if env and env._cache_enabled and path in env._cache_egg.path:
|
| 865 |
+
metadata = env._cache_egg.path[path].metadata
|
| 866 |
+
set_name_and_version(self, metadata.name, metadata.version)
|
| 867 |
+
else:
|
| 868 |
+
metadata = self._get_metadata(path)
|
| 869 |
+
|
| 870 |
+
# Need to be set before caching
|
| 871 |
+
set_name_and_version(self, metadata.name, metadata.version)
|
| 872 |
+
|
| 873 |
+
if env and env._cache_enabled:
|
| 874 |
+
env._cache_egg.add(self)
|
| 875 |
+
super(EggInfoDistribution, self).__init__(metadata, path, env)
|
| 876 |
+
|
| 877 |
+
def _get_metadata(self, path):
|
| 878 |
+
requires = None
|
| 879 |
+
|
| 880 |
+
def parse_requires_data(data):
|
| 881 |
+
"""Create a list of dependencies from a requires.txt file.
|
| 882 |
+
|
| 883 |
+
*data*: the contents of a setuptools-produced requires.txt file.
|
| 884 |
+
"""
|
| 885 |
+
reqs = []
|
| 886 |
+
lines = data.splitlines()
|
| 887 |
+
for line in lines:
|
| 888 |
+
line = line.strip()
|
| 889 |
+
# sectioned files have bare newlines (separating sections)
|
| 890 |
+
if not line: # pragma: no cover
|
| 891 |
+
continue
|
| 892 |
+
if line.startswith('['): # pragma: no cover
|
| 893 |
+
logger.warning('Unexpected line: quitting requirement scan: %r', line)
|
| 894 |
+
break
|
| 895 |
+
r = parse_requirement(line)
|
| 896 |
+
if not r: # pragma: no cover
|
| 897 |
+
logger.warning('Not recognised as a requirement: %r', line)
|
| 898 |
+
continue
|
| 899 |
+
if r.extras: # pragma: no cover
|
| 900 |
+
logger.warning('extra requirements in requires.txt are '
|
| 901 |
+
'not supported')
|
| 902 |
+
if not r.constraints:
|
| 903 |
+
reqs.append(r.name)
|
| 904 |
+
else:
|
| 905 |
+
cons = ', '.join('%s%s' % c for c in r.constraints)
|
| 906 |
+
reqs.append('%s (%s)' % (r.name, cons))
|
| 907 |
+
return reqs
|
| 908 |
+
|
| 909 |
+
def parse_requires_path(req_path):
|
| 910 |
+
"""Create a list of dependencies from a requires.txt file.
|
| 911 |
+
|
| 912 |
+
*req_path*: the path to a setuptools-produced requires.txt file.
|
| 913 |
+
"""
|
| 914 |
+
|
| 915 |
+
reqs = []
|
| 916 |
+
try:
|
| 917 |
+
with codecs.open(req_path, 'r', 'utf-8') as fp:
|
| 918 |
+
reqs = parse_requires_data(fp.read())
|
| 919 |
+
except IOError:
|
| 920 |
+
pass
|
| 921 |
+
return reqs
|
| 922 |
+
|
| 923 |
+
tl_path = tl_data = None
|
| 924 |
+
if path.endswith('.egg'):
|
| 925 |
+
if os.path.isdir(path):
|
| 926 |
+
p = os.path.join(path, 'EGG-INFO')
|
| 927 |
+
meta_path = os.path.join(p, 'PKG-INFO')
|
| 928 |
+
metadata = Metadata(path=meta_path, scheme='legacy')
|
| 929 |
+
req_path = os.path.join(p, 'requires.txt')
|
| 930 |
+
tl_path = os.path.join(p, 'top_level.txt')
|
| 931 |
+
requires = parse_requires_path(req_path)
|
| 932 |
+
else:
|
| 933 |
+
# FIXME handle the case where zipfile is not available
|
| 934 |
+
zipf = zipimport.zipimporter(path)
|
| 935 |
+
fileobj = StringIO(zipf.get_data('EGG-INFO/PKG-INFO').decode('utf8'))
|
| 936 |
+
metadata = Metadata(fileobj=fileobj, scheme='legacy')
|
| 937 |
+
try:
|
| 938 |
+
data = zipf.get_data('EGG-INFO/requires.txt')
|
| 939 |
+
tl_data = zipf.get_data('EGG-INFO/top_level.txt').decode('utf-8')
|
| 940 |
+
requires = parse_requires_data(data.decode('utf-8'))
|
| 941 |
+
except IOError:
|
| 942 |
+
requires = None
|
| 943 |
+
elif path.endswith('.egg-info'):
|
| 944 |
+
if os.path.isdir(path):
|
| 945 |
+
req_path = os.path.join(path, 'requires.txt')
|
| 946 |
+
requires = parse_requires_path(req_path)
|
| 947 |
+
path = os.path.join(path, 'PKG-INFO')
|
| 948 |
+
tl_path = os.path.join(path, 'top_level.txt')
|
| 949 |
+
metadata = Metadata(path=path, scheme='legacy')
|
| 950 |
+
else:
|
| 951 |
+
raise DistlibException('path must end with .egg-info or .egg, '
|
| 952 |
+
'got %r' % path)
|
| 953 |
+
|
| 954 |
+
if requires:
|
| 955 |
+
metadata.add_requirements(requires)
|
| 956 |
+
# look for top-level modules in top_level.txt, if present
|
| 957 |
+
if tl_data is None:
|
| 958 |
+
if tl_path is not None and os.path.exists(tl_path):
|
| 959 |
+
with open(tl_path, 'rb') as f:
|
| 960 |
+
tl_data = f.read().decode('utf-8')
|
| 961 |
+
if not tl_data:
|
| 962 |
+
tl_data = []
|
| 963 |
+
else:
|
| 964 |
+
tl_data = tl_data.splitlines()
|
| 965 |
+
self.modules = tl_data
|
| 966 |
+
return metadata
|
| 967 |
+
|
| 968 |
+
def __repr__(self):
|
| 969 |
+
return '<EggInfoDistribution %r %s at %r>' % (self.name, self.version, self.path)
|
| 970 |
+
|
| 971 |
+
def __str__(self):
|
| 972 |
+
return "%s %s" % (self.name, self.version)
|
| 973 |
+
|
| 974 |
+
def check_installed_files(self):
|
| 975 |
+
"""
|
| 976 |
+
Checks that the hashes and sizes of the files in ``RECORD`` are
|
| 977 |
+
matched by the files themselves. Returns a (possibly empty) list of
|
| 978 |
+
mismatches. Each entry in the mismatch list will be a tuple consisting
|
| 979 |
+
of the path, 'exists', 'size' or 'hash' according to what didn't match
|
| 980 |
+
(existence is checked first, then size, then hash), the expected
|
| 981 |
+
value and the actual value.
|
| 982 |
+
"""
|
| 983 |
+
mismatches = []
|
| 984 |
+
record_path = os.path.join(self.path, 'installed-files.txt')
|
| 985 |
+
if os.path.exists(record_path):
|
| 986 |
+
for path, _, _ in self.list_installed_files():
|
| 987 |
+
if path == record_path:
|
| 988 |
+
continue
|
| 989 |
+
if not os.path.exists(path):
|
| 990 |
+
mismatches.append((path, 'exists', True, False))
|
| 991 |
+
return mismatches
|
| 992 |
+
|
| 993 |
+
def list_installed_files(self):
|
| 994 |
+
"""
|
| 995 |
+
Iterates over the ``installed-files.txt`` entries and returns a tuple
|
| 996 |
+
``(path, hash, size)`` for each line.
|
| 997 |
+
|
| 998 |
+
:returns: a list of (path, hash, size)
|
| 999 |
+
"""
|
| 1000 |
+
|
| 1001 |
+
def _md5(path):
|
| 1002 |
+
f = open(path, 'rb')
|
| 1003 |
+
try:
|
| 1004 |
+
content = f.read()
|
| 1005 |
+
finally:
|
| 1006 |
+
f.close()
|
| 1007 |
+
return hashlib.md5(content).hexdigest()
|
| 1008 |
+
|
| 1009 |
+
def _size(path):
|
| 1010 |
+
return os.stat(path).st_size
|
| 1011 |
+
|
| 1012 |
+
record_path = os.path.join(self.path, 'installed-files.txt')
|
| 1013 |
+
result = []
|
| 1014 |
+
if os.path.exists(record_path):
|
| 1015 |
+
with codecs.open(record_path, 'r', encoding='utf-8') as f:
|
| 1016 |
+
for line in f:
|
| 1017 |
+
line = line.strip()
|
| 1018 |
+
p = os.path.normpath(os.path.join(self.path, line))
|
| 1019 |
+
# "./" is present as a marker between installed files
|
| 1020 |
+
# and installation metadata files
|
| 1021 |
+
if not os.path.exists(p):
|
| 1022 |
+
logger.warning('Non-existent file: %s', p)
|
| 1023 |
+
if p.endswith(('.pyc', '.pyo')):
|
| 1024 |
+
continue
|
| 1025 |
+
# otherwise fall through and fail
|
| 1026 |
+
if not os.path.isdir(p):
|
| 1027 |
+
result.append((p, _md5(p), _size(p)))
|
| 1028 |
+
result.append((record_path, None, None))
|
| 1029 |
+
return result
|
| 1030 |
+
|
| 1031 |
+
def list_distinfo_files(self, absolute=False):
|
| 1032 |
+
"""
|
| 1033 |
+
Iterates over the ``installed-files.txt`` entries and returns paths for
|
| 1034 |
+
each line if the path is pointing to a file located in the
|
| 1035 |
+
``.egg-info`` directory or one of its subdirectories.
|
| 1036 |
+
|
| 1037 |
+
:parameter absolute: If *absolute* is ``True``, each returned path is
|
| 1038 |
+
transformed into a local absolute path. Otherwise the
|
| 1039 |
+
raw value from ``installed-files.txt`` is returned.
|
| 1040 |
+
:type absolute: boolean
|
| 1041 |
+
:returns: iterator of paths
|
| 1042 |
+
"""
|
| 1043 |
+
record_path = os.path.join(self.path, 'installed-files.txt')
|
| 1044 |
+
if os.path.exists(record_path):
|
| 1045 |
+
skip = True
|
| 1046 |
+
with codecs.open(record_path, 'r', encoding='utf-8') as f:
|
| 1047 |
+
for line in f:
|
| 1048 |
+
line = line.strip()
|
| 1049 |
+
if line == './':
|
| 1050 |
+
skip = False
|
| 1051 |
+
continue
|
| 1052 |
+
if not skip:
|
| 1053 |
+
p = os.path.normpath(os.path.join(self.path, line))
|
| 1054 |
+
if p.startswith(self.path):
|
| 1055 |
+
if absolute:
|
| 1056 |
+
yield p
|
| 1057 |
+
else:
|
| 1058 |
+
yield line
|
| 1059 |
+
|
| 1060 |
+
def __eq__(self, other):
|
| 1061 |
+
return (isinstance(other, EggInfoDistribution) and self.path == other.path)
|
| 1062 |
+
|
| 1063 |
+
# See http://docs.python.org/reference/datamodel#object.__hash__
|
| 1064 |
+
__hash__ = object.__hash__
|
| 1065 |
+
|
| 1066 |
+
|
| 1067 |
+
new_dist_class = InstalledDistribution
|
| 1068 |
+
old_dist_class = EggInfoDistribution
|
| 1069 |
+
|
| 1070 |
+
|
| 1071 |
+
class DependencyGraph(object):
|
| 1072 |
+
"""
|
| 1073 |
+
Represents a dependency graph between distributions.
|
| 1074 |
+
|
| 1075 |
+
The dependency relationships are stored in an ``adjacency_list`` that maps
|
| 1076 |
+
distributions to a list of ``(other, label)`` tuples where ``other``
|
| 1077 |
+
is a distribution and the edge is labeled with ``label`` (i.e. the version
|
| 1078 |
+
specifier, if such was provided). Also, for more efficient traversal, for
|
| 1079 |
+
every distribution ``x``, a list of predecessors is kept in
|
| 1080 |
+
``reverse_list[x]``. An edge from distribution ``a`` to
|
| 1081 |
+
distribution ``b`` means that ``a`` depends on ``b``. If any missing
|
| 1082 |
+
dependencies are found, they are stored in ``missing``, which is a
|
| 1083 |
+
dictionary that maps distributions to a list of requirements that were not
|
| 1084 |
+
provided by any other distributions.
|
| 1085 |
+
"""
|
| 1086 |
+
|
| 1087 |
+
def __init__(self):
|
| 1088 |
+
self.adjacency_list = {}
|
| 1089 |
+
self.reverse_list = {}
|
| 1090 |
+
self.missing = {}
|
| 1091 |
+
|
| 1092 |
+
def add_distribution(self, distribution):
|
| 1093 |
+
"""Add the *distribution* to the graph.
|
| 1094 |
+
|
| 1095 |
+
:type distribution: :class:`distutils2.database.InstalledDistribution`
|
| 1096 |
+
or :class:`distutils2.database.EggInfoDistribution`
|
| 1097 |
+
"""
|
| 1098 |
+
self.adjacency_list[distribution] = []
|
| 1099 |
+
self.reverse_list[distribution] = []
|
| 1100 |
+
# self.missing[distribution] = []
|
| 1101 |
+
|
| 1102 |
+
def add_edge(self, x, y, label=None):
|
| 1103 |
+
"""Add an edge from distribution *x* to distribution *y* with the given
|
| 1104 |
+
*label*.
|
| 1105 |
+
|
| 1106 |
+
:type x: :class:`distutils2.database.InstalledDistribution` or
|
| 1107 |
+
:class:`distutils2.database.EggInfoDistribution`
|
| 1108 |
+
:type y: :class:`distutils2.database.InstalledDistribution` or
|
| 1109 |
+
:class:`distutils2.database.EggInfoDistribution`
|
| 1110 |
+
:type label: ``str`` or ``None``
|
| 1111 |
+
"""
|
| 1112 |
+
self.adjacency_list[x].append((y, label))
|
| 1113 |
+
# multiple edges are allowed, so be careful
|
| 1114 |
+
if x not in self.reverse_list[y]:
|
| 1115 |
+
self.reverse_list[y].append(x)
|
| 1116 |
+
|
| 1117 |
+
def add_missing(self, distribution, requirement):
|
| 1118 |
+
"""
|
| 1119 |
+
Add a missing *requirement* for the given *distribution*.
|
| 1120 |
+
|
| 1121 |
+
:type distribution: :class:`distutils2.database.InstalledDistribution`
|
| 1122 |
+
or :class:`distutils2.database.EggInfoDistribution`
|
| 1123 |
+
:type requirement: ``str``
|
| 1124 |
+
"""
|
| 1125 |
+
logger.debug('%s missing %r', distribution, requirement)
|
| 1126 |
+
self.missing.setdefault(distribution, []).append(requirement)
|
| 1127 |
+
|
| 1128 |
+
def _repr_dist(self, dist):
|
| 1129 |
+
return '%s %s' % (dist.name, dist.version)
|
| 1130 |
+
|
| 1131 |
+
def repr_node(self, dist, level=1):
|
| 1132 |
+
"""Prints only a subgraph"""
|
| 1133 |
+
output = [self._repr_dist(dist)]
|
| 1134 |
+
for other, label in self.adjacency_list[dist]:
|
| 1135 |
+
dist = self._repr_dist(other)
|
| 1136 |
+
if label is not None:
|
| 1137 |
+
dist = '%s [%s]' % (dist, label)
|
| 1138 |
+
output.append(' ' * level + str(dist))
|
| 1139 |
+
suboutput = self.repr_node(other, level + 1)
|
| 1140 |
+
subs = suboutput.split('\n')
|
| 1141 |
+
output.extend(subs[1:])
|
| 1142 |
+
return '\n'.join(output)
|
| 1143 |
+
|
| 1144 |
+
def to_dot(self, f, skip_disconnected=True):
|
| 1145 |
+
"""Writes a DOT output for the graph to the provided file *f*.
|
| 1146 |
+
|
| 1147 |
+
If *skip_disconnected* is set to ``True``, then all distributions
|
| 1148 |
+
that are not dependent on any other distribution are skipped.
|
| 1149 |
+
|
| 1150 |
+
:type f: has to support ``file``-like operations
|
| 1151 |
+
:type skip_disconnected: ``bool``
|
| 1152 |
+
"""
|
| 1153 |
+
disconnected = []
|
| 1154 |
+
|
| 1155 |
+
f.write("digraph dependencies {\n")
|
| 1156 |
+
for dist, adjs in self.adjacency_list.items():
|
| 1157 |
+
if len(adjs) == 0 and not skip_disconnected:
|
| 1158 |
+
disconnected.append(dist)
|
| 1159 |
+
for other, label in adjs:
|
| 1160 |
+
if label is not None:
|
| 1161 |
+
f.write('"%s" -> "%s" [label="%s"]\n' % (dist.name, other.name, label))
|
| 1162 |
+
else:
|
| 1163 |
+
f.write('"%s" -> "%s"\n' % (dist.name, other.name))
|
| 1164 |
+
if not skip_disconnected and len(disconnected) > 0:
|
| 1165 |
+
f.write('subgraph disconnected {\n')
|
| 1166 |
+
f.write('label = "Disconnected"\n')
|
| 1167 |
+
f.write('bgcolor = red\n')
|
| 1168 |
+
|
| 1169 |
+
for dist in disconnected:
|
| 1170 |
+
f.write('"%s"' % dist.name)
|
| 1171 |
+
f.write('\n')
|
| 1172 |
+
f.write('}\n')
|
| 1173 |
+
f.write('}\n')
|
| 1174 |
+
|
| 1175 |
+
def topological_sort(self):
|
| 1176 |
+
"""
|
| 1177 |
+
Perform a topological sort of the graph.
|
| 1178 |
+
:return: A tuple, the first element of which is a topologically sorted
|
| 1179 |
+
list of distributions, and the second element of which is a
|
| 1180 |
+
list of distributions that cannot be sorted because they have
|
| 1181 |
+
circular dependencies and so form a cycle.
|
| 1182 |
+
"""
|
| 1183 |
+
result = []
|
| 1184 |
+
# Make a shallow copy of the adjacency list
|
| 1185 |
+
alist = {}
|
| 1186 |
+
for k, v in self.adjacency_list.items():
|
| 1187 |
+
alist[k] = v[:]
|
| 1188 |
+
while True:
|
| 1189 |
+
# See what we can remove in this run
|
| 1190 |
+
to_remove = []
|
| 1191 |
+
for k, v in list(alist.items())[:]:
|
| 1192 |
+
if not v:
|
| 1193 |
+
to_remove.append(k)
|
| 1194 |
+
del alist[k]
|
| 1195 |
+
if not to_remove:
|
| 1196 |
+
# What's left in alist (if anything) is a cycle.
|
| 1197 |
+
break
|
| 1198 |
+
# Remove from the adjacency list of others
|
| 1199 |
+
for k, v in alist.items():
|
| 1200 |
+
alist[k] = [(d, r) for d, r in v if d not in to_remove]
|
| 1201 |
+
logger.debug('Moving to result: %s', ['%s (%s)' % (d.name, d.version) for d in to_remove])
|
| 1202 |
+
result.extend(to_remove)
|
| 1203 |
+
return result, list(alist.keys())
|
| 1204 |
+
|
| 1205 |
+
def __repr__(self):
|
| 1206 |
+
"""Representation of the graph"""
|
| 1207 |
+
output = []
|
| 1208 |
+
for dist, adjs in self.adjacency_list.items():
|
| 1209 |
+
output.append(self.repr_node(dist))
|
| 1210 |
+
return '\n'.join(output)
|
| 1211 |
+
|
| 1212 |
+
|
| 1213 |
+
def make_graph(dists, scheme='default'):
|
| 1214 |
+
"""Makes a dependency graph from the given distributions.
|
| 1215 |
+
|
| 1216 |
+
:parameter dists: a list of distributions
|
| 1217 |
+
:type dists: list of :class:`distutils2.database.InstalledDistribution` and
|
| 1218 |
+
:class:`distutils2.database.EggInfoDistribution` instances
|
| 1219 |
+
:rtype: a :class:`DependencyGraph` instance
|
| 1220 |
+
"""
|
| 1221 |
+
scheme = get_scheme(scheme)
|
| 1222 |
+
graph = DependencyGraph()
|
| 1223 |
+
provided = {} # maps names to lists of (version, dist) tuples
|
| 1224 |
+
|
| 1225 |
+
# first, build the graph and find out what's provided
|
| 1226 |
+
for dist in dists:
|
| 1227 |
+
graph.add_distribution(dist)
|
| 1228 |
+
|
| 1229 |
+
for p in dist.provides:
|
| 1230 |
+
name, version = parse_name_and_version(p)
|
| 1231 |
+
logger.debug('Add to provided: %s, %s, %s', name, version, dist)
|
| 1232 |
+
provided.setdefault(name, []).append((version, dist))
|
| 1233 |
+
|
| 1234 |
+
# now make the edges
|
| 1235 |
+
for dist in dists:
|
| 1236 |
+
requires = (dist.run_requires | dist.meta_requires | dist.build_requires | dist.dev_requires)
|
| 1237 |
+
for req in requires:
|
| 1238 |
+
try:
|
| 1239 |
+
matcher = scheme.matcher(req)
|
| 1240 |
+
except UnsupportedVersionError:
|
| 1241 |
+
# XXX compat-mode if cannot read the version
|
| 1242 |
+
logger.warning('could not read version %r - using name only', req)
|
| 1243 |
+
name = req.split()[0]
|
| 1244 |
+
matcher = scheme.matcher(name)
|
| 1245 |
+
|
| 1246 |
+
name = matcher.key # case-insensitive
|
| 1247 |
+
|
| 1248 |
+
matched = False
|
| 1249 |
+
if name in provided:
|
| 1250 |
+
for version, provider in provided[name]:
|
| 1251 |
+
try:
|
| 1252 |
+
match = matcher.match(version)
|
| 1253 |
+
except UnsupportedVersionError:
|
| 1254 |
+
match = False
|
| 1255 |
+
|
| 1256 |
+
if match:
|
| 1257 |
+
graph.add_edge(dist, provider, req)
|
| 1258 |
+
matched = True
|
| 1259 |
+
break
|
| 1260 |
+
if not matched:
|
| 1261 |
+
graph.add_missing(dist, req)
|
| 1262 |
+
return graph
|
| 1263 |
+
|
| 1264 |
+
|
| 1265 |
+
def get_dependent_dists(dists, dist):
|
| 1266 |
+
"""Recursively generate a list of distributions from *dists* that are
|
| 1267 |
+
dependent on *dist*.
|
| 1268 |
+
|
| 1269 |
+
:param dists: a list of distributions
|
| 1270 |
+
:param dist: a distribution, member of *dists* for which we are interested
|
| 1271 |
+
"""
|
| 1272 |
+
if dist not in dists:
|
| 1273 |
+
raise DistlibException('given distribution %r is not a member '
|
| 1274 |
+
'of the list' % dist.name)
|
| 1275 |
+
graph = make_graph(dists)
|
| 1276 |
+
|
| 1277 |
+
dep = [dist] # dependent distributions
|
| 1278 |
+
todo = graph.reverse_list[dist] # list of nodes we should inspect
|
| 1279 |
+
|
| 1280 |
+
while todo:
|
| 1281 |
+
d = todo.pop()
|
| 1282 |
+
dep.append(d)
|
| 1283 |
+
for succ in graph.reverse_list[d]:
|
| 1284 |
+
if succ not in dep:
|
| 1285 |
+
todo.append(succ)
|
| 1286 |
+
|
| 1287 |
+
dep.pop(0) # remove dist from dep, was there to prevent infinite loops
|
| 1288 |
+
return dep
|
| 1289 |
+
|
| 1290 |
+
|
| 1291 |
+
def get_required_dists(dists, dist):
|
| 1292 |
+
"""Recursively generate a list of distributions from *dists* that are
|
| 1293 |
+
required by *dist*.
|
| 1294 |
+
|
| 1295 |
+
:param dists: a list of distributions
|
| 1296 |
+
:param dist: a distribution, member of *dists* for which we are interested
|
| 1297 |
+
in finding the dependencies.
|
| 1298 |
+
"""
|
| 1299 |
+
if dist not in dists:
|
| 1300 |
+
raise DistlibException('given distribution %r is not a member '
|
| 1301 |
+
'of the list' % dist.name)
|
| 1302 |
+
graph = make_graph(dists)
|
| 1303 |
+
|
| 1304 |
+
req = set() # required distributions
|
| 1305 |
+
todo = graph.adjacency_list[dist] # list of nodes we should inspect
|
| 1306 |
+
seen = set(t[0] for t in todo) # already added to todo
|
| 1307 |
+
|
| 1308 |
+
while todo:
|
| 1309 |
+
d = todo.pop()[0]
|
| 1310 |
+
req.add(d)
|
| 1311 |
+
pred_list = graph.adjacency_list[d]
|
| 1312 |
+
for pred in pred_list:
|
| 1313 |
+
d = pred[0]
|
| 1314 |
+
if d not in req and d not in seen:
|
| 1315 |
+
seen.add(d)
|
| 1316 |
+
todo.append(pred)
|
| 1317 |
+
return req
|
| 1318 |
+
|
| 1319 |
+
|
| 1320 |
+
def make_dist(name, version, **kwargs):
|
| 1321 |
+
"""
|
| 1322 |
+
A convenience method for making a dist given just a name and version.
|
| 1323 |
+
"""
|
| 1324 |
+
summary = kwargs.pop('summary', 'Placeholder for summary')
|
| 1325 |
+
md = Metadata(**kwargs)
|
| 1326 |
+
md.name = name
|
| 1327 |
+
md.version = version
|
| 1328 |
+
md.summary = summary or 'Placeholder for summary'
|
| 1329 |
+
return Distribution(md)
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/index.py
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2013-2023 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
import hashlib
|
| 8 |
+
import logging
|
| 9 |
+
import os
|
| 10 |
+
import shutil
|
| 11 |
+
import subprocess
|
| 12 |
+
import tempfile
|
| 13 |
+
try:
|
| 14 |
+
from threading import Thread
|
| 15 |
+
except ImportError: # pragma: no cover
|
| 16 |
+
from dummy_threading import Thread
|
| 17 |
+
|
| 18 |
+
from . import DistlibException
|
| 19 |
+
from .compat import (HTTPBasicAuthHandler, Request, HTTPPasswordMgr,
|
| 20 |
+
urlparse, build_opener, string_types)
|
| 21 |
+
from .util import zip_dir, ServerProxy
|
| 22 |
+
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
DEFAULT_INDEX = 'https://pypi.org/pypi'
|
| 26 |
+
DEFAULT_REALM = 'pypi'
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class PackageIndex(object):
|
| 30 |
+
"""
|
| 31 |
+
This class represents a package index compatible with PyPI, the Python
|
| 32 |
+
Package Index.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
boundary = b'----------ThIs_Is_tHe_distlib_index_bouNdaRY_$'
|
| 36 |
+
|
| 37 |
+
def __init__(self, url=None):
|
| 38 |
+
"""
|
| 39 |
+
Initialise an instance.
|
| 40 |
+
|
| 41 |
+
:param url: The URL of the index. If not specified, the URL for PyPI is
|
| 42 |
+
used.
|
| 43 |
+
"""
|
| 44 |
+
self.url = url or DEFAULT_INDEX
|
| 45 |
+
self.read_configuration()
|
| 46 |
+
scheme, netloc, path, params, query, frag = urlparse(self.url)
|
| 47 |
+
if params or query or frag or scheme not in ('http', 'https'):
|
| 48 |
+
raise DistlibException('invalid repository: %s' % self.url)
|
| 49 |
+
self.password_handler = None
|
| 50 |
+
self.ssl_verifier = None
|
| 51 |
+
self.gpg = None
|
| 52 |
+
self.gpg_home = None
|
| 53 |
+
with open(os.devnull, 'w') as sink:
|
| 54 |
+
# Use gpg by default rather than gpg2, as gpg2 insists on
|
| 55 |
+
# prompting for passwords
|
| 56 |
+
for s in ('gpg', 'gpg2'):
|
| 57 |
+
try:
|
| 58 |
+
rc = subprocess.check_call([s, '--version'], stdout=sink,
|
| 59 |
+
stderr=sink)
|
| 60 |
+
if rc == 0:
|
| 61 |
+
self.gpg = s
|
| 62 |
+
break
|
| 63 |
+
except OSError:
|
| 64 |
+
pass
|
| 65 |
+
|
| 66 |
+
def _get_pypirc_command(self):
|
| 67 |
+
"""
|
| 68 |
+
Get the distutils command for interacting with PyPI configurations.
|
| 69 |
+
:return: the command.
|
| 70 |
+
"""
|
| 71 |
+
from .util import _get_pypirc_command as cmd
|
| 72 |
+
return cmd()
|
| 73 |
+
|
| 74 |
+
def read_configuration(self):
|
| 75 |
+
"""
|
| 76 |
+
Read the PyPI access configuration as supported by distutils. This populates
|
| 77 |
+
``username``, ``password``, ``realm`` and ``url`` attributes from the
|
| 78 |
+
configuration.
|
| 79 |
+
"""
|
| 80 |
+
from .util import _load_pypirc
|
| 81 |
+
cfg = _load_pypirc(self)
|
| 82 |
+
self.username = cfg.get('username')
|
| 83 |
+
self.password = cfg.get('password')
|
| 84 |
+
self.realm = cfg.get('realm', 'pypi')
|
| 85 |
+
self.url = cfg.get('repository', self.url)
|
| 86 |
+
|
| 87 |
+
def save_configuration(self):
|
| 88 |
+
"""
|
| 89 |
+
Save the PyPI access configuration. You must have set ``username`` and
|
| 90 |
+
``password`` attributes before calling this method.
|
| 91 |
+
"""
|
| 92 |
+
self.check_credentials()
|
| 93 |
+
from .util import _store_pypirc
|
| 94 |
+
_store_pypirc(self)
|
| 95 |
+
|
| 96 |
+
def check_credentials(self):
|
| 97 |
+
"""
|
| 98 |
+
Check that ``username`` and ``password`` have been set, and raise an
|
| 99 |
+
exception if not.
|
| 100 |
+
"""
|
| 101 |
+
if self.username is None or self.password is None:
|
| 102 |
+
raise DistlibException('username and password must be set')
|
| 103 |
+
pm = HTTPPasswordMgr()
|
| 104 |
+
_, netloc, _, _, _, _ = urlparse(self.url)
|
| 105 |
+
pm.add_password(self.realm, netloc, self.username, self.password)
|
| 106 |
+
self.password_handler = HTTPBasicAuthHandler(pm)
|
| 107 |
+
|
| 108 |
+
def register(self, metadata): # pragma: no cover
|
| 109 |
+
"""
|
| 110 |
+
Register a distribution on PyPI, using the provided metadata.
|
| 111 |
+
|
| 112 |
+
:param metadata: A :class:`Metadata` instance defining at least a name
|
| 113 |
+
and version number for the distribution to be
|
| 114 |
+
registered.
|
| 115 |
+
:return: The HTTP response received from PyPI upon submission of the
|
| 116 |
+
request.
|
| 117 |
+
"""
|
| 118 |
+
self.check_credentials()
|
| 119 |
+
metadata.validate()
|
| 120 |
+
d = metadata.todict()
|
| 121 |
+
d[':action'] = 'verify'
|
| 122 |
+
request = self.encode_request(d.items(), [])
|
| 123 |
+
self.send_request(request)
|
| 124 |
+
d[':action'] = 'submit'
|
| 125 |
+
request = self.encode_request(d.items(), [])
|
| 126 |
+
return self.send_request(request)
|
| 127 |
+
|
| 128 |
+
def _reader(self, name, stream, outbuf):
|
| 129 |
+
"""
|
| 130 |
+
Thread runner for reading lines of from a subprocess into a buffer.
|
| 131 |
+
|
| 132 |
+
:param name: The logical name of the stream (used for logging only).
|
| 133 |
+
:param stream: The stream to read from. This will typically a pipe
|
| 134 |
+
connected to the output stream of a subprocess.
|
| 135 |
+
:param outbuf: The list to append the read lines to.
|
| 136 |
+
"""
|
| 137 |
+
while True:
|
| 138 |
+
s = stream.readline()
|
| 139 |
+
if not s:
|
| 140 |
+
break
|
| 141 |
+
s = s.decode('utf-8').rstrip()
|
| 142 |
+
outbuf.append(s)
|
| 143 |
+
logger.debug('%s: %s' % (name, s))
|
| 144 |
+
stream.close()
|
| 145 |
+
|
| 146 |
+
def get_sign_command(self, filename, signer, sign_password, keystore=None): # pragma: no cover
|
| 147 |
+
"""
|
| 148 |
+
Return a suitable command for signing a file.
|
| 149 |
+
|
| 150 |
+
:param filename: The pathname to the file to be signed.
|
| 151 |
+
:param signer: The identifier of the signer of the file.
|
| 152 |
+
:param sign_password: The passphrase for the signer's
|
| 153 |
+
private key used for signing.
|
| 154 |
+
:param keystore: The path to a directory which contains the keys
|
| 155 |
+
used in verification. If not specified, the
|
| 156 |
+
instance's ``gpg_home`` attribute is used instead.
|
| 157 |
+
:return: The signing command as a list suitable to be
|
| 158 |
+
passed to :class:`subprocess.Popen`.
|
| 159 |
+
"""
|
| 160 |
+
cmd = [self.gpg, '--status-fd', '2', '--no-tty']
|
| 161 |
+
if keystore is None:
|
| 162 |
+
keystore = self.gpg_home
|
| 163 |
+
if keystore:
|
| 164 |
+
cmd.extend(['--homedir', keystore])
|
| 165 |
+
if sign_password is not None:
|
| 166 |
+
cmd.extend(['--batch', '--passphrase-fd', '0'])
|
| 167 |
+
td = tempfile.mkdtemp()
|
| 168 |
+
sf = os.path.join(td, os.path.basename(filename) + '.asc')
|
| 169 |
+
cmd.extend(['--detach-sign', '--armor', '--local-user',
|
| 170 |
+
signer, '--output', sf, filename])
|
| 171 |
+
logger.debug('invoking: %s', ' '.join(cmd))
|
| 172 |
+
return cmd, sf
|
| 173 |
+
|
| 174 |
+
def run_command(self, cmd, input_data=None):
|
| 175 |
+
"""
|
| 176 |
+
Run a command in a child process , passing it any input data specified.
|
| 177 |
+
|
| 178 |
+
:param cmd: The command to run.
|
| 179 |
+
:param input_data: If specified, this must be a byte string containing
|
| 180 |
+
data to be sent to the child process.
|
| 181 |
+
:return: A tuple consisting of the subprocess' exit code, a list of
|
| 182 |
+
lines read from the subprocess' ``stdout``, and a list of
|
| 183 |
+
lines read from the subprocess' ``stderr``.
|
| 184 |
+
"""
|
| 185 |
+
kwargs = {
|
| 186 |
+
'stdout': subprocess.PIPE,
|
| 187 |
+
'stderr': subprocess.PIPE,
|
| 188 |
+
}
|
| 189 |
+
if input_data is not None:
|
| 190 |
+
kwargs['stdin'] = subprocess.PIPE
|
| 191 |
+
stdout = []
|
| 192 |
+
stderr = []
|
| 193 |
+
p = subprocess.Popen(cmd, **kwargs)
|
| 194 |
+
# We don't use communicate() here because we may need to
|
| 195 |
+
# get clever with interacting with the command
|
| 196 |
+
t1 = Thread(target=self._reader, args=('stdout', p.stdout, stdout))
|
| 197 |
+
t1.start()
|
| 198 |
+
t2 = Thread(target=self._reader, args=('stderr', p.stderr, stderr))
|
| 199 |
+
t2.start()
|
| 200 |
+
if input_data is not None:
|
| 201 |
+
p.stdin.write(input_data)
|
| 202 |
+
p.stdin.close()
|
| 203 |
+
|
| 204 |
+
p.wait()
|
| 205 |
+
t1.join()
|
| 206 |
+
t2.join()
|
| 207 |
+
return p.returncode, stdout, stderr
|
| 208 |
+
|
| 209 |
+
def sign_file(self, filename, signer, sign_password, keystore=None): # pragma: no cover
|
| 210 |
+
"""
|
| 211 |
+
Sign a file.
|
| 212 |
+
|
| 213 |
+
:param filename: The pathname to the file to be signed.
|
| 214 |
+
:param signer: The identifier of the signer of the file.
|
| 215 |
+
:param sign_password: The passphrase for the signer's
|
| 216 |
+
private key used for signing.
|
| 217 |
+
:param keystore: The path to a directory which contains the keys
|
| 218 |
+
used in signing. If not specified, the instance's
|
| 219 |
+
``gpg_home`` attribute is used instead.
|
| 220 |
+
:return: The absolute pathname of the file where the signature is
|
| 221 |
+
stored.
|
| 222 |
+
"""
|
| 223 |
+
cmd, sig_file = self.get_sign_command(filename, signer, sign_password,
|
| 224 |
+
keystore)
|
| 225 |
+
rc, stdout, stderr = self.run_command(cmd,
|
| 226 |
+
sign_password.encode('utf-8'))
|
| 227 |
+
if rc != 0:
|
| 228 |
+
raise DistlibException('sign command failed with error '
|
| 229 |
+
'code %s' % rc)
|
| 230 |
+
return sig_file
|
| 231 |
+
|
| 232 |
+
def upload_file(self, metadata, filename, signer=None, sign_password=None,
|
| 233 |
+
filetype='sdist', pyversion='source', keystore=None):
|
| 234 |
+
"""
|
| 235 |
+
Upload a release file to the index.
|
| 236 |
+
|
| 237 |
+
:param metadata: A :class:`Metadata` instance defining at least a name
|
| 238 |
+
and version number for the file to be uploaded.
|
| 239 |
+
:param filename: The pathname of the file to be uploaded.
|
| 240 |
+
:param signer: The identifier of the signer of the file.
|
| 241 |
+
:param sign_password: The passphrase for the signer's
|
| 242 |
+
private key used for signing.
|
| 243 |
+
:param filetype: The type of the file being uploaded. This is the
|
| 244 |
+
distutils command which produced that file, e.g.
|
| 245 |
+
``sdist`` or ``bdist_wheel``.
|
| 246 |
+
:param pyversion: The version of Python which the release relates
|
| 247 |
+
to. For code compatible with any Python, this would
|
| 248 |
+
be ``source``, otherwise it would be e.g. ``3.2``.
|
| 249 |
+
:param keystore: The path to a directory which contains the keys
|
| 250 |
+
used in signing. If not specified, the instance's
|
| 251 |
+
``gpg_home`` attribute is used instead.
|
| 252 |
+
:return: The HTTP response received from PyPI upon submission of the
|
| 253 |
+
request.
|
| 254 |
+
"""
|
| 255 |
+
self.check_credentials()
|
| 256 |
+
if not os.path.exists(filename):
|
| 257 |
+
raise DistlibException('not found: %s' % filename)
|
| 258 |
+
metadata.validate()
|
| 259 |
+
d = metadata.todict()
|
| 260 |
+
sig_file = None
|
| 261 |
+
if signer:
|
| 262 |
+
if not self.gpg:
|
| 263 |
+
logger.warning('no signing program available - not signed')
|
| 264 |
+
else:
|
| 265 |
+
sig_file = self.sign_file(filename, signer, sign_password,
|
| 266 |
+
keystore)
|
| 267 |
+
with open(filename, 'rb') as f:
|
| 268 |
+
file_data = f.read()
|
| 269 |
+
md5_digest = hashlib.md5(file_data).hexdigest()
|
| 270 |
+
sha256_digest = hashlib.sha256(file_data).hexdigest()
|
| 271 |
+
d.update({
|
| 272 |
+
':action': 'file_upload',
|
| 273 |
+
'protocol_version': '1',
|
| 274 |
+
'filetype': filetype,
|
| 275 |
+
'pyversion': pyversion,
|
| 276 |
+
'md5_digest': md5_digest,
|
| 277 |
+
'sha256_digest': sha256_digest,
|
| 278 |
+
})
|
| 279 |
+
files = [('content', os.path.basename(filename), file_data)]
|
| 280 |
+
if sig_file:
|
| 281 |
+
with open(sig_file, 'rb') as f:
|
| 282 |
+
sig_data = f.read()
|
| 283 |
+
files.append(('gpg_signature', os.path.basename(sig_file),
|
| 284 |
+
sig_data))
|
| 285 |
+
shutil.rmtree(os.path.dirname(sig_file))
|
| 286 |
+
request = self.encode_request(d.items(), files)
|
| 287 |
+
return self.send_request(request)
|
| 288 |
+
|
| 289 |
+
def upload_documentation(self, metadata, doc_dir): # pragma: no cover
|
| 290 |
+
"""
|
| 291 |
+
Upload documentation to the index.
|
| 292 |
+
|
| 293 |
+
:param metadata: A :class:`Metadata` instance defining at least a name
|
| 294 |
+
and version number for the documentation to be
|
| 295 |
+
uploaded.
|
| 296 |
+
:param doc_dir: The pathname of the directory which contains the
|
| 297 |
+
documentation. This should be the directory that
|
| 298 |
+
contains the ``index.html`` for the documentation.
|
| 299 |
+
:return: The HTTP response received from PyPI upon submission of the
|
| 300 |
+
request.
|
| 301 |
+
"""
|
| 302 |
+
self.check_credentials()
|
| 303 |
+
if not os.path.isdir(doc_dir):
|
| 304 |
+
raise DistlibException('not a directory: %r' % doc_dir)
|
| 305 |
+
fn = os.path.join(doc_dir, 'index.html')
|
| 306 |
+
if not os.path.exists(fn):
|
| 307 |
+
raise DistlibException('not found: %r' % fn)
|
| 308 |
+
metadata.validate()
|
| 309 |
+
name, version = metadata.name, metadata.version
|
| 310 |
+
zip_data = zip_dir(doc_dir).getvalue()
|
| 311 |
+
fields = [(':action', 'doc_upload'),
|
| 312 |
+
('name', name), ('version', version)]
|
| 313 |
+
files = [('content', name, zip_data)]
|
| 314 |
+
request = self.encode_request(fields, files)
|
| 315 |
+
return self.send_request(request)
|
| 316 |
+
|
| 317 |
+
def get_verify_command(self, signature_filename, data_filename,
|
| 318 |
+
keystore=None):
|
| 319 |
+
"""
|
| 320 |
+
Return a suitable command for verifying a file.
|
| 321 |
+
|
| 322 |
+
:param signature_filename: The pathname to the file containing the
|
| 323 |
+
signature.
|
| 324 |
+
:param data_filename: The pathname to the file containing the
|
| 325 |
+
signed data.
|
| 326 |
+
:param keystore: The path to a directory which contains the keys
|
| 327 |
+
used in verification. If not specified, the
|
| 328 |
+
instance's ``gpg_home`` attribute is used instead.
|
| 329 |
+
:return: The verifying command as a list suitable to be
|
| 330 |
+
passed to :class:`subprocess.Popen`.
|
| 331 |
+
"""
|
| 332 |
+
cmd = [self.gpg, '--status-fd', '2', '--no-tty']
|
| 333 |
+
if keystore is None:
|
| 334 |
+
keystore = self.gpg_home
|
| 335 |
+
if keystore:
|
| 336 |
+
cmd.extend(['--homedir', keystore])
|
| 337 |
+
cmd.extend(['--verify', signature_filename, data_filename])
|
| 338 |
+
logger.debug('invoking: %s', ' '.join(cmd))
|
| 339 |
+
return cmd
|
| 340 |
+
|
| 341 |
+
def verify_signature(self, signature_filename, data_filename,
|
| 342 |
+
keystore=None):
|
| 343 |
+
"""
|
| 344 |
+
Verify a signature for a file.
|
| 345 |
+
|
| 346 |
+
:param signature_filename: The pathname to the file containing the
|
| 347 |
+
signature.
|
| 348 |
+
:param data_filename: The pathname to the file containing the
|
| 349 |
+
signed data.
|
| 350 |
+
:param keystore: The path to a directory which contains the keys
|
| 351 |
+
used in verification. If not specified, the
|
| 352 |
+
instance's ``gpg_home`` attribute is used instead.
|
| 353 |
+
:return: True if the signature was verified, else False.
|
| 354 |
+
"""
|
| 355 |
+
if not self.gpg:
|
| 356 |
+
raise DistlibException('verification unavailable because gpg '
|
| 357 |
+
'unavailable')
|
| 358 |
+
cmd = self.get_verify_command(signature_filename, data_filename,
|
| 359 |
+
keystore)
|
| 360 |
+
rc, stdout, stderr = self.run_command(cmd)
|
| 361 |
+
if rc not in (0, 1):
|
| 362 |
+
raise DistlibException('verify command failed with error code %s' % rc)
|
| 363 |
+
return rc == 0
|
| 364 |
+
|
| 365 |
+
def download_file(self, url, destfile, digest=None, reporthook=None):
|
| 366 |
+
"""
|
| 367 |
+
This is a convenience method for downloading a file from an URL.
|
| 368 |
+
Normally, this will be a file from the index, though currently
|
| 369 |
+
no check is made for this (i.e. a file can be downloaded from
|
| 370 |
+
anywhere).
|
| 371 |
+
|
| 372 |
+
The method is just like the :func:`urlretrieve` function in the
|
| 373 |
+
standard library, except that it allows digest computation to be
|
| 374 |
+
done during download and checking that the downloaded data
|
| 375 |
+
matched any expected value.
|
| 376 |
+
|
| 377 |
+
:param url: The URL of the file to be downloaded (assumed to be
|
| 378 |
+
available via an HTTP GET request).
|
| 379 |
+
:param destfile: The pathname where the downloaded file is to be
|
| 380 |
+
saved.
|
| 381 |
+
:param digest: If specified, this must be a (hasher, value)
|
| 382 |
+
tuple, where hasher is the algorithm used (e.g.
|
| 383 |
+
``'md5'``) and ``value`` is the expected value.
|
| 384 |
+
:param reporthook: The same as for :func:`urlretrieve` in the
|
| 385 |
+
standard library.
|
| 386 |
+
"""
|
| 387 |
+
if digest is None:
|
| 388 |
+
digester = None
|
| 389 |
+
logger.debug('No digest specified')
|
| 390 |
+
else:
|
| 391 |
+
if isinstance(digest, (list, tuple)):
|
| 392 |
+
hasher, digest = digest
|
| 393 |
+
else:
|
| 394 |
+
hasher = 'md5'
|
| 395 |
+
digester = getattr(hashlib, hasher)()
|
| 396 |
+
logger.debug('Digest specified: %s' % digest)
|
| 397 |
+
# The following code is equivalent to urlretrieve.
|
| 398 |
+
# We need to do it this way so that we can compute the
|
| 399 |
+
# digest of the file as we go.
|
| 400 |
+
with open(destfile, 'wb') as dfp:
|
| 401 |
+
# addinfourl is not a context manager on 2.x
|
| 402 |
+
# so we have to use try/finally
|
| 403 |
+
sfp = self.send_request(Request(url))
|
| 404 |
+
try:
|
| 405 |
+
headers = sfp.info()
|
| 406 |
+
blocksize = 8192
|
| 407 |
+
size = -1
|
| 408 |
+
read = 0
|
| 409 |
+
blocknum = 0
|
| 410 |
+
if "content-length" in headers:
|
| 411 |
+
size = int(headers["Content-Length"])
|
| 412 |
+
if reporthook:
|
| 413 |
+
reporthook(blocknum, blocksize, size)
|
| 414 |
+
while True:
|
| 415 |
+
block = sfp.read(blocksize)
|
| 416 |
+
if not block:
|
| 417 |
+
break
|
| 418 |
+
read += len(block)
|
| 419 |
+
dfp.write(block)
|
| 420 |
+
if digester:
|
| 421 |
+
digester.update(block)
|
| 422 |
+
blocknum += 1
|
| 423 |
+
if reporthook:
|
| 424 |
+
reporthook(blocknum, blocksize, size)
|
| 425 |
+
finally:
|
| 426 |
+
sfp.close()
|
| 427 |
+
|
| 428 |
+
# check that we got the whole file, if we can
|
| 429 |
+
if size >= 0 and read < size:
|
| 430 |
+
raise DistlibException(
|
| 431 |
+
'retrieval incomplete: got only %d out of %d bytes'
|
| 432 |
+
% (read, size))
|
| 433 |
+
# if we have a digest, it must match.
|
| 434 |
+
if digester:
|
| 435 |
+
actual = digester.hexdigest()
|
| 436 |
+
if digest != actual:
|
| 437 |
+
raise DistlibException('%s digest mismatch for %s: expected '
|
| 438 |
+
'%s, got %s' % (hasher, destfile,
|
| 439 |
+
digest, actual))
|
| 440 |
+
logger.debug('Digest verified: %s', digest)
|
| 441 |
+
|
| 442 |
+
def send_request(self, req):
|
| 443 |
+
"""
|
| 444 |
+
Send a standard library :class:`Request` to PyPI and return its
|
| 445 |
+
response.
|
| 446 |
+
|
| 447 |
+
:param req: The request to send.
|
| 448 |
+
:return: The HTTP response from PyPI (a standard library HTTPResponse).
|
| 449 |
+
"""
|
| 450 |
+
handlers = []
|
| 451 |
+
if self.password_handler:
|
| 452 |
+
handlers.append(self.password_handler)
|
| 453 |
+
if self.ssl_verifier:
|
| 454 |
+
handlers.append(self.ssl_verifier)
|
| 455 |
+
opener = build_opener(*handlers)
|
| 456 |
+
return opener.open(req)
|
| 457 |
+
|
| 458 |
+
def encode_request(self, fields, files):
|
| 459 |
+
"""
|
| 460 |
+
Encode fields and files for posting to an HTTP server.
|
| 461 |
+
|
| 462 |
+
:param fields: The fields to send as a list of (fieldname, value)
|
| 463 |
+
tuples.
|
| 464 |
+
:param files: The files to send as a list of (fieldname, filename,
|
| 465 |
+
file_bytes) tuple.
|
| 466 |
+
"""
|
| 467 |
+
# Adapted from packaging, which in turn was adapted from
|
| 468 |
+
# http://code.activestate.com/recipes/146306
|
| 469 |
+
|
| 470 |
+
parts = []
|
| 471 |
+
boundary = self.boundary
|
| 472 |
+
for k, values in fields:
|
| 473 |
+
if not isinstance(values, (list, tuple)):
|
| 474 |
+
values = [values]
|
| 475 |
+
|
| 476 |
+
for v in values:
|
| 477 |
+
parts.extend((
|
| 478 |
+
b'--' + boundary,
|
| 479 |
+
('Content-Disposition: form-data; name="%s"' %
|
| 480 |
+
k).encode('utf-8'),
|
| 481 |
+
b'',
|
| 482 |
+
v.encode('utf-8')))
|
| 483 |
+
for key, filename, value in files:
|
| 484 |
+
parts.extend((
|
| 485 |
+
b'--' + boundary,
|
| 486 |
+
('Content-Disposition: form-data; name="%s"; filename="%s"' %
|
| 487 |
+
(key, filename)).encode('utf-8'),
|
| 488 |
+
b'',
|
| 489 |
+
value))
|
| 490 |
+
|
| 491 |
+
parts.extend((b'--' + boundary + b'--', b''))
|
| 492 |
+
|
| 493 |
+
body = b'\r\n'.join(parts)
|
| 494 |
+
ct = b'multipart/form-data; boundary=' + boundary
|
| 495 |
+
headers = {
|
| 496 |
+
'Content-type': ct,
|
| 497 |
+
'Content-length': str(len(body))
|
| 498 |
+
}
|
| 499 |
+
return Request(self.url, body, headers)
|
| 500 |
+
|
| 501 |
+
def search(self, terms, operator=None): # pragma: no cover
|
| 502 |
+
if isinstance(terms, string_types):
|
| 503 |
+
terms = {'name': terms}
|
| 504 |
+
rpc_proxy = ServerProxy(self.url, timeout=3.0)
|
| 505 |
+
try:
|
| 506 |
+
return rpc_proxy.search(terms, operator or 'and')
|
| 507 |
+
finally:
|
| 508 |
+
rpc_proxy('close')()
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/locators.py
ADDED
|
@@ -0,0 +1,1295 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2012-2023 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
|
| 8 |
+
import gzip
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
import json
|
| 11 |
+
import logging
|
| 12 |
+
import os
|
| 13 |
+
import posixpath
|
| 14 |
+
import re
|
| 15 |
+
try:
|
| 16 |
+
import threading
|
| 17 |
+
except ImportError: # pragma: no cover
|
| 18 |
+
import dummy_threading as threading
|
| 19 |
+
import zlib
|
| 20 |
+
|
| 21 |
+
from . import DistlibException
|
| 22 |
+
from .compat import (urljoin, urlparse, urlunparse, url2pathname, pathname2url, queue, quote, unescape, build_opener,
|
| 23 |
+
HTTPRedirectHandler as BaseRedirectHandler, text_type, Request, HTTPError, URLError)
|
| 24 |
+
from .database import Distribution, DistributionPath, make_dist
|
| 25 |
+
from .metadata import Metadata, MetadataInvalidError
|
| 26 |
+
from .util import (cached_property, ensure_slash, split_filename, get_project_data, parse_requirement,
|
| 27 |
+
parse_name_and_version, ServerProxy, normalize_name)
|
| 28 |
+
from .version import get_scheme, UnsupportedVersionError
|
| 29 |
+
from .wheel import Wheel, is_compatible
|
| 30 |
+
|
| 31 |
+
logger = logging.getLogger(__name__)
|
| 32 |
+
|
| 33 |
+
HASHER_HASH = re.compile(r'^(\w+)=([a-f0-9]+)')
|
| 34 |
+
CHARSET = re.compile(r';\s*charset\s*=\s*(.*)\s*$', re.I)
|
| 35 |
+
HTML_CONTENT_TYPE = re.compile('text/html|application/x(ht)?ml')
|
| 36 |
+
DEFAULT_INDEX = 'https://pypi.org/pypi'
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_all_distribution_names(url=None):
|
| 40 |
+
"""
|
| 41 |
+
Return all distribution names known by an index.
|
| 42 |
+
:param url: The URL of the index.
|
| 43 |
+
:return: A list of all known distribution names.
|
| 44 |
+
"""
|
| 45 |
+
if url is None:
|
| 46 |
+
url = DEFAULT_INDEX
|
| 47 |
+
client = ServerProxy(url, timeout=3.0)
|
| 48 |
+
try:
|
| 49 |
+
return client.list_packages()
|
| 50 |
+
finally:
|
| 51 |
+
client('close')()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class RedirectHandler(BaseRedirectHandler):
|
| 55 |
+
"""
|
| 56 |
+
A class to work around a bug in some Python 3.2.x releases.
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
# There's a bug in the base version for some 3.2.x
|
| 60 |
+
# (e.g. 3.2.2 on Ubuntu Oneiric). If a Location header
|
| 61 |
+
# returns e.g. /abc, it bails because it says the scheme ''
|
| 62 |
+
# is bogus, when actually it should use the request's
|
| 63 |
+
# URL for the scheme. See Python issue #13696.
|
| 64 |
+
def http_error_302(self, req, fp, code, msg, headers):
|
| 65 |
+
# Some servers (incorrectly) return multiple Location headers
|
| 66 |
+
# (so probably same goes for URI). Use first header.
|
| 67 |
+
newurl = None
|
| 68 |
+
for key in ('location', 'uri'):
|
| 69 |
+
if key in headers:
|
| 70 |
+
newurl = headers[key]
|
| 71 |
+
break
|
| 72 |
+
if newurl is None: # pragma: no cover
|
| 73 |
+
return
|
| 74 |
+
urlparts = urlparse(newurl)
|
| 75 |
+
if urlparts.scheme == '':
|
| 76 |
+
newurl = urljoin(req.get_full_url(), newurl)
|
| 77 |
+
if hasattr(headers, 'replace_header'):
|
| 78 |
+
headers.replace_header(key, newurl)
|
| 79 |
+
else:
|
| 80 |
+
headers[key] = newurl
|
| 81 |
+
return BaseRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
|
| 82 |
+
|
| 83 |
+
http_error_301 = http_error_303 = http_error_307 = http_error_302
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class Locator(object):
|
| 87 |
+
"""
|
| 88 |
+
A base class for locators - things that locate distributions.
|
| 89 |
+
"""
|
| 90 |
+
source_extensions = ('.tar.gz', '.tar.bz2', '.tar', '.zip', '.tgz', '.tbz')
|
| 91 |
+
binary_extensions = ('.egg', '.exe', '.whl')
|
| 92 |
+
excluded_extensions = ('.pdf', )
|
| 93 |
+
|
| 94 |
+
# A list of tags indicating which wheels you want to match. The default
|
| 95 |
+
# value of None matches against the tags compatible with the running
|
| 96 |
+
# Python. If you want to match other values, set wheel_tags on a locator
|
| 97 |
+
# instance to a list of tuples (pyver, abi, arch) which you want to match.
|
| 98 |
+
wheel_tags = None
|
| 99 |
+
|
| 100 |
+
downloadable_extensions = source_extensions + ('.whl', )
|
| 101 |
+
|
| 102 |
+
def __init__(self, scheme='default'):
|
| 103 |
+
"""
|
| 104 |
+
Initialise an instance.
|
| 105 |
+
:param scheme: Because locators look for most recent versions, they
|
| 106 |
+
need to know the version scheme to use. This specifies
|
| 107 |
+
the current PEP-recommended scheme - use ``'legacy'``
|
| 108 |
+
if you need to support existing distributions on PyPI.
|
| 109 |
+
"""
|
| 110 |
+
self._cache = {}
|
| 111 |
+
self.scheme = scheme
|
| 112 |
+
# Because of bugs in some of the handlers on some of the platforms,
|
| 113 |
+
# we use our own opener rather than just using urlopen.
|
| 114 |
+
self.opener = build_opener(RedirectHandler())
|
| 115 |
+
# If get_project() is called from locate(), the matcher instance
|
| 116 |
+
# is set from the requirement passed to locate(). See issue #18 for
|
| 117 |
+
# why this can be useful to know.
|
| 118 |
+
self.matcher = None
|
| 119 |
+
self.errors = queue.Queue()
|
| 120 |
+
|
| 121 |
+
def get_errors(self):
|
| 122 |
+
"""
|
| 123 |
+
Return any errors which have occurred.
|
| 124 |
+
"""
|
| 125 |
+
result = []
|
| 126 |
+
while not self.errors.empty(): # pragma: no cover
|
| 127 |
+
try:
|
| 128 |
+
e = self.errors.get(False)
|
| 129 |
+
result.append(e)
|
| 130 |
+
except self.errors.Empty:
|
| 131 |
+
continue
|
| 132 |
+
self.errors.task_done()
|
| 133 |
+
return result
|
| 134 |
+
|
| 135 |
+
def clear_errors(self):
|
| 136 |
+
"""
|
| 137 |
+
Clear any errors which may have been logged.
|
| 138 |
+
"""
|
| 139 |
+
# Just get the errors and throw them away
|
| 140 |
+
self.get_errors()
|
| 141 |
+
|
| 142 |
+
def clear_cache(self):
|
| 143 |
+
self._cache.clear()
|
| 144 |
+
|
| 145 |
+
def _get_scheme(self):
|
| 146 |
+
return self._scheme
|
| 147 |
+
|
| 148 |
+
def _set_scheme(self, value):
|
| 149 |
+
self._scheme = value
|
| 150 |
+
|
| 151 |
+
scheme = property(_get_scheme, _set_scheme)
|
| 152 |
+
|
| 153 |
+
def _get_project(self, name):
|
| 154 |
+
"""
|
| 155 |
+
For a given project, get a dictionary mapping available versions to Distribution
|
| 156 |
+
instances.
|
| 157 |
+
|
| 158 |
+
This should be implemented in subclasses.
|
| 159 |
+
|
| 160 |
+
If called from a locate() request, self.matcher will be set to a
|
| 161 |
+
matcher for the requirement to satisfy, otherwise it will be None.
|
| 162 |
+
"""
|
| 163 |
+
raise NotImplementedError('Please implement in the subclass')
|
| 164 |
+
|
| 165 |
+
def get_distribution_names(self):
|
| 166 |
+
"""
|
| 167 |
+
Return all the distribution names known to this locator.
|
| 168 |
+
"""
|
| 169 |
+
raise NotImplementedError('Please implement in the subclass')
|
| 170 |
+
|
| 171 |
+
def get_project(self, name):
|
| 172 |
+
"""
|
| 173 |
+
For a given project, get a dictionary mapping available versions to Distribution
|
| 174 |
+
instances.
|
| 175 |
+
|
| 176 |
+
This calls _get_project to do all the work, and just implements a caching layer on top.
|
| 177 |
+
"""
|
| 178 |
+
if self._cache is None: # pragma: no cover
|
| 179 |
+
result = self._get_project(name)
|
| 180 |
+
elif name in self._cache:
|
| 181 |
+
result = self._cache[name]
|
| 182 |
+
else:
|
| 183 |
+
self.clear_errors()
|
| 184 |
+
result = self._get_project(name)
|
| 185 |
+
self._cache[name] = result
|
| 186 |
+
return result
|
| 187 |
+
|
| 188 |
+
def score_url(self, url):
|
| 189 |
+
"""
|
| 190 |
+
Give an url a score which can be used to choose preferred URLs
|
| 191 |
+
for a given project release.
|
| 192 |
+
"""
|
| 193 |
+
t = urlparse(url)
|
| 194 |
+
basename = posixpath.basename(t.path)
|
| 195 |
+
compatible = True
|
| 196 |
+
is_wheel = basename.endswith('.whl')
|
| 197 |
+
is_downloadable = basename.endswith(self.downloadable_extensions)
|
| 198 |
+
if is_wheel:
|
| 199 |
+
compatible = is_compatible(Wheel(basename), self.wheel_tags)
|
| 200 |
+
return (t.scheme == 'https', 'pypi.org' in t.netloc, is_downloadable, is_wheel, compatible, basename)
|
| 201 |
+
|
| 202 |
+
def prefer_url(self, url1, url2):
|
| 203 |
+
"""
|
| 204 |
+
Choose one of two URLs where both are candidates for distribution
|
| 205 |
+
archives for the same version of a distribution (for example,
|
| 206 |
+
.tar.gz vs. zip).
|
| 207 |
+
|
| 208 |
+
The current implementation favours https:// URLs over http://, archives
|
| 209 |
+
from PyPI over those from other locations, wheel compatibility (if a
|
| 210 |
+
wheel) and then the archive name.
|
| 211 |
+
"""
|
| 212 |
+
result = url2
|
| 213 |
+
if url1:
|
| 214 |
+
s1 = self.score_url(url1)
|
| 215 |
+
s2 = self.score_url(url2)
|
| 216 |
+
if s1 > s2:
|
| 217 |
+
result = url1
|
| 218 |
+
if result != url2:
|
| 219 |
+
logger.debug('Not replacing %r with %r', url1, url2)
|
| 220 |
+
else:
|
| 221 |
+
logger.debug('Replacing %r with %r', url1, url2)
|
| 222 |
+
return result
|
| 223 |
+
|
| 224 |
+
def split_filename(self, filename, project_name):
|
| 225 |
+
"""
|
| 226 |
+
Attempt to split a filename in project name, version and Python version.
|
| 227 |
+
"""
|
| 228 |
+
return split_filename(filename, project_name)
|
| 229 |
+
|
| 230 |
+
def convert_url_to_download_info(self, url, project_name):
|
| 231 |
+
"""
|
| 232 |
+
See if a URL is a candidate for a download URL for a project (the URL
|
| 233 |
+
has typically been scraped from an HTML page).
|
| 234 |
+
|
| 235 |
+
If it is, a dictionary is returned with keys "name", "version",
|
| 236 |
+
"filename" and "url"; otherwise, None is returned.
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
def same_project(name1, name2):
|
| 240 |
+
return normalize_name(name1) == normalize_name(name2)
|
| 241 |
+
|
| 242 |
+
result = None
|
| 243 |
+
scheme, netloc, path, params, query, frag = urlparse(url)
|
| 244 |
+
if frag.lower().startswith('egg='): # pragma: no cover
|
| 245 |
+
logger.debug('%s: version hint in fragment: %r', project_name, frag)
|
| 246 |
+
m = HASHER_HASH.match(frag)
|
| 247 |
+
if m:
|
| 248 |
+
algo, digest = m.groups()
|
| 249 |
+
else:
|
| 250 |
+
algo, digest = None, None
|
| 251 |
+
origpath = path
|
| 252 |
+
if path and path[-1] == '/': # pragma: no cover
|
| 253 |
+
path = path[:-1]
|
| 254 |
+
if path.endswith('.whl'):
|
| 255 |
+
try:
|
| 256 |
+
wheel = Wheel(path)
|
| 257 |
+
if not is_compatible(wheel, self.wheel_tags):
|
| 258 |
+
logger.debug('Wheel not compatible: %s', path)
|
| 259 |
+
else:
|
| 260 |
+
if project_name is None:
|
| 261 |
+
include = True
|
| 262 |
+
else:
|
| 263 |
+
include = same_project(wheel.name, project_name)
|
| 264 |
+
if include:
|
| 265 |
+
result = {
|
| 266 |
+
'name': wheel.name,
|
| 267 |
+
'version': wheel.version,
|
| 268 |
+
'filename': wheel.filename,
|
| 269 |
+
'url': urlunparse((scheme, netloc, origpath, params, query, '')),
|
| 270 |
+
'python-version': ', '.join(['.'.join(list(v[2:])) for v in wheel.pyver]),
|
| 271 |
+
}
|
| 272 |
+
except Exception: # pragma: no cover
|
| 273 |
+
logger.warning('invalid path for wheel: %s', path)
|
| 274 |
+
elif not path.endswith(self.downloadable_extensions): # pragma: no cover
|
| 275 |
+
logger.debug('Not downloadable: %s', path)
|
| 276 |
+
else: # downloadable extension
|
| 277 |
+
path = filename = posixpath.basename(path)
|
| 278 |
+
for ext in self.downloadable_extensions:
|
| 279 |
+
if path.endswith(ext):
|
| 280 |
+
path = path[:-len(ext)]
|
| 281 |
+
t = self.split_filename(path, project_name)
|
| 282 |
+
if not t: # pragma: no cover
|
| 283 |
+
logger.debug('No match for project/version: %s', path)
|
| 284 |
+
else:
|
| 285 |
+
name, version, pyver = t
|
| 286 |
+
if not project_name or same_project(project_name, name):
|
| 287 |
+
result = {
|
| 288 |
+
'name': name,
|
| 289 |
+
'version': version,
|
| 290 |
+
'filename': filename,
|
| 291 |
+
'url': urlunparse((scheme, netloc, origpath, params, query, '')),
|
| 292 |
+
}
|
| 293 |
+
if pyver: # pragma: no cover
|
| 294 |
+
result['python-version'] = pyver
|
| 295 |
+
break
|
| 296 |
+
if result and algo:
|
| 297 |
+
result['%s_digest' % algo] = digest
|
| 298 |
+
return result
|
| 299 |
+
|
| 300 |
+
def _get_digest(self, info):
|
| 301 |
+
"""
|
| 302 |
+
Get a digest from a dictionary by looking at a "digests" dictionary
|
| 303 |
+
or keys of the form 'algo_digest'.
|
| 304 |
+
|
| 305 |
+
Returns a 2-tuple (algo, digest) if found, else None. Currently
|
| 306 |
+
looks only for SHA256, then MD5.
|
| 307 |
+
"""
|
| 308 |
+
result = None
|
| 309 |
+
if 'digests' in info:
|
| 310 |
+
digests = info['digests']
|
| 311 |
+
for algo in ('sha256', 'md5'):
|
| 312 |
+
if algo in digests:
|
| 313 |
+
result = (algo, digests[algo])
|
| 314 |
+
break
|
| 315 |
+
if not result:
|
| 316 |
+
for algo in ('sha256', 'md5'):
|
| 317 |
+
key = '%s_digest' % algo
|
| 318 |
+
if key in info:
|
| 319 |
+
result = (algo, info[key])
|
| 320 |
+
break
|
| 321 |
+
return result
|
| 322 |
+
|
| 323 |
+
def _update_version_data(self, result, info):
|
| 324 |
+
"""
|
| 325 |
+
Update a result dictionary (the final result from _get_project) with a
|
| 326 |
+
dictionary for a specific version, which typically holds information
|
| 327 |
+
gleaned from a filename or URL for an archive for the distribution.
|
| 328 |
+
"""
|
| 329 |
+
name = info.pop('name')
|
| 330 |
+
version = info.pop('version')
|
| 331 |
+
if version in result:
|
| 332 |
+
dist = result[version]
|
| 333 |
+
md = dist.metadata
|
| 334 |
+
else:
|
| 335 |
+
dist = make_dist(name, version, scheme=self.scheme)
|
| 336 |
+
md = dist.metadata
|
| 337 |
+
dist.digest = digest = self._get_digest(info)
|
| 338 |
+
url = info['url']
|
| 339 |
+
result['digests'][url] = digest
|
| 340 |
+
if md.source_url != info['url']:
|
| 341 |
+
md.source_url = self.prefer_url(md.source_url, url)
|
| 342 |
+
result['urls'].setdefault(version, set()).add(url)
|
| 343 |
+
dist.locator = self
|
| 344 |
+
result[version] = dist
|
| 345 |
+
|
| 346 |
+
def locate(self, requirement, prereleases=False):
|
| 347 |
+
"""
|
| 348 |
+
Find the most recent distribution which matches the given
|
| 349 |
+
requirement.
|
| 350 |
+
|
| 351 |
+
:param requirement: A requirement of the form 'foo (1.0)' or perhaps
|
| 352 |
+
'foo (>= 1.0, < 2.0, != 1.3)'
|
| 353 |
+
:param prereleases: If ``True``, allow pre-release versions
|
| 354 |
+
to be located. Otherwise, pre-release versions
|
| 355 |
+
are not returned.
|
| 356 |
+
:return: A :class:`Distribution` instance, or ``None`` if no such
|
| 357 |
+
distribution could be located.
|
| 358 |
+
"""
|
| 359 |
+
result = None
|
| 360 |
+
r = parse_requirement(requirement)
|
| 361 |
+
if r is None: # pragma: no cover
|
| 362 |
+
raise DistlibException('Not a valid requirement: %r' % requirement)
|
| 363 |
+
scheme = get_scheme(self.scheme)
|
| 364 |
+
self.matcher = matcher = scheme.matcher(r.requirement)
|
| 365 |
+
logger.debug('matcher: %s (%s)', matcher, type(matcher).__name__)
|
| 366 |
+
versions = self.get_project(r.name)
|
| 367 |
+
if len(versions) > 2: # urls and digests keys are present
|
| 368 |
+
# sometimes, versions are invalid
|
| 369 |
+
slist = []
|
| 370 |
+
vcls = matcher.version_class
|
| 371 |
+
for k in versions:
|
| 372 |
+
if k in ('urls', 'digests'):
|
| 373 |
+
continue
|
| 374 |
+
try:
|
| 375 |
+
if not matcher.match(k):
|
| 376 |
+
pass # logger.debug('%s did not match %r', matcher, k)
|
| 377 |
+
else:
|
| 378 |
+
if prereleases or not vcls(k).is_prerelease:
|
| 379 |
+
slist.append(k)
|
| 380 |
+
except Exception: # pragma: no cover
|
| 381 |
+
logger.warning('error matching %s with %r', matcher, k)
|
| 382 |
+
pass # slist.append(k)
|
| 383 |
+
if len(slist) > 1:
|
| 384 |
+
slist = sorted(slist, key=scheme.key)
|
| 385 |
+
if slist:
|
| 386 |
+
logger.debug('sorted list: %s', slist)
|
| 387 |
+
version = slist[-1]
|
| 388 |
+
result = versions[version]
|
| 389 |
+
if result:
|
| 390 |
+
if r.extras:
|
| 391 |
+
result.extras = r.extras
|
| 392 |
+
result.download_urls = versions.get('urls', {}).get(version, set())
|
| 393 |
+
d = {}
|
| 394 |
+
sd = versions.get('digests', {})
|
| 395 |
+
for url in result.download_urls:
|
| 396 |
+
if url in sd: # pragma: no cover
|
| 397 |
+
d[url] = sd[url]
|
| 398 |
+
result.digests = d
|
| 399 |
+
self.matcher = None
|
| 400 |
+
return result
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
class PyPIRPCLocator(Locator):
|
| 404 |
+
"""
|
| 405 |
+
This locator uses XML-RPC to locate distributions. It therefore
|
| 406 |
+
cannot be used with simple mirrors (that only mirror file content).
|
| 407 |
+
"""
|
| 408 |
+
|
| 409 |
+
def __init__(self, url, **kwargs):
|
| 410 |
+
"""
|
| 411 |
+
Initialise an instance.
|
| 412 |
+
|
| 413 |
+
:param url: The URL to use for XML-RPC.
|
| 414 |
+
:param kwargs: Passed to the superclass constructor.
|
| 415 |
+
"""
|
| 416 |
+
super(PyPIRPCLocator, self).__init__(**kwargs)
|
| 417 |
+
self.base_url = url
|
| 418 |
+
self.client = ServerProxy(url, timeout=3.0)
|
| 419 |
+
|
| 420 |
+
def get_distribution_names(self):
|
| 421 |
+
"""
|
| 422 |
+
Return all the distribution names known to this locator.
|
| 423 |
+
"""
|
| 424 |
+
return set(self.client.list_packages())
|
| 425 |
+
|
| 426 |
+
def _get_project(self, name):
|
| 427 |
+
result = {'urls': {}, 'digests': {}}
|
| 428 |
+
versions = self.client.package_releases(name, True)
|
| 429 |
+
for v in versions:
|
| 430 |
+
urls = self.client.release_urls(name, v)
|
| 431 |
+
data = self.client.release_data(name, v)
|
| 432 |
+
metadata = Metadata(scheme=self.scheme)
|
| 433 |
+
metadata.name = data['name']
|
| 434 |
+
metadata.version = data['version']
|
| 435 |
+
metadata.license = data.get('license')
|
| 436 |
+
metadata.keywords = data.get('keywords', [])
|
| 437 |
+
metadata.summary = data.get('summary')
|
| 438 |
+
dist = Distribution(metadata)
|
| 439 |
+
if urls:
|
| 440 |
+
info = urls[0]
|
| 441 |
+
metadata.source_url = info['url']
|
| 442 |
+
dist.digest = self._get_digest(info)
|
| 443 |
+
dist.locator = self
|
| 444 |
+
result[v] = dist
|
| 445 |
+
for info in urls:
|
| 446 |
+
url = info['url']
|
| 447 |
+
digest = self._get_digest(info)
|
| 448 |
+
result['urls'].setdefault(v, set()).add(url)
|
| 449 |
+
result['digests'][url] = digest
|
| 450 |
+
return result
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
class PyPIJSONLocator(Locator):
|
| 454 |
+
"""
|
| 455 |
+
This locator uses PyPI's JSON interface. It's very limited in functionality
|
| 456 |
+
and probably not worth using.
|
| 457 |
+
"""
|
| 458 |
+
|
| 459 |
+
def __init__(self, url, **kwargs):
|
| 460 |
+
super(PyPIJSONLocator, self).__init__(**kwargs)
|
| 461 |
+
self.base_url = ensure_slash(url)
|
| 462 |
+
|
| 463 |
+
def get_distribution_names(self):
|
| 464 |
+
"""
|
| 465 |
+
Return all the distribution names known to this locator.
|
| 466 |
+
"""
|
| 467 |
+
raise NotImplementedError('Not available from this locator')
|
| 468 |
+
|
| 469 |
+
def _get_project(self, name):
|
| 470 |
+
result = {'urls': {}, 'digests': {}}
|
| 471 |
+
url = urljoin(self.base_url, '%s/json' % quote(name))
|
| 472 |
+
try:
|
| 473 |
+
resp = self.opener.open(url)
|
| 474 |
+
data = resp.read().decode() # for now
|
| 475 |
+
d = json.loads(data)
|
| 476 |
+
md = Metadata(scheme=self.scheme)
|
| 477 |
+
data = d['info']
|
| 478 |
+
md.name = data['name']
|
| 479 |
+
md.version = data['version']
|
| 480 |
+
md.license = data.get('license')
|
| 481 |
+
md.keywords = data.get('keywords', [])
|
| 482 |
+
md.summary = data.get('summary')
|
| 483 |
+
dist = Distribution(md)
|
| 484 |
+
dist.locator = self
|
| 485 |
+
# urls = d['urls']
|
| 486 |
+
result[md.version] = dist
|
| 487 |
+
for info in d['urls']:
|
| 488 |
+
url = info['url']
|
| 489 |
+
dist.download_urls.add(url)
|
| 490 |
+
dist.digests[url] = self._get_digest(info)
|
| 491 |
+
result['urls'].setdefault(md.version, set()).add(url)
|
| 492 |
+
result['digests'][url] = self._get_digest(info)
|
| 493 |
+
# Now get other releases
|
| 494 |
+
for version, infos in d['releases'].items():
|
| 495 |
+
if version == md.version:
|
| 496 |
+
continue # already done
|
| 497 |
+
omd = Metadata(scheme=self.scheme)
|
| 498 |
+
omd.name = md.name
|
| 499 |
+
omd.version = version
|
| 500 |
+
odist = Distribution(omd)
|
| 501 |
+
odist.locator = self
|
| 502 |
+
result[version] = odist
|
| 503 |
+
for info in infos:
|
| 504 |
+
url = info['url']
|
| 505 |
+
odist.download_urls.add(url)
|
| 506 |
+
odist.digests[url] = self._get_digest(info)
|
| 507 |
+
result['urls'].setdefault(version, set()).add(url)
|
| 508 |
+
result['digests'][url] = self._get_digest(info)
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
# for info in urls:
|
| 512 |
+
# md.source_url = info['url']
|
| 513 |
+
# dist.digest = self._get_digest(info)
|
| 514 |
+
# dist.locator = self
|
| 515 |
+
# for info in urls:
|
| 516 |
+
# url = info['url']
|
| 517 |
+
# result['urls'].setdefault(md.version, set()).add(url)
|
| 518 |
+
# result['digests'][url] = self._get_digest(info)
|
| 519 |
+
except Exception as e:
|
| 520 |
+
self.errors.put(text_type(e))
|
| 521 |
+
logger.exception('JSON fetch failed: %s', e)
|
| 522 |
+
return result
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
class Page(object):
|
| 526 |
+
"""
|
| 527 |
+
This class represents a scraped HTML page.
|
| 528 |
+
"""
|
| 529 |
+
# The following slightly hairy-looking regex just looks for the contents of
|
| 530 |
+
# an anchor link, which has an attribute "href" either immediately preceded
|
| 531 |
+
# or immediately followed by a "rel" attribute. The attribute values can be
|
| 532 |
+
# declared with double quotes, single quotes or no quotes - which leads to
|
| 533 |
+
# the length of the expression.
|
| 534 |
+
_href = re.compile(
|
| 535 |
+
"""
|
| 536 |
+
(rel\\s*=\\s*(?:"(?P<rel1>[^"]*)"|'(?P<rel2>[^']*)'|(?P<rel3>[^>\\s\n]*))\\s+)?
|
| 537 |
+
href\\s*=\\s*(?:"(?P<url1>[^"]*)"|'(?P<url2>[^']*)'|(?P<url3>[^>\\s\n]*))
|
| 538 |
+
(\\s+rel\\s*=\\s*(?:"(?P<rel4>[^"]*)"|'(?P<rel5>[^']*)'|(?P<rel6>[^>\\s\n]*)))?
|
| 539 |
+
""", re.I | re.S | re.X)
|
| 540 |
+
_base = re.compile(r"""<base\s+href\s*=\s*['"]?([^'">]+)""", re.I | re.S)
|
| 541 |
+
|
| 542 |
+
def __init__(self, data, url):
|
| 543 |
+
"""
|
| 544 |
+
Initialise an instance with the Unicode page contents and the URL they
|
| 545 |
+
came from.
|
| 546 |
+
"""
|
| 547 |
+
self.data = data
|
| 548 |
+
self.base_url = self.url = url
|
| 549 |
+
m = self._base.search(self.data)
|
| 550 |
+
if m:
|
| 551 |
+
self.base_url = m.group(1)
|
| 552 |
+
|
| 553 |
+
_clean_re = re.compile(r'[^a-z0-9$&+,/:;=?@.#%_\\|-]', re.I)
|
| 554 |
+
|
| 555 |
+
@cached_property
|
| 556 |
+
def links(self):
|
| 557 |
+
"""
|
| 558 |
+
Return the URLs of all the links on a page together with information
|
| 559 |
+
about their "rel" attribute, for determining which ones to treat as
|
| 560 |
+
downloads and which ones to queue for further scraping.
|
| 561 |
+
"""
|
| 562 |
+
|
| 563 |
+
def clean(url):
|
| 564 |
+
"Tidy up an URL."
|
| 565 |
+
scheme, netloc, path, params, query, frag = urlparse(url)
|
| 566 |
+
return urlunparse((scheme, netloc, quote(path), params, query, frag))
|
| 567 |
+
|
| 568 |
+
result = set()
|
| 569 |
+
for match in self._href.finditer(self.data):
|
| 570 |
+
d = match.groupdict('')
|
| 571 |
+
rel = (d['rel1'] or d['rel2'] or d['rel3'] or d['rel4'] or d['rel5'] or d['rel6'])
|
| 572 |
+
url = d['url1'] or d['url2'] or d['url3']
|
| 573 |
+
url = urljoin(self.base_url, url)
|
| 574 |
+
url = unescape(url)
|
| 575 |
+
url = self._clean_re.sub(lambda m: '%%%2x' % ord(m.group(0)), url)
|
| 576 |
+
result.add((url, rel))
|
| 577 |
+
# We sort the result, hoping to bring the most recent versions
|
| 578 |
+
# to the front
|
| 579 |
+
result = sorted(result, key=lambda t: t[0], reverse=True)
|
| 580 |
+
return result
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
class SimpleScrapingLocator(Locator):
|
| 584 |
+
"""
|
| 585 |
+
A locator which scrapes HTML pages to locate downloads for a distribution.
|
| 586 |
+
This runs multiple threads to do the I/O; performance is at least as good
|
| 587 |
+
as pip's PackageFinder, which works in an analogous fashion.
|
| 588 |
+
"""
|
| 589 |
+
|
| 590 |
+
# These are used to deal with various Content-Encoding schemes.
|
| 591 |
+
decoders = {
|
| 592 |
+
'deflate': zlib.decompress,
|
| 593 |
+
'gzip': lambda b: gzip.GzipFile(fileobj=BytesIO(b)).read(),
|
| 594 |
+
'none': lambda b: b,
|
| 595 |
+
}
|
| 596 |
+
|
| 597 |
+
def __init__(self, url, timeout=None, num_workers=10, **kwargs):
|
| 598 |
+
"""
|
| 599 |
+
Initialise an instance.
|
| 600 |
+
:param url: The root URL to use for scraping.
|
| 601 |
+
:param timeout: The timeout, in seconds, to be applied to requests.
|
| 602 |
+
This defaults to ``None`` (no timeout specified).
|
| 603 |
+
:param num_workers: The number of worker threads you want to do I/O,
|
| 604 |
+
This defaults to 10.
|
| 605 |
+
:param kwargs: Passed to the superclass.
|
| 606 |
+
"""
|
| 607 |
+
super(SimpleScrapingLocator, self).__init__(**kwargs)
|
| 608 |
+
self.base_url = ensure_slash(url)
|
| 609 |
+
self.timeout = timeout
|
| 610 |
+
self._page_cache = {}
|
| 611 |
+
self._seen = set()
|
| 612 |
+
self._to_fetch = queue.Queue()
|
| 613 |
+
self._bad_hosts = set()
|
| 614 |
+
self.skip_externals = False
|
| 615 |
+
self.num_workers = num_workers
|
| 616 |
+
self._lock = threading.RLock()
|
| 617 |
+
# See issue #45: we need to be resilient when the locator is used
|
| 618 |
+
# in a thread, e.g. with concurrent.futures. We can't use self._lock
|
| 619 |
+
# as it is for coordinating our internal threads - the ones created
|
| 620 |
+
# in _prepare_threads.
|
| 621 |
+
self._gplock = threading.RLock()
|
| 622 |
+
self.platform_check = False # See issue #112
|
| 623 |
+
|
| 624 |
+
def _prepare_threads(self):
|
| 625 |
+
"""
|
| 626 |
+
Threads are created only when get_project is called, and terminate
|
| 627 |
+
before it returns. They are there primarily to parallelise I/O (i.e.
|
| 628 |
+
fetching web pages).
|
| 629 |
+
"""
|
| 630 |
+
self._threads = []
|
| 631 |
+
for i in range(self.num_workers):
|
| 632 |
+
t = threading.Thread(target=self._fetch)
|
| 633 |
+
t.daemon = True
|
| 634 |
+
t.start()
|
| 635 |
+
self._threads.append(t)
|
| 636 |
+
|
| 637 |
+
def _wait_threads(self):
|
| 638 |
+
"""
|
| 639 |
+
Tell all the threads to terminate (by sending a sentinel value) and
|
| 640 |
+
wait for them to do so.
|
| 641 |
+
"""
|
| 642 |
+
# Note that you need two loops, since you can't say which
|
| 643 |
+
# thread will get each sentinel
|
| 644 |
+
for t in self._threads:
|
| 645 |
+
self._to_fetch.put(None) # sentinel
|
| 646 |
+
for t in self._threads:
|
| 647 |
+
t.join()
|
| 648 |
+
self._threads = []
|
| 649 |
+
|
| 650 |
+
def _get_project(self, name):
|
| 651 |
+
result = {'urls': {}, 'digests': {}}
|
| 652 |
+
with self._gplock:
|
| 653 |
+
self.result = result
|
| 654 |
+
self.project_name = name
|
| 655 |
+
url = urljoin(self.base_url, '%s/' % quote(name))
|
| 656 |
+
self._seen.clear()
|
| 657 |
+
self._page_cache.clear()
|
| 658 |
+
self._prepare_threads()
|
| 659 |
+
try:
|
| 660 |
+
logger.debug('Queueing %s', url)
|
| 661 |
+
self._to_fetch.put(url)
|
| 662 |
+
self._to_fetch.join()
|
| 663 |
+
finally:
|
| 664 |
+
self._wait_threads()
|
| 665 |
+
del self.result
|
| 666 |
+
return result
|
| 667 |
+
|
| 668 |
+
platform_dependent = re.compile(r'\b(linux_(i\d86|x86_64|arm\w+)|'
|
| 669 |
+
r'win(32|_amd64)|macosx_?\d+)\b', re.I)
|
| 670 |
+
|
| 671 |
+
def _is_platform_dependent(self, url):
|
| 672 |
+
"""
|
| 673 |
+
Does an URL refer to a platform-specific download?
|
| 674 |
+
"""
|
| 675 |
+
return self.platform_dependent.search(url)
|
| 676 |
+
|
| 677 |
+
def _process_download(self, url):
|
| 678 |
+
"""
|
| 679 |
+
See if an URL is a suitable download for a project.
|
| 680 |
+
|
| 681 |
+
If it is, register information in the result dictionary (for
|
| 682 |
+
_get_project) about the specific version it's for.
|
| 683 |
+
|
| 684 |
+
Note that the return value isn't actually used other than as a boolean
|
| 685 |
+
value.
|
| 686 |
+
"""
|
| 687 |
+
if self.platform_check and self._is_platform_dependent(url):
|
| 688 |
+
info = None
|
| 689 |
+
else:
|
| 690 |
+
info = self.convert_url_to_download_info(url, self.project_name)
|
| 691 |
+
logger.debug('process_download: %s -> %s', url, info)
|
| 692 |
+
if info:
|
| 693 |
+
with self._lock: # needed because self.result is shared
|
| 694 |
+
self._update_version_data(self.result, info)
|
| 695 |
+
return info
|
| 696 |
+
|
| 697 |
+
def _should_queue(self, link, referrer, rel):
|
| 698 |
+
"""
|
| 699 |
+
Determine whether a link URL from a referring page and with a
|
| 700 |
+
particular "rel" attribute should be queued for scraping.
|
| 701 |
+
"""
|
| 702 |
+
scheme, netloc, path, _, _, _ = urlparse(link)
|
| 703 |
+
if path.endswith(self.source_extensions + self.binary_extensions + self.excluded_extensions):
|
| 704 |
+
result = False
|
| 705 |
+
elif self.skip_externals and not link.startswith(self.base_url):
|
| 706 |
+
result = False
|
| 707 |
+
elif not referrer.startswith(self.base_url):
|
| 708 |
+
result = False
|
| 709 |
+
elif rel not in ('homepage', 'download'):
|
| 710 |
+
result = False
|
| 711 |
+
elif scheme not in ('http', 'https', 'ftp'):
|
| 712 |
+
result = False
|
| 713 |
+
elif self._is_platform_dependent(link):
|
| 714 |
+
result = False
|
| 715 |
+
else:
|
| 716 |
+
host = netloc.split(':', 1)[0]
|
| 717 |
+
if host.lower() == 'localhost':
|
| 718 |
+
result = False
|
| 719 |
+
else:
|
| 720 |
+
result = True
|
| 721 |
+
logger.debug('should_queue: %s (%s) from %s -> %s', link, rel, referrer, result)
|
| 722 |
+
return result
|
| 723 |
+
|
| 724 |
+
def _fetch(self):
|
| 725 |
+
"""
|
| 726 |
+
Get a URL to fetch from the work queue, get the HTML page, examine its
|
| 727 |
+
links for download candidates and candidates for further scraping.
|
| 728 |
+
|
| 729 |
+
This is a handy method to run in a thread.
|
| 730 |
+
"""
|
| 731 |
+
while True:
|
| 732 |
+
url = self._to_fetch.get()
|
| 733 |
+
try:
|
| 734 |
+
if url:
|
| 735 |
+
page = self.get_page(url)
|
| 736 |
+
if page is None: # e.g. after an error
|
| 737 |
+
continue
|
| 738 |
+
for link, rel in page.links:
|
| 739 |
+
if link not in self._seen:
|
| 740 |
+
try:
|
| 741 |
+
self._seen.add(link)
|
| 742 |
+
if (not self._process_download(link) and self._should_queue(link, url, rel)):
|
| 743 |
+
logger.debug('Queueing %s from %s', link, url)
|
| 744 |
+
self._to_fetch.put(link)
|
| 745 |
+
except MetadataInvalidError: # e.g. invalid versions
|
| 746 |
+
pass
|
| 747 |
+
except Exception as e: # pragma: no cover
|
| 748 |
+
self.errors.put(text_type(e))
|
| 749 |
+
finally:
|
| 750 |
+
# always do this, to avoid hangs :-)
|
| 751 |
+
self._to_fetch.task_done()
|
| 752 |
+
if not url:
|
| 753 |
+
# logger.debug('Sentinel seen, quitting.')
|
| 754 |
+
break
|
| 755 |
+
|
| 756 |
+
def get_page(self, url):
|
| 757 |
+
"""
|
| 758 |
+
Get the HTML for an URL, possibly from an in-memory cache.
|
| 759 |
+
|
| 760 |
+
XXX TODO Note: this cache is never actually cleared. It's assumed that
|
| 761 |
+
the data won't get stale over the lifetime of a locator instance (not
|
| 762 |
+
necessarily true for the default_locator).
|
| 763 |
+
"""
|
| 764 |
+
# http://peak.telecommunity.com/DevCenter/EasyInstall#package-index-api
|
| 765 |
+
scheme, netloc, path, _, _, _ = urlparse(url)
|
| 766 |
+
if scheme == 'file' and os.path.isdir(url2pathname(path)):
|
| 767 |
+
url = urljoin(ensure_slash(url), 'index.html')
|
| 768 |
+
|
| 769 |
+
if url in self._page_cache:
|
| 770 |
+
result = self._page_cache[url]
|
| 771 |
+
logger.debug('Returning %s from cache: %s', url, result)
|
| 772 |
+
else:
|
| 773 |
+
host = netloc.split(':', 1)[0]
|
| 774 |
+
result = None
|
| 775 |
+
if host in self._bad_hosts:
|
| 776 |
+
logger.debug('Skipping %s due to bad host %s', url, host)
|
| 777 |
+
else:
|
| 778 |
+
req = Request(url, headers={'Accept-encoding': 'identity'})
|
| 779 |
+
try:
|
| 780 |
+
logger.debug('Fetching %s', url)
|
| 781 |
+
resp = self.opener.open(req, timeout=self.timeout)
|
| 782 |
+
logger.debug('Fetched %s', url)
|
| 783 |
+
headers = resp.info()
|
| 784 |
+
content_type = headers.get('Content-Type', '')
|
| 785 |
+
if HTML_CONTENT_TYPE.match(content_type):
|
| 786 |
+
final_url = resp.geturl()
|
| 787 |
+
data = resp.read()
|
| 788 |
+
encoding = headers.get('Content-Encoding')
|
| 789 |
+
if encoding:
|
| 790 |
+
decoder = self.decoders[encoding] # fail if not found
|
| 791 |
+
data = decoder(data)
|
| 792 |
+
encoding = 'utf-8'
|
| 793 |
+
m = CHARSET.search(content_type)
|
| 794 |
+
if m:
|
| 795 |
+
encoding = m.group(1)
|
| 796 |
+
try:
|
| 797 |
+
data = data.decode(encoding)
|
| 798 |
+
except UnicodeError: # pragma: no cover
|
| 799 |
+
data = data.decode('latin-1') # fallback
|
| 800 |
+
result = Page(data, final_url)
|
| 801 |
+
self._page_cache[final_url] = result
|
| 802 |
+
except HTTPError as e:
|
| 803 |
+
if e.code != 404:
|
| 804 |
+
logger.exception('Fetch failed: %s: %s', url, e)
|
| 805 |
+
except URLError as e: # pragma: no cover
|
| 806 |
+
logger.exception('Fetch failed: %s: %s', url, e)
|
| 807 |
+
with self._lock:
|
| 808 |
+
self._bad_hosts.add(host)
|
| 809 |
+
except Exception as e: # pragma: no cover
|
| 810 |
+
logger.exception('Fetch failed: %s: %s', url, e)
|
| 811 |
+
finally:
|
| 812 |
+
self._page_cache[url] = result # even if None (failure)
|
| 813 |
+
return result
|
| 814 |
+
|
| 815 |
+
_distname_re = re.compile('<a href=[^>]*>([^<]+)<')
|
| 816 |
+
|
| 817 |
+
def get_distribution_names(self):
|
| 818 |
+
"""
|
| 819 |
+
Return all the distribution names known to this locator.
|
| 820 |
+
"""
|
| 821 |
+
result = set()
|
| 822 |
+
page = self.get_page(self.base_url)
|
| 823 |
+
if not page:
|
| 824 |
+
raise DistlibException('Unable to get %s' % self.base_url)
|
| 825 |
+
for match in self._distname_re.finditer(page.data):
|
| 826 |
+
result.add(match.group(1))
|
| 827 |
+
return result
|
| 828 |
+
|
| 829 |
+
|
| 830 |
+
class DirectoryLocator(Locator):
|
| 831 |
+
"""
|
| 832 |
+
This class locates distributions in a directory tree.
|
| 833 |
+
"""
|
| 834 |
+
|
| 835 |
+
def __init__(self, path, **kwargs):
|
| 836 |
+
"""
|
| 837 |
+
Initialise an instance.
|
| 838 |
+
:param path: The root of the directory tree to search.
|
| 839 |
+
:param kwargs: Passed to the superclass constructor,
|
| 840 |
+
except for:
|
| 841 |
+
* recursive - if True (the default), subdirectories are
|
| 842 |
+
recursed into. If False, only the top-level directory
|
| 843 |
+
is searched,
|
| 844 |
+
"""
|
| 845 |
+
self.recursive = kwargs.pop('recursive', True)
|
| 846 |
+
super(DirectoryLocator, self).__init__(**kwargs)
|
| 847 |
+
path = os.path.abspath(path)
|
| 848 |
+
if not os.path.isdir(path): # pragma: no cover
|
| 849 |
+
raise DistlibException('Not a directory: %r' % path)
|
| 850 |
+
self.base_dir = path
|
| 851 |
+
|
| 852 |
+
def should_include(self, filename, parent):
|
| 853 |
+
"""
|
| 854 |
+
Should a filename be considered as a candidate for a distribution
|
| 855 |
+
archive? As well as the filename, the directory which contains it
|
| 856 |
+
is provided, though not used by the current implementation.
|
| 857 |
+
"""
|
| 858 |
+
return filename.endswith(self.downloadable_extensions)
|
| 859 |
+
|
| 860 |
+
def _get_project(self, name):
|
| 861 |
+
result = {'urls': {}, 'digests': {}}
|
| 862 |
+
for root, dirs, files in os.walk(self.base_dir):
|
| 863 |
+
for fn in files:
|
| 864 |
+
if self.should_include(fn, root):
|
| 865 |
+
fn = os.path.join(root, fn)
|
| 866 |
+
url = urlunparse(('file', '', pathname2url(os.path.abspath(fn)), '', '', ''))
|
| 867 |
+
info = self.convert_url_to_download_info(url, name)
|
| 868 |
+
if info:
|
| 869 |
+
self._update_version_data(result, info)
|
| 870 |
+
if not self.recursive:
|
| 871 |
+
break
|
| 872 |
+
return result
|
| 873 |
+
|
| 874 |
+
def get_distribution_names(self):
|
| 875 |
+
"""
|
| 876 |
+
Return all the distribution names known to this locator.
|
| 877 |
+
"""
|
| 878 |
+
result = set()
|
| 879 |
+
for root, dirs, files in os.walk(self.base_dir):
|
| 880 |
+
for fn in files:
|
| 881 |
+
if self.should_include(fn, root):
|
| 882 |
+
fn = os.path.join(root, fn)
|
| 883 |
+
url = urlunparse(('file', '', pathname2url(os.path.abspath(fn)), '', '', ''))
|
| 884 |
+
info = self.convert_url_to_download_info(url, None)
|
| 885 |
+
if info:
|
| 886 |
+
result.add(info['name'])
|
| 887 |
+
if not self.recursive:
|
| 888 |
+
break
|
| 889 |
+
return result
|
| 890 |
+
|
| 891 |
+
|
| 892 |
+
class JSONLocator(Locator):
|
| 893 |
+
"""
|
| 894 |
+
This locator uses special extended metadata (not available on PyPI) and is
|
| 895 |
+
the basis of performant dependency resolution in distlib. Other locators
|
| 896 |
+
require archive downloads before dependencies can be determined! As you
|
| 897 |
+
might imagine, that can be slow.
|
| 898 |
+
"""
|
| 899 |
+
|
| 900 |
+
def get_distribution_names(self):
|
| 901 |
+
"""
|
| 902 |
+
Return all the distribution names known to this locator.
|
| 903 |
+
"""
|
| 904 |
+
raise NotImplementedError('Not available from this locator')
|
| 905 |
+
|
| 906 |
+
def _get_project(self, name):
|
| 907 |
+
result = {'urls': {}, 'digests': {}}
|
| 908 |
+
data = get_project_data(name)
|
| 909 |
+
if data:
|
| 910 |
+
for info in data.get('files', []):
|
| 911 |
+
if info['ptype'] != 'sdist' or info['pyversion'] != 'source':
|
| 912 |
+
continue
|
| 913 |
+
# We don't store summary in project metadata as it makes
|
| 914 |
+
# the data bigger for no benefit during dependency
|
| 915 |
+
# resolution
|
| 916 |
+
dist = make_dist(data['name'],
|
| 917 |
+
info['version'],
|
| 918 |
+
summary=data.get('summary', 'Placeholder for summary'),
|
| 919 |
+
scheme=self.scheme)
|
| 920 |
+
md = dist.metadata
|
| 921 |
+
md.source_url = info['url']
|
| 922 |
+
# TODO SHA256 digest
|
| 923 |
+
if 'digest' in info and info['digest']:
|
| 924 |
+
dist.digest = ('md5', info['digest'])
|
| 925 |
+
md.dependencies = info.get('requirements', {})
|
| 926 |
+
dist.exports = info.get('exports', {})
|
| 927 |
+
result[dist.version] = dist
|
| 928 |
+
result['urls'].setdefault(dist.version, set()).add(info['url'])
|
| 929 |
+
return result
|
| 930 |
+
|
| 931 |
+
|
| 932 |
+
class DistPathLocator(Locator):
|
| 933 |
+
"""
|
| 934 |
+
This locator finds installed distributions in a path. It can be useful for
|
| 935 |
+
adding to an :class:`AggregatingLocator`.
|
| 936 |
+
"""
|
| 937 |
+
|
| 938 |
+
def __init__(self, distpath, **kwargs):
|
| 939 |
+
"""
|
| 940 |
+
Initialise an instance.
|
| 941 |
+
|
| 942 |
+
:param distpath: A :class:`DistributionPath` instance to search.
|
| 943 |
+
"""
|
| 944 |
+
super(DistPathLocator, self).__init__(**kwargs)
|
| 945 |
+
assert isinstance(distpath, DistributionPath)
|
| 946 |
+
self.distpath = distpath
|
| 947 |
+
|
| 948 |
+
def _get_project(self, name):
|
| 949 |
+
dist = self.distpath.get_distribution(name)
|
| 950 |
+
if dist is None:
|
| 951 |
+
result = {'urls': {}, 'digests': {}}
|
| 952 |
+
else:
|
| 953 |
+
result = {
|
| 954 |
+
dist.version: dist,
|
| 955 |
+
'urls': {
|
| 956 |
+
dist.version: set([dist.source_url])
|
| 957 |
+
},
|
| 958 |
+
'digests': {
|
| 959 |
+
dist.version: set([None])
|
| 960 |
+
}
|
| 961 |
+
}
|
| 962 |
+
return result
|
| 963 |
+
|
| 964 |
+
|
| 965 |
+
class AggregatingLocator(Locator):
|
| 966 |
+
"""
|
| 967 |
+
This class allows you to chain and/or merge a list of locators.
|
| 968 |
+
"""
|
| 969 |
+
|
| 970 |
+
def __init__(self, *locators, **kwargs):
|
| 971 |
+
"""
|
| 972 |
+
Initialise an instance.
|
| 973 |
+
|
| 974 |
+
:param locators: The list of locators to search.
|
| 975 |
+
:param kwargs: Passed to the superclass constructor,
|
| 976 |
+
except for:
|
| 977 |
+
* merge - if False (the default), the first successful
|
| 978 |
+
search from any of the locators is returned. If True,
|
| 979 |
+
the results from all locators are merged (this can be
|
| 980 |
+
slow).
|
| 981 |
+
"""
|
| 982 |
+
self.merge = kwargs.pop('merge', False)
|
| 983 |
+
self.locators = locators
|
| 984 |
+
super(AggregatingLocator, self).__init__(**kwargs)
|
| 985 |
+
|
| 986 |
+
def clear_cache(self):
|
| 987 |
+
super(AggregatingLocator, self).clear_cache()
|
| 988 |
+
for locator in self.locators:
|
| 989 |
+
locator.clear_cache()
|
| 990 |
+
|
| 991 |
+
def _set_scheme(self, value):
|
| 992 |
+
self._scheme = value
|
| 993 |
+
for locator in self.locators:
|
| 994 |
+
locator.scheme = value
|
| 995 |
+
|
| 996 |
+
scheme = property(Locator.scheme.fget, _set_scheme)
|
| 997 |
+
|
| 998 |
+
def _get_project(self, name):
|
| 999 |
+
result = {}
|
| 1000 |
+
for locator in self.locators:
|
| 1001 |
+
d = locator.get_project(name)
|
| 1002 |
+
if d:
|
| 1003 |
+
if self.merge:
|
| 1004 |
+
files = result.get('urls', {})
|
| 1005 |
+
digests = result.get('digests', {})
|
| 1006 |
+
# next line could overwrite result['urls'], result['digests']
|
| 1007 |
+
result.update(d)
|
| 1008 |
+
df = result.get('urls')
|
| 1009 |
+
if files and df:
|
| 1010 |
+
for k, v in files.items():
|
| 1011 |
+
if k in df:
|
| 1012 |
+
df[k] |= v
|
| 1013 |
+
else:
|
| 1014 |
+
df[k] = v
|
| 1015 |
+
dd = result.get('digests')
|
| 1016 |
+
if digests and dd:
|
| 1017 |
+
dd.update(digests)
|
| 1018 |
+
else:
|
| 1019 |
+
# See issue #18. If any dists are found and we're looking
|
| 1020 |
+
# for specific constraints, we only return something if
|
| 1021 |
+
# a match is found. For example, if a DirectoryLocator
|
| 1022 |
+
# returns just foo (1.0) while we're looking for
|
| 1023 |
+
# foo (>= 2.0), we'll pretend there was nothing there so
|
| 1024 |
+
# that subsequent locators can be queried. Otherwise we
|
| 1025 |
+
# would just return foo (1.0) which would then lead to a
|
| 1026 |
+
# failure to find foo (>= 2.0), because other locators
|
| 1027 |
+
# weren't searched. Note that this only matters when
|
| 1028 |
+
# merge=False.
|
| 1029 |
+
if self.matcher is None:
|
| 1030 |
+
found = True
|
| 1031 |
+
else:
|
| 1032 |
+
found = False
|
| 1033 |
+
for k in d:
|
| 1034 |
+
if self.matcher.match(k):
|
| 1035 |
+
found = True
|
| 1036 |
+
break
|
| 1037 |
+
if found:
|
| 1038 |
+
result = d
|
| 1039 |
+
break
|
| 1040 |
+
return result
|
| 1041 |
+
|
| 1042 |
+
def get_distribution_names(self):
|
| 1043 |
+
"""
|
| 1044 |
+
Return all the distribution names known to this locator.
|
| 1045 |
+
"""
|
| 1046 |
+
result = set()
|
| 1047 |
+
for locator in self.locators:
|
| 1048 |
+
try:
|
| 1049 |
+
result |= locator.get_distribution_names()
|
| 1050 |
+
except NotImplementedError:
|
| 1051 |
+
pass
|
| 1052 |
+
return result
|
| 1053 |
+
|
| 1054 |
+
|
| 1055 |
+
# We use a legacy scheme simply because most of the dists on PyPI use legacy
|
| 1056 |
+
# versions which don't conform to PEP 440.
|
| 1057 |
+
default_locator = AggregatingLocator(
|
| 1058 |
+
# JSONLocator(), # don't use as PEP 426 is withdrawn
|
| 1059 |
+
SimpleScrapingLocator('https://pypi.org/simple/', timeout=3.0),
|
| 1060 |
+
scheme='legacy')
|
| 1061 |
+
|
| 1062 |
+
locate = default_locator.locate
|
| 1063 |
+
|
| 1064 |
+
|
| 1065 |
+
class DependencyFinder(object):
|
| 1066 |
+
"""
|
| 1067 |
+
Locate dependencies for distributions.
|
| 1068 |
+
"""
|
| 1069 |
+
|
| 1070 |
+
def __init__(self, locator=None):
|
| 1071 |
+
"""
|
| 1072 |
+
Initialise an instance, using the specified locator
|
| 1073 |
+
to locate distributions.
|
| 1074 |
+
"""
|
| 1075 |
+
self.locator = locator or default_locator
|
| 1076 |
+
self.scheme = get_scheme(self.locator.scheme)
|
| 1077 |
+
|
| 1078 |
+
def add_distribution(self, dist):
|
| 1079 |
+
"""
|
| 1080 |
+
Add a distribution to the finder. This will update internal information
|
| 1081 |
+
about who provides what.
|
| 1082 |
+
:param dist: The distribution to add.
|
| 1083 |
+
"""
|
| 1084 |
+
logger.debug('adding distribution %s', dist)
|
| 1085 |
+
name = dist.key
|
| 1086 |
+
self.dists_by_name[name] = dist
|
| 1087 |
+
self.dists[(name, dist.version)] = dist
|
| 1088 |
+
for p in dist.provides:
|
| 1089 |
+
name, version = parse_name_and_version(p)
|
| 1090 |
+
logger.debug('Add to provided: %s, %s, %s', name, version, dist)
|
| 1091 |
+
self.provided.setdefault(name, set()).add((version, dist))
|
| 1092 |
+
|
| 1093 |
+
def remove_distribution(self, dist):
|
| 1094 |
+
"""
|
| 1095 |
+
Remove a distribution from the finder. This will update internal
|
| 1096 |
+
information about who provides what.
|
| 1097 |
+
:param dist: The distribution to remove.
|
| 1098 |
+
"""
|
| 1099 |
+
logger.debug('removing distribution %s', dist)
|
| 1100 |
+
name = dist.key
|
| 1101 |
+
del self.dists_by_name[name]
|
| 1102 |
+
del self.dists[(name, dist.version)]
|
| 1103 |
+
for p in dist.provides:
|
| 1104 |
+
name, version = parse_name_and_version(p)
|
| 1105 |
+
logger.debug('Remove from provided: %s, %s, %s', name, version, dist)
|
| 1106 |
+
s = self.provided[name]
|
| 1107 |
+
s.remove((version, dist))
|
| 1108 |
+
if not s:
|
| 1109 |
+
del self.provided[name]
|
| 1110 |
+
|
| 1111 |
+
def get_matcher(self, reqt):
|
| 1112 |
+
"""
|
| 1113 |
+
Get a version matcher for a requirement.
|
| 1114 |
+
:param reqt: The requirement
|
| 1115 |
+
:type reqt: str
|
| 1116 |
+
:return: A version matcher (an instance of
|
| 1117 |
+
:class:`distlib.version.Matcher`).
|
| 1118 |
+
"""
|
| 1119 |
+
try:
|
| 1120 |
+
matcher = self.scheme.matcher(reqt)
|
| 1121 |
+
except UnsupportedVersionError: # pragma: no cover
|
| 1122 |
+
# XXX compat-mode if cannot read the version
|
| 1123 |
+
name = reqt.split()[0]
|
| 1124 |
+
matcher = self.scheme.matcher(name)
|
| 1125 |
+
return matcher
|
| 1126 |
+
|
| 1127 |
+
def find_providers(self, reqt):
|
| 1128 |
+
"""
|
| 1129 |
+
Find the distributions which can fulfill a requirement.
|
| 1130 |
+
|
| 1131 |
+
:param reqt: The requirement.
|
| 1132 |
+
:type reqt: str
|
| 1133 |
+
:return: A set of distribution which can fulfill the requirement.
|
| 1134 |
+
"""
|
| 1135 |
+
matcher = self.get_matcher(reqt)
|
| 1136 |
+
name = matcher.key # case-insensitive
|
| 1137 |
+
result = set()
|
| 1138 |
+
provided = self.provided
|
| 1139 |
+
if name in provided:
|
| 1140 |
+
for version, provider in provided[name]:
|
| 1141 |
+
try:
|
| 1142 |
+
match = matcher.match(version)
|
| 1143 |
+
except UnsupportedVersionError:
|
| 1144 |
+
match = False
|
| 1145 |
+
|
| 1146 |
+
if match:
|
| 1147 |
+
result.add(provider)
|
| 1148 |
+
break
|
| 1149 |
+
return result
|
| 1150 |
+
|
| 1151 |
+
def try_to_replace(self, provider, other, problems):
|
| 1152 |
+
"""
|
| 1153 |
+
Attempt to replace one provider with another. This is typically used
|
| 1154 |
+
when resolving dependencies from multiple sources, e.g. A requires
|
| 1155 |
+
(B >= 1.0) while C requires (B >= 1.1).
|
| 1156 |
+
|
| 1157 |
+
For successful replacement, ``provider`` must meet all the requirements
|
| 1158 |
+
which ``other`` fulfills.
|
| 1159 |
+
|
| 1160 |
+
:param provider: The provider we are trying to replace with.
|
| 1161 |
+
:param other: The provider we're trying to replace.
|
| 1162 |
+
:param problems: If False is returned, this will contain what
|
| 1163 |
+
problems prevented replacement. This is currently
|
| 1164 |
+
a tuple of the literal string 'cantreplace',
|
| 1165 |
+
``provider``, ``other`` and the set of requirements
|
| 1166 |
+
that ``provider`` couldn't fulfill.
|
| 1167 |
+
:return: True if we can replace ``other`` with ``provider``, else
|
| 1168 |
+
False.
|
| 1169 |
+
"""
|
| 1170 |
+
rlist = self.reqts[other]
|
| 1171 |
+
unmatched = set()
|
| 1172 |
+
for s in rlist:
|
| 1173 |
+
matcher = self.get_matcher(s)
|
| 1174 |
+
if not matcher.match(provider.version):
|
| 1175 |
+
unmatched.add(s)
|
| 1176 |
+
if unmatched:
|
| 1177 |
+
# can't replace other with provider
|
| 1178 |
+
problems.add(('cantreplace', provider, other, frozenset(unmatched)))
|
| 1179 |
+
result = False
|
| 1180 |
+
else:
|
| 1181 |
+
# can replace other with provider
|
| 1182 |
+
self.remove_distribution(other)
|
| 1183 |
+
del self.reqts[other]
|
| 1184 |
+
for s in rlist:
|
| 1185 |
+
self.reqts.setdefault(provider, set()).add(s)
|
| 1186 |
+
self.add_distribution(provider)
|
| 1187 |
+
result = True
|
| 1188 |
+
return result
|
| 1189 |
+
|
| 1190 |
+
def find(self, requirement, meta_extras=None, prereleases=False):
|
| 1191 |
+
"""
|
| 1192 |
+
Find a distribution and all distributions it depends on.
|
| 1193 |
+
|
| 1194 |
+
:param requirement: The requirement specifying the distribution to
|
| 1195 |
+
find, or a Distribution instance.
|
| 1196 |
+
:param meta_extras: A list of meta extras such as :test:, :build: and
|
| 1197 |
+
so on.
|
| 1198 |
+
:param prereleases: If ``True``, allow pre-release versions to be
|
| 1199 |
+
returned - otherwise, don't return prereleases
|
| 1200 |
+
unless they're all that's available.
|
| 1201 |
+
|
| 1202 |
+
Return a set of :class:`Distribution` instances and a set of
|
| 1203 |
+
problems.
|
| 1204 |
+
|
| 1205 |
+
The distributions returned should be such that they have the
|
| 1206 |
+
:attr:`required` attribute set to ``True`` if they were
|
| 1207 |
+
from the ``requirement`` passed to ``find()``, and they have the
|
| 1208 |
+
:attr:`build_time_dependency` attribute set to ``True`` unless they
|
| 1209 |
+
are post-installation dependencies of the ``requirement``.
|
| 1210 |
+
|
| 1211 |
+
The problems should be a tuple consisting of the string
|
| 1212 |
+
``'unsatisfied'`` and the requirement which couldn't be satisfied
|
| 1213 |
+
by any distribution known to the locator.
|
| 1214 |
+
"""
|
| 1215 |
+
|
| 1216 |
+
self.provided = {}
|
| 1217 |
+
self.dists = {}
|
| 1218 |
+
self.dists_by_name = {}
|
| 1219 |
+
self.reqts = {}
|
| 1220 |
+
|
| 1221 |
+
meta_extras = set(meta_extras or [])
|
| 1222 |
+
if ':*:' in meta_extras:
|
| 1223 |
+
meta_extras.remove(':*:')
|
| 1224 |
+
# :meta: and :run: are implicitly included
|
| 1225 |
+
meta_extras |= set([':test:', ':build:', ':dev:'])
|
| 1226 |
+
|
| 1227 |
+
if isinstance(requirement, Distribution):
|
| 1228 |
+
dist = odist = requirement
|
| 1229 |
+
logger.debug('passed %s as requirement', odist)
|
| 1230 |
+
else:
|
| 1231 |
+
dist = odist = self.locator.locate(requirement, prereleases=prereleases)
|
| 1232 |
+
if dist is None:
|
| 1233 |
+
raise DistlibException('Unable to locate %r' % requirement)
|
| 1234 |
+
logger.debug('located %s', odist)
|
| 1235 |
+
dist.requested = True
|
| 1236 |
+
problems = set()
|
| 1237 |
+
todo = set([dist])
|
| 1238 |
+
install_dists = set([odist])
|
| 1239 |
+
while todo:
|
| 1240 |
+
dist = todo.pop()
|
| 1241 |
+
name = dist.key # case-insensitive
|
| 1242 |
+
if name not in self.dists_by_name:
|
| 1243 |
+
self.add_distribution(dist)
|
| 1244 |
+
else:
|
| 1245 |
+
# import pdb; pdb.set_trace()
|
| 1246 |
+
other = self.dists_by_name[name]
|
| 1247 |
+
if other != dist:
|
| 1248 |
+
self.try_to_replace(dist, other, problems)
|
| 1249 |
+
|
| 1250 |
+
ireqts = dist.run_requires | dist.meta_requires
|
| 1251 |
+
sreqts = dist.build_requires
|
| 1252 |
+
ereqts = set()
|
| 1253 |
+
if meta_extras and dist in install_dists:
|
| 1254 |
+
for key in ('test', 'build', 'dev'):
|
| 1255 |
+
e = ':%s:' % key
|
| 1256 |
+
if e in meta_extras:
|
| 1257 |
+
ereqts |= getattr(dist, '%s_requires' % key)
|
| 1258 |
+
all_reqts = ireqts | sreqts | ereqts
|
| 1259 |
+
for r in all_reqts:
|
| 1260 |
+
providers = self.find_providers(r)
|
| 1261 |
+
if not providers:
|
| 1262 |
+
logger.debug('No providers found for %r', r)
|
| 1263 |
+
provider = self.locator.locate(r, prereleases=prereleases)
|
| 1264 |
+
# If no provider is found and we didn't consider
|
| 1265 |
+
# prereleases, consider them now.
|
| 1266 |
+
if provider is None and not prereleases:
|
| 1267 |
+
provider = self.locator.locate(r, prereleases=True)
|
| 1268 |
+
if provider is None:
|
| 1269 |
+
logger.debug('Cannot satisfy %r', r)
|
| 1270 |
+
problems.add(('unsatisfied', r))
|
| 1271 |
+
else:
|
| 1272 |
+
n, v = provider.key, provider.version
|
| 1273 |
+
if (n, v) not in self.dists:
|
| 1274 |
+
todo.add(provider)
|
| 1275 |
+
providers.add(provider)
|
| 1276 |
+
if r in ireqts and dist in install_dists:
|
| 1277 |
+
install_dists.add(provider)
|
| 1278 |
+
logger.debug('Adding %s to install_dists', provider.name_and_version)
|
| 1279 |
+
for p in providers:
|
| 1280 |
+
name = p.key
|
| 1281 |
+
if name not in self.dists_by_name:
|
| 1282 |
+
self.reqts.setdefault(p, set()).add(r)
|
| 1283 |
+
else:
|
| 1284 |
+
other = self.dists_by_name[name]
|
| 1285 |
+
if other != p:
|
| 1286 |
+
# see if other can be replaced by p
|
| 1287 |
+
self.try_to_replace(p, other, problems)
|
| 1288 |
+
|
| 1289 |
+
dists = set(self.dists.values())
|
| 1290 |
+
for dist in dists:
|
| 1291 |
+
dist.build_time_dependency = dist not in install_dists
|
| 1292 |
+
if dist.build_time_dependency:
|
| 1293 |
+
logger.debug('%s is a build-time dependency only.', dist.name_and_version)
|
| 1294 |
+
logger.debug('find done for %s', odist)
|
| 1295 |
+
return dists, problems
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/manifest.py
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2012-2023 Python Software Foundation.
|
| 4 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 5 |
+
#
|
| 6 |
+
"""
|
| 7 |
+
Class representing the list of files in a distribution.
|
| 8 |
+
|
| 9 |
+
Equivalent to distutils.filelist, but fixes some problems.
|
| 10 |
+
"""
|
| 11 |
+
import fnmatch
|
| 12 |
+
import logging
|
| 13 |
+
import os
|
| 14 |
+
import re
|
| 15 |
+
import sys
|
| 16 |
+
|
| 17 |
+
from . import DistlibException
|
| 18 |
+
from .compat import fsdecode
|
| 19 |
+
from .util import convert_path
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
__all__ = ['Manifest']
|
| 23 |
+
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
# a \ followed by some spaces + EOL
|
| 27 |
+
_COLLAPSE_PATTERN = re.compile('\\\\w*\n', re.M)
|
| 28 |
+
_COMMENTED_LINE = re.compile('#.*?(?=\n)|\n(?=$)', re.M | re.S)
|
| 29 |
+
|
| 30 |
+
#
|
| 31 |
+
# Due to the different results returned by fnmatch.translate, we need
|
| 32 |
+
# to do slightly different processing for Python 2.7 and 3.2 ... this needed
|
| 33 |
+
# to be brought in for Python 3.6 onwards.
|
| 34 |
+
#
|
| 35 |
+
_PYTHON_VERSION = sys.version_info[:2]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class Manifest(object):
|
| 39 |
+
"""
|
| 40 |
+
A list of files built by exploring the filesystem and filtered by applying various
|
| 41 |
+
patterns to what we find there.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
def __init__(self, base=None):
|
| 45 |
+
"""
|
| 46 |
+
Initialise an instance.
|
| 47 |
+
|
| 48 |
+
:param base: The base directory to explore under.
|
| 49 |
+
"""
|
| 50 |
+
self.base = os.path.abspath(os.path.normpath(base or os.getcwd()))
|
| 51 |
+
self.prefix = self.base + os.sep
|
| 52 |
+
self.allfiles = None
|
| 53 |
+
self.files = set()
|
| 54 |
+
|
| 55 |
+
#
|
| 56 |
+
# Public API
|
| 57 |
+
#
|
| 58 |
+
|
| 59 |
+
def findall(self):
|
| 60 |
+
"""Find all files under the base and set ``allfiles`` to the absolute
|
| 61 |
+
pathnames of files found.
|
| 62 |
+
"""
|
| 63 |
+
from stat import S_ISREG, S_ISDIR, S_ISLNK
|
| 64 |
+
|
| 65 |
+
self.allfiles = allfiles = []
|
| 66 |
+
root = self.base
|
| 67 |
+
stack = [root]
|
| 68 |
+
pop = stack.pop
|
| 69 |
+
push = stack.append
|
| 70 |
+
|
| 71 |
+
while stack:
|
| 72 |
+
root = pop()
|
| 73 |
+
names = os.listdir(root)
|
| 74 |
+
|
| 75 |
+
for name in names:
|
| 76 |
+
fullname = os.path.join(root, name)
|
| 77 |
+
|
| 78 |
+
# Avoid excess stat calls -- just one will do, thank you!
|
| 79 |
+
stat = os.stat(fullname)
|
| 80 |
+
mode = stat.st_mode
|
| 81 |
+
if S_ISREG(mode):
|
| 82 |
+
allfiles.append(fsdecode(fullname))
|
| 83 |
+
elif S_ISDIR(mode) and not S_ISLNK(mode):
|
| 84 |
+
push(fullname)
|
| 85 |
+
|
| 86 |
+
def add(self, item):
|
| 87 |
+
"""
|
| 88 |
+
Add a file to the manifest.
|
| 89 |
+
|
| 90 |
+
:param item: The pathname to add. This can be relative to the base.
|
| 91 |
+
"""
|
| 92 |
+
if not item.startswith(self.prefix):
|
| 93 |
+
item = os.path.join(self.base, item)
|
| 94 |
+
self.files.add(os.path.normpath(item))
|
| 95 |
+
|
| 96 |
+
def add_many(self, items):
|
| 97 |
+
"""
|
| 98 |
+
Add a list of files to the manifest.
|
| 99 |
+
|
| 100 |
+
:param items: The pathnames to add. These can be relative to the base.
|
| 101 |
+
"""
|
| 102 |
+
for item in items:
|
| 103 |
+
self.add(item)
|
| 104 |
+
|
| 105 |
+
def sorted(self, wantdirs=False):
|
| 106 |
+
"""
|
| 107 |
+
Return sorted files in directory order
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
def add_dir(dirs, d):
|
| 111 |
+
dirs.add(d)
|
| 112 |
+
logger.debug('add_dir added %s', d)
|
| 113 |
+
if d != self.base:
|
| 114 |
+
parent, _ = os.path.split(d)
|
| 115 |
+
assert parent not in ('', '/')
|
| 116 |
+
add_dir(dirs, parent)
|
| 117 |
+
|
| 118 |
+
result = set(self.files) # make a copy!
|
| 119 |
+
if wantdirs:
|
| 120 |
+
dirs = set()
|
| 121 |
+
for f in result:
|
| 122 |
+
add_dir(dirs, os.path.dirname(f))
|
| 123 |
+
result |= dirs
|
| 124 |
+
return [os.path.join(*path_tuple) for path_tuple in
|
| 125 |
+
sorted(os.path.split(path) for path in result)]
|
| 126 |
+
|
| 127 |
+
def clear(self):
|
| 128 |
+
"""Clear all collected files."""
|
| 129 |
+
self.files = set()
|
| 130 |
+
self.allfiles = []
|
| 131 |
+
|
| 132 |
+
def process_directive(self, directive):
|
| 133 |
+
"""
|
| 134 |
+
Process a directive which either adds some files from ``allfiles`` to
|
| 135 |
+
``files``, or removes some files from ``files``.
|
| 136 |
+
|
| 137 |
+
:param directive: The directive to process. This should be in a format
|
| 138 |
+
compatible with distutils ``MANIFEST.in`` files:
|
| 139 |
+
|
| 140 |
+
http://docs.python.org/distutils/sourcedist.html#commands
|
| 141 |
+
"""
|
| 142 |
+
# Parse the line: split it up, make sure the right number of words
|
| 143 |
+
# is there, and return the relevant words. 'action' is always
|
| 144 |
+
# defined: it's the first word of the line. Which of the other
|
| 145 |
+
# three are defined depends on the action; it'll be either
|
| 146 |
+
# patterns, (dir and patterns), or (dirpattern).
|
| 147 |
+
action, patterns, thedir, dirpattern = self._parse_directive(directive)
|
| 148 |
+
|
| 149 |
+
# OK, now we know that the action is valid and we have the
|
| 150 |
+
# right number of words on the line for that action -- so we
|
| 151 |
+
# can proceed with minimal error-checking.
|
| 152 |
+
if action == 'include':
|
| 153 |
+
for pattern in patterns:
|
| 154 |
+
if not self._include_pattern(pattern, anchor=True):
|
| 155 |
+
logger.warning('no files found matching %r', pattern)
|
| 156 |
+
|
| 157 |
+
elif action == 'exclude':
|
| 158 |
+
for pattern in patterns:
|
| 159 |
+
self._exclude_pattern(pattern, anchor=True)
|
| 160 |
+
|
| 161 |
+
elif action == 'global-include':
|
| 162 |
+
for pattern in patterns:
|
| 163 |
+
if not self._include_pattern(pattern, anchor=False):
|
| 164 |
+
logger.warning('no files found matching %r '
|
| 165 |
+
'anywhere in distribution', pattern)
|
| 166 |
+
|
| 167 |
+
elif action == 'global-exclude':
|
| 168 |
+
for pattern in patterns:
|
| 169 |
+
self._exclude_pattern(pattern, anchor=False)
|
| 170 |
+
|
| 171 |
+
elif action == 'recursive-include':
|
| 172 |
+
for pattern in patterns:
|
| 173 |
+
if not self._include_pattern(pattern, prefix=thedir):
|
| 174 |
+
logger.warning('no files found matching %r '
|
| 175 |
+
'under directory %r', pattern, thedir)
|
| 176 |
+
|
| 177 |
+
elif action == 'recursive-exclude':
|
| 178 |
+
for pattern in patterns:
|
| 179 |
+
self._exclude_pattern(pattern, prefix=thedir)
|
| 180 |
+
|
| 181 |
+
elif action == 'graft':
|
| 182 |
+
if not self._include_pattern(None, prefix=dirpattern):
|
| 183 |
+
logger.warning('no directories found matching %r',
|
| 184 |
+
dirpattern)
|
| 185 |
+
|
| 186 |
+
elif action == 'prune':
|
| 187 |
+
if not self._exclude_pattern(None, prefix=dirpattern):
|
| 188 |
+
logger.warning('no previously-included directories found '
|
| 189 |
+
'matching %r', dirpattern)
|
| 190 |
+
else: # pragma: no cover
|
| 191 |
+
# This should never happen, as it should be caught in
|
| 192 |
+
# _parse_template_line
|
| 193 |
+
raise DistlibException(
|
| 194 |
+
'invalid action %r' % action)
|
| 195 |
+
|
| 196 |
+
#
|
| 197 |
+
# Private API
|
| 198 |
+
#
|
| 199 |
+
|
| 200 |
+
def _parse_directive(self, directive):
|
| 201 |
+
"""
|
| 202 |
+
Validate a directive.
|
| 203 |
+
:param directive: The directive to validate.
|
| 204 |
+
:return: A tuple of action, patterns, thedir, dir_patterns
|
| 205 |
+
"""
|
| 206 |
+
words = directive.split()
|
| 207 |
+
if len(words) == 1 and words[0] not in ('include', 'exclude',
|
| 208 |
+
'global-include',
|
| 209 |
+
'global-exclude',
|
| 210 |
+
'recursive-include',
|
| 211 |
+
'recursive-exclude',
|
| 212 |
+
'graft', 'prune'):
|
| 213 |
+
# no action given, let's use the default 'include'
|
| 214 |
+
words.insert(0, 'include')
|
| 215 |
+
|
| 216 |
+
action = words[0]
|
| 217 |
+
patterns = thedir = dir_pattern = None
|
| 218 |
+
|
| 219 |
+
if action in ('include', 'exclude',
|
| 220 |
+
'global-include', 'global-exclude'):
|
| 221 |
+
if len(words) < 2:
|
| 222 |
+
raise DistlibException(
|
| 223 |
+
'%r expects <pattern1> <pattern2> ...' % action)
|
| 224 |
+
|
| 225 |
+
patterns = [convert_path(word) for word in words[1:]]
|
| 226 |
+
|
| 227 |
+
elif action in ('recursive-include', 'recursive-exclude'):
|
| 228 |
+
if len(words) < 3:
|
| 229 |
+
raise DistlibException(
|
| 230 |
+
'%r expects <dir> <pattern1> <pattern2> ...' % action)
|
| 231 |
+
|
| 232 |
+
thedir = convert_path(words[1])
|
| 233 |
+
patterns = [convert_path(word) for word in words[2:]]
|
| 234 |
+
|
| 235 |
+
elif action in ('graft', 'prune'):
|
| 236 |
+
if len(words) != 2:
|
| 237 |
+
raise DistlibException(
|
| 238 |
+
'%r expects a single <dir_pattern>' % action)
|
| 239 |
+
|
| 240 |
+
dir_pattern = convert_path(words[1])
|
| 241 |
+
|
| 242 |
+
else:
|
| 243 |
+
raise DistlibException('unknown action %r' % action)
|
| 244 |
+
|
| 245 |
+
return action, patterns, thedir, dir_pattern
|
| 246 |
+
|
| 247 |
+
def _include_pattern(self, pattern, anchor=True, prefix=None,
|
| 248 |
+
is_regex=False):
|
| 249 |
+
"""Select strings (presumably filenames) from 'self.files' that
|
| 250 |
+
match 'pattern', a Unix-style wildcard (glob) pattern.
|
| 251 |
+
|
| 252 |
+
Patterns are not quite the same as implemented by the 'fnmatch'
|
| 253 |
+
module: '*' and '?' match non-special characters, where "special"
|
| 254 |
+
is platform-dependent: slash on Unix; colon, slash, and backslash on
|
| 255 |
+
DOS/Windows; and colon on Mac OS.
|
| 256 |
+
|
| 257 |
+
If 'anchor' is true (the default), then the pattern match is more
|
| 258 |
+
stringent: "*.py" will match "foo.py" but not "foo/bar.py". If
|
| 259 |
+
'anchor' is false, both of these will match.
|
| 260 |
+
|
| 261 |
+
If 'prefix' is supplied, then only filenames starting with 'prefix'
|
| 262 |
+
(itself a pattern) and ending with 'pattern', with anything in between
|
| 263 |
+
them, will match. 'anchor' is ignored in this case.
|
| 264 |
+
|
| 265 |
+
If 'is_regex' is true, 'anchor' and 'prefix' are ignored, and
|
| 266 |
+
'pattern' is assumed to be either a string containing a regex or a
|
| 267 |
+
regex object -- no translation is done, the regex is just compiled
|
| 268 |
+
and used as-is.
|
| 269 |
+
|
| 270 |
+
Selected strings will be added to self.files.
|
| 271 |
+
|
| 272 |
+
Return True if files are found.
|
| 273 |
+
"""
|
| 274 |
+
# XXX docstring lying about what the special chars are?
|
| 275 |
+
found = False
|
| 276 |
+
pattern_re = self._translate_pattern(pattern, anchor, prefix, is_regex)
|
| 277 |
+
|
| 278 |
+
# delayed loading of allfiles list
|
| 279 |
+
if self.allfiles is None:
|
| 280 |
+
self.findall()
|
| 281 |
+
|
| 282 |
+
for name in self.allfiles:
|
| 283 |
+
if pattern_re.search(name):
|
| 284 |
+
self.files.add(name)
|
| 285 |
+
found = True
|
| 286 |
+
return found
|
| 287 |
+
|
| 288 |
+
def _exclude_pattern(self, pattern, anchor=True, prefix=None,
|
| 289 |
+
is_regex=False):
|
| 290 |
+
"""Remove strings (presumably filenames) from 'files' that match
|
| 291 |
+
'pattern'.
|
| 292 |
+
|
| 293 |
+
Other parameters are the same as for 'include_pattern()', above.
|
| 294 |
+
The list 'self.files' is modified in place. Return True if files are
|
| 295 |
+
found.
|
| 296 |
+
|
| 297 |
+
This API is public to allow e.g. exclusion of SCM subdirs, e.g. when
|
| 298 |
+
packaging source distributions
|
| 299 |
+
"""
|
| 300 |
+
found = False
|
| 301 |
+
pattern_re = self._translate_pattern(pattern, anchor, prefix, is_regex)
|
| 302 |
+
for f in list(self.files):
|
| 303 |
+
if pattern_re.search(f):
|
| 304 |
+
self.files.remove(f)
|
| 305 |
+
found = True
|
| 306 |
+
return found
|
| 307 |
+
|
| 308 |
+
def _translate_pattern(self, pattern, anchor=True, prefix=None,
|
| 309 |
+
is_regex=False):
|
| 310 |
+
"""Translate a shell-like wildcard pattern to a compiled regular
|
| 311 |
+
expression.
|
| 312 |
+
|
| 313 |
+
Return the compiled regex. If 'is_regex' true,
|
| 314 |
+
then 'pattern' is directly compiled to a regex (if it's a string)
|
| 315 |
+
or just returned as-is (assumes it's a regex object).
|
| 316 |
+
"""
|
| 317 |
+
if is_regex:
|
| 318 |
+
if isinstance(pattern, str):
|
| 319 |
+
return re.compile(pattern)
|
| 320 |
+
else:
|
| 321 |
+
return pattern
|
| 322 |
+
|
| 323 |
+
if _PYTHON_VERSION > (3, 2):
|
| 324 |
+
# ditch start and end characters
|
| 325 |
+
start, _, end = self._glob_to_re('_').partition('_')
|
| 326 |
+
|
| 327 |
+
if pattern:
|
| 328 |
+
pattern_re = self._glob_to_re(pattern)
|
| 329 |
+
if _PYTHON_VERSION > (3, 2):
|
| 330 |
+
assert pattern_re.startswith(start) and pattern_re.endswith(end)
|
| 331 |
+
else:
|
| 332 |
+
pattern_re = ''
|
| 333 |
+
|
| 334 |
+
base = re.escape(os.path.join(self.base, ''))
|
| 335 |
+
if prefix is not None:
|
| 336 |
+
# ditch end of pattern character
|
| 337 |
+
if _PYTHON_VERSION <= (3, 2):
|
| 338 |
+
empty_pattern = self._glob_to_re('')
|
| 339 |
+
prefix_re = self._glob_to_re(prefix)[:-len(empty_pattern)]
|
| 340 |
+
else:
|
| 341 |
+
prefix_re = self._glob_to_re(prefix)
|
| 342 |
+
assert prefix_re.startswith(start) and prefix_re.endswith(end)
|
| 343 |
+
prefix_re = prefix_re[len(start): len(prefix_re) - len(end)]
|
| 344 |
+
sep = os.sep
|
| 345 |
+
if os.sep == '\\':
|
| 346 |
+
sep = r'\\'
|
| 347 |
+
if _PYTHON_VERSION <= (3, 2):
|
| 348 |
+
pattern_re = '^' + base + sep.join((prefix_re,
|
| 349 |
+
'.*' + pattern_re))
|
| 350 |
+
else:
|
| 351 |
+
pattern_re = pattern_re[len(start): len(pattern_re) - len(end)]
|
| 352 |
+
pattern_re = r'%s%s%s%s.*%s%s' % (start, base, prefix_re, sep,
|
| 353 |
+
pattern_re, end)
|
| 354 |
+
else: # no prefix -- respect anchor flag
|
| 355 |
+
if anchor:
|
| 356 |
+
if _PYTHON_VERSION <= (3, 2):
|
| 357 |
+
pattern_re = '^' + base + pattern_re
|
| 358 |
+
else:
|
| 359 |
+
pattern_re = r'%s%s%s' % (start, base, pattern_re[len(start):])
|
| 360 |
+
|
| 361 |
+
return re.compile(pattern_re)
|
| 362 |
+
|
| 363 |
+
def _glob_to_re(self, pattern):
|
| 364 |
+
"""Translate a shell-like glob pattern to a regular expression.
|
| 365 |
+
|
| 366 |
+
Return a string containing the regex. Differs from
|
| 367 |
+
'fnmatch.translate()' in that '*' does not match "special characters"
|
| 368 |
+
(which are platform-specific).
|
| 369 |
+
"""
|
| 370 |
+
pattern_re = fnmatch.translate(pattern)
|
| 371 |
+
|
| 372 |
+
# '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
|
| 373 |
+
# IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
|
| 374 |
+
# and by extension they shouldn't match such "special characters" under
|
| 375 |
+
# any OS. So change all non-escaped dots in the RE to match any
|
| 376 |
+
# character except the special characters (currently: just os.sep).
|
| 377 |
+
sep = os.sep
|
| 378 |
+
if os.sep == '\\':
|
| 379 |
+
# we're using a regex to manipulate a regex, so we need
|
| 380 |
+
# to escape the backslash twice
|
| 381 |
+
sep = r'\\\\'
|
| 382 |
+
escaped = r'\1[^%s]' % sep
|
| 383 |
+
pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re)
|
| 384 |
+
return pattern_re
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/markers.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2012-2023 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
"""
|
| 8 |
+
Parser for the environment markers micro-language defined in PEP 508.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
# Note: In PEP 345, the micro-language was Python compatible, so the ast
|
| 12 |
+
# module could be used to parse it. However, PEP 508 introduced operators such
|
| 13 |
+
# as ~= and === which aren't in Python, necessitating a different approach.
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import re
|
| 17 |
+
import sys
|
| 18 |
+
import platform
|
| 19 |
+
|
| 20 |
+
from .compat import string_types
|
| 21 |
+
from .util import in_venv, parse_marker
|
| 22 |
+
from .version import LegacyVersion as LV
|
| 23 |
+
|
| 24 |
+
__all__ = ['interpret']
|
| 25 |
+
|
| 26 |
+
_VERSION_PATTERN = re.compile(r'((\d+(\.\d+)*\w*)|\'(\d+(\.\d+)*\w*)\'|\"(\d+(\.\d+)*\w*)\")')
|
| 27 |
+
_VERSION_MARKERS = {'python_version', 'python_full_version'}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _is_version_marker(s):
|
| 31 |
+
return isinstance(s, string_types) and s in _VERSION_MARKERS
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _is_literal(o):
|
| 35 |
+
if not isinstance(o, string_types) or not o:
|
| 36 |
+
return False
|
| 37 |
+
return o[0] in '\'"'
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _get_versions(s):
|
| 41 |
+
return {LV(m.groups()[0]) for m in _VERSION_PATTERN.finditer(s)}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class Evaluator(object):
|
| 45 |
+
"""
|
| 46 |
+
This class is used to evaluate marker expressions.
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
operations = {
|
| 50 |
+
'==': lambda x, y: x == y,
|
| 51 |
+
'===': lambda x, y: x == y,
|
| 52 |
+
'~=': lambda x, y: x == y or x > y,
|
| 53 |
+
'!=': lambda x, y: x != y,
|
| 54 |
+
'<': lambda x, y: x < y,
|
| 55 |
+
'<=': lambda x, y: x == y or x < y,
|
| 56 |
+
'>': lambda x, y: x > y,
|
| 57 |
+
'>=': lambda x, y: x == y or x > y,
|
| 58 |
+
'and': lambda x, y: x and y,
|
| 59 |
+
'or': lambda x, y: x or y,
|
| 60 |
+
'in': lambda x, y: x in y,
|
| 61 |
+
'not in': lambda x, y: x not in y,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
def evaluate(self, expr, context):
|
| 65 |
+
"""
|
| 66 |
+
Evaluate a marker expression returned by the :func:`parse_requirement`
|
| 67 |
+
function in the specified context.
|
| 68 |
+
"""
|
| 69 |
+
if isinstance(expr, string_types):
|
| 70 |
+
if expr[0] in '\'"':
|
| 71 |
+
result = expr[1:-1]
|
| 72 |
+
else:
|
| 73 |
+
if expr not in context:
|
| 74 |
+
raise SyntaxError('unknown variable: %s' % expr)
|
| 75 |
+
result = context[expr]
|
| 76 |
+
else:
|
| 77 |
+
assert isinstance(expr, dict)
|
| 78 |
+
op = expr['op']
|
| 79 |
+
if op not in self.operations:
|
| 80 |
+
raise NotImplementedError('op not implemented: %s' % op)
|
| 81 |
+
elhs = expr['lhs']
|
| 82 |
+
erhs = expr['rhs']
|
| 83 |
+
if _is_literal(expr['lhs']) and _is_literal(expr['rhs']):
|
| 84 |
+
raise SyntaxError('invalid comparison: %s %s %s' % (elhs, op, erhs))
|
| 85 |
+
|
| 86 |
+
lhs = self.evaluate(elhs, context)
|
| 87 |
+
rhs = self.evaluate(erhs, context)
|
| 88 |
+
if ((_is_version_marker(elhs) or _is_version_marker(erhs)) and
|
| 89 |
+
op in ('<', '<=', '>', '>=', '===', '==', '!=', '~=')):
|
| 90 |
+
lhs = LV(lhs)
|
| 91 |
+
rhs = LV(rhs)
|
| 92 |
+
elif _is_version_marker(elhs) and op in ('in', 'not in'):
|
| 93 |
+
lhs = LV(lhs)
|
| 94 |
+
rhs = _get_versions(rhs)
|
| 95 |
+
result = self.operations[op](lhs, rhs)
|
| 96 |
+
return result
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
_DIGITS = re.compile(r'\d+\.\d+')
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def default_context():
|
| 103 |
+
|
| 104 |
+
def format_full_version(info):
|
| 105 |
+
version = '%s.%s.%s' % (info.major, info.minor, info.micro)
|
| 106 |
+
kind = info.releaselevel
|
| 107 |
+
if kind != 'final':
|
| 108 |
+
version += kind[0] + str(info.serial)
|
| 109 |
+
return version
|
| 110 |
+
|
| 111 |
+
if hasattr(sys, 'implementation'):
|
| 112 |
+
implementation_version = format_full_version(sys.implementation.version)
|
| 113 |
+
implementation_name = sys.implementation.name
|
| 114 |
+
else:
|
| 115 |
+
implementation_version = '0'
|
| 116 |
+
implementation_name = ''
|
| 117 |
+
|
| 118 |
+
ppv = platform.python_version()
|
| 119 |
+
m = _DIGITS.match(ppv)
|
| 120 |
+
pv = m.group(0)
|
| 121 |
+
result = {
|
| 122 |
+
'implementation_name': implementation_name,
|
| 123 |
+
'implementation_version': implementation_version,
|
| 124 |
+
'os_name': os.name,
|
| 125 |
+
'platform_machine': platform.machine(),
|
| 126 |
+
'platform_python_implementation': platform.python_implementation(),
|
| 127 |
+
'platform_release': platform.release(),
|
| 128 |
+
'platform_system': platform.system(),
|
| 129 |
+
'platform_version': platform.version(),
|
| 130 |
+
'platform_in_venv': str(in_venv()),
|
| 131 |
+
'python_full_version': ppv,
|
| 132 |
+
'python_version': pv,
|
| 133 |
+
'sys_platform': sys.platform,
|
| 134 |
+
}
|
| 135 |
+
return result
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
DEFAULT_CONTEXT = default_context()
|
| 139 |
+
del default_context
|
| 140 |
+
|
| 141 |
+
evaluator = Evaluator()
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def interpret(marker, execution_context=None):
|
| 145 |
+
"""
|
| 146 |
+
Interpret a marker and return a result depending on environment.
|
| 147 |
+
|
| 148 |
+
:param marker: The marker to interpret.
|
| 149 |
+
:type marker: str
|
| 150 |
+
:param execution_context: The context used for name lookup.
|
| 151 |
+
:type execution_context: mapping
|
| 152 |
+
"""
|
| 153 |
+
try:
|
| 154 |
+
expr, rest = parse_marker(marker)
|
| 155 |
+
except Exception as e:
|
| 156 |
+
raise SyntaxError('Unable to interpret marker syntax: %s: %s' % (marker, e))
|
| 157 |
+
if rest and rest[0] != '#':
|
| 158 |
+
raise SyntaxError('unexpected trailing data in marker: %s: %s' % (marker, rest))
|
| 159 |
+
context = dict(DEFAULT_CONTEXT)
|
| 160 |
+
if execution_context:
|
| 161 |
+
context.update(execution_context)
|
| 162 |
+
return evaluator.evaluate(expr, context)
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/metadata.py
ADDED
|
@@ -0,0 +1,1031 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2012 The Python Software Foundation.
|
| 4 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 5 |
+
#
|
| 6 |
+
"""Implementation of the Metadata for Python packages PEPs.
|
| 7 |
+
|
| 8 |
+
Supports all metadata formats (1.0, 1.1, 1.2, 1.3/2.1 and 2.2).
|
| 9 |
+
"""
|
| 10 |
+
from __future__ import unicode_literals
|
| 11 |
+
|
| 12 |
+
import codecs
|
| 13 |
+
from email import message_from_file
|
| 14 |
+
import json
|
| 15 |
+
import logging
|
| 16 |
+
import re
|
| 17 |
+
|
| 18 |
+
from . import DistlibException, __version__
|
| 19 |
+
from .compat import StringIO, string_types, text_type
|
| 20 |
+
from .markers import interpret
|
| 21 |
+
from .util import extract_by_key, get_extras
|
| 22 |
+
from .version import get_scheme, PEP440_VERSION_RE
|
| 23 |
+
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class MetadataMissingError(DistlibException):
|
| 28 |
+
"""A required metadata is missing"""
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class MetadataConflictError(DistlibException):
|
| 32 |
+
"""Attempt to read or write metadata fields that are conflictual."""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class MetadataUnrecognizedVersionError(DistlibException):
|
| 36 |
+
"""Unknown metadata version number."""
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class MetadataInvalidError(DistlibException):
|
| 40 |
+
"""A metadata value is invalid"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# public API of this module
|
| 44 |
+
__all__ = ['Metadata', 'PKG_INFO_ENCODING', 'PKG_INFO_PREFERRED_VERSION']
|
| 45 |
+
|
| 46 |
+
# Encoding used for the PKG-INFO files
|
| 47 |
+
PKG_INFO_ENCODING = 'utf-8'
|
| 48 |
+
|
| 49 |
+
# preferred version. Hopefully will be changed
|
| 50 |
+
# to 1.2 once PEP 345 is supported everywhere
|
| 51 |
+
PKG_INFO_PREFERRED_VERSION = '1.1'
|
| 52 |
+
|
| 53 |
+
_LINE_PREFIX_1_2 = re.compile('\n \\|')
|
| 54 |
+
_LINE_PREFIX_PRE_1_2 = re.compile('\n ')
|
| 55 |
+
_241_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform', 'Summary', 'Description', 'Keywords', 'Home-page',
|
| 56 |
+
'Author', 'Author-email', 'License')
|
| 57 |
+
|
| 58 |
+
_314_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform', 'Supported-Platform', 'Summary', 'Description',
|
| 59 |
+
'Keywords', 'Home-page', 'Author', 'Author-email', 'License', 'Classifier', 'Download-URL', 'Obsoletes',
|
| 60 |
+
'Provides', 'Requires')
|
| 61 |
+
|
| 62 |
+
_314_MARKERS = ('Obsoletes', 'Provides', 'Requires', 'Classifier', 'Download-URL')
|
| 63 |
+
|
| 64 |
+
_345_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform', 'Supported-Platform', 'Summary', 'Description',
|
| 65 |
+
'Keywords', 'Home-page', 'Author', 'Author-email', 'Maintainer', 'Maintainer-email', 'License',
|
| 66 |
+
'Classifier', 'Download-URL', 'Obsoletes-Dist', 'Project-URL', 'Provides-Dist', 'Requires-Dist',
|
| 67 |
+
'Requires-Python', 'Requires-External')
|
| 68 |
+
|
| 69 |
+
_345_MARKERS = ('Provides-Dist', 'Requires-Dist', 'Requires-Python', 'Obsoletes-Dist', 'Requires-External',
|
| 70 |
+
'Maintainer', 'Maintainer-email', 'Project-URL')
|
| 71 |
+
|
| 72 |
+
_426_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform', 'Supported-Platform', 'Summary', 'Description',
|
| 73 |
+
'Keywords', 'Home-page', 'Author', 'Author-email', 'Maintainer', 'Maintainer-email', 'License',
|
| 74 |
+
'Classifier', 'Download-URL', 'Obsoletes-Dist', 'Project-URL', 'Provides-Dist', 'Requires-Dist',
|
| 75 |
+
'Requires-Python', 'Requires-External', 'Private-Version', 'Obsoleted-By', 'Setup-Requires-Dist',
|
| 76 |
+
'Extension', 'Provides-Extra')
|
| 77 |
+
|
| 78 |
+
_426_MARKERS = ('Private-Version', 'Provides-Extra', 'Obsoleted-By', 'Setup-Requires-Dist', 'Extension')
|
| 79 |
+
|
| 80 |
+
# See issue #106: Sometimes 'Requires' and 'Provides' occur wrongly in
|
| 81 |
+
# the metadata. Include them in the tuple literal below to allow them
|
| 82 |
+
# (for now).
|
| 83 |
+
# Ditto for Obsoletes - see issue #140.
|
| 84 |
+
_566_FIELDS = _426_FIELDS + ('Description-Content-Type', 'Requires', 'Provides', 'Obsoletes')
|
| 85 |
+
|
| 86 |
+
_566_MARKERS = ('Description-Content-Type', )
|
| 87 |
+
|
| 88 |
+
_643_MARKERS = ('Dynamic', 'License-File')
|
| 89 |
+
|
| 90 |
+
_643_FIELDS = _566_FIELDS + _643_MARKERS
|
| 91 |
+
|
| 92 |
+
_ALL_FIELDS = set()
|
| 93 |
+
_ALL_FIELDS.update(_241_FIELDS)
|
| 94 |
+
_ALL_FIELDS.update(_314_FIELDS)
|
| 95 |
+
_ALL_FIELDS.update(_345_FIELDS)
|
| 96 |
+
_ALL_FIELDS.update(_426_FIELDS)
|
| 97 |
+
_ALL_FIELDS.update(_566_FIELDS)
|
| 98 |
+
_ALL_FIELDS.update(_643_FIELDS)
|
| 99 |
+
|
| 100 |
+
EXTRA_RE = re.compile(r'''extra\s*==\s*("([^"]+)"|'([^']+)')''')
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _version2fieldlist(version):
|
| 104 |
+
if version == '1.0':
|
| 105 |
+
return _241_FIELDS
|
| 106 |
+
elif version == '1.1':
|
| 107 |
+
return _314_FIELDS
|
| 108 |
+
elif version == '1.2':
|
| 109 |
+
return _345_FIELDS
|
| 110 |
+
elif version in ('1.3', '2.1'):
|
| 111 |
+
# avoid adding field names if already there
|
| 112 |
+
return _345_FIELDS + tuple(f for f in _566_FIELDS if f not in _345_FIELDS)
|
| 113 |
+
elif version == '2.0':
|
| 114 |
+
raise ValueError('Metadata 2.0 is withdrawn and not supported')
|
| 115 |
+
# return _426_FIELDS
|
| 116 |
+
elif version == '2.2':
|
| 117 |
+
return _643_FIELDS
|
| 118 |
+
raise MetadataUnrecognizedVersionError(version)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def _best_version(fields):
|
| 122 |
+
"""Detect the best version depending on the fields used."""
|
| 123 |
+
|
| 124 |
+
def _has_marker(keys, markers):
|
| 125 |
+
return any(marker in keys for marker in markers)
|
| 126 |
+
|
| 127 |
+
keys = [key for key, value in fields.items() if value not in ([], 'UNKNOWN', None)]
|
| 128 |
+
possible_versions = ['1.0', '1.1', '1.2', '1.3', '2.1', '2.2'] # 2.0 removed
|
| 129 |
+
|
| 130 |
+
# first let's try to see if a field is not part of one of the version
|
| 131 |
+
for key in keys:
|
| 132 |
+
if key not in _241_FIELDS and '1.0' in possible_versions:
|
| 133 |
+
possible_versions.remove('1.0')
|
| 134 |
+
logger.debug('Removed 1.0 due to %s', key)
|
| 135 |
+
if key not in _314_FIELDS and '1.1' in possible_versions:
|
| 136 |
+
possible_versions.remove('1.1')
|
| 137 |
+
logger.debug('Removed 1.1 due to %s', key)
|
| 138 |
+
if key not in _345_FIELDS and '1.2' in possible_versions:
|
| 139 |
+
possible_versions.remove('1.2')
|
| 140 |
+
logger.debug('Removed 1.2 due to %s', key)
|
| 141 |
+
if key not in _566_FIELDS and '1.3' in possible_versions:
|
| 142 |
+
possible_versions.remove('1.3')
|
| 143 |
+
logger.debug('Removed 1.3 due to %s', key)
|
| 144 |
+
if key not in _566_FIELDS and '2.1' in possible_versions:
|
| 145 |
+
if key != 'Description': # In 2.1, description allowed after headers
|
| 146 |
+
possible_versions.remove('2.1')
|
| 147 |
+
logger.debug('Removed 2.1 due to %s', key)
|
| 148 |
+
if key not in _643_FIELDS and '2.2' in possible_versions:
|
| 149 |
+
possible_versions.remove('2.2')
|
| 150 |
+
logger.debug('Removed 2.2 due to %s', key)
|
| 151 |
+
# if key not in _426_FIELDS and '2.0' in possible_versions:
|
| 152 |
+
# possible_versions.remove('2.0')
|
| 153 |
+
# logger.debug('Removed 2.0 due to %s', key)
|
| 154 |
+
|
| 155 |
+
# possible_version contains qualified versions
|
| 156 |
+
if len(possible_versions) == 1:
|
| 157 |
+
return possible_versions[0] # found !
|
| 158 |
+
elif len(possible_versions) == 0:
|
| 159 |
+
logger.debug('Out of options - unknown metadata set: %s', fields)
|
| 160 |
+
raise MetadataConflictError('Unknown metadata set')
|
| 161 |
+
|
| 162 |
+
# let's see if one unique marker is found
|
| 163 |
+
is_1_1 = '1.1' in possible_versions and _has_marker(keys, _314_MARKERS)
|
| 164 |
+
is_1_2 = '1.2' in possible_versions and _has_marker(keys, _345_MARKERS)
|
| 165 |
+
is_2_1 = '2.1' in possible_versions and _has_marker(keys, _566_MARKERS)
|
| 166 |
+
# is_2_0 = '2.0' in possible_versions and _has_marker(keys, _426_MARKERS)
|
| 167 |
+
is_2_2 = '2.2' in possible_versions and _has_marker(keys, _643_MARKERS)
|
| 168 |
+
if int(is_1_1) + int(is_1_2) + int(is_2_1) + int(is_2_2) > 1:
|
| 169 |
+
raise MetadataConflictError('You used incompatible 1.1/1.2/2.1/2.2 fields')
|
| 170 |
+
|
| 171 |
+
# we have the choice, 1.0, or 1.2, 2.1 or 2.2
|
| 172 |
+
# - 1.0 has a broken Summary field but works with all tools
|
| 173 |
+
# - 1.1 is to avoid
|
| 174 |
+
# - 1.2 fixes Summary but has little adoption
|
| 175 |
+
# - 2.1 adds more features
|
| 176 |
+
# - 2.2 is the latest
|
| 177 |
+
if not is_1_1 and not is_1_2 and not is_2_1 and not is_2_2:
|
| 178 |
+
# we couldn't find any specific marker
|
| 179 |
+
if PKG_INFO_PREFERRED_VERSION in possible_versions:
|
| 180 |
+
return PKG_INFO_PREFERRED_VERSION
|
| 181 |
+
if is_1_1:
|
| 182 |
+
return '1.1'
|
| 183 |
+
if is_1_2:
|
| 184 |
+
return '1.2'
|
| 185 |
+
if is_2_1:
|
| 186 |
+
return '2.1'
|
| 187 |
+
# if is_2_2:
|
| 188 |
+
# return '2.2'
|
| 189 |
+
|
| 190 |
+
return '2.2'
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
# This follows the rules about transforming keys as described in
|
| 194 |
+
# https://www.python.org/dev/peps/pep-0566/#id17
|
| 195 |
+
_ATTR2FIELD = {name.lower().replace("-", "_"): name for name in _ALL_FIELDS}
|
| 196 |
+
_FIELD2ATTR = {field: attr for attr, field in _ATTR2FIELD.items()}
|
| 197 |
+
|
| 198 |
+
_PREDICATE_FIELDS = ('Requires-Dist', 'Obsoletes-Dist', 'Provides-Dist')
|
| 199 |
+
_VERSIONS_FIELDS = ('Requires-Python', )
|
| 200 |
+
_VERSION_FIELDS = ('Version', )
|
| 201 |
+
_LISTFIELDS = ('Platform', 'Classifier', 'Obsoletes', 'Requires', 'Provides', 'Obsoletes-Dist', 'Provides-Dist',
|
| 202 |
+
'Requires-Dist', 'Requires-External', 'Project-URL', 'Supported-Platform', 'Setup-Requires-Dist',
|
| 203 |
+
'Provides-Extra', 'Extension', 'License-File')
|
| 204 |
+
_LISTTUPLEFIELDS = ('Project-URL', )
|
| 205 |
+
|
| 206 |
+
_ELEMENTSFIELD = ('Keywords', )
|
| 207 |
+
|
| 208 |
+
_UNICODEFIELDS = ('Author', 'Maintainer', 'Summary', 'Description')
|
| 209 |
+
|
| 210 |
+
_MISSING = object()
|
| 211 |
+
|
| 212 |
+
_FILESAFE = re.compile('[^A-Za-z0-9.]+')
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def _get_name_and_version(name, version, for_filename=False):
|
| 216 |
+
"""Return the distribution name with version.
|
| 217 |
+
|
| 218 |
+
If for_filename is true, return a filename-escaped form."""
|
| 219 |
+
if for_filename:
|
| 220 |
+
# For both name and version any runs of non-alphanumeric or '.'
|
| 221 |
+
# characters are replaced with a single '-'. Additionally any
|
| 222 |
+
# spaces in the version string become '.'
|
| 223 |
+
name = _FILESAFE.sub('-', name)
|
| 224 |
+
version = _FILESAFE.sub('-', version.replace(' ', '.'))
|
| 225 |
+
return '%s-%s' % (name, version)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
class LegacyMetadata(object):
|
| 229 |
+
"""The legacy metadata of a release.
|
| 230 |
+
|
| 231 |
+
Supports versions 1.0, 1.1, 1.2, 2.0 and 1.3/2.1 (auto-detected). You can
|
| 232 |
+
instantiate the class with one of these arguments (or none):
|
| 233 |
+
- *path*, the path to a metadata file
|
| 234 |
+
- *fileobj* give a file-like object with metadata as content
|
| 235 |
+
- *mapping* is a dict-like object
|
| 236 |
+
- *scheme* is a version scheme name
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
# TODO document the mapping API and UNKNOWN default key
|
| 240 |
+
|
| 241 |
+
def __init__(self, path=None, fileobj=None, mapping=None, scheme='default'):
|
| 242 |
+
if [path, fileobj, mapping].count(None) < 2:
|
| 243 |
+
raise TypeError('path, fileobj and mapping are exclusive')
|
| 244 |
+
self._fields = {}
|
| 245 |
+
self.requires_files = []
|
| 246 |
+
self._dependencies = None
|
| 247 |
+
self.scheme = scheme
|
| 248 |
+
if path is not None:
|
| 249 |
+
self.read(path)
|
| 250 |
+
elif fileobj is not None:
|
| 251 |
+
self.read_file(fileobj)
|
| 252 |
+
elif mapping is not None:
|
| 253 |
+
self.update(mapping)
|
| 254 |
+
self.set_metadata_version()
|
| 255 |
+
|
| 256 |
+
def set_metadata_version(self):
|
| 257 |
+
self._fields['Metadata-Version'] = _best_version(self._fields)
|
| 258 |
+
|
| 259 |
+
def _write_field(self, fileobj, name, value):
|
| 260 |
+
fileobj.write('%s: %s\n' % (name, value))
|
| 261 |
+
|
| 262 |
+
def __getitem__(self, name):
|
| 263 |
+
return self.get(name)
|
| 264 |
+
|
| 265 |
+
def __setitem__(self, name, value):
|
| 266 |
+
return self.set(name, value)
|
| 267 |
+
|
| 268 |
+
def __delitem__(self, name):
|
| 269 |
+
field_name = self._convert_name(name)
|
| 270 |
+
try:
|
| 271 |
+
del self._fields[field_name]
|
| 272 |
+
except KeyError:
|
| 273 |
+
raise KeyError(name)
|
| 274 |
+
|
| 275 |
+
def __contains__(self, name):
|
| 276 |
+
return (name in self._fields or self._convert_name(name) in self._fields)
|
| 277 |
+
|
| 278 |
+
def _convert_name(self, name):
|
| 279 |
+
if name in _ALL_FIELDS:
|
| 280 |
+
return name
|
| 281 |
+
name = name.replace('-', '_').lower()
|
| 282 |
+
return _ATTR2FIELD.get(name, name)
|
| 283 |
+
|
| 284 |
+
def _default_value(self, name):
|
| 285 |
+
if name in _LISTFIELDS or name in _ELEMENTSFIELD:
|
| 286 |
+
return []
|
| 287 |
+
return 'UNKNOWN'
|
| 288 |
+
|
| 289 |
+
def _remove_line_prefix(self, value):
|
| 290 |
+
if self.metadata_version in ('1.0', '1.1'):
|
| 291 |
+
return _LINE_PREFIX_PRE_1_2.sub('\n', value)
|
| 292 |
+
else:
|
| 293 |
+
return _LINE_PREFIX_1_2.sub('\n', value)
|
| 294 |
+
|
| 295 |
+
def __getattr__(self, name):
|
| 296 |
+
if name in _ATTR2FIELD:
|
| 297 |
+
return self[name]
|
| 298 |
+
raise AttributeError(name)
|
| 299 |
+
|
| 300 |
+
#
|
| 301 |
+
# Public API
|
| 302 |
+
#
|
| 303 |
+
|
| 304 |
+
def get_fullname(self, filesafe=False):
|
| 305 |
+
"""
|
| 306 |
+
Return the distribution name with version.
|
| 307 |
+
|
| 308 |
+
If filesafe is true, return a filename-escaped form.
|
| 309 |
+
"""
|
| 310 |
+
return _get_name_and_version(self['Name'], self['Version'], filesafe)
|
| 311 |
+
|
| 312 |
+
def is_field(self, name):
|
| 313 |
+
"""return True if name is a valid metadata key"""
|
| 314 |
+
name = self._convert_name(name)
|
| 315 |
+
return name in _ALL_FIELDS
|
| 316 |
+
|
| 317 |
+
def is_multi_field(self, name):
|
| 318 |
+
name = self._convert_name(name)
|
| 319 |
+
return name in _LISTFIELDS
|
| 320 |
+
|
| 321 |
+
def read(self, filepath):
|
| 322 |
+
"""Read the metadata values from a file path."""
|
| 323 |
+
fp = codecs.open(filepath, 'r', encoding='utf-8')
|
| 324 |
+
try:
|
| 325 |
+
self.read_file(fp)
|
| 326 |
+
finally:
|
| 327 |
+
fp.close()
|
| 328 |
+
|
| 329 |
+
def read_file(self, fileob):
|
| 330 |
+
"""Read the metadata values from a file object."""
|
| 331 |
+
msg = message_from_file(fileob)
|
| 332 |
+
self._fields['Metadata-Version'] = msg['metadata-version']
|
| 333 |
+
|
| 334 |
+
# When reading, get all the fields we can
|
| 335 |
+
for field in _ALL_FIELDS:
|
| 336 |
+
if field not in msg:
|
| 337 |
+
continue
|
| 338 |
+
if field in _LISTFIELDS:
|
| 339 |
+
# we can have multiple lines
|
| 340 |
+
values = msg.get_all(field)
|
| 341 |
+
if field in _LISTTUPLEFIELDS and values is not None:
|
| 342 |
+
values = [tuple(value.split(',')) for value in values]
|
| 343 |
+
self.set(field, values)
|
| 344 |
+
else:
|
| 345 |
+
# single line
|
| 346 |
+
value = msg[field]
|
| 347 |
+
if value is not None and value != 'UNKNOWN':
|
| 348 |
+
self.set(field, value)
|
| 349 |
+
|
| 350 |
+
# PEP 566 specifies that the body be used for the description, if
|
| 351 |
+
# available
|
| 352 |
+
body = msg.get_payload()
|
| 353 |
+
self["Description"] = body if body else self["Description"]
|
| 354 |
+
# logger.debug('Attempting to set metadata for %s', self)
|
| 355 |
+
# self.set_metadata_version()
|
| 356 |
+
|
| 357 |
+
def write(self, filepath, skip_unknown=False):
|
| 358 |
+
"""Write the metadata fields to filepath."""
|
| 359 |
+
fp = codecs.open(filepath, 'w', encoding='utf-8')
|
| 360 |
+
try:
|
| 361 |
+
self.write_file(fp, skip_unknown)
|
| 362 |
+
finally:
|
| 363 |
+
fp.close()
|
| 364 |
+
|
| 365 |
+
def write_file(self, fileobject, skip_unknown=False):
|
| 366 |
+
"""Write the PKG-INFO format data to a file object."""
|
| 367 |
+
self.set_metadata_version()
|
| 368 |
+
|
| 369 |
+
for field in _version2fieldlist(self['Metadata-Version']):
|
| 370 |
+
values = self.get(field)
|
| 371 |
+
if skip_unknown and values in ('UNKNOWN', [], ['UNKNOWN']):
|
| 372 |
+
continue
|
| 373 |
+
if field in _ELEMENTSFIELD:
|
| 374 |
+
self._write_field(fileobject, field, ','.join(values))
|
| 375 |
+
continue
|
| 376 |
+
if field not in _LISTFIELDS:
|
| 377 |
+
if field == 'Description':
|
| 378 |
+
if self.metadata_version in ('1.0', '1.1'):
|
| 379 |
+
values = values.replace('\n', '\n ')
|
| 380 |
+
else:
|
| 381 |
+
values = values.replace('\n', '\n |')
|
| 382 |
+
values = [values]
|
| 383 |
+
|
| 384 |
+
if field in _LISTTUPLEFIELDS:
|
| 385 |
+
values = [','.join(value) for value in values]
|
| 386 |
+
|
| 387 |
+
for value in values:
|
| 388 |
+
self._write_field(fileobject, field, value)
|
| 389 |
+
|
| 390 |
+
def update(self, other=None, **kwargs):
|
| 391 |
+
"""Set metadata values from the given iterable `other` and kwargs.
|
| 392 |
+
|
| 393 |
+
Behavior is like `dict.update`: If `other` has a ``keys`` method,
|
| 394 |
+
they are looped over and ``self[key]`` is assigned ``other[key]``.
|
| 395 |
+
Else, ``other`` is an iterable of ``(key, value)`` iterables.
|
| 396 |
+
|
| 397 |
+
Keys that don't match a metadata field or that have an empty value are
|
| 398 |
+
dropped.
|
| 399 |
+
"""
|
| 400 |
+
|
| 401 |
+
def _set(key, value):
|
| 402 |
+
if key in _ATTR2FIELD and value:
|
| 403 |
+
self.set(self._convert_name(key), value)
|
| 404 |
+
|
| 405 |
+
if not other:
|
| 406 |
+
# other is None or empty container
|
| 407 |
+
pass
|
| 408 |
+
elif hasattr(other, 'keys'):
|
| 409 |
+
for k in other.keys():
|
| 410 |
+
_set(k, other[k])
|
| 411 |
+
else:
|
| 412 |
+
for k, v in other:
|
| 413 |
+
_set(k, v)
|
| 414 |
+
|
| 415 |
+
if kwargs:
|
| 416 |
+
for k, v in kwargs.items():
|
| 417 |
+
_set(k, v)
|
| 418 |
+
|
| 419 |
+
def set(self, name, value):
|
| 420 |
+
"""Control then set a metadata field."""
|
| 421 |
+
name = self._convert_name(name)
|
| 422 |
+
|
| 423 |
+
if ((name in _ELEMENTSFIELD or name == 'Platform') and not isinstance(value, (list, tuple))):
|
| 424 |
+
if isinstance(value, string_types):
|
| 425 |
+
value = [v.strip() for v in value.split(',')]
|
| 426 |
+
else:
|
| 427 |
+
value = []
|
| 428 |
+
elif (name in _LISTFIELDS and not isinstance(value, (list, tuple))):
|
| 429 |
+
if isinstance(value, string_types):
|
| 430 |
+
value = [value]
|
| 431 |
+
else:
|
| 432 |
+
value = []
|
| 433 |
+
|
| 434 |
+
if logger.isEnabledFor(logging.WARNING):
|
| 435 |
+
project_name = self['Name']
|
| 436 |
+
|
| 437 |
+
scheme = get_scheme(self.scheme)
|
| 438 |
+
if name in _PREDICATE_FIELDS and value is not None:
|
| 439 |
+
for v in value:
|
| 440 |
+
# check that the values are valid
|
| 441 |
+
if not scheme.is_valid_matcher(v.split(';')[0]):
|
| 442 |
+
logger.warning("'%s': '%s' is not valid (field '%s')", project_name, v, name)
|
| 443 |
+
# FIXME this rejects UNKNOWN, is that right?
|
| 444 |
+
elif name in _VERSIONS_FIELDS and value is not None:
|
| 445 |
+
if not scheme.is_valid_constraint_list(value):
|
| 446 |
+
logger.warning("'%s': '%s' is not a valid version (field '%s')", project_name, value, name)
|
| 447 |
+
elif name in _VERSION_FIELDS and value is not None:
|
| 448 |
+
if not scheme.is_valid_version(value):
|
| 449 |
+
logger.warning("'%s': '%s' is not a valid version (field '%s')", project_name, value, name)
|
| 450 |
+
|
| 451 |
+
if name in _UNICODEFIELDS:
|
| 452 |
+
if name == 'Description':
|
| 453 |
+
value = self._remove_line_prefix(value)
|
| 454 |
+
|
| 455 |
+
self._fields[name] = value
|
| 456 |
+
|
| 457 |
+
def get(self, name, default=_MISSING):
|
| 458 |
+
"""Get a metadata field."""
|
| 459 |
+
name = self._convert_name(name)
|
| 460 |
+
if name not in self._fields:
|
| 461 |
+
if default is _MISSING:
|
| 462 |
+
default = self._default_value(name)
|
| 463 |
+
return default
|
| 464 |
+
if name in _UNICODEFIELDS:
|
| 465 |
+
value = self._fields[name]
|
| 466 |
+
return value
|
| 467 |
+
elif name in _LISTFIELDS:
|
| 468 |
+
value = self._fields[name]
|
| 469 |
+
if value is None:
|
| 470 |
+
return []
|
| 471 |
+
res = []
|
| 472 |
+
for val in value:
|
| 473 |
+
if name not in _LISTTUPLEFIELDS:
|
| 474 |
+
res.append(val)
|
| 475 |
+
else:
|
| 476 |
+
# That's for Project-URL
|
| 477 |
+
res.append((val[0], val[1]))
|
| 478 |
+
return res
|
| 479 |
+
|
| 480 |
+
elif name in _ELEMENTSFIELD:
|
| 481 |
+
value = self._fields[name]
|
| 482 |
+
if isinstance(value, string_types):
|
| 483 |
+
return value.split(',')
|
| 484 |
+
return self._fields[name]
|
| 485 |
+
|
| 486 |
+
def check(self, strict=False):
|
| 487 |
+
"""Check if the metadata is compliant. If strict is True then raise if
|
| 488 |
+
no Name or Version are provided"""
|
| 489 |
+
self.set_metadata_version()
|
| 490 |
+
|
| 491 |
+
# XXX should check the versions (if the file was loaded)
|
| 492 |
+
missing, warnings = [], []
|
| 493 |
+
|
| 494 |
+
for attr in ('Name', 'Version'): # required by PEP 345
|
| 495 |
+
if attr not in self:
|
| 496 |
+
missing.append(attr)
|
| 497 |
+
|
| 498 |
+
if strict and missing != []:
|
| 499 |
+
msg = 'missing required metadata: %s' % ', '.join(missing)
|
| 500 |
+
raise MetadataMissingError(msg)
|
| 501 |
+
|
| 502 |
+
for attr in ('Home-page', 'Author'):
|
| 503 |
+
if attr not in self:
|
| 504 |
+
missing.append(attr)
|
| 505 |
+
|
| 506 |
+
# checking metadata 1.2 (XXX needs to check 1.1, 1.0)
|
| 507 |
+
if self['Metadata-Version'] != '1.2':
|
| 508 |
+
return missing, warnings
|
| 509 |
+
|
| 510 |
+
scheme = get_scheme(self.scheme)
|
| 511 |
+
|
| 512 |
+
def are_valid_constraints(value):
|
| 513 |
+
for v in value:
|
| 514 |
+
if not scheme.is_valid_matcher(v.split(';')[0]):
|
| 515 |
+
return False
|
| 516 |
+
return True
|
| 517 |
+
|
| 518 |
+
for fields, controller in ((_PREDICATE_FIELDS, are_valid_constraints),
|
| 519 |
+
(_VERSIONS_FIELDS, scheme.is_valid_constraint_list), (_VERSION_FIELDS,
|
| 520 |
+
scheme.is_valid_version)):
|
| 521 |
+
for field in fields:
|
| 522 |
+
value = self.get(field, None)
|
| 523 |
+
if value is not None and not controller(value):
|
| 524 |
+
warnings.append("Wrong value for '%s': %s" % (field, value))
|
| 525 |
+
|
| 526 |
+
return missing, warnings
|
| 527 |
+
|
| 528 |
+
def todict(self, skip_missing=False):
|
| 529 |
+
"""Return fields as a dict.
|
| 530 |
+
|
| 531 |
+
Field names will be converted to use the underscore-lowercase style
|
| 532 |
+
instead of hyphen-mixed case (i.e. home_page instead of Home-page).
|
| 533 |
+
This is as per https://www.python.org/dev/peps/pep-0566/#id17.
|
| 534 |
+
"""
|
| 535 |
+
self.set_metadata_version()
|
| 536 |
+
|
| 537 |
+
fields = _version2fieldlist(self['Metadata-Version'])
|
| 538 |
+
|
| 539 |
+
data = {}
|
| 540 |
+
|
| 541 |
+
for field_name in fields:
|
| 542 |
+
if not skip_missing or field_name in self._fields:
|
| 543 |
+
key = _FIELD2ATTR[field_name]
|
| 544 |
+
if key != 'project_url':
|
| 545 |
+
data[key] = self[field_name]
|
| 546 |
+
else:
|
| 547 |
+
data[key] = [','.join(u) for u in self[field_name]]
|
| 548 |
+
|
| 549 |
+
return data
|
| 550 |
+
|
| 551 |
+
def add_requirements(self, requirements):
|
| 552 |
+
if self['Metadata-Version'] == '1.1':
|
| 553 |
+
# we can't have 1.1 metadata *and* Setuptools requires
|
| 554 |
+
for field in ('Obsoletes', 'Requires', 'Provides'):
|
| 555 |
+
if field in self:
|
| 556 |
+
del self[field]
|
| 557 |
+
self['Requires-Dist'] += requirements
|
| 558 |
+
|
| 559 |
+
# Mapping API
|
| 560 |
+
# TODO could add iter* variants
|
| 561 |
+
|
| 562 |
+
def keys(self):
|
| 563 |
+
return list(_version2fieldlist(self['Metadata-Version']))
|
| 564 |
+
|
| 565 |
+
def __iter__(self):
|
| 566 |
+
for key in self.keys():
|
| 567 |
+
yield key
|
| 568 |
+
|
| 569 |
+
def values(self):
|
| 570 |
+
return [self[key] for key in self.keys()]
|
| 571 |
+
|
| 572 |
+
def items(self):
|
| 573 |
+
return [(key, self[key]) for key in self.keys()]
|
| 574 |
+
|
| 575 |
+
def __repr__(self):
|
| 576 |
+
return '<%s %s %s>' % (self.__class__.__name__, self.name, self.version)
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
METADATA_FILENAME = 'pydist.json'
|
| 580 |
+
WHEEL_METADATA_FILENAME = 'metadata.json'
|
| 581 |
+
LEGACY_METADATA_FILENAME = 'METADATA'
|
| 582 |
+
|
| 583 |
+
|
| 584 |
+
class Metadata(object):
|
| 585 |
+
"""
|
| 586 |
+
The metadata of a release. This implementation uses 2.1
|
| 587 |
+
metadata where possible. If not possible, it wraps a LegacyMetadata
|
| 588 |
+
instance which handles the key-value metadata format.
|
| 589 |
+
"""
|
| 590 |
+
|
| 591 |
+
METADATA_VERSION_MATCHER = re.compile(r'^\d+(\.\d+)*$')
|
| 592 |
+
|
| 593 |
+
NAME_MATCHER = re.compile('^[0-9A-Z]([0-9A-Z_.-]*[0-9A-Z])?$', re.I)
|
| 594 |
+
|
| 595 |
+
FIELDNAME_MATCHER = re.compile('^[A-Z]([0-9A-Z-]*[0-9A-Z])?$', re.I)
|
| 596 |
+
|
| 597 |
+
VERSION_MATCHER = PEP440_VERSION_RE
|
| 598 |
+
|
| 599 |
+
SUMMARY_MATCHER = re.compile('.{1,2047}')
|
| 600 |
+
|
| 601 |
+
METADATA_VERSION = '2.0'
|
| 602 |
+
|
| 603 |
+
GENERATOR = 'distlib (%s)' % __version__
|
| 604 |
+
|
| 605 |
+
MANDATORY_KEYS = {
|
| 606 |
+
'name': (),
|
| 607 |
+
'version': (),
|
| 608 |
+
'summary': ('legacy', ),
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
INDEX_KEYS = ('name version license summary description author '
|
| 612 |
+
'author_email keywords platform home_page classifiers '
|
| 613 |
+
'download_url')
|
| 614 |
+
|
| 615 |
+
DEPENDENCY_KEYS = ('extras run_requires test_requires build_requires '
|
| 616 |
+
'dev_requires provides meta_requires obsoleted_by '
|
| 617 |
+
'supports_environments')
|
| 618 |
+
|
| 619 |
+
SYNTAX_VALIDATORS = {
|
| 620 |
+
'metadata_version': (METADATA_VERSION_MATCHER, ()),
|
| 621 |
+
'name': (NAME_MATCHER, ('legacy', )),
|
| 622 |
+
'version': (VERSION_MATCHER, ('legacy', )),
|
| 623 |
+
'summary': (SUMMARY_MATCHER, ('legacy', )),
|
| 624 |
+
'dynamic': (FIELDNAME_MATCHER, ('legacy', )),
|
| 625 |
+
}
|
| 626 |
+
|
| 627 |
+
__slots__ = ('_legacy', '_data', 'scheme')
|
| 628 |
+
|
| 629 |
+
def __init__(self, path=None, fileobj=None, mapping=None, scheme='default'):
|
| 630 |
+
if [path, fileobj, mapping].count(None) < 2:
|
| 631 |
+
raise TypeError('path, fileobj and mapping are exclusive')
|
| 632 |
+
self._legacy = None
|
| 633 |
+
self._data = None
|
| 634 |
+
self.scheme = scheme
|
| 635 |
+
# import pdb; pdb.set_trace()
|
| 636 |
+
if mapping is not None:
|
| 637 |
+
try:
|
| 638 |
+
self._validate_mapping(mapping, scheme)
|
| 639 |
+
self._data = mapping
|
| 640 |
+
except MetadataUnrecognizedVersionError:
|
| 641 |
+
self._legacy = LegacyMetadata(mapping=mapping, scheme=scheme)
|
| 642 |
+
self.validate()
|
| 643 |
+
else:
|
| 644 |
+
data = None
|
| 645 |
+
if path:
|
| 646 |
+
with open(path, 'rb') as f:
|
| 647 |
+
data = f.read()
|
| 648 |
+
elif fileobj:
|
| 649 |
+
data = fileobj.read()
|
| 650 |
+
if data is None:
|
| 651 |
+
# Initialised with no args - to be added
|
| 652 |
+
self._data = {
|
| 653 |
+
'metadata_version': self.METADATA_VERSION,
|
| 654 |
+
'generator': self.GENERATOR,
|
| 655 |
+
}
|
| 656 |
+
else:
|
| 657 |
+
if not isinstance(data, text_type):
|
| 658 |
+
data = data.decode('utf-8')
|
| 659 |
+
try:
|
| 660 |
+
self._data = json.loads(data)
|
| 661 |
+
self._validate_mapping(self._data, scheme)
|
| 662 |
+
except ValueError:
|
| 663 |
+
# Note: MetadataUnrecognizedVersionError does not
|
| 664 |
+
# inherit from ValueError (it's a DistlibException,
|
| 665 |
+
# which should not inherit from ValueError).
|
| 666 |
+
# The ValueError comes from the json.load - if that
|
| 667 |
+
# succeeds and we get a validation error, we want
|
| 668 |
+
# that to propagate
|
| 669 |
+
self._legacy = LegacyMetadata(fileobj=StringIO(data), scheme=scheme)
|
| 670 |
+
self.validate()
|
| 671 |
+
|
| 672 |
+
common_keys = set(('name', 'version', 'license', 'keywords', 'summary'))
|
| 673 |
+
|
| 674 |
+
none_list = (None, list)
|
| 675 |
+
none_dict = (None, dict)
|
| 676 |
+
|
| 677 |
+
mapped_keys = {
|
| 678 |
+
'run_requires': ('Requires-Dist', list),
|
| 679 |
+
'build_requires': ('Setup-Requires-Dist', list),
|
| 680 |
+
'dev_requires': none_list,
|
| 681 |
+
'test_requires': none_list,
|
| 682 |
+
'meta_requires': none_list,
|
| 683 |
+
'extras': ('Provides-Extra', list),
|
| 684 |
+
'modules': none_list,
|
| 685 |
+
'namespaces': none_list,
|
| 686 |
+
'exports': none_dict,
|
| 687 |
+
'commands': none_dict,
|
| 688 |
+
'classifiers': ('Classifier', list),
|
| 689 |
+
'source_url': ('Download-URL', None),
|
| 690 |
+
'metadata_version': ('Metadata-Version', None),
|
| 691 |
+
}
|
| 692 |
+
|
| 693 |
+
del none_list, none_dict
|
| 694 |
+
|
| 695 |
+
def __getattribute__(self, key):
|
| 696 |
+
common = object.__getattribute__(self, 'common_keys')
|
| 697 |
+
mapped = object.__getattribute__(self, 'mapped_keys')
|
| 698 |
+
if key in mapped:
|
| 699 |
+
lk, maker = mapped[key]
|
| 700 |
+
if self._legacy:
|
| 701 |
+
if lk is None:
|
| 702 |
+
result = None if maker is None else maker()
|
| 703 |
+
else:
|
| 704 |
+
result = self._legacy.get(lk)
|
| 705 |
+
else:
|
| 706 |
+
value = None if maker is None else maker()
|
| 707 |
+
if key not in ('commands', 'exports', 'modules', 'namespaces', 'classifiers'):
|
| 708 |
+
result = self._data.get(key, value)
|
| 709 |
+
else:
|
| 710 |
+
# special cases for PEP 459
|
| 711 |
+
sentinel = object()
|
| 712 |
+
result = sentinel
|
| 713 |
+
d = self._data.get('extensions')
|
| 714 |
+
if d:
|
| 715 |
+
if key == 'commands':
|
| 716 |
+
result = d.get('python.commands', value)
|
| 717 |
+
elif key == 'classifiers':
|
| 718 |
+
d = d.get('python.details')
|
| 719 |
+
if d:
|
| 720 |
+
result = d.get(key, value)
|
| 721 |
+
else:
|
| 722 |
+
d = d.get('python.exports')
|
| 723 |
+
if not d:
|
| 724 |
+
d = self._data.get('python.exports')
|
| 725 |
+
if d:
|
| 726 |
+
result = d.get(key, value)
|
| 727 |
+
if result is sentinel:
|
| 728 |
+
result = value
|
| 729 |
+
elif key not in common:
|
| 730 |
+
result = object.__getattribute__(self, key)
|
| 731 |
+
elif self._legacy:
|
| 732 |
+
result = self._legacy.get(key)
|
| 733 |
+
else:
|
| 734 |
+
result = self._data.get(key)
|
| 735 |
+
return result
|
| 736 |
+
|
| 737 |
+
def _validate_value(self, key, value, scheme=None):
|
| 738 |
+
if key in self.SYNTAX_VALIDATORS:
|
| 739 |
+
pattern, exclusions = self.SYNTAX_VALIDATORS[key]
|
| 740 |
+
if (scheme or self.scheme) not in exclusions:
|
| 741 |
+
m = pattern.match(value)
|
| 742 |
+
if not m:
|
| 743 |
+
raise MetadataInvalidError("'%s' is an invalid value for "
|
| 744 |
+
"the '%s' property" % (value, key))
|
| 745 |
+
|
| 746 |
+
def __setattr__(self, key, value):
|
| 747 |
+
self._validate_value(key, value)
|
| 748 |
+
common = object.__getattribute__(self, 'common_keys')
|
| 749 |
+
mapped = object.__getattribute__(self, 'mapped_keys')
|
| 750 |
+
if key in mapped:
|
| 751 |
+
lk, _ = mapped[key]
|
| 752 |
+
if self._legacy:
|
| 753 |
+
if lk is None:
|
| 754 |
+
raise NotImplementedError
|
| 755 |
+
self._legacy[lk] = value
|
| 756 |
+
elif key not in ('commands', 'exports', 'modules', 'namespaces', 'classifiers'):
|
| 757 |
+
self._data[key] = value
|
| 758 |
+
else:
|
| 759 |
+
# special cases for PEP 459
|
| 760 |
+
d = self._data.setdefault('extensions', {})
|
| 761 |
+
if key == 'commands':
|
| 762 |
+
d['python.commands'] = value
|
| 763 |
+
elif key == 'classifiers':
|
| 764 |
+
d = d.setdefault('python.details', {})
|
| 765 |
+
d[key] = value
|
| 766 |
+
else:
|
| 767 |
+
d = d.setdefault('python.exports', {})
|
| 768 |
+
d[key] = value
|
| 769 |
+
elif key not in common:
|
| 770 |
+
object.__setattr__(self, key, value)
|
| 771 |
+
else:
|
| 772 |
+
if key == 'keywords':
|
| 773 |
+
if isinstance(value, string_types):
|
| 774 |
+
value = value.strip()
|
| 775 |
+
if value:
|
| 776 |
+
value = value.split()
|
| 777 |
+
else:
|
| 778 |
+
value = []
|
| 779 |
+
if self._legacy:
|
| 780 |
+
self._legacy[key] = value
|
| 781 |
+
else:
|
| 782 |
+
self._data[key] = value
|
| 783 |
+
|
| 784 |
+
@property
|
| 785 |
+
def name_and_version(self):
|
| 786 |
+
return _get_name_and_version(self.name, self.version, True)
|
| 787 |
+
|
| 788 |
+
@property
|
| 789 |
+
def provides(self):
|
| 790 |
+
if self._legacy:
|
| 791 |
+
result = self._legacy['Provides-Dist']
|
| 792 |
+
else:
|
| 793 |
+
result = self._data.setdefault('provides', [])
|
| 794 |
+
s = '%s (%s)' % (self.name, self.version)
|
| 795 |
+
if s not in result:
|
| 796 |
+
result.append(s)
|
| 797 |
+
return result
|
| 798 |
+
|
| 799 |
+
@provides.setter
|
| 800 |
+
def provides(self, value):
|
| 801 |
+
if self._legacy:
|
| 802 |
+
self._legacy['Provides-Dist'] = value
|
| 803 |
+
else:
|
| 804 |
+
self._data['provides'] = value
|
| 805 |
+
|
| 806 |
+
def get_requirements(self, reqts, extras=None, env=None):
|
| 807 |
+
"""
|
| 808 |
+
Base method to get dependencies, given a set of extras
|
| 809 |
+
to satisfy and an optional environment context.
|
| 810 |
+
:param reqts: A list of sometimes-wanted dependencies,
|
| 811 |
+
perhaps dependent on extras and environment.
|
| 812 |
+
:param extras: A list of optional components being requested.
|
| 813 |
+
:param env: An optional environment for marker evaluation.
|
| 814 |
+
"""
|
| 815 |
+
if self._legacy:
|
| 816 |
+
result = reqts
|
| 817 |
+
else:
|
| 818 |
+
result = []
|
| 819 |
+
extras = get_extras(extras or [], self.extras)
|
| 820 |
+
for d in reqts:
|
| 821 |
+
if 'extra' not in d and 'environment' not in d:
|
| 822 |
+
# unconditional
|
| 823 |
+
include = True
|
| 824 |
+
else:
|
| 825 |
+
if 'extra' not in d:
|
| 826 |
+
# Not extra-dependent - only environment-dependent
|
| 827 |
+
include = True
|
| 828 |
+
else:
|
| 829 |
+
include = d.get('extra') in extras
|
| 830 |
+
if include:
|
| 831 |
+
# Not excluded because of extras, check environment
|
| 832 |
+
marker = d.get('environment')
|
| 833 |
+
if marker:
|
| 834 |
+
include = interpret(marker, env)
|
| 835 |
+
if include:
|
| 836 |
+
result.extend(d['requires'])
|
| 837 |
+
for key in ('build', 'dev', 'test'):
|
| 838 |
+
e = ':%s:' % key
|
| 839 |
+
if e in extras:
|
| 840 |
+
extras.remove(e)
|
| 841 |
+
# A recursive call, but it should terminate since 'test'
|
| 842 |
+
# has been removed from the extras
|
| 843 |
+
reqts = self._data.get('%s_requires' % key, [])
|
| 844 |
+
result.extend(self.get_requirements(reqts, extras=extras, env=env))
|
| 845 |
+
return result
|
| 846 |
+
|
| 847 |
+
@property
|
| 848 |
+
def dictionary(self):
|
| 849 |
+
if self._legacy:
|
| 850 |
+
return self._from_legacy()
|
| 851 |
+
return self._data
|
| 852 |
+
|
| 853 |
+
@property
|
| 854 |
+
def dependencies(self):
|
| 855 |
+
if self._legacy:
|
| 856 |
+
raise NotImplementedError
|
| 857 |
+
else:
|
| 858 |
+
return extract_by_key(self._data, self.DEPENDENCY_KEYS)
|
| 859 |
+
|
| 860 |
+
@dependencies.setter
|
| 861 |
+
def dependencies(self, value):
|
| 862 |
+
if self._legacy:
|
| 863 |
+
raise NotImplementedError
|
| 864 |
+
else:
|
| 865 |
+
self._data.update(value)
|
| 866 |
+
|
| 867 |
+
def _validate_mapping(self, mapping, scheme):
|
| 868 |
+
if mapping.get('metadata_version') != self.METADATA_VERSION:
|
| 869 |
+
raise MetadataUnrecognizedVersionError()
|
| 870 |
+
missing = []
|
| 871 |
+
for key, exclusions in self.MANDATORY_KEYS.items():
|
| 872 |
+
if key not in mapping:
|
| 873 |
+
if scheme not in exclusions:
|
| 874 |
+
missing.append(key)
|
| 875 |
+
if missing:
|
| 876 |
+
msg = 'Missing metadata items: %s' % ', '.join(missing)
|
| 877 |
+
raise MetadataMissingError(msg)
|
| 878 |
+
for k, v in mapping.items():
|
| 879 |
+
self._validate_value(k, v, scheme)
|
| 880 |
+
|
| 881 |
+
def validate(self):
|
| 882 |
+
if self._legacy:
|
| 883 |
+
missing, warnings = self._legacy.check(True)
|
| 884 |
+
if missing or warnings:
|
| 885 |
+
logger.warning('Metadata: missing: %s, warnings: %s', missing, warnings)
|
| 886 |
+
else:
|
| 887 |
+
self._validate_mapping(self._data, self.scheme)
|
| 888 |
+
|
| 889 |
+
def todict(self):
|
| 890 |
+
if self._legacy:
|
| 891 |
+
return self._legacy.todict(True)
|
| 892 |
+
else:
|
| 893 |
+
result = extract_by_key(self._data, self.INDEX_KEYS)
|
| 894 |
+
return result
|
| 895 |
+
|
| 896 |
+
def _from_legacy(self):
|
| 897 |
+
assert self._legacy and not self._data
|
| 898 |
+
result = {
|
| 899 |
+
'metadata_version': self.METADATA_VERSION,
|
| 900 |
+
'generator': self.GENERATOR,
|
| 901 |
+
}
|
| 902 |
+
lmd = self._legacy.todict(True) # skip missing ones
|
| 903 |
+
for k in ('name', 'version', 'license', 'summary', 'description', 'classifier'):
|
| 904 |
+
if k in lmd:
|
| 905 |
+
if k == 'classifier':
|
| 906 |
+
nk = 'classifiers'
|
| 907 |
+
else:
|
| 908 |
+
nk = k
|
| 909 |
+
result[nk] = lmd[k]
|
| 910 |
+
kw = lmd.get('Keywords', [])
|
| 911 |
+
if kw == ['']:
|
| 912 |
+
kw = []
|
| 913 |
+
result['keywords'] = kw
|
| 914 |
+
keys = (('requires_dist', 'run_requires'), ('setup_requires_dist', 'build_requires'))
|
| 915 |
+
for ok, nk in keys:
|
| 916 |
+
if ok in lmd and lmd[ok]:
|
| 917 |
+
result[nk] = [{'requires': lmd[ok]}]
|
| 918 |
+
result['provides'] = self.provides
|
| 919 |
+
# author = {}
|
| 920 |
+
# maintainer = {}
|
| 921 |
+
return result
|
| 922 |
+
|
| 923 |
+
LEGACY_MAPPING = {
|
| 924 |
+
'name': 'Name',
|
| 925 |
+
'version': 'Version',
|
| 926 |
+
('extensions', 'python.details', 'license'): 'License',
|
| 927 |
+
'summary': 'Summary',
|
| 928 |
+
'description': 'Description',
|
| 929 |
+
('extensions', 'python.project', 'project_urls', 'Home'): 'Home-page',
|
| 930 |
+
('extensions', 'python.project', 'contacts', 0, 'name'): 'Author',
|
| 931 |
+
('extensions', 'python.project', 'contacts', 0, 'email'): 'Author-email',
|
| 932 |
+
'source_url': 'Download-URL',
|
| 933 |
+
('extensions', 'python.details', 'classifiers'): 'Classifier',
|
| 934 |
+
}
|
| 935 |
+
|
| 936 |
+
def _to_legacy(self):
|
| 937 |
+
|
| 938 |
+
def process_entries(entries):
|
| 939 |
+
reqts = set()
|
| 940 |
+
for e in entries:
|
| 941 |
+
extra = e.get('extra')
|
| 942 |
+
env = e.get('environment')
|
| 943 |
+
rlist = e['requires']
|
| 944 |
+
for r in rlist:
|
| 945 |
+
if not env and not extra:
|
| 946 |
+
reqts.add(r)
|
| 947 |
+
else:
|
| 948 |
+
marker = ''
|
| 949 |
+
if extra:
|
| 950 |
+
marker = 'extra == "%s"' % extra
|
| 951 |
+
if env:
|
| 952 |
+
if marker:
|
| 953 |
+
marker = '(%s) and %s' % (env, marker)
|
| 954 |
+
else:
|
| 955 |
+
marker = env
|
| 956 |
+
reqts.add(';'.join((r, marker)))
|
| 957 |
+
return reqts
|
| 958 |
+
|
| 959 |
+
assert self._data and not self._legacy
|
| 960 |
+
result = LegacyMetadata()
|
| 961 |
+
nmd = self._data
|
| 962 |
+
# import pdb; pdb.set_trace()
|
| 963 |
+
for nk, ok in self.LEGACY_MAPPING.items():
|
| 964 |
+
if not isinstance(nk, tuple):
|
| 965 |
+
if nk in nmd:
|
| 966 |
+
result[ok] = nmd[nk]
|
| 967 |
+
else:
|
| 968 |
+
d = nmd
|
| 969 |
+
found = True
|
| 970 |
+
for k in nk:
|
| 971 |
+
try:
|
| 972 |
+
d = d[k]
|
| 973 |
+
except (KeyError, IndexError):
|
| 974 |
+
found = False
|
| 975 |
+
break
|
| 976 |
+
if found:
|
| 977 |
+
result[ok] = d
|
| 978 |
+
r1 = process_entries(self.run_requires + self.meta_requires)
|
| 979 |
+
r2 = process_entries(self.build_requires + self.dev_requires)
|
| 980 |
+
if self.extras:
|
| 981 |
+
result['Provides-Extra'] = sorted(self.extras)
|
| 982 |
+
result['Requires-Dist'] = sorted(r1)
|
| 983 |
+
result['Setup-Requires-Dist'] = sorted(r2)
|
| 984 |
+
# TODO: any other fields wanted
|
| 985 |
+
return result
|
| 986 |
+
|
| 987 |
+
def write(self, path=None, fileobj=None, legacy=False, skip_unknown=True):
|
| 988 |
+
if [path, fileobj].count(None) != 1:
|
| 989 |
+
raise ValueError('Exactly one of path and fileobj is needed')
|
| 990 |
+
self.validate()
|
| 991 |
+
if legacy:
|
| 992 |
+
if self._legacy:
|
| 993 |
+
legacy_md = self._legacy
|
| 994 |
+
else:
|
| 995 |
+
legacy_md = self._to_legacy()
|
| 996 |
+
if path:
|
| 997 |
+
legacy_md.write(path, skip_unknown=skip_unknown)
|
| 998 |
+
else:
|
| 999 |
+
legacy_md.write_file(fileobj, skip_unknown=skip_unknown)
|
| 1000 |
+
else:
|
| 1001 |
+
if self._legacy:
|
| 1002 |
+
d = self._from_legacy()
|
| 1003 |
+
else:
|
| 1004 |
+
d = self._data
|
| 1005 |
+
if fileobj:
|
| 1006 |
+
json.dump(d, fileobj, ensure_ascii=True, indent=2, sort_keys=True)
|
| 1007 |
+
else:
|
| 1008 |
+
with codecs.open(path, 'w', 'utf-8') as f:
|
| 1009 |
+
json.dump(d, f, ensure_ascii=True, indent=2, sort_keys=True)
|
| 1010 |
+
|
| 1011 |
+
def add_requirements(self, requirements):
|
| 1012 |
+
if self._legacy:
|
| 1013 |
+
self._legacy.add_requirements(requirements)
|
| 1014 |
+
else:
|
| 1015 |
+
run_requires = self._data.setdefault('run_requires', [])
|
| 1016 |
+
always = None
|
| 1017 |
+
for entry in run_requires:
|
| 1018 |
+
if 'environment' not in entry and 'extra' not in entry:
|
| 1019 |
+
always = entry
|
| 1020 |
+
break
|
| 1021 |
+
if always is None:
|
| 1022 |
+
always = {'requires': requirements}
|
| 1023 |
+
run_requires.insert(0, always)
|
| 1024 |
+
else:
|
| 1025 |
+
rset = set(always['requires']) | set(requirements)
|
| 1026 |
+
always['requires'] = sorted(rset)
|
| 1027 |
+
|
| 1028 |
+
def __repr__(self):
|
| 1029 |
+
name = self.name or '(no name)'
|
| 1030 |
+
version = self.version or 'no version'
|
| 1031 |
+
return '<%s %s %s (%s)>' % (self.__class__.__name__, self.metadata_version, name, version)
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/resources.py
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2013-2017 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
from __future__ import unicode_literals
|
| 8 |
+
|
| 9 |
+
import bisect
|
| 10 |
+
import io
|
| 11 |
+
import logging
|
| 12 |
+
import os
|
| 13 |
+
import pkgutil
|
| 14 |
+
import sys
|
| 15 |
+
import types
|
| 16 |
+
import zipimport
|
| 17 |
+
|
| 18 |
+
from . import DistlibException
|
| 19 |
+
from .util import cached_property, get_cache_base, Cache
|
| 20 |
+
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
cache = None # created when needed
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class ResourceCache(Cache):
|
| 28 |
+
def __init__(self, base=None):
|
| 29 |
+
if base is None:
|
| 30 |
+
# Use native string to avoid issues on 2.x: see Python #20140.
|
| 31 |
+
base = os.path.join(get_cache_base(), str('resource-cache'))
|
| 32 |
+
super(ResourceCache, self).__init__(base)
|
| 33 |
+
|
| 34 |
+
def is_stale(self, resource, path):
|
| 35 |
+
"""
|
| 36 |
+
Is the cache stale for the given resource?
|
| 37 |
+
|
| 38 |
+
:param resource: The :class:`Resource` being cached.
|
| 39 |
+
:param path: The path of the resource in the cache.
|
| 40 |
+
:return: True if the cache is stale.
|
| 41 |
+
"""
|
| 42 |
+
# Cache invalidation is a hard problem :-)
|
| 43 |
+
return True
|
| 44 |
+
|
| 45 |
+
def get(self, resource):
|
| 46 |
+
"""
|
| 47 |
+
Get a resource into the cache,
|
| 48 |
+
|
| 49 |
+
:param resource: A :class:`Resource` instance.
|
| 50 |
+
:return: The pathname of the resource in the cache.
|
| 51 |
+
"""
|
| 52 |
+
prefix, path = resource.finder.get_cache_info(resource)
|
| 53 |
+
if prefix is None:
|
| 54 |
+
result = path
|
| 55 |
+
else:
|
| 56 |
+
result = os.path.join(self.base, self.prefix_to_dir(prefix), path)
|
| 57 |
+
dirname = os.path.dirname(result)
|
| 58 |
+
if not os.path.isdir(dirname):
|
| 59 |
+
os.makedirs(dirname)
|
| 60 |
+
if not os.path.exists(result):
|
| 61 |
+
stale = True
|
| 62 |
+
else:
|
| 63 |
+
stale = self.is_stale(resource, path)
|
| 64 |
+
if stale:
|
| 65 |
+
# write the bytes of the resource to the cache location
|
| 66 |
+
with open(result, 'wb') as f:
|
| 67 |
+
f.write(resource.bytes)
|
| 68 |
+
return result
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class ResourceBase(object):
|
| 72 |
+
def __init__(self, finder, name):
|
| 73 |
+
self.finder = finder
|
| 74 |
+
self.name = name
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class Resource(ResourceBase):
|
| 78 |
+
"""
|
| 79 |
+
A class representing an in-package resource, such as a data file. This is
|
| 80 |
+
not normally instantiated by user code, but rather by a
|
| 81 |
+
:class:`ResourceFinder` which manages the resource.
|
| 82 |
+
"""
|
| 83 |
+
is_container = False # Backwards compatibility
|
| 84 |
+
|
| 85 |
+
def as_stream(self):
|
| 86 |
+
"""
|
| 87 |
+
Get the resource as a stream.
|
| 88 |
+
|
| 89 |
+
This is not a property to make it obvious that it returns a new stream
|
| 90 |
+
each time.
|
| 91 |
+
"""
|
| 92 |
+
return self.finder.get_stream(self)
|
| 93 |
+
|
| 94 |
+
@cached_property
|
| 95 |
+
def file_path(self):
|
| 96 |
+
global cache
|
| 97 |
+
if cache is None:
|
| 98 |
+
cache = ResourceCache()
|
| 99 |
+
return cache.get(self)
|
| 100 |
+
|
| 101 |
+
@cached_property
|
| 102 |
+
def bytes(self):
|
| 103 |
+
return self.finder.get_bytes(self)
|
| 104 |
+
|
| 105 |
+
@cached_property
|
| 106 |
+
def size(self):
|
| 107 |
+
return self.finder.get_size(self)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class ResourceContainer(ResourceBase):
|
| 111 |
+
is_container = True # Backwards compatibility
|
| 112 |
+
|
| 113 |
+
@cached_property
|
| 114 |
+
def resources(self):
|
| 115 |
+
return self.finder.get_resources(self)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class ResourceFinder(object):
|
| 119 |
+
"""
|
| 120 |
+
Resource finder for file system resources.
|
| 121 |
+
"""
|
| 122 |
+
|
| 123 |
+
if sys.platform.startswith('java'):
|
| 124 |
+
skipped_extensions = ('.pyc', '.pyo', '.class')
|
| 125 |
+
else:
|
| 126 |
+
skipped_extensions = ('.pyc', '.pyo')
|
| 127 |
+
|
| 128 |
+
def __init__(self, module):
|
| 129 |
+
self.module = module
|
| 130 |
+
self.loader = getattr(module, '__loader__', None)
|
| 131 |
+
self.base = os.path.dirname(getattr(module, '__file__', ''))
|
| 132 |
+
|
| 133 |
+
def _adjust_path(self, path):
|
| 134 |
+
return os.path.realpath(path)
|
| 135 |
+
|
| 136 |
+
def _make_path(self, resource_name):
|
| 137 |
+
# Issue #50: need to preserve type of path on Python 2.x
|
| 138 |
+
# like os.path._get_sep
|
| 139 |
+
if isinstance(resource_name, bytes): # should only happen on 2.x
|
| 140 |
+
sep = b'/'
|
| 141 |
+
else:
|
| 142 |
+
sep = '/'
|
| 143 |
+
parts = resource_name.split(sep)
|
| 144 |
+
parts.insert(0, self.base)
|
| 145 |
+
result = os.path.join(*parts)
|
| 146 |
+
return self._adjust_path(result)
|
| 147 |
+
|
| 148 |
+
def _find(self, path):
|
| 149 |
+
return os.path.exists(path)
|
| 150 |
+
|
| 151 |
+
def get_cache_info(self, resource):
|
| 152 |
+
return None, resource.path
|
| 153 |
+
|
| 154 |
+
def find(self, resource_name):
|
| 155 |
+
path = self._make_path(resource_name)
|
| 156 |
+
if not self._find(path):
|
| 157 |
+
result = None
|
| 158 |
+
else:
|
| 159 |
+
if self._is_directory(path):
|
| 160 |
+
result = ResourceContainer(self, resource_name)
|
| 161 |
+
else:
|
| 162 |
+
result = Resource(self, resource_name)
|
| 163 |
+
result.path = path
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
def get_stream(self, resource):
|
| 167 |
+
return open(resource.path, 'rb')
|
| 168 |
+
|
| 169 |
+
def get_bytes(self, resource):
|
| 170 |
+
with open(resource.path, 'rb') as f:
|
| 171 |
+
return f.read()
|
| 172 |
+
|
| 173 |
+
def get_size(self, resource):
|
| 174 |
+
return os.path.getsize(resource.path)
|
| 175 |
+
|
| 176 |
+
def get_resources(self, resource):
|
| 177 |
+
def allowed(f):
|
| 178 |
+
return (f != '__pycache__' and not
|
| 179 |
+
f.endswith(self.skipped_extensions))
|
| 180 |
+
return set([f for f in os.listdir(resource.path) if allowed(f)])
|
| 181 |
+
|
| 182 |
+
def is_container(self, resource):
|
| 183 |
+
return self._is_directory(resource.path)
|
| 184 |
+
|
| 185 |
+
_is_directory = staticmethod(os.path.isdir)
|
| 186 |
+
|
| 187 |
+
def iterator(self, resource_name):
|
| 188 |
+
resource = self.find(resource_name)
|
| 189 |
+
if resource is not None:
|
| 190 |
+
todo = [resource]
|
| 191 |
+
while todo:
|
| 192 |
+
resource = todo.pop(0)
|
| 193 |
+
yield resource
|
| 194 |
+
if resource.is_container:
|
| 195 |
+
rname = resource.name
|
| 196 |
+
for name in resource.resources:
|
| 197 |
+
if not rname:
|
| 198 |
+
new_name = name
|
| 199 |
+
else:
|
| 200 |
+
new_name = '/'.join([rname, name])
|
| 201 |
+
child = self.find(new_name)
|
| 202 |
+
if child.is_container:
|
| 203 |
+
todo.append(child)
|
| 204 |
+
else:
|
| 205 |
+
yield child
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
class ZipResourceFinder(ResourceFinder):
|
| 209 |
+
"""
|
| 210 |
+
Resource finder for resources in .zip files.
|
| 211 |
+
"""
|
| 212 |
+
def __init__(self, module):
|
| 213 |
+
super(ZipResourceFinder, self).__init__(module)
|
| 214 |
+
archive = self.loader.archive
|
| 215 |
+
self.prefix_len = 1 + len(archive)
|
| 216 |
+
# PyPy doesn't have a _files attr on zipimporter, and you can't set one
|
| 217 |
+
if hasattr(self.loader, '_files'):
|
| 218 |
+
self._files = self.loader._files
|
| 219 |
+
else:
|
| 220 |
+
self._files = zipimport._zip_directory_cache[archive]
|
| 221 |
+
self.index = sorted(self._files)
|
| 222 |
+
|
| 223 |
+
def _adjust_path(self, path):
|
| 224 |
+
return path
|
| 225 |
+
|
| 226 |
+
def _find(self, path):
|
| 227 |
+
path = path[self.prefix_len:]
|
| 228 |
+
if path in self._files:
|
| 229 |
+
result = True
|
| 230 |
+
else:
|
| 231 |
+
if path and path[-1] != os.sep:
|
| 232 |
+
path = path + os.sep
|
| 233 |
+
i = bisect.bisect(self.index, path)
|
| 234 |
+
try:
|
| 235 |
+
result = self.index[i].startswith(path)
|
| 236 |
+
except IndexError:
|
| 237 |
+
result = False
|
| 238 |
+
if not result:
|
| 239 |
+
logger.debug('_find failed: %r %r', path, self.loader.prefix)
|
| 240 |
+
else:
|
| 241 |
+
logger.debug('_find worked: %r %r', path, self.loader.prefix)
|
| 242 |
+
return result
|
| 243 |
+
|
| 244 |
+
def get_cache_info(self, resource):
|
| 245 |
+
prefix = self.loader.archive
|
| 246 |
+
path = resource.path[1 + len(prefix):]
|
| 247 |
+
return prefix, path
|
| 248 |
+
|
| 249 |
+
def get_bytes(self, resource):
|
| 250 |
+
return self.loader.get_data(resource.path)
|
| 251 |
+
|
| 252 |
+
def get_stream(self, resource):
|
| 253 |
+
return io.BytesIO(self.get_bytes(resource))
|
| 254 |
+
|
| 255 |
+
def get_size(self, resource):
|
| 256 |
+
path = resource.path[self.prefix_len:]
|
| 257 |
+
return self._files[path][3]
|
| 258 |
+
|
| 259 |
+
def get_resources(self, resource):
|
| 260 |
+
path = resource.path[self.prefix_len:]
|
| 261 |
+
if path and path[-1] != os.sep:
|
| 262 |
+
path += os.sep
|
| 263 |
+
plen = len(path)
|
| 264 |
+
result = set()
|
| 265 |
+
i = bisect.bisect(self.index, path)
|
| 266 |
+
while i < len(self.index):
|
| 267 |
+
if not self.index[i].startswith(path):
|
| 268 |
+
break
|
| 269 |
+
s = self.index[i][plen:]
|
| 270 |
+
result.add(s.split(os.sep, 1)[0]) # only immediate children
|
| 271 |
+
i += 1
|
| 272 |
+
return result
|
| 273 |
+
|
| 274 |
+
def _is_directory(self, path):
|
| 275 |
+
path = path[self.prefix_len:]
|
| 276 |
+
if path and path[-1] != os.sep:
|
| 277 |
+
path += os.sep
|
| 278 |
+
i = bisect.bisect(self.index, path)
|
| 279 |
+
try:
|
| 280 |
+
result = self.index[i].startswith(path)
|
| 281 |
+
except IndexError:
|
| 282 |
+
result = False
|
| 283 |
+
return result
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
_finder_registry = {
|
| 287 |
+
type(None): ResourceFinder,
|
| 288 |
+
zipimport.zipimporter: ZipResourceFinder
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
try:
|
| 292 |
+
# In Python 3.6, _frozen_importlib -> _frozen_importlib_external
|
| 293 |
+
try:
|
| 294 |
+
import _frozen_importlib_external as _fi
|
| 295 |
+
except ImportError:
|
| 296 |
+
import _frozen_importlib as _fi
|
| 297 |
+
_finder_registry[_fi.SourceFileLoader] = ResourceFinder
|
| 298 |
+
_finder_registry[_fi.FileFinder] = ResourceFinder
|
| 299 |
+
# See issue #146
|
| 300 |
+
_finder_registry[_fi.SourcelessFileLoader] = ResourceFinder
|
| 301 |
+
del _fi
|
| 302 |
+
except (ImportError, AttributeError):
|
| 303 |
+
pass
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
def register_finder(loader, finder_maker):
|
| 307 |
+
_finder_registry[type(loader)] = finder_maker
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
_finder_cache = {}
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def finder(package):
|
| 314 |
+
"""
|
| 315 |
+
Return a resource finder for a package.
|
| 316 |
+
:param package: The name of the package.
|
| 317 |
+
:return: A :class:`ResourceFinder` instance for the package.
|
| 318 |
+
"""
|
| 319 |
+
if package in _finder_cache:
|
| 320 |
+
result = _finder_cache[package]
|
| 321 |
+
else:
|
| 322 |
+
if package not in sys.modules:
|
| 323 |
+
__import__(package)
|
| 324 |
+
module = sys.modules[package]
|
| 325 |
+
path = getattr(module, '__path__', None)
|
| 326 |
+
if path is None:
|
| 327 |
+
raise DistlibException('You cannot get a finder for a module, '
|
| 328 |
+
'only for a package')
|
| 329 |
+
loader = getattr(module, '__loader__', None)
|
| 330 |
+
finder_maker = _finder_registry.get(type(loader))
|
| 331 |
+
if finder_maker is None:
|
| 332 |
+
raise DistlibException('Unable to locate finder for %r' % package)
|
| 333 |
+
result = finder_maker(module)
|
| 334 |
+
_finder_cache[package] = result
|
| 335 |
+
return result
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
_dummy_module = types.ModuleType(str('__dummy__'))
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def finder_for_path(path):
|
| 342 |
+
"""
|
| 343 |
+
Return a resource finder for a path, which should represent a container.
|
| 344 |
+
|
| 345 |
+
:param path: The path.
|
| 346 |
+
:return: A :class:`ResourceFinder` instance for the path.
|
| 347 |
+
"""
|
| 348 |
+
result = None
|
| 349 |
+
# calls any path hooks, gets importer into cache
|
| 350 |
+
pkgutil.get_importer(path)
|
| 351 |
+
loader = sys.path_importer_cache.get(path)
|
| 352 |
+
finder = _finder_registry.get(type(loader))
|
| 353 |
+
if finder:
|
| 354 |
+
module = _dummy_module
|
| 355 |
+
module.__file__ = os.path.join(path, '')
|
| 356 |
+
module.__loader__ = loader
|
| 357 |
+
result = finder(module)
|
| 358 |
+
return result
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/scripts.py
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
#
|
| 3 |
+
# Copyright (C) 2013-2023 Vinay Sajip.
|
| 4 |
+
# Licensed to the Python Software Foundation under a contributor agreement.
|
| 5 |
+
# See LICENSE.txt and CONTRIBUTORS.txt.
|
| 6 |
+
#
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
import logging
|
| 9 |
+
import os
|
| 10 |
+
import re
|
| 11 |
+
import struct
|
| 12 |
+
import sys
|
| 13 |
+
import time
|
| 14 |
+
from zipfile import ZipInfo
|
| 15 |
+
|
| 16 |
+
from .compat import sysconfig, detect_encoding, ZipFile
|
| 17 |
+
from .resources import finder
|
| 18 |
+
from .util import (FileOperator, get_export_entry, convert_path, get_executable, get_platform, in_venv)
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
_DEFAULT_MANIFEST = '''
|
| 23 |
+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 24 |
+
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
|
| 25 |
+
<assemblyIdentity version="1.0.0.0"
|
| 26 |
+
processorArchitecture="X86"
|
| 27 |
+
name="%s"
|
| 28 |
+
type="win32"/>
|
| 29 |
+
|
| 30 |
+
<!-- Identify the application security requirements. -->
|
| 31 |
+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
|
| 32 |
+
<security>
|
| 33 |
+
<requestedPrivileges>
|
| 34 |
+
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
|
| 35 |
+
</requestedPrivileges>
|
| 36 |
+
</security>
|
| 37 |
+
</trustInfo>
|
| 38 |
+
</assembly>'''.strip()
|
| 39 |
+
|
| 40 |
+
# check if Python is called on the first line with this expression
|
| 41 |
+
FIRST_LINE_RE = re.compile(b'^#!.*pythonw?[0-9.]*([ \t].*)?$')
|
| 42 |
+
SCRIPT_TEMPLATE = r'''# -*- coding: utf-8 -*-
|
| 43 |
+
import re
|
| 44 |
+
import sys
|
| 45 |
+
from %(module)s import %(import_name)s
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
| 48 |
+
sys.exit(%(func)s())
|
| 49 |
+
'''
|
| 50 |
+
|
| 51 |
+
# Pre-fetch the contents of all executable wrapper stubs.
|
| 52 |
+
# This is to address https://github.com/pypa/pip/issues/12666.
|
| 53 |
+
# When updating pip, we rename the old pip in place before installing the
|
| 54 |
+
# new version. If we try to fetch a wrapper *after* that rename, the finder
|
| 55 |
+
# machinery will be confused as the package is no longer available at the
|
| 56 |
+
# location where it was imported from. So we load everything into memory in
|
| 57 |
+
# advance.
|
| 58 |
+
|
| 59 |
+
if os.name == 'nt' or (os.name == 'java' and os._name == 'nt'):
|
| 60 |
+
# Issue 31: don't hardcode an absolute package name, but
|
| 61 |
+
# determine it relative to the current package
|
| 62 |
+
DISTLIB_PACKAGE = __name__.rsplit('.', 1)[0]
|
| 63 |
+
|
| 64 |
+
WRAPPERS = {
|
| 65 |
+
r.name: r.bytes
|
| 66 |
+
for r in finder(DISTLIB_PACKAGE).iterator("")
|
| 67 |
+
if r.name.endswith(".exe")
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def enquote_executable(executable):
|
| 72 |
+
if ' ' in executable:
|
| 73 |
+
# make sure we quote only the executable in case of env
|
| 74 |
+
# for example /usr/bin/env "/dir with spaces/bin/jython"
|
| 75 |
+
# instead of "/usr/bin/env /dir with spaces/bin/jython"
|
| 76 |
+
# otherwise whole
|
| 77 |
+
if executable.startswith('/usr/bin/env '):
|
| 78 |
+
env, _executable = executable.split(' ', 1)
|
| 79 |
+
if ' ' in _executable and not _executable.startswith('"'):
|
| 80 |
+
executable = '%s "%s"' % (env, _executable)
|
| 81 |
+
else:
|
| 82 |
+
if not executable.startswith('"'):
|
| 83 |
+
executable = '"%s"' % executable
|
| 84 |
+
return executable
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Keep the old name around (for now), as there is at least one project using it!
|
| 88 |
+
_enquote_executable = enquote_executable
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class ScriptMaker(object):
|
| 92 |
+
"""
|
| 93 |
+
A class to copy or create scripts from source scripts or callable
|
| 94 |
+
specifications.
|
| 95 |
+
"""
|
| 96 |
+
script_template = SCRIPT_TEMPLATE
|
| 97 |
+
|
| 98 |
+
executable = None # for shebangs
|
| 99 |
+
|
| 100 |
+
def __init__(self, source_dir, target_dir, add_launchers=True, dry_run=False, fileop=None):
|
| 101 |
+
self.source_dir = source_dir
|
| 102 |
+
self.target_dir = target_dir
|
| 103 |
+
self.add_launchers = add_launchers
|
| 104 |
+
self.force = False
|
| 105 |
+
self.clobber = False
|
| 106 |
+
# It only makes sense to set mode bits on POSIX.
|
| 107 |
+
self.set_mode = (os.name == 'posix') or (os.name == 'java' and os._name == 'posix')
|
| 108 |
+
self.variants = set(('', 'X.Y'))
|
| 109 |
+
self._fileop = fileop or FileOperator(dry_run)
|
| 110 |
+
|
| 111 |
+
self._is_nt = os.name == 'nt' or (os.name == 'java' and os._name == 'nt')
|
| 112 |
+
self.version_info = sys.version_info
|
| 113 |
+
|
| 114 |
+
def _get_alternate_executable(self, executable, options):
|
| 115 |
+
if options.get('gui', False) and self._is_nt: # pragma: no cover
|
| 116 |
+
dn, fn = os.path.split(executable)
|
| 117 |
+
fn = fn.replace('python', 'pythonw')
|
| 118 |
+
executable = os.path.join(dn, fn)
|
| 119 |
+
return executable
|
| 120 |
+
|
| 121 |
+
if sys.platform.startswith('java'): # pragma: no cover
|
| 122 |
+
|
| 123 |
+
def _is_shell(self, executable):
|
| 124 |
+
"""
|
| 125 |
+
Determine if the specified executable is a script
|
| 126 |
+
(contains a #! line)
|
| 127 |
+
"""
|
| 128 |
+
try:
|
| 129 |
+
with open(executable) as fp:
|
| 130 |
+
return fp.read(2) == '#!'
|
| 131 |
+
except (OSError, IOError):
|
| 132 |
+
logger.warning('Failed to open %s', executable)
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
+
def _fix_jython_executable(self, executable):
|
| 136 |
+
if self._is_shell(executable):
|
| 137 |
+
# Workaround for Jython is not needed on Linux systems.
|
| 138 |
+
import java
|
| 139 |
+
|
| 140 |
+
if java.lang.System.getProperty('os.name') == 'Linux':
|
| 141 |
+
return executable
|
| 142 |
+
elif executable.lower().endswith('jython.exe'):
|
| 143 |
+
# Use wrapper exe for Jython on Windows
|
| 144 |
+
return executable
|
| 145 |
+
return '/usr/bin/env %s' % executable
|
| 146 |
+
|
| 147 |
+
def _build_shebang(self, executable, post_interp):
|
| 148 |
+
"""
|
| 149 |
+
Build a shebang line. In the simple case (on Windows, or a shebang line
|
| 150 |
+
which is not too long or contains spaces) use a simple formulation for
|
| 151 |
+
the shebang. Otherwise, use /bin/sh as the executable, with a contrived
|
| 152 |
+
shebang which allows the script to run either under Python or sh, using
|
| 153 |
+
suitable quoting. Thanks to Harald Nordgren for his input.
|
| 154 |
+
|
| 155 |
+
See also: http://www.in-ulm.de/~mascheck/various/shebang/#length
|
| 156 |
+
https://hg.mozilla.org/mozilla-central/file/tip/mach
|
| 157 |
+
"""
|
| 158 |
+
if os.name != 'posix':
|
| 159 |
+
simple_shebang = True
|
| 160 |
+
elif getattr(sys, "cross_compiling", False):
|
| 161 |
+
# In a cross-compiling environment, the shebang will likely be a
|
| 162 |
+
# script; this *must* be invoked with the "safe" version of the
|
| 163 |
+
# shebang, or else using os.exec() to run the entry script will
|
| 164 |
+
# fail, raising "OSError 8 [Errno 8] Exec format error".
|
| 165 |
+
simple_shebang = False
|
| 166 |
+
else:
|
| 167 |
+
# Add 3 for '#!' prefix and newline suffix.
|
| 168 |
+
shebang_length = len(executable) + len(post_interp) + 3
|
| 169 |
+
if sys.platform == 'darwin':
|
| 170 |
+
max_shebang_length = 512
|
| 171 |
+
else:
|
| 172 |
+
max_shebang_length = 127
|
| 173 |
+
simple_shebang = ((b' ' not in executable) and (shebang_length <= max_shebang_length))
|
| 174 |
+
|
| 175 |
+
if simple_shebang:
|
| 176 |
+
result = b'#!' + executable + post_interp + b'\n'
|
| 177 |
+
else:
|
| 178 |
+
result = b'#!/bin/sh\n'
|
| 179 |
+
result += b"'''exec' " + executable + post_interp + b' "$0" "$@"\n'
|
| 180 |
+
result += b"' '''\n"
|
| 181 |
+
return result
|
| 182 |
+
|
| 183 |
+
def _get_shebang(self, encoding, post_interp=b'', options=None):
|
| 184 |
+
enquote = True
|
| 185 |
+
if self.executable:
|
| 186 |
+
executable = self.executable
|
| 187 |
+
enquote = False # assume this will be taken care of
|
| 188 |
+
elif not sysconfig.is_python_build():
|
| 189 |
+
executable = get_executable()
|
| 190 |
+
elif in_venv(): # pragma: no cover
|
| 191 |
+
executable = os.path.join(sysconfig.get_path('scripts'), 'python%s' % sysconfig.get_config_var('EXE'))
|
| 192 |
+
else: # pragma: no cover
|
| 193 |
+
if os.name == 'nt':
|
| 194 |
+
# for Python builds from source on Windows, no Python executables with
|
| 195 |
+
# a version suffix are created, so we use python.exe
|
| 196 |
+
executable = os.path.join(sysconfig.get_config_var('BINDIR'),
|
| 197 |
+
'python%s' % (sysconfig.get_config_var('EXE')))
|
| 198 |
+
else:
|
| 199 |
+
executable = os.path.join(
|
| 200 |
+
sysconfig.get_config_var('BINDIR'),
|
| 201 |
+
'python%s%s' % (sysconfig.get_config_var('VERSION'), sysconfig.get_config_var('EXE')))
|
| 202 |
+
if options:
|
| 203 |
+
executable = self._get_alternate_executable(executable, options)
|
| 204 |
+
|
| 205 |
+
if sys.platform.startswith('java'): # pragma: no cover
|
| 206 |
+
executable = self._fix_jython_executable(executable)
|
| 207 |
+
|
| 208 |
+
# Normalise case for Windows - COMMENTED OUT
|
| 209 |
+
# executable = os.path.normcase(executable)
|
| 210 |
+
# N.B. The normalising operation above has been commented out: See
|
| 211 |
+
# issue #124. Although paths in Windows are generally case-insensitive,
|
| 212 |
+
# they aren't always. For example, a path containing a ẞ (which is a
|
| 213 |
+
# LATIN CAPITAL LETTER SHARP S - U+1E9E) is normcased to ß (which is a
|
| 214 |
+
# LATIN SMALL LETTER SHARP S' - U+00DF). The two are not considered by
|
| 215 |
+
# Windows as equivalent in path names.
|
| 216 |
+
|
| 217 |
+
# If the user didn't specify an executable, it may be necessary to
|
| 218 |
+
# cater for executable paths with spaces (not uncommon on Windows)
|
| 219 |
+
if enquote:
|
| 220 |
+
executable = enquote_executable(executable)
|
| 221 |
+
# Issue #51: don't use fsencode, since we later try to
|
| 222 |
+
# check that the shebang is decodable using utf-8.
|
| 223 |
+
executable = executable.encode('utf-8')
|
| 224 |
+
# in case of IronPython, play safe and enable frames support
|
| 225 |
+
if (sys.platform == 'cli' and '-X:Frames' not in post_interp and
|
| 226 |
+
'-X:FullFrames' not in post_interp): # pragma: no cover
|
| 227 |
+
post_interp += b' -X:Frames'
|
| 228 |
+
shebang = self._build_shebang(executable, post_interp)
|
| 229 |
+
# Python parser starts to read a script using UTF-8 until
|
| 230 |
+
# it gets a #coding:xxx cookie. The shebang has to be the
|
| 231 |
+
# first line of a file, the #coding:xxx cookie cannot be
|
| 232 |
+
# written before. So the shebang has to be decodable from
|
| 233 |
+
# UTF-8.
|
| 234 |
+
try:
|
| 235 |
+
shebang.decode('utf-8')
|
| 236 |
+
except UnicodeDecodeError: # pragma: no cover
|
| 237 |
+
raise ValueError('The shebang (%r) is not decodable from utf-8' % shebang)
|
| 238 |
+
# If the script is encoded to a custom encoding (use a
|
| 239 |
+
# #coding:xxx cookie), the shebang has to be decodable from
|
| 240 |
+
# the script encoding too.
|
| 241 |
+
if encoding != 'utf-8':
|
| 242 |
+
try:
|
| 243 |
+
shebang.decode(encoding)
|
| 244 |
+
except UnicodeDecodeError: # pragma: no cover
|
| 245 |
+
raise ValueError('The shebang (%r) is not decodable '
|
| 246 |
+
'from the script encoding (%r)' % (shebang, encoding))
|
| 247 |
+
return shebang
|
| 248 |
+
|
| 249 |
+
def _get_script_text(self, entry):
|
| 250 |
+
return self.script_template % dict(
|
| 251 |
+
module=entry.prefix, import_name=entry.suffix.split('.')[0], func=entry.suffix)
|
| 252 |
+
|
| 253 |
+
manifest = _DEFAULT_MANIFEST
|
| 254 |
+
|
| 255 |
+
def get_manifest(self, exename):
|
| 256 |
+
base = os.path.basename(exename)
|
| 257 |
+
return self.manifest % base
|
| 258 |
+
|
| 259 |
+
def _write_script(self, names, shebang, script_bytes, filenames, ext):
|
| 260 |
+
use_launcher = self.add_launchers and self._is_nt
|
| 261 |
+
if not use_launcher:
|
| 262 |
+
script_bytes = shebang + script_bytes
|
| 263 |
+
else: # pragma: no cover
|
| 264 |
+
if ext == 'py':
|
| 265 |
+
launcher = self._get_launcher('t')
|
| 266 |
+
else:
|
| 267 |
+
launcher = self._get_launcher('w')
|
| 268 |
+
stream = BytesIO()
|
| 269 |
+
with ZipFile(stream, 'w') as zf:
|
| 270 |
+
source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH')
|
| 271 |
+
if source_date_epoch:
|
| 272 |
+
date_time = time.gmtime(int(source_date_epoch))[:6]
|
| 273 |
+
zinfo = ZipInfo(filename='__main__.py', date_time=date_time)
|
| 274 |
+
zf.writestr(zinfo, script_bytes)
|
| 275 |
+
else:
|
| 276 |
+
zf.writestr('__main__.py', script_bytes)
|
| 277 |
+
zip_data = stream.getvalue()
|
| 278 |
+
script_bytes = launcher + shebang + zip_data
|
| 279 |
+
for name in names:
|
| 280 |
+
outname = os.path.join(self.target_dir, name)
|
| 281 |
+
if use_launcher: # pragma: no cover
|
| 282 |
+
n, e = os.path.splitext(outname)
|
| 283 |
+
if e.startswith('.py'):
|
| 284 |
+
outname = n
|
| 285 |
+
outname = '%s.exe' % outname
|
| 286 |
+
try:
|
| 287 |
+
self._fileop.write_binary_file(outname, script_bytes)
|
| 288 |
+
except Exception:
|
| 289 |
+
# Failed writing an executable - it might be in use.
|
| 290 |
+
logger.warning('Failed to write executable - trying to '
|
| 291 |
+
'use .deleteme logic')
|
| 292 |
+
dfname = '%s.deleteme' % outname
|
| 293 |
+
if os.path.exists(dfname):
|
| 294 |
+
os.remove(dfname) # Not allowed to fail here
|
| 295 |
+
os.rename(outname, dfname) # nor here
|
| 296 |
+
self._fileop.write_binary_file(outname, script_bytes)
|
| 297 |
+
logger.debug('Able to replace executable using '
|
| 298 |
+
'.deleteme logic')
|
| 299 |
+
try:
|
| 300 |
+
os.remove(dfname)
|
| 301 |
+
except Exception:
|
| 302 |
+
pass # still in use - ignore error
|
| 303 |
+
else:
|
| 304 |
+
if self._is_nt and not outname.endswith('.' + ext): # pragma: no cover
|
| 305 |
+
outname = '%s.%s' % (outname, ext)
|
| 306 |
+
if os.path.exists(outname) and not self.clobber:
|
| 307 |
+
logger.warning('Skipping existing file %s', outname)
|
| 308 |
+
continue
|
| 309 |
+
self._fileop.write_binary_file(outname, script_bytes)
|
| 310 |
+
if self.set_mode:
|
| 311 |
+
self._fileop.set_executable_mode([outname])
|
| 312 |
+
filenames.append(outname)
|
| 313 |
+
|
| 314 |
+
variant_separator = '-'
|
| 315 |
+
|
| 316 |
+
def get_script_filenames(self, name):
|
| 317 |
+
result = set()
|
| 318 |
+
if '' in self.variants:
|
| 319 |
+
result.add(name)
|
| 320 |
+
if 'X' in self.variants:
|
| 321 |
+
result.add('%s%s' % (name, self.version_info[0]))
|
| 322 |
+
if 'X.Y' in self.variants:
|
| 323 |
+
result.add('%s%s%s.%s' % (name, self.variant_separator, self.version_info[0], self.version_info[1]))
|
| 324 |
+
return result
|
| 325 |
+
|
| 326 |
+
def _make_script(self, entry, filenames, options=None):
|
| 327 |
+
post_interp = b''
|
| 328 |
+
if options:
|
| 329 |
+
args = options.get('interpreter_args', [])
|
| 330 |
+
if args:
|
| 331 |
+
args = ' %s' % ' '.join(args)
|
| 332 |
+
post_interp = args.encode('utf-8')
|
| 333 |
+
shebang = self._get_shebang('utf-8', post_interp, options=options)
|
| 334 |
+
script = self._get_script_text(entry).encode('utf-8')
|
| 335 |
+
scriptnames = self.get_script_filenames(entry.name)
|
| 336 |
+
if options and options.get('gui', False):
|
| 337 |
+
ext = 'pyw'
|
| 338 |
+
else:
|
| 339 |
+
ext = 'py'
|
| 340 |
+
self._write_script(scriptnames, shebang, script, filenames, ext)
|
| 341 |
+
|
| 342 |
+
def _copy_script(self, script, filenames):
|
| 343 |
+
adjust = False
|
| 344 |
+
script = os.path.join(self.source_dir, convert_path(script))
|
| 345 |
+
outname = os.path.join(self.target_dir, os.path.basename(script))
|
| 346 |
+
if not self.force and not self._fileop.newer(script, outname):
|
| 347 |
+
logger.debug('not copying %s (up-to-date)', script)
|
| 348 |
+
return
|
| 349 |
+
|
| 350 |
+
# Always open the file, but ignore failures in dry-run mode --
|
| 351 |
+
# that way, we'll get accurate feedback if we can read the
|
| 352 |
+
# script.
|
| 353 |
+
try:
|
| 354 |
+
f = open(script, 'rb')
|
| 355 |
+
except IOError: # pragma: no cover
|
| 356 |
+
if not self.dry_run:
|
| 357 |
+
raise
|
| 358 |
+
f = None
|
| 359 |
+
else:
|
| 360 |
+
first_line = f.readline()
|
| 361 |
+
if not first_line: # pragma: no cover
|
| 362 |
+
logger.warning('%s is an empty file (skipping)', script)
|
| 363 |
+
return
|
| 364 |
+
|
| 365 |
+
match = FIRST_LINE_RE.match(first_line.replace(b'\r\n', b'\n'))
|
| 366 |
+
if match:
|
| 367 |
+
adjust = True
|
| 368 |
+
post_interp = match.group(1) or b''
|
| 369 |
+
|
| 370 |
+
if not adjust:
|
| 371 |
+
if f:
|
| 372 |
+
f.close()
|
| 373 |
+
self._fileop.copy_file(script, outname)
|
| 374 |
+
if self.set_mode:
|
| 375 |
+
self._fileop.set_executable_mode([outname])
|
| 376 |
+
filenames.append(outname)
|
| 377 |
+
else:
|
| 378 |
+
logger.info('copying and adjusting %s -> %s', script, self.target_dir)
|
| 379 |
+
if not self._fileop.dry_run:
|
| 380 |
+
encoding, lines = detect_encoding(f.readline)
|
| 381 |
+
f.seek(0)
|
| 382 |
+
shebang = self._get_shebang(encoding, post_interp)
|
| 383 |
+
if b'pythonw' in first_line: # pragma: no cover
|
| 384 |
+
ext = 'pyw'
|
| 385 |
+
else:
|
| 386 |
+
ext = 'py'
|
| 387 |
+
n = os.path.basename(outname)
|
| 388 |
+
self._write_script([n], shebang, f.read(), filenames, ext)
|
| 389 |
+
if f:
|
| 390 |
+
f.close()
|
| 391 |
+
|
| 392 |
+
@property
|
| 393 |
+
def dry_run(self):
|
| 394 |
+
return self._fileop.dry_run
|
| 395 |
+
|
| 396 |
+
@dry_run.setter
|
| 397 |
+
def dry_run(self, value):
|
| 398 |
+
self._fileop.dry_run = value
|
| 399 |
+
|
| 400 |
+
if os.name == 'nt' or (os.name == 'java' and os._name == 'nt'): # pragma: no cover
|
| 401 |
+
# Executable launcher support.
|
| 402 |
+
# Launchers are from https://bitbucket.org/vinay.sajip/simple_launcher/
|
| 403 |
+
|
| 404 |
+
def _get_launcher(self, kind):
|
| 405 |
+
if struct.calcsize('P') == 8: # 64-bit
|
| 406 |
+
bits = '64'
|
| 407 |
+
else:
|
| 408 |
+
bits = '32'
|
| 409 |
+
platform_suffix = '-arm' if get_platform() == 'win-arm64' else ''
|
| 410 |
+
name = '%s%s%s.exe' % (kind, bits, platform_suffix)
|
| 411 |
+
if name not in WRAPPERS:
|
| 412 |
+
msg = ('Unable to find resource %s in package %s' %
|
| 413 |
+
(name, DISTLIB_PACKAGE))
|
| 414 |
+
raise ValueError(msg)
|
| 415 |
+
return WRAPPERS[name]
|
| 416 |
+
|
| 417 |
+
# Public API follows
|
| 418 |
+
|
| 419 |
+
def make(self, specification, options=None):
|
| 420 |
+
"""
|
| 421 |
+
Make a script.
|
| 422 |
+
|
| 423 |
+
:param specification: The specification, which is either a valid export
|
| 424 |
+
entry specification (to make a script from a
|
| 425 |
+
callable) or a filename (to make a script by
|
| 426 |
+
copying from a source location).
|
| 427 |
+
:param options: A dictionary of options controlling script generation.
|
| 428 |
+
:return: A list of all absolute pathnames written to.
|
| 429 |
+
"""
|
| 430 |
+
filenames = []
|
| 431 |
+
entry = get_export_entry(specification)
|
| 432 |
+
if entry is None:
|
| 433 |
+
self._copy_script(specification, filenames)
|
| 434 |
+
else:
|
| 435 |
+
self._make_script(entry, filenames, options=options)
|
| 436 |
+
return filenames
|
| 437 |
+
|
| 438 |
+
def make_multiple(self, specifications, options=None):
|
| 439 |
+
"""
|
| 440 |
+
Take a list of specifications and make scripts from them,
|
| 441 |
+
:param specifications: A list of specifications.
|
| 442 |
+
:return: A list of all absolute pathnames written to,
|
| 443 |
+
"""
|
| 444 |
+
filenames = []
|
| 445 |
+
for specification in specifications:
|
| 446 |
+
filenames.extend(self.make(specification, options))
|
| 447 |
+
return filenames
|
ACE_plus/flashenv/lib/python3.10/site-packages/pip/_vendor/distlib/t32.exe
ADDED
|
Binary file (97.8 kB). View file
|
|
|