diff --git a/.hooks/README.md b/.hooks/README.md deleted file mode 100644 index d9f58a1b6ce..00000000000 --- a/.hooks/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## How to use pre-commit hooks to check the order in the yaml files -We keep the test cases in alphabetical order in the yaml files, and we use pre-commit hook to check if the test cases in correct order. -In order to use git hook, we should specify the hook folder using`git config core.hooksPath 'path'` before commit. We config this path using `init_hooks.sh` under the folder `sonic-mgmt`. - -Assume we use git under the `sonic-mgmt` folder, firstly we give the execute permission to init file using `chmod u+x init_hooks.sh` and then we can config hook path by executing the script `init_hooks.sh`. - -If we add a test case in one of the yaml files and break the alphabetical order, we can not commit successfully and will get the prompt `Pleace check the order in tests/common/plugins/conditional_mark/tests_mark_conditions*.yaml`. diff --git a/.hooks/pre-commit b/.hooks/pre-commit deleted file mode 100755 index 75cc711833a..00000000000 --- a/.hooks/pre-commit +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -stage_files=$(git diff --cached --name-only | grep "tests_mark_conditions*") -if [[ -z $stage_files ]];then - exit 0 -fi -chmod +x .hooks/pre-commit.py -python .hooks/pre-commit.py $stage_files -if [ $? != 0 ];then - echo "The entries in tests/common/plugins/conditional_mark/tests_mark_conditions*.yaml are not sorted in alphabetic order." - exit 1 -else - exit 0 -fi diff --git a/.hooks/pre-commit.py b/.hooks/pre-commit.py deleted file mode 100755 index ce4cdb1d306..00000000000 --- a/.hooks/pre-commit.py +++ /dev/null @@ -1,24 +0,0 @@ -import re -import sys - - -def main(): - stage_files = sys.argv[1:] - for stage_file in stage_files: - conditions = [] - with open(stage_file, 'r') as f: - file_contents = f.readlines() - if not file_contents: - continue - for line in file_contents: - if re.match('^[a-zA-Z]', line): - conditions.append(line.strip().rstrip(":")) - sorted_conditions = conditions[:] - sorted_conditions.sort() - if conditions != sorted_conditions: - sys.exit(-1) - sys.exit(0) - - -if __name__ == "__main__": - main() diff --git a/.hooks/pre_commit_hooks/__init__.py b/.hooks/pre_commit_hooks/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/.hooks/pre_commit_hooks/check_conditional_mark_sort.py b/.hooks/pre_commit_hooks/check_conditional_mark_sort.py new file mode 100755 index 00000000000..fac50d255f1 --- /dev/null +++ b/.hooks/pre_commit_hooks/check_conditional_mark_sort.py @@ -0,0 +1,28 @@ +import re +import sys + + +def main(): + stage_files = sys.argv[1:] + retval = 0 + for stage_file in stage_files: + if "tests_mark_conditions" in stage_file: + conditions = [] + with open(stage_file, 'r') as f: + file_contents = f.readlines() + if not file_contents: + continue + for line in file_contents: + if re.match('^[a-zA-Z]', line): + conditions.append(line.strip().rstrip(":")) + sorted_conditions = conditions[:] + sorted_conditions.sort() + if conditions != sorted_conditions: + print("The entries in tests/common/plugins/conditional_mark/tests_mark_conditions*.yaml " + "are not sorted in alphabetic order.") + retval = 1 + return retval + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a082ad13ee8..b6ef1d058b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,3 +15,8 @@ repos: hooks: - id: flake8 args: ["--max-line-length=120"] + +- repo: https://github.com/sonic-net/sonic-mgmt + rev: 1.0.0+pre_commit + hooks: + - id: check-conditional-mark-sort diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000000..7bee82287d8 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +- id: check-conditional-mark-sort + name: check conditional mark sort + description: check conditional mark yaml file sort from A to Z. + entry: check-conditional-mark-sort + language: python + types: [yaml] diff --git a/init_hooks.sh b/init_hooks.sh deleted file mode 100644 index ee7e3bbe1c8..00000000000 --- a/init_hooks.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -git config core.hooksPath .hooks -chmod +x .hooks/pre-commit diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000000..2124601599d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,32 @@ +# Local pre-commit hook config +[project] +name = "sonic-mgmt-pre-commit-hooks" +version = "1.0.0+pre_commit" +description = "Some hooks for pre-commit in sonic-mgmt repo." +classifiers = [ + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +requires-python = ">=3.7" +dependencies = ["tomli>=2.0.1;python_version<'4.0'"] + +[project.urls] +Homepage = "https://github.com/sonic-net/sonic-mgmt" + +[tool.setuptools] +package-dir = {"" = ".hooks"} + +[tool.setuptools.packages.find] +where = [".hooks"] + +[project.scripts] +check-conditional-mark-sort = 'pre_commit_hooks.check_conditional_mark_sort:main' + +[tool.black] +exclude = ''' +/( + spytest/ +)/ +'''