From 73e19c2ed10671721ad665fff55a50a12f426e69 Mon Sep 17 00:00:00 2001 From: Rob Nieuwenhuizen Date: Tue, 8 Aug 2017 08:56:04 +0200 Subject: [PATCH] Add support for pip list format=columns (#21990) * Use 'pip list --format=freeze' to simplify output code parse --- lib/ansible/modules/packaging/language/pip.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py index 126a4e4fef6..b6f254ef504 100644 --- a/lib/ansible/modules/packaging/language/pip.py +++ b/lib/ansible/modules/packaging/language/pip.py @@ -250,7 +250,7 @@ def _get_full_name(name, version=None): def _get_packages(module, pip, chdir): '''Return results of pip command to get packages.''' # Try 'pip list' command first. - command = '%s list' % pip + command = '%s list --format=freeze' % pip lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C') rc, out, err = module.run_command(command, cwd=chdir, environ_update=lang_env) @@ -267,19 +267,8 @@ def _get_packages(module, pip, chdir): def _is_present(name, version, installed_pkgs, pkg_command): '''Return whether or not package is installed.''' for pkg in installed_pkgs: - # Package listing will be different depending on which pip - # command was used ('pip list' vs. 'pip freeze'). - if 'list' in pkg_command: - pkg = pkg.replace('(', '').replace(')', '') - if ',' in pkg: - pkg_name, pkg_version, _ = pkg.replace(',', '').split(' ') - else: - pkg_name, pkg_version = pkg.split(' ') - elif 'freeze' in pkg_command: - if '==' in pkg: - pkg_name, pkg_version = pkg.split('==') - else: - continue + if '==' in pkg: + pkg_name, pkg_version = pkg.split('==') else: continue