From 00a66969107610385f9876403d6aec915b64bc66 2024-03-03 13:57:08 From: Branko Majic Date: 2024-03-03 13:57:08 Subject: [PATCH] MAR-192: Fix incorrect prompt for pipreqcheck virtual environment: - Ensure that the virtualenv indicator string ends with a whitespace. - The virtualenv prompt parameter behaves slightly different between the versions of Debian 11 Bullseye and Debian 12 Bookworm - the former being a bit more user-friendly. - Recreate virtual environment if the prompt does not match-up. --- diff --git a/roles/common/molecule/default/tests/test_default.py b/roles/common/molecule/default/tests/test_default.py index 1cbd19f3b3ff08ab03d6e66b02f471e39bcd8c6a..9bdc107f561ba4fb34bc2f41cfe0f247b6a445c2 100644 --- a/roles/common/molecule/default/tests/test_default.py +++ b/roles/common/molecule/default/tests/test_default.py @@ -250,6 +250,25 @@ def test_pipreqcheck_virtualenv(host): assert virtualenv_activate.mode == 0o644 +def test_pipreqcheck_virtualenv_prompt(host): + """ + Tests if Python virtual environment prompt has been set-up + correctly. + """ + + with host.sudo("pipreqcheck"): + prompt = host.run('bash -c "source /var/lib/pipreqcheck/virtualenv/bin/activate; printenv PS1"') + + # Chop off trailing newline if present (this is from the + # host.run itself). + if prompt.stdout.endswith("\n"): + prompt_stdout = prompt.stdout[:-1] + else: + prompt_stdout = prompt.stdout + + assert prompt_stdout == "(pipreqcheck) " + + def test_pipreqcheck_directories(host): """ Tests creation of directories used for storing configuration used by script diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml index d4afc69d9871d96d8966582e8bd4a99145df2839..c459b176cf7ea40efb5116245018972b493ae571 100644 --- a/roles/common/tasks/main.yml +++ b/roles/common/tasks/main.yml @@ -369,11 +369,24 @@ changed_when: false register: virtualenv_python_version -- name: Remove virtual environment if Python version does not match +- name: Retrieve virtual environment prompt + command: + argv: + - "bash" + - "-c" + - "source '/var/lib/pipreqcheck/virtualenv/bin/activate'; printenv PS1" + failed_when: false + changed_when: false + register: current_virtualenv_prompt + +- name: Remove virtual environment in case of mismatches file: path: "/var/lib/pipreqcheck/virtualenv" state: absent - when: "virtualenv_python_version.rc != 0 or virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip()" + when: | + virtualenv_python_version.rc != 0 or + virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip() or + current_virtualenv_prompt.stdout != "(pipreqcheck) " - name: Create directory for Python virtual environment used for installing/running pip-tools file: @@ -387,7 +400,7 @@ - "/var/lib/pipreqcheck/virtualenv" - name: Create Python virtual environment used for installing/running pip-tools - command: "/usr/bin/virtualenv --python '{{ item.python_path }}' --prompt '({{ item.name }})' '{{ item.virtualenv_path }}'" + command: "/usr/bin/virtualenv --python '{{ item.python_path }}' --prompt '{{ item.virtualenv_prompt }}' '{{ item.virtualenv_path }}'" args: creates: "{{ item.creates }}" become: true @@ -395,6 +408,7 @@ with_items: - name: pipreqcheck virtualenv_path: "/var/lib/pipreqcheck/virtualenv" + virtualenv_prompt: "{% if ansible_distribution_release == 'bullseye' %}(pipreqcheck) {% else %}pipreqcheck{% endif %}" python_path: "/usr/bin/python3" creates: "/var/lib/pipreqcheck/virtualenv/bin/python3"