Skip to content

Commit 661a0cb

Browse files
committed
Merge pull request ansible#2223 from bcoca/pip_chdir_fixes
make chdir a path so it resolves shell aliases
2 parents 3a9d046 + 22bfb54 commit 661a0cb

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

packaging/language/pip.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def main():
239239
virtualenv_python=dict(default=None, required=False, type='str'),
240240
use_mirrors=dict(default='yes', type='bool'),
241241
extra_args=dict(default=None, required=False),
242-
chdir=dict(default=None, required=False),
242+
chdir=dict(default=None, required=False, type='path'),
243243
executable=dict(default=None, required=False),
244244
),
245245
required_one_of=[['name', 'requirements']],
@@ -258,6 +258,10 @@ def main():
258258
if state == 'latest' and version is not None:
259259
module.fail_json(msg='version is incompatible with state=latest')
260260

261+
if chdir is None:
262+
# this is done to avoid permissions issues with privilege escalation and virtualenvs
263+
chdir = tempfile.gettempdir()
264+
261265
err = ''
262266
out = ''
263267

@@ -285,10 +289,7 @@ def main():
285289
cmd += ' -p%s' % virtualenv_python
286290

287291
cmd = "%s %s" % (cmd, env)
288-
this_dir = tempfile.gettempdir()
289-
if chdir:
290-
this_dir = os.path.join(this_dir, chdir)
291-
rc, out_venv, err_venv = module.run_command(cmd, cwd=this_dir)
292+
rc, out_venv, err_venv = module.run_command(cmd, cwd=chdir)
292293
out += out_venv
293294
err += err_venv
294295
if rc != 0:
@@ -328,9 +329,6 @@ def main():
328329
elif requirements:
329330
cmd += ' -r %s' % requirements
330331

331-
this_dir = tempfile.gettempdir()
332-
if chdir:
333-
this_dir = os.path.join(this_dir, chdir)
334332

335333
if module.check_mode:
336334
if extra_args or requirements or state == 'latest' or not name:
@@ -340,7 +338,8 @@ def main():
340338
module.exit_json(changed=True)
341339

342340
freeze_cmd = '%s freeze' % pip
343-
rc, out_pip, err_pip = module.run_command(freeze_cmd, cwd=this_dir)
341+
342+
rc, out_pip, err_pip = module.run_command(freeze_cmd, cwd=chdir)
344343

345344
if rc != 0:
346345
module.exit_json(changed=True)
@@ -353,7 +352,7 @@ def main():
353352
changed = (state == 'present' and not is_present) or (state == 'absent' and is_present)
354353
module.exit_json(changed=changed, cmd=freeze_cmd, stdout=out, stderr=err)
355354

356-
rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix, cwd=this_dir)
355+
rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix, cwd=chdir)
357356
out += out_pip
358357
err += err_pip
359358
if rc == 1 and state == 'absent' and \

0 commit comments

Comments
 (0)