# Python package
# Create and test a Python package on multiple Python versions.
# Add steps that analyze code, save the dist with the build record, publish to a PyPI-compatible index, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

stages:

- stage: check_codestyle
  jobs:
    - job: pre_commit
      pool:
        vmImage: 'ubuntu-latest'

      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '3.13'
        - script: |
            python -m pip install --upgrade build pip setuptools wheel
          displayName: 'Install Python build tools and dependencies'
        - script: |
            python -m build
            python -m pip install '.[dev]'
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            pre-commit install ; pre-commit run --all-files
          displayName: 'Run pre-commit hooks'
        - script: |
            check-wheel-contents dist/*.whl
          displayName: 'Run check-wheel-contents'
        - script: |
            twine check dist/*
          displayName: "Run twine check"

- stage: build_documentation
  dependsOn: check_codestyle
  condition: succeededOrFailed()
  jobs:
    - job: build_documentation
      pool:
        vmImage: 'ubuntu-latest'

      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '3.13'
        - script: |
            python -m pip install --upgrade build pip setuptools wheel
          displayName: 'Install Python build tools'
        - script: |
            python -m build
            python -m pip install '.[doc]'
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            sudo apt-get update && sudo apt-get install -qq -y texlive-latex-extra latexmk
          displayName: 'Install TeX Live'
        - script: |
            cd doc ; make all
          displayName: 'Build the documentation'

- stage: tests_minimum_dependencies
  dependsOn: check_codestyle
  condition: succeededOrFailed()
  jobs:
    - job:
      pool:
        vmImage: 'ubuntu-latest'
      strategy:
        matrix:
          Python39:
            python.version: '3.9'

      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '$(python.version)'
          displayName: 'Use Python $(python.version)'
        - script: |
            sudo apt-get update && sudo apt-get install -yq --no-install-suggests --no-install-recommends \
            libatlas-base-dev liblapack-dev gfortran libgmp-dev libmpfr-dev libsuitesparse-dev ccache \
            swig libmpc-dev
          displayName: 'Install dependencies'
        - script: |
            python -m pip install --upgrade build pip wheel
            python -m pip install asteval==1.0 numpy==1.24.0 scipy==1.10.0 uncertainties==3.2.2
          displayName: 'Install minimum required version of dependencies'
        - script: |
            python -m build
            python -m pip install ".[test]"
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            python -m pip list
          displayName: 'List installed Python packages'
        - script: |
            python -m pip install pytest-azurepipelines
            pytest
          displayName: 'Run test-suite and collect coverage'
        - script: |
            curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --import # One-time step
            curl -Os https://uploader.codecov.io/latest/linux/codecov
            curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
            curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
            gpg --verify codecov.SHA256SUM.sig codecov.SHA256SUM
            shasum -a 256 -c codecov.SHA256SUM
            chmod +x codecov
          displayName: 'Download and verify codecov uploader'
        - script: |
            ./codecov -v -f "coverage.xml"
          displayName: 'Upload to codecov.io'

- stage: tests_latest_dependencies
  dependsOn: check_codestyle
  condition: succeededOrFailed()
  jobs:
    - job:
      pool:
        vmImage: 'ubuntu-latest'
      strategy:
        matrix:
          Python39:
            python.version: '3.9'
          Python310:
            python.version: '3.10'
          Python311:
            python.version: '3.11'
          Python312:
            python.version: '3.12'
          Python313:
            python.version: '3.13'

      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '$(python.version)'
          displayName: 'Use Python $(python.version)'
        - script: |
            sudo apt-get update && sudo apt-get install -yq --no-install-suggests --no-install-recommends \
            libatlas-base-dev liblapack-dev gfortran libgmp-dev libmpfr-dev libsuitesparse-dev ccache \
            swig libmpc-dev
          displayName: 'Install dependencies'
        - script: |
            python -m pip install --upgrade build pip setuptools wheel
          displayName: 'Install latest available version of Python dependencies'
        - script: |
            python -m build
            python -m pip install '.[all]'
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            python -m pip list
          displayName: 'List installed Python packages'
        - script: |
            python -m pip install pytest-azurepipelines
            pytest
          displayName: 'Run test-suite and collect coverage'
        - script: |
            curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --import # One-time step
            curl -Os https://uploader.codecov.io/latest/linux/codecov
            curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
            curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
            gpg --verify codecov.SHA256SUM.sig codecov.SHA256SUM
            shasum -a 256 -c codecov.SHA256SUM
            chmod +x codecov
          displayName: 'Download and verify codecov uploader'
        - script: |
            ./codecov -v -f "coverage.xml"
          displayName: 'Upload to codecov.io'

- stage: test_Windows_latest
  dependsOn: check_codestyle
  condition: succeededOrFailed()
  jobs:
    - job:
      pool:
        vmImage: 'windows-latest'
      strategy:
        matrix:
          Python313:
            python.version: '3.13'

      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '$(python.version)'
          displayName: 'Use Python $(python.version)'
        - script: |
            python -m pip install --upgrade build pip setuptools wheel
          displayName: 'Install latest available version of Python dependencies'
        - script: |
            python -m build
            python -m pip install .[all]
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            python -m pip list
          displayName: 'List installed Python packages'
        - script: |
            python -m pip install pytest-azurepipelines
            pytest
          displayName: 'Run test-suite'
        - powershell:
            cd doc ; .\make.bat html
          displayName: 'Build the HTML documentation'

- stage: test_macOS_latest
  dependsOn: check_codestyle
  condition: succeededOrFailed()
  jobs:
    - job:
      pool:
        vmImage: 'macos-latest'
      strategy:
        matrix:
          Python313:
            python.version: '3.13'

      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '$(python.version)'
          displayName: 'Use Python $(python.version)'
        - script: |
            python -m pip install --upgrade build pip setuptools wheel
          displayName: 'Install latest available version of Python dependencies'
        - script: |
            python -m build
            python -m pip install '.[all]'
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            python -m pip list
          displayName: 'List installed Python packages'
        - script: |
            python -m pip install pytest-azurepipelines
            pytest
          displayName: 'Run test-suite and collect coverage'

- stage: development_version
  dependsOn: check_codestyle
  condition: succeededOrFailed()
  jobs:
    - job: Python314_dev
      pool:
        vmImage: 'ubuntu-latest'
      steps:
        - script: |
            sudo add-apt-repository ppa:deadsnakes/nightly
            sudo apt-get update && sudo apt-get install -y --no-install-recommends python3.14-dev python3.14-venv
          displayName: Install Python development version from the deadsnakes PPA
        - script: |
            sudo apt-get update && sudo apt-get install -yq --no-install-suggests --no-install-recommends \
            libatlas-base-dev liblapack-dev gfortran libgmp-dev libmpfr-dev libsuitesparse-dev ccache \
            swig libmpc-dev
          displayName: 'Install dependencies'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            python3.14 -m ensurepip --upgrade
            pip3.14 install -U build pip setuptools wheel pybind11 cython || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Install build, pip, setuptools, wheel, pybind11, and cython'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            export numpy_version=2.2.1
            wget https://github.com/numpy/numpy/releases/download/v${numpy_version}/numpy-${numpy_version}.tar.gz
            tar xzvf numpy-${numpy_version}.tar.gz
            cd numpy-${numpy_version}
            python3.14 -m build || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
            python3.14 -m pip install . --user || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Install latest available version of NumPy'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            pip3.14 install -U pythran || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Install pythran'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            export scipy_version=1.14.1
            wget https://github.com/scipy/scipy/releases/download/v${scipy_version}/scipy-${scipy_version}.tar.gz
            tar xzvf scipy-${scipy_version}.tar.gz
            cd scipy-${scipy_version}
            python3.14 -m build || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
            python3.14 -m pip install . --user || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Install latest available version of SciPy'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            # remove numdifftools for now as it pulls in statsmodels, that wants to build with NumPy 1.14.5
            pip3.14 install asteval uncertainties dill emcee flaky pytest pytest-cov || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Install latest available version of Python dependencies'
        - script: |
            python3.14 -m build
            python3.14 -m pip install '.[test]' --user || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Build wheel/sdist and install lmfit'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            pip3.14 list || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'List installed Python packages'
        - script: |
            export PATH=/home/vsts/.local/bin:$PATH
            pip3.14 install pytest-azurepipelines
            cd $(Agent.BuildDirectory)/s/tests
            pytest || echo -e "\043#vso[task.logissue type=warning;] Allowed failure for development version!!"
          displayName: 'Run test-suite'
