⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandtools.py

📁 JMVM MPEG MVC/3DAV 测试平台 国际通用标准
💻 PY
字号:

"""
This python module contains various utilities that are used to
run and report results of commands.
"""

import os, commands, sys, smtplib, traceback

def DoCommand(commandString,printCommand=0,printResult=0,
	      writeResultToFile=None,adminEmail=None):
	"""
	DoCommand(commandString,printCommand=0,printResult=0,
	          writeResultToFile=None,adminEmail=None):

	commandString:  String representing the command to execute.
	printCommand:   If non-zero, print commandString before execution.
	printResult:    If non-zero, print result after execution.
	writeResultToFile: If not None, then a file with the name given
	                   by this variable will be created and the output
			   of the command will be written to it.
        adminEmail:     Email address of someone to send complaint emails
                        to if command does not work.

	This command runs the shell command in commandString and
	raises an exception if it fails.  If the command succeeds,
	it's output is returned as the result of this function.
	"""
	if (printResult):
            ImmediatePrint('Prepare to execute command '+`commandString`+'\n')

        commandString = commandString + ' 2>&1 '
        if (writeResultToFile):
            commandString = commandString + '| tee ' + writeResultToFile
            ImmediatePrint('Output will be written to:\t\n%s\n' %
                           writeResultToFile)
	pipeForCommand = os.popen(commandString)
	commandResult = pipeForCommand.read() # returns output of command
	status = pipeForCommand.close() # will be None or integer error code

	if (None != status):
		msg = ('\nERROR: While attempting command ' + `commandString` +
		       '\n\n\nGot status ' + `status` + ' and error:\n\n ' +
		       MakeStringIfNecessary(commandResult) + '.\n\n' )
                if (adminEmail != None and adminEmail.strip() != ''):
                    print msg
                    print 'Sending complaint email to %s\n' % adminEmail
                    SendComplaintEmail(adminEmail, msg)
                    print 'Done sending complaint email. Continuing program.\n'
                else:
                    print msg
                    raise msg
                    
	else:
            if (printResult):
                ImmediatePrint( 'Result of command ' + commandString +
				'\nwas ' + commandResult + '\n' )
		return commandResult

def MakeStringIfNecessary(data):
    if (type('') != type(data)):
        return `data`
    else:
        return data

def ImmediatePrint(data):
	print data
	sys.stdout.flush()

def GetTracebackString(exc_info):
    s = 'Traceback:\n'
    for line in traceback.format_tb(exc_info[2]):
        s = s + line
    return s


def SendComplaintEmail(emailAddr,complaint):

	return SendMessageToUser(emailAddr,
				 'Complaint from MVCRunner.py',complaint)
	
def SendMessageToUser(emailAddr,subject,message,debugLevel=1):

	try:
		if (None == emailAddr or '' == emailAddr.strip() or 'None'==emailAddr):
			return

		msg = "Subject: %s\r\nFrom: %s\r\nTo: %s\r\n\r\n%s\r\n" % (
		    subject,emailAddr,emailAddr,message)

		server = smtplib.SMTP(emailAddr.split('@')[1])
		server.set_debuglevel(debugLevel)
		server.sendmail(emailAddr, emailAddr, msg)
		server.quit()
	except Exception, e:
		exc_info = sys.exc_info()
		msg = 'In %s, Got Exception of type %s:%s\ntraceback:\n%s' % (
			'SendMessageToUser',`sys.exc_type`, e.__str__(),
			GetTracebackString(exc_info))
		print msg

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -