Skip to content

Commit

Permalink
Introducing ruff as linter and formatter (basic setup) (open-telemetr…
Browse files Browse the repository at this point in the history
  • Loading branch information
emdneto authored Oct 23, 2024
1 parent 9d06509 commit 0adc58b
Show file tree
Hide file tree
Showing 73 changed files with 269 additions and 532 deletions.
37 changes: 0 additions & 37 deletions .flake8

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/generate_workflows.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pathlib import Path

from generate_workflows_lib import (
generate_test_workflow,
generate_lint_workflow,
generate_misc_workflow
generate_misc_workflow,
generate_test_workflow,
)

tox_ini_path = Path(__file__).parent.parent.parent.joinpath("tox.ini")
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/generate_workflows_lib/hatch_build.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
from pathlib import Path

from hatchling.builders.hooks.plugin.interface import BuildHookInterface

class CustomBuildHook(BuildHookInterface):

class CustomBuildHook(BuildHookInterface):
def initialize(self, version, build_data):

with open(
Path(__file__).parent.parent.parent.parent.joinpath("tox.ini")
) as tox_ini_file_0:
with open(
Path(__file__).parent.joinpath("src/generate_workflows_lib/tox.ini"), "w"
Path(__file__).parent.joinpath(
"src/generate_workflows_lib/tox.ini"
),
"w",
) as tox_ini_file_1:
tox_ini_file_1.write(tox_ini_file_0.read())
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections import defaultdict
from pathlib import Path
from re import compile as re_compile

from jinja2 import Environment, FileSystemLoader
from pathlib import Path
from tox.config.cli.parse import get_options
from tox.session.state import State
from tox.config.sets import CoreConfigSet
from tox.config.source.tox_ini import ToxIni
from collections import defaultdict

from tox.session.state import State

_tox_test_env_regex = re_compile(
r"(?P<python_version>py\w+)-test-"
Expand All @@ -19,27 +19,23 @@


def get_tox_envs(tox_ini_path: Path) -> list:

tox_ini = ToxIni(tox_ini_path)

conf = State(get_options(), []).conf

tox_section = next(tox_ini.sections())

core_config_set = (
CoreConfigSet(conf, tox_section, tox_ini_path.parent, tox_ini_path)
core_config_set = CoreConfigSet(
conf, tox_section, tox_ini_path.parent, tox_ini_path
)

(
core_config_set.
loaders.
extend(
tox_ini.
get_loaders(
core_config_set.loaders.extend(
tox_ini.get_loaders(
tox_section,
base=[],
override_map=defaultdict(list, {}),
conf=core_config_set
conf=core_config_set,
)
)
)
Expand All @@ -48,11 +44,7 @@ def get_tox_envs(tox_ini_path: Path) -> list:


def get_test_job_datas(tox_envs: list, operating_systems: list) -> list:

os_alias = {
"ubuntu-latest": "Ubuntu",
"windows-latest": "Windows"
}
os_alias = {"ubuntu-latest": "Ubuntu", "windows-latest": "Windows"}

python_version_alias = {
"pypy3": "pypy-3.8",
Expand All @@ -67,17 +59,16 @@ def get_test_job_datas(tox_envs: list, operating_systems: list) -> list:

for operating_system in operating_systems:
for tox_env in tox_envs:

tox_test_env_match = _tox_test_env_regex.match(tox_env)

if tox_test_env_match is None:
continue

groups = tox_test_env_match.groupdict()

aliased_python_version = (
python_version_alias[groups["python_version"]]
)
aliased_python_version = python_version_alias[
groups["python_version"]
]
tox_env = tox_test_env_match.string

test_requirements = groups["test_requirements"]
Expand All @@ -99,20 +90,17 @@ def get_test_job_datas(tox_envs: list, operating_systems: list) -> list:
),
"python_version": aliased_python_version,
"tox_env": tox_env,
"os": operating_system
"os": operating_system,
}

)

return test_job_datas


def get_lint_job_datas(tox_envs: list) -> list:

lint_job_datas = []

for tox_env in tox_envs:

tox_lint_env_match = _tox_lint_env_regex.match(tox_env)

if tox_lint_env_match is None:
Expand All @@ -126,18 +114,15 @@ def get_lint_job_datas(tox_envs: list) -> list:
"ui_name": f"{tox_lint_env_match.groupdict()['name']}",
"tox_env": tox_env,
}

)

return lint_job_datas


def get_contrib_job_datas(tox_envs: list) -> list:

contrib_job_datas = []

for tox_env in tox_envs:

tox_contrib_env_match = _tox_contrib_env_regex.match(tox_env)

if tox_contrib_env_match is None:
Expand All @@ -157,30 +142,25 @@ def get_contrib_job_datas(tox_envs: list) -> list:

contrib_job_datas.append(
{
"ui_name": (
f"{groups['name']}"
f"{contrib_requirements}"
),
"ui_name": (f"{groups['name']}" f"{contrib_requirements}"),
"tox_env": tox_env,
}

)

return contrib_job_datas


def get_misc_job_datas(tox_envs: list) -> list:

misc_job_datas = []

_tox_benchmark_env_regex = re_compile(r"benchmark.+")

for tox_env in tox_envs:
if (
_tox_test_env_regex.match(tox_env) is not None or
_tox_lint_env_regex.match(tox_env) is not None or
_tox_contrib_env_regex.match(tox_env) is not None or
_tox_benchmark_env_regex.match(tox_env) is not None
_tox_test_env_regex.match(tox_env) is not None
or _tox_lint_env_regex.match(tox_env) is not None
or _tox_contrib_env_regex.match(tox_env) is not None
or _tox_benchmark_env_regex.match(tox_env) is not None
):
continue

Expand All @@ -192,76 +172,64 @@ def get_misc_job_datas(tox_envs: list) -> list:
def _generate_workflow(
job_datas: list, name: str, workflow_directory_path: Path
):

# Github seems to limit the amount of jobs in a workflow file, that is why
# they are split in groups of 250 per workflow file.
for file_number, job_datas in enumerate(
[
job_datas[index:index + 250]
job_datas[index : index + 250]
for index in range(0, len(job_datas), 250)
]
):

with open(
workflow_directory_path.joinpath(f"{name}_{file_number}.yml"),
"w"
workflow_directory_path.joinpath(f"{name}_{file_number}.yml"), "w"
) as test_yml_file:

test_yml_file.write(
Environment(
loader=FileSystemLoader(Path(__file__).parent)
).get_template(f"{name}.yml.j2").render(
job_datas=job_datas, file_number=file_number
)
Environment(loader=FileSystemLoader(Path(__file__).parent))
.get_template(f"{name}.yml.j2")
.render(job_datas=job_datas, file_number=file_number)
)
test_yml_file.write("\n")


def generate_test_workflow(
tox_ini_path: Path,
workflow_directory_path: Path,
*operating_systems
tox_ini_path: Path, workflow_directory_path: Path, *operating_systems
) -> None:

_generate_workflow(
get_test_job_datas(get_tox_envs(tox_ini_path), operating_systems),
"test",
workflow_directory_path
workflow_directory_path,
)


def generate_lint_workflow(
tox_ini_path: Path,
workflow_directory_path: Path,
) -> None:

_generate_workflow(
get_lint_job_datas(get_tox_envs(tox_ini_path)),
"lint",
workflow_directory_path
workflow_directory_path,
)


def generate_contrib_workflow(
workflow_directory_path: Path,
) -> None:

_generate_workflow(
get_contrib_job_datas(
get_tox_envs(Path(__file__).parent.joinpath("tox.ini"))
),
"contrib",
workflow_directory_path
workflow_directory_path,
)


def generate_misc_workflow(
tox_ini_path: Path,
workflow_directory_path: Path,
) -> None:

_generate_workflow(
get_misc_job_datas(get_tox_envs(tox_ini_path)),
"misc",
workflow_directory_path
workflow_directory_path,
)
18 changes: 18 additions & 0 deletions .github/workflows/misc_0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ jobs:

- name: Run tests
run: tox -e shellcheck

ruff:
name: ruff
runs-on: ubuntu-latest
steps:
- name: Checkout repo @ SHA - ${{ github.sha }}
uses: actions/checkout@v4

- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e ruff
19 changes: 0 additions & 19 deletions .isort.cfg

This file was deleted.

22 changes: 9 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.3.0
hooks:
- id: black
language_version: python3.12
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: '6.1.0'
hooks:
- id: flake8
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.6.9
hooks:
# Run the linter.
- id: ruff
args: ["--fix", "--show-fixes"]
# Run the formatter.
- id: ruff-format
Loading

0 comments on commit 0adc58b

Please sign in to comment.