Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit workflow #473

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
env:
SKIP: no-commit-to-branch
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: |
3.9
3.x
- uses: pre-commit/[email protected]
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.0
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
16 changes: 9 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Please don't include any private/sensitive information in your issue!

## Tools we use

- Python 3.6
- [pylint](https://www.pylint.org/)
- [black](https://github.com/psf/black)
- Python >= 3.9
- [pre-commit](https://pre-commit.com/)
- [ruff](https://docs.astral.sh/ruff/)
- [tox](https://tox.readthedocs.io/en/latest/)
- You may need to use [virtualenv](https://virtualenv.pypa.io/en/latest/) to
support Python 3.6
Expand Down Expand Up @@ -65,10 +65,12 @@ tox -e coverage

## Coding conventions

- Use [black](https://github.com/psf/black) code formatter. If you have tox
installed, run `tox -e black` to see what changes will be made. You can use
`tox -e black-format` to update the code formatting prior to committing.
- Pass pylint
- Formatting and linting are incorporated using [ruff](https://docs.astral.sh/ruff/).
- If you use [pre-commit](https://pre-commit.com/) the checks will run automatically when you commit some changes
- If you prefer to run the ckecks with pre-commit, use `pre-commit run -a` to run the pre-commit checks for you.
- If you'd like to see what's happening with the checks you can run the [linter](https://docs.astral.sh/ruff/linter/)
and [formatter](https://docs.astral.sh/ruff/formatter/) separately with `ruff check --diff` and `ruff format --diff`
- Checks need to pass pylint
- exceptions are possible, but you will need to make a good argument
- Use spaces not tabs for indentation
- This is open source software. Consider the people who will read your code,
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ podman:

.PHONY: lint
lint: tox
$(PYTHON) -m tox -e black,pylint
$(PYTHON) -m tox -e format,lint

.PHONY: tests
tests: tox
Expand Down
1 change: 0 additions & 1 deletion contrib/cirrus/build_podman.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ systemctl stop podman.socket || :
dnf erase podman -y
dnf copr enable rhcontainerbot/podman-next -y
dnf install podman -y

1 change: 0 additions & 1 deletion docs/source/_templates/apidoc/toc.rst_t
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
{% for docname in docnames %}
{{ docname }}
{%- endfor %}

6 changes: 3 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

# -- Project information -----------------------------------------------------

project = u'Podman Python SDK'
copyright = u'2021, Red Hat Inc'
author = u'Red Hat Inc'
project = 'Podman Python SDK'
copyright = '2021, Red Hat Inc'
author = 'Red Hat Inc'

# The full version, including alpha/beta/rc tags
version = '3.2.1.0'
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ Indices and tables

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
* :ref:`search`
1 change: 1 addition & 0 deletions podman/api/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,7 @@ class GenProto(Protocol[T]):
def meth(self) -> T:
...
"""

__slots__ = ()
_is_protocol = True

Expand Down
3 changes: 2 additions & 1 deletion podman/domain/volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def list(self, *_, **kwargs) -> List[Volume]:
return [self.prepare_model(i) for i in response.json()]

def prune(
self, filters: Optional[Dict[str, str]] = None # pylint: disable=unused-argument
self,
filters: Optional[Dict[str, str]] = None, # pylint: disable=unused-argument
) -> Dict[Literal["VolumesDeleted", "SpaceReclaimed"], Any]:
"""Delete unused volumes.

Expand Down
1 change: 0 additions & 1 deletion podman/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
## Coverage Reporting Framework

`coverage.py` see https://coverage.readthedocs.io/en/coverage-5.0.3/#quick-start

1 change: 1 addition & 0 deletions podman/tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.
#
"""Base integration test code"""

import logging
import os
import shutil
Expand Down
2 changes: 1 addition & 1 deletion podman/tests/integration/test_container_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_container_directory_volume_mount(self):
with self.subTest("Check bind mount"):
volumes = {
"/etc/hosts": dict(bind="/test_ro", mode='ro'),
"/etc/hosts": dict(bind="/test_rw", mode='rw'),
"/etc/hosts": dict(bind="/test_rw", mode='rw'), # noqa: F601
}
container = self.client.containers.create(
self.alpine_image, command=["cat", "/test_ro", "/test_rw"], volumes=volumes
Expand Down
1 change: 1 addition & 0 deletions podman/tests/integration/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.
#
"""Images integration tests."""

import io
import queue
import tarfile
Expand Down
1 change: 1 addition & 0 deletions podman/tests/integration/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.
#
"""Network integration tests."""

import os
import random
import unittest
Expand Down
5 changes: 2 additions & 3 deletions podman/tests/integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.
#
"""Integration Test Utils"""

import logging
import os
import shutil
Expand Down Expand Up @@ -97,9 +98,7 @@ def consume_lines(pipe, consume_fn):
def consume(line: str):
logger.debug(line.strip("\n") + f" refid={self.reference_id}")

self.proc = subprocess.Popen(
self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
) # pylint: disable=consider-using-with
self.proc = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # pylint: disable=consider-using-with
threading.Thread(target=consume_lines, args=[self.proc.stdout, consume]).start()

if not check_socket:
Expand Down
3 changes: 2 additions & 1 deletion podman/tests/unit/test_imagesmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,8 @@ def test_list_with_name_overrides_reference_filter(self, mock):

# The name parameter should override the reference filter
images = self.client.images.list(
name="fedora", filters={"reference": "ubuntu"} # This should be overridden
name="fedora",
filters={"reference": "ubuntu"}, # This should be overridden
)

self.assertEqual(len(images), 1)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ log_cli = true
log_cli_level = "DEBUG"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"

50 changes: 50 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

line-length = 100
[format]
exclude = [
".git",
".venv",
".history",
"build",
"dist",
"docs",
"hack",
]
quote-style = "preserve"
[lint]
select = [
# More stuff here https://docs.astral.sh/ruff/rules/
"F", # Pyflakes
"E", # Pycodestyle Error
"W", # Pycodestyle Warning
"N", # PEP8 Naming
# TODO "UP", # Pyupgrade
# TODO "ANN",
# TODO "S", # Bandit
# "B", # Bugbear
"A", # flake-8-builtins
"YTT", # flake-8-2020
"PLC", # Pylint Convention
"PLE", # Pylint Error
"PLW", # Pylint Warning
]
# Some checks should be enabled for code sanity disabled now
# to avoid changing too many lines
ignore = [
"F821", # TODO Undefined name
"F541", # TODO f-string is missing placeholders
"F401", # TODO Module imported but unused
"F841", # TODO Local variable is assigned to but never used
"E402", # TODO Module level import not at top of file
"E741", # TODO ambiguous variable name
"E722", # TODO do not use bare 'except'
"E501", # TODO line too long
"N818", # TODO Error Suffix in exception name
"N80", # TODO Invalid Name
"ANN10", # Missing type annotation
"PLW2901", # TODO Redefined Loop Name
]
[lint.per-file-ignores]
"podman/tests/*.py" = ["S"]
[lint.flake8-builtins]
builtins-ignorelist = ["copyright", "all"]
3 changes: 1 addition & 2 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Any changes should be copied into pyproject.toml
-r requirements.txt
black
ruff
coverage
fixtures
pylint
pytest
requests-mock >= 1.11.0
tox
22 changes: 13 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
minversion = 3.2.0
envlist = pylint,coverage,py39,py310,py311,py312,py313
envlist = coverage,py39,py310,py311,py312,py313
ignore_basepython_conflict = true

[testenv]
Expand All @@ -17,21 +17,25 @@ setenv =
[testenv:venv]
commands = {posargs}

[testenv:pylint]
depends = py310
basepython = python3.10
allowlist_externals = pylint
commands = pylint podman
[testenv:lint]
depends = ruff
allowlist_externals = ruff
commands = ruff check --diff

# TODO: add pylint as alias of lint for compatibility

[testenv:coverage]
commands =
coverage run -m pytest
coverage report -m --skip-covered --fail-under=80 --omit=podman/tests/* --omit=.tox/*

[testenv:black]
deps = black
[testenv:format]
deps = ruff
allowlist_externals = ruff
commands =
black --diff --check .
ruff format --diff

# TODO: add black as alias of format for compatibility

[testenv:black-format]
deps = black
Expand Down
Loading