简体   繁体   English

使用python子进程运行ant脚本时出错

[英]Error when running ant script using python subprocess

I'm trying to run an ant job using python subprocess. 我正在尝试使用python子进程运行一个蚂蚁作业。 Following is the command I'm trying to execute. 以下是我尝试执行的命令。

ant -f ../lib/java/build.xml -Dno-gen-thrift="" -Dtestargs "--protocol=binary --transport=buffered" run-testserver

But when I ran this using subprocess using following command 但是当我使用以下命令使用子进程运行此命令时

subprocess.call(['ant','-f','lib/java/build.xml','-Dno-gen-thrift=\"\"','-Dtestargs \"--protocol=binary --transport=buffered\"','run-testserver'])

I'm getting error saying "Unknown argument: --transport=buffered" . 我收到错误消息,说“未知参数:--transport = buffered”。

Unknown argument: --protocol=binaty
ant [options] [target [target2 [target3] ...]]
Options: 
  -help, -h              print this message
  -projecthelp, -p       print project help information  ...........

Here '--protocol=binary' and '--transport=buffered' are command line arguments parsed to a java class executes using this ant script. 这里的'--protocol = binary'和'--transport = buffered'是使用此ant脚本执行的解析到java类的命令行参数。 Also following commands run without any issue, when I send only one arguement. 当我仅发送一个论据时,以下命令也可以正常运行。

subprocess.call(['ant','-f','lib/java/build.xml','-Dno-gen-thrift=\"\"','-Dtestargs \"--protocol=binary\"','run-testserver'])

subprocess.call(['ant','-f','lib/java/build.xml','-Dno-gen-thrift=\"\"','-Dtestargs \"--transport=buffered\"','run-testserver'])

What is the reason for this? 这是什么原因呢?

In your original command line which you would run right in a shell, 在您将直接在Shell中运行的原始命令行中,

-Dtestargs "--protocol=binary --transport=buffered"

are actually two command line arguments. 实际上是两个命令行参数。 The shell parses the outer double quotes from the second argument and provides the byte string --protocol=binary --transport=buffered as an argument to the ant executable. shell解析第二个参数的外部双引号,并提供字节字符串--protocol=binary --transport=buffered作为ant可执行文件的参数。 Ant does not see the double quotes anymore. Ant不再看到双引号。 You should reproduce the same with subprocess , and not provide '-Dtestargs \\"--protocol=binary --transport=buffered\\"' as a single argument including double quotes. 您应该再现与同一subprocess ,而不是提供'-Dtestargs \\"--protocol=binary --transport=buffered\\"'作为一个参数,包括双引号。 Provide two independent arguments, ie two list items, one being '-Dtestargs' , and the other being '--protocol=binary --transport=buffered' . 提供两个独立的参数,即两个列表项,一个为'-Dtestargs' ,另一个为'--protocol=binary --transport=buffered'

Honestly, this is just an educated guess, but I am quite sure that this is part of your issue. 老实说,这只是有根据的猜测,但是我很确定这是您问题的一部分。

Also, you should note that command line parsing can be quite a delicate issue. 另外,您应该注意命令行解析可能是一个非常棘手的问题。 Arguments go through separate layers which may not be aware of each other. 参数经过可能彼此不认识的单独层。 For instance, when you run a Python command through a shell, the shell first parses the arguments using a certain method, provides them to the CPython executable, which parses them again using a certain method, and then the Python application code again parses the arguments using a certain method. 例如,当您通过外壳程序运行Python命令时,外壳程序首先使用某种方法解析参数,将其提供给CPython可执行文件,后者再使用某种方法再次解析参数,然后Python应用程序代码再次解析参数使用某种方法。 In your case, Python's subprocess module creates the argument data using a certain method before using a system call to spawn a new process, which introduces even more complecity. 在您的情况下,Python的子进程模块会在使用系统调用生成新进程之前使用某种方法创建参数数据,这会引入更大的复杂性。 All in all, the result may be unexpected behavior and you might have to adjust your command line in order to somehow make Ant understand the right thing. 总而言之,结果可能是意外行为,您可能必须调整命令行以某种方式使Ant理解正确的东西。 That can be tricky. 那可能很棘手。

当我使用以下内容时,它对我有用

subprocess.call(['ant','-f','lib/java/build.xml','-Dno-gen-thrift=\"\"','-Dtestargs', '\"--protocol=binary', '--transport=buffered\"','run-testserver'])

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

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