Skip to content

Commit

Permalink
[CI] Add a pre-commit trailing blanks linter via tox
Browse files Browse the repository at this point in the history
- add a pre-commit yaml configuration file with a basic profile to
  remove trailing blanks
- add a rule to exclude from its scope
  - patch files (generated from git diff)
    Trailing blanks are part of their syntax.
  - .conf files in the src folder
    Their trailing blanks removal make some tests fail during build.
  - jinja templates in docker folder
    Though trailing blank should not be part of the jinja2 syntax,
    these files use a known hack that requires trailing blanks.
    Their removal makes build fail.
- create a tox configuration to run/install/uninstall/autoupdate
  pre-commit in a python venv
- create a tox profile for the CI
  to run pre-commit only on files modified since this commit
- add a shell script to ease running pre-commit inside various CI

Issue sonic-net#15114

Signed-off-by: Guillaume Lambert <guillaume.lambert@orange.com>
  • Loading branch information
guillaumelambert committed May 24, 2023
1 parent a73d443 commit 1aebc16
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude: '\.patch$|^src/.*\.conf$|^files/.*\.j2$'
#TODO fix the few files in ./files and ./src folders whose clean-up makes build fail
28 changes: 28 additions & 0 deletions scripts/tox_entry_point.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

# Copyright © 2023 Orange
#
# 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.

cd $(dirname $0)
cd ..
export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get -qq -y update 2>&1 >/dev/null || \
apt-get -qq -y update 2>&1 >/dev/null
sudo -E apt-get -qq -y install git gzip python3-pip tox >&1 >/dev/null || \
apt-get -qq -y install git gzip python3-pip tox >&1 >/dev/null
# Run tests inside a python tox virtual environment
# the PBR_VERSION variable is normally not needed and is here to avoid troubles in some CI
# that raise an exception for versioning w/o sdsit tarball or access to an upstream git repo
export PBR_VERSION=5.10.0
tox
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[metadata]
name = sonic_linters
home_page = https://github.com/sonic-net/sonic-buildimage

[files]
packages = sonic_linters

9 changes: 9 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python

import setuptools

# PBR doc available at https://docs.openstack.org/pbr/latest/

setuptools.setup(
setup_requires=['pbr'],
pbr=True)
47 changes: 47 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[tox]
minversion = 3.7.0
envlist =
pre-commit-ci
skipsdist = true

[testenv]
passenv = http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
usedevelop = true
basepython = python3
deps =
setuptools>=7.0

[testenv:pre-commit-install]
basepython = python3
deps = pre-commit
commands =
pre-commit install
pre-commit install --hook-type commit-msg

[testenv:pre-commit-uninstall]
basepython = python3
deps = pre-commit
commands =
pre-commit uninstall
pre-commit uninstall --hook-type commit-msg

[testenv:pre-commit-autoupdate]
basepython = python3
deps = pre-commit
commands =
pre-commit autoupdate

[testenv:pre-commit]
basepython = python3
deps = pre-commit
passenv = HOME
commands =
pre-commit run --all-files

[testenv:pre-commit-ci]
basepython = python3
deps = pre-commit
passenv = HOME
commands =
pre-commit run --all-files --show-diff-on-failure --from-ref a73d443c1d02698d9f3e030947c469677798bd32 --to-ref HEAD

0 comments on commit 1aebc16

Please sign in to comment.