Skip to content

Commit

Permalink
Modernize project structure.
Browse files Browse the repository at this point in the history
 * Adds pyproject.toml
 * Migrates setup.py to setup.cfg
 * Drops support for EOL Python versions
 * Moves code under `src/suitable`, tests to top-level directory `tests`
 * Revamps tox configuration (wip)
 * Adds basic github actions to run tests
 * Removes obsolete travis badge from docs.
 * Adds bugbear and bandit
 * Adds pre-commit hooks
  • Loading branch information
strfx committed Sep 4, 2023
1 parent 326fee7 commit 7975ee7
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 222 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ commit = True
tag = True
message = Release {new_version}

[bumpversion:file:setup.py]
[bumpversion:file:setup.cfg]

[bumpversion:file:docs/source/conf.py]

Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/python-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: tests (pull_request)

on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, '3.10']

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox without uploading coverage
run: tox
41 changes: 41 additions & 0 deletions .github/workflows/python-tox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: tests

on:
push:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, '3.10']

steps:
- uses: actions/checkout@v2

- name: Get branch name (merge)
if: github.event_name != 'pull_request'
shell: bash
run: |
echo "CODECOV_BRANCH=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" \
>> $GITHUB_ENV
- name: Get branch name (pull request)
if: github.event_name == 'pull_request'
shell: bash
run: |
echo "CODECOV_BRANCH=$(echo ${GITHUB_HEAD_REF} | tr / -)" \
>> $GITHUB_ENV
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox and upload coverage results
run: tox -- --codecov --codecov-token=${{ secrets.CODECOV_TOKEN }}
34 changes: 34 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude: '^(.bumpversion.cfg)$'
- id: mixed-line-ending
- id: check-merge-conflict
- id: check-ast
- id: debug-statements
- repo: https://github.com/Lucas-C/pre-commit-hooks-markup
rev: v1.0.1
hooks:
- id: rst-linter
files: '^[A-Z]+\.rst$'
- repo: https://github.com/seantis/pre-commit-hooks
rev: v1.0.1
hooks:
- id: nocheckin
exclude: .pre-commit-config.yaml
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
files: '^(src/.*|tests/.*)\.py$'
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: bandit
args: ["-c", "pyproject.toml", "--quiet"]
additional_dependencies: [ "bandit[toml]" ]

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
recursive-include suitable *
recursive-include src *
include *.rst
include LICENSE
global-exclude *.pyc
Expand Down
20 changes: 16 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The official way to use Ansible from Python is documented here:
Compatibility
-------------

* Python 2.7 and Python 3.5+.
* Python 3.8+
* Ansible 2.4+
* Mitogen 0.2.6+ (currently incompatible with Ansible 2.8)

Expand All @@ -40,9 +40,9 @@ Run Tests
Build Status
------------

.. image:: https://travis-ci.org/seantis/suitable.svg?branch=master
:target: https://travis-ci.org/seantis/suitable
:alt: Build status
.. image:: https://github.com/seantis/suitable/actions/workflows/python-tox.yaml/badge.svg
:target: https://github.com/seantis/suitable/actions
:alt: Tests

Test Coverage
-------------
Expand All @@ -60,6 +60,18 @@ Latest Release

Changelog
---------
Modernizes project structure [strfx]:

- Drops support for Python < 3.8

- Switches to `pyproject.toml`

- Moves code to source directory `src/`

- Sets up Github Actions

- Checks code with bugbear and bandit (including pre-commit hooks)

0.17.3 (2023-07-13)
~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import sys

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down Expand Up @@ -118,7 +118,7 @@
'github_user': 'seantis',
'github_repo': 'suitable',
'github_type': 'star',
'travis_button': True,
'travis_button': False,
'codecov_button': True
}
html_style = 'custom.css'
Expand Down
56 changes: 56 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
log_level = "INFO"
testpaths = ["tests"]

[tool.coverage.run]
branch = true
source = ["src"]

[tool.bandit]
exclude_dirs = ["tests"]
skips = ["B101"]

[tool.tox]
legacy_tox_ini = """
[tox]
envlist = py38,py39,py310,flake8,bandit,report
[testenv]
usedevelop = true
setenv =
py{38,39,310}: COVERAGE_FILE = .coverage.{envname}
deps =
-e{toxinidir}[tests]
commands = pytest --cov --cov-report= {posargs}
passenv = *
[testenv:flake8]
basepython = python3.10
skip_install = true
deps =
flake8
flake8-bugbear
commands = flake8 src/ tests/
[testenv:bandit]
basepython = python3.10
skip_install = true
deps =
bandit[toml]
commands = bandit -q -c pyproject.toml -r src
[testenv:report]
deps =
coverage
skip_install = true
commands =
coverage combine
coverage report -m
"""
55 changes: 54 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
[metadata]
name = suitable
version = 0.17.3
url = http://github.com/seantis/suitable/
author = Denis Krienbühl
author_email = [email protected]
maintainer = Seantis GmbH
maintainer_email = [email protected]
description = Suitable is a thin wrapper around the Ansible API.
long_description = file: README.rst
long_description_content_type = text/x-rst
license = GPLv3
license_files = LICENSE
classifiers =
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Operating System :: OS Independent
Programming Language :: Python
Topic :: Software Development :: Libraries :: Python Modules


[options]
zip_safe = False
include_package_data = True
package_dir =
= src
packages =
suitable
python_requires = >= 3.8
platforms = any
install_requires =
ansible>=2.8.0.0
ansible-core<2.14

[options.extras_require]
dev =
bandit[toml]
flake8
flake8-bugbear
pre-commit
tox
tests =
mitogen>=0.2.8
paramiko
port-for
pytest
pytest-codecov[git]

[flake8]
extend-select = B901,B903,B904,B908
exclude=.venv,.git,.tox,dist,docs,*lib/python*,*egg,build

[bdist_wheel]
universal = 1
universal = 1
69 changes: 0 additions & 69 deletions setup.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 1 addition & 4 deletions suitable/inventory.py → src/suitable/inventory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from suitable.compat import string_types


class Inventory(dict):

def __init__(self, ansible_connection=None, hosts=None):
Expand Down Expand Up @@ -35,7 +32,7 @@ def add_host(self, server, host_variables):
self[server]['ansible_connection'] = 'local'

def add_hosts(self, servers):
if isinstance(servers, string_types):
if isinstance(servers, str):
for server in servers.split(u' '):
self.add_host(server, {})
elif isinstance(servers, dict):
Expand Down
6 changes: 4 additions & 2 deletions suitable/mitogen.py → src/suitable/mitogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def load_mitogen():

try:
import ansible_mitogen
except ImportError: # pragma: no cover
raise RuntimeError("Mitogen could not be found. Is it installed?")
except ImportError as err: # pragma: no cover
raise RuntimeError(
"Mitogen could not be found. Is it installed?"
) from err

strategy_path = os.path.join(
os.path.dirname(ansible_mitogen.__file__),
Expand Down
Loading

0 comments on commit 7975ee7

Please sign in to comment.