Debian Codegraph @

lib/ansible/_vendor/__init__.py

raw
1 # (c) 2020 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 import os
7 import pkgutil
8 import sys
9 import warnings
10
11 # This package exists to host vendored top-level Python packages for downstream packaging. Any Python packages
12 # installed beneath this one will be masked from the Ansible loader, and available from the front of sys.path.
13 # It is expected that the vendored packages will be loaded very early, so a warning will be fired on import of
14 # the top-level ansible package if any packages beneath this are already loaded at that point.
15 #
16 # Python packages may be installed here during downstream packaging using something like:
17 # pip install --upgrade -t (path to this dir) cryptography pyyaml packaging jinja2
18
19 # mask vendored content below this package from being accessed as an ansible subpackage
20 __path__ = []
21
22
23 def _ensure_vendored_path_entry():
24 """
25 Ensure that any downstream-bundled content beneath this package is available at the top of sys.path
26 """
27 # patch our vendored dir onto sys.path
28 vendored_path_entry = os.path.dirname(__file__)
29 vendored_module_names = set(m[1] for m in pkgutil.iter_modules([vendored_path_entry], '')) # m[1] == m.name
30
31 if vendored_module_names:
32 # patch us early to load vendored deps transparently
33 if vendored_path_entry in sys.path:
34 # handle reload case by removing the existing entry, wherever it might be
35 sys.path.remove(vendored_path_entry)
36 sys.path.insert(0, vendored_path_entry)
37
38 already_loaded_vendored_modules = set(sys.modules.keys()).intersection(vendored_module_names)
39
40 if already_loaded_vendored_modules:
41 warnings.warn('One or more Python packages bundled by this ansible-core distribution were already '
42 'loaded ({0}). This may result in undefined behavior.'.format(', '.join(sorted(already_loaded_vendored_modules))))
43
44
45 _ensure_vendored_path_entry()