mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add PS dependency analysis to ansible-test.
This commit is contained in:
parent
3456bba631
commit
07bb7684b0
5 changed files with 108 additions and 8 deletions
|
@ -3,8 +3,8 @@
|
|||
# Copyright (c) 2017 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy.psm1
|
||||
#Requires -Module Ansible.ModuleUtils.SID.psm1
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||
#Requires -Module Ansible.ModuleUtils.SID
|
||||
|
||||
$params = Parse-Args -arguments $args -supports_check_mode $true
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# (c) 2017, Andrew Saraceni <andrew.saraceni@gmail.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy.psm1
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||
|
||||
# Test module used to grab the latest entry from an event log and output its properties
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!powershell
|
||||
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy.psm1
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||
|
||||
# basic script to get the lsit of users in a particular right
|
||||
# this is quite complex to put as a simple script so this is
|
||||
|
|
|
@ -23,6 +23,10 @@ from lib.import_analysis import (
|
|||
get_python_module_utils_imports,
|
||||
)
|
||||
|
||||
from lib.powershell_import_analysis import (
|
||||
get_powershell_module_utils_imports,
|
||||
)
|
||||
|
||||
from lib.config import (
|
||||
TestConfig,
|
||||
IntegrationConfig,
|
||||
|
@ -122,6 +126,7 @@ class PathMapper(object):
|
|||
self.compile_targets = list(walk_compile_targets())
|
||||
self.units_targets = list(walk_units_targets())
|
||||
self.sanity_targets = list(walk_sanity_targets())
|
||||
self.powershell_targets = [t for t in self.sanity_targets if os.path.splitext(t.path)[1] == '.ps1']
|
||||
|
||||
self.compile_paths = set(t.path for t in self.compile_targets)
|
||||
self.units_modules = set(t.module for t in self.units_targets if t.module)
|
||||
|
@ -143,6 +148,7 @@ class PathMapper(object):
|
|||
self.integration_dependencies = analyze_integration_target_dependencies(self.integration_targets)
|
||||
|
||||
self.python_module_utils_imports = {} # populated on first use to reduce overhead when not needed
|
||||
self.powershell_module_utils_imports = {} # populated on first use to reduce overhead when not needed
|
||||
|
||||
def get_dependent_paths(self, path):
|
||||
"""
|
||||
|
@ -155,6 +161,9 @@ class PathMapper(object):
|
|||
if ext == '.py':
|
||||
return self.get_python_module_utils_usage(path)
|
||||
|
||||
if ext == '.psm1':
|
||||
return self.get_powershell_module_utils_usage(path)
|
||||
|
||||
if path.startswith('test/integration/targets/'):
|
||||
return self.get_integration_target_usage(path)
|
||||
|
||||
|
@ -182,6 +191,22 @@ class PathMapper(object):
|
|||
|
||||
return sorted(self.python_module_utils_imports[name])
|
||||
|
||||
def get_powershell_module_utils_usage(self, path):
|
||||
"""
|
||||
:type path: str
|
||||
:rtype: list[str]
|
||||
"""
|
||||
if not self.powershell_module_utils_imports:
|
||||
display.info('Analyzing powershell module_utils imports...')
|
||||
before = time.time()
|
||||
self.powershell_module_utils_imports = get_powershell_module_utils_imports(self.powershell_targets)
|
||||
after = time.time()
|
||||
display.info('Processed %d powershell module_utils in %d second(s).' % (len(self.powershell_module_utils_imports), after - before))
|
||||
|
||||
name = os.path.splitext(os.path.basename(path))[0]
|
||||
|
||||
return sorted(self.powershell_module_utils_imports[name])
|
||||
|
||||
def get_integration_target_usage(self, path):
|
||||
"""
|
||||
:type path: str
|
||||
|
@ -263,10 +288,8 @@ class PathMapper(object):
|
|||
return minimal
|
||||
|
||||
if path.startswith('lib/ansible/module_utils/'):
|
||||
if ext in ('.ps1', '.psm1'):
|
||||
return {
|
||||
'windows-integration': self.integration_all_target,
|
||||
}
|
||||
if ext == '.psm1':
|
||||
return minimal # already expanded using get_dependent_paths
|
||||
|
||||
if ext == '.py':
|
||||
return minimal # already expanded using get_dependent_paths
|
||||
|
|
77
test/runner/lib/powershell_import_analysis.py
Normal file
77
test/runner/lib/powershell_import_analysis.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
"""Analyze powershell import statements."""
|
||||
|
||||
from __future__ import absolute_import, print_function
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
from lib.util import (
|
||||
display,
|
||||
)
|
||||
|
||||
|
||||
def get_powershell_module_utils_imports(powershell_targets):
|
||||
"""Return a dictionary of module_utils names mapped to sets of powershell file paths.
|
||||
:type powershell_targets: list[TestTarget]
|
||||
:rtype: dict[str, set[str]]
|
||||
"""
|
||||
|
||||
module_utils = enumerate_module_utils()
|
||||
|
||||
imports_by_target_path = {}
|
||||
|
||||
for target in powershell_targets:
|
||||
imports_by_target_path[target.path] = extract_powershell_module_utils_imports(target.path, module_utils)
|
||||
|
||||
imports = dict([(module_util, set()) for module_util in module_utils])
|
||||
|
||||
for target_path in imports_by_target_path:
|
||||
for module_util in imports_by_target_path[target_path]:
|
||||
imports[module_util].add(target_path)
|
||||
|
||||
for module_util in sorted(imports):
|
||||
if not imports[module_util]:
|
||||
display.warning('No imports found which use the "%s" module_util.' % module_util)
|
||||
|
||||
return imports
|
||||
|
||||
|
||||
def enumerate_module_utils():
|
||||
"""Return a list of available module_utils imports.
|
||||
:rtype: set[str]
|
||||
"""
|
||||
return set(os.path.splitext(p)[0] for p in os.listdir('lib/ansible/module_utils/powershell') if os.path.splitext(p)[1] == '.psm1')
|
||||
|
||||
|
||||
def extract_powershell_module_utils_imports(path, module_utils):
|
||||
"""Return a list of module_utils imports found in the specified source file.
|
||||
:type path: str
|
||||
:type module_utils: set[str]
|
||||
:rtype: set[str]
|
||||
"""
|
||||
imports = set()
|
||||
|
||||
with open(path, 'r') as module_fd:
|
||||
code = module_fd.read()
|
||||
|
||||
if '# POWERSHELL_COMMON' in code:
|
||||
imports.add('Ansible.ModuleUtils.Legacy')
|
||||
|
||||
lines = code.splitlines()
|
||||
line_number = 0
|
||||
|
||||
for line in lines:
|
||||
line_number += 1
|
||||
match = re.search(r'(?i)^#\s*requires\s+-module(?:s?)\s*(Ansible\.ModuleUtils\..+)', line)
|
||||
|
||||
if not match:
|
||||
continue
|
||||
|
||||
import_name = match.group(1)
|
||||
|
||||
if import_name in module_utils:
|
||||
imports.add(import_name)
|
||||
else:
|
||||
display.warning('%s:%d Invalid module_utils import: %s' % (path, line_number, import_name))
|
||||
|
||||
return imports
|
Loading…
Reference in a new issue