Skip to content

Commit

Permalink
chore: Use Ruff for linting and formatting (#147)
Browse files Browse the repository at this point in the history
* Use Ruff for linting and formatting.
   - Use Ruff in pre-commit over pyupgrade, black, and flake8.
   - Enable additional linting in pyproject.toml ruff tool section.
   - Remove .flake8 and black config section of pyproject.toml.
   - Use Ruff in lint workflow.
* Disable some recommended Ruff lints.
   - c.f. https://learn.scientific-python.org/development/guides/style/#ruff
* Apply ruff formatting to source code.
* Add Ruff formatting badge to README.
  • Loading branch information
matthewfeickert committed Jun 17, 2024
1 parent 880b592 commit 3517c21
Show file tree
Hide file tree
Showing 23 changed files with 264 additions and 214 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

8 changes: 2 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ jobs:
with:
python-version: '3.x'

- name: Lint with flake8
- name: Lint with Ruff
run: |
pipx run flake8 .
- name: Lint with Black
run: |
pipx run black --check --diff --verbose .
pipx run ruff check --diff --verbose .
22 changes: 8 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,15 @@ repos:
exclude: ^deploy/
- id: trailing-whitespace

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.9
hooks:
- id: pyupgrade
args: ["--py36-plus"]

- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black-jupyter

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
# Run the linter.
- id: ruff
args: ["--fix", "--show-fixes"]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/recast-hep/recast-atlas/main.svg)](https://results.pre-commit.ci/latest/github/recast-hep/recast-atlas/main)
[![PyPI version](https://badge.fury.io/py/recast-atlas.svg)](https://badge.fury.io/py/recast-atlas)

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

ATLAS tools to facilitate integration of ATLAS analyses into RECAST

## Getting Started
Expand Down
59 changes: 46 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ all = ["recast-atlas[local,kubernetes,reana]"]
# Developer extras
develop = [
"pytest>=7.0.0",
"flake8>=3.9.0",
"black",
"ruff",
"pre-commit",
]

Expand All @@ -101,14 +100,48 @@ only-include = [
[tool.hatch.build.targets.wheel]
packages = ["src/recastatlas"]

[tool.black]
line-length = 88
skip-string-normalization = true
include = '\.pyi?$'
exclude = '''
/(
\.git
| .eggs
| build
)/
'''
[tool.ruff]
src = ["src"]

[tool.ruff.lint]
extend-select = [
# "B", # flake8-bugbear
"I", # isort
# "ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
# "PT", # flake8-pytest-style
# "PTH", # flake8-use-pathlib
# "RET", # flake8-return
"RUF", # Ruff-specific
# "SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"FURB", # refurb
"PYI", # flake8-pyi
]
ignore = [
"PLR09", # Too many <...>
"PLR2004", # Magic value used in comparison
"ISC001", # Conflicts with formatter
# FIXME: Ingores are to cover over failing checks at Ruff adoption point
"C414",
"EM101",
"EM103",
"G004",
"PLW2901",
"T201",
]
typing-modules = ["mypackage._compat.typing"]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["T20"]
31 changes: 16 additions & 15 deletions src/recastatlas/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
from __future__ import annotations

import json
import logging
import subprocess
import os
import textwrap
import shlex
from .. import exceptions
import subprocess
import textwrap

from recastatlas.exceptions import BackendNotAvailableException

from .. import exceptions

log = logging.getLogger(__name__)

BACKENDS = {}
try:
from .reana import ReanaBackend

BACKENDS['reana'] = ReanaBackend()
BACKENDS["reana"] = ReanaBackend()
except (ImportError, exceptions.BackendNotAvailableException):
pass

try:
from .kubernetes import KubernetesBackend

BACKENDS['kubernetes'] = KubernetesBackend()
BACKENDS["kubernetes"] = KubernetesBackend()
except (ImportError, exceptions.BackendNotAvailableException):
pass

try:
from .local import LocalBackend

BACKENDS['local'] = LocalBackend()
BACKENDS["local"] = LocalBackend()
except (ImportError, exceptions.BackendNotAvailableException):
pass

try:
from .docker import DockerBackend
from .docker import setup_docker
from .docker import DockerBackend, setup_docker

BACKENDS['docker'] = DockerBackend()
BACKENDS["docker"] = DockerBackend()
except (ImportError, exceptions.BackendNotAvailableException):
pass

Expand All @@ -56,13 +59,11 @@ def get_shell_packtivity(name, spec, backend):
return subprocess.check_output(shlex.split(shellcmd)).decode("ascii")
if backend == "docker":
command, dockerconfig = setup_docker()
script = """\
script = f"""\
mkdir -p ~/.docker
echo '{dockerconfig}' > ~/.docker/config.json
{command}
""".format(
dockerconfig=json.dumps(dockerconfig), command=shellcmd
)
echo '{json.dumps(dockerconfig)}' > ~/.docker/config.json
{shellcmd}
"""
command += ["sh", "-c", textwrap.dedent(script)]
return subprocess.check_output(command).decode("ascii")

Expand Down
38 changes: 20 additions & 18 deletions src/recastatlas/backends/docker.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from ..config import config
from ..exceptions import FailedRunException
import logging
import os
from __future__ import annotations

import base64
import json
import textwrap
import logging
import os
import subprocess
import textwrap

import yaml

from ..config import config
from ..exceptions import FailedRunException

log = logging.getLogger(__name__)


Expand All @@ -17,11 +21,11 @@ def setup_docker():
image = config.backends[backend]["image"]

special_envs = [
'PACKTIVITY_CVMFS_LOCATION',
'PACKTIVITY_CVMFS_PROPAGATION',
'PACKTIVITY_AUTH_LOCATION',
'YADAGE_SCHEMA_LOAD_TOKEN',
'YADAGE_INIT_TOKEN',
"PACKTIVITY_CVMFS_LOCATION",
"PACKTIVITY_CVMFS_PROPAGATION",
"PACKTIVITY_AUTH_LOCATION",
"YADAGE_SCHEMA_LOAD_TOKEN",
"YADAGE_INIT_TOKEN",
]
assert special_envs

Expand Down Expand Up @@ -98,20 +102,18 @@ def setup_docker():

class DockerBackend:
def run_workflow(self, name, spec):
backend_config = config.backends['docker']["fromstring"]
backend_config = config.backends["docker"]["fromstring"]

spec["backend"] = spec.get('backend', backend_config)
spec["backend"] = spec.get("backend", backend_config)
command, dockerconfig = setup_docker()

script = """\
script = f"""\
mkdir -p ~/.docker
echo '{dockerconfig}' > ~/.docker/config.json
echo '{json.dumps(dockerconfig)}' > ~/.docker/config.json
cat << 'EOF' | yadage-run -f -
{spec}
{json.dumps(spec)}
EOF
""".format(
spec=json.dumps(spec), dockerconfig=json.dumps(dockerconfig)
)
"""
command += ["sh", "-c", textwrap.dedent(script)]
subprocess.check_call(command)

Expand Down
2 changes: 2 additions & 0 deletions src/recastatlas/backends/kubernetes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json


Expand Down
14 changes: 9 additions & 5 deletions src/recastatlas/backends/local.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from yadage.utils import setupbackend_fromstring
from __future__ import annotations

import logging
import os
import subprocess

from yadage.steering_api import run_workflow
from yadage.utils import setupbackend_fromstring

from ..config import config
from ..exceptions import FailedRunException
import logging
import subprocess
import os

log = logging.getLogger(__name__)


class LocalBackend:
def run_workflow(self, name, spec):
backend_config = config.backends['local']["fromstring"]
backend_config = config.backends["local"]["fromstring"]

spec["backend"] = setupbackend_fromstring(
backend_config, spec.pop("backendopts", {})
Expand Down
Loading

0 comments on commit 3517c21

Please sign in to comment.