0

I'm trying to run an ant job using python subprocess. 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" .

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. 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?

1
  • 1
    you don't need to escape double quotes inside single quotes in Python: shlex.split('-Dno-gen-thrift "" -Dtestargs "--protocol=binary --transport=buffered"') Commented Jul 31, 2014 at 16:45

2 Answers 2

2

In your original command line which you would run right in a 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. Ant does not see the double quotes anymore. You should reproduce the same with subprocess, and not provide '-Dtestargs \"--protocol=binary --transport=buffered\"' as a single argument including double quotes. Provide two independent arguments, i.e. two list items, one being '-Dtestargs', and the other being '--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. 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. 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. That can be tricky.

Sign up to request clarification or add additional context in comments.

1 Comment

also -Dno-gen-thrift="" should probably be translated to '-Dno-gen-thrift', ''
0

It worked for me when I used the following,

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

2 Comments

As J.F. Sebastian pointed out, there is no need to escape the double quotes inside a string literal with single quote terminators. Did you try what I proposed?
'--protocol=binary --transport=buffered' as one arguement doesn't worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.