#NEWLINE# def measure_energy( oldEnergy = None ) : #NEWLINE# #INDENT# '\n    returns a dictionary with the currently available values of energy consumptions (like a time-stamp).\n    If oldEnergy is not None, the difference (currentValue - oldEnergy) is returned.\n    ' #NEWLINE# newEnergy = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# executable = find_executable ( 'read-energy.sh' , exitOnError = False ) #NEWLINE# if ( executable is None ) : #NEWLINE# #INDENT# logging . debug ( 'Energy measurement not available because read-energy.sh could not be found.' ) #NEWLINE# return newEnergy #NEWLINE# #UNINDENT# for energyType in ENERGY_TYPES : #NEWLINE# #INDENT# logging . debug ( 'Reading %s energy measurement for value.' , energyType ) #NEWLINE# energysh = subprocess . Popen ( [ executable , energyType ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) #NEWLINE# ( stdout , stderr ) = energysh . communicate ( ) #NEWLINE# if ( energysh . returncode or stderr ) : #NEWLINE# #INDENT# logging . debug ( 'Error while reading %s energy measurement: retval=%s, out=%s, err=%s' , energyType , energysh . returncode , stdout , stderr ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# newEnergy [ energyType ] = int ( stdout ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# logging . debug ( 'Invalid value while reading %s energy measurement: %s' , energyType , stdout ) #NEWLINE# #UNINDENT# #UNINDENT# logging . debug ( 'Finished reading energy measurements.' ) #NEWLINE# if ( oldEnergy is None ) : #NEWLINE# #INDENT# return newEnergy #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return _energy_difference ( newEnergy , oldEnergy )