Skip to content
Snippets Groups Projects
Commit 2fc2125e authored by Hannes Feldt's avatar Hannes Feldt
Browse files

Add pre-commit hook and improve with better linting

parent 4bf827ef
No related branches found
No related tags found
2 merge requests!10Add pre commit improve tox setup,!9Draft: Add pre-commit hook and improve with better linting
Pipeline #109987 failed
Pipeline: Python Binary Wheel Package

#109988

    Showing
    with 112 additions and 61 deletions
    { {
    "project_name": "My Awesome App", "project_name": "My Awesome App",
    "project_slug": "{{ cookiecutter.project_name.lower()|replace(' ', '_')|replace('-', '_')|replace('.', '_')|trim() }}", "project_slug": "{{ cookiecutter.project_name.lower()|replace(' ', '_')|replace('-', '_')|replace('.', '_')|trim() }}",
    "project_package": "{{ cookiecutter.project_name.lower()|replace(' ', '-')|replace('_', '-')|replace('.', '-')|trim() }}",
    "project_url": "https://git.astron.nl/{{ cookiecutter.project_slug }}", "project_url": "https://git.astron.nl/{{ cookiecutter.project_slug }}",
    "description": "An example package for CI/CD working group" "description": "An example package for CI/CD working group"
    } }
    FROM python:3.11 FROM python:3.12
    RUN python -m pip install --upgrade pip RUN python -m pip install --upgrade pip
    RUN pip install --upgrade cookiecutter tox twine cibuildwheel==2.13.1 cookiecutter RUN python -m pip install --upgrade cookiecutter tox twine cibuildwheel==2.13.1 cookiecutter
    RUN curl -sSL https://get.docker.com/ | sh RUN curl -sSL https://get.docker.com/ | sh
    .tox
    build
    *.egg-info
    .venv
    dist/* dist/*
    *.egg-info *.egg-info
    *.pyc *.pyc
    .tox
    .venv
    .coverage .coverage
    coverage.xml coverage.xml
    htmlcov/* htmlcov/*
    build
    dist
    # Tox # Documentation
    .tox docs/source/source_documentation
    !docs/source/source_documentation/index.rst
    docs/build
    # setuptools-scm # setuptools-scm
    src/{{cookiecutter.project_slug}}/_version.py src/{{cookiecutter.project_slug}}/_version.py
    ......
    ...@@ -14,6 +14,7 @@ stages: ...@@ -14,6 +14,7 @@ stages:
    - lint - lint
    - test - test
    - package - package
    - images
    - integration - integration
    - publish # publish instead of deploy - publish # publish instead of deploy
    ...@@ -35,22 +36,10 @@ trigger_prepare: ...@@ -35,22 +36,10 @@ trigger_prepare:
    strategy: depend strategy: depend
    include: .prepare.gitlab-ci.yml include: .prepare.gitlab-ci.yml
    run_black: run_lint:
    stage: lint stage: lint
    script: script:
    - tox -e black - tox -e lint
    allow_failure: true
    run_flake8:
    stage: lint
    script:
    - tox -e pep8
    allow_failure: true
    run_pylint:
    stage: lint
    script:
    - tox -e pylint
    allow_failure: true allow_failure: true
    sast: sast:
    ...@@ -74,7 +63,7 @@ secret_detection: ...@@ -74,7 +63,7 @@ secret_detection:
    before_script: before_script:
    - python --version # For debugging - python --version # For debugging
    - python -m pip install --upgrade pip - python -m pip install --upgrade pip
    - pip install --upgrade tox twine - python -m pip install --upgrade tox twine
    # Run all unit tests for Python versions except the base image # Run all unit tests for Python versions except the base image
    run_unit_tests: run_unit_tests:
    ...@@ -156,8 +145,7 @@ publish_on_test_pypi: ...@@ -156,8 +145,7 @@ publish_on_test_pypi:
    - package_files - package_files
    when: manual when: manual
    rules: rules:
    - if: $CI_COMMIT_TAG - if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
    allow_failure: true
    script: script:
    - echo "run twine for test pypi" - echo "run twine for test pypi"
    # - | # - |
    ...@@ -175,8 +163,7 @@ publish_on_pypi: ...@@ -175,8 +163,7 @@ publish_on_pypi:
    - package_files - package_files
    when: manual when: manual
    rules: rules:
    - if: $CI_COMMIT_TAG - if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
    allow_failure: true
    script: script:
    - echo "run twine for pypi" - echo "run twine for pypi"
    # - | # - |
    ...@@ -195,7 +182,7 @@ publish_to_readthedocs: ...@@ -195,7 +182,7 @@ publish_to_readthedocs:
    - package_docs - package_docs
    when: manual when: manual
    rules: rules:
    - if: $CI_COMMIT_TAG - if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
    script: script:
    - echo "scp docs/* ???" - echo "scp docs/* ???"
    - exit 1 - exit 1
    ......
    default_stages: [ pre-commit, pre-push ]
    default_language_version:
    python: python3
    exclude: '^docs/.*\.py$'
    repos:
    - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
    - id: end-of-file-fixer
    - id: trailing-whitespace
    - id: check-yaml
    - id: check-toml
    - id: detect-private-key
    - repo: local
    hooks:
    - id: tox-lint
    name: tox-lint (local)
    entry: tox
    language: python
    types: [file, python]
    args: ["-e", "lint", "--"]
    #!/bin/bash
    if [ ! -f "setup.sh" ]; then
    echo "pre-commit.sh must be executed with repository root as working directory!"
    exit 1
    fi
    pre-commit install --hook-type pre-push
    FROM python:3.13 FROM python:3.13
    RUN python -m pip install --upgrade pip RUN python -m pip install --upgrade pip
    RUN pip install --upgrade tox twine cibuildwheel==2.13.1 RUN python -m pip install --upgrade tox twine cibuildwheel==2.13.1
    RUN curl -sSL https://get.docker.com/ | sh RUN curl -sSL https://get.docker.com/ | sh
    ARG BUILD_ENV=no_copy
    FROM python:3.13 AS build_no_copy
    ADD ../../requirements.txt .
    COPY ../.. /work
    RUN rm -r /work/dist | true
    RUN python -m pip install --user tox
    WORKDIR /work
    RUN python -m tox -e build
    FROM python:3.13 AS build_copy
    COPY dist /work/dist
    FROM build_${BUILD_ENV} AS build
    FROM python:3.13-slim
    COPY --from=build /work/dist /dist
    RUN python -m pip install /dist/*.whl
    #!/usr/bin/env python3
    # Copyright (C) 2023 ASTRON (Netherlands Institute for Radio Astronomy)
    # SPDX-License-Identifier: Apache-2.0
    import os
    file_dir = os.path.dirname(os.path.realpath(__file__))
    clean_dir = os.path.join(file_dir, "source", "source_documentation")
    print(f"Cleaning.. {clean_dir}/*")
    if not os.path.exists(clean_dir):
    exit()
    for file_name in os.listdir(clean_dir):
    file = os.path.join(clean_dir, file_name)
    if file_name == "index.rst":
    continue
    print(f"Removing.. {file}")
    os.remove(file)
    #!/bin/sh
    FILE_DIR=$(dirname -- "$(readlink -f -- "${0}")")
    echo "Cleaning.. ${FILE_DIR}/source/source_documentation/*"
    for f in "${FILE_DIR}"/source/source_documentation/*
    do
    case $f in
    */index.rst) true;;
    *) echo "Removing.. ${f}"; rm "${f}";;
    esac
    done
    ...@@ -46,20 +46,19 @@ archs = ["x86_64", "i686"] ...@@ -46,20 +46,19 @@ archs = ["x86_64", "i686"]
    [tool.cibuildwheel.windows] [tool.cibuildwheel.windows]
    archs = ["AMD64", "x86"] archs = ["AMD64", "x86"]
    [tool.pylint]
    ignore = "_version.py"
    # This does nothing until the issue below is resolved: [tool.ruff]
    # https://github.com/PyCQA/flake8/issues/234 exclude = [
    [tool.flake8] ".venv",
    max-line-length = 88 ".git",
    extend-ignore = ["E203"] ".tox",
    exclude = ["_version.py"] "dist",
    "docs",
    "*lib/python*",
    "*egg",
    "_version.py"
    ]
    [tool.ruff.lint]
    ignore = ["E203"]
    [tool.black]
    line-length = 88
    extend-exclude = '''
    (
    _version.py
    )
    '''
    autopep8 >= 1.7.0 # MIT
    black >= 22.0.0 # MIT
    build >= 0.8.0 # MIT
    flake8 >= 5.0.0 # MIT
    pyproject-flake8 >= 5.0.4 # Unlicense
    pylint >= 2.15.0 # GPLv2
    pytest >= 7.0.0 # MIT pytest >= 7.0.0 # MIT
    pytest-cov >= 3.0.0 # MIT pytest-cov >= 3.0.0 # MIT
    # Copyright (C) 2025 ASTRON (Netherlands Institute for Radio Astronomy)
    # SPDX-License-Identifier: Apache-2.0
    """Testing of the Cool Module""" """Testing of the Cool Module"""
    from unittest import TestCase from unittest import TestCase
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment