From 551a3666eb7e8f56665951f514e63a2e7919f22b Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Mon, 5 Mar 2012 15:01:08 -0800 Subject: [PATCH] Tweak errRun; add errFail and numCores. --- mininet/util.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mininet/util.py b/mininet/util.py index da83490..8975048 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -61,10 +61,14 @@ def errRun( *cmd, **kwargs ): cmd = cmd[ 0 ] if isinstance( cmd, str ): cmd = cmd.split( ' ' ) + cmd = [ str( arg ) for arg in cmd ] # By default we separate stderr, don't run in a shell, and don't echo stderr = kwargs.get( 'stderr', PIPE ) shell = kwargs.get( 'shell', False ) echo = kwargs.get( 'echo', False ) + if echo: + # cmd goes to stderr, output goes to stdout + info( cmd, '\n' ) popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell ) # We use poll() because select() doesn't work with large fd numbers out, err = '', '' @@ -90,6 +94,14 @@ def errRun( *cmd, **kwargs ): break return out, err, returncode +def errFail( *cmd, **kwargs ): + "Run a command using errRun and raise exception on nonzero exit" + out, err, ret = errRun( *cmd, **kwargs ) + if ret: + raise Exception( "errFail: failed with return code %s" + % ret ) + return out, err, ret + def quietRun( cmd, **kwargs ): "Run a command and return merged stdout and stderr" return errRun( cmd, stderr=STDOUT, **kwargs )[ 0 ] @@ -260,3 +272,13 @@ def natural( text ): def num( s ): return int( s ) if s.isdigit() else text return [ num( s ) for s in re.split( r'(\d+)', text ) ] + +def numCores(): + "Returns number of CPU cores based on /proc/cpuinfo" + if hasattr( numCores, 'ncores' ): + return numCores.ncores + try: + numCores.ncores = int( quietRun('grep -c processor /proc/cpuinfo') ) + except ValueError: + return 0 + return numCores.ncores