From 5f5fe7eb2927c09557d1f280303f19f384f0aaa9 Mon Sep 17 00:00:00 2001 From: jbajic Date: Wed, 9 Nov 2022 10:39:29 +0100 Subject: [PATCH] Add python checks --- .github/workflows/diff.yaml | 31 ++++++++++++++++++++++++++++--- .pre-commit-config.yaml | 16 ++++++---------- init | 7 +++++++ pyproject.toml | 13 +++++++++++++ 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 pyproject.toml diff --git a/.github/workflows/diff.yaml b/.github/workflows/diff.yaml index ef5cf2ee2..e75cad88b 100644 --- a/.github/workflows/diff.yaml +++ b/.github/workflows/diff.yaml @@ -75,14 +75,39 @@ jobs: - name: Fetch all history for all tags and branches run: git fetch + - name: Initialize deps + run: | + # Activate toolchain. + source /opt/toolchain-v4/activate + # Initialize dependencies. + ./init + + - name: Set base branch + if: ${{ github.event_name == 'pull_request' }} + run: | + echo "BASE_BRANCH=origin/${{ github.base_ref }}" >> $GITHUB_ENV + + - name: Set base branch # if we manually dispatch or push to master + if: ${{ github.event_name != 'pull_request' }} + run: | + echo "BASE_BRANCH=origin/master" >> $GITHUB_ENV + + - name: Python code analysis + run: | + CHANGED_FILES=$(git diff -U0 ${{ env.BASE_BRANCH }}... --name-only) + for file in ${CHANGED_FILES}; do + echo ${file} + if [[ ${file} == *.py ]]; then + python3 -m black --check --diff ${file} + python3 -m isort --check-only --diff ${file} + fi + done + - name: Build combined ASAN, UBSAN and coverage binaries run: | # Activate toolchain. source /opt/toolchain-v4/activate - # Initialize dependencies. - ./init - cd build cmake -DTEST_COVERAGE=ON -DASAN=ON -DUBSAN=ON .. make -j$THREADS memgraph__unit diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 869991668..5abd746c9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,18 +6,14 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 22.10.0 hooks: - id: black - args: # arguments to configure black - - --line-length=120 - - --include='\.pyi?$' - # these folders wont be formatted by black - - --exclude="""\.git | - \.__pycache__| - build| - libs| - .cache""" + - repo: https://github.com/pycqa/isort + rev: 5.10.1 + hooks: + - id: isort + name: isort (python) - repo: https://github.com/pre-commit/mirrors-clang-format rev: v13.0.0 hooks: diff --git a/init b/init index 313855963..2c0eb4784 100755 --- a/init +++ b/init @@ -140,5 +140,12 @@ done; python3 -m pip install pre-commit python3 -m pre_commit install +# Install py format tools +echo "Install black formatter" +python3 -m pip install black==22.10.* +echo "Install isort" +python3 -m pip install isort==5.10.* + + # Link `include/mgp.py` with `release/mgp/mgp.py` ln -v -f include/mgp.py release/mgp/mgp.py diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..b4f64f523 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,13 @@ + +[tool.black] +line-length = 120 +include = '\.pyi?$' +extend-exclude = ''' +/( + | .git + | .__pycache__ + | build + | libs + | .cache +)/ +'''