|
| 1 | +""" |
| 2 | +This module returns the installation location of cacert.pem or its contents. |
| 3 | +
|
| 4 | +Functionality derived from https://github.com/certifi/python-certifi/blob/master/certifi/core.py |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import atexit |
| 9 | + |
| 10 | + |
| 11 | +def exit_MVARCS_CTX() -> None: |
| 12 | + _MVARCS_CTX.__exit__(None, None, None) # type: ignore[union-attr] |
| 13 | + |
| 14 | + |
| 15 | +if sys.version_info >= (3, 11): |
| 16 | + |
| 17 | + from importlib.resources import as_file, files |
| 18 | + |
| 19 | + _MVARCS_CTX = None |
| 20 | + _MVARCS_PATH = None |
| 21 | + |
| 22 | + def where() -> str: |
| 23 | + # This is slightly terrible, but we want to delay extracting the file |
| 24 | + # in cases where we're inside of a zipimport situation until someone |
| 25 | + # actually calls where(), but we don't want to re-extract the file |
| 26 | + # on every call of where(), so we'll do it once then store it in a |
| 27 | + # global variable. |
| 28 | + global _MVARCS_CTX |
| 29 | + global _MVARCS_PATH |
| 30 | + if _MVARCS_PATH is None: |
| 31 | + # This is slightly janky, the importlib.resources API wants you to |
| 32 | + # manage the cleanup of this file, so it doesn't actually return a |
| 33 | + # path, it returns a context manager that will give you the path |
| 34 | + # when you enter it and will do any cleanup when you leave it. In |
| 35 | + # the common case of not needing a temporary file, it will just |
| 36 | + # return the file system location and the __exit__() is a no-op. |
| 37 | + # |
| 38 | + # We also have to hold onto the actual context manager, because |
| 39 | + # it will do the cleanup whenever it gets garbage collected, so |
| 40 | + # we will also store that at the global level as well. |
| 41 | + _MVARCS_CTX = as_file(files("mvarcs").joinpath("mvarcs/cacert.pem")) |
| 42 | + _MVARCS_PATH = str(_MVARCS_CTX.__enter__()) |
| 43 | + atexit.register(exit_MVARCS_CTX) |
| 44 | + |
| 45 | + return _MVARCS_PATH |
| 46 | + |
| 47 | + def contents() -> str: |
| 48 | + return files("mvarcs").joinpath("mvarcs/cacert.pem").read_text(encoding="ascii") |
| 49 | + |
| 50 | +else: |
| 51 | + import os |
| 52 | + import types |
| 53 | + from typing import Union |
| 54 | + |
| 55 | + Package = Union[types.ModuleType, str] |
| 56 | + Resource = Union[str, "os.PathLike"] |
| 57 | + |
| 58 | + # This fallback will work for Python versions prior to 3.7 that lack the |
| 59 | + # importlib.resources module but relies on the existing `where` function |
| 60 | + # so won't address issues with environments like PyOxidizer that don't set |
| 61 | + # __file__ on modules. |
| 62 | + def read_text( |
| 63 | + package: Package, |
| 64 | + resource: Resource, |
| 65 | + encoding: str = "utf-8", |
| 66 | + errors: str = "strict", |
| 67 | + ) -> str: |
| 68 | + with open(where(), encoding=encoding) as data: |
| 69 | + return data.read() |
| 70 | + |
| 71 | + # If we don't have importlib.resources, then we will just do the old logic |
| 72 | + # of assuming we're on the filesystem and munge the path directly. |
| 73 | + def where() -> str: |
| 74 | + f = os.path.dirname(__file__) |
| 75 | + return os.path.join(f, os.path.join("mvarcs", "cacert.pem")) |
| 76 | + |
| 77 | + def contents() -> str: |
| 78 | + return read_text("mvarcs", "mvarcs/cacert.pem", encoding="ascii") |
0 commit comments