comparison tests/dummysmtpd.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 78f1899e4202
children c102b704edb5
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
16 server, 16 server,
17 sslutil, 17 sslutil,
18 ui as uimod, 18 ui as uimod,
19 ) 19 )
20 20
21
21 def log(msg): 22 def log(msg):
22 sys.stdout.write(msg) 23 sys.stdout.write(msg)
23 sys.stdout.flush() 24 sys.stdout.flush()
25
24 26
25 class dummysmtpserver(smtpd.SMTPServer): 27 class dummysmtpserver(smtpd.SMTPServer):
26 def __init__(self, localaddr): 28 def __init__(self, localaddr):
27 smtpd.SMTPServer.__init__(self, localaddr, remoteaddr=None) 29 smtpd.SMTPServer.__init__(self, localaddr, remoteaddr=None)
28 30
35 # current connection and subsequent ones fail on the client side with 37 # current connection and subsequent ones fail on the client side with
36 # "No connection could be made because the target machine actively 38 # "No connection could be made because the target machine actively
37 # refused it". If we eat the error, then the client properly aborts in 39 # refused it". If we eat the error, then the client properly aborts in
38 # the expected way, and the server is available for subsequent requests. 40 # the expected way, and the server is available for subsequent requests.
39 traceback.print_exc() 41 traceback.print_exc()
42
40 43
41 class dummysmtpsecureserver(dummysmtpserver): 44 class dummysmtpsecureserver(dummysmtpserver):
42 def __init__(self, localaddr, certfile): 45 def __init__(self, localaddr, certfile):
43 dummysmtpserver.__init__(self, localaddr) 46 dummysmtpserver.__init__(self, localaddr)
44 self._certfile = certfile 47 self._certfile = certfile
56 log('%s ssl error\n' % addr[0]) 59 log('%s ssl error\n' % addr[0])
57 conn.close() 60 conn.close()
58 return 61 return
59 smtpd.SMTPChannel(self, conn, addr) 62 smtpd.SMTPChannel(self, conn, addr)
60 63
64
61 def run(): 65 def run():
62 try: 66 try:
63 asyncore.loop() 67 asyncore.loop()
64 except KeyboardInterrupt: 68 except KeyboardInterrupt:
65 pass 69 pass
66 70
71
67 def _encodestrsonly(v): 72 def _encodestrsonly(v):
68 if isinstance(v, type(u'')): 73 if isinstance(v, type(u'')):
69 return v.encode('ascii') 74 return v.encode('ascii')
70 return v 75 return v
76
71 77
72 def bytesvars(obj): 78 def bytesvars(obj):
73 unidict = vars(obj) 79 unidict = vars(obj)
74 bd = {k.encode('ascii'): _encodestrsonly(v) for k, v in unidict.items()} 80 bd = {k.encode('ascii'): _encodestrsonly(v) for k, v in unidict.items()}
75 if bd[b'daemon_postexec'] is not None: 81 if bd[b'daemon_postexec'] is not None:
76 bd[b'daemon_postexec'] = [ 82 bd[b'daemon_postexec'] = [
77 _encodestrsonly(v) for v in bd[b'daemon_postexec']] 83 _encodestrsonly(v) for v in bd[b'daemon_postexec']
84 ]
78 return bd 85 return bd
86
79 87
80 def main(): 88 def main():
81 op = optparse.OptionParser() 89 op = optparse.OptionParser()
82 op.add_option('-d', '--daemon', action='store_true') 90 op.add_option('-d', '--daemon', action='store_true')
83 op.add_option('--daemon-postexec', action='append') 91 op.add_option('--daemon-postexec', action='append')
90 opts, args = op.parse_args() 98 opts, args = op.parse_args()
91 if opts.tls == 'smtps' and not opts.certificate: 99 if opts.tls == 'smtps' and not opts.certificate:
92 op.error('--certificate must be specified') 100 op.error('--certificate must be specified')
93 101
94 addr = (opts.address, opts.port) 102 addr = (opts.address, opts.port)
103
95 def init(): 104 def init():
96 if opts.tls == 'none': 105 if opts.tls == 'none':
97 dummysmtpserver(addr) 106 dummysmtpserver(addr)
98 else: 107 else:
99 dummysmtpsecureserver(addr, opts.certificate) 108 dummysmtpsecureserver(addr, opts.certificate)
100 log('listening at %s:%d\n' % addr) 109 log('listening at %s:%d\n' % addr)
101 110
102 server.runservice( 111 server.runservice(
103 bytesvars(opts), initfn=init, runfn=run, 112 bytesvars(opts),
104 runargs=[pycompat.sysexecutable, 113 initfn=init,
105 pycompat.fsencode(__file__)] + pycompat.sysargv[1:]) 114 runfn=run,
115 runargs=[pycompat.sysexecutable, pycompat.fsencode(__file__)]
116 + pycompat.sysargv[1:],
117 )
118
106 119
107 if __name__ == '__main__': 120 if __name__ == '__main__':
108 main() 121 main()