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/setup.cfg b/setup.cfg new file mode 100644 index 00000000000..a241f32f61c --- /dev/null +++ b/setup.cfg @@ -0,0 +1,51 @@ +[metadata] +name = Some pre commit hooks +version = 1.0.0+pre_commit +description = Some hooks for pre-commit. +long_description = file: README.md +long_description_content_type = text/markdown +url = https://github.com/sonic-net/sonic-mgmt +classifiers = + License :: OSI Approved :: MIT License + Programming Language :: Python :: 3 + Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: Implementation :: CPython + Programming Language :: Python :: Implementation :: PyPy + +[options] +packages = find: +package_dir = + =.hooks +install_requires = + ruamel.yaml>=0.15 + tomli>=1.1.0;python_version<"3.11" +python_requires = >=3.7 + +[options.packages.find] +where = .hooks +exclude = + spytest* + +[options.entry_points] +console_scripts = + check-conditional-mark-sort = pre_commit_hooks.check_conditional_mark_sort:main + +[bdist_wheel] +universal = True + +[coverage:run] +plugins = covdefaults + +[mypy] +check_untyped_defs = true +disallow_any_generics = true +disallow_incomplete_defs = true +disallow_untyped_defs = true +warn_redundant_casts = true +warn_unused_ignores = true + +[mypy-testing.*] +disallow_untyped_defs = false + +[mypy-tests.*] +disallow_untyped_defs = false diff --git a/setup.py b/setup.py new file mode 100644 index 00000000000..d07f48b7282 --- /dev/null +++ b/setup.py @@ -0,0 +1,5 @@ +# Local pre-commit hook setup +from __future__ import annotations + +from setuptools import setup +setup()