简体   繁体   English

使用 Python 子进程运行 `eval($ something)` 命令

[英]Running `eval($ something)` commands using Python sub-process

I have the following subprocess/command I run inside terminal:我在终端内运行了以下子进程/命令:

$ eval $(docker-machine env machine-name)

I assumed the python equivalent would be:我假设 python 等价物是:

sp = subprocess.run(["eval", "$(docker-machine env {})".format(self.project_machine_name)])

or perhaps:也许:

sp = subprocess.run(["eval", "$(docker-machine", "env", "{})".format(self.project_machine_name)])

However, neither of the above seem to work.但是,以上两种方法似乎都不起作用。

I was wondering, how would I run the above eval within a python script...我想知道,我将如何在 python 脚本中运行上述eval ......

NB .注意 For brevity, assume self.project_machine_name = 'machine-name'为简洁起见,假设 self.project_machine_name = 'machine-name'

This is not the same as calling a sub-process as it needs to be eval() .这与调用子进程不同,因为它需要是eval()

try尝试

sp = subprocess.run(['docker-machine', 'env', self.project_machine_name])

More info and additional arguments - in the docs更多信息和附加参数 - 在文档中

EDIT: if you want the output, use subprocess.check_output() instead.编辑:如果您想要输出,请改用subprocess.check_output()

It depends what you're trying to do but I may be doing something similar.这取决于你想要做什么,但我可能正在做类似的事情。 I'm building my dockers in minikube and have a python script that I use to build those dockers.我正在 minikube 中构建我的 docker,并有一个 python 脚本用于构建这些 docker。

Before I run the docker build I need to make sure the right environment variables are set, meaning I need to run在运行 docker build 之前,我需要确保设置了正确的环境变量,这意味着我需要运行

eval $(minikube -p minikube docker-env)

Which generates a bunch of statements that you're meant to run in the shell (typically with an eval)这会生成一堆您打算在 shell 中运行的语句(通常带有 eval)

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.49.2:2376"
export DOCKER_CERT_PATH="/home/aluchko/.minikube/certs"
export MINIKUBE_ACTIVE_DOCKERD="minikube"

# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p minikube docker-env)

To be able to trigger my builds in python I need these environment variables set when I my docker build commands.为了能够在 python 中触发我的构建,我需要在我的 docker 构建命令时设置这些环境变量。

Now there's basically two ways to do this (aside from doing the eval in a wrapper around your script).现在基本上有两种方法可以做到这一点(除了在脚本的包装器中执行 eval 之外)。 Both ways involve getting the that string with the export commands.两种方法都涉及使用导出命令获取该字符串。

output = subprocess.check_output(['minikube', '-p', 'minikube', 'docker-env'])
  1. Parse the results of the minikube (or docker-machine command) and set those variables in the shell for future subprocesses.解析 minikube(或 docker-machine 命令)的结果,并在 shell 中为未来的子进程设置这些变量。 You can be a bit sloppy with the RE since you know the output of the docker-machine command.由于您知道 docker-machine 命令的输出,因此您可能对 RE 有点草率。 The shell=True is just for the echo. shell=True 仅用于回声。

     output = subprocess.check_output(['minikube', '-p', 'minikube', 'docker-env']) import re export_re = re.compile('export ([AZ\\_]+)="(.*)"\\\\n') export_pairs = export_re.findall(output.decode("UTF-8")) build_env = os.environ.copy() for k,v in export_pairs: build_env[k] = v subprocess.run('echo $DOCKER_HOST', shell=True, env=build_env)
  2. A bit sloppier, and more prone to injection attacks, just sandwich the strings together so all your commands are basically a script that starts with a bunch of exports, this isn't nearly as clean and locks you into using the shell=True subprocess functionality, but it does work.有点草率,更容易受到注入攻击,只需将字符串夹在一起,这样您的所有命令基本上都是一个以一堆导出开头的脚本,这几乎没有那么干净,并且会锁定您使用 shell=True 子进程功能,但它确实有效。

     output = subprocess.check_output(['minikube', '-p', 'minikube', 'docker-env']) extended_output = output + b"\\necho $DOCKER_HOST" print(extended_output) subprocess.run(extended_output, shell=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM