Skip to content

Commit

Permalink
Merge pull request #381 from tisnik/pr-check-for-python
Browse files Browse the repository at this point in the history
PR check for Python sources
  • Loading branch information
tisnik authored Dec 8, 2023
2 parents f4f6cb7 + 674b94d commit 83b550b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 21 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/pr_approval.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Pytest

on:
- push
- pull_request

jobs:
pytest:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
- "3.11"
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: pip install --upgrade setuptools
- run: pip install --upgrade wheel
- run: pip install pycodestyle
- run: pip install pydocstyle
- name: Style checks
run: make style
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.PHONY: style code-style

style: code-style docs-style shellcheck ## Perform all style checks

code-style: ## Check code style for all Python sources from this repository
python3 tools/run_pycodestyle.py

ruff: ## Run Ruff linter
ruff .

docs-style: ## Check documentation strings in all Python sources from this repository
pydocstyle .

shellcheck: ## Run shellcheck
./shellcheck.sh

help: ## Show this help screen
@echo 'Usage: make <OPTIONS> ... <TARGETS>'
@echo ''
@echo 'Available targets are:'
@echo ''
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-35s\033[0m %s\n", $$1, $$2}'
@echo ''
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
max-line-length = 100

[pydocstyle]
ignore = D301, D212, D203
ignore = D301, D211, D212, D213, D203

[metadata]
name = aggregator-utils
Expand Down
23 changes: 23 additions & 0 deletions shellcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
# Copyright 2020 Red Hat, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if ! command -v shellcheck > /dev/null 2>&1; then
scversion="stable" # or "v0.4.7", or "latest"
wget -qO- "https://github.com/koalaman/shellcheck/releases/download/${scversion?}/shellcheck-${scversion?}.linux.x86_64.tar.xz" | tar -xJv
shellcheck-stable/shellcheck --version
shellcheck-stable/shellcheck --exclude=SC2230 -- *.sh
else
shellcheck --exclude=SC2230 -- *.sh
fi
56 changes: 56 additions & 0 deletions tools/run_pycodestyle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3

# Copyright © 2020, 2021, 2022 Pavel Tisnovsky
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Simple checker of all Python sources in the given directory (usually repository)."""

from pathlib import Path
from sys import exit
import pycodestyle


def main():
"""Run pydocstyle checker against all Python sources in the given directory."""
# Find all files in current directory and subdirectories with '*.py' extension.
# Files are found recursivelly in all subdirectories as well.
files = list(Path(".").rglob("*.py"))
files = [f for f in files if not str(f).startswith("venv/")]
print("Files to check:")
print("\n".join(str(f) for f in files))
print("\nChecks:")

# Setup the module to check style of Python sources. We (usually) already
# have global configuration file 'setup.cfg' that can be used. Also verbose
# mode would be useful for our purposes, so we set `quiet` to `False` to
# enable verbose output.
style = pycodestyle.StyleGuide(quiet=False, config_file="setup.cfg")

# Check the style for all Python sources found in current directory and all
# subdirectories too. All detected issues are displayed in the meantime.
result = style.check_files(files)

# Print number of errors found at the end of check.
print("Total errors:", result.total_errors)

# If any error is found, return with exit code check to non-zero value.
if result.total_errors > 0:
exit(1)
# Default exit code is 0 == success


# If this script is started from command line, run the `main` function which
# represents entry point to the processing.
if __name__ == "__main__":
main()

0 comments on commit 83b550b

Please sign in to comment.