Merge pull request #15656 from abadger/ziploader-namespace

Fix ziploader for the cornercase of ansible invoking ansible.
pull/15673/head
Toshio Kuratomi 9 years ago
commit 1b78fd57b1

@ -19,5 +19,10 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
__version__ = '2.2.0' # Note: Do not add any code to this file. The ansible module may be
__author__ = 'Ansible, Inc.' # 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.
#
# This is for backwards compat. Code should be ported to get these from
# ansible.release instead of from here.
from ansible.release import __version__, __author__

@ -30,7 +30,7 @@ import getpass
import signal import signal
import subprocess import subprocess
from ansible import __version__ from ansible.release import __version__
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleOptionsError from ansible.errors import AnsibleError, AnsibleOptionsError
from ansible.utils.unicode import to_bytes, to_unicode from ansible.utils.unicode import to_bytes, to_unicode

@ -30,7 +30,7 @@ import zipfile
from io import BytesIO from io import BytesIO
# from Ansible # from Ansible
from ansible import __version__ from ansible.release import __version__, __author__
from ansible import constants as C from ansible import constants as C
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.utils.unicode import to_bytes, to_unicode from ansible.utils.unicode import to_bytes, to_unicode
@ -533,6 +533,7 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
constants = dict( constants = dict(
SELINUX_SPECIAL_FS=C.DEFAULT_SELINUX_SPECIAL_FS, SELINUX_SPECIAL_FS=C.DEFAULT_SELINUX_SPECIAL_FS,
SYSLOG_FACILITY=_get_facility(task_vars), SYSLOG_FACILITY=_get_facility(task_vars),
ANSIBLE_VERSION=__version__,
) )
params = dict(ANSIBLE_MODULE_ARGS=module_args, params = dict(ANSIBLE_MODULE_ARGS=module_args,
ANSIBLE_MODULE_CONSTANTS=constants, ANSIBLE_MODULE_CONSTANTS=constants,
@ -562,8 +563,8 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
# Create the module zip data # Create the module zip data
zipoutput = BytesIO() zipoutput = BytesIO()
zf = zipfile.ZipFile(zipoutput, mode='w', compression=compression_method) zf = zipfile.ZipFile(zipoutput, mode='w', compression=compression_method)
zf.writestr('ansible/__init__.py', b''.join((b"__version__ = '", to_bytes(__version__), b"'\n"))) zf.writestr('ansible/__init__.py', b'from pkgutil import extend_path\n__path__=extend_path(__path__,__name__)\ntry:\n from ansible.release import __version__,__author__\nexcept ImportError:\n __version__="' + to_bytes(__version__) + b'"\n __author__="' + to_bytes(__author__) + b'"\n')
zf.writestr('ansible/module_utils/__init__.py', b'') zf.writestr('ansible/module_utils/__init__.py', b'from pkgutil import extend_path\n__path__=extend_path(__path__,__name__)\n')
zf.writestr('ansible_module_%s.py' % module_name, module_data) zf.writestr('ansible_module_%s.py' % module_name, module_data)

@ -15,3 +15,6 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Note: Do not add any code to this file. module_utils 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.

@ -219,10 +219,6 @@ except ImportError:
_literal_eval = literal_eval _literal_eval = literal_eval
from ansible import __version__
# Backwards compat. New code should just import and use __version__
ANSIBLE_VERSION = __version__
# Internal global holding passed in params and constants. This is consulted # Internal global holding passed in params and constants. This is consulted
# in case multiple AnsibleModules are created. Otherwise each AnsibleModule # in case multiple AnsibleModules are created. Otherwise each AnsibleModule
# would attempt to read from stdin. Other code should not use this directly # would attempt to read from stdin. Other code should not use this directly

@ -32,7 +32,6 @@ import os
import re import re
from uuid import UUID from uuid import UUID
from ansible import __version__
from ansible.module_utils.basic import BOOLEANS from ansible.module_utils.basic import BOOLEANS
FINAL_STATUSES = ('ACTIVE', 'ERROR') FINAL_STATUSES = ('ACTIVE', 'ERROR')
@ -264,7 +263,7 @@ def rax_required_together():
def setup_rax_module(module, rax_module, region_required=True): def setup_rax_module(module, rax_module, region_required=True):
"""Set up pyrax in a standard way for all modules""" """Set up pyrax in a standard way for all modules"""
rax_module.USER_AGENT = 'ansible/%s %s' % (__version__, rax_module.USER_AGENT = 'ansible/%s %s' % (module.constants['ANSIBLE_VERSION'],
rax_module.USER_AGENT) rax_module.USER_AGENT)
api_key = module.params.get('api_key') api_key = module.params.get('api_key')

@ -145,15 +145,15 @@ class PluginLoader:
def _get_package_paths(self): def _get_package_paths(self):
''' Gets the path of a Python package ''' ''' Gets the path of a Python package '''
paths = []
if not self.package: if not self.package:
return [] return []
if not hasattr(self, 'package_path'): if not hasattr(self, 'package_path'):
m = __import__(self.package) m = __import__(self.package)
parts = self.package.split('.')[1:] parts = self.package.split('.')[1:]
self.package_path = os.path.join(os.path.dirname(m.__file__), *parts) for parent_mod in parts:
paths.extend(self._all_directories(self.package_path)) m = getattr(m, parent_mod)
return paths self.package_path = os.path.dirname(m.__file__)
return self._all_directories(self.package_path)
def _get_paths(self): def _get_paths(self):
''' Return a list of paths to search for plugins in ''' ''' Return a list of paths to search for plugins in '''

@ -0,0 +1,23 @@
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
__version__ = '2.2.0'
__author__ = 'Ansible, Inc.'

@ -4,7 +4,7 @@ import os
import sys import sys
sys.path.insert(0, os.path.abspath('lib')) sys.path.insert(0, os.path.abspath('lib'))
from ansible import __version__, __author__ from ansible.release import __version__, __author__
try: try:
from setuptools import setup, find_packages from setuptools import setup, find_packages
except ImportError: except ImportError:

@ -33,7 +33,7 @@ try:
except ImportError: except ImportError:
import __builtin__ as builtins import __builtin__ as builtins
from ansible import __version__ as ansible_version from ansible.release import __version__ as ansible_version
from ansible import constants as C from ansible import constants as C
from ansible.compat.six import text_type from ansible.compat.six import text_type
from ansible.compat.tests import unittest from ansible.compat.tests import unittest

@ -53,11 +53,15 @@ class TestErrors(unittest.TestCase):
# python library, and then uses the __file__ attribute of # python library, and then uses the __file__ attribute of
# the result for that to get the library path, so we mock # the result for that to get the library path, so we mock
# that here and patch the builtin to use our mocked result # that here and patch the builtin to use our mocked result
m = MagicMock() foo = MagicMock()
m.return_value.__file__ = '/path/to/my/test.py' bar = MagicMock()
bam = MagicMock()
bam.__file__ = '/path/to/my/foo/bar/bam/__init__.py'
bar.bam = bam
foo.return_value.bar = bar
pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin') pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin')
with patch('{0}.__import__'.format(BUILTINS), m): with patch('{0}.__import__'.format(BUILTINS), foo):
self.assertEqual(pl._get_package_paths(), ['/path/to/my/bar/bam']) self.assertEqual(pl._get_package_paths(), ['/path/to/my/foo/bar/bam'])
def test_plugins__get_paths(self): def test_plugins__get_paths(self):
pl = PluginLoader('test', '', 'test', 'test_plugin') pl = PluginLoader('test', '', 'test', 'test_plugin')

Loading…
Cancel
Save