# Python CircleCI 2.0 configuration file
# Check https://circleci.com/docs/2.0/language-python/ for more details
#
# References:
#     # how to setup multiple python versions
#     https://stackoverflow.com/questions/948354/default-behavior-of-git-push-without-a-branch-specified
#     https://github.com/adambrenecki/virtualfish/blob/aa3d6271bcb86ad27b6d24f96b5bd386d176f588/.circleci/config.yml
#
#     # Multiple files for a checksum
#     https://discuss.circleci.com/t/cant-checksum-multiple-files-with-slashes-in-the-file-path/20667/2
#     
#     # Auto Cancel Redundant Builds
#     https://circleci.com/docs/2.0/skip-build/#steps-to-enable-auto-cancel-for-pipelines-triggered-by-pushes-to-github-or-the-api
#     https://app.circleci.com/settings/project/github/pyutils/line_profiler/advanced?return-to=https%3A%2F%2Fapp.circleci.com%2Fpipelines%2Fgithub%2Fpyutils%2FPYPKG


# Note: Main CI is now in github actions

version: 2
workflows:
  version: 2
  test:
    jobs:
      - test_full/cp39-cp39-manylinux2010
      - test_full/cp38-cp38-manylinux2010
      - test_full/cp37-cp37m-manylinux2010
      - test_full/cp36-cp36m-manylinux2010


jobs:

  ###########
  # TEMPLATES
  ###########

  .common_template: &common_template
    docker:
      - image: cimg/python
    steps:
      - checkout


  .test_full_template: &test_full_template  
    <<: 
        - *common_template
    resource_class: small
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: prepare_env
          command: |
            python -m venv venv
            . venv/bin/activate
            pip install --upgrade pip
      - run:
          name: build_wheel
          command: |
            . venv/bin/activate
            VERSION=$(python -c "import setup; print(setup.VERSION)") 
            export CIBW_BUILD="${MB_PYTHON_TAG}-*"
            docker info
            pip install -r requirements/build.txt
            cibuildwheel --platform=linux --arch=native
      - persist_to_workspace:
          root: .
          paths:
              - wheelhouse
      - run:
          name: install_wheel
          command: |
            . venv/bin/activate
            VERSION=$(python -c "import setup; print(setup.VERSION)") 
            # Hack in the arch
            BDIST_WHEEL_PATH=$(ls wheelhouse/*-$VERSION-$MB_PYTHON_TAG*manylinux*_x86_64.whl)
            echo "BDIST_WHEEL_PATH=${BDIST_WHEEL_PATH}"
            pip install ${BDIST_WHEEL_PATH}[all]
      - run:
          name: run_tests
          command: |
            . venv/bin/activate
            python run_tests.py


  ###################################
  ### INHERIT FROM BASE TEMPLATES ###
  ###################################

  # Define tests fo the other python verisons using the "test3.6" template
  # and indicating what needs to be modified.
  #
  # All we need to do is change the base docker image so python is the
  # version we want we can reuse everything else from the template
  
  test_full/cp39-cp39-manylinux2010:
    <<: *test_full_template
    environment:
      - MB_PYTHON_TAG=cp39
    docker:
      - image: cimg/python:3.9
    working_directory: ~/repo-full-cp39
  
  test_full/cp38-cp38-manylinux2010:
    <<: *test_full_template
    environment:
      - MB_PYTHON_TAG=cp38
    docker:
      - image: cimg/python:3.8
    working_directory: ~/repo-full-cp38
  
  test_full/cp37-cp37m-manylinux2010:
    <<: *test_full_template
    environment:
      - MB_PYTHON_TAG=cp37
    docker:
      - image: cimg/python:3.7
    working_directory: ~/repo-full-cp37

  test_full/cp36-cp36m-manylinux2010:
    <<: *test_full_template
    environment:
      - MB_PYTHON_TAG=cp36
    docker:
      - image: cimg/python:3.6
    working_directory: ~/repo-full-cp36


__scratch_work__:
    docker:
      - image: pypy:3
    working_directory: ~/dev-only-not-a-real-job
    steps:
      - | 
        __doc__=" 
        # Run circleci scripts on a local machine
        # snap install circleci
        mkdir -p $HOME/Downloads
        curl -fLSs https://raw.githubusercontent.com/CircleCI-Public/circleci-cli/master/install.sh | DESTDIR=$HOME/Downloads bash

        circleci update
        circleci switch

        circleci config validate
        circleci local execute --job test_full/cp39-cp39-manylinux2010
        circleci local execute --config .circleci/config.yml --job test_full/cp38-cp38-manylinux2010
        circleci local execute --config .circleci/config.yml 

        circleci local execute --job test_full/cp38-cp38-manylinux2010
        "
