仿真进猛砸在Python“源” [英] Emulating Bash 'source' in Python

查看:98
本文介绍了仿真进猛砸在Python“源”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,看起来是这样的:

I have a script that looks something like this:

export foo=/tmp/foo                                          
export bar=/tmp/bar

每当我建立我运行'源init_env(其中init_env是上面的脚本)来设置一些变量。

Every time I build I run 'source init_env' (where init_env is the above script) to set up some variables.

要完成在Python一样我有这个code运行,

To accomplish the same in Python I had this code running,

reg = re.compile('export (?P<name>\w+)(\=(?P<value>.+))*')
for line in open(file):
    m = reg.match(line)
    if m:
        name = m.group('name')
        value = ''
        if m.group('value'):
            value = m.group('value')
        os.putenv(name, value)

但随后的有人的决定这将是很好添加一行类似下面的 init_env 文件:

But then someone decided it would be nice to add a line like the following to the init_env file:

export PATH="/foo/bar:/bar/foo:$PATH"     

显然,我的Python脚本土崩瓦解。我可以修改Python脚本来处理这条线,但随后会后来才打破时的有人的来了一个新的功能在 init_env 文件。

Obviously my Python script fell apart. I could modify the Python script to handle this line, but then it'll just break later on when someone comes up with a new feature to use in the init_env file.

现在的问题是,如果有一个简单的方法来运行bash命令,让它改变我的 os.environ

The question is if there is an easy way to run a Bash command and let it modify my os.environ?

推荐答案

你的方法的问题是,您要跨preT的bash脚本。首先,你只是尝试间preT出口声明。然后,你发现人们正在使用变量扩展。后来,人们就会把条件语句在他们的文件或程序替换。最终,你将有一个完全成熟的bash脚本间preTER有一个极大的错误。不这样做。

The problem with your approach is that you are trying to interpret bash scripts. First you just try to interpret the export statement. Then you notice people are using variable expansion. Later people will put conditionals in their files, or process substitutions. In the end you will have a full blown bash script interpreter with a gazillion bugs. Don't do that.

让猛砸间preT为你的文件,然后收集结果。

Let Bash interpret the file for you and then collect the results.

您可以做到这一点是这样的:

You can do it like this:

#! /usr/bin/env python

import os
import pprint
import subprocess

command = ['bash', '-c', 'source init_env && env']

proc = subprocess.Popen(command, stdout = subprocess.PIPE)

for line in proc.stdout:
  (key, _, value) = line.partition("=")
  os.environ[key] = value

proc.communicate()

pprint.pprint(dict(os.environ))

请确保您处理的情况下,bash中未能源init_env 错误,或bash本身无法执行,或子流程执行失败的bash,或任何其他错误。

Make sure that you handle errors in case bash fails to source init_env, or bash itself fails to execute, or subprocess fails to execute bash, or any other errors.

阅读的文档了解更多信息。

Read the documentation on subprocess for more details.

请注意:这只会捕捉与导出语句设置变量,如 ENV 只打印输出变量

Note: this will only capture variables set with the export statement, as env only prints exported variables.

享受。

注意 Python文档的说,如果你要操作的环境中,你应该操纵 os.environ 直接而不是使用 os.putenv()。我认为这是一个错误,但我离题了。

Note that the Python documentation says that if you want to manipulate the environment you should manipulate os.environ directly instead of using os.putenv(). I consider that a bug, but I digress.

这篇关于仿真进猛砸在Python“源”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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