@ -19,6 +19,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
__metaclass__ = type
import runpy
import runpy
import inspect
import json
import json
import os
import os
import subprocess
import subprocess
@ -27,13 +28,16 @@ import sys
from contextlib import contextmanager
from contextlib import contextmanager
from ansible . executor . powershell . module_manifest import PSModuleDepFinder
from ansible . executor . powershell . module_manifest import PSModuleDepFinder
from ansible . module_utils . basic import FILE_COMMON_ARGUMENTS
from ansible . module_utils . basic import FILE_COMMON_ARGUMENTS , AnsibleModule
from ansible . module_utils . six import reraise
from ansible . module_utils . six import reraise
from ansible . module_utils . _text import to_bytes , to_text
from ansible . module_utils . _text import to_bytes , to_text
from . utils import CaptureStd , find_executable , get_module_name_from_filename
from . utils import CaptureStd , find_executable , get_module_name_from_filename
ANSIBLE_MODULE_CONSTRUCTOR_ARGS = tuple ( list ( inspect . signature ( AnsibleModule . __init__ ) . parameters ) [ 1 : ] )
class AnsibleModuleCallError ( RuntimeError ) :
class AnsibleModuleCallError ( RuntimeError ) :
pass
pass
@ -53,7 +57,12 @@ class _FakeAnsibleModuleInit:
self . called = False
self . called = False
def __call__ ( self , * args , * * kwargs ) :
def __call__ ( self , * args , * * kwargs ) :
self . args = args
if args and isinstance ( args [ 0 ] , AnsibleModule ) :
# Make sure, due to creative calling, that we didn't end up with
# ``self`` in ``args``
self . args = args [ 1 : ]
else :
self . args = args
self . kwargs = kwargs
self . kwargs = kwargs
self . called = True
self . called = True
raise AnsibleModuleCallError ( ' AnsibleModuleCallError ' )
raise AnsibleModuleCallError ( ' AnsibleModuleCallError ' )
@ -126,7 +135,7 @@ def get_ps_argument_spec(filename, collection):
# the validate-modules code expects the options spec to be under the argument_spec key not options as set in PS
# the validate-modules code expects the options spec to be under the argument_spec key not options as set in PS
kwargs [ ' argument_spec ' ] = kwargs . pop ( ' options ' , { } )
kwargs [ ' argument_spec ' ] = kwargs . pop ( ' options ' , { } )
return kwargs [ ' argument_spec ' ] , ( ) , kwargs
return kwargs [ ' argument_spec ' ] , kwargs
def get_py_argument_spec ( filename , collection ) :
def get_py_argument_spec ( filename , collection ) :
@ -146,11 +155,11 @@ def get_py_argument_spec(filename, collection):
raise AnsibleModuleNotInitialized ( )
raise AnsibleModuleNotInitialized ( )
try :
try :
# Convert positional arguments to kwargs to make sure that all parameters are actually checked
for arg , arg_name in zip ( fake . args , ANSIBLE_MODULE_CONSTRUCTOR_ARGS ) :
fake . kwargs [ arg_name ] = arg
# for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
# for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
if ' argument_spec ' in fake . kwargs :
argument_spec = fake . kwargs . get ( ' argument_spec ' ) or { }
argument_spec = fake . kwargs [ ' argument_spec ' ]
else :
argument_spec = fake . args [ 0 ]
# If add_file_common_args is truish, add options from FILE_COMMON_ARGUMENTS when not present.
# If add_file_common_args is truish, add options from FILE_COMMON_ARGUMENTS when not present.
# This is the only modification to argument_spec done by AnsibleModule itself, and which is
# This is the only modification to argument_spec done by AnsibleModule itself, and which is
# not caught by setup_env's AnsibleModule replacement
# not caught by setup_env's AnsibleModule replacement
@ -158,9 +167,9 @@ def get_py_argument_spec(filename, collection):
for k , v in FILE_COMMON_ARGUMENTS . items ( ) :
for k , v in FILE_COMMON_ARGUMENTS . items ( ) :
if k not in argument_spec :
if k not in argument_spec :
argument_spec [ k ] = v
argument_spec [ k ] = v
return argument_spec , fake . args, fake . kwargs
return argument_spec , fake . kwargs
except ( TypeError , IndexError ) :
except ( TypeError , IndexError ) :
return { } , () , {}
return { } , {}
def get_argument_spec ( filename , collection ) :
def get_argument_spec ( filename , collection ) :