结构全局设置环境变量 [英] Fabric set environment variable globally

查看:96
本文介绍了结构全局设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个fabfile,我想为我用于开发的virtualenv包括一个环境变量.我不想放

I am writing a fabfile that I want to include an environment variable for the virtualenv that I'm using for development. I don't want to have to put

with shell_env(venv=VIRTUALENV):
    ...

每个函数中的

.我希望能够在文件的开头添加此代码,并将其应用于全局所有任务.有办法吗?

in every single function. I'd like to be able to add this at the beginning of the file and have it apply to all tasks globally. Is there a way to do this?

例如,我有任务

def setup_dev_env():
    with shell_env(venv=VIRTUALENV):
        local('virtualenv $(venv)')
        with prefix('workon $(venv)'):
            local('pip install -r requirements.txt')
        test()

def test():
    with shell_env(venv=VIRTUALENV):
        with prefix('workon $(vnenv)'):
            local('python3 manage.py test')

如果我不必在两个函数的开头重复相同的行,那将是很好的选择.

and it would be nice if I didn't have to repeat exactly the same line at the beginning of both functions.

推荐答案

我不确定这是否被视为

I'm not sure if this is considered a hack of command_prefixes but it works:

env.command_prefixes=["export myvar='Hello world'",]
def echo_env():
    run(r"echo $myvar")

输出:

C:\Users\swozn\PycharmProjects\aaetuea>fab echo_env
[swozn@localhost] Executing task 'echo_env'
[swozn@localhost] run: echo $myvar
[swozn@localhost] out: Hello world

优点是无法忘记,因为它会自动添加到所有命令的前面.

The advantage is that it is impossible to forget, because it is automatically prepended to all your commands.

这篇关于结构全局设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆