Skip to content

Commit 487e656

Browse files
committed
Fix ziploader for the cornercase of ansible invoking ansible.
* Make ziploader's ansible and ansible.module_utils libraries into namespace packages. * Move __version__ and __author__ from ansible/__init__ to ansible/release.py. This is because namespace packages only load one __init__.py. If that is not the __init__.py with the author and version info then those won't be available. * In ziplaoder, move the version ito ANSIBLE_CONSTANTS. * Change PluginLoader to properly construct the path to the plugins even when namespace packages are present.
1 parent 7af47a3 commit 487e656

File tree

11 files changed

+53
-22
lines changed

11 files changed

+53
-22
lines changed

lib/ansible/__init__.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
from __future__ import (absolute_import, division, print_function)
2020
__metaclass__ = type
2121

22-
__version__ = '2.2.0'
23-
__author__ = 'Ansible, Inc.'
22+
# Note: Do not add any code to this file. The ansible module may be
23+
# a namespace package when using Ansible-2.1+ Anything in this file may not be
24+
# available if one of the other packages in the namespace is loaded first.
25+
#
26+
# This is for backwards compat. Code should be ported to get these from
27+
# ansible.release instead of from here.
28+
from ansible.release import __version__, __author__

lib/ansible/cli/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import signal
3131
import subprocess
3232

33-
from ansible import __version__
33+
from ansible.release import __version__
3434
from ansible import constants as C
3535
from ansible.errors import AnsibleError, AnsibleOptionsError
3636
from ansible.utils.unicode import to_bytes, to_unicode

lib/ansible/executor/module_common.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from io import BytesIO
3131

3232
# from Ansible
33-
from ansible import __version__
33+
from ansible.release import __version__, __author__
3434
from ansible import constants as C
3535
from ansible.errors import AnsibleError
3636
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
533533
constants = dict(
534534
SELINUX_SPECIAL_FS=C.DEFAULT_SELINUX_SPECIAL_FS,
535535
SYSLOG_FACILITY=_get_facility(task_vars),
536+
ANSIBLE_VERSION=__version__,
536537
)
537538
params = dict(ANSIBLE_MODULE_ARGS=module_args,
538539
ANSIBLE_MODULE_CONSTANTS=constants,
@@ -562,8 +563,8 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta
562563
# Create the module zip data
563564
zipoutput = BytesIO()
564565
zf = zipfile.ZipFile(zipoutput, mode='w', compression=compression_method)
565-
zf.writestr('ansible/__init__.py', b''.join((b"__version__ = '", to_bytes(__version__), b"'\n")))
566-
zf.writestr('ansible/module_utils/__init__.py', b'')
566+
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')
567+
zf.writestr('ansible/module_utils/__init__.py', b'from pkgutil import extend_path\n__path__=extend_path(__path__,__name__)\n')
567568

568569
zf.writestr('ansible_module_%s.py' % module_name, module_data)
569570

lib/ansible/module_utils/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
1717

18+
# Note: Do not add any code to this file. module_utils may be a namespace
19+
# package when using Ansible-2.1+ Anything in this file may not be available
20+
# if one of the other packages in the namespace is loaded first.

lib/ansible/module_utils/basic.py

-4
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,6 @@ def _convert(node):
219219

220220
_literal_eval = literal_eval
221221

222-
from ansible import __version__
223-
# Backwards compat. New code should just import and use __version__
224-
ANSIBLE_VERSION = __version__
225-
226222
# Internal global holding passed in params and constants. This is consulted
227223
# in case multiple AnsibleModules are created. Otherwise each AnsibleModule
228224
# would attempt to read from stdin. Other code should not use this directly

lib/ansible/module_utils/rax.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import re
3333
from uuid import UUID
3434

35-
from ansible import __version__
3635
from ansible.module_utils.basic import BOOLEANS
3736

3837
FINAL_STATUSES = ('ACTIVE', 'ERROR')
@@ -264,7 +263,7 @@ def rax_required_together():
264263

265264
def setup_rax_module(module, rax_module, region_required=True):
266265
"""Set up pyrax in a standard way for all modules"""
267-
rax_module.USER_AGENT = 'ansible/%s %s' % (__version__,
266+
rax_module.USER_AGENT = 'ansible/%s %s' % (module.constants['ANSIBLE_VERSION'],
268267
rax_module.USER_AGENT)
269268

270269
api_key = module.params.get('api_key')

lib/ansible/plugins/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ def _all_directories(self, dir):
145145
def _get_package_paths(self):
146146
''' Gets the path of a Python package '''
147147

148-
paths = []
149148
if not self.package:
150149
return []
151150
if not hasattr(self, 'package_path'):
152151
m = __import__(self.package)
153152
parts = self.package.split('.')[1:]
154-
self.package_path = os.path.join(os.path.dirname(m.__file__), *parts)
155-
paths.extend(self._all_directories(self.package_path))
156-
return paths
153+
for parent_mod in parts:
154+
m = getattr(m, parent_mod)
155+
self.package_path = os.path.dirname(m.__file__)
156+
return self._all_directories(self.package_path)
157157

158158
def _get_paths(self):
159159
''' Return a list of paths to search for plugins in '''

lib/ansible/release.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# (c) 2012-2014, Michael DeHaan <[email protected]>
2+
#
3+
# This file is part of Ansible
4+
#
5+
# Ansible is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Ansible is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17+
18+
# Make coding more python3-ish
19+
from __future__ import (absolute_import, division, print_function)
20+
__metaclass__ = type
21+
22+
__version__ = '2.2.0'
23+
__author__ = 'Ansible, Inc.'

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55

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

test/units/plugins/action/test_action.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
except ImportError:
3434
import __builtin__ as builtins
3535

36-
from ansible import __version__ as ansible_version
36+
from ansible.release import __version__ as ansible_version
3737
from ansible import constants as C
3838
from ansible.compat.six import text_type
3939
from ansible.compat.tests import unittest

test/units/plugins/test_plugins.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ def test_plugins__get_package_paths_with_package(self):
5353
# python library, and then uses the __file__ attribute of
5454
# the result for that to get the library path, so we mock
5555
# that here and patch the builtin to use our mocked result
56-
m = MagicMock()
57-
m.return_value.__file__ = '/path/to/my/test.py'
56+
foo = MagicMock()
57+
bar = MagicMock()
58+
bam = MagicMock()
59+
bam.__file__ = '/path/to/my/foo/bar/bam/__init__.py'
60+
bar.bam = bam
61+
foo.return_value.bar = bar
5862
pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin')
59-
with patch('{0}.__import__'.format(BUILTINS), m):
60-
self.assertEqual(pl._get_package_paths(), ['/path/to/my/bar/bam'])
63+
with patch('{0}.__import__'.format(BUILTINS), foo):
64+
self.assertEqual(pl._get_package_paths(), ['/path/to/my/foo/bar/bam'])
6165

6266
def test_plugins__get_paths(self):
6367
pl = PluginLoader('test', '', 'test', 'test_plugin')

0 commit comments

Comments
 (0)