From 8c353d051668110417fd5b1b6d8dc50ea41cd333 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 7 Oct 2015 10:38:47 -0400 Subject: [PATCH 1/3] make chdir a path so it resolves shell aliases also removed this_dir logic as it is not needed, chdir is None by default and run_command can handle that. --- packaging/language/pip.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packaging/language/pip.py b/packaging/language/pip.py index 8bbae35038d..30adce43ef1 100644 --- a/packaging/language/pip.py +++ b/packaging/language/pip.py @@ -239,7 +239,7 @@ def main(): virtualenv_python=dict(default=None, required=False, type='str'), use_mirrors=dict(default='yes', type='bool'), extra_args=dict(default=None, required=False), - chdir=dict(default=None, required=False), + chdir=dict(default=None, required=False, type='path'), executable=dict(default=None, required=False), ), required_one_of=[['name', 'requirements']], @@ -285,10 +285,7 @@ def main(): cmd += ' -p%s' % virtualenv_python cmd = "%s %s" % (cmd, env) - this_dir = tempfile.gettempdir() - if chdir: - this_dir = os.path.join(this_dir, chdir) - rc, out_venv, err_venv = module.run_command(cmd, cwd=this_dir) + rc, out_venv, err_venv = module.run_command(cmd, cwd=chdir) out += out_venv err += err_venv if rc != 0: @@ -328,9 +325,6 @@ def main(): elif requirements: cmd += ' -r %s' % requirements - this_dir = tempfile.gettempdir() - if chdir: - this_dir = os.path.join(this_dir, chdir) if module.check_mode: if extra_args or requirements or state == 'latest' or not name: @@ -340,7 +334,8 @@ def main(): module.exit_json(changed=True) freeze_cmd = '%s freeze' % pip - rc, out_pip, err_pip = module.run_command(freeze_cmd, cwd=this_dir) + + rc, out_pip, err_pip = module.run_command(freeze_cmd, cwd=chdir) if rc != 0: module.exit_json(changed=True) @@ -353,7 +348,7 @@ def main(): changed = (state == 'present' and not is_present) or (state == 'absent' and is_present) module.exit_json(changed=changed, cmd=freeze_cmd, stdout=out, stderr=err) - rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix, cwd=this_dir) + rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix, cwd=chdir) out += out_pip err += err_pip if rc == 1 and state == 'absent' and \ From a639da7c44472503f88bf3de66647686d01e4839 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 7 Oct 2015 12:18:36 -0400 Subject: [PATCH 2/3] default chdir to tmpdir to avoid virtualenv issues --- packaging/language/pip.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packaging/language/pip.py b/packaging/language/pip.py index 30adce43ef1..e587fad920f 100644 --- a/packaging/language/pip.py +++ b/packaging/language/pip.py @@ -258,6 +258,9 @@ def main(): if state == 'latest' and version is not None: module.fail_json(msg='version is incompatible with state=latest') + if chdir is None: + chdir = tempfile.gettempdir() + err = '' out = '' From 22bfb54d9d8ba784425d64bd4094b0153b159806 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 7 Oct 2015 12:19:50 -0400 Subject: [PATCH 3/3] added comment explaining chdir defaults --- packaging/language/pip.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packaging/language/pip.py b/packaging/language/pip.py index e587fad920f..a4af27ccee5 100644 --- a/packaging/language/pip.py +++ b/packaging/language/pip.py @@ -259,6 +259,7 @@ def main(): module.fail_json(msg='version is incompatible with state=latest') if chdir is None: + # this is done to avoid permissions issues with privilege escalation and virtualenvs chdir = tempfile.gettempdir() err = ''