Use tempfile

This commit is contained in:
Stefano Rivera 2011-12-03 21:57:11 +02:00
parent 8a6502aaa4
commit 899d1b5361

View File

@ -25,6 +25,7 @@ import re
import sys import sys
import smtplib import smtplib
import socket import socket
import tempfile
from debian.changelog import Changelog, Version from debian.changelog import Changelog, Version
from devscripts.logger import Logger from devscripts.logger import Logger
@ -164,12 +165,14 @@ Content-Type: text/plain; charset=UTF-8
confirmation_prompt() confirmation_prompt()
# save mail in temporary file # save mail in temporary file
f=open("/tmp/requestsync-" + re.sub("[^a-zA-Z0-9_\-]","",bugtitle.replace(" ","_")), "w") backup = tempfile.NamedTemporaryFile(mode='w', delete=False,
f.write(mail) prefix='requestsync-' + re.sub(r'[^a-zA-Z0-9_-]', '',
f.close() bugtitle.replace(' ', '_')))
with backup:
backup.write(mail)
Logger.normal('The e-mail has been saved in %s and will be deleted ' Logger.normal('The e-mail has been saved in %s and will be deleted '
'after succesful transmission', f.name) 'after succesful transmission', backup.name)
# connect to the server # connect to the server
while True: while True:
@ -205,7 +208,7 @@ Content-Type: text/plain; charset=UTF-8
try: try:
s.sendmail(myemailaddr, to, mail.encode('utf-8')) s.sendmail(myemailaddr, to, mail.encode('utf-8'))
s.quit() s.quit()
os.remove(f.name) os.remove(backup.name)
Logger.normal('Sync request mailed.') Logger.normal('Sync request mailed.')
break break
except smtplib.SMTPRecipientsRefused, smtperror: except smtplib.SMTPRecipientsRefused, smtperror: