Better parse virtualenv_command option for pip (#80624)

Atthe moment if a users wants to protect  virtualenv_command by using
quotes around 'venv', module will fail out as literal parsing is used
at the moment. In order to imrpove things, argparse is leveraged to
parse out passed value to the virtualenv_command

Closes-Bug: #76372

Signed-off-by: Dmitriy Rabotyagov <noonedeadpunk@gmail.com>
pull/80691/head
Dmitriy Rabotyagov 2 years ago committed by GitHub
parent 251360314d
commit 7f48fa0129
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,6 @@
---
bugfixes:
- >-
Fixed `pip` module failure in case of usage quotes for
`virtualenv_command` option for the venv command.
(https://github.com/ansible/ansible/issues/76372)

@ -264,6 +264,7 @@ virtualenv:
sample: "/tmp/virtualenv" sample: "/tmp/virtualenv"
''' '''
import argparse
import os import os
import re import re
import sys import sys
@ -306,6 +307,18 @@ def _is_vcs_url(name):
return re.match(_VCS_RE, name) return re.match(_VCS_RE, name)
def _is_venv_command(command):
venv_parser = argparse.ArgumentParser()
venv_parser.add_argument('-m', type=str)
argv = shlex.split(command)
if argv[0] == 'pyvenv':
return True
args, dummy = venv_parser.parse_known_args(argv[1:])
if args.m == 'venv':
return True
return False
def _is_package_name(name): def _is_package_name(name):
"""Test whether the name is a package name or a version specifier.""" """Test whether the name is a package name or a version specifier."""
return not name.lstrip().startswith(tuple(op_dict.keys())) return not name.lstrip().startswith(tuple(op_dict.keys()))
@ -540,7 +553,7 @@ def setup_virtualenv(module, env, chdir, out, err):
virtualenv_python = module.params['virtualenv_python'] virtualenv_python = module.params['virtualenv_python']
# -p is a virtualenv option, not compatible with pyenv or venv # -p is a virtualenv option, not compatible with pyenv or venv
# this conditional validates if the command being used is not any of them # this conditional validates if the command being used is not any of them
if not any(ex in module.params['virtualenv_command'] for ex in ('pyvenv', '-m venv')): if not _is_venv_command(module.params['virtualenv_command']):
if virtualenv_python: if virtualenv_python:
cmd.append('-p%s' % virtualenv_python) cmd.append('-p%s' % virtualenv_python)
elif PY3: elif PY3:

@ -568,6 +568,28 @@
that: that:
- "version13 is success" - "version13 is success"
- name: Test virtualenv command with venv formatting
when: ansible_python.version.major > 2
block:
- name: Clean up the virtualenv
file:
state: absent
name: "{{ remote_tmp_dir }}/pipenv"
# ref: https://github.com/ansible/ansible/issues/76372
- name: install using different venv formatting
pip:
name: "{{ pip_test_package }}"
virtualenv: "{{ remote_tmp_dir }}/pipenv"
virtualenv_command: "{{ ansible_python_interpreter ~ ' -mvenv' }}"
state: present
register: version14
- name: ensure install using virtualenv_command with venv formatting
assert:
that:
- "version14 is changed"
### test virtualenv_command end ### ### test virtualenv_command end ###
# https://github.com/ansible/ansible/issues/68592 # https://github.com/ansible/ansible/issues/68592

Loading…
Cancel
Save