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

Refactor tool to support external settings #9

Merged
merged 10 commits into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions .dockerignore
@@ -1,8 +1,15 @@
.git
.github
__pycache__
.*project
.pytest_cache
.vscode
docs/
/.settings/
/tests
bin
Dockerfile
docs/
include
lib
lib64
LICENSE.txt
Makefile
README.md
README.md
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Expand Up @@ -2,6 +2,8 @@ name: Release new Docker image

on:
push:
branches:
- 'main'
tags:
- 'v*.*.*'
pull_request:
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
@@ -0,0 +1,27 @@
name: Test codebase

on: push

jobs:

tests:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install requirements
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r tests/requirements.txt

- name: Run tests
run: |
python -m pytest tests/
8 changes: 7 additions & 1 deletion .gitignore
@@ -1,3 +1,9 @@
__pycache__
.*project
.pytest_cache
.vscode
/.settings/
.*project
bin
include
lib
lib64
19 changes: 15 additions & 4 deletions Dockerfile
@@ -1,14 +1,25 @@
FROM python:3.10-slim
FROM python:3.10-slim as base
FROM base as builder

RUN pip install -U pip wheel \
&& mkdir /app /wheelhouse

COPY requirements.txt /app/
COPY src /app/src

RUN cd /app/ && pip wheel -r requirements.txt --wheel-dir=/wheelhouse

FROM base

LABEL maintainer="Plone Community <dev@plone.org>" \
org.label-schema.name="code-quality" \
org.label-schema.description="Plone code quality tool" \
org.label-schema.vendor="Plone Foundation" \
org.label-schema.docker.cmd="docker run -rm -v "${PWD}":/github/workspace plone/code-quality check black src"
ericof marked this conversation as resolved.
Show resolved Hide resolved

COPY requirements.txt pyproject.toml docker-entrypoint.py ./

RUN pip install -U pip && pip install -r requirements.txt
COPY docker-entrypoint.py /
COPY --from=builder /wheelhouse /wheelhouse
RUN pip install --force-reinstall --no-index --no-deps /wheelhouse/*
ericof marked this conversation as resolved.
Show resolved Hide resolved

WORKDIR /github/workspace

Expand Down
29 changes: 27 additions & 2 deletions Makefile
@@ -1,6 +1,6 @@
IMAGE_NAME=plone/code-quality
DOCKERFILE=Dockerfile
CODEBASE=docker-entrypoint.py
CODEBASE=docker-entrypoint.py src/setup.py src/plone_code_analysis tests/fixtures/packages/ok tests/package tests/conftest.py
LINT=docker run --rm -v "${PWD}":/github/workspace "${IMAGE_NAME}:latest" check
FORMAT=docker run --rm -v "${PWD}":/github/workspace "${IMAGE_NAME}:latest" format
CURRENT_USER=$$(whoami)
Expand All @@ -11,6 +11,26 @@ CURRENT_USER=$$(whoami)
help: ## This help message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

bin/pip:
@echo "$(GREEN)==> Setup Virtual Env$(RESET)"
python3 -m venv .
bin/pip install -r requirements.txt

bin/pytest:
bin/pip install -r tests/requirements.txt

.PHONY: clean
clean: ## remove virtual environment
rm -fr bin include lib lib64

.PHONY: setup
setup: bin/pytest ## Create virtualenv and run pip install

.PHONY: test
test: bin/pytest ## Create virtualenv and run pip install
@echo "$(GREEN)==> Run tests $(RESET)"
bin/python -m pytest tests

.PHONY: build-image
build-image: ## Build Docker Image
@echo "Building $(IMAGE_NAME):latest"
Expand All @@ -23,8 +43,13 @@ lint: build-image ## Lint code with existing image
$(LINT) flake8 "${CODEBASE}"
$(LINT) isort "${CODEBASE}"

.PHONY: lint-all
lint-all: build-image ## Lint code with existing image using configurations from pyproject.toml
@echo "Linting ${CODEBASE} $(IMAGE_NAME):latest"
$(LINT)

.PHONY: format
format: build-image ## Format code with existing image
@echo "Formatting ${CODEBASE} $(IMAGE_NAME):latest"
$(FORMAT) "${CODEBASE}"
$(FORMAT)
sudo chown -R ${CURRENT_USER}: *
176 changes: 127 additions & 49 deletions README.md
Expand Up @@ -16,53 +16,51 @@

</div>

## Usage

First, go to the repository you want to check.
## Configuration

This tool looks for configuration in a `pyproject.toml`file in the root of the codebase being analysed.

### [black](https://black.readthedocs.io/en/stable/)
The default configuration values are:

Check **src** directory and **setup.py** file with **black**
```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check black src setup.py
```toml
[tool.plone-code-analysis]
checkers = ["black", "flake8", "isort", "pyroma", "zpretty"]
formatters = ["black", "isort", "zpretty"]
ericof marked this conversation as resolved.
Show resolved Hide resolved
paths = "."
ericof marked this conversation as resolved.
Show resolved Hide resolved
paths_pyroma = "."
paths_black = "."
paths_flake8 = "."
paths_isort = "."
paths_pyroma = "."
paths_zpretty = "."
```

### [flake8](https://flake8.pycqa.org/en/stable/)
If you want to change only the `paths`, you should add to your `pyproject.toml` the following settings:

Flake8 checks, using [flakeheaven](https://pypi.org/project/flakeheaven/) configuration format.

Current plugins in use:

* [flake8-blind-except](https://pypi.org/project/flake8-blind-except/)
* [flake8-debugger](https://pypi.org/project/flake8-debugger/)
* [flake8-print](https://pypi.org/project/flake8-print/)

Check **src** directory and **setup.py** file with **flake8**

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check flake8 src setup.py
```toml
[tool.plone-code-analysis]
paths = "src/ setup.py"
ericof marked this conversation as resolved.
Show resolved Hide resolved
```
Also, it is possible to change the paths used for individual tools:

### [isort](https://pycqa.github.io/isort/)

Check **src** directory and **setup.py** file with **isort**

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check isort src setup.py
```toml
[tool.plone-code-analysis]
paths_black = "src/ tests/ setup.py"
paths_flake8 = "src/ setup.py"
```

### [zpretty](https://pypi.org/project/zpretty/)
Or explicitly set `checkers` or `formatters` to be used:

Check **src** directory with **zpretty**

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check zpretty src

```toml
[tool.plone-code-analysis]
checkers = ["black", "flake8", "isort", "pyroma", ]
formatters = ["black", "isort",]
```
## Configuration

To configure black, flake8 (via flakeheaven) and isort, make sure you have a pyproject.toml in the root of the directory you are mounting.
### Tools configuration

To configure black, flake8 (via flakeheaven) and isort, also use the `pyproject.toml` file in the root of the directory you are mounting.

An example configuration, used by this image, follows:

Expand All @@ -71,22 +69,6 @@ An example configuration, used by this image, follows:
line-length = 88
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
)
'''

[tool.isort]
profile = "black"
wesleybl marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -105,6 +87,102 @@ max-complexity=25
pycodestyle = ["+*"]
pyflakes = ["+*"]
"flake8-*" = ["+*"]

[tool.plone-code-analysis]
paths = "docker-entrypoint.py src/setup.py src/plone_code_analysis tests/fixtures/packages/ok tests/package tests/conftest.py"
paths_pyroma = "src/ tests/fixtures/packages/ok"
```

## Usage

First, go to the repository you want to check or format.

### Run all Checks

Using the configuration available in `pyproject.toml`, run:

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check
```

### Check with [black](https://black.readthedocs.io/en/stable/)

Check with `pyproject.toml` settings:

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check black
```

Explicitly check **src** directory and **setup.py** file.

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check black src setup.py
```

### Check with [flake8](https://flake8.pycqa.org/en/stable/)

Flake8 checks, using [flakeheaven](https://pypi.org/project/flakeheaven/) configuration format.

Current plugins in use:

* [flake8-blind-except](https://pypi.org/project/flake8-blind-except/)
* [flake8-debugger](https://pypi.org/project/flake8-debugger/)
* [flake8-print](https://pypi.org/project/flake8-print/)

Check with `pyproject.toml` settings:

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check flake8
```

Explicitly check **src** directory and **setup.py** file.

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check flake8 src setup.py
```

### Check with [isort](https://pycqa.github.io/isort/)

Check with `pyproject.toml` settings:

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check isort
```

Explicitly check **src** directory and **setup.py** file.

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check isort src setup.py
```


### Check with [pyroma](https://pycqa.github.io/pyroma/)

Check with `pyproject.toml` settings:

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check pyroma
```

Explicitly check **src/mypackage** directory .

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check pyroma src/mypackage
```

### Check with [zpretty](https://pypi.org/project/zpretty/)

Check with `pyproject.toml` settings:

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check zpretty
```

Explicitly check **src** directory .

```bash
docker run --rm -v "${PWD}":/github/workspace plone/code-quality:latest check zpretty src

```

## Contribute
Expand Down