From e628bfbe348b24d63a89cf43fe4f42a62cef03a1 Mon Sep 17 00:00:00 2001 From: Michael Bienia Date: Sat, 22 Aug 2009 17:12:55 +0200 Subject: [PATCH] requestsync: - move mail_bug() to ubuntutools.requestsync.mail - implement support for UBU* environment variables (lp: #400133) --- debian/changelog | 4 +- requestsync | 101 +------------------------------- ubuntutools/requestsync/mail.py | 94 +++++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 104 deletions(-) diff --git a/debian/changelog b/debian/changelog index ba79c1c..eaf805a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -14,6 +14,8 @@ ubuntu-dev-tools (0.76) UNRELEASED; urgency=low [ Michael Bienia ] * Drop python-launchpad-bugs from Depends. * buildd: Add a --batch mode for batch retrying/rescoring of packages. + * requestsync: + - Use UBU* environment variables before the DEB* ones (lp: #400133) [ Iain Lane ] * requestsync: @@ -39,7 +41,7 @@ ubuntu-dev-tools (0.76) UNRELEASED; urgency=low - debian/rules: set DEB_PYTHON_SYSTEM to pysupport. - ubuntu-dev-tools.preinst: remove stale pycentral files on upgrades. - -- Luca Falavigna Fri, 21 Aug 2009 17:30:05 +0200 + -- Michael Bienia Sat, 22 Aug 2009 17:10:13 +0200 ubuntu-dev-tools (0.75) karmic; urgency=low diff --git a/requestsync b/requestsync index e73b39c..87ee6c8 100755 --- a/requestsync +++ b/requestsync @@ -92,103 +92,6 @@ def checkExistingReports(package): "please press enter." raw_input_exit_on_ctrlc() -def mail_bug(source_package, subscribe, status, bugtitle, bugtext, keyid = None): - '''Submit the sync request per email. - Return True if email successfully send, otherwise False.''' - - import smtplib - import socket - - to = 'new@bugs.launchpad.net' - - myemailaddr = get_email_address() - if not myemailaddr: - return False - - # generate initial mailbody - mailbody = '' - if source_package: - mailbody += ' affects ubuntu/%s\n' % source_package - else: - mailbody += ' affects ubuntu\n' - mailbody = mailbody + ' status %s\n importance wishlist\n subscribe %s\n done\n\n%s' % (status, subscribe, bugtext) - - # prepare sign_command - sign_command = 'gpg' - for cmd in ('gpg2', 'gnome-gpg'): - if os.access('/usr/bin/%s' % cmd, os.X_OK): - sign_command = cmd - - gpg_command = [sign_command, '--clearsign'] - if keyid: - gpg_command.extend(('-u', keyid)) - - # sign it - gpg = subprocess.Popen(gpg_command, stdin = subprocess.PIPE, stdout = subprocess.PIPE) - signed_report = gpg.communicate(mailbody)[0] - assert gpg.returncode == 0 - - # generate email - mail = '''\ -From: %s -To: %s -Subject: %s -Content-Type: text/plain; charset=UTF-8 - -%s''' % (myemailaddr, to, bugtitle, signed_report) - - # ask for confirmation and allow to edit: - print mail - print 'Do you want to edit the report before sending [y/N]? Press Control-C to abort.' - raw_input_exit_on_ctrlc() - - # get server address - mailserver = os.getenv('DEBSMTP') - if mailserver: - print 'Using custom SMTP server:', mailserver - else: - mailserver = 'fiordland.ubuntu.com' - - # get server port - mailserver_port = os.getenv('DEBSMTP_PORT') - if mailserver_port: - print 'Using custom SMTP port:', mailserver_port - else: - mailserver_port = 25 - - # connect to the server - try: - s = smtplib.SMTP(mailserver, mailserver_port) - except socket.error, s: - print >> sys.stderr, "Could not connect to mailserver %s at port %s: %s (%i)" % \ - (mailserver, mailserver_port, s[1], s[0]) - print "The port %s may be firewalled. Please try using requestsync with" \ - % mailserver_port - print "the '--lp' flag to file a sync request with the launchpadlib " \ - "module." - return False - - # authenticate to the server - mailserver_user = os.getenv('DEBSMTP_USER') - mailserver_pass = os.getenv('DEBSMTP_PASS') - if mailserver_user and mailserver_pass: - try: - s.login(mailserver_user, mailserver_pass) - except smtplib.SMTPAuthenticationError: - print 'Error authenticating to the server: invalid username and password.' - s.quit() - return False - except: - print 'Unknown SMTP error.' - s.quit() - return False - - s.sendmail(myemailaddr, to, mail) - s.quit() - print 'Sync request mailed.' - - return True - # # entry point @@ -232,7 +135,7 @@ if __name__ == '__main__': distro = options.dist ffe = options.ffe - if not use_lp_bugs and not get_email_address(): + if not use_lp_bugs and not getEmailAddress(): sys.exit(1) if len(args) == 0: @@ -366,7 +269,7 @@ if __name__ == '__main__': sys.exit(1) # Mail sync request: - if mail_bug(srcpkg, subscribe, status, title, report, keyid): + if mailBug(srcpkg, subscribe, status, title, report, keyid): sys.exit(0) print 'Something went wrong. No sync request filed.' diff --git a/ubuntutools/requestsync/mail.py b/ubuntutools/requestsync/mail.py index c9c2ac8..1e00081 100644 --- a/ubuntutools/requestsync/mail.py +++ b/ubuntutools/requestsync/mail.py @@ -22,6 +22,8 @@ import os import sys import subprocess +import smtplib +import socket from .common import raw_input_exit_on_ctrlc from ..lp.udtexceptions import PackageNotFoundException @@ -83,12 +85,12 @@ def getDebianSrcPkg(name, release): def getUbuntuSrcPkg(name, release): return getSrcPkg('ubuntu', name, release) -def get_email_address(): +def getEmailAddress(): ''' - Get the From email address from the DEBEMAIL or EMAIL environment - variable or give an error. + Get the From email address from the UBUMAIL, DEBEMAIL or EMAIL + environment variable or give an error. ''' - myemailaddr = os.getenv('DEBEMAIL') or os.getenv('EMAIL') + myemailaddr = os.getenv('UBUMAIL') or os.getenv('DEBEMAIL') or os.getenv('EMAIL') if not myemailaddr: print >> sys.stderr, 'The environment variable DEBEMAIL or ' \ 'EMAIL needs to be set to let this script mail the ' \ @@ -111,3 +113,87 @@ def needSponsorship(name, component): return True else: print 'Invalid answer' + +def mailBug(srcpkg, subscribe, status, bugtitle, bugtext, keyid = None): + ''' + Submit the sync request per email. + ''' + + to = 'new@bugs.launchpad.net' + + # getEmailAddress() can't fail here as the main code in requestsync + # already checks its return value + myemailaddr = getEmailAddress() + + # generate mailbody + if srcpkg: + mailbody = ' affects ubuntu/%s\n' % srcpkg.getPackageName() + else: + mailbody = ' affects ubuntu\n' + mailbody += '''\ + status %s + importance wishlist + subscribe %s + done + +%s''' % (status, subscribe, bugtext) + + # prepare sign command + gpg_command = None + for cmd in ('gpg', 'gpg2', 'gnome-gpg'): + if os.access('/usr/bin/%s' % cmd, os.X_OK): + gpg_command = [cmd] + assert gpg_command # TODO: catch exception and produce error message + + gpg_command.append('--clearsign') + if keyid: + gpg_command.extend(('-u', keyid)) + + # sign the mail body + gpg = subprocess.Popen(gpg_command, stdin = subprocess.PIPE, stdout = subprocess.PIPE) + signed_report = gpg.communicate(mailbody)[0] + assert gpg.returncode == 0 + + # generate email + mail = '''\ +From: %s +To: %s +Subject: %s +Content-Type: text/plain; charset=UTF-8 + +%s''' % (myemailaddr, to, bugtitle, signed_report) + + print 'The final report is:\n%s' % mail + raw_input_exit_on_ctrlc('Press [Enter] to continue or [Ctrl-C] to abort. ') + + # get server address and port + mailserver_host = os.getenv('UBUSMTP') or os.getenv('DEBSMTP') or 'fiordland.ubuntu.com' + mailserver_port = os.getenv('UBUSMTP_PORT') or os.getenv('DEBSMTP_PORT') or 25 + + # connect to the server + try: + print 'Connecting to %s:%s ...' % (mailserver_host, mailserver_port) + s = smtplib.SMTP(mailserver_host, mailserver_port) + except socket.error, s: + print >> sys.stderr, "Could not connect to %s:%s: %s (%i)" % \ + (mailserver_host, mailserver_port, s[1], s[0]) + return + + # authenticate to the server + mailserver_user = os.getenv('UBUSMTP_USER') or os.getenv('DEBSMTP_USER') + mailserver_pass = os.getenv('UBUSMTP_PASS') or os.getenv('DEBSMTP_PASS') + if mailserver_user and mailserver_pass: + try: + s.login(mailserver_user, mailserver_pass) + except smtplib.SMTPAuthenticationError: + print 'Error authenticating to the server: invalid username and password.' + s.quit() + return + except: + print 'Unknown SMTP error.' + s.quit() + return + + s.sendmail(myemailaddr, to, mail) + s.quit() + print 'Sync request mailed.'