Skip to content

Commit

Permalink
Embed ansible_compat in linter
Browse files Browse the repository at this point in the history
This change ease maintenance and downstream packaging of linter by
reducing the dependency chain and avoiding surprises from untested
library versions.
  • Loading branch information
ssbarnea committed Feb 8, 2023
1 parent db1c847 commit b996e26
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 7 deletions.
1 change: 0 additions & 1 deletion .config/requirements-lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
# pip-compile --no-annotate --output-file=.config/requirements-lock.txt --resolver=backtracking --strip-extras --unsafe-package=ruamel-yaml-clib pyproject.toml
#
ansible-compat==3.0.1
ansible-core==2.14.1 ; python_version >= "3.9"
attrs==22.2.0
black==22.12.0
Expand Down
2 changes: 1 addition & 1 deletion .config/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
will-not-work-on-windows-try-from-wsl-instead; platform_system=='Windows'
ansible-core>=2.12.0,<2.14.0; python_version<'3.9' # GPLv3
ansible-core>=2.12.0; python_version>='3.9' # GPLv3
ansible-compat>=3.0.1 # GPLv3
# alphabetically sorted:
black>=22.8.0 # MIT
filelock>=3.3.0 # The Unlicense
Expand All @@ -11,5 +10,6 @@ packaging>=21.3 # Apache-2.0,BSD-2-Clause
pyyaml>=5.4.1 # MIT (centos 9 has 5.3.1)
rich>=12.0.0 # MIT
ruamel.yaml>=0.17.21,<0.18 # MIT, next version is planned to have breaking changes
subprocess-tee>=0.4.1 # MIT, used by ansible-compat
yamllint >= 1.26.3 # GPLv3
wcmatch>=8.1.2 # MIT
1 change: 0 additions & 1 deletion .config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
# pip-compile --extra=docs --extra=test --no-annotate --output-file=.config/requirements.txt --resolver=backtracking --strip-extras --unsafe-package=ansible-core --unsafe-package=ruamel-yaml-clib pyproject.toml
#
ansible-compat==3.0.1
astroid==2.14.1
attrs==22.2.0
black==22.12.0
Expand Down
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extend-exclude =
env,
# Metadata of `pip wheel` cmd is autogenerated
pip-wheel-metadata,
# vendored code
src/ansiblelint/_vendor/*,

# Let's not over-complicate the code:
max-complexity = 10
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0 # needed by setuptools-scm
submodules: true
- name: Build dists
run: python -m tox
- name: Publish to pypi.org
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # needed by setuptools-scm
submodules: true

- name: Set pre-commit cache
uses: actions/cache@v3
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule ".projects/ansible-compat"]
path = .projects/ansible-compat
url = https://github.com/ansible/ansible-compat
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ci:
# https://github.com/pre-commit-ci/issues/issues/55
- pip-compile
- schemas
submodules: true
exclude: >
(?x)^(
.config/requirements.*|
Expand Down Expand Up @@ -159,14 +160,15 @@ repos:
# empty args needed in order to match mypy cli behavior
args: [--strict]
additional_dependencies:
- ansible-compat>=3.0.1
- ansible-compat
- black>=22.10.0
- cryptography>=39.0.1
- filelock
- jinja2
- pytest>=7.2.0
- rich>=13.2.0
- ruamel.yaml
- subprocess-tee
- types-PyYAML
- types-jsonschema>=4.4.2
- types-pkg_resources
Expand All @@ -184,7 +186,7 @@ repos:
args:
- --output-format=colorized
additional_dependencies:
- ansible-compat>=3.0.1
- ansible-compat
- ansible-core>=2.14.0
- black>=22.10.0
- docutils
Expand Down
1 change: 1 addition & 0 deletions .projects/ansible-compat
Submodule ansible-compat added at fb52b7
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ source = ["src", ".tox/*/site-packages"]

[tool.coverage.report]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]
omit = ["*/src/ansible_compat/*", "test/*"]
omit = ["*/src/*/_vendor/*", "test/*"]
# Increase it just so it would pass on any single-python run
fail_under = 88
skip_covered = true
Expand Down Expand Up @@ -123,7 +123,7 @@ preferred-modules = ["py:pathlib", "unittest:pytest"]
# pylint defaults + f,fh,v,id
good-names = ["i", "j", "k", "ex", "Run", "_", "f", "fh", "v", "id", "T"]
# Ignore as being generated:
ignore-paths = "^src/ansiblelint/_version.*$"
ignore-paths = "^src/ansiblelint/(_version|_vendor).*$"

[tool.pylint."MESSAGES CONTROL"]
# increase from default is 50 which is too aggressive
Expand Down Expand Up @@ -190,13 +190,15 @@ norecursedirs = [
"collections",
"dist",
"docs",
"src/ansiblelint/_vendor",
"src/ansible_lint.egg-info",
".cache",
".eggs",
".git",
".github",
".tox",
"*.egg",
".projects",
]
python_files = [
"test_*.py",
Expand Down
4 changes: 4 additions & 0 deletions src/ansiblelint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"""Main ansible-lint package."""
from __future__ import annotations

import ansiblelint._vendor
from ansiblelint.version import __version__

# make vendored top-level modules accessible EARLY


__all__ = ("__version__",)
48 changes: 48 additions & 0 deletions src/ansiblelint/_vendor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import pkgutil
import sys
import warnings

# This package exists to host vendored top-level Python packages for downstream packaging. Any Python packages
# installed beneath this one will be masked from the Ansible loader, and available from the front of sys.path.
# It is expected that the vendored packages will be loaded very early, so a warning will be fired on import of
# the top-level ansible package if any packages beneath this are already loaded at that point.
#
# Python packages may be installed here during downstream packaging using something like:
# pip install --upgrade -t (path to this dir) cryptography pyyaml packaging jinja2

# mask vendored content below this package from being accessed as a subpackage
__path__ = []


def _ensure_vendored_path_entry() -> None:
"""
Ensure that any downstream-bundled content beneath this package is available at the top of sys.path
"""
# patch our vendored dir onto sys.path
vendored_path_entry = os.path.dirname(__file__)
vendored_module_names = {
m[1] for m in pkgutil.iter_modules([vendored_path_entry], "")
} # m[1] == m.name

if vendored_module_names:
# patch us early to load vendored deps transparently
if vendored_path_entry in sys.path:
# handle reload case by removing the existing entry, wherever it might be
sys.path.remove(vendored_path_entry)
sys.path.insert(0, vendored_path_entry)

already_loaded_vendored_modules = set(sys.modules.keys()).intersection(
vendored_module_names
)

if already_loaded_vendored_modules:
warnings.warn(
"One or more Python packages bundled by this ansible-lint distribution were already "
"loaded ({}). This may result in undefined behavior.".format(
", ".join(sorted(already_loaded_vendored_modules))
)
)


_ensure_vendored_path_entry()
1 change: 1 addition & 0 deletions src/ansiblelint/_vendor/ansible_compat

0 comments on commit b996e26

Please sign in to comment.