---
- name: Build wheels of Python package {{ wheel_name }}  # noqa no-changed-when
  become: true
  become_user: "buildslave"
  become_method: su
  ansible.builtin.shell:
    cmd: |
      source {{ virtualenv_path }}/bin/activate
      pip3 wheel -w '{{ wheels_dir }}' \
        --no-binary h5py \
        --extra-index-url {{ pip_extra_index_url }} \
        -c "{{ wheels_dir }}/upper-constraints.txt" \
        "{{ wheel_name }}"
    executable: /bin/bash
  environment:
    NPY_NUM_BUILD_JOBS: "{{ ansible_processor_vcpus }}"
    GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS: "{{ ansible_processor_vcpus }}"

- name: Get list of wheel files matching {{ wheel_name }}
  ansible.builtin.find:
    path: "{{ wheels_dir }}"
    patterns: 
      - "{{ wheel_name | regex_replace('.=.*', '') }}*py3*.whl"
      - "{{ wheel_name | regex_replace('.=.*', '') }}*{{ py_ver }}*.whl"
    file_type: file
  register: wheel_files

- name: Get filename of latest {{ wheel_name }} wheel
  ansible.builtin.set_fact:
    wheel_file: "{{ wheel_files.files | sort(attribute='mtime') | last }}"

- name: Install {{ wheel_file.path | basename }} Python package
  become: true
  become_user: "buildslave"
  become_method: su
  ansible.builtin.pip:
    name: "{{ wheel_file.path }}"
    virtualenv: "{{ virtualenv_path }}"
    virtualenv_python: "/opt/python/{{ py_ver }}/bin/python3"
    extra_args: "--extra-index-url {{ pip_extra_index_url }} -c '{{ wheels_dir }}/upper-constraints.txt'"
