~stefanor/ubuntu-dev-tools/progressbar-845787

« back to all changes in this revision

Viewing changes to ubuntutools/requestsync/mail.py

  • Committer: Stefano Rivera
  • Date: 2011-09-09 18:03:47 UTC
  • Revision ID: stefanor@ubuntu.com-20110909180347-ojwxbjmnm27mda4p
requestsync: Add nice error messages to gpg-signing code, rather than
simple assertions (LP: #537288)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import socket
27
27
 
28
28
from debian.changelog import Version
 
29
from devscripts.logger import Logger
29
30
from distro_info import DebianDistroInfo
30
31
 
31
32
from ubuntutools.archive import rmadison, FakeSPPH
113
114
 
114
115
    # prepare sign command
115
116
    gpg_command = None
116
 
    for cmd in ('gpg', 'gpg2', 'gnome-gpg'):
 
117
    for cmd in ('gnome-gpg', 'gpg2', 'gpg'):
117
118
        if os.access('/usr/bin/%s' % cmd, os.X_OK):
118
119
            gpg_command = [cmd]
119
 
    assert gpg_command # TODO: catch exception and produce error message
 
120
            break
 
121
 
 
122
    if not gpg_command:
 
123
        Logger.error("Cannot locate gpg, please install the 'gnupg' package")
 
124
        sys.exit(1)
120
125
 
121
126
    gpg_command.append('--clearsign')
122
127
    if keyid:
126
131
    gpg = subprocess.Popen(gpg_command, stdin=subprocess.PIPE,
127
132
                           stdout=subprocess.PIPE)
128
133
    signed_report = gpg.communicate(mailbody.encode('utf-8'))[0].decode('utf-8')
129
 
    assert gpg.returncode == 0
 
134
    if gpg.returncode != 0:
 
135
        Logger.error("%s failed", gpg_command[0])
 
136
        sys.exit(1)
130
137
 
131
138
    # generate email
132
139
    mail = u'''\
142
149
 
143
150
    # connect to the server
144
151
    try:
145
 
        print 'Connecting to %s:%s ...' % (mailserver_host, mailserver_port)
 
152
        Logger.info('Connecting to %s:%s ...', mailserver_host, mailserver_port)
146
153
        s = smtplib.SMTP(mailserver_host, mailserver_port)
147
154
    except socket.error, s:
148
 
        print >> sys.stderr, 'E: Could not connect to %s:%s: %s (%i)' % \
149
 
            (mailserver_host, mailserver_port, s[1], s[0])
 
155
        Logger.error('Could not connect to %s:%s: %s (%i)',
 
156
                     mailserver_host, mailserver_port, s[1], s[0])
150
157
        return
151
158
 
152
159
    if mailserver_user and mailserver_pass:
153
160
        try:
154
161
            s.login(mailserver_user, mailserver_pass)
155
162
        except smtplib.SMTPAuthenticationError:
156
 
            print >> sys.stderr, ('E: Error authenticating to the server: '
157
 
                                  'invalid username and password.')
 
163
            Logger.error('Error authenticating to the server: '
 
164
                         'invalid username and password.')
158
165
            s.quit()
159
166
            return
160
167
        except:
161
 
            print >> sys.stderr, 'E: Unknown SMTP error.'
 
168
            Logger.error('Unknown SMTP error.')
162
169
            s.quit()
163
170
            return
164
171
 
165
172
    s.sendmail(myemailaddr, to, mail.encode('utf-8'))
166
173
    s.quit()
167
 
    print 'Sync request mailed.'
 
174
    Logger.normal('Sync request mailed.')