---

- name: "check if upper-constraints file exists"
  stat:
    path: "{{ openstack_requirements_dir }}/upper-constraints.txt"
  register: upper_constraints_file


- name: "ensure Bindep is installed"
  command: >
    '{{ python_executable }}' -m pip install \
    {% if upper_constraints_file.stat.exists == true %}-c'{{ upper_constraints_file.stat.path }}'{% endif %} \
    --user bindep>='{{ bindep_min_version }}'
  register: install_bindep
  changed_when: "'Successfully installed' in install_bindep.stdout"


- block:
    - name: "get '{{ bindep_command }}' full path"
      shell: |
        export PATH={{ python_exe_path }}
        which '{{ bindep_command }}'
      register: get_bindep_path

    - name: "set bindep_executable fact"
      set_fact:
        bindep_executable: '{{ get_bindep_path.stdout_lines | first }}'

  rescue:
    - name: "set bindep_executable fact"
      set_fact:
        bindep_executable: '{{ ansible_user_dir }}/.local/bin/bindep'


- name: "get '{{ bindep_executable }}' version"
  shell: "'{{ bindep_executable }}' --version 2>&1"
  register: get_bindep_version
  failed_when: '(get_bindep_version.stdout_lines | length) == 0'


- name: "update bindep_version fact"
  set_fact:
    bindep_version: '{{ get_bindep_version.stdout_lines | first }}'


- name: "show Bindep facts"
  debug:
    msg:
      bindep_executable: '{{ bindep_executable }}'
      bindep_version: '{{ bindep_version }}'
