From 7732e6d1512075a474b65e112c25c1fabee26159 Mon Sep 17 00:00:00 2001 From: Xudong Hao Date: Tue, 28 May 2024 14:44:00 +0800 Subject: [PATCH 1/2] Enable github PR python pre-check for KVM Check the python code and cfg code in KVM, to align the code style with Avocado. Signed-off-by: Xudong Hao --- .github/scripts/cfg-lint-check.py | 43 +++++++++++++++++ .github/scripts/requirements-ci.txt | 3 ++ .github/workflows/pull_request.yml | 71 +++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100755 .github/scripts/cfg-lint-check.py create mode 100644 .github/scripts/requirements-ci.txt diff --git a/.github/scripts/cfg-lint-check.py b/.github/scripts/cfg-lint-check.py new file mode 100755 index 00000000..639a5a17 --- /dev/null +++ b/.github/scripts/cfg-lint-check.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import os +import re +import sys + + +def cfg_lint_check(): + print('Running cfg lint check...') + exit_code = 0 + for file in sys.argv[1:]: + status_code = 0 + blank_line = 0 + cfg_path = os.path.abspath(os.path.join(os.path.dirname(__file__), + os.pardir, file)) + with open(cfg_path, 'r') as f: + contents = f.read() + for num, line in enumerate(contents.splitlines(), 1): + # Only strip whitespaces, handle other blank characters below + stripped_line = line.lstrip(' ') + blank_line = (blank_line + 1 + if re.search(r'^\s*$', stripped_line) else 0) + if blank_line >= 2: + print(f'{file}:{num}: Too many blank lines') + status_code = 1 + if re.search(r'\s$', line): + print(f'{file}:{num}: Trailing whitespaces') + status_code = 1 + if re.search(r'^\s', stripped_line): + print(f'{file}:{num}: Wrong indent(Unexpected blank characters') + status_code = 1 + if (len(line) - len(stripped_line)) % 4: + print(f'{file}:{num}: Wrong indent(4x spaces mismatch)') + status_code = 1 + if not contents.endswith('\n'): + print(f'{file} Missing final newline') + status_code = 1 + exit_code = exit_code or status_code + sys.exit(exit_code) + + +if __name__ == '__main__': + cfg_lint_check() diff --git a/.github/scripts/requirements-ci.txt b/.github/scripts/requirements-ci.txt new file mode 100644 index 00000000..0f217f35 --- /dev/null +++ b/.github/scripts/requirements-ci.txt @@ -0,0 +1,3 @@ +pylint +inspektor==0.5.3 +autopep8 diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 2ce42cef..ff428012 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -51,3 +51,74 @@ jobs: run: sudo docker build -f BM/Dockerfile.build -t builder BM - name: Build Project run: sudo ./.github/scripts/build_check + python-style-check: + name: Python ${{ matrix.python-version }} + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: [3.6, 3.9] + fail-fast: false + + steps: + - name: Check out repository code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install sphinx + pip install -r ./.github/scripts/requirements-ci.txt + - name: Run inspekt + run: inspekt checkall ./KVM --disable-style E501,E265,W601,E402,E722,E741 --disable-lint=W,R,C,E0601,E1002,E1101,E1103,E1120,F0401,I0011,I1101 --enable-lint W0611,W1201 --no-license-check + - run: echo "This job's status is ${{ job.status }}." + cfg-lint-check: + name: Cfg lint + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + + steps: + - name: Check out repository code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Get changed files + id: cfg-files + uses: tj-actions/changed-files@v35 + with: + files: | + ./KVM/qemu/*.cfg + - name: Set matrix + id: set-matrix + run: echo matrix=$(python3 -c 'print("${{ steps.cfg-files.outputs.all_changed_files }}".split())') >> $GITHUB_OUTPUT + - name: Check cfg files lint + if: steps.cfg-files.outputs.any_changed == 'true' + run: | + ./.github/scripts/cfg-lint-check.py ${{ steps.cfg-files.outputs.all_changed_files }} + cartesian-syntax-check: + name: Cartesian syntax + runs-on: ubuntu-latest + needs: cfg-lint-check + if: ${{ needs.cfg-lint-check.outputs.matrix != '[]' }} + strategy: + matrix: + file: ${{ fromJson(needs.cfg-lint-check.outputs.matrix) }} + fail-fast: false + + steps: + - name: Check out repository code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Parse ${{ matrix.file }} into Cartesian configuration + env: + CFG_FILE: ${{ matrix.file }} + run: | + echo "Parse ${CFG_FILE} into Cartesian configuration" + sed -i '1s/^/variants:\n/' ${CFG_FILE} + curl -fsSL https://raw.githubusercontent.com/avocado-framework/avocado-vt/master/virttest/cartesian_config.py | python3 - -f ${CFG_FILE} From e51234e6d690b10eecfac660c8ad55a90b5f06f8 Mon Sep 17 00:00:00 2001 From: Xudong Hao Date: Tue, 28 May 2024 17:19:27 +0800 Subject: [PATCH 2/2] Remove python 3.6 from PR yaml file Signed-off-by: Xudong Hao --- .github/workflows/pull_request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ff428012..4a984215 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -57,7 +57,7 @@ jobs: strategy: matrix: - python-version: [3.6, 3.9] + python-version: [3.9] fail-fast: false steps: