diff options
author | Matt Davis <nitzmahone@users.noreply.github.com> | 2020-06-15 16:22:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-15 16:22:25 -0700 |
commit | de63cba7e88ea509930f8f695fcdc2b2fe61be2a (patch) | |
tree | eed553d33b839e91d1d45b5962438f93181709d0 /lib | |
parent | 7641d32f8ec0f18722ded8b2bea253a705d3917c (diff) | |
download | ansible-de63cba7e88ea509930f8f695fcdc2b2fe61be2a.tar.gz |
transparent downstream vendoring (#69850)
* builtin downstream vendoring support
* allows downstream packagers to install packages to `ansible/_vendor` that will automatically be added to head of sys.path during `ansible` package load
* tests
* sort conflicting package names in warning text
* sanity fixes
* skip unnecessary comparison
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/__init__.py | 3 | ||||
-rw-r--r-- | lib/ansible/_vendor/__init__.py | 46 |
2 files changed, 49 insertions, 0 deletions
diff --git a/lib/ansible/__init__.py b/lib/ansible/__init__.py index d8aeac5e58..e4905a1853 100644 --- a/lib/ansible/__init__.py +++ b/lib/ansible/__init__.py @@ -19,6 +19,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +# make vendored top-level modules accessible EARLY +import ansible._vendor + # Note: Do not add any code to this file. The ansible module may be # a namespace package when using Ansible-2.1+ Anything in this file may not be # available if one of the other packages in the namespace is loaded first. diff --git a/lib/ansible/_vendor/__init__.py b/lib/ansible/_vendor/__init__.py new file mode 100644 index 0000000000..e6a4c56d6a --- /dev/null +++ b/lib/ansible/_vendor/__init__.py @@ -0,0 +1,46 @@ +# (c) 2020 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import pkgutil +import sys +import warnings + +# This package exists to host vendored top-level Python packages for downstream packaging. Any Python packages +# installed beneath this one will be masked from the Ansible loader, and available from the front of sys.path. +# It is expected that the vendored packages will be loaded very early, so a warning will be fired on import of +# the top-level ansible package if any packages beneath this are already loaded at that point. +# +# Python packages may be installed here during downstream packaging using something like: +# pip install --upgrade -t (path to this dir) cryptography pyyaml packaging jinja2 + +# mask vendored content below this package from being accessed as an ansible subpackage +__path__ = [] + + +def _ensure_vendored_path_entry(): + """ + Ensure that any downstream-bundled content beneath this package is available at the top of sys.path + """ + # patch our vendored dir onto sys.path + vendored_path_entry = os.path.dirname(__file__) + vendored_module_names = set(m[1] for m in pkgutil.iter_modules([vendored_path_entry], '')) # m[1] == m.name + + if vendored_module_names: + # patch us early to load vendored deps transparently + if vendored_path_entry in sys.path: + # handle reload case by removing the existing entry, wherever it might be + sys.path.remove(vendored_path_entry) + sys.path.insert(0, vendored_path_entry) + + already_loaded_vendored_modules = set(sys.modules.keys()).intersection(vendored_module_names) + + if already_loaded_vendored_modules: + warnings.warn('One or more Python packages bundled by this ansible-base distribution were already ' + 'loaded ({0}). This may result in undefined behavior.'.format(', '.join(sorted(already_loaded_vendored_modules)))) + + +_ensure_vendored_path_entry() |