Skip to content

Commit

Permalink
Add sphinx-lint
Browse files Browse the repository at this point in the history
- Adds sphinx-lint to archery as archer lint --docs
- Adds archery lint --docs to pre-commit hook
- Adds sphinx-lint to appropriate setup.py, conda lock, and requirements.txt
  • Loading branch information
amoeba committed Feb 10, 2024
1 parent 7dc2ead commit 8996f30
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ repos:
- dockerfile
entry: --entrypoint /bin/hadolint hadolint/hadolint:latest -
exclude: ^dev/.*$
- id: sphinx-lint
name: Sphinx Lint
entry: archery lint --docs
language: system
files: ^docs/

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions ci/conda_env_sphinx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pydata-sphinx-theme=0.14
sphinx-autobuild
sphinx-design
sphinx-copybutton
sphinx-lint
sphinxcontrib-jquery
sphinx==6.2
# Requirement for doctest-cython
Expand Down
6 changes: 4 additions & 2 deletions dev/archery/archery/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def build(ctx, src, build_dir, force, targets, **kwargs):
"Check all sources files for license texts via Apache RAT."),
LintCheck('r', "Lint R files."),
LintCheck('docker', "Lint Dockerfiles with hadolint."),
LintCheck('docs', "Lint docs with sphinx-lint."),
]


Expand All @@ -284,9 +285,10 @@ def decorate_lint_command(cmd):
help="Run IWYU on all C++ files if enabled")
@click.option("-a", "--all", is_flag=True, default=False,
help="Enable all checks.")
@click.argument("path", required=False)
@decorate_lint_command
@click.pass_context
def lint(ctx, src, fix, iwyu_all, **checks):
def lint(ctx, src, fix, iwyu_all, path, **checks):
if checks.pop('all'):
# "--all" is given => enable all non-selected checks
for k, v in checks.items():
Expand All @@ -296,7 +298,7 @@ def lint(ctx, src, fix, iwyu_all, **checks):
raise click.UsageError(
"Need to enable at least one lint check (try --help)")
try:
linter(src, fix, iwyu_all=iwyu_all, **checks)
linter(src, fix, iwyu_all=iwyu_all, path=path, **checks)
except LintValidationException:
sys.exit(1)

Expand Down
52 changes: 50 additions & 2 deletions dev/archery/archery/utils/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,55 @@ def docker_linter(src):
cwd=src.path))


def linter(src, fix=False, *, clang_format=False, cpplint=False,
class SphinxLint(Command):
def __init__(self, src, path=None, sphinx_lint_bin=None, disable=None, enable=None):
self.src = src
self.path = path
self.bin = default_bin(sphinx_lint_bin, "sphinx-lint")
self.disable = disable or "all"
self.enable = enable

def lint(self, *args, check=False):
docs_path = os.path.join(self.src.path, "docs")

args = []

if self.disable:
args.extend(["--disable", self.disable])

if self.enable:
args.extend(["--enable", self.enable])

if self.path is not None:
args.extend([self.path])
else:
args.extend([docs_path])

return self.run(*args, check=check)


def docs_linter(src, path=None):
"""Run sphinx-lint on docs."""
logger.info("Running docs linter (sphinx-lint)")

sphinx_lint = SphinxLint(
src,
path=path,
disable="all",
enable="trailing-whitespace,missing-final-newline"
)

if not sphinx_lint.available:
logger.error("sphinx-lint linter requested but sphinx-lint binary not found")
return

yield LintResult.from_cmd(sphinx_lint.lint())


def linter(src, fix=False, path=None, *, clang_format=False, cpplint=False,
clang_tidy=False, iwyu=False, iwyu_all=False,
python=False, numpydoc=False, cmake_format=False, rat=False,
r=False, docker=False):
r=False, docker=False, docs=False):
"""Run all linters."""
with tmpdir(prefix="arrow-lint-") as root:
build_dir = os.path.join(root, "cpp-build")
Expand Down Expand Up @@ -480,6 +525,9 @@ def linter(src, fix=False, *, clang_format=False, cpplint=False,
if docker:
results.extend(docker_linter(src))

if docs:
results.extend(docs_linter(src, path))

# Raise error if one linter failed, ensuring calling code can exit with
# non-zero.
for result in results:
Expand Down
2 changes: 1 addition & 1 deletion dev/archery/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
'integration': ['cffi'],
'integration-java': ['jpype1'],
'lint': ['numpydoc==1.1.0', 'autopep8', 'flake8==6.1.0', 'cython-lint',
'cmake_format==0.6.13'],
'cmake_format==0.6.13', 'sphinx-lint==0.9.1'],
'numpydoc': ['numpydoc==1.1.0'],
'release': ['pygithub', jinja_req, 'jira', 'semver', 'gitpython'],
}
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pydata-sphinx-theme~=0.14
sphinx-autobuild
sphinx-design
sphinx-copybutton
sphinx-lint
sphinx==6.2
pandas

0 comments on commit 8996f30

Please sign in to comment.