1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #4413 from jerm/ansible

Add capability to pass in a PATH prefix to run_command and allow pip module
to utilize that to make virtualenv bin/ available in pip installs
This commit is contained in:
James Tanner 2013-11-07 15:50:41 -05:00
commit 898de833b0
2 changed files with 36 additions and 9 deletions

View file

@ -939,7 +939,7 @@ class AnsibleModule(object):
# rename might not preserve context # rename might not preserve context
self.set_context_if_different(dest, context, False) self.set_context_if_different(dest, context, False)
def run_command(self, args, check_rc=False, close_fds=False, executable=None, data=None, binary_data=False): def run_command(self, args, check_rc=False, close_fds=False, executable=None, data=None, binary_data=False, path_prefix=None):
''' '''
Execute a command, returns rc, stdout, and stderr. Execute a command, returns rc, stdout, and stderr.
args is the command to run args is the command to run
@ -963,9 +963,25 @@ class AnsibleModule(object):
rc = 0 rc = 0
msg = None msg = None
st_in = None st_in = None
# Set a temporart env path if a prefix is passed
env=os.environ
if path_prefix:
env['PATH']="%s:%s" % (path_prefix, env['PATH'])
if data: if data:
st_in = subprocess.PIPE st_in = subprocess.PIPE
try: try:
if path_prefix is not None:
cmd = subprocess.Popen(args,
executable=executable,
shell=shell,
close_fds=close_fds,
stdin=st_in,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env)
else:
cmd = subprocess.Popen(args, cmd = subprocess.Popen(args,
executable=executable, executable=executable,
shell=shell, shell=shell,
@ -973,6 +989,7 @@ class AnsibleModule(object):
stdin=st_in, stdin=st_in,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
if data: if data:
if not binary_data: if not binary_data:
data += '\\n' data += '\\n'

View file

@ -276,6 +276,16 @@ def main():
cmd = '%s %s' % (pip, state_map[state]) cmd = '%s %s' % (pip, state_map[state])
# If there's a virtualenv we want things we install to be able to use other
# installations that exist as binaries within this virtualenv. Example: we
# install cython and then gevent -- gevent needs to use the cython binary,
# not just a python package that will be found by calling the right python.
# So if there's a virtualenv, we add that bin/ to the beginning of the PATH
# in run_command by setting path_prefix here.
path_prefix = None
if env:
path_prefix="/".join(pip.split('/')[:-1])
if extra_args: if extra_args:
cmd += ' %s' % extra_args cmd += ' %s' % extra_args
if name: if name:
@ -320,7 +330,7 @@ def main():
os.chdir(tempfile.gettempdir()) os.chdir(tempfile.gettempdir())
if chdir: if chdir:
os.chdir(chdir) os.chdir(chdir)
rc, out_pip, err_pip = module.run_command(cmd) rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix)
out += out_pip out += out_pip
err += err_pip err += err_pip
if rc == 1 and state == 'absent' and 'not installed' in out_pip: if rc == 1 and state == 'absent' and 'not installed' in out_pip: