fix "which" calls always returning true

The redirection of stderr towards stdout means calls to "which program"
always return something even when "program" is not installed.

This patch checks for the return value instead before returning the
value.

See #814.
This commit is contained in:
Matthieu Coudron
2018-09-25 22:29:30 +09:00
parent 1969669f51
commit a73e776695
2 changed files with 10 additions and 5 deletions
+5 -5
View File
@@ -63,7 +63,7 @@ from time import sleep
from mininet.log import info, error, warn, debug from mininet.log import info, error, warn, debug
from mininet.util import ( quietRun, errRun, errFail, moveIntf, isShellBuiltin, from mininet.util import ( quietRun, errRun, errFail, moveIntf, isShellBuiltin,
numCores, retry, mountCgroups, BaseString, decode, numCores, retry, mountCgroups, BaseString, decode,
encode, Python3 ) encode, Python3, which )
from mininet.moduledeps import moduleDeps, pathCheck, TUN from mininet.moduledeps import moduleDeps, pathCheck, TUN
from mininet.link import Link, Intf, TCIntf, OVSIntf from mininet.link import Link, Intf, TCIntf, OVSIntf
from re import findall from re import findall
@@ -1447,7 +1447,7 @@ class Controller( Node ):
@classmethod @classmethod
def isAvailable( cls ): def isAvailable( cls ):
"Is controller available?" "Is controller available?"
return quietRun( 'which controller' ) return which( 'controller' )
class OVSController( Controller ): class OVSController( Controller ):
@@ -1459,9 +1459,9 @@ class OVSController( Controller ):
@classmethod @classmethod
def isAvailable( cls ): def isAvailable( cls ):
return ( quietRun( 'which ovs-controller' ) or return (which( 'ovs-controller' ) or
quietRun( 'which test-controller' ) or which( 'test-controller' ) or
quietRun( 'which ovs-testcontroller' ) ).strip() which( 'ovs-testcontroller' ))
class NOX( Controller ): class NOX( Controller ):
"Controller to run a NOX application." "Controller to run a NOX application."
+5
View File
@@ -171,6 +171,11 @@ def quietRun( cmd, **kwargs ):
"Run a command and return merged stdout and stderr" "Run a command and return merged stdout and stderr"
return errRun( cmd, stderr=STDOUT, **kwargs )[ 0 ] return errRun( cmd, stderr=STDOUT, **kwargs )[ 0 ]
def which(cmd, **kwargs ):
"Run a command and return merged stdout and stderr"
out, _, ret = errRun( ["which", cmd], stderr=STDOUT, **kwargs )
return out.rstrip() if ret == 0 else None
# pylint: enable=maybe-no-member # pylint: enable=maybe-no-member
def isShellBuiltin( cmd ): def isShellBuiltin( cmd ):