Mercurial > hg
annotate tests/dummysmtpd.py @ 30435:b86a448a2965
zstd: vendor python-zstandard 0.5.0
As the commit message for the previous changeset says, we wish
for zstd to be a 1st class citizen in Mercurial. To make that
happen, we need to enable Python to talk to the zstd C API. And
that requires bindings.
This commit vendors a copy of existing Python bindings. Why do we
need to vendor? As the commit message of the previous commit says,
relying on systems in the wild to have the bindings or zstd present
is a losing proposition. By distributing the zstd and bindings with
Mercurial, we significantly increase our chances that zstd will
work. Since zstd will deliver a better end-user experience by
achieving better performance, this benefits our users. Another
reason is that the Python bindings still aren't stable and the
API is somewhat fluid. While Mercurial could be coded to target
multiple versions of the Python bindings, it is safer to bundle
an explicit, known working version.
The added Python bindings are mostly a fully-featured interface
to the zstd C API. They allow one-shot operations, streaming,
reading and writing from objects implements the file object
protocol, dictionary compression, control over low-level compression
parameters, and more. The Python bindings work on Python 2.6,
2.7, and 3.3+ and have been tested on Linux and Windows. There are
CFFI bindings, but they are lacking compared to the C extension.
Upstream work will be needed before we can support zstd with PyPy.
But it will be possible.
The files added in this commit come from Git commit
e637c1b214d5f869cf8116c550dcae23ec13b677 from
https://github.com/indygreg/python-zstandard and are added without
modifications. Some files from the upstream repository have been
omitted, namely files related to continuous integration.
In the spirit of full disclosure, I'm the maintainer of the
"python-zstandard" project and have authored 100% of the code
added in this commit. Unfortunately, the Python bindings have
not been formally code reviewed by anyone. While I've tested
much of the code thoroughly (I even have tests that fuzz APIs),
there's a good chance there are bugs, memory leaks, not well
thought out APIs, etc. If someone wants to review the code and
send feedback to the GitHub project, it would be greatly
appreciated.
Despite my involvement with both projects, my opinions of code
style differ from Mercurial's. The code in this commit introduces
numerous code style violations in Mercurial's linters. So, the code
is excluded from most lints. However, some violations I agree with.
These have been added to the known violations ignore list for now.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 10 Nov 2016 22:15:58 -0800 |
parents | 1b8b6adb2365 |
children | d9d8d78e6bc9 |
rev | line source |
---|---|
29332
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 """dummy SMTP server for use in tests""" |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 from __future__ import absolute_import |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
6 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 import asyncore |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
8 import optparse |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 import smtpd |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
10 import ssl |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 import sys |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
12 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
13 from mercurial import ( |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
14 cmdutil, |
29556
1b8b6adb2365
tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29332
diff
changeset
|
15 sslutil, |
1b8b6adb2365
tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29332
diff
changeset
|
16 ui as uimod, |
29332
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
17 ) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
18 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
19 def log(msg): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
20 sys.stdout.write(msg) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
21 sys.stdout.flush() |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 class dummysmtpserver(smtpd.SMTPServer): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 def __init__(self, localaddr): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
25 smtpd.SMTPServer.__init__(self, localaddr, remoteaddr=None) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
26 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
27 def process_message(self, peer, mailfrom, rcpttos, data): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
28 log('%s from=%s to=%s\n' % (peer[0], mailfrom, ', '.join(rcpttos))) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
29 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
30 class dummysmtpsecureserver(dummysmtpserver): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
31 def __init__(self, localaddr, certfile): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
32 dummysmtpserver.__init__(self, localaddr) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
33 self._certfile = certfile |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
34 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
35 def handle_accept(self): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
36 pair = self.accept() |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
37 if not pair: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
38 return |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
39 conn, addr = pair |
29556
1b8b6adb2365
tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29332
diff
changeset
|
40 ui = uimod.ui() |
29332
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
41 try: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
42 # wrap_socket() would block, but we don't care |
29556
1b8b6adb2365
tests: use sslutil.wrapserversocket()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29332
diff
changeset
|
43 conn = sslutil.wrapserversocket(conn, ui, certfile=self._certfile) |
29332
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
44 except ssl.SSLError: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
45 log('%s ssl error\n' % addr[0]) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
46 conn.close() |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
47 return |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
48 smtpd.SMTPChannel(self, conn, addr) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
49 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
50 def run(): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
51 try: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
52 asyncore.loop() |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
53 except KeyboardInterrupt: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
54 pass |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
55 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
56 def main(): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
57 op = optparse.OptionParser() |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
58 op.add_option('-d', '--daemon', action='store_true') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
59 op.add_option('--daemon-postexec', action='append') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
60 op.add_option('-p', '--port', type=int, default=8025) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
61 op.add_option('-a', '--address', default='localhost') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
62 op.add_option('--pid-file', metavar='FILE') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
63 op.add_option('--tls', choices=['none', 'smtps'], default='none') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
64 op.add_option('--certificate', metavar='FILE') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
65 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
66 opts, args = op.parse_args() |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
67 if opts.tls == 'smtps' and not opts.certificate: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
68 op.error('--certificate must be specified') |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
69 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
70 addr = (opts.address, opts.port) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
71 def init(): |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
72 if opts.tls == 'none': |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
73 dummysmtpserver(addr) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
74 else: |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
75 dummysmtpsecureserver(addr, opts.certificate) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
76 log('listening at %s:%d\n' % addr) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
77 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
78 cmdutil.service(vars(opts), initfn=init, runfn=run, |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
79 runargs=[sys.executable, __file__] + sys.argv[1:]) |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
80 |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
81 if __name__ == '__main__': |
2bb0ddd8267b
tests: add dummy SMTP daemon for SSL tests
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
82 main() |