#
# BusyB, an automated build utility.
#
# Copyright (C) 1997-2005
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import locale
import tempfile
import os
import smtplib
from BusyBException import BusyBException
#
# Execute the cmd, placing the output in the log file.
#
def cmd( logFile, cmd ):
outputPath = tempfile.mktemp()
newCmd = '(' + cmd + ')' + ' >' + outputPath + ' 2>&1'
logFile.timeStamp( "Executing command: " + newCmd )
logFile.paragraph()
logFile.startQuote()
errorLog=''
print 'RUNNING CMD: ', newCmd
status = os.system(newCmd)
print 'Done running cmd'
print 'checking isfile'
if os.path.isfile(outputPath):
print 'after if'
inFile = open( outputPath, "r")
print 'after reading file'
l = inFile.readline()
print 'before loop'
while l:
logFile.write( l )
l = inFile.readline()
logFile.endQuote()
inFile.close()
print 'end of if'
print 'getting the tail of the log'
log = tailLog(outputPath)
print 'got the tail of the log'
logFile.writeln( "Command returned " + str(status) )
os.remove(outputPath)
print 'checking status'
if status != 0:
print 'status bad'
raise BusyBException( "Command failed [" + str(status) + "]: " + cmd, logFile )
return status
#
# Return the last 2k or so of the given file.
#
def tailLog(path):
try:
length = os.stat(path)[6]
except:
return 'Cant read log file: ' + path
inFile=open( path, "r")
if length > 2000:
inFile.seek( -2000, 2 )
errorLog=inFile.read()
inFile.close()
return errorLog
def writeFile( path, contents ):
f = open( path, "w" )
f.write( contents )
f.write( '\n' )
f.close()
def readFile( path ):
f = open( path, "r")
ret = f.read()
f.close()
return ret.strip()
#
# Send an email.
#
def sendMail( host, port, fromAddr, toAddr, message):
mailer = smtplib.SMTP(host, port)
if type(message)==unicode:
codeset=locale.getpreferredencoding()
message=message.encode(codeset)
mailer.sendmail( fromAddr, toAddr, message )
|