| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2018, Ansible Project |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
""" |
| 19 |
This test checks whether the libraries we're bundling are out of date and need to be synced with |
| 20 |
a newer upstream release. |
| 21 |
""" |
| 22 |
|
| 23 |
from __future__ import annotations |
| 24 |
|
| 25 |
import fnmatch |
| 26 |
import json |
| 27 |
import re |
| 28 |
import sys |
| 29 |
from ansible.module_utils.compat.version import LooseVersion |
| 30 |
|
| 31 |
import packaging.specifiers |
| 32 |
|
| 33 |
from ansible.module_utils.urls import open_url |
| 34 |
|
| 35 |
BUNDLED_RE = re.compile(b'\\b_BUNDLED_METADATA\\b') |
| 36 |
|
| 37 |
|
| 38 |
def get_bundled_libs(paths): |
| 39 |
""" |
| 40 |
Return the set of known bundled libraries |
| 41 |
|
| 42 |
:arg paths: The paths which the test has been instructed to check |
| 43 |
:returns: The list of all files which we know to contain bundled libraries. If a bundled |
| 44 |
library consists of multiple files, this should be the file which has metadata included. |
| 45 |
""" |
| 46 |
bundled_libs = set() |
| 47 |
for filename in fnmatch.filter(paths, 'lib/ansible/compat/*/__init__.py'): |
| 48 |
bundled_libs.add(filename) |
| 49 |
|
| 50 |
bundled_libs.add('lib/ansible/module_utils/distro/__init__.py') |
| 51 |
bundled_libs.add('lib/ansible/module_utils/six/__init__.py') |
| 52 |
bundled_libs.add('lib/ansible/_internal/_wrapt.py') |
| 53 |
|
| 54 |
return bundled_libs |
| 55 |
|
| 56 |
|
| 57 |
def get_files_with_bundled_metadata(paths): |
| 58 |
""" |
| 59 |
Search for any files which have bundled metadata inside of them |
| 60 |
|
| 61 |
:arg paths: Iterable of filenames to search for metadata inside of |
| 62 |
:returns: A set of pathnames which contained metadata |
| 63 |
""" |
| 64 |
|
| 65 |
with_metadata = set() |
| 66 |
for path in paths: |
| 67 |
with open(path, 'rb') as f: |
| 68 |
body = f.read() |
| 69 |
|
| 70 |
if BUNDLED_RE.search(body): |
| 71 |
with_metadata.add(path) |
| 72 |
|
| 73 |
return with_metadata |
| 74 |
|
| 75 |
|
| 76 |
def get_bundled_metadata(filename): |
| 77 |
""" |
| 78 |
Retrieve the metadata about a bundled library from a python file |
| 79 |
|
| 80 |
:arg filename: The filename to look inside for the metadata |
| 81 |
:raises ValueError: If we're unable to extract metadata from the file |
| 82 |
:returns: The metadata from the python file |
| 83 |
""" |
| 84 |
with open(filename, 'r') as module: |
| 85 |
for line in module: |
| 86 |
if line.strip().startswith('# NOT_BUNDLED'): |
| 87 |
return None |
| 88 |
|
| 89 |
if line.strip().startswith('# CANT_UPDATE'): |
| 90 |
print(f'{filename} marked as CANT_UPDATE, so skipping. Manual check for CVEs required.') |
| 91 |
return None |
| 92 |
|
| 93 |
if line.strip().startswith('_BUNDLED_METADATA'): |
| 94 |
data = line[line.index('{') :].strip() |
| 95 |
break |
| 96 |
else: |
| 97 |
raise ValueError( |
| 98 |
'Unable to check bundled library for update. ' |
| 99 |
'Please add _BUNDLED_METADATA dictionary to the library file with information on pypi name and bundled version.' |
| 100 |
) |
| 101 |
metadata = json.loads(data) |
| 102 |
return metadata |
| 103 |
|
| 104 |
|
| 105 |
def get_latest_applicable_version(pypi_data, constraints=None): |
| 106 |
"""Get the latest pypi version of the package that we allow |
| 107 |
|
| 108 |
:arg pypi_data: Pypi information about the data as returned by |
| 109 |
``https://pypi.org/pypi/{pkg_name}/json`` |
| 110 |
:kwarg constraints: version constraints on what we're allowed to use as specified by |
| 111 |
the bundled metadata |
| 112 |
:returns: The most recent version on pypi that are allowed by ``constraints`` |
| 113 |
""" |
| 114 |
latest_version = "0" |
| 115 |
if constraints: |
| 116 |
version_specification = packaging.specifiers.SpecifierSet(constraints) |
| 117 |
for version in pypi_data['releases']: |
| 118 |
if version in version_specification: |
| 119 |
if LooseVersion(version) > LooseVersion(latest_version): |
| 120 |
latest_version = version |
| 121 |
else: |
| 122 |
latest_version = pypi_data['info']['version'] |
| 123 |
|
| 124 |
return latest_version |
| 125 |
|
| 126 |
|
| 127 |
def main(): |
| 128 |
"""Entrypoint to the script""" |
| 129 |
|
| 130 |
paths = sys.argv[1:] or sys.stdin.read().splitlines() |
| 131 |
|
| 132 |
bundled_libs = get_bundled_libs(paths) |
| 133 |
files_with_bundled_metadata = get_files_with_bundled_metadata(paths) |
| 134 |
|
| 135 |
for filename in files_with_bundled_metadata.difference(bundled_libs): |
| 136 |
if filename.startswith('test/support/'): |
| 137 |
continue # bundled support code does not need to be updated or tracked |
| 138 |
|
| 139 |
print(f'{filename}: ERROR: File contains _BUNDLED_METADATA but needs to be added to test/sanity/code-smell/update-bundled.py') |
| 140 |
|
| 141 |
for filename in bundled_libs: |
| 142 |
try: |
| 143 |
metadata = get_bundled_metadata(filename) |
| 144 |
except ValueError as e: |
| 145 |
print(f'{filename}: ERROR: {e}') |
| 146 |
continue |
| 147 |
except OSError as e: |
| 148 |
if e.errno == 2: |
| 149 |
print( |
| 150 |
f'{filename}: ERROR: {e}. ' |
| 151 |
'Perhaps the bundled library has been removed or moved and the bundled library test needs to be modified as well?' |
| 152 |
) |
| 153 |
|
| 154 |
if metadata is None: |
| 155 |
continue |
| 156 |
|
| 157 |
pypi_fh = open_url('https://pypi.org/pypi/{0}/json'.format(metadata['pypi_name'])) |
| 158 |
pypi_data = json.loads(pypi_fh.read().decode('utf-8')) |
| 159 |
|
| 160 |
constraints = metadata.get('version_constraints', None) |
| 161 |
latest_version = get_latest_applicable_version(pypi_data, constraints) |
| 162 |
|
| 163 |
if LooseVersion(metadata['version']) < LooseVersion(latest_version): |
| 164 |
name = metadata['pypi_name'] |
| 165 |
version = metadata['version'] |
| 166 |
url = f"https://pypi.org/pypi/{name}/json" |
| 167 |
|
| 168 |
print(f"{filename}: UPDATE {name} from {version} to {latest_version} {url}") |
| 169 |
|
| 170 |
|
| 171 |
if __name__ == '__main__': |
| 172 |
main() |