# Python CircleCI 2.1 configuration file
#
# Check https://circleci.com/docs/2.1/language-python/ for more details
#
version: 2.1

# Aliases to reuse
_defaults: &defaults
  docker:
    # CircleCI maintains a library of pre-built images
    # documented at https://circleci.com/developer/images/image/cimg/python
    - image: cimg/python:3.11.10
  working_directory: ~/repo


jobs:
  build:
    <<: *defaults
    steps:
      - checkout

      - run:
          name: check skip
          command: |
            export git_log=$(git log --max-count=1 --pretty=format:"%B" | tr "\n" " ")
            echo "Got commit message:"
            echo "${git_log}"
            if [[ -v CIRCLE_PULL_REQUEST ]] && \
               ([[ "$git_log" == *"[skip circle]"* ]] || \
                [[ "$git_log" == *"[circle skip]"* ]])
            then
              echo "Skip detected, exiting job ${CIRCLE_JOB} for PR ${CIRCLE_PULL_REQUEST}."
              circleci-agent step halt;
            fi
      - run:
          name: pull changes from merge
          command: |
            if [[ -v CI_PULL_REQUEST ]] ; then git pull --ff-only origin "refs/pull/${CI_PULL_REQUEST//*pull\//}/merge" ; fi

      - run:
          name: update submodules
          command: |
            git submodule update --init

      - run:
          name: install system dependencies
          command: |
            sudo apt-get update
            sudo apt-get install -y graphviz texlive-fonts-recommended texlive-latex-recommended \
              texlive-latex-extra latexmk texlive-xetex texlive-lang-chinese doxygen

      - run:
          name: build NumPy
          command: |
            python3.11 -m venv venv
            . venv/bin/activate
            pip install --progress-bar=off -r requirements/test_requirements.txt \
               -r requirements/build_requirements.txt \
               -r requirements/ci_requirements.txt
            # get newer, pre-release versions of critical packages
            pip install --progress-bar=off --pre -r requirements/doc_requirements.txt
            # then install numpy HEAD, which will override the version installed above
            spin build --with-scipy-openblas=64 -j 2

      - run:
          name: build devdocs w/ref warnings
          command: |
            . venv/bin/activate
            # Don't use -q, show warning summary"
            SPHINXOPTS="-W -n" spin docs
            if [[ $(find doc/build/html -type f | wc -l) -lt 1000 ]]; then
                echo "doc build failed: doc/build/html is empty"
                exit -1
            fi

      - run:
          name: build neps
          command: |
            . venv/bin/activate
            cd doc/neps
            SPHINXOPTS="-n" make -e html || echo "ignoring errors for now"

      - store_artifacts:
          path: doc/build/html/

      - store_artifacts:
          path: doc/neps/_build/html/
     #      destination: neps

      - run:
          name: check doctests
          command: |
            . venv/bin/activate
            spin check-docs -v
            spin check-tutorials -v
            # Currently, this does two checks not done by check-docs:
            #  - validates ReST blocks (via validate_rst_syntax)
            #  - checks that all of a module's `__all__` is reflected in the
            #    module-level docstring autosummary
            echo calling python3 tools/refguide_check.py -v
            python3 tools/refguide_check.py -v

      - persist_to_workspace:
          root: ~/repo
          paths:
            - doc/build/html
            - doc/neps/_build
            - tools/ci/push_docs_to_repo.py

  deploy:
    <<: *defaults
    steps:
      - checkout

      - attach_workspace:
          at: ~/repo

      - add_ssh_keys:
          fingerprints:
            - "45:d8:d1:d6:f7:53:47:c5:d0:9e:35:19:79:e7:ff:24"

      -  run:
          name: deploy devdocs
          command: |
            touch doc/build/html/.nojekyll

            ./tools/ci/push_docs_to_repo.py doc/build/html \
                --committer "numpy-circleci-bot" \
                --email "numpy-circleci-bot@nomail" \
                --message "Docs build of $CIRCLE_SHA1" \
                --count 5 \
                --force \
                git@github.com:numpy/devdocs.git

      - add_ssh_keys:
          fingerprints:
            - "df:8b:fb:34:2d:38:7d:49:fc:1b:e8:44:4f:bd:2c:0e"

      - run:
          name: select SSH key for neps repo
          command: |
            cat \<<\EOF > ~/.ssh/config
            Host github.com
              IdentitiesOnly yes
              IdentityFile /home/circleci/.ssh/id_rsa_df8bfb342d387d49fc1be8444fbd2c0e
            EOF

      -  run:
          name: deploy neps
          command: |
            touch doc/neps/_build/html/.nojekyll

            ./tools/ci/push_docs_to_repo.py doc/neps/_build/html \
                --committer "numpy-circleci-bot" \
                --email "numpy-circleci-bot@nomail" \
                --message "Docs build of $CIRCLE_SHA1" \
                --count 5 \
                --force \
                git@github.com:numpy/neps.git \

workflows:
  version: 2
  default:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: main
