Reputation: 5553
I have a bash snippet I would like to port over to Python. It finds where SVN is located and whether it is executable.
SVN=`which svn 2>&1`
if [[ ! -x $SVN ]]; then
echo "A subversion binary could not be found ($SVN)"
fi
Here is my current attempt in Python using the subprocess module:
SVN = Popen('which svn 2>&1', shell=True, stdout=PIPE).communicate()[0]
Popen("if [[ ! -x SVN ]]; then echo 'svn could not be found or executed'; fi", shell=True)
This does not work because while I do have the location of SVN saved in the local namespace of Python, I can't access it from Popen.
I also tried combining into one Popen object:
Popen("if [[ ! -x 'which svn 2>&1']]; then echo 'svn could not be found'; fi", shell=True)
But I get this error (and needless to say, looks very unwieldy)
/bin/sh: -c: line 0: syntax error near `;'
/bin/sh: -c: line 0: `if [[ ! -x 'which svn 2>&1']]; then echo 'svn could not be found'; fi'
Is there a Python version of the test construct "-x"? I think that would be ideal. Other workarounds would be appreciated as well.
Thanks
Upvotes: 0
Views: 105
Reputation: 12428
SVN = Popen('which svn 2>&1', shell=True, stdout=PIPE).communicate()[0]
str="if [[ ! -x " + SVN + " ]]; then echo 'svn could not be found or executed'; fi"
Popen(str, shell=True)
Upvotes: 1
Reputation: 7608
There is no need to use which, you can already try to run svn without parameters and if it works it means it's there.
try:
SVN = subprocess.Popen('svn')
SVN.wait()
print "svn exists"
except OSError:
print "svn does not exist"
Upvotes: 1
Reputation: 72299
This is the simplest solution:
path_to_svn = shutil.which('svn')
is_executable = os.access(path_to_svn, os.X_OK)
shutil.which
is new in Python 3.3; there's a polyfill in this answer. You could grab the path from Popen
too if you really want, but that's not necessary.
And here are the docs for os.access
.
Upvotes: 4