Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your <code>prefix</code>-based solution is perfectly valid. However, if you want to have a <code>shell_env</code> context manager as the one proposed in <a href="https://github.com/fabric/fabric/issues/263">issue#263</a>, you can use the following alternative implementation in your fab files:</p> <pre><code>from fabric.api import run, env, prefix from contextlib import contextmanager @contextmanager def shell_env(**env_vars): orig_shell = env['shell'] env_vars_str = ' '.join('{0}={1}'.format(key, value) for key, value in env_vars.items()) env['shell']='{0} {1}'.format(env_vars_str, orig_shell) yield env['shell']= orig_shell def my_task(): with prefix('echo FOO1=$FOO1, FOO2=$FOO2, FOO3=$FOO3'): with shell_env(FOO1='BAR1', FOO2='BAR2', FOO3='BAR3'): run('env | grep BAR') </code></pre> <p>Note that this context manager modifies <code>env['shell']</code> instead of <code>env['command_prefixes']</code> (as <code>prefix</code> context manager does), so you:</p> <ul> <li>can still use <code>prefix</code> (see example output below) without the interaction problems mentioned in <a href="https://github.com/fabric/fabric/issues/263">issue#263</a>.</li> <li>have to apply any changes to <code>env['shell']</code> before using <code>shell_env</code>. Otherwise, <code>shell_env</code> changes will be overwritten and environment variables won't be available for your commands.</li> </ul> <p>When executing the fab file above, you get the following output:</p> <pre><code>$ fab -H localhost my_task [localhost] Executing task 'my_task' [localhost] run: env | grep BAR [localhost] out: FOO1=BAR1, FOO2=BAR2, FOO3=BAR3 [localhost] out: FOO1=BAR1 [localhost] out: FOO2=BAR2 [localhost] out: FOO3=BAR3 [localhost] out: Done. Disconnecting from localhost... done. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload