Skip to content

Instantly share code, notes, and snippets.

@shivendra14
Last active July 6, 2018 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivendra14/381d626ab62a936bf18a4b331b407624 to your computer and use it in GitHub Desktop.
Save shivendra14/381d626ab62a936bf18a4b331b407624 to your computer and use it in GitHub Desktop.
Python script to run an exe, capture the logs and email them as attachment
#
#Revision-1
#Author: Shivendra Agarwal
#Year: 2018
#Title: Python script to run an executable, capture the logs and email them as attachment
#
# Script to run the tests
import subprocess, sys
def IsPlatformWin():
return (sys.platform == "win32")
command = R'GoogleTest' # path of executable you want to run
if (IsPlatformWin):
command+= '.exe'
p = subprocess.Popen(command,stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = p.communicate()
print stderrdata
content = stdoutdata
# Script to parse above content and send a mail if any failure.
status = content.partition('Global test environment tear-down')[2]
print (status)
if '[ FAILED ]' in status:
# Script to send a mail in case of failure
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
msg = MIMEMultipart()
msg['From'] = 'from@gmail.com' # fix the address
msg['To'] = 'to@gmail.com' # fix the address
msg['Subject'] = 'GoogleTest failure report'
body = status
msg.attach(MIMEText(body, 'plain'))
part = MIMEBase('application', "octet-stream")
part.set_payload(content)
part.add_header('Content-Disposition', 'attachment; filename="log.txt"') # name of file
msg.attach(part)
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.login(msg['From'], 'password') # password of from
# if you face issue while logging in gmail
# https://www.lifewire.com/what-are-the-gmail-smtp-settings-1170854
# https://www.lifewire.com/unlock-gmail-for-a-new-email-program-or-service-1171974
mailServer.sendmail(msg['From'], msg['To'], msg.as_string())
mailServer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment