From 677e1b8e4ea57fd67eb432812f8a7f6cf406d862 Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Sun, 27 Nov 2011 22:14:05 +0100 Subject: [PATCH 01/12] requestsync: Give user option to retry in case of temporary error (LP: #850360) --- debian/changelog | 3 ++- ubuntutools/requestsync/mail.py | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/debian/changelog b/debian/changelog index 8aeb5f3..b8041d7 100644 --- a/debian/changelog +++ b/debian/changelog @@ -18,8 +18,9 @@ ubuntu-dev-tools (0.137) UNRELEASED; urgency=low [ Andreas Moog ] * sponsor-patch: Check permission to unsubscribe sponsors-team (LP: #896884) + * requestsync: Give user option to retry in case of temporary error (LP: #850360) - -- Andreas Moog Sun, 27 Nov 2011 16:47:05 +0100 + -- Andreas Moog Sun, 27 Nov 2011 22:13:22 +0100 ubuntu-dev-tools (0.136) unstable; urgency=low diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index c6607d6..4708b7a 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -163,13 +163,20 @@ Content-Type: text/plain; charset=UTF-8 confirmation_prompt() # connect to the server - try: - Logger.info('Connecting to %s:%s ...', mailserver_host, mailserver_port) - s = smtplib.SMTP(mailserver_host, mailserver_port) - except socket.error, s: - Logger.error('Could not connect to %s:%s: %s (%i)', - mailserver_host, mailserver_port, s[1], s[0]) - return + while True: + try: + Logger.info('Connecting to %s:%s ...', mailserver_host, mailserver_port) + s = smtplib.SMTP(mailserver_host, mailserver_port) + break + except socket.error, s: + Logger.error('Could not connect to %s:%s: %s (%i)', + mailserver_host, mailserver_port, s[1], s[0]) + return + except smtplib.SMTPConnectError, s: + Logger.error('Could not connect to %s:%s: %s (%i)', + mailserver_host, mailserver_port, s[1], s[0]) + if s[0] == 421: + confirmation_prompt(message="This is a temporary error, press [Enter] to retry. Press [Ctrl-C] to abort now.") if mailserver_user and mailserver_pass: try: From d9c00752c063a4c6740fafc99536c166c519f1b1 Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Mon, 28 Nov 2011 19:53:23 +0100 Subject: [PATCH 02/12] Wrap long line --- ubuntutools/requestsync/mail.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 4708b7a..88a4bef 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -175,8 +175,9 @@ Content-Type: text/plain; charset=UTF-8 except smtplib.SMTPConnectError, s: Logger.error('Could not connect to %s:%s: %s (%i)', mailserver_host, mailserver_port, s[1], s[0]) - if s[0] == 421: - confirmation_prompt(message="This is a temporary error, press [Enter] to retry. Press [Ctrl-C] to abort now.") + if s.smtp_code == 421: + confirmation_prompt(message='This is a temporary error, press' + '[Enter] to retry. Press [Ctrl-C] to abort now.') if mailserver_user and mailserver_pass: try: From 10e9b18ca7fcde4549d606810ebdd16cd2d5bd2f Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Mon, 28 Nov 2011 20:50:28 +0100 Subject: [PATCH 03/12] Save a temporary file in case sending fails --- ubuntutools/requestsync/mail.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 88a4bef..a95f911 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -162,6 +162,16 @@ Content-Type: text/plain; charset=UTF-8 print 'The final report is:\n%s' % mail confirmation_prompt() + # save mail in temporary file + f=open("/tmp/requestsync-" + srcpkg,"w") + f.write(mail) + f.close() + + print ''' +The e-mail has been saved in %s and will be deleted +after succesful transmission +''' % f.name + # connect to the server while True: try: @@ -176,8 +186,8 @@ Content-Type: text/plain; charset=UTF-8 Logger.error('Could not connect to %s:%s: %s (%i)', mailserver_host, mailserver_port, s[1], s[0]) if s.smtp_code == 421: - confirmation_prompt(message='This is a temporary error, press' - '[Enter] to retry. Press [Ctrl-C] to abort now.') + confirmation_prompt(message='This is a temporary error, press ' + '[Enter] to retry. Press [Ctrl-C] to abort now.') if mailserver_user and mailserver_pass: try: @@ -192,6 +202,10 @@ Content-Type: text/plain; charset=UTF-8 s.quit() return - s.sendmail(myemailaddr, to, mail.encode('utf-8')) - s.quit() - Logger.normal('Sync request mailed.') + try: + s.sendmail(myemailaddr, to, mail.encode('utf-8')) + s.quit() + os.remove(f.name) + Logger.normal('Sync request mailed.') + except: + Logger.error('Unknown error while sending the mail.') From 0e3a831df988b68837781bc7784594956f576496 Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Tue, 29 Nov 2011 20:20:25 +0100 Subject: [PATCH 04/12] Use Logger.normal --- ubuntutools/requestsync/mail.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index a95f911..7ce536b 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -167,10 +167,8 @@ Content-Type: text/plain; charset=UTF-8 f.write(mail) f.close() - print ''' -The e-mail has been saved in %s and will be deleted -after succesful transmission -''' % f.name + Logger.normal('The e-mail has been saved in %s and will be deleted ' + 'after succesful transmission', f.name) # connect to the server while True: From 9199747fc35cc35b4c69b14e1adc5370e3ee7a2b Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Fri, 2 Dec 2011 20:06:57 +0100 Subject: [PATCH 05/12] Space after comma --- ubuntutools/requestsync/mail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 7ce536b..2d3cac2 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -163,7 +163,7 @@ Content-Type: text/plain; charset=UTF-8 confirmation_prompt() # save mail in temporary file - f=open("/tmp/requestsync-" + srcpkg,"w") + f=open("/tmp/requestsync-" + srcpkg, "w") f.write(mail) f.close() From 12030d99e9818366a8258dcd19ad7123dac29c77 Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Sat, 3 Dec 2011 11:12:11 +0100 Subject: [PATCH 06/12] Use bugtitle as temporary filename --- ubuntutools/requestsync/mail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 2d3cac2..748d216 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -163,7 +163,7 @@ Content-Type: text/plain; charset=UTF-8 confirmation_prompt() # save mail in temporary file - f=open("/tmp/requestsync-" + srcpkg, "w") + f=open("/tmp/requestsync-" + bugtitle.replace(" ","_"), "w") f.write(mail) f.close() From d18a35c449ddfb017a5f4fd25ac4e6bc41b8d11a Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Sat, 3 Dec 2011 11:57:18 +0100 Subject: [PATCH 07/12] Also allow retry if recipient got rejected --- ubuntutools/requestsync/mail.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 748d216..dce0cac 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -200,10 +200,18 @@ Content-Type: text/plain; charset=UTF-8 s.quit() return - try: - s.sendmail(myemailaddr, to, mail.encode('utf-8')) - s.quit() - os.remove(f.name) - Logger.normal('Sync request mailed.') - except: - Logger.error('Unknown error while sending the mail.') + while True: + try: + s.sendmail(myemailaddr, to, mail.encode('utf-8')) + s.quit() + os.remove(f.name) + Logger.normal('Sync request mailed.') + break + except smtplib.SMTPRecipientsRefused, smtperror: + smtp_code, smtp_message = smtperror.recipients[to] + Logger.error('Error while sending: %i, %s', smtp_code, smtp_message) + if smtp_code == 450: + confirmation_prompt(message='This is a temporary error, press ' + '[Enter] to retry. Press [Ctrl-C] to abort now.') + else: + return From 32a85dc1729eceee27561531cbb680ae585e7cb8 Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Sat, 3 Dec 2011 18:01:55 +0100 Subject: [PATCH 08/12] Add a exception for disconnected server --- ubuntutools/requestsync/mail.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index dce0cac..70801f3 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -215,3 +215,6 @@ Content-Type: text/plain; charset=UTF-8 '[Enter] to retry. Press [Ctrl-C] to abort now.') else: return + except smtplib.SMTPServerDisconnected: + Logger.error('Server disconnected while sending the mail.') + return From 8a6502aaa49478e56fe867917d7edc293efd3329 Mon Sep 17 00:00:00 2001 From: Andreas Moog Date: Sat, 3 Dec 2011 18:09:49 +0100 Subject: [PATCH 09/12] Use regex to filter unwanted characters in filename --- ubuntutools/requestsync/mail.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 70801f3..a1684ce 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -21,6 +21,7 @@ # of the GNU General Public License license. import os +import re import sys import smtplib import socket @@ -163,7 +164,7 @@ Content-Type: text/plain; charset=UTF-8 confirmation_prompt() # save mail in temporary file - f=open("/tmp/requestsync-" + bugtitle.replace(" ","_"), "w") + f=open("/tmp/requestsync-" + re.sub("[^a-zA-Z0-9_\-]","",bugtitle.replace(" ","_")), "w") f.write(mail) f.close() From 899d1b5361900f3560568d2827d27e4b229627b4 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sat, 3 Dec 2011 21:57:11 +0200 Subject: [PATCH 10/12] Use tempfile --- ubuntutools/requestsync/mail.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index a1684ce..723ee15 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -25,6 +25,7 @@ import re import sys import smtplib import socket +import tempfile from debian.changelog import Changelog, Version from devscripts.logger import Logger @@ -164,12 +165,14 @@ Content-Type: text/plain; charset=UTF-8 confirmation_prompt() # save mail in temporary file - f=open("/tmp/requestsync-" + re.sub("[^a-zA-Z0-9_\-]","",bugtitle.replace(" ","_")), "w") - f.write(mail) - f.close() + backup = tempfile.NamedTemporaryFile(mode='w', delete=False, + prefix='requestsync-' + re.sub(r'[^a-zA-Z0-9_-]', '', + bugtitle.replace(' ', '_'))) + with backup: + backup.write(mail) 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 while True: @@ -205,7 +208,7 @@ Content-Type: text/plain; charset=UTF-8 try: s.sendmail(myemailaddr, to, mail.encode('utf-8')) s.quit() - os.remove(f.name) + os.remove(backup.name) Logger.normal('Sync request mailed.') break except smtplib.SMTPRecipientsRefused, smtperror: From 9442e2ea30f898812743f4b13a46e458067b3195 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sat, 3 Dec 2011 22:06:49 +0200 Subject: [PATCH 11/12] Catch response exceptions in the DATA phase --- ubuntutools/requestsync/mail.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 723ee15..769af10 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -219,6 +219,10 @@ Content-Type: text/plain; charset=UTF-8 '[Enter] to retry. Press [Ctrl-C] to abort now.') else: return + except smtplib.SMTPResponseException, e: + Logger.error('Error while sending: %i, %s', + e.smtp_code, e.smtp_error) + return except smtplib.SMTPServerDisconnected: Logger.error('Server disconnected while sending the mail.') return From e68d3a699dc31b1da659f583149379aac2add677 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sat, 3 Dec 2011 22:07:18 +0200 Subject: [PATCH 12/12] Bump logging level of 'connecting' message --- ubuntutools/requestsync/mail.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index 769af10..1d94876 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -177,7 +177,8 @@ Content-Type: text/plain; charset=UTF-8 # connect to the server while True: try: - Logger.info('Connecting to %s:%s ...', mailserver_host, mailserver_port) + Logger.normal('Connecting to %s:%s ...', mailserver_host, + mailserver_port) s = smtplib.SMTP(mailserver_host, mailserver_port) break except socket.error, s: