[PATCH 3/3] Import pexpect.

Jelmer Vernooij jelmer at samba.org
Thu Jan 29 14:52:46 MST 2015


Change-Id: I94ee5e9d3d91c8d72e76cbdcc4534452321dffb1
Signed-Off-By: Jelmer Vernooij <jelmer at samba.org>
---
 lib/extras/extras/__init__.py                      |    2 +
 lib/extras/extras/tests/test_extras.py             |    2 +
 lib/mimeparse/mimeparse.py                         |    0
 lib/mimeparse/setup.py                             |    5 +-
 lib/pep8/pep8.py                                   |  146 +-
 lib/subunit/INSTALL                                |    5 +-
 lib/subunit/MANIFEST.in                            |    2 +-
 lib/subunit/Makefile.am                            |   26 +-
 lib/subunit/NEWS                                   |  227 +++
 lib/subunit/README                                 |  281 ++-
 lib/subunit/configure.ac                           |    5 +-
 lib/subunit/filters/subunit-filter                 |   25 +-
 lib/subunit/filters/subunit-ls                     |   32 +-
 lib/subunit/filters/subunit-notify                 |    6 +-
 lib/subunit/filters/subunit-stats                  |   29 +-
 lib/subunit/filters/subunit-tags                   |    1 +
 lib/subunit/filters/subunit2csv                    |    5 +-
 lib/subunit/filters/subunit2gtk                    |   77 +-
 lib/subunit/filters/subunit2junitxml               |    7 +-
 lib/subunit/filters/subunit2pyunit                 |   23 +-
 lib/subunit/perl/Makefile.PL.in                    |    4 +-
 lib/subunit/python/subunit/__init__.py             |  162 +-
 lib/subunit/python/subunit/filters.py              |  111 +-
 lib/subunit/python/subunit/run.py                  |   80 +-
 lib/subunit/python/subunit/test_results.py         |   64 +-
 lib/subunit/python/subunit/tests/TestUtil.py       |   80 -
 lib/subunit/python/subunit/tests/__init__.py       |   52 +-
 lib/subunit/python/subunit/tests/test_chunked.py   |    6 -
 lib/subunit/python/subunit/tests/test_details.py   |    6 -
 .../python/subunit/tests/test_progress_model.py    |    6 -
 lib/subunit/python/subunit/tests/test_run.py       |   97 +-
 .../python/subunit/tests/test_subunit_filter.py    |  110 +-
 .../python/subunit/tests/test_subunit_stats.py     |    6 -
 .../python/subunit/tests/test_subunit_tags.py      |   90 +-
 .../python/subunit/tests/test_tap2subunit.py       |  378 ++--
 .../python/subunit/tests/test_test_protocol.py     |   75 +-
 .../python/subunit/tests/test_test_results.py      |    6 -
 lib/subunit/runtests.py                            |  138 --
 lib/subunit/setup.py                               |   22 +-
 lib/testtools/.testr.conf                          |    2 +-
 lib/testtools/LICENSE                              |    1 +
 lib/testtools/MANIFEST.in                          |    4 +-
 lib/testtools/Makefile                             |    8 +-
 lib/testtools/NEWS                                 |   64 +-
 lib/testtools/README                               |   89 -
 lib/testtools/doc/for-framework-folk.rst           |  235 ++-
 lib/testtools/doc/for-test-authors.rst             |  149 +-
 lib/testtools/doc/hacking.rst                      |   97 +-
 lib/testtools/doc/index.rst                        |    2 +-
 lib/testtools/doc/overview.rst                     |   13 +-
 lib/testtools/scripts/_lp_release.py               |    6 +-
 lib/testtools/setup.cfg                            |    3 +
 lib/testtools/setup.py                             |   57 +-
 lib/testtools/testtools/__init__.py                |  120 +-
 lib/testtools/testtools/compat.py                  |  102 +-
 lib/testtools/testtools/content.py                 |  229 ++-
 lib/testtools/testtools/content_type.py            |    2 +-
 lib/testtools/testtools/deferredruntest.py         |   18 +-
 lib/testtools/testtools/distutilscmd.py            |    2 +-
 lib/testtools/testtools/helpers.py                 |   83 +-
 lib/testtools/testtools/matchers/__init__.py       |    6 +
 lib/testtools/testtools/matchers/_basic.py         |   13 +-
 lib/testtools/testtools/matchers/_dict.py          |    2 +-
 lib/testtools/testtools/matchers/_exception.py     |   24 +-
 lib/testtools/testtools/matchers/_higherorder.py   |   79 +
 lib/testtools/testtools/matchers/_impl.py          |    4 +-
 lib/testtools/testtools/run.py                     |  352 ++--
 lib/testtools/testtools/runtest.py                 |   38 +-
 lib/testtools/testtools/testcase.py                |  332 +++-
 lib/testtools/testtools/testresult/__init__.py     |   24 +
 lib/testtools/testtools/testresult/doubles.py      |   24 +
 lib/testtools/testtools/testresult/real.py         |  800 ++++++++-
 lib/testtools/testtools/tests/__init__.py          |    7 +-
 lib/testtools/testtools/tests/helpers.py           |   13 +-
 .../testtools/tests/matchers/test_basic.py         |   24 +-
 .../testtools/tests/matchers/test_dict.py          |    7 +-
 .../testtools/tests/matchers/test_exception.py     |    7 +-
 .../testtools/tests/matchers/test_higherorder.py   |   27 +
 lib/testtools/testtools/tests/test_compat.py       |  183 +-
 lib/testtools/testtools/tests/test_content.py      |   91 +-
 lib/testtools/testtools/tests/test_content_type.py |    2 +-
 .../testtools/tests/test_deferredruntest.py        |   28 +-
 lib/testtools/testtools/tests/test_distutilscmd.py |    7 +-
 .../testtools/tests/test_fixturesupport.py         |   30 +-
 lib/testtools/testtools/tests/test_helpers.py      |  183 --
 lib/testtools/testtools/tests/test_run.py          |  258 ++-
 lib/testtools/testtools/tests/test_runtest.py      |   58 +-
 lib/testtools/testtools/tests/test_spinner.py      |   20 +-
 lib/testtools/testtools/tests/test_testcase.py     |  425 ++++-
 lib/testtools/testtools/tests/test_testresult.py   |  890 +++++++++-
 lib/testtools/testtools/tests/test_testsuite.py    |  192 +-
 lib/testtools/testtools/tests/test_with_with.py    |   20 +
 lib/testtools/testtools/testsuite.py               |  178 +-
 third_party/pexpect/.gitignore                     |    9 +
 third_party/pexpect/.travis.yml                    |   23 +
 third_party/pexpect/ANSI.py                        |    7 +
 third_party/pexpect/DEVELOPERS.rst                 |   12 +
 third_party/pexpect/FSM.py                         |    7 +
 third_party/pexpect/LICENSE                        |   19 +
 third_party/pexpect/MANIFEST.in                    |    8 +
 third_party/pexpect/README.rst                     |   53 +
 third_party/pexpect/doc/FAQ.rst                    |  136 ++
 third_party/pexpect/doc/Makefile                   |  153 ++
 third_party/pexpect/doc/api/ANSI.rst               |   14 +
 third_party/pexpect/doc/api/fdpexpect.rst          |   22 +
 third_party/pexpect/doc/api/index.rst              |   12 +
 third_party/pexpect/doc/api/pexpect.rst            |  111 ++
 third_party/pexpect/doc/api/pxssh.rst              |   34 +
 third_party/pexpect/doc/api/replwrap.rst           |   26 +
 third_party/pexpect/doc/api/screen.rst             |   10 +
 third_party/pexpect/doc/clean.css                  |  103 ++
 third_party/pexpect/doc/commonissues.rst           |  115 ++
 third_party/pexpect/doc/conf.py                    |  250 +++
 third_party/pexpect/doc/examples.rst               |   63 +
 third_party/pexpect/doc/history.rst                |  215 +++
 third_party/pexpect/doc/index.rst                  |   50 +
 third_party/pexpect/doc/install.rst                |   20 +
 third_party/pexpect/doc/make.bat                   |  190 ++
 third_party/pexpect/doc/overview.rst               |  240 +++
 third_party/pexpect/doc/requirements.txt           |    1 +
 third_party/pexpect/doc/sphinxext/github.py        |  155 ++
 third_party/pexpect/examples/README                |   89 +
 third_party/pexpect/examples/astat.py              |   99 ++
 third_party/pexpect/examples/cgishell.cgi          |  766 ++++++++
 third_party/pexpect/examples/chess.py              |  148 ++
 third_party/pexpect/examples/chess2.py             |  153 ++
 third_party/pexpect/examples/chess3.py             |  157 ++
 third_party/pexpect/examples/df.py                 |   57 +
 third_party/pexpect/examples/ftp.py                |   73 +
 third_party/pexpect/examples/hive.py               |  466 +++++
 third_party/pexpect/examples/monitor.py            |  229 +++
 third_party/pexpect/examples/passmass.py           |  116 ++
 third_party/pexpect/examples/python.py             |   49 +
 third_party/pexpect/examples/script.py             |  114 ++
 third_party/pexpect/examples/ssh_tunnel.py         |  105 ++
 third_party/pexpect/examples/table_test.html       |  106 ++
 third_party/pexpect/examples/topip.py              |  299 ++++
 third_party/pexpect/examples/uptime.py             |   81 +
 third_party/pexpect/fdpexpect.py                   |    7 +
 third_party/pexpect/notes/my_forkpty.py            |   89 +
 third_party/pexpect/notes/notes.txt                |   50 +
 third_party/pexpect/notes/posixmodule.c.diff       |  233 +++
 third_party/pexpect/pexpect/ANSI.py                |  351 ++++
 third_party/pexpect/pexpect/FSM.py                 |  334 ++++
 third_party/pexpect/pexpect/__init__.py            |  232 +++
 third_party/pexpect/pexpect/async.py               |   70 +
 third_party/pexpect/pexpect/bashrc.sh              |    5 +
 third_party/pexpect/pexpect/exceptions.py          |   35 +
 third_party/pexpect/pexpect/expect.py              |  297 ++++
 third_party/pexpect/pexpect/fdpexpect.py           |   86 +
 third_party/pexpect/pexpect/pty_spawn.py           |  819 +++++++++
 third_party/pexpect/pexpect/pxssh.py               |  403 +++++
 third_party/pexpect/pexpect/replwrap.py            |  113 ++
 third_party/pexpect/pexpect/screen.py              |  424 +++++
 third_party/pexpect/pexpect/spawnbase.py           |  484 +++++
 third_party/pexpect/pexpect/utils.py               |  112 ++
 third_party/pexpect/pxssh.py                       |    7 +
 third_party/pexpect/screen.py                      |    7 +
 third_party/pexpect/setup.cfg                      |    2 +
 third_party/pexpect/setup.py                       |   65 +
 third_party/pexpect/tests/PexpectTestCase.py       |  108 ++
 third_party/pexpect/tests/README                   |    8 +
 third_party/pexpect/tests/TESTDATA.txt             |    8 +
 third_party/pexpect/tests/__init__.py              |   25 +
 third_party/pexpect/tests/adhoc.py                 |   36 +
 third_party/pexpect/tests/alarm_die.py             |    5 +
 third_party/pexpect/tests/bambi.vt                 |  417 +++++
 .../tests/depricated_test_filedescriptor.py        |   82 +
 third_party/pexpect/tests/echo_w_prompt.py         |   15 +
 third_party/pexpect/tests/echo_wait.py             |   41 +
 third_party/pexpect/tests/exit1.py                 |   24 +
 third_party/pexpect/tests/exit667.c                |   26 +
 third_party/pexpect/tests/fakessh/ssh              |   29 +
 third_party/pexpect/tests/getch.py                 |   48 +
 third_party/pexpect/tests/globe.vt                 |  690 ++++++++
 third_party/pexpect/tests/interact.py              |   40 +
 third_party/pexpect/tests/interact_unicode.py      |   24 +
 third_party/pexpect/tests/list100.py               |    2 +
 third_party/pexpect/tests/needs_kill.py            |   13 +
 third_party/pexpect/tests/pexpectTest.py           |   69 +
 .../tests/platform_checks/CSIGNALTEST/test.c       |   90 +
 third_party/pexpect/tests/platform_checks/README   |    2 +
 third_party/pexpect/tests/platform_checks/check.py |   77 +
 .../pexpect/tests/platform_checks/check2.py        |   44 +
 .../platform_checks/check_control_terminal.py      |   19 +
 .../pexpect/tests/platform_checks/check_handler.py |   60 +
 .../pexpect/tests/platform_checks/check_read.py    |   35 +
 .../pexpect/tests/platform_checks/check_signals.py |   67 +
 third_party/pexpect/tests/qa.py                    |   26 +
 third_party/pexpect/tests/sigwinch_report.py       |   49 +
 third_party/pexpect/tests/sleep_for.py             |   41 +
 third_party/pexpect/tests/swapcase_echo.py         |   26 +
 third_party/pexpect/tests/test_FSM.py              |   34 +
 third_party/pexpect/tests/test_ansi.py             |  228 +++
 third_party/pexpect/tests/test_async.py            |   51 +
 .../pexpect/tests/test_command_list_split.py       |   40 +
 third_party/pexpect/tests/test_constructor.py      |   48 +
 third_party/pexpect/tests/test_ctrl_chars.py       |  125 ++
 third_party/pexpect/tests/test_destructor.py       |   84 +
 third_party/pexpect/tests/test_dotall.py           |   43 +
 third_party/pexpect/tests/test_expect.py           |  583 +++++++
 third_party/pexpect/tests/test_filedescriptor.py   |   72 +
 third_party/pexpect/tests/test_interact.py         |   94 +
 third_party/pexpect/tests/test_isalive.py          |  114 ++
 third_party/pexpect/tests/test_log.py              |  108 ++
 third_party/pexpect/tests/test_maxcanon.py         |  152 ++
 third_party/pexpect/tests/test_misc.py             |  351 ++++
 third_party/pexpect/tests/test_missing_command.py  |   38 +
 third_party/pexpect/tests/test_performance.py      |  107 ++
 third_party/pexpect/tests/test_pickling.py         |   14 +
 third_party/pexpect/tests/test_pxssh.py            |   53 +
 third_party/pexpect/tests/test_replwrap.py         |   84 +
 third_party/pexpect/tests/test_repr.py             |   26 +
 third_party/pexpect/tests/test_run.py              |  117 ++
 third_party/pexpect/tests/test_run_out_of_pty.py   |   51 +
 third_party/pexpect/tests/test_screen.py           |  287 +++
 third_party/pexpect/tests/test_timeout_pattern.py  |   92 +
 third_party/pexpect/tests/test_unicode.py          |  179 ++
 third_party/pexpect/tests/test_which.py            |  269 +++
 third_party/pexpect/tests/test_winsize.py          |   67 +
 third_party/pexpect/tests/tetris.data              |    3 +
 third_party/pexpect/tests/ticker.py                |   28 +
 third_party/pexpect/tests/torturet.vt              |   61 +
 third_party/pexpect/tests/utils.py                 |    7 +
 third_party/pexpect/tools/display-sighandlers.py   |   20 +
 third_party/pexpect/tools/display-terminalinfo.py  |  209 +++
 .../pexpect/tools/teamcity-coverage-report.sh      |   27 +
 third_party/pexpect/tools/teamcity-runtests.sh     |   61 +
 third_party/zlib/ChangeLog                         |  619 ++++++-
 third_party/zlib/FAQ                               |  267 +--
 third_party/zlib/INDEX                             |   41 +-
 third_party/zlib/Makefile                          |  157 +-
 third_party/zlib/Makefile.in                       |  264 ++-
 third_party/zlib/README                            |   94 +-
 third_party/zlib/adler32.c                         |   99 +-
 third_party/zlib/algorithm.txt                     |  209 ---
 third_party/zlib/amiga/Makefile.pup                |    9 +-
 third_party/zlib/amiga/Makefile.sas                |    9 +-
 third_party/zlib/as400/bndsrc                      |   83 +
 third_party/zlib/as400/compile.clp                 |   95 +-
 third_party/zlib/as400/readme.txt                  |   14 +-
 third_party/zlib/as400/zlib.inc                    |  160 +-
 third_party/zlib/compress.c                        |   10 +-
 third_party/zlib/contrib/README.contrib            |   23 +-
 third_party/zlib/contrib/ada/zlib-streams.ads      |    6 +-
 third_party/zlib/contrib/ada/zlib-thin.ads         |    2 +-
 third_party/zlib/contrib/asm586/README.586         |   43 -
 third_party/zlib/contrib/asm586/match.S            |  364 ----
 third_party/zlib/contrib/asm686/README.686         |   17 +
 third_party/zlib/contrib/asm686/match.S            |   40 +-
 third_party/zlib/contrib/blast/blast.c             |    8 +-
 third_party/zlib/contrib/blast/blast.h             |    8 +-
 third_party/zlib/contrib/delphi/ZLib.pas           |    2 +-
 third_party/zlib/contrib/delphi/zlibd32.mak        |   20 +-
 third_party/zlib/contrib/dotzlib/DotZLib.build     |    4 +-
 third_party/zlib/contrib/dotzlib/DotZLib.chm       |  Bin 72728 -> 72726 bytes
 .../zlib/contrib/dotzlib/DotZLib/AssemblyInfo.cs   |   20 +-
 .../zlib/contrib/dotzlib/DotZLib/ChecksumImpl.cs   |   10 +-
 .../zlib/contrib/dotzlib/DotZLib/CircularBuffer.cs |    4 +-
 .../zlib/contrib/dotzlib/DotZLib/CodecBase.cs      |    8 +-
 .../zlib/contrib/dotzlib/DotZLib/Deflater.cs       |    6 +-
 .../zlib/contrib/dotzlib/DotZLib/DotZLib.cs        |   20 +-
 .../zlib/contrib/dotzlib/DotZLib/GZipStream.cs     |   18 +-
 .../zlib/contrib/dotzlib/DotZLib/Inflater.cs       |    6 +-
 .../zlib/contrib/dotzlib/DotZLib/UnitTests.cs      |   10 +-
 third_party/zlib/contrib/dotzlib/readme.txt        |   18 +-
 third_party/zlib/contrib/infback9/infback9.c       |   95 +-
 third_party/zlib/contrib/infback9/inftree9.c       |   21 +-
 third_party/zlib/contrib/infback9/inftree9.h       |   24 +-
 third_party/zlib/contrib/inflate86/inffas86.c      |    2 +-
 third_party/zlib/contrib/masm686/match.asm         |  413 -----
 third_party/zlib/contrib/masmx64/gvmat64.asm       |   52 +-
 third_party/zlib/contrib/masmx64/inffas8664.c      |    6 +-
 third_party/zlib/contrib/masmx64/inffasx64.asm     |   12 +-
 third_party/zlib/contrib/masmx64/readme.txt        |    7 +-
 third_party/zlib/contrib/masmx86/bld_ml32.bat      |    4 +-
 third_party/zlib/contrib/masmx86/gvmat32.asm       |  972 -----------
 third_party/zlib/contrib/masmx86/gvmat32c.c        |   62 -
 third_party/zlib/contrib/masmx86/inffas32.asm      |   21 +-
 third_party/zlib/contrib/masmx86/mkasm.bat         |    3 -
 third_party/zlib/contrib/masmx86/readme.txt        |   48 +-
 third_party/zlib/contrib/minizip/ChangeLogUnzip    |   67 -
 third_party/zlib/contrib/minizip/crypt.h           |   23 +-
 third_party/zlib/contrib/minizip/ioapi.c           |  228 ++-
 third_party/zlib/contrib/minizip/ioapi.h           |  189 +-
 third_party/zlib/contrib/minizip/iowin32.c         |  391 +++--
 third_party/zlib/contrib/minizip/iowin32.h         |   15 +-
 third_party/zlib/contrib/minizip/miniunz.c         |  173 +-
 third_party/zlib/contrib/minizip/minizip.c         |  158 +-
 third_party/zlib/contrib/minizip/mztools.c         |   54 +-
 third_party/zlib/contrib/minizip/mztools.h         |   14 +-
 third_party/zlib/contrib/minizip/unzip.c           | 1305 +++++++++-----
 third_party/zlib/contrib/minizip/unzip.h           |  137 +-
 third_party/zlib/contrib/minizip/zip.c             | 1844 ++++++++++++++------
 third_party/zlib/contrib/minizip/zip.h             |  175 +-
 third_party/zlib/contrib/pascal/zlibd32.mak        |   20 +-
 third_party/zlib/contrib/pascal/zlibpas.pas        |   42 +-
 third_party/zlib/contrib/puff/Makefile             |   40 +-
 third_party/zlib/contrib/puff/puff.c               |  209 +--
 third_party/zlib/contrib/puff/puff.h               |   10 +-
 third_party/zlib/contrib/puff/zeros.raw            |  Bin 1213 -> 2517 bytes
 third_party/zlib/contrib/testzlib/testzlib.c       |   10 +-
 third_party/zlib/contrib/vstudio/readme.txt        |   42 +-
 .../zlib/contrib/vstudio/vc7/miniunz.vcproj        |  126 --
 .../zlib/contrib/vstudio/vc7/minizip.vcproj        |  126 --
 .../zlib/contrib/vstudio/vc7/testzlib.vcproj       |  126 --
 third_party/zlib/contrib/vstudio/vc7/zlib.rc       |   32 -
 .../zlib/contrib/vstudio/vc7/zlibstat.vcproj       |  246 ---
 third_party/zlib/contrib/vstudio/vc7/zlibvc.def    |   92 -
 third_party/zlib/contrib/vstudio/vc7/zlibvc.sln    |   78 -
 third_party/zlib/contrib/vstudio/vc7/zlibvc.vcproj |  445 -----
 .../zlib/contrib/vstudio/vc8/miniunz.vcproj        |  566 ------
 .../zlib/contrib/vstudio/vc8/minizip.vcproj        |  563 ------
 .../zlib/contrib/vstudio/vc8/testzlib.vcproj       |  948 ----------
 .../zlib/contrib/vstudio/vc8/testzlibdll.vcproj    |  567 ------
 third_party/zlib/contrib/vstudio/vc8/zlib.rc       |   32 -
 .../zlib/contrib/vstudio/vc8/zlibstat.vcproj       |  870 ---------
 third_party/zlib/contrib/vstudio/vc8/zlibvc.def    |   92 -
 third_party/zlib/contrib/vstudio/vc8/zlibvc.sln    |  144 --
 third_party/zlib/contrib/vstudio/vc8/zlibvc.vcproj | 1219 -------------
 third_party/zlib/crc32.c                           |  116 +-
 third_party/zlib/crc32.h                           |    2 +-
 third_party/zlib/deflate.c                         |  523 ++++--
 third_party/zlib/deflate.h                         |   43 +-
 third_party/zlib/example.c                         |  565 ------
 third_party/zlib/examples/README.examples          |   15 +-
 third_party/zlib/examples/fitblk.c                 |    2 +-
 third_party/zlib/examples/gun.c                    |   51 +-
 third_party/zlib/examples/gzappend.c               |   22 +-
 third_party/zlib/examples/gzjoin.c                 |   13 +-
 third_party/zlib/examples/gzlog.c                  | 1304 ++++++++++----
 third_party/zlib/examples/gzlog.h                  |   95 +-
 third_party/zlib/examples/zlib_how.html            |   36 +-
 third_party/zlib/examples/zpipe.c                  |   24 +-
 third_party/zlib/examples/zran.c                   |   13 +-
 third_party/zlib/gzio.c                            | 1024 -----------
 third_party/zlib/infback.c                         |  153 +-
 third_party/zlib/inffast.c                         |   96 +-
 third_party/zlib/inffast.h                         |    4 +-
 third_party/zlib/inffixed.h                        |    6 +-
 third_party/zlib/inflate.c                         |  490 ++++--
 third_party/zlib/inflate.h                         |   33 +-
 third_party/zlib/inftrees.c                        |   93 +-
 third_party/zlib/inftrees.h                        |   27 +-
 third_party/zlib/make_vms.com                      |  748 ++++++--
 third_party/zlib/minigzip.c                        |  322 ----
 third_party/zlib/msdos/Makefile.bor                |   20 +-
 third_party/zlib/msdos/Makefile.dj2                |    4 +-
 third_party/zlib/msdos/Makefile.emx                |    4 +-
 third_party/zlib/msdos/Makefile.msc                |   16 +-
 third_party/zlib/msdos/Makefile.tc                 |   20 +-
 third_party/zlib/old/zlib.html                     |  971 -----------
 third_party/zlib/projects/README.projects          |   41 -
 third_party/zlib/projects/visualc6/README.txt      |   73 -
 third_party/zlib/projects/visualc6/example.dsp     |  278 ---
 third_party/zlib/projects/visualc6/minigzip.dsp    |  278 ---
 third_party/zlib/projects/visualc6/zlib.dsp        |  609 -------
 third_party/zlib/projects/visualc6/zlib.dsw        |   59 -
 third_party/zlib/qnx/package.qpg                   |   10 +-
 third_party/zlib/trees.c                           |  137 +-
 third_party/zlib/trees.h                           |    4 +-
 third_party/zlib/uncompr.c                         |    9 +-
 third_party/zlib/win32/DLL_FAQ.txt                 |    8 +-
 third_party/zlib/win32/Makefile.bor                |   25 +-
 third_party/zlib/win32/Makefile.emx                |   69 -
 third_party/zlib/win32/Makefile.gcc                |  121 +-
 third_party/zlib/win32/Makefile.msc                |  123 +-
 third_party/zlib/win32/zlib.def                    |   32 +-
 third_party/zlib/win32/zlib1.rc                    |   17 +-
 third_party/zlib/zconf.h                           |  292 +++-
 third_party/zlib/zconf.in.h                        |  332 ----
 third_party/zlib/zlib.h                            | 1392 +++++++++------
 third_party/zlib/zutil.c                           |   56 +-
 third_party/zlib/zutil.h                           |  140 +-
 374 files changed, 31279 insertions(+), 19224 deletions(-)
 mode change 100644 => 100755 lib/mimeparse/mimeparse.py
 delete mode 100644 lib/subunit/python/subunit/tests/TestUtil.py
 delete mode 100755 lib/subunit/runtests.py
 delete mode 100644 lib/testtools/README
 create mode 100644 third_party/pexpect/.gitignore
 create mode 100644 third_party/pexpect/.travis.yml
 create mode 100644 third_party/pexpect/ANSI.py
 create mode 100644 third_party/pexpect/DEVELOPERS.rst
 create mode 100644 third_party/pexpect/FSM.py
 create mode 100644 third_party/pexpect/LICENSE
 create mode 100644 third_party/pexpect/MANIFEST.in
 create mode 100644 third_party/pexpect/README.rst
 create mode 100644 third_party/pexpect/doc/FAQ.rst
 create mode 100644 third_party/pexpect/doc/Makefile
 create mode 100644 third_party/pexpect/doc/api/ANSI.rst
 create mode 100644 third_party/pexpect/doc/api/fdpexpect.rst
 create mode 100644 third_party/pexpect/doc/api/index.rst
 create mode 100644 third_party/pexpect/doc/api/pexpect.rst
 create mode 100644 third_party/pexpect/doc/api/pxssh.rst
 create mode 100644 third_party/pexpect/doc/api/replwrap.rst
 create mode 100644 third_party/pexpect/doc/api/screen.rst
 create mode 100644 third_party/pexpect/doc/clean.css
 create mode 100644 third_party/pexpect/doc/commonissues.rst
 create mode 100644 third_party/pexpect/doc/conf.py
 create mode 100644 third_party/pexpect/doc/examples.rst
 create mode 100644 third_party/pexpect/doc/history.rst
 create mode 100644 third_party/pexpect/doc/index.rst
 create mode 100644 third_party/pexpect/doc/install.rst
 create mode 100644 third_party/pexpect/doc/make.bat
 create mode 100644 third_party/pexpect/doc/overview.rst
 create mode 100644 third_party/pexpect/doc/requirements.txt
 create mode 100644 third_party/pexpect/doc/sphinxext/github.py
 create mode 100644 third_party/pexpect/examples/README
 create mode 100755 third_party/pexpect/examples/astat.py
 create mode 100755 third_party/pexpect/examples/cgishell.cgi
 create mode 100755 third_party/pexpect/examples/chess.py
 create mode 100755 third_party/pexpect/examples/chess2.py
 create mode 100755 third_party/pexpect/examples/chess3.py
 create mode 100755 third_party/pexpect/examples/df.py
 create mode 100755 third_party/pexpect/examples/ftp.py
 create mode 100755 third_party/pexpect/examples/hive.py
 create mode 100755 third_party/pexpect/examples/monitor.py
 create mode 100755 third_party/pexpect/examples/passmass.py
 create mode 100755 third_party/pexpect/examples/python.py
 create mode 100755 third_party/pexpect/examples/script.py
 create mode 100755 third_party/pexpect/examples/ssh_tunnel.py
 create mode 100644 third_party/pexpect/examples/table_test.html
 create mode 100755 third_party/pexpect/examples/topip.py
 create mode 100755 third_party/pexpect/examples/uptime.py
 create mode 100644 third_party/pexpect/fdpexpect.py
 create mode 100644 third_party/pexpect/notes/my_forkpty.py
 create mode 100644 third_party/pexpect/notes/notes.txt
 create mode 100644 third_party/pexpect/notes/posixmodule.c.diff
 create mode 100644 third_party/pexpect/pexpect/ANSI.py
 create mode 100644 third_party/pexpect/pexpect/FSM.py
 create mode 100644 third_party/pexpect/pexpect/__init__.py
 create mode 100644 third_party/pexpect/pexpect/async.py
 create mode 100644 third_party/pexpect/pexpect/bashrc.sh
 create mode 100644 third_party/pexpect/pexpect/exceptions.py
 create mode 100644 third_party/pexpect/pexpect/expect.py
 create mode 100644 third_party/pexpect/pexpect/fdpexpect.py
 create mode 100644 third_party/pexpect/pexpect/pty_spawn.py
 create mode 100644 third_party/pexpect/pexpect/pxssh.py
 create mode 100644 third_party/pexpect/pexpect/replwrap.py
 create mode 100644 third_party/pexpect/pexpect/screen.py
 create mode 100644 third_party/pexpect/pexpect/spawnbase.py
 create mode 100644 third_party/pexpect/pexpect/utils.py
 create mode 100644 third_party/pexpect/pxssh.py
 create mode 100644 third_party/pexpect/screen.py
 create mode 100644 third_party/pexpect/setup.cfg
 create mode 100644 third_party/pexpect/setup.py
 create mode 100644 third_party/pexpect/tests/PexpectTestCase.py
 create mode 100644 third_party/pexpect/tests/README
 create mode 100644 third_party/pexpect/tests/TESTDATA.txt
 create mode 100755 third_party/pexpect/tests/__init__.py
 create mode 100755 third_party/pexpect/tests/adhoc.py
 create mode 100644 third_party/pexpect/tests/alarm_die.py
 create mode 100644 third_party/pexpect/tests/bambi.vt
 create mode 100755 third_party/pexpect/tests/depricated_test_filedescriptor.py
 create mode 100644 third_party/pexpect/tests/echo_w_prompt.py
 create mode 100755 third_party/pexpect/tests/echo_wait.py
 create mode 100755 third_party/pexpect/tests/exit1.py
 create mode 100644 third_party/pexpect/tests/exit667.c
 create mode 100755 third_party/pexpect/tests/fakessh/ssh
 create mode 100755 third_party/pexpect/tests/getch.py
 create mode 100644 third_party/pexpect/tests/globe.vt
 create mode 100755 third_party/pexpect/tests/interact.py
 create mode 100644 third_party/pexpect/tests/interact_unicode.py
 create mode 100644 third_party/pexpect/tests/list100.py
 create mode 100755 third_party/pexpect/tests/needs_kill.py
 create mode 100755 third_party/pexpect/tests/pexpectTest.py
 create mode 100644 third_party/pexpect/tests/platform_checks/CSIGNALTEST/test.c
 create mode 100644 third_party/pexpect/tests/platform_checks/README
 create mode 100755 third_party/pexpect/tests/platform_checks/check.py
 create mode 100755 third_party/pexpect/tests/platform_checks/check2.py
 create mode 100755 third_party/pexpect/tests/platform_checks/check_control_terminal.py
 create mode 100755 third_party/pexpect/tests/platform_checks/check_handler.py
 create mode 100755 third_party/pexpect/tests/platform_checks/check_read.py
 create mode 100755 third_party/pexpect/tests/platform_checks/check_signals.py
 create mode 100755 third_party/pexpect/tests/qa.py
 create mode 100755 third_party/pexpect/tests/sigwinch_report.py
 create mode 100755 third_party/pexpect/tests/sleep_for.py
 create mode 100755 third_party/pexpect/tests/swapcase_echo.py
 create mode 100644 third_party/pexpect/tests/test_FSM.py
 create mode 100755 third_party/pexpect/tests/test_ansi.py
 create mode 100644 third_party/pexpect/tests/test_async.py
 create mode 100755 third_party/pexpect/tests/test_command_list_split.py
 create mode 100755 third_party/pexpect/tests/test_constructor.py
 create mode 100755 third_party/pexpect/tests/test_ctrl_chars.py
 create mode 100755 third_party/pexpect/tests/test_destructor.py
 create mode 100755 third_party/pexpect/tests/test_dotall.py
 create mode 100755 third_party/pexpect/tests/test_expect.py
 create mode 100755 third_party/pexpect/tests/test_filedescriptor.py
 create mode 100755 third_party/pexpect/tests/test_interact.py
 create mode 100755 third_party/pexpect/tests/test_isalive.py
 create mode 100755 third_party/pexpect/tests/test_log.py
 create mode 100644 third_party/pexpect/tests/test_maxcanon.py
 create mode 100755 third_party/pexpect/tests/test_misc.py
 create mode 100755 third_party/pexpect/tests/test_missing_command.py
 create mode 100755 third_party/pexpect/tests/test_performance.py
 create mode 100644 third_party/pexpect/tests/test_pickling.py
 create mode 100644 third_party/pexpect/tests/test_pxssh.py
 create mode 100644 third_party/pexpect/tests/test_replwrap.py
 create mode 100644 third_party/pexpect/tests/test_repr.py
 create mode 100755 third_party/pexpect/tests/test_run.py
 create mode 100755 third_party/pexpect/tests/test_run_out_of_pty.py
 create mode 100755 third_party/pexpect/tests/test_screen.py
 create mode 100755 third_party/pexpect/tests/test_timeout_pattern.py
 create mode 100644 third_party/pexpect/tests/test_unicode.py
 create mode 100644 third_party/pexpect/tests/test_which.py
 create mode 100755 third_party/pexpect/tests/test_winsize.py
 create mode 100644 third_party/pexpect/tests/tetris.data
 create mode 100755 third_party/pexpect/tests/ticker.py
 create mode 100644 third_party/pexpect/tests/torturet.vt
 create mode 100644 third_party/pexpect/tests/utils.py
 create mode 100755 third_party/pexpect/tools/display-sighandlers.py
 create mode 100755 third_party/pexpect/tools/display-terminalinfo.py
 create mode 100755 third_party/pexpect/tools/teamcity-coverage-report.sh
 create mode 100755 third_party/pexpect/tools/teamcity-runtests.sh
 delete mode 100644 third_party/zlib/algorithm.txt
 delete mode 100644 third_party/zlib/contrib/asm586/README.586
 delete mode 100644 third_party/zlib/contrib/asm586/match.S
 delete mode 100644 third_party/zlib/contrib/masm686/match.asm
 delete mode 100644 third_party/zlib/contrib/masmx86/gvmat32.asm
 delete mode 100644 third_party/zlib/contrib/masmx86/gvmat32c.c
 delete mode 100755 third_party/zlib/contrib/masmx86/mkasm.bat
 delete mode 100644 third_party/zlib/contrib/minizip/ChangeLogUnzip
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/miniunz.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/minizip.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/testzlib.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/zlib.rc
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/zlibstat.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/zlibvc.def
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/zlibvc.sln
 delete mode 100644 third_party/zlib/contrib/vstudio/vc7/zlibvc.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/miniunz.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/minizip.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/testzlib.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/testzlibdll.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/zlib.rc
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/zlibstat.vcproj
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/zlibvc.def
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/zlibvc.sln
 delete mode 100644 third_party/zlib/contrib/vstudio/vc8/zlibvc.vcproj
 delete mode 100644 third_party/zlib/example.c
 delete mode 100644 third_party/zlib/gzio.c
 delete mode 100644 third_party/zlib/minigzip.c
 delete mode 100644 third_party/zlib/old/zlib.html
 delete mode 100644 third_party/zlib/projects/README.projects
 delete mode 100644 third_party/zlib/projects/visualc6/README.txt
 delete mode 100644 third_party/zlib/projects/visualc6/example.dsp
 delete mode 100644 third_party/zlib/projects/visualc6/minigzip.dsp
 delete mode 100644 third_party/zlib/projects/visualc6/zlib.dsp
 delete mode 100644 third_party/zlib/projects/visualc6/zlib.dsw
 delete mode 100644 third_party/zlib/win32/Makefile.emx
 delete mode 100644 third_party/zlib/zconf.in.h

diff --git a/lib/extras/extras/__init__.py b/lib/extras/extras/__init__.py
index 5f16625..2d34b52 100644
--- a/lib/extras/extras/__init__.py
+++ b/lib/extras/extras/__init__.py
@@ -101,3 +101,5 @@ def safe_hasattr(obj, attr, _marker=object()):
     properties.
     """
     return getattr(obj, attr, _marker) is not _marker
+
+
diff --git a/lib/extras/extras/tests/test_extras.py b/lib/extras/extras/tests/test_extras.py
index 33dc87c..be1ed1c 100644
--- a/lib/extras/extras/tests/test_extras.py
+++ b/lib/extras/extras/tests/test_extras.py
@@ -184,3 +184,5 @@ class TestTryImports(TestCase):
         check_error_callback(self, try_imports,
             ['os.path'],
             0, True)
+
+
diff --git a/lib/mimeparse/mimeparse.py b/lib/mimeparse/mimeparse.py
old mode 100644
new mode 100755
diff --git a/lib/mimeparse/setup.py b/lib/mimeparse/setup.py
index de3e81b..e559f9d 100644
--- a/lib/mimeparse/setup.py
+++ b/lib/mimeparse/setup.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-#old way
+#old way 
 from distutils.core import setup
 
 #new way
@@ -11,7 +11,7 @@ setup(name='mimeparse',
       description='A module provides basic functions for parsing mime-type names and matching them against a list of media-ranges.',
       long_description="""
 This module provides basic functions for handling mime-types. It can handle
-matching mime-types against a list of media-ranges. See section 14.1 of
+matching mime-types against a list of media-ranges. See section 14.1 of 
 the HTTP specification [RFC 2616] for a complete explanation.
 
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
@@ -48,3 +48,4 @@ Contents:
       license='MIT',
       py_modules=['mimeparse']
       )
+
diff --git a/lib/pep8/pep8.py b/lib/pep8/pep8.py
index b31a978..67b32d1 100755
--- a/lib/pep8/pep8.py
+++ b/lib/pep8/pep8.py
@@ -2,6 +2,7 @@
 # pep8.py - Check Python source code formatting, according to PEP 8
 # Copyright (C) 2006-2009 Johann C. Rocholl <johann at rocholl.net>
 # Copyright (C) 2009-2014 Florent Xicluna <florent.xicluna at gmail.com>
+# Copyright (C) 2014 Ian Lee <ianlee1521 at gmail.com>
 #
 # Permission is hereby granted, free of charge, to any person
 # obtaining a copy of this software and associated documentation files
@@ -46,8 +47,6 @@ W warnings
 """
 from __future__ import with_statement
 
-__version__ = '1.6.0a0'
-
 import os
 import sys
 import re
@@ -63,13 +62,19 @@ try:
 except ImportError:
     from ConfigParser import RawConfigParser
 
-DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__'
-DEFAULT_IGNORE = 'E123,E226,E24,E704'
-if sys.platform == 'win32':
-    DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8')
-else:
-    DEFAULT_CONFIG = os.path.join(os.getenv('XDG_CONFIG_HOME') or
-                                  os.path.expanduser('~/.config'), 'pep8')
+__version__ = '1.6.0a0'
+
+DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__,.tox'
+DEFAULT_IGNORE = 'E121,E123,E126,E226,E24,E704'
+try:
+    if sys.platform == 'win32':
+        DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8')
+    else:
+        DEFAULT_CONFIG = os.path.join(os.getenv('XDG_CONFIG_HOME') or
+                                      os.path.expanduser('~/.config'), 'pep8')
+except ImportError:
+    DEFAULT_CONFIG = None
+
 PROJECT_CONFIG = ('setup.cfg', 'tox.ini', '.pep8')
 TESTSUITE_PATH = os.path.join(os.path.dirname(__file__), 'testsuite')
 MAX_LINE_LENGTH = 79
@@ -101,8 +106,9 @@ ERRORCODE_REGEX = re.compile(r'\b[A-Z]\d{3}\b')
 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
 EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
 WHITESPACE_AFTER_COMMA_REGEX = re.compile(r'[,;:]\s*(?:  |\t)')
-COMPARE_SINGLETON_REGEX = re.compile(r'([=!]=)\s*(None|False|True)')
-COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+[^[({ ]+\s+(in|is)\s')
+COMPARE_SINGLETON_REGEX = re.compile(r'\b(None|False|True)?\s*([=!]=)'
+                                     r'\s*(?(1)|(None|False|True))\b')
+COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+[^][)(}{ ]+\s+(in|is)\s')
 COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
                                 r'|\s*\(\s*([^)]*[^ )])\s*\))')
 KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
@@ -680,7 +686,7 @@ def missing_whitespace_around_operator(logical_line, tokens):
                 if need_space is True or need_space[1]:
                     # A needed trailing space was not found
                     yield prev_end, "E225 missing whitespace around operator"
-                else:
+                elif prev_text != '**':
                     code, optype = 'E226', 'arithmetic'
                     if prev_text == '%':
                         code, optype = 'E228', 'modulo'
@@ -748,6 +754,7 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
     Okay: boolean(a != b)
     Okay: boolean(a <= b)
     Okay: boolean(a >= b)
+    Okay: def foo(arg: int = 42):
 
     E251: def complex(real, imag = 0.0):
     E251: return magic(r = real, i = imag)
@@ -755,6 +762,8 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
     parens = 0
     no_space = False
     prev_end = None
+    annotated_func_arg = False
+    in_def = logical_line.startswith('def')
     message = "E251 unexpected spaces around keyword / parameter equals"
     for token_type, text, start, end, line in tokens:
         if token_type == tokenize.NL:
@@ -763,15 +772,22 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
             no_space = False
             if start != prev_end:
                 yield (prev_end, message)
-        elif token_type == tokenize.OP:
+        if token_type == tokenize.OP:
             if text == '(':
                 parens += 1
             elif text == ')':
                 parens -= 1
-            elif parens and text == '=':
+            elif in_def and text == ':' and parens == 1:
+                annotated_func_arg = True
+            elif parens and text == ',' and parens == 1:
+                annotated_func_arg = False
+            elif parens and text == '=' and not annotated_func_arg:
                 no_space = True
                 if start != prev_end:
                     yield (prev_end, message)
+            if not parens:
+                annotated_func_arg = False
+
         prev_end = end
 
 
@@ -835,6 +851,56 @@ def imports_on_separate_lines(logical_line):
             yield found, "E401 multiple imports on one line"
 
 
+def module_imports_on_top_of_file(
+        logical_line, indent_level, checker_state, noqa):
+    r"""Imports are always put at the top of the file, just after any module
+    comments and docstrings, and before module globals and constants.
+
+    Okay: import os
+    Okay: # this is a comment\nimport os
+    Okay: '''this is a module docstring'''\nimport os
+    Okay: r'''this is a module docstring'''\nimport os
+    Okay: try:\n    import x\nexcept:\n    pass\nelse:\n    pass\nimport y
+    Okay: try:\n    import x\nexcept:\n    pass\nfinally:\n    pass\nimport y
+    E402: a=1\nimport os
+    E402: 'One string'\n"Two string"\nimport os
+    E402: a=1\nfrom sys import x
+
+    Okay: if x:\n    import os
+    """
+    def is_string_literal(line):
+        if line[0] in 'uUbB':
+            line = line[1:]
+        if line and line[0] in 'rR':
+            line = line[1:]
+        return line and (line[0] == '"' or line[0] == "'")
+
+    allowed_try_keywords = ('try', 'except', 'else', 'finally')
+
+    if indent_level:  # Allow imports in conditional statements or functions
+        return
+    if not logical_line:  # Allow empty lines or comments
+        return
+    if noqa:
+        return
+    line = logical_line
+    if line.startswith('import ') or line.startswith('from '):
+        if checker_state.get('seen_non_imports', False):
+            yield 0, "E402 module level import not at top of file"
+    elif any(line.startswith(kw) for kw in allowed_try_keywords):
+        # Allow try, except, else, finally keywords intermixed with imports in
+        # order to support conditional importing
+        return
+    elif is_string_literal(line):
+        # The first literal is a docstring, allow it. Otherwise, report error.
+        if checker_state.get('seen_docstring', False):
+            checker_state['seen_non_imports'] = True
+        else:
+            checker_state['seen_docstring'] = True
+    else:
+        checker_state['seen_non_imports'] = True
+
+
 def compound_statements(logical_line):
     r"""Compound statements (on the same line) are generally discouraged.
 
@@ -871,8 +937,12 @@ def compound_statements(logical_line):
         if ((before.count('{') <= before.count('}') and   # {'a': 1} (dict)
              before.count('[') <= before.count(']') and   # [1:2] (slice)
              before.count('(') <= before.count(')'))):    # (annotation)
-            if LAMBDA_REGEX.search(before):
-                yield 0, "E731 do not assign a lambda expression, use a def"
+            lambda_kw = LAMBDA_REGEX.search(before)
+            if lambda_kw:
+                before = line[:lambda_kw.start()].rstrip()
+                if before[-1:] == '=' and isidentifier(before[:-1].strip()):
+                    yield 0, ("E731 do not assign a lambda expression, use a "
+                              "def")
                 break
             if before.startswith('def '):
                 yield 0, "E704 multiple statements on one line (def)"
@@ -930,7 +1000,9 @@ def comparison_to_singleton(logical_line, noqa):
 
     Okay: if arg is not None:
     E711: if arg != None:
+    E711: if None == arg:
     E712: if arg == True:
+    E712: if False == arg:
 
     Also, beware of writing if x when you really mean if x is not None --
     e.g. when testing whether a variable or argument that defaults to None was
@@ -939,8 +1011,9 @@ def comparison_to_singleton(logical_line, noqa):
     """
     match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line)
     if match:
-        same = (match.group(1) == '==')
-        singleton = match.group(2)
+        singleton = match.group(1) or match.group(3)
+        same = (match.group(2) == '==')
+
         msg = "'if cond is %s:'" % (('' if same else 'not ') + singleton)
         if singleton in ('None',):
             code = 'E711'
@@ -949,7 +1022,7 @@ def comparison_to_singleton(logical_line, noqa):
             nonzero = ((singleton == 'True' and same) or
                        (singleton == 'False' and not same))
             msg += " or 'if %scond:'" % ('' if nonzero else 'not ')
-        yield match.start(1), ("%s comparison to %s should be %s" %
+        yield match.start(2), ("%s comparison to %s should be %s" %
                                (code, singleton, msg))
 
 
@@ -1056,7 +1129,7 @@ if '' == ''.encode():
         """Read the source code."""
         with open(filename, 'rU') as f:
             return f.readlines()
-    isidentifier = re.compile(r'[a-zA-Z_]\w*').match
+    isidentifier = re.compile(r'[a-zA-Z_]\w*$').match
     stdin_get_value = sys.stdin.read
 else:
     # Python 3
@@ -1155,10 +1228,13 @@ def normalize_paths(value, parent=os.curdir):
 
     Return a list of absolute paths.
     """
-    if not value or isinstance(value, list):
+    if not value:
+        return []
+    if isinstance(value, list):
         return value
     paths = []
     for path in value.split(','):
+        path = path.strip()
         if '/' in path:
             path = os.path.abspath(os.path.join(parent, path))
         paths.append(path.rstrip('/'))
@@ -1175,14 +1251,12 @@ def filename_match(filename, patterns, default=True):
     return any(fnmatch(filename, pattern) for pattern in patterns)
 
 
+def _is_eol_token(token):
+    return token[0] in NEWLINE or token[4][token[3][1]:].lstrip() == '\\\n'
 if COMMENT_WITH_NL:
-    def _is_eol_token(token):
-        return (token[0] in NEWLINE or
-                (token[0] == tokenize.COMMENT and token[1] == token[4]))
-else:
-    def _is_eol_token(token):
-        return token[0] in NEWLINE
-
+    def _is_eol_token(token, _eol_token=_is_eol_token):
+        return _eol_token(token) or (token[0] == tokenize.COMMENT and
+                                     token[1] == token[4])
 
 ##############################################################################
 # Framework to run all checks
@@ -1239,6 +1313,8 @@ class Checker(object):
         self.hang_closing = options.hang_closing
         self.verbose = options.verbose
         self.filename = filename
+        # Dictionary where a checker can store its custom state.
+        self._checker_states = {}
         if filename is None:
             self.filename = 'stdin'
             self.lines = lines or []
@@ -1294,10 +1370,16 @@ class Checker(object):
             arguments.append(getattr(self, name))
         return check(*arguments)
 
+    def init_checker_state(self, name, argument_names):
+        """ Prepares a custom state for the specific checker plugin."""
+        if 'checker_state' in argument_names:
+            self.checker_state = self._checker_states.setdefault(name, {})
+
     def check_physical(self, line):
         """Run all physical checks on a raw input line."""
         self.physical_line = line
         for name, check, argument_names in self._physical_checks:
+            self.init_checker_state(name, argument_names)
             result = self.run_check(check, argument_names)
             if result is not None:
                 (offset, text) = result
@@ -1342,6 +1424,10 @@ class Checker(object):
         """Build a line from tokens and run all logical checks on it."""
         self.report.increment_logical_line()
         mapping = self.build_tokens_line()
+
+        if not mapping:
+            return
+
         (start_row, start_col) = mapping[0][1]
         start_line = self.lines[start_row - 1]
         self.indent_level = expand_indent(start_line[:start_col])
@@ -1352,6 +1438,7 @@ class Checker(object):
         for name, check, argument_names in self._logical_checks:
             if self.verbose >= 4:
                 print('   ' + name)
+            self.init_checker_state(name, argument_names)
             for offset, text in self.run_check(check, argument_names) or ():
                 if not isinstance(offset, tuple):
                     for token_offset, pos in mapping:
@@ -1789,7 +1876,8 @@ def get_parser(prog='pep8', version=__version__):
     parser.add_option('--select', metavar='errors', default='',
                       help="select errors and warnings (e.g. E,W6)")
     parser.add_option('--ignore', metavar='errors', default='',
-                      help="skip errors and warnings (e.g. E4,W)")
+                      help="skip errors and warnings (e.g. E4,W) "
+                           "(default: %s)" % DEFAULT_IGNORE)
     parser.add_option('--show-source', action='store_true',
                       help="show source code for each error")
     parser.add_option('--show-pep8', action='store_true',
diff --git a/lib/subunit/INSTALL b/lib/subunit/INSTALL
index eeea734..29052eb 100644
--- a/lib/subunit/INSTALL
+++ b/lib/subunit/INSTALL
@@ -14,9 +14,12 @@ Dependencies
 * Python for the filters
 * 'testtools' (On Debian and Ubuntu systems the 'python-testtools' package,
   the testtools package on pypi, or https://launchpad.net/testtools) for
-  the extended test API which permits attachments. Version 0.9.23 or newer is 
+  the extended test API which permits attachments. Version 0.9.30 or newer is
   required. Of particular note, http://testtools.python-hosting.com/ is not
   the testtools you want.
+* 'testscenarios' (On Debian and Ubuntu systems the 'python-testscenarios'
+  package, the 'testscenarios' package on pypi, or
+  https://launchpad.net/testscenarios) for running some of the python unit tests.
 * A C compiler for the C bindings
 * Perl for the Perl tools (including subunit-diff)
 * Check to run the subunit test suite.
diff --git a/lib/subunit/MANIFEST.in b/lib/subunit/MANIFEST.in
index eb98981..4f521dc 100644
--- a/lib/subunit/MANIFEST.in
+++ b/lib/subunit/MANIFEST.in
@@ -1,4 +1,4 @@
-exclude .bzrignore
+exclude .gitignore
 exclude aclocal.m4
 prune autom4te.cache
 prune c
diff --git a/lib/subunit/Makefile.am b/lib/subunit/Makefile.am
index da16020..765ae40 100644
--- a/lib/subunit/Makefile.am
+++ b/lib/subunit/Makefile.am
@@ -1,11 +1,12 @@
 EXTRA_DIST =  \
-	.bzrignore \
+	.gitignore \
 	Apache-2.0 \
 	BSD \
 	INSTALL \
 	Makefile.am \
 	NEWS \
 	README \
+	all_tests.py \
 	c++/README \
 	c/README \
 	c/check-subunit-0.9.3.patch \
@@ -20,21 +21,22 @@ EXTRA_DIST =  \
 	python/iso8601/README.subunit \
 	python/iso8601/setup.py \
 	python/iso8601/test_iso8601.py \
-	python/subunit/tests/TestUtil.py \
 	python/subunit/tests/__init__.py \
 	python/subunit/tests/sample-script.py \
 	python/subunit/tests/sample-two-script.py \
 	python/subunit/tests/test_chunked.py \
 	python/subunit/tests/test_details.py \
+	python/subunit/tests/test_filters.py \
+	python/subunit/tests/test_output_filter.py \
 	python/subunit/tests/test_progress_model.py \
-	python/subunit/tests/test_subunit_filter.py \
 	python/subunit/tests/test_run.py \
+	python/subunit/tests/test_subunit_filter.py \
 	python/subunit/tests/test_subunit_stats.py \
 	python/subunit/tests/test_subunit_tags.py \
 	python/subunit/tests/test_tap2subunit.py \
 	python/subunit/tests/test_test_protocol.py \
+	python/subunit/tests/test_test_protocol2.py \
 	python/subunit/tests/test_test_results.py \
-	runtests.py \
 	setup.py \
 	shell/README \
 	shell/share/subunit.sh \
@@ -47,9 +49,12 @@ ACLOCAL_AMFLAGS = -I m4
 include_subunitdir = $(includedir)/subunit
 
 dist_bin_SCRIPTS = \
+	filters/subunit-1to2 \
+	filters/subunit-2to1 \
 	filters/subunit-filter \
 	filters/subunit-ls \
 	filters/subunit-notify \
+	filters/subunit-output \
 	filters/subunit-stats \
 	filters/subunit-tags \
 	filters/subunit2csv \
@@ -58,8 +63,7 @@ dist_bin_SCRIPTS = \
 	filters/subunit2pyunit \
 	filters/tap2subunit
 
-TESTS_ENVIRONMENT = SHELL_SHARE='$(top_srcdir)/shell/share/' PYTHONPATH='$(abs_top_srcdir)/python':${PYTHONPATH}
-TESTS = runtests.py $(check_PROGRAMS)
+TESTS = $(check_PROGRAMS)
 
 ## install libsubunit.pc
 pcdatadir = $(libdir)/pkgconfig
@@ -75,7 +79,9 @@ pkgpython_PYTHON = \
 	python/subunit/iso8601.py \
 	python/subunit/progress_model.py \
 	python/subunit/run.py \
-	python/subunit/test_results.py
+	python/subunit/v2.py \
+	python/subunit/test_results.py \
+	python/subunit/_output.py
 
 lib_LTLIBRARIES = libsubunit.la
 lib_LTLIBRARIES +=  libcppunit_subunit.la
@@ -87,9 +93,6 @@ include_subunit_HEADERS = \
 check_PROGRAMS = \
 	c/tests/test_child
 
-check_SCRIPTS = \
-	runtests.py
-
 libsubunit_la_SOURCES = \
 	c/lib/child.c \
 	c/include/subunit/child.h
@@ -108,6 +111,9 @@ all-local: perl/Makefile
 
 check-local: perl/Makefile
 	$(MAKE) -C perl check
+	SHELL_SHARE='$(top_srcdir)/shell/share/' \
+	PYTHONPATH='$(abs_top_srcdir)/python':'$(abs_top_srcdir)':${PYTHONPATH} \
+	$(PYTHON) -m testtools.run all_tests.test_suite
 
 clean-local:
 	find . -type f -name "*.pyc" -exec rm {} ';'
diff --git a/lib/subunit/NEWS b/lib/subunit/NEWS
index 081dc5d..f6221a8 100644
--- a/lib/subunit/NEWS
+++ b/lib/subunit/NEWS
@@ -5,6 +5,233 @@ subunit release notes
 NEXT (In development)
 ---------------------
 
+BUGFIXES
+~~~~~~~~
+
+* Unencapsulated strings and encapsulated stdout will be forwarded by
+  subunit-2to1. (Robert Collins, #1400519)
+
+* SUBUNIT_FORMATTER which has not been honoured for years is now removed from 
+  the codebase. (Jelmer Vernooij)
+
+1.0.0
+-----
+
+BUGFIXES
+~~~~~~~~
+
+* Tests have been fixed with testtools 1.2.0 and above.
+  (Robert Collins)
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* With testtools 1.4.0 and above import errors are now
+  shown in detail by ``subunit.run``. (Robert Collins)
+
+0.0.21
+------
+
+BUGFIXES
+~~~~~~~~
+
+* Brown bag bugfix - 0.0.20's setup.py referenced cvs not csv.
+  (Robert Collins, #1361924)
+
+0.0.20
+------
+
+BUGFIXES
+~~~~~~~~
+
+* subunit2csv is now installed when using pip.
+  (Robert Collins, #1279669)
+
+* testscenarios is now a test dependency, not an install dependency.
+  (Arfrever Frehtes Taifersar Arahesis, #1292757)
+
+* The python-subunit tarball can now have setup run from the current
+  directory. (Robert Collins, #1361857)
+
+0.0.19
+------
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* ``subunit.run`` in Python will now exit 0 as long as the test stream has
+  been generated correctly - this has always been the intent but API friction
+  with testtools had prevented it working.
+  (Robert Collins)
+
+0.0.18
+------
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* Fix compatibility with testtools 0.9.35 which dropped the 'all' compat
+  symbol. This breaks support for Python versions lower than 2.6.
+  (Robert Collins, #1274056)
+
+0.0.17
+------
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* Add ``subunit-output`` tool that can generate a Subunit v2 bytestream from
+  arguments passed on the command line. (Thomi Richards, #1252084)
+
+0.0.16
+------
+
+BUG FIXES
+~~~~~~~~~
+
+* Perl files should now honour perl system config.
+  (Benedikt Morbach, #1233198)
+
+* Python 3.1 and 3.2 have an inconsistent memoryview implementation which
+  required a workaround for NUL byte detection. (Robert Collins, #1216246)
+
+* The test suite was failing 6 tests due to testtools changing it's output
+  formatting of exceptions. (Robert Collins)
+
+* V2 parser errors now set appropriate mime types for the encapsulated packet
+  data and the error message. (Robert Collins)
+
+* When tests fail to import ``python subunit.run -l ...`` will now write a 
+  subunit file attachment listing the failed imports and exit 2, rather than
+  listing the stub objects from the importer and exiting 0.
+  (Robert Collins, #1245672)
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* Most filters will now accept a file path argument instead of only reading
+  from stdin. (Robert Collins, #409206)
+
+0.0.15
+------
+
+BUG FIXES
+~~~~~~~~~
+
+* Clients of subunit did not expect memoryview objects in StreamResult events.
+  (Robert Collins)
+
+* Memoryview and struct were mutually incompatible in 2.7.3 and 3.2.
+  (Robert Collins, #1216163)
+
+0.0.14
+------
+
+BUG FIXES
+~~~~~~~~~
+
+* Memoryview detection was broken and thus it's use was never really tested.
+  (Robert Collins, 1216101)
+
+* TestProtocol2's tag tests were set sort order dependent.
+  (Robert Collins, #1025392)
+
+* TestTestProtocols' test_tags_both was set sort order dependent.
+  (Robert Collins, #1025392)
+
+* TestTestProtocols' test_*_details were dictionary sort order dependent.
+  (Robert Collins, #1025392)
+
+* TestSubUnitTags's test_add_tag was also se sort order dependent.
+  (Robert Collins, #1025392)
+
+0.0.13
+------
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* subunit should now build with automake 1.11 again. (Robert Collins)
+
+* `subunit-stats` no longer outputs encapsulated stdout as subunit.
+  (Robert Collins, #1171987)
+
+* The logic for `subunit.run` is now importable via python -
+  `subunit.run.main`. (Robert Collins, #606770)
+
+BUG FIXES
+~~~~~~~~~
+
+* Removed GPL files that were (C) non Subunit Developers - they are
+  incompatible for binary distribution, which affects redistributors.
+  (Robert Collins, #1185591)
+
+0.0.12
+------
+
+BUG FIXES
+~~~~~~~~~
+
+* Subunit v2 packets with both file content and route code were not being
+  parsed correctly - they would incorrectly emit a parser error, due to trying
+  to parse the route code length from the first byes of the file content.
+  (Robert Collins, 1172815)
+
+0.0.11
+------
+
+v2 protocol draft included in this release. The v2 protocol trades off human
+readability for a massive improvement in robustness, the ability to represent
+concurrent tests in a single stream, cheaper parsing, and that provides
+significantly better in-line debugging support and structured forwarding
+of non-test data (such as stdout or stdin data).
+
+This change includes two new filters (subunit-1to2 and subunit-2to1). Use
+these filters to convert old streams to v2 and convert v2 streams to v1.
+
+All the other filters now only parse and emit v2 streams. V2 is still in
+draft format, so if you want to delay and wait for v2 to be finalised, you
+should use subunit-2to1 before any serialisation steps take place.
+With the ability to encapsulate multiple non-test streams, another significant
+cange is that filters which emit subunit now encapsulate any non-subunit they
+encounter, labelling it 'stdout'. This permits multiplexing such streams and
+detangling the stdout streams from each input.
+
+The subunit libraries (Python etc) have not changed their behaviour: they
+still emit v1 from their existing API calls. New API's are being added
+and applications should migrate once their language has those API's available.
+
+IMPROVEMENTS
+~~~~~~~~~~~~
+
+* ``subunit.run`` now replaces sys.stdout to ensure that stdout is unbuffered
+  - without this pdb output is not reliably visible when stdout is a pipe
+  as it usually is. (Robert Collins)
+
+* v2 protocol draft included in this release. (Python implementation only so
+  far). (Robert Collins)
+
+* Two new Python classes -- ``StreamResultToBytes`` and
+  ``ByteStreamToStreamResult`` handle v2 generation and parsing.
+  (Robert Collins)
+
+0.0.10
+------
+
+BUG FIXES
+~~~~~~~~~
+
+* make_stream_binary is now public for reuse. (Robert Collins)
+
+* NAME was not defined in the protocol BNF. (Robert Collins)
+
+* UnsupportedOperation is available in the Python2.6 io library, so ask
+  forgiveness rather than permission for obtaining it. (Robert Collins)
+
+* Streams with no fileno() attribute are now supported, but they are not
+  checked for being in binary mode: be sure to take care of that if using
+  the library yourself. (Robert Collins)
+
 0.0.9
 -----
 
diff --git a/lib/subunit/README b/lib/subunit/README
index 47a9734..76a7418 100644
--- a/lib/subunit/README
+++ b/lib/subunit/README
@@ -1,12 +1,12 @@
 
   subunit: A streaming protocol for test results
-  Copyright (C) 2005-2009 Robert Collins <robertc at robertcollins.net>
+  Copyright (C) 2005-2013 Robert Collins <robertc at robertcollins.net>
 
   Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
   license at the users choice. A copy of both licenses are available in the
   project source as Apache-2.0 and BSD. You may not use this file except in
   compliance with one of these two licences.
-  
+
   Unless required by applicable law or agreed to in writing, software
   distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
@@ -21,9 +21,26 @@
 Subunit
 -------
 
-Subunit is a streaming protocol for test results. The protocol is human
-readable and easily generated and parsed. By design all the components of 
-the protocol conceptually fit into the xUnit TestCase->TestResult interaction.
+Subunit is a streaming protocol for test results.
+
+There are two major revisions of the protocol. Version 1 was trivially human
+readable but had significant defects as far as highly parallel testing was
+concerned - it had no room for doing discovery and execution in parallel,
+required substantial buffering when multiplexing and was fragile - a corrupt
+byte could cause an entire stream to be misparsed. Version 1.1 added
+encapsulation of binary streams which mitigated some of the issues but the
+core remained.
+
+Version 2 shares many of the good characteristics of Version 1 - it can be
+embedded into a regular text stream (e.g. from a build system) and it still
+models xUnit style test execution. It also fixes many of the issues with
+Version 1 - Version 2 can be multiplexed without excessive buffering (in
+time or space), it has a well defined recovery mechanism for dealing with
+corrupted streams (e.g. where two processes write to the same stream
+concurrently, or where the stream generator suffers a bug).
+
+More details on both protocol version s can be found in the 'Protocol' section
+of this document.
 
 Subunit comes with command line filters to process a subunit stream and
 language bindings for python, C, C++ and shell. Bindings are easy to write
@@ -32,11 +49,12 @@ for other languages.
 A number of useful things can be done easily with subunit:
  * Test aggregation: Tests run separately can be combined and then
    reported/displayed together. For instance, tests from different languages
-   can be shown as a seamless whole.
+   can be shown as a seamless whole, and tests running on multiple machines
+   can be aggregated into a single stream through a multiplexer.
  * Test archiving: A test run may be recorded and replayed later.
  * Test isolation: Tests that may crash or otherwise interact badly with each
-   other can be run separately and then aggregated, rather than interfering
-   with each other.
+   other can be run seperately and then aggregated, rather than interfering
+   with each other or requiring an adhoc test->runner reporting protocol.
  * Grid testing: subunit can act as the necessary serialisation and
    deserialiation to get test runs on distributed machines to be reported in
    real time.
@@ -68,20 +86,20 @@ Subunit has excellent Python support: most of the filters and tools are written
 in python and there are facilities for using Subunit to increase test isolation
 seamlessly within a test suite.
 
-One simple way to run an existing python test suite and have it output subunit
-is the module ``subunit.run``::
+The most common way is to run an existing python test suite and have it output
+subunit via the ``subunit.run`` module::
 
   $ python -m subunit.run mypackage.tests.test_suite
- 
+
 For more information on the Python support Subunit offers , please see
-``pydoc subunit``, or the source in ``python/subunit/__init__.py``
+``pydoc subunit``, or the source in ``python/subunit/``
 
 C
 =
 
-Subunit has C bindings to emit the protocol, and comes with a patch for 'check'
-which has been nominally accepted by the 'check' developers. See 'c/README' for
-more details.
+Subunit has C bindings to emit the protocol. The 'check' C unit testing project
+has included subunit support in their project for some years now. See
+'c/README' for more details.
 
 C++
 ===
@@ -92,9 +110,13 @@ CPPUnit is included in the Subunit distribution. See 'c++/README' for details.
 shell
 =====
 
-Similar to C, the shell bindings consist of simple functions to output protocol
-elements, and a patch for adding subunit output to the 'ShUnit' shell test
-runner. See 'shell/README' for details.
+There are two sets of shell tools. There are filters, which accept a subunit
+stream on stdin and output processed data (or a transformed stream) on stdout.
+
+Then there are unittest facilities similar to those for C : shell bindings
+consisting of simple functions to output protocol elements, and a patch for
+adding subunit output to the 'ShUnit' shell test runner. See 'shell/README' for
+details.
 
 Filter recipes
 --------------
@@ -104,9 +126,225 @@ To ignore some failing tests whose root cause is already known::
   subunit-filter --without 'AttributeError.*flavor'
 
 
+The xUnit test model
+--------------------
+
+Subunit implements a slightly modified xUnit test model. The stock standard
+model is that there are tests, which have an id(), can be run, and when run
+start, emit an outcome (like success or failure) and then finish.
+
+Subunit extends this with the idea of test enumeration (find out about tests
+a runner has without running them), tags (allow users to describe tests in
+ways the test framework doesn't apply any semantic value to), file attachments
+(allow arbitrary data to make analysing a failure easy) and timestamps.
+
 The protocol
 ------------
 
+Version 2, or v2 is new and still under development, but is intended to
+supercede version 1 in the very near future. Subunit's bundled tools accept
+only version 2 and only emit version 2, but the new filters subunit-1to2 and
+subunit-2to1 can be used to interoperate with older third party libraries.
+
+Version 2
+=========
+
+Version 2 is a binary protocol consisting of independent packets that can be
+embedded in the output from tools like make - as long as each packet has no
+other bytes mixed in with it (which 'make -j N>1' has a tendency of doing).
+Version 2 is currently in draft form, and early adopters should be willing
+to either discard stored results (if protocol changes are made), or bulk
+convert them back to v1 and then to a newer edition of v2.
+
+The protocol synchronises at the start of the stream, after a packet, or
+after any 0x0A byte. That is, a subunit v2 packet starts after a newline or
+directly after the end of the prior packet.
+
+Subunit is intended to be transported over a reliable streaming protocol such
+as TCP. As such it does not concern itself with out of order delivery of
+packets. However, because of the possibility of corruption due to either
+bugs in the sender, or due to mixed up data from concurrent writes to the same
+fd when being embedded, subunit strives to recover reasonably gracefully from
+damaged data.
+
+A key design goal for Subunit version 2 is to allow processing and multiplexing
+without forcing buffering for semantic correctness, as buffering tends to hide
+hung or otherwise misbehaving tests. That said, limited time based buffering
+for network efficiency is a good idea - this is ultimately implementator
+choice. Line buffering is also discouraged for subunit streams, as dropping
+into a debugger or other tool may require interactive traffic even if line
+buffering would not otherwise be a problem.
+
+In version two there are two conceptual events - a test status event and a file
+attachment event. Events may have timestamps, and the path of multiplexers that
+an event is routed through is recorded to permit sending actions back to the
+source (such as new tests to run or stdin for driving debuggers and other
+interactive input). Test status events are used to enumerate tests, to report
+tests and test helpers as they run. Tests may have tags, used to allow
+tunnelling extra meanings through subunit without requiring parsing of
+arbitrary file attachments. Things that are not standalone tests get marked
+as such by setting the 'Runnable' flag to false. (For instance, individual
+assertions in TAP are not runnable tests, only the top level TAP test script
+is runnable).
+
+File attachments are used to provide rich detail about the nature of a failure.
+File attachments can also be used to encapsulate stdout and stderr both during
+and outside tests.
+
+Most numbers are stored in network byte order - Most Significant Byte first
+encoded using a variation of http://www.dlugosz.com/ZIP2/VLI.html. The first
+byte's top 2 high order bits encode the total number of octets in the number.
+This encoding can encode values from 0 to 2**30-1, enough to encode a
+nanosecond. Numbers that are not variable length encoded are still stored in
+MSB order.
+
+ prefix   octets   max       max
++-------+--------+---------+------------+
+| 00    |      1 |  2**6-1 |         63 |
+| 01    |      2 | 2**14-1 |      16383 |
+| 10    |      3 | 2**22-1 |    4194303 |
+| 11    |      4 | 2**30-1 | 1073741823 |
++-------+--------+---------+------------+
+
+All variable length elements of the packet are stored with a length prefix
+number allowing them to be skipped over for consumers that don't need to
+interpret them.
+
+UTF-8 strings are with no terminating NUL and should not have any embedded NULs
+(implementations SHOULD validate any such strings that they process and take
+some remedial action (such as discarding the packet as corrupt).
+
+In short the structure of a packet is:
+PACKET := SIGNATURE FLAGS PACKET_LENGTH TIMESTAMP? TESTID? TAGS? MIME?
+          FILECONTENT? ROUTING_CODE? CRC32
+
+In more detail...
+
+Packets are identified by a single byte signature - 0xB3, which is never legal
+in a UTF-8 stream as the first byte of a character. 0xB3 starts with the first
+bit set and the second not, which is the UTF-8 signature for a continuation
+byte. 0xB3 was chosen as 0x73 ('s' in ASCII') with the top two bits replaced by
+the 1 and 0 for a continuation byte.
+
+If subunit packets are being embedded in a non-UTF-8 text stream, where 0x73 is
+a legal character, consider either recoding the text to UTF-8, or using
+subunit's 'file' packets to embed the text stream in subunit, rather than the
+other way around.
+
+Following the signature byte comes a 16-bit flags field, which includes a
+4-bit version field - if the version is not 0x2 then the packet cannot be
+read. It is recommended to signal an error at this point (e.g. by emitting
+a synthetic error packet and returning to the top level loop to look for
+new packets, or exiting with an error). If recovery is desired, treat the
+packet signature as an opaque byte and scan for a new synchronisation point.
+NB: Subunit V1 and V2 packets may legitimately included 0xB3 internally,
+as they are an 8-bit safe container format, so recovery from this situation
+may involve an arbitrary number of false positives until an actual packet
+is encountered : and even then it may still be false, failing after passing
+the version check due to coincidence.
+
+Flags are stored in network byte order too.
++-------------------------+------------------------+
+| High byte               | Low byte               |
+| 15 14 13 12 11 10  9  8 | 7  6  5  4  3  2  1  0 |
+| VERSION    |feature bits|                        |
++------------+------------+------------------------+
+
+Valid version values are:
+0x2 - version 2
+
+Feature bits:
+Bit 11 - mask 0x0800 - Test id present.
+Bit 10 - mask 0x0400 - Routing code present.
+Bit  9 - mask 0x0200 - Timestamp present.
+Bit  8 - mask 0x0100 - Test is 'runnable'.
+Bit  7 - mask 0x0080 - Tags are present.
+Bit  6 - mask 0x0040 - File content is present.
+Bit  5 - mask 0x0020 - File MIME type is present.
+Bit  4 - mask 0x0010 - EOF marker.
+Bit  3 - mask 0x0008 - Must be zero in version 2.
+
+Test status gets three bits:
+Bit 2 | Bit 1 | Bit 0 - mask 0x0007 - A test status enum lookup:
+000 - undefined / no test
+001 - Enumeration / existence
+002 - In progress
+003 - Success
+004 - Unexpected Success
+005 - Skipped
+006 - Failed
+007 - Expected failure
+
+After the flags field is a number field giving the length in bytes for the
+entire packet including the signature and the checksum. This length must
+be less than 4MiB - 4194303 bytes. The encoding can obviously record a larger
+number but one of the goals is to avoid requiring large buffers, or causing
+large latency in the packet forward/processing pipeline. Larger file
+attachments can be communicated in multiple packets, and the overhead in such a
+4MiB packet is approximately 0.2%.
+
+The rest of the packet is a series of optional features as specified by the set
+feature bits in the flags field. When absent they are entirely absent.
+
+Forwarding and multiplexing of packets can be done without interpreting the
+remainder of the packet until the routing code and checksum (which are both at
+the end of the packet). Additionally, routers can often avoid copying or moving
+the bulk of the packet, as long as the routing code size increase doesn't force
+the length encoding to take up a new byte (which will only happen to packets
+less than or equal to 16KiB in length) - large packets are very efficient to
+route.
+
+Timestamp when present is a 32 bit unsigned integer for secnods, and a variable
+length number for nanoseconds, representing UTC time since Unix Epoch in
+seconds and nanoseconds.
+
+Test id when present is a UTF-8 string. The test id should uniquely identify
+runnable tests such that they can be selected individually. For tests and other
+actions which cannot be individually run (such as test
+fixtures/layers/subtests) uniqueness is not required (though being human
+meaningful is highly recommended).
+
+Tags when present is a length prefixed vector of UTF-8 strings, one per tag.
+There are no restrictions on tag content (other than the restrictions on UTF-8
+strings in subunit in general). Tags have no ordering.
+
+When a MIME type is present, it defines the MIME type for the file across all
+packets same file (routing code + testid + name uniquely identifies a file,
+reset when EOF is flagged). If a file never has a MIME type set, it should be
+treated as application/octet-stream.
+
+File content when present is a UTF-8 string for the name followed by the length
+in bytes of the content, and then the content octets.
+
+If present routing code is a UTF-8 string. The routing code is used to
+determine which test backend a test was running on when doing data analysis,
+and to route stdin to the test process if interaction is required.
+
+Multiplexers SHOULD add a routing code if none is present, and prefix any
+existing routing code with a routing code ('/' separated) if one is already
+present. For example, a multiplexer might label each stream it is multiplexing
+with a simple ordinal ('0', '1' etc), and given an incoming packet with route
+code '3' from stream '0' would adjust the route code when forwarding the packet
+to be '0/3'.
+
+Following the end of the packet is a CRC-32 checksum of the contents of the
+packet including the signature.
+
+Example packets
+~~~~~~~~~~~~~~~
+
+Trivial test "foo" enumeration packet, with test id, runnable set,
+status=enumeration. Spaces below are to visually break up signature / flags /
+length / testid / crc32
+
+b3 2901 0c 03666f6f 08555f1b
+
+
+Version 1 (and 1.1)
+===================
+
+Version 1 (and 1.1) are mostly human readable protocols.
+
 Sample subunit wire contents
 ----------------------------
 
@@ -163,6 +401,7 @@ tags: [-]TAG ...
 time: YYYY-MM-DD HH:MM:SSZ
 
 LABEL: UTF8*
+NAME: UTF8*
 DETAILS ::= BRACKETED | MULTIPART
 BRACKETED ::= '[' CR UTF8-lines ']' CR
 MULTIPART ::= '[ multipart' CR PART* ']' CR
@@ -223,7 +462,9 @@ Releases
 ========
 
 * Update versions in configure.ac and python/subunit/__init__.py.
-* Make PyPI and regular tarball releases. Upload the regular one to LP, the
-  PyPI one to PyPI.
+* Update NEWS.
+* Do a make distcheck, which will update Makefile etc.
+* Do a PyPI release: PYTHONPATH=../../python python ../../setup.py sdist bdist_wheel upload -s
+* Upload the regular one to LP.
 * Push a tagged commit.
 
diff --git a/lib/subunit/configure.ac b/lib/subunit/configure.ac
index cf21d55..47071c5 100644
--- a/lib/subunit/configure.ac
+++ b/lib/subunit/configure.ac
@@ -1,6 +1,6 @@
-m4_define([SUBUNIT_MAJOR_VERSION], [0])
+m4_define([SUBUNIT_MAJOR_VERSION], [1])
 m4_define([SUBUNIT_MINOR_VERSION], [0])
-m4_define([SUBUNIT_MICRO_VERSION], [9])
+m4_define([SUBUNIT_MICRO_VERSION], [0])
 m4_define([SUBUNIT_VERSION],
 m4_defn([SUBUNIT_MAJOR_VERSION]).m4_defn([SUBUNIT_MINOR_VERSION]).m4_defn([SUBUNIT_MICRO_VERSION]))
 AC_PREREQ([2.59])
@@ -19,6 +19,7 @@ AC_SUBST([SUBUNIT_VERSION])
 AC_USE_SYSTEM_EXTENSIONS
 AC_PROG_CC
 AC_PROG_CXX
+m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
 AM_PROG_CC_C_O
 AC_PROG_INSTALL
 AC_PROG_LN_S
diff --git a/lib/subunit/filters/subunit-filter b/lib/subunit/filters/subunit-filter
index 6a1ecc9..e9e2bb0 100755
--- a/lib/subunit/filters/subunit-filter
+++ b/lib/subunit/filters/subunit-filter
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #  subunit: extensions to python unittest to get test results from subprocesses.
-#  Copyright (C) 2008  Robert Collins <robertc at robertcollins.net>
+#  Copyright (C) 200-2013  Robert Collins <robertc at robertcollins.net>
 #            (C) 2009  Martin Pool
 #
 #  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
@@ -30,13 +30,15 @@ from optparse import OptionParser
 import sys
 import re
 
+from testtools import ExtendedToStreamDecorator, StreamToExtendedDecorator
+
 from subunit import (
     DiscardStream,
     ProtocolTestCase,
-    TestProtocolClient,
+    StreamResultToBytes,
     read_test_list,
     )
-from subunit.filters import filter_by_result
+from subunit.filters import filter_by_result, find_stream
 from subunit.test_results import (
     and_predicates,
     make_tag_filter,
@@ -55,9 +57,11 @@ def make_options(description):
     parser.add_option("-f", "--no-failure", action="store_true",
         help="exclude failures", dest="failure")
     parser.add_option("--passthrough", action="store_false",
-        help="Show all non subunit input.", default=False, dest="no_passthrough")
+        help="Forward non-subunit input as 'stdout'.", default=False,
+        dest="no_passthrough")
     parser.add_option("--no-passthrough", action="store_true",
-        help="Hide all non subunit input.", default=False, dest="no_passthrough")
+        help="Discard all non subunit input.", default=False,
+        dest="no_passthrough")
     parser.add_option("-s", "--success", action="store_false",
         help="include successes", dest="success")
     parser.add_option("--no-success", action="store_true",
@@ -126,15 +130,16 @@ def _make_result(output, options, predicate):
     fixup_expected_failures = set()
     for path in options.fixup_expected_failures or ():
         fixup_expected_failures.update(read_test_list(path))
-    return TestResultFilter(
-        TestProtocolClient(output),
+    return StreamToExtendedDecorator(TestResultFilter(
+        ExtendedToStreamDecorator(
+        StreamResultToBytes(output)),
         filter_error=options.error,
         filter_failure=options.failure,
         filter_success=options.success,
         filter_skip=options.skip,
         filter_xfail=options.xfail,
         filter_predicate=predicate,
-        fixup_expected_failures=fixup_expected_failures)
+        fixup_expected_failures=fixup_expected_failures))
 
 
 def main():
@@ -150,7 +155,9 @@ def main():
         lambda output_to: _make_result(sys.stdout, options, filter_predicate),
         output_path=None,
         passthrough=(not options.no_passthrough),
-        forward=False)
+        forward=False,
+        protocol_version=2,
+        input_stream=find_stream(sys.stdin, args))
     sys.exit(0)
 
 
diff --git a/lib/subunit/filters/subunit-ls b/lib/subunit/filters/subunit-ls
index 82db4c3..8c6a1e7 100755
--- a/lib/subunit/filters/subunit-ls
+++ b/lib/subunit/filters/subunit-ls
@@ -19,9 +19,14 @@
 from optparse import OptionParser
 import sys
 
-from subunit import DiscardStream, ProtocolTestCase
+from testtools import (
+    CopyStreamResult, StreamToExtendedDecorator, StreamResultRouter,
+    StreamSummary)
+
+from subunit import ByteStreamToStreamResult
+from subunit.filters import find_stream, run_tests_from_stream
 from subunit.test_results import (
-    AutoTimingTestResultDecorator,
+    CatFiles,
     TestIdPrintingResult,
     )
 
@@ -30,18 +35,25 @@ parser = OptionParser(description=__doc__)
 parser.add_option("--times", action="store_true",
     help="list the time each test took (requires a timestamped stream)",
         default=False)
+parser.add_option("--exists", action="store_true",
+    help="list tests that are reported as existing (as well as ran)",
+        default=False)
 parser.add_option("--no-passthrough", action="store_true",
     help="Hide all non subunit input.", default=False, dest="no_passthrough")
 (options, args) = parser.parse_args()
-result = AutoTimingTestResultDecorator(
-    TestIdPrintingResult(sys.stdout, options.times))
-if options.no_passthrough:
-    passthrough_stream = DiscardStream()
-else:
-    passthrough_stream = None
-test = ProtocolTestCase(sys.stdin, passthrough=passthrough_stream)
+test = ByteStreamToStreamResult(
+    find_stream(sys.stdin, args), non_subunit_name="stdout")
+result = TestIdPrintingResult(sys.stdout, options.times, options.exists)
+if not options.no_passthrough:
+    result = StreamResultRouter(result)
+    cat = CatFiles(sys.stdout)
+    result.add_rule(cat, 'test_id', test_id=None)
+summary = StreamSummary()
+result = CopyStreamResult([result, summary])
+result.startTestRun()
 test.run(result)
-if result.wasSuccessful():
+result.stopTestRun()
+if summary.wasSuccessful():
     exit_code = 0
 else:
     exit_code = 1
diff --git a/lib/subunit/filters/subunit-notify b/lib/subunit/filters/subunit-notify
index 8cce2d1..bc833da 100755
--- a/lib/subunit/filters/subunit-notify
+++ b/lib/subunit/filters/subunit-notify
@@ -19,6 +19,7 @@
 import pygtk
 pygtk.require('2.0')
 import pynotify
+from testtools import StreamToExtendedDecorator
 
 from subunit import TestResultStats
 from subunit.filters import run_filter_script
@@ -28,6 +29,7 @@ if not pynotify.init("Subunit-notify"):
 
 
 def notify_of_result(result):
+    result = result.decorated
     if result.failed_tests > 0:
         summary = "Test run failed"
     else:
@@ -41,4 +43,6 @@ def notify_of_result(result):
     nw.show()
 
 
-run_filter_script(TestResultStats, __doc__, notify_of_result)
+run_filter_script(
+    lambda output:StreamToExtendedDecorator(TestResultStats(output)),
+    __doc__, notify_of_result, protocol_version=2)
diff --git a/lib/subunit/filters/subunit-stats b/lib/subunit/filters/subunit-stats
index 4734988..79733b0 100755
--- a/lib/subunit/filters/subunit-stats
+++ b/lib/subunit/filters/subunit-stats
@@ -16,26 +16,17 @@
 
 """Filter a subunit stream to get aggregate statistics."""
 
-from optparse import OptionParser
 import sys
-import unittest
 
-from subunit import DiscardStream, ProtocolTestCase, TestResultStats
+from testtools import StreamToExtendedDecorator
+
+from subunit import TestResultStats
+from subunit.filters import run_filter_script
+
 
-parser = OptionParser(description=__doc__)
-parser.add_option("--no-passthrough", action="store_true",
-    help="Hide all non subunit input.", default=False, dest="no_passthrough")
-(options, args) = parser.parse_args()
 result = TestResultStats(sys.stdout)
-if options.no_passthrough:
-    passthrough_stream = DiscardStream()
-else:
-    passthrough_stream = None
-test = ProtocolTestCase(sys.stdin, passthrough=passthrough_stream)
-test.run(result)
-result.formatStats()
-if result.wasSuccessful():
-    exit_code = 0
-else:
-    exit_code = 1
-sys.exit(exit_code)
+def show_stats(r):
+    r.decorated.formatStats()
+run_filter_script(
+    lambda output:StreamToExtendedDecorator(result),
+    __doc__, show_stats, protocol_version=2, passthrough_subunit=False)
diff --git a/lib/subunit/filters/subunit-tags b/lib/subunit/filters/subunit-tags
index edbbfce..1022492 100755
--- a/lib/subunit/filters/subunit-tags
+++ b/lib/subunit/filters/subunit-tags
@@ -23,4 +23,5 @@ subunit-tags foo -bar -> adds foo and removes bar
 import sys
 
 from subunit import tag_stream
+
 sys.exit(tag_stream(sys.stdin, sys.stdout, sys.argv[1:]))
diff --git a/lib/subunit/filters/subunit2csv b/lib/subunit/filters/subunit2csv
index 14620ff..4adf5cd 100755
--- a/lib/subunit/filters/subunit2csv
+++ b/lib/subunit/filters/subunit2csv
@@ -16,8 +16,11 @@
 
 """Turn a subunit stream into a CSV"""
 
+from testtools import StreamToExtendedDecorator
+
 from subunit.filters import run_filter_script
 from subunit.test_results import CsvResult
 
 
-run_filter_script(CsvResult, __doc__)
+run_filter_script(lambda output:StreamToExtendedDecorator(CsvResult(output)),
+    __doc__, protocol_version=2)
diff --git a/lib/subunit/filters/subunit2gtk b/lib/subunit/filters/subunit2gtk
index c2cb2de..78b4309 100755
--- a/lib/subunit/filters/subunit2gtk
+++ b/lib/subunit/filters/subunit2gtk
@@ -46,17 +46,20 @@
 """Display a subunit stream in a gtk progress window."""
 
 import sys
+import threading
 import unittest
 
 import pygtk
 pygtk.require('2.0')
 import gtk, gtk.gdk, gobject
 
+from testtools import StreamToExtendedDecorator
+
 from subunit import (
     PROGRESS_POP,
     PROGRESS_PUSH,
     PROGRESS_SET,
-    TestProtocolServer,
+    ByteStreamToStreamResult,
     )
 from subunit.progress_model import  ProgressModel
 
@@ -139,6 +142,9 @@ class GTKTestResult(unittest.TestResult):
 
     def stopTest(self, test):
         super(GTKTestResult, self).stopTest(test)
+        gobject.idle_add(self._stopTest)
+
+    def _stopTest(self):
         self.progress_model.advance()
         if self.progress_model.width() == 0:
             self.pbar.pulse()
@@ -153,26 +159,26 @@ class GTKTestResult(unittest.TestResult):
             super(GTKTestResult, self).stopTestRun()
         except AttributeError:
             pass
-        self.pbar.set_text('Finished')
+        gobject.idle_add(self.pbar.set_text, 'Finished')
 
     def addError(self, test, err):
         super(GTKTestResult, self).addError(test, err)
-        self.update_counts()
+        gobject.idle_add(self.update_counts)
 
     def addFailure(self, test, err):
         super(GTKTestResult, self).addFailure(test, err)
-        self.update_counts()
+        gobject.idle_add(self.update_counts)
 
     def addSuccess(self, test):
         super(GTKTestResult, self).addSuccess(test)
-        self.update_counts()
+        gobject.idle_add(self.update_counts)
 
     def addSkip(self, test, reason):
         # addSkip is new in Python 2.7/3.1
         addSkip = getattr(super(GTKTestResult, self), 'addSkip', None)
         if callable(addSkip):
             addSkip(test, reason)
-        self.update_counts()
+        gobject.idle_add(self.update_counts)
 
     def addExpectedFailure(self, test, err):
         # addExpectedFailure is new in Python 2.7/3.1
@@ -180,7 +186,7 @@ class GTKTestResult(unittest.TestResult):
             'addExpectedFailure', None)
         if callable(addExpectedFailure):
             addExpectedFailure(test, err)
-        self.update_counts()
+        gobject.idle_add(self.update_counts)
 
     def addUnexpectedSuccess(self, test):
         # addUnexpectedSuccess is new in Python 2.7/3.1
@@ -188,7 +194,7 @@ class GTKTestResult(unittest.TestResult):
             'addUnexpectedSuccess', None)
         if callable(addUnexpectedSuccess):
             addUnexpectedSuccess(test)
-        self.update_counts()
+        gobject.idle_add(self.update_counts)
 
     def progress(self, offset, whence):
         if whence == PROGRESS_PUSH:
@@ -212,47 +218,22 @@ class GTKTestResult(unittest.TestResult):
         self.ok_label.set_text(str(self.testsRun - bad))
         self.not_ok_label.set_text(str(bad))
 
-
-class GIOProtocolTestCase(object):
-
-    def __init__(self, stream, result, on_finish):
-        self.stream = stream
-        self.schedule_read()
-        self.hup_id = gobject.io_add_watch(stream, gobject.IO_HUP, self.hup)
-        self.protocol = TestProtocolServer(result)
-        self.on_finish = on_finish
-
-    def read(self, source, condition, all=False):
-        #NB: \o/ actually blocks
-        line = source.readline()
-        if not line:
-            self.protocol.lostConnection()
-            self.on_finish()
-            return False
-        self.protocol.lineReceived(line)
-        # schedule more IO shortly - if we say we're willing to do it
-        # immediately we starve things.
-        if not all:
-            source_id = gobject.timeout_add(1, self.schedule_read)
-            return False
-        else:
-            return True
-
-    def schedule_read(self):
-        self.read_id = gobject.io_add_watch(self.stream, gobject.IO_IN, self.read)
-
-    def hup(self, source, condition):
-        while self.read(source, condition, all=True): pass
-        self.protocol.lostConnection()
-        gobject.source_remove(self.read_id)
-        self.on_finish()
-        return False
-
-
-result = GTKTestResult()
-test = GIOProtocolTestCase(sys.stdin, result, result.stopTestRun)
+gobject.threads_init()
+result = StreamToExtendedDecorator(GTKTestResult())
+test = ByteStreamToStreamResult(sys.stdin, non_subunit_name='stdout')
+# Get setup
+while gtk.events_pending():
+  gtk.main_iteration()
+# Start IO
+def run_and_finish():
+    test.run(result)
+    result.stopTestRun()
+t = threading.Thread(target=run_and_finish)
+t.daemon = True
+result.startTestRun()
+t.start()
 gtk.main()
-if result.wasSuccessful():
+if result.decorated.wasSuccessful():
     exit_code = 0
 else:
     exit_code = 1
diff --git a/lib/subunit/filters/subunit2junitxml b/lib/subunit/filters/subunit2junitxml
index d568c71..8e827d5 100755
--- a/lib/subunit/filters/subunit2junitxml
+++ b/lib/subunit/filters/subunit2junitxml
@@ -18,6 +18,9 @@
 
 
 import sys
+
+from testtools import StreamToExtendedDecorator
+
 from subunit.filters import run_filter_script
 
 try:
@@ -28,4 +31,6 @@ except ImportError:
     raise
 
 
-run_filter_script(JUnitXmlResult, __doc__)
+run_filter_script(
+    lambda output:StreamToExtendedDecorator(JUnitXmlResult(output)), __doc__,
+    protocol_version=2)
diff --git a/lib/subunit/filters/subunit2pyunit b/lib/subunit/filters/subunit2pyunit
index 83a23d1..d10ceea 100755
--- a/lib/subunit/filters/subunit2pyunit
+++ b/lib/subunit/filters/subunit2pyunit
@@ -16,11 +16,16 @@
 
 """Display a subunit stream through python's unittest test runner."""
 
+from operator import methodcaller
 from optparse import OptionParser
 import sys
 import unittest
 
-from subunit import DiscardStream, ProtocolTestCase, TestProtocolServer
+from testtools import StreamToExtendedDecorator, DecorateTestCaseResult, StreamResultRouter
+
+from subunit import ByteStreamToStreamResult
+from subunit.filters import find_stream
+from subunit.test_results import CatFiles
 
 parser = OptionParser(description=__doc__)
 parser.add_option("--no-passthrough", action="store_true",
@@ -29,11 +34,17 @@ parser.add_option("--progress", action="store_true",
     help="Use bzrlib's test reporter (requires bzrlib)",
         default=False)
 (options, args) = parser.parse_args()
-if options.no_passthrough:
-    passthrough_stream = DiscardStream()
-else:
-    passthrough_stream = None
-test = ProtocolTestCase(sys.stdin, passthrough=passthrough_stream)
+test = ByteStreamToStreamResult(
+    find_stream(sys.stdin, args), non_subunit_name='stdout')
+def wrap_result(result):
+    result = StreamToExtendedDecorator(result)
+    if not options.no_passthrough:
+        result = StreamResultRouter(result)
+        result.add_rule(CatFiles(sys.stdout), 'test_id', test_id=None)
+    return result
+test = DecorateTestCaseResult(test, wrap_result,
+    before_run=methodcaller('startTestRun'),
+    after_run=methodcaller('stopTestRun'))
 if options.progress:
     from bzrlib.tests import TextTestRunner
     from bzrlib import ui
diff --git a/lib/subunit/perl/Makefile.PL.in b/lib/subunit/perl/Makefile.PL.in
index cf5e6c4..90a6a5e 100755
--- a/lib/subunit/perl/Makefile.PL.in
+++ b/lib/subunit/perl/Makefile.PL.in
@@ -1,6 +1,6 @@
 use ExtUtils::MakeMaker;
 WriteMakefile(
-    'INSTALL_BASE' => '@prefix@',
+    'PREFIX' => '@prefix@',
     'NAME'	=> 'Subunit',
     'VERSION' => '@SUBUNIT_VERSION@',
     'test' => { 'TESTS' => 'tests/*.pl' },
@@ -12,7 +12,7 @@ sub MY::postamble {
 check: # test
 
 uninstall_distcheck:
-	rm -fr $(DESTINSTALLARCHLIB)
+	find $(DESTDIR)$(INSTALLSITEARCH) -type f -exec rm {} \; 
 	rm MYMETA.yml
 
 VPATH = @srcdir@
diff --git a/lib/subunit/python/subunit/__init__.py b/lib/subunit/python/subunit/__init__.py
index 42dcf29..bfa27a1 100644
--- a/lib/subunit/python/subunit/__init__.py
+++ b/lib/subunit/python/subunit/__init__.py
@@ -121,31 +121,25 @@ import re
 import subprocess
 import sys
 import unittest
-if sys.version_info > (3, 0):
+try:
     from io import UnsupportedOperation as _UnsupportedOperation
-else:
+except ImportError:
     _UnsupportedOperation = AttributeError
 
-
+from extras import safe_hasattr
 from testtools import content, content_type, ExtendedToOriginalDecorator
 from testtools.content import TracebackContent
 from testtools.compat import _b, _u, BytesIO, StringIO
 try:
     from testtools.testresult.real import _StringException
     RemoteException = _StringException
-    # For testing: different pythons have different str() implementations.
-    if sys.version_info > (3, 0):
-        _remote_exception_str = "testtools.testresult.real._StringException"
-        _remote_exception_str_chunked = "34\r\n" + _remote_exception_str
-    else:
-        _remote_exception_str = "_StringException" 
-        _remote_exception_str_chunked = "1A\r\n" + _remote_exception_str
 except ImportError:
     raise ImportError ("testtools.testresult.real does not contain "
         "_StringException, check your version.")
-from testtools import testresult
+from testtools import testresult, CopyStreamResult
 
 from subunit import chunked, details, iso8601, test_results
+from subunit.v2 import ByteStreamToStreamResult, StreamResultToBytes
 
 # same format as sys.version_info: "A tuple containing the five components of
 # the version number: major, minor, micro, releaselevel, and serial. All
@@ -159,7 +153,7 @@ from subunit import chunked, details, iso8601, test_results
 # If the releaselevel is 'final', then the tarball will be major.minor.micro.
 # Otherwise it is major.minor.micro~$(revno).
 
-__version__ = (0, 0, 9, 'final', 0)
+__version__ = (1, 0, 0, 'final', 0)
 
 PROGRESS_SET = 0
 PROGRESS_CUR = 1
@@ -624,7 +618,7 @@ class TestProtocolClient(testresult.TestResult):
 
     def __init__(self, stream):
         testresult.TestResult.__init__(self)
-        stream = _make_stream_binary(stream)
+        stream = make_stream_binary(stream)
         self._stream = stream
         self._progress_fmt = _b("progress: ")
         self._bytes_eol = _b("\n")
@@ -921,7 +915,7 @@ class ExecTestCase(unittest.TestCase):
         protocol = TestProtocolServer(result)
         process = subprocess.Popen(self.script, shell=True,
             stdout=subprocess.PIPE)
-        _make_stream_binary(process.stdout)
+        make_stream_binary(process.stdout)
         output = process.communicate()[0]
         protocol.readFrom(BytesIO(output))
 
@@ -992,44 +986,51 @@ def run_isolated(klass, self, result):
     return result
 
 
-def TAP2SubUnit(tap, subunit):
+def TAP2SubUnit(tap, output_stream):
     """Filter a TAP pipe into a subunit pipe.
 
-    :param tap: A tap pipe/stream/file object.
+    This should be invoked once per TAP script, as TAP scripts get
+    mapped to a single runnable case with multiple components.
+
+    :param tap: A tap pipe/stream/file object - should emit unicode strings.
     :param subunit: A pipe/stream/file object to write subunit results to.
     :return: The exit code to exit with.
     """
+    output = StreamResultToBytes(output_stream)
+    UTF8_TEXT = 'text/plain; charset=UTF8'
     BEFORE_PLAN = 0
     AFTER_PLAN = 1
     SKIP_STREAM = 2
     state = BEFORE_PLAN
     plan_start = 1
     plan_stop = 0
-    def _skipped_test(subunit, plan_start):
-        # Some tests were skipped.
-        subunit.write('test test %d\n' % plan_start)
-        subunit.write('error test %d [\n' % plan_start)
-        subunit.write('test missing from TAP output\n')
-        subunit.write(']\n')
-        return plan_start + 1
     # Test data for the next test to emit
     test_name = None
     log = []
     result = None
+    def missing_test(plan_start):
+        output.status(test_id='test %d' % plan_start,
+            test_status='fail', runnable=False, 
+            mime_type=UTF8_TEXT, eof=True, file_name="tap meta",
+            file_bytes=b"test missing from TAP output")
     def _emit_test():
         "write out a test"
         if test_name is None:
             return
-        subunit.write("test %s\n" % test_name)
-        if not log:
-            subunit.write("%s %s\n" % (result, test_name))
-        else:
-            subunit.write("%s %s [\n" % (result, test_name))
         if log:
-            for line in log:
-                subunit.write("%s\n" % line)
-            subunit.write("]\n")
+            log_bytes = b'\n'.join(log_line.encode('utf8') for log_line in log)
+            mime_type = UTF8_TEXT
+            file_name = 'tap comment'
+            eof = True
+        else:
+            log_bytes = None
+            mime_type = None
+            file_name = None
+            eof = True
         del log[:]
+        output.status(test_id=test_name, test_status=result,
+            file_bytes=log_bytes, mime_type=mime_type, eof=eof,
+            file_name=file_name, runnable=False)
     for line in tap:
         if state == BEFORE_PLAN:
             match = re.match("(\d+)\.\.(\d+)\s*(?:\#\s+(.*))?\n", line)
@@ -1040,10 +1041,9 @@ def TAP2SubUnit(tap, subunit):
                 if plan_start > plan_stop and plan_stop == 0:
                     # skipped file
                     state = SKIP_STREAM
-                    subunit.write("test file skip\n")
-                    subunit.write("skip file skip [\n")
-                    subunit.write("%s\n" % comment)
-                    subunit.write("]\n")
+                    output.status(test_id='file skip', test_status='skip',
+                        file_bytes=comment.encode('utf8'), eof=True,
+                        file_name='tap comment')
                 continue
         # not a plan line, or have seen one before
         match = re.match("(ok|not ok)(?:\s+(\d+)?)?(?:\s+([^#]*[^#\s]+)\s*)?(?:\s+#\s+(TODO|SKIP|skip|todo)(?:\s+(.*))?)?\n", line)
@@ -1054,7 +1054,7 @@ def TAP2SubUnit(tap, subunit):
             if status == 'ok':
                 result = 'success'
             else:
-                result = "failure"
+                result = "fail"
             if description is None:
                 description = ''
             else:
@@ -1069,7 +1069,8 @@ def TAP2SubUnit(tap, subunit):
             if number is not None:
                 number = int(number)
                 while plan_start < number:
-                    plan_start = _skipped_test(subunit, plan_start)
+                    missing_test(plan_start)
+                    plan_start += 1
             test_name = "test %d%s" % (plan_start, description)
             plan_start += 1
             continue
@@ -1082,18 +1083,21 @@ def TAP2SubUnit(tap, subunit):
                 extra = ' %s' % reason
             _emit_test()
             test_name = "Bail out!%s" % extra
-            result = "error"
+            result = "fail"
             state = SKIP_STREAM
             continue
         match = re.match("\#.*\n", line)
         if match:
             log.append(line[:-1])
             continue
-        subunit.write(line)
+        # Should look at buffering status and binding this to the prior result.
+        output.status(file_bytes=line.encode('utf8'), file_name='stdout',
+            mime_type=UTF8_TEXT)
     _emit_test()
     while plan_start <= plan_stop:
         # record missed tests
-        plan_start = _skipped_test(subunit, plan_start)
+        missing_test(plan_start)
+        plan_start += 1
     return 0
 
 
@@ -1121,24 +1125,21 @@ def tag_stream(original, filtered, tags):
     :return: 0
     """
     new_tags, gone_tags = tags_to_new_gone(tags)
-    def write_tags(new_tags, gone_tags):
-        if new_tags or gone_tags:
-            filtered.write("tags: " + ' '.join(new_tags))
-            if gone_tags:
-                for tag in gone_tags:
-                    filtered.write("-" + tag)
-            filtered.write("\n")
-    write_tags(new_tags, gone_tags)
-    # TODO: use the protocol parser and thus don't mangle test comments.
-    for line in original:
-        if line.startswith("tags:"):
-            line_tags = line[5:].split()
-            line_new, line_gone = tags_to_new_gone(line_tags)
-            line_new = line_new - gone_tags
-            line_gone = line_gone - new_tags
-            write_tags(line_new, line_gone)
-        else:
-            filtered.write(line)
+    source = ByteStreamToStreamResult(original, non_subunit_name='stdout')
+    class Tagger(CopyStreamResult):
+        def status(self, **kwargs):
+            tags = kwargs.get('test_tags')
+            if not tags:
+                tags = set()
+            tags.update(new_tags)
+            tags.difference_update(gone_tags)
+            if tags:
+                kwargs['test_tags'] = tags
+            else:
+                kwargs['test_tags'] = None
+            super(Tagger, self).status(**kwargs)
+    output = Tagger([StreamResultToBytes(filtered)])
+    source.run(output)
     return 0
 
 
@@ -1177,11 +1178,11 @@ class ProtocolTestCase(object):
         :param forward: A stream to pass subunit input on to. If not supplied
             subunit input is not forwarded.
         """
-        stream = _make_stream_binary(stream)
+        stream = make_stream_binary(stream)
         self._stream = stream
         self._passthrough = passthrough
         if forward is not None:
-            forward = _make_stream_binary(forward)
+            forward = make_stream_binary(forward)
         self._forward = forward
 
     def __call__(self, result=None):
@@ -1249,21 +1250,6 @@ class TestResultStats(testresult.TestResult):
         return self.failed_tests == 0
 
 
-def get_default_formatter():
-    """Obtain the default formatter to write to.
-
-    :return: A file-like object.
-    """
-    formatter = os.getenv("SUBUNIT_FORMATTER")
-    if formatter:
-        return os.popen(formatter, "w")
-    else:
-        stream = sys.stdout
-        if sys.version_info > (3, 0):
-            stream = stream.buffer
-        return stream
-
-
 def read_test_list(path):
     """Read a list of test ids from a file on disk.
 
@@ -1277,7 +1263,7 @@ def read_test_list(path):
         f.close()
 
 
-def _make_stream_binary(stream):
+def make_stream_binary(stream):
     """Ensure that a stream will be binary safe. See _make_binary_on_windows.
     
     :return: A binary version of the same stream (some streams cannot be
@@ -1285,12 +1271,13 @@ def _make_stream_binary(stream):
     """
     try:
         fileno = stream.fileno()
-    except _UnsupportedOperation:
+    except (_UnsupportedOperation, AttributeError):
         pass
     else:
         _make_binary_on_windows(fileno)
     return _unwrap_text(stream)
 
+
 def _make_binary_on_windows(fileno):
     """Win32 mangles \r\n to \n and that breaks streams. See bug lp:505078."""
     if sys.platform == "win32":
@@ -1301,14 +1288,17 @@ def _make_binary_on_windows(fileno):
 def _unwrap_text(stream):
     """Unwrap stream if it is a text stream to get the original buffer."""
     if sys.version_info > (3, 0):
+        unicode_type = str
+    else:
+        unicode_type = unicode
+    try:
+        # Read streams
+        if type(stream.read(0)) is unicode_type:
+            return stream.buffer
+    except (_UnsupportedOperation, IOError):
+        # Cannot read from the stream: try via writes
         try:
-            # Read streams
-            if type(stream.read(0)) is str:
-                return stream.buffer
-        except (_UnsupportedOperation, IOError):
-            # Cannot read from the stream: try via writes
-            try:
-                stream.write(_b(''))
-            except TypeError:
-                return stream.buffer
+            stream.write(_b(''))
+        except TypeError:
+            return stream.buffer
     return stream
diff --git a/lib/subunit/python/subunit/filters.py b/lib/subunit/python/subunit/filters.py
index dc3fd8a..0a0a185 100644
--- a/lib/subunit/python/subunit/filters.py
+++ b/lib/subunit/python/subunit/filters.py
@@ -17,7 +17,14 @@
 from optparse import OptionParser
 import sys
 
-from subunit import DiscardStream, ProtocolTestCase
+from extras import safe_hasattr
+from testtools import CopyStreamResult, StreamResult, StreamResultRouter
+
+from subunit import (
+    DiscardStream, ProtocolTestCase, ByteStreamToStreamResult,
+    StreamResultToBytes,
+    )
+from subunit.test_results import CatFiles
 
 
 def make_options(description):
@@ -31,33 +38,76 @@ def make_options(description):
         help="Send the output to this path rather than stdout.")
     parser.add_option(
         "-f", "--forward", action="store_true", default=False,
-        help="Forward subunit stream on stdout.")
+        help="Forward subunit stream on stdout. When set, received "
+            "non-subunit output will be encapsulated in subunit.")
     return parser
 
 
 def run_tests_from_stream(input_stream, result, passthrough_stream=None,
-                          forward_stream=None):
+    forward_stream=None, protocol_version=1, passthrough_subunit=True):
     """Run tests from a subunit input stream through 'result'.
 
+    Non-test events - top level file attachments - are expected to be
+    dropped by v2 StreamResults at the present time (as all the analysis code
+    is in ExtendedTestResult API's), so to implement passthrough_stream they
+    are diverted and copied directly when that is set.
+
     :param input_stream: A stream containing subunit input.
     :param result: A TestResult that will receive the test events.
+        NB: This should be an ExtendedTestResult for v1 and a StreamResult for
+        v2.
     :param passthrough_stream: All non-subunit input received will be
         sent to this stream.  If not provided, uses the ``TestProtocolServer``
         default, which is ``sys.stdout``.
     :param forward_stream: All subunit input received will be forwarded
-        to this stream.  If not provided, uses the ``TestProtocolServer``
-        default, which is to not forward any input.
+        to this stream. If not provided, uses the ``TestProtocolServer``
+        default, which is to not forward any input. Do not set this when
+        transforming the stream - items would be double-reported.
+    :param protocol_version: What version of the subunit protocol to expect.
+    :param passthrough_subunit: If True, passthrough should be as subunit
+        otherwise unwrap it. Only has effect when forward_stream is None.
+        (when forwarding as subunit non-subunit input is always turned into
+        subunit)
     """
-    test = ProtocolTestCase(
-        input_stream, passthrough=passthrough_stream,
-        forward=forward_stream)
+    if 1==protocol_version:
+        test = ProtocolTestCase(
+            input_stream, passthrough=passthrough_stream,
+            forward=forward_stream)
+    elif 2==protocol_version:
+        # In all cases we encapsulate unknown inputs.
+        if forward_stream is not None:
+            # Send events to forward_stream as subunit.
+            forward_result = StreamResultToBytes(forward_stream)
+            # If we're passing non-subunit through, copy:
+            if passthrough_stream is None:
+                # Not passing non-test events - split them off to nothing.
+                router = StreamResultRouter(forward_result)
+                router.add_rule(StreamResult(), 'test_id', test_id=None)
+                result = CopyStreamResult([router, result])
+            else:
+                # otherwise, copy all events to forward_result
+                result = CopyStreamResult([forward_result, result])
+        elif passthrough_stream is not None:
+            if not passthrough_subunit:
+                # Route non-test events to passthrough_stream, unwrapping them for
+                # display.
+                passthrough_result = CatFiles(passthrough_stream)
+            else:
+                passthrough_result = StreamResultToBytes(passthrough_stream)
+            result = StreamResultRouter(result)
+            result.add_rule(passthrough_result, 'test_id', test_id=None)
+        test = ByteStreamToStreamResult(input_stream,
+            non_subunit_name='stdout')
+    else:
+        raise Exception("Unknown protocol version.")
     result.startTestRun()
     test.run(result)
     result.stopTestRun()
 
 
 def filter_by_result(result_factory, output_path, passthrough, forward,
-                     input_stream=sys.stdin):
+                     input_stream=sys.stdin, protocol_version=1,
+                     passthrough_subunit=True):
     """Filter an input stream using a test result.
 
     :param result_factory: A callable that when passed an output stream
@@ -71,17 +121,24 @@ def filter_by_result(result_factory, output_path, passthrough, forward,
         ``sys.stdout`` as well as to the ``TestResult``.
     :param input_stream: The source of subunit input.  Defaults to
         ``sys.stdin``.
-    :return: A test result with the resultts of the run.
+    :param protocol_version: The subunit protocol version to expect.
+    :param passthrough_subunit: If True, passthrough should be as subunit.
+    :return: A test result with the results of the run.
     """
     if passthrough:
         passthrough_stream = sys.stdout
     else:
-        passthrough_stream = DiscardStream()
+        if 1==protocol_version:
+            passthrough_stream = DiscardStream()
+        else:
+            passthrough_stream = None
 
     if forward:
         forward_stream = sys.stdout
-    else:
+    elif 1==protocol_version:
         forward_stream = DiscardStream()
+    else:
+        forward_stream = None
 
     if output_path is None:
         output_to = sys.stdout
@@ -91,14 +148,17 @@ def filter_by_result(result_factory, output_path, passthrough, forward,
     try:
         result = result_factory(output_to)
         run_tests_from_stream(
-            input_stream, result, passthrough_stream, forward_stream)
+            input_stream, result, passthrough_stream, forward_stream,
+            protocol_version=protocol_version,
+            passthrough_subunit=passthrough_subunit)
     finally:
         if output_path:
             output_to.close()
     return result
 
 
-def run_filter_script(result_factory, description, post_run_hook=None):
+def run_filter_script(result_factory, description, post_run_hook=None,
+    protocol_version=1, passthrough_subunit=True):
     """Main function for simple subunit filter scripts.
 
     Many subunit filter scripts take a stream of subunit input and use a
@@ -111,15 +171,36 @@ def run_filter_script(result_factory, description, post_run_hook=None):
     :param result_factory: A callable that takes an output stream and returns
         a test result that outputs to that stream.
     :param description: A description of the filter script.
+    :param protocol_version: What protocol version to consume/emit.
+    :param passthrough_subunit: If True, passthrough should be as subunit.
     """
     parser = make_options(description)
     (options, args) = parser.parse_args()
     result = filter_by_result(
         result_factory, options.output_to, not options.no_passthrough,
-        options.forward)
+        options.forward, protocol_version=protocol_version,
+        passthrough_subunit=passthrough_subunit,
+        input_stream=find_stream(sys.stdin, args))
     if post_run_hook:
         post_run_hook(result)
+    if not safe_hasattr(result, 'wasSuccessful'):
+        result = result.decorated
     if result.wasSuccessful():
         sys.exit(0)
     else:
         sys.exit(1)
+
+
+def find_stream(stdin, argv):
+    """Find a stream to use as input for filters.
+
+    :param stdin: Standard in - used if no files are named in argv.
+    :param argv: Command line arguments after option parsing. If one file
+        is named, that is opened in read only binary mode and returned.
+        A missing file will raise an exception, as will multiple file names.
+    """
+    assert len(argv) < 2, "Too many filenames."
+    if argv:
+        return open(argv[0], 'rb')
+    else:
+        return stdin
diff --git a/lib/subunit/python/subunit/run.py b/lib/subunit/python/subunit/run.py
index b5ccea4..8469ac9 100755
--- a/lib/subunit/python/subunit/run.py
+++ b/lib/subunit/python/subunit/run.py
@@ -20,39 +20,83 @@
   $ python -m subunit.run mylib.tests.test_suite
 """
 
+import io
+import os
 import sys
 
-from subunit import TestProtocolClient, get_default_formatter
+from testtools import ExtendedToStreamDecorator
+
+from subunit import StreamResultToBytes
 from subunit.test_results import AutoTimingTestResultDecorator
 from testtools.run import (
     BUFFEROUTPUT,
     CATCHBREAK,
     FAILFAST,
+    list_test,
     TestProgram,
     USAGE_AS_MAIN,
     )
 
 
 class SubunitTestRunner(object):
-    def __init__(self, verbosity=None, failfast=None, buffer=None, stream=None):
+    def __init__(self, verbosity=None, failfast=None, buffer=None, stream=None,
+        stdout=None):
         """Create a TestToolsTestRunner.
 
         :param verbosity: Ignored.
         :param failfast: Stop running tests at the first failure.
         :param buffer: Ignored.
+        :param stream: Upstream unittest stream parameter.
+        :param stdout: Testtools stream parameter.
+
+        Either stream or stdout can be supplied, and stream will take
+        precedence.
         """
         self.failfast = failfast
-        self.stream = stream or sys.stdout
+        self.stream = stream or stdout or sys.stdout
 
     def run(self, test):
         "Run the given test case or test suite."
-        result = TestProtocolClient(self.stream)
+        result, _ = self._list(test)
+        result = ExtendedToStreamDecorator(result)
         result = AutoTimingTestResultDecorator(result)
         if self.failfast is not None:
             result.failfast = self.failfast
-        test(result)
+        result.startTestRun()
+        try:
+            test(result)
+        finally:
+            result.stopTestRun()
         return result
 
+    def list(self, test, loader=None):
+        "List the test."
+        result, errors = self._list(test)
+        if loader is not None:
+            # We were called with the updated API by testtools.run, so look for
+            # errors on the loader, not the test list result.
+            errors = loader.errors
+        if errors:
+            failed_descr = '\n'.join(errors).encode('utf8')
+            result.status(file_name="import errors", runnable=False,
+                file_bytes=failed_descr, mime_type="text/plain;charset=utf8")
+            sys.exit(2)
+
+    def _list(self, test):
+        test_ids, errors = list_test(test)
+        try:
+            fileno = self.stream.fileno()
+        except:
+            fileno = None
+        if fileno is not None:
+            stream = os.fdopen(fileno, 'wb', 0)
+        else:
+            stream = self.stream
+        result = StreamResultToBytes(stream)
+        for test_id in test_ids:
+            result.status(test_id=test_id, test_status='exists')
+        return result, errors
+
 
 class SubunitTestProgram(TestProgram):
 
@@ -77,8 +121,26 @@ class SubunitTestProgram(TestProgram):
         sys.exit(2)
 
 
-if __name__ == '__main__':
-    stream = get_default_formatter()
+def main(argv=None, stdout=None):
+    if argv is None:
+        argv = sys.argv
     runner = SubunitTestRunner
-    SubunitTestProgram(module=None, argv=sys.argv, testRunner=runner,
-        stdout=sys.stdout)
+    # stdout is None except in unit tests.
+    if stdout is None:
+        stdout = sys.stdout
+        # Disable the default buffering, for Python 2.x where pdb doesn't do it
+        # on non-ttys.
+        if hasattr(stdout, 'fileno'):
+            # Patch stdout to be unbuffered, so that pdb works well on 2.6/2.7.
+            binstdout = io.open(stdout.fileno(), 'wb', 0)
+            if sys.version_info[0] > 2:
+                sys.stdout = io.TextIOWrapper(binstdout, encoding=sys.stdout.encoding)
+            else:
+                sys.stdout = binstdout
+            stdout = sys.stdout
+    SubunitTestProgram(module=None, argv=argv, testRunner=runner,
+        stdout=stdout, exit=False)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/lib/subunit/python/subunit/test_results.py b/lib/subunit/python/subunit/test_results.py
index 91c9bbd..b3ca968 100644
--- a/lib/subunit/python/subunit/test_results.py
+++ b/lib/subunit/python/subunit/test_results.py
@@ -20,13 +20,14 @@ import csv
 import datetime
 
 import testtools
-from testtools.compat import all
 from testtools.content import (
     text_content,
     TracebackContent,
     )
+from testtools import StreamResult
 
 from subunit import iso8601
+import subunit
 
 
 # NOT a TestResult, because we are implementing the interface, not inheriting
@@ -525,16 +526,24 @@ class TestResultFilter(TestResultDecorator):
 
 
 class TestIdPrintingResult(testtools.TestResult):
+    """Print test ids to a stream.
 
-    def __init__(self, stream, show_times=False):
+    Implements both TestResult and StreamResult, for compatibility.
+    """
+
+    def __init__(self, stream, show_times=False, show_exists=False):
         """Create a FilterResult object outputting to stream."""
         super(TestIdPrintingResult, self).__init__()
         self._stream = stream
+        self.show_exists = show_exists
+        self.show_times = show_times
+
+    def startTestRun(self):
         self.failed_tests = 0
         self.__time = None
-        self.show_times = show_times
         self._test = None
         self._test_duration = 0
+        self._active_tests = {}
 
     def addError(self, test, err):
         self.failed_tests += 1
@@ -557,21 +566,44 @@ class TestIdPrintingResult(testtools.TestResult):
     def addExpectedFailure(self, test, err=None, details=None):
         self._test = test
 
-    def reportTest(self, test, duration):
+    def reportTest(self, test_id, duration):
         if self.show_times:
             seconds = duration.seconds
             seconds += duration.days * 3600 * 24
             seconds += duration.microseconds / 1000000.0
-            self._stream.write(test.id() + ' %0.3f\n' % seconds)
+            self._stream.write(test_id + ' %0.3f\n' % seconds)
         else:
-            self._stream.write(test.id() + '\n')
+            self._stream.write(test_id + '\n')
 
     def startTest(self, test):
         self._start_time = self._time()
 
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        if not test_id:
+            return
+        if timestamp is not None:
+            self.time(timestamp)
+        if test_status=='exists':
+            if self.show_exists:
+                self.reportTest(test_id, 0)
+        elif test_status in ('inprogress', None):
+            self._active_tests[test_id] = self._time()
+        else:
+            self._end_test(test_id)
+
+    def _end_test(self, test_id):
+        test_start = self._active_tests.pop(test_id, None)
+        if not test_start:
+            test_duration = 0
+        else:
+            test_duration = self._time() - test_start
+        self.reportTest(test_id, test_duration)
+
     def stopTest(self, test):
         test_duration = self._time() - self._start_time
-        self.reportTest(self._test, test_duration)
+        self.reportTest(self._test.id(), test_duration)
 
     def time(self, time):
         self.__time = time
@@ -583,6 +615,10 @@ class TestIdPrintingResult(testtools.TestResult):
         "Tells whether or not this result was a success"
         return self.failed_tests == 0
 
+    def stopTestRun(self):
+        for test_id in list(self._active_tests.keys()):
+            self._end_test(test_id)
+
 
 class TestByTestResult(testtools.TestResult):
     """Call something every time a test completes."""
@@ -676,3 +712,17 @@ class CsvResult(TestByTestResult):
     def startTestRun(self):
         super(CsvResult, self).startTestRun()
         self._write_row(['test', 'status', 'start_time', 'stop_time'])
+
+
+class CatFiles(StreamResult):
+    """Cat file attachments received to a stream."""
+
+    def __init__(self, byte_stream):
+        self.stream = subunit.make_stream_binary(byte_stream)
+
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        if file_name is not None:
+            self.stream.write(file_bytes)
+            self.stream.flush()
diff --git a/lib/subunit/python/subunit/tests/TestUtil.py b/lib/subunit/python/subunit/tests/TestUtil.py
deleted file mode 100644
index 39d901e..0000000
--- a/lib/subunit/python/subunit/tests/TestUtil.py
+++ /dev/null
@@ -1,80 +0,0 @@
-# Copyright (c) 2004 Canonical Limited
-#       Author: Robert Collins <robert.collins at canonical.com>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-#
-
-import sys
-import logging
-import unittest
-
-
-class LogCollector(logging.Handler):
-    def __init__(self):
-        logging.Handler.__init__(self)
-        self.records=[]
-    def emit(self, record):
-        self.records.append(record.getMessage())
-
-
-def makeCollectingLogger():
-    """I make a logger instance that collects its logs for programmatic analysis
-    -> (logger, collector)"""
-    logger=logging.Logger("collector")
-    handler=LogCollector()
-    handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
-    logger.addHandler(handler)
-    return logger, handler
-
-
-def visitTests(suite, visitor):
-    """A foreign method for visiting the tests in a test suite."""
-    for test in suite._tests:
-        #Abusing types to avoid monkey patching unittest.TestCase.
-        # Maybe that would be better?
-        try:
-            test.visit(visitor)
-        except AttributeError:
-            if isinstance(test, unittest.TestCase):
-                visitor.visitCase(test)
-            elif isinstance(test, unittest.TestSuite):
-                visitor.visitSuite(test)
-                visitTests(test, visitor)
-            else:
-                print ("unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__))
-
-
-class TestSuite(unittest.TestSuite):
-    """I am an extended TestSuite with a visitor interface.
-    This is primarily to allow filtering of tests - and suites or
-    more in the future. An iterator of just tests wouldn't scale..."""
-
-    def visit(self, visitor):
-        """visit the composite. Visiting is depth-first.
-        current callbacks are visitSuite and visitCase."""
-        visitor.visitSuite(self)
-        visitTests(self, visitor)
-
-
-class TestLoader(unittest.TestLoader):
-    """Custome TestLoader to set the right TestSuite class."""
-    suiteClass = TestSuite
-
-class TestVisitor(object):
-    """A visitor for Tests"""
-    def visitSuite(self, aTestSuite):
-        pass
-    def visitCase(self, aTestCase):
-        pass
diff --git a/lib/subunit/python/subunit/tests/__init__.py b/lib/subunit/python/subunit/tests/__init__.py
index e0e1eb1..c1c2c64 100644
--- a/lib/subunit/python/subunit/tests/__init__.py
+++ b/lib/subunit/python/subunit/tests/__init__.py
@@ -6,7 +6,7 @@
 #  license at the users choice. A copy of both licenses are available in the
 #  project source as Apache-2.0 and BSD. You may not use this file except in
 #  compliance with one of these two licences.
-#  
+#
 #  Unless required by applicable law or agreed to in writing, software
 #  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
@@ -14,10 +14,29 @@
 #  limitations under that license.
 #
 
+import sys
+from unittest import TestLoader
+
+from testscenarios import generate_scenarios
+
+
+# Before the test module imports to avoid circularity.
+# For testing: different pythons have different str() implementations.
+if sys.version_info > (3, 0):
+    _remote_exception_repr = "testtools.testresult.real._StringException"
+    _remote_exception_str = "Traceback (most recent call last):\ntesttools.testresult.real._StringException"
+    _remote_exception_str_chunked = "57\r\n" + _remote_exception_str + ": boo qux\n0\r\n"
+else:
+    _remote_exception_repr = "_StringException" 
+    _remote_exception_str = "Traceback (most recent call last):\n_StringException" 
+    _remote_exception_str_chunked = "3D\r\n" + _remote_exception_str + ": boo qux\n0\r\n"
+
+
 from subunit.tests import (
-    TestUtil,
     test_chunked,
     test_details,
+    test_filters,
+    test_output_filter,
     test_progress_model,
     test_run,
     test_subunit_filter,
@@ -25,19 +44,26 @@ from subunit.tests import (
     test_subunit_tags,
     test_tap2subunit,
     test_test_protocol,
+    test_test_protocol2,
     test_test_results,
     )
 
+
 def test_suite():
-    result = TestUtil.TestSuite()
-    result.addTest(test_chunked.test_suite())
-    result.addTest(test_details.test_suite())
-    result.addTest(test_progress_model.test_suite())
-    result.addTest(test_test_results.test_suite())
-    result.addTest(test_test_protocol.test_suite())
-    result.addTest(test_tap2subunit.test_suite())
-    result.addTest(test_subunit_filter.test_suite())
-    result.addTest(test_subunit_tags.test_suite())
-    result.addTest(test_subunit_stats.test_suite())
-    result.addTest(test_run.test_suite())
+    loader = TestLoader()
+    result = loader.loadTestsFromModule(test_chunked)
+    result.addTest(loader.loadTestsFromModule(test_details))
+    result.addTest(loader.loadTestsFromModule(test_filters))
+    result.addTest(loader.loadTestsFromModule(test_progress_model))
+    result.addTest(loader.loadTestsFromModule(test_test_results))
+    result.addTest(loader.loadTestsFromModule(test_test_protocol))
+    result.addTest(loader.loadTestsFromModule(test_test_protocol2))
+    result.addTest(loader.loadTestsFromModule(test_tap2subunit))
+    result.addTest(loader.loadTestsFromModule(test_subunit_filter))
+    result.addTest(loader.loadTestsFromModule(test_subunit_tags))
+    result.addTest(loader.loadTestsFromModule(test_subunit_stats))
+    result.addTest(loader.loadTestsFromModule(test_run))
+    result.addTests(
+        generate_scenarios(loader.loadTestsFromModule(test_output_filter))
+    )
     return result
diff --git a/lib/subunit/python/subunit/tests/test_chunked.py b/lib/subunit/python/subunit/tests/test_chunked.py
index e0742f1..5100b32 100644
--- a/lib/subunit/python/subunit/tests/test_chunked.py
+++ b/lib/subunit/python/subunit/tests/test_chunked.py
@@ -22,12 +22,6 @@ from testtools.compat import _b, BytesIO
 import subunit.chunked
 
 
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
-
-
 class TestDecode(unittest.TestCase):
 
     def setUp(self):
diff --git a/lib/subunit/python/subunit/tests/test_details.py b/lib/subunit/python/subunit/tests/test_details.py
index 746aa04..8605c5a 100644
--- a/lib/subunit/python/subunit/tests/test_details.py
+++ b/lib/subunit/python/subunit/tests/test_details.py
@@ -22,12 +22,6 @@ import subunit.tests
 from subunit import content, content_type, details
 
 
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
-
-
 class TestSimpleDetails(unittest.TestCase):
 
     def test_lineReceived(self):
diff --git a/lib/subunit/python/subunit/tests/test_progress_model.py b/lib/subunit/python/subunit/tests/test_progress_model.py
index 76200c6..2ca0888 100644
--- a/lib/subunit/python/subunit/tests/test_progress_model.py
+++ b/lib/subunit/python/subunit/tests/test_progress_model.py
@@ -110,9 +110,3 @@ class TestProgressModel(unittest.TestCase):
         progress.advance()
         progress.pop()
         self.assertProgressSummary(1, 3, progress)
-
-
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
diff --git a/lib/subunit/python/subunit/tests/test_run.py b/lib/subunit/python/subunit/tests/test_run.py
index 10519ed..3339a82 100644
--- a/lib/subunit/python/subunit/tests/test_run.py
+++ b/lib/subunit/python/subunit/tests/test_run.py
@@ -14,39 +14,92 @@
 #  limitations under that license.
 #
 
-from testtools.compat import BytesIO
+import io
 import unittest
 
-from testtools import PlaceHolder
+from testtools import PlaceHolder, TestCase
+from testtools.compat import _b
+from testtools.matchers import StartsWith
+from testtools.testresult.doubles import StreamResult
 
 import subunit
+from subunit import run
 from subunit.run import SubunitTestRunner
 
 
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
+class TestSubunitTestRunner(TestCase):
 
+    def test_includes_timing_output(self):
+        bytestream = io.BytesIO()
+        runner = SubunitTestRunner(stream=bytestream)
+        test = PlaceHolder('name')
+        runner.run(test)
+        bytestream.seek(0)
+        eventstream = StreamResult()
+        subunit.ByteStreamToStreamResult(bytestream).run(eventstream)
+        timestamps = [event[-1] for event in eventstream._events
+            if event is not None]
+        self.assertNotEqual([], timestamps)
 
-class TimeCollectingTestResult(unittest.TestResult):
+    def test_enumerates_tests_before_run(self):
+        bytestream = io.BytesIO()
+        runner = SubunitTestRunner(stream=bytestream)
+        test1 = PlaceHolder('name1')
+        test2 = PlaceHolder('name2')
+        case = unittest.TestSuite([test1, test2])
+        runner.run(case)
+        bytestream.seek(0)
+        eventstream = StreamResult()
+        subunit.ByteStreamToStreamResult(bytestream).run(eventstream)
+        self.assertEqual([
+            ('status', 'name1', 'exists'),
+            ('status', 'name2', 'exists'),
+            ], [event[:3] for event in eventstream._events[:2]])
 
-    def __init__(self, *args, **kwargs):
-        super(TimeCollectingTestResult, self).__init__(*args, **kwargs)
-        self.time_called = []
+    def test_list_errors_if_errors_from_list_test(self):
+        bytestream = io.BytesIO()
+        runner = SubunitTestRunner(stream=bytestream)
+        def list_test(test):
+            return [], ['failed import']
+        self.patch(run, 'list_test', list_test)
+        exc = self.assertRaises(SystemExit, runner.list, None)
+        self.assertEqual((2,), exc.args)
 
-    def time(self, a_time):
-        self.time_called.append(a_time)
+    def test_list_includes_loader_errors(self):
+        bytestream = io.BytesIO()
+        runner = SubunitTestRunner(stream=bytestream)
+        def list_test(test):
+            return [], []
+        class Loader(object):
+            errors = ['failed import']
+        loader = Loader()
+        self.patch(run, 'list_test', list_test)
+        exc = self.assertRaises(SystemExit, runner.list, None, loader=loader)
+        self.assertEqual((2,), exc.args)
 
+    class FailingTest(TestCase):
+        def test_fail(self):
+            1/0
 
-class TestSubunitTestRunner(unittest.TestCase):
+    def test_exits_zero_when_tests_fail(self):
+        bytestream = io.BytesIO()
+        stream = io.TextIOWrapper(bytestream, encoding="utf8")
+        try:
+            self.assertEqual(None, run.main(
+                argv=["progName", "subunit.tests.test_run.TestSubunitTestRunner.FailingTest"],
+                stdout=stream))
+        except SystemExit:
+            self.fail("SystemExit raised")
+        self.assertThat(bytestream.getvalue(), StartsWith(_b('\xb3')))
 
-    def test_includes_timing_output(self):
-        io = BytesIO()
-        runner = SubunitTestRunner(stream=io)
-        test = PlaceHolder('name')
-        runner.run(test)
-        client = TimeCollectingTestResult()
-        io.seek(0)
-        subunit.TestProtocolServer(client).readFrom(io)
-        self.assertTrue(len(client.time_called) > 0)
+    class ExitingTest(TestCase):
+        def test_exit(self):
+            raise SystemExit(0)
+
+    def test_exits_nonzero_when_execution_errors(self):
+        bytestream = io.BytesIO()
+        stream = io.TextIOWrapper(bytestream, encoding="utf8")
+        exc = self.assertRaises(SystemExit, run.main,
+                argv=["progName", "subunit.tests.test_run.TestSubunitTestRunner.ExitingTest"],
+                stdout=stream)
+        self.assertEqual(0, exc.args[0])
diff --git a/lib/subunit/python/subunit/tests/test_subunit_filter.py b/lib/subunit/python/subunit/tests/test_subunit_filter.py
index 33b9248..5f34b3b 100644
--- a/lib/subunit/python/subunit/tests/test_subunit_filter.py
+++ b/lib/subunit/python/subunit/tests/test_subunit_filter.py
@@ -25,10 +25,11 @@ import unittest
 
 from testtools import TestCase
 from testtools.compat import _b, BytesIO
-from testtools.testresult.doubles import ExtendedTestResult
+from testtools.testresult.doubles import ExtendedTestResult, StreamResult
 
 import subunit
 from subunit.test_results import make_tag_filter, TestResultFilter
+from subunit import ByteStreamToStreamResult, StreamResultToBytes
 
 
 class TestTestResultFilter(TestCase):
@@ -286,23 +287,6 @@ xfail todo
 
 class TestFilterCommand(TestCase):
 
-    example_subunit_stream = _b("""\
-tags: global
-test passed
-success passed
-test failed
-tags: local
-failure failed
-test error
-error error [
-error details
-]
-test skipped
-skip skipped
-test todo
-xfail todo
-""")
-
     def run_command(self, args, stream):
         root = os.path.dirname(
             os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
@@ -316,55 +300,47 @@ xfail todo
             raise RuntimeError("%s failed: %s" % (command, err))
         return out
 
-    def to_events(self, stream):
-        test = subunit.ProtocolTestCase(BytesIO(stream))
-        result = ExtendedTestResult()
-        test.run(result)
-        return result._events
-
     def test_default(self):
-        output = self.run_command([], _b(
-                "test: foo\n"
-                "skip: foo\n"
-                ))
-        events = self.to_events(output)
-        foo = subunit.RemotedTestCase('foo')
-        self.assertEqual(
-            [('startTest', foo),
-             ('addSkip', foo, {}),
-             ('stopTest', foo)],
-            events)
+        byte_stream = BytesIO()
+        stream = StreamResultToBytes(byte_stream)
+        stream.status(test_id="foo", test_status="inprogress")
+        stream.status(test_id="foo", test_status="skip")
+        output = self.run_command([], byte_stream.getvalue())
+        events = StreamResult()
+        ByteStreamToStreamResult(BytesIO(output)).run(events)
+        ids = set(event[1] for event in events._events)
+        self.assertEqual([
+            ('status', 'foo', 'inprogress'),
+            ('status', 'foo', 'skip'),
+            ], [event[:3] for event in events._events])
 
     def test_tags(self):
-        output = self.run_command(['-s', '--with-tag', 'a'], _b(
-                "tags: a\n"
-                "test: foo\n"
-                "success: foo\n"
-                "tags: -a\n"
-                "test: bar\n"
-                "success: bar\n"
-                "test: baz\n"
-                "tags: a\n"
-                "success: baz\n"
-                ))
-        events = self.to_events(output)
-        foo = subunit.RemotedTestCase('foo')
-        baz = subunit.RemotedTestCase('baz')
-        self.assertEqual(
-            [('tags', set(['a']), set()),
-             ('startTest', foo),
-             ('addSuccess', foo),
-             ('stopTest', foo),
-             ('tags', set(), set(['a'])),
-             ('startTest', baz),
-             ('tags', set(['a']), set()),
-             ('addSuccess', baz),
-             ('stopTest', baz),
-             ],
-            events)
-
-
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
+        byte_stream = BytesIO()
+        stream = StreamResultToBytes(byte_stream)
+        stream.status(
+            test_id="foo", test_status="inprogress", test_tags=set(["a"]))
+        stream.status(
+            test_id="foo", test_status="success", test_tags=set(["a"]))
+        stream.status(test_id="bar", test_status="inprogress")
+        stream.status(test_id="bar", test_status="inprogress")
+        stream.status(
+            test_id="baz", test_status="inprogress", test_tags=set(["a"]))
+        stream.status(
+            test_id="baz", test_status="success", test_tags=set(["a"]))
+        output = self.run_command(
+            ['-s', '--with-tag', 'a'], byte_stream.getvalue())
+        events = StreamResult()
+        ByteStreamToStreamResult(BytesIO(output)).run(events)
+        ids = set(event[1] for event in events._events)
+        self.assertEqual(set(['foo', 'baz']), ids)
+
+    def test_no_passthrough(self):
+        output = self.run_command(['--no-passthrough'], b'hi thar')
+        self.assertEqual(b'', output)
+
+    def test_passthrough(self):
+        output = self.run_command([], b'hi thar')
+        byte_stream = BytesIO()
+        stream = StreamResultToBytes(byte_stream)
+        stream.status(file_name="stdout", file_bytes=b'hi thar')
+        self.assertEqual(byte_stream.getvalue(), output)
diff --git a/lib/subunit/python/subunit/tests/test_subunit_stats.py b/lib/subunit/python/subunit/tests/test_subunit_stats.py
index 6fd3301..7c5e42d 100644
--- a/lib/subunit/python/subunit/tests/test_subunit_stats.py
+++ b/lib/subunit/python/subunit/tests/test_subunit_stats.py
@@ -76,9 +76,3 @@ Seen tags: global, local
         self.setUpUsedStream()
         self.result.formatStats()
         self.assertEqual(expected, self.output.getvalue())
-
-
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
diff --git a/lib/subunit/python/subunit/tests/test_subunit_tags.py b/lib/subunit/python/subunit/tests/test_subunit_tags.py
index c98506a..a16edc1 100644
--- a/lib/subunit/python/subunit/tests/test_subunit_tags.py
+++ b/lib/subunit/python/subunit/tests/test_subunit_tags.py
@@ -16,54 +16,70 @@
 
 """Tests for subunit.tag_stream."""
 
-import unittest
+from io import BytesIO
 
-from testtools.compat import StringIO
+import testtools
+from testtools.matchers import Contains
 
 import subunit
 import subunit.test_results
 
 
-class TestSubUnitTags(unittest.TestCase):
+class TestSubUnitTags(testtools.TestCase):
 
     def setUp(self):
-        self.original = StringIO()
-        self.filtered = StringIO()
+        super(TestSubUnitTags, self).setUp()
+        self.original = BytesIO()
+        self.filtered = BytesIO()
 
     def test_add_tag(self):
-        self.original.write("tags: foo\n")
-        self.original.write("test: test\n")
-        self.original.write("tags: bar -quux\n")
-        self.original.write("success: test\n")
+        # Literal values to avoid set sort-order dependencies. Python code show
+        # derivation.
+        # reference = BytesIO()
+        # stream = subunit.StreamResultToBytes(reference)
+        # stream.status(
+        #     test_id='test', test_status='inprogress', test_tags=set(['quux', 'foo']))
+        # stream.status(
+        #     test_id='test', test_status='success', test_tags=set(['bar', 'quux', 'foo']))
+        reference = [
+            b'\xb3)\x82\x17\x04test\x02\x04quux\x03foo\x05\x97n\x86\xb3)'
+                b'\x83\x1b\x04test\x03\x03bar\x04quux\x03fooqn\xab)',
+            b'\xb3)\x82\x17\x04test\x02\x04quux\x03foo\x05\x97n\x86\xb3)'
+                b'\x83\x1b\x04test\x03\x04quux\x03foo\x03bar\xaf\xbd\x9d\xd6',
+            b'\xb3)\x82\x17\x04test\x02\x04quux\x03foo\x05\x97n\x86\xb3)'
+                b'\x83\x1b\x04test\x03\x04quux\x03bar\x03foo\x03\x04b\r',
+            b'\xb3)\x82\x17\x04test\x02\x04quux\x03foo\x05\x97n\x86\xb3)'
+                b'\x83\x1b\x04test\x03\x03bar\x03foo\x04quux\xd2\x18\x1bC',
+            b'\xb3)\x82\x17\x04test\x02\x03foo\x04quux\xa6\xe1\xde\xec\xb3)'
+                b'\x83\x1b\x04test\x03\x03foo\x04quux\x03bar\x08\xc2X\x83',
+            b'\xb3)\x82\x17\x04test\x02\x03foo\x04quux\xa6\xe1\xde\xec\xb3)'
+                b'\x83\x1b\x04test\x03\x03bar\x03foo\x04quux\xd2\x18\x1bC',
+            b'\xb3)\x82\x17\x04test\x02\x03foo\x04quux\xa6\xe1\xde\xec\xb3)'
+                b'\x83\x1b\x04test\x03\x03foo\x03bar\x04quux:\x05e\x80',
+            ]
+        stream = subunit.StreamResultToBytes(self.original)
+        stream.status(
+            test_id='test', test_status='inprogress', test_tags=set(['foo']))
+        stream.status(
+            test_id='test', test_status='success', test_tags=set(['foo', 'bar']))
         self.original.seek(0)
-        result = subunit.tag_stream(self.original, self.filtered, ["quux"])
-        self.assertEqual([
-            "tags: quux",
-            "tags: foo",
-            "test: test",
-            "tags: bar",
-            "success: test",
-            ],
-            self.filtered.getvalue().splitlines())
+        self.assertEqual(
+            0, subunit.tag_stream(self.original, self.filtered, ["quux"]))
+        self.assertThat(reference, Contains(self.filtered.getvalue()))
 
     def test_remove_tag(self):
-        self.original.write("tags: foo\n")
-        self.original.write("test: test\n")
-        self.original.write("tags: bar -quux\n")
-        self.original.write("success: test\n")
+        reference = BytesIO()
+        stream = subunit.StreamResultToBytes(reference)
+        stream.status(
+            test_id='test', test_status='inprogress', test_tags=set(['foo']))
+        stream.status(
+            test_id='test', test_status='success', test_tags=set(['foo']))
+        stream = subunit.StreamResultToBytes(self.original)
+        stream.status(
+            test_id='test', test_status='inprogress', test_tags=set(['foo']))
+        stream.status(
+            test_id='test', test_status='success', test_tags=set(['foo', 'bar']))
         self.original.seek(0)
-        result = subunit.tag_stream(self.original, self.filtered, ["-bar"])
-        self.assertEqual([
-            "tags: -bar",
-            "tags: foo",
-            "test: test",
-            "tags: -quux",
-            "success: test",
-            ],
-            self.filtered.getvalue().splitlines())
-
-
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
+        self.assertEqual(
+            0, subunit.tag_stream(self.original, self.filtered, ["-bar"]))
+        self.assertEqual(reference.getvalue(), self.filtered.getvalue())
diff --git a/lib/subunit/python/subunit/tests/test_tap2subunit.py b/lib/subunit/python/subunit/tests/test_tap2subunit.py
index 11bc191..5b7c07a 100644
--- a/lib/subunit/python/subunit/tests/test_tap2subunit.py
+++ b/lib/subunit/python/subunit/tests/test_tap2subunit.py
@@ -16,14 +16,19 @@
 
 """Tests for TAP2SubUnit."""
 
+from io import BytesIO, StringIO
 import unittest
 
-from testtools.compat import StringIO
+from testtools import TestCase
+from testtools.compat import _u
+from testtools.testresult.doubles import StreamResult
 
 import subunit
 
+UTF8_TEXT = 'text/plain; charset=UTF8'
 
-class TestTAP2SubUnit(unittest.TestCase):
+
+class TestTAP2SubUnit(TestCase):
     """Tests for TAP2SubUnit.
 
     These tests test TAP string data in, and subunit string data out.
@@ -34,24 +39,21 @@ class TestTAP2SubUnit(unittest.TestCase):
     """
 
     def setUp(self):
+        super(TestTAP2SubUnit, self).setUp()
         self.tap = StringIO()
-        self.subunit = StringIO()
+        self.subunit = BytesIO()
 
     def test_skip_entire_file(self):
         # A file
         # 1..- # Skipped: comment
         # results in a single skipped test.
-        self.tap.write("1..0 # Skipped: entire file skipped\n")
+        self.tap.write(_u("1..0 # Skipped: entire file skipped\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test file skip",
-            "skip file skip [",
-            "Skipped: entire file skipped",
-            "]",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'file skip', 'skip', None, True,
+            'tap comment', b'Skipped: entire file skipped', True, None, None,
+            None)])
 
     def test_ok_test_pass(self):
         # A file
@@ -59,164 +61,128 @@ class TestTAP2SubUnit(unittest.TestCase):
         # results in a passed test with name 'test 1' (a synthetic name as tap
         # does not require named fixtures - it is the first test in the tap
         # stream).
-        self.tap.write("ok\n")
+        self.tap.write(_u("ok\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "success test 1",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'success', None, False, None,
+            None, True, None, None, None)])
 
     def test_ok_test_number_pass(self):
         # A file
         # ok 1
         # results in a passed test with name 'test 1'
-        self.tap.write("ok 1\n")
+        self.tap.write(_u("ok 1\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "success test 1",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'success', None, False, None,
+            None, True, None, None, None)])
 
     def test_ok_test_number_description_pass(self):
         # A file
         # ok 1 - There is a description
         # results in a passed test with name 'test 1 - There is a description'
-        self.tap.write("ok 1 - There is a description\n")
+        self.tap.write(_u("ok 1 - There is a description\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1 - There is a description",
-            "success test 1 - There is a description",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1 - There is a description',
+            'success', None, False, None, None, True, None, None, None)])
 
     def test_ok_test_description_pass(self):
         # A file
         # ok There is a description
         # results in a passed test with name 'test 1 There is a description'
-        self.tap.write("ok There is a description\n")
+        self.tap.write(_u("ok There is a description\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1 There is a description",
-            "success test 1 There is a description",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1 There is a description',
+            'success', None, False, None, None, True, None, None, None)])
 
     def test_ok_SKIP_skip(self):
         # A file
         # ok # SKIP
         # results in a skkip test with name 'test 1'
-        self.tap.write("ok # SKIP\n")
+        self.tap.write(_u("ok # SKIP\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "skip test 1",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'skip', None, False, None,
+            None, True, None, None, None)])
 
     def test_ok_skip_number_comment_lowercase(self):
-        self.tap.write("ok 1 # skip no samba environment available, skipping compilation\n")
+        self.tap.write(_u("ok 1 # skip no samba environment available, skipping compilation\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "skip test 1 [", 
-            "no samba environment available, skipping compilation",
-            "]"
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'skip', None, False, 'tap comment',
+            b'no samba environment available, skipping compilation', True,
+            'text/plain; charset=UTF8', None, None)])
 
     def test_ok_number_description_SKIP_skip_comment(self):
         # A file
         # ok 1 foo  # SKIP Not done yet
         # results in a skip test with name 'test 1 foo' and a log of
         # Not done yet
-        self.tap.write("ok 1 foo  # SKIP Not done yet\n")
+        self.tap.write(_u("ok 1 foo  # SKIP Not done yet\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1 foo",
-            "skip test 1 foo [",
-            "Not done yet",
-            "]",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1 foo', 'skip', None, False,
+            'tap comment', b'Not done yet', True, 'text/plain; charset=UTF8',
+            None, None)])
 
     def test_ok_SKIP_skip_comment(self):
         # A file
         # ok # SKIP Not done yet
         # results in a skip test with name 'test 1' and a log of Not done yet
-        self.tap.write("ok # SKIP Not done yet\n")
+        self.tap.write(_u("ok # SKIP Not done yet\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "skip test 1 [",
-            "Not done yet",
-            "]",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'skip', None, False,
+            'tap comment', b'Not done yet', True, 'text/plain; charset=UTF8',
+            None, None)])
 
     def test_ok_TODO_xfail(self):
         # A file
         # ok # TODO
         # results in a xfail test with name 'test 1'
-        self.tap.write("ok # TODO\n")
+        self.tap.write(_u("ok # TODO\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "xfail test 1",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'xfail', None, False, None,
+            None, True, None, None, None)])
 
     def test_ok_TODO_xfail_comment(self):
         # A file
         # ok # TODO Not done yet
         # results in a xfail test with name 'test 1' and a log of Not done yet
-        self.tap.write("ok # TODO Not done yet\n")
+        self.tap.write(_u("ok # TODO Not done yet\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1",
-            "xfail test 1 [",
-            "Not done yet",
-            "]",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([('status', 'test 1', 'xfail', None, False,
+            'tap comment', b'Not done yet', True, 'text/plain; charset=UTF8',
+            None, None)])
 
     def test_bail_out_errors(self):
         # A file with line in it
         # Bail out! COMMENT
         # is treated as an error
-        self.tap.write("ok 1 foo\n")
-        self.tap.write("Bail out! Lifejacket engaged\n")
+        self.tap.write(_u("ok 1 foo\n"))
+        self.tap.write(_u("Bail out! Lifejacket engaged\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            "test test 1 foo",
-            "success test 1 foo",
-            "test Bail out! Lifejacket engaged",
-            "error Bail out! Lifejacket engaged",
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 foo', 'success', None, False, None, None, True,
+             None, None, None),
+            ('status', 'Bail out! Lifejacket engaged', 'fail', None, False,
+             None, None, True, None, None, None)])
 
     def test_missing_test_at_end_with_plan_adds_error(self):
         # A file
@@ -224,23 +190,20 @@ class TestTAP2SubUnit(unittest.TestCase):
         # ok first test
         # not ok third test
         # results in three tests, with the third being created
-        self.tap.write('1..3\n')
-        self.tap.write('ok first test\n')
-        self.tap.write('not ok second test\n')
+        self.tap.write(_u('1..3\n'))
+        self.tap.write(_u('ok first test\n'))
+        self.tap.write(_u('not ok second test\n'))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 first test',
-            'success test 1 first test',
-            'test test 2 second test',
-            'failure test 2 second test',
-            'test test 3',
-            'error test 3 [',
-            'test missing from TAP output',
-            ']',
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 first test', 'success', None, False, None,
+             None, True, None, None, None),
+            ('status', 'test 2 second test', 'fail', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 3', 'fail', None, False, 'tap meta',
+             b'test missing from TAP output', True, 'text/plain; charset=UTF8',
+             None, None)])
 
     def test_missing_test_with_plan_adds_error(self):
         # A file
@@ -248,45 +211,39 @@ class TestTAP2SubUnit(unittest.TestCase):
         # ok first test
         # not ok 3 third test
         # results in three tests, with the second being created
-        self.tap.write('1..3\n')
-        self.tap.write('ok first test\n')
-        self.tap.write('not ok 3 third test\n')
+        self.tap.write(_u('1..3\n'))
+        self.tap.write(_u('ok first test\n'))
+        self.tap.write(_u('not ok 3 third test\n'))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 first test',
-            'success test 1 first test',
-            'test test 2',
-            'error test 2 [',
-            'test missing from TAP output',
-            ']',
-            'test test 3 third test',
-            'failure test 3 third test',
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 first test', 'success', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 2', 'fail', None, False, 'tap meta',
+             b'test missing from TAP output', True, 'text/plain; charset=UTF8',
+             None, None),
+            ('status', 'test 3 third test', 'fail', None, False, None, None,
+             True, None, None, None)])
 
     def test_missing_test_no_plan_adds_error(self):
         # A file
         # ok first test
         # not ok 3 third test
         # results in three tests, with the second being created
-        self.tap.write('ok first test\n')
-        self.tap.write('not ok 3 third test\n')
+        self.tap.write(_u('ok first test\n'))
+        self.tap.write(_u('not ok 3 third test\n'))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 first test',
-            'success test 1 first test',
-            'test test 2',
-            'error test 2 [',
-            'test missing from TAP output',
-            ']',
-            'test test 3 third test',
-            'failure test 3 third test',
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 first test', 'success', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 2', 'fail', None, False, 'tap meta',
+             b'test missing from TAP output', True, 'text/plain; charset=UTF8',
+             None, None),
+            ('status', 'test 3 third test', 'fail', None, False, None, None,
+             True, None, None, None)])
 
     def test_four_tests_in_a_row_trailing_plan(self):
         # A file
@@ -296,25 +253,23 @@ class TestTAP2SubUnit(unittest.TestCase):
         # not ok 4 - fourth
         # 1..4
         # results in four tests numbered and named
-        self.tap.write('ok 1 - first test in a script with trailing plan\n')
-        self.tap.write('not ok 2 - second\n')
-        self.tap.write('ok 3 - third\n')
-        self.tap.write('not ok 4 - fourth\n')
-        self.tap.write('1..4\n')
+        self.tap.write(_u('ok 1 - first test in a script with trailing plan\n'))
+        self.tap.write(_u('not ok 2 - second\n'))
+        self.tap.write(_u('ok 3 - third\n'))
+        self.tap.write(_u('not ok 4 - fourth\n'))
+        self.tap.write(_u('1..4\n'))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 - first test in a script with trailing plan',
-            'success test 1 - first test in a script with trailing plan',
-            'test test 2 - second',
-            'failure test 2 - second',
-            'test test 3 - third',
-            'success test 3 - third',
-            'test test 4 - fourth',
-            'failure test 4 - fourth'
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 - first test in a script with trailing plan',
+             'success', None, False, None, None, True, None, None, None),
+            ('status', 'test 2 - second', 'fail', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 3 - third', 'success', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 4 - fourth', 'fail', None, False, None, None,
+             True, None, None, None)])
 
     def test_four_tests_in_a_row_with_plan(self):
         # A file
@@ -324,25 +279,23 @@ class TestTAP2SubUnit(unittest.TestCase):
         # ok 3 - third
         # not ok 4 - fourth
         # results in four tests numbered and named
-        self.tap.write('1..4\n')
-        self.tap.write('ok 1 - first test in a script with a plan\n')
-        self.tap.write('not ok 2 - second\n')
-        self.tap.write('ok 3 - third\n')
-        self.tap.write('not ok 4 - fourth\n')
+        self.tap.write(_u('1..4\n'))
+        self.tap.write(_u('ok 1 - first test in a script with a plan\n'))
+        self.tap.write(_u('not ok 2 - second\n'))
+        self.tap.write(_u('ok 3 - third\n'))
+        self.tap.write(_u('not ok 4 - fourth\n'))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 - first test in a script with a plan',
-            'success test 1 - first test in a script with a plan',
-            'test test 2 - second',
-            'failure test 2 - second',
-            'test test 3 - third',
-            'success test 3 - third',
-            'test test 4 - fourth',
-            'failure test 4 - fourth'
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 - first test in a script with a plan',
+             'success', None, False, None, None, True, None, None, None),
+            ('status', 'test 2 - second', 'fail', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 3 - third', 'success', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 4 - fourth', 'fail', None, False, None, None,
+             True, None, None, None)])
 
     def test_four_tests_in_a_row_no_plan(self):
         # A file
@@ -351,46 +304,43 @@ class TestTAP2SubUnit(unittest.TestCase):
         # ok 3 - third
         # not ok 4 - fourth
         # results in four tests numbered and named
-        self.tap.write('ok 1 - first test in a script with no plan at all\n')
-        self.tap.write('not ok 2 - second\n')
-        self.tap.write('ok 3 - third\n')
-        self.tap.write('not ok 4 - fourth\n')
+        self.tap.write(_u('ok 1 - first test in a script with no plan at all\n'))
+        self.tap.write(_u('not ok 2 - second\n'))
+        self.tap.write(_u('ok 3 - third\n'))
+        self.tap.write(_u('not ok 4 - fourth\n'))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 - first test in a script with no plan at all',
-            'success test 1 - first test in a script with no plan at all',
-            'test test 2 - second',
-            'failure test 2 - second',
-            'test test 3 - third',
-            'success test 3 - third',
-            'test test 4 - fourth',
-            'failure test 4 - fourth'
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1 - first test in a script with no plan at all',
+             'success', None, False, None, None, True, None, None, None),
+            ('status', 'test 2 - second', 'fail', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 3 - third', 'success', None, False, None, None,
+             True, None, None, None),
+            ('status', 'test 4 - fourth', 'fail', None, False, None, None,
+             True, None, None, None)])
 
     def test_todo_and_skip(self):
         # A file
         # not ok 1 - a fail but # TODO but is TODO
         # not ok 2 - another fail # SKIP instead
         # results in two tests, numbered and commented.
-        self.tap.write("not ok 1 - a fail but # TODO but is TODO\n")
-        self.tap.write("not ok 2 - another fail # SKIP instead\n")
+        self.tap.write(_u("not ok 1 - a fail but # TODO but is TODO\n"))
+        self.tap.write(_u("not ok 2 - another fail # SKIP instead\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1 - a fail but',
-            'xfail test 1 - a fail but [',
-            'but is TODO',
-            ']',
-            'test test 2 - another fail',
-            'skip test 2 - another fail [',
-            'instead',
-            ']',
-            ],
-            self.subunit.getvalue().splitlines())
+        self.subunit.seek(0)
+        events = StreamResult()
+        subunit.ByteStreamToStreamResult(self.subunit).run(events)
+        self.check_events([
+            ('status', 'test 1 - a fail but', 'xfail', None, False,
+             'tap comment', b'but is TODO', True, 'text/plain; charset=UTF8',
+             None, None),
+            ('status', 'test 2 - another fail', 'skip', None, False,
+             'tap comment', b'instead', True, 'text/plain; charset=UTF8',
+             None, None)])
 
     def test_leading_comments_add_to_next_test_log(self):
         # A file
@@ -399,21 +349,17 @@ class TestTAP2SubUnit(unittest.TestCase):
         # ok
         # results in a single test with the comment included
         # in the first test and not the second.
-        self.tap.write("# comment\n")
-        self.tap.write("ok\n")
-        self.tap.write("ok\n")
+        self.tap.write(_u("# comment\n"))
+        self.tap.write(_u("ok\n"))
+        self.tap.write(_u("ok\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1',
-            'success test 1 [',
-            '# comment',
-            ']',
-            'test test 2',
-            'success test 2',
-            ],
-            self.subunit.getvalue().splitlines())
+        self.check_events([
+            ('status', 'test 1', 'success', None, False, 'tap comment',
+             b'# comment', True, 'text/plain; charset=UTF8', None, None),
+            ('status', 'test 2', 'success', None, False, None, None, True,
+             None, None, None)])
     
     def test_trailing_comments_are_included_in_last_test_log(self):
         # A file
@@ -422,24 +368,20 @@ class TestTAP2SubUnit(unittest.TestCase):
         # # comment
         # results in a two tests, with the second having the comment
         # attached to its log.
-        self.tap.write("ok\n")
-        self.tap.write("ok\n")
-        self.tap.write("# comment\n")
+        self.tap.write(_u("ok\n"))
+        self.tap.write(_u("ok\n"))
+        self.tap.write(_u("# comment\n"))
         self.tap.seek(0)
         result = subunit.TAP2SubUnit(self.tap, self.subunit)
         self.assertEqual(0, result)
-        self.assertEqual([
-            'test test 1',
-            'success test 1',
-            'test test 2',
-            'success test 2 [',
-            '# comment',
-            ']',
-            ],
-            self.subunit.getvalue().splitlines())
-
+        self.check_events([
+            ('status', 'test 1', 'success', None, False, None, None, True,
+             None, None, None),
+            ('status', 'test 2', 'success', None, False, 'tap comment',
+             b'# comment', True, 'text/plain; charset=UTF8', None, None)])
 
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
+    def check_events(self, events):
+        self.subunit.seek(0)
+        eventstream = StreamResult()
+        subunit.ByteStreamToStreamResult(self.subunit).run(eventstream)
+        self.assertEqual(events, eventstream._events)
diff --git a/lib/subunit/python/subunit/tests/test_test_protocol.py b/lib/subunit/python/subunit/tests/test_test_protocol.py
index 7831ba1..c6008f4 100644
--- a/lib/subunit/python/subunit/tests/test_test_protocol.py
+++ b/lib/subunit/python/subunit/tests/test_test_protocol.py
@@ -34,9 +34,14 @@ except ImportError:
         Python27TestResult,
         ExtendedTestResult,
         )
+from testtools.matchers import Contains
 
 import subunit
-from subunit import _remote_exception_str, _remote_exception_str_chunked
+from subunit.tests import (
+    _remote_exception_repr,
+    _remote_exception_str,
+    _remote_exception_str_chunked,
+    )
 import subunit.iso8601 as iso8601
 
 
@@ -104,10 +109,10 @@ class TestTestProtocolServerPipe(unittest.TestCase):
         bing = subunit.RemotedTestCase("bing crosby")
         an_error = subunit.RemotedTestCase("an error")
         self.assertEqual(client.errors,
-                         [(an_error, _remote_exception_str + '\n')])
+                         [(an_error, _remote_exception_repr + '\n')])
         self.assertEqual(
             client.failures,
-            [(bing, _remote_exception_str + ": "
+            [(bing, _remote_exception_repr + ": "
               + details_to_str({'traceback': text_content(traceback)}) + "\n")])
         self.assertEqual(client.testsRun, 3)
 
@@ -962,7 +967,7 @@ class TestRemotedTestCase(unittest.TestCase):
                          "'A test description'>", "%r" % test)
         result = unittest.TestResult()
         test.run(result)
-        self.assertEqual([(test, _remote_exception_str + ": "
+        self.assertEqual([(test, _remote_exception_repr + ": "
                                  "Cannot run RemotedTestCases.\n\n")],
                          result.errors)
         self.assertEqual(1, result.testsRun)
@@ -1128,9 +1133,10 @@ class TestIsolatedTestSuite(TestCase):
         self.assertEqual(self.SampleTestToIsolate.TEST, False)
 
 
-class TestTestProtocolClient(unittest.TestCase):
+class TestTestProtocolClient(TestCase):
 
     def setUp(self):
+        super(TestTestProtocolClient, self).setUp()
         self.io = BytesIO()
         self.protocol = subunit.TestProtocolClient(self.io)
         self.unicode_test = PlaceHolder(_u('\u2603'))
@@ -1191,15 +1197,23 @@ class TestTestProtocolClient(unittest.TestCase):
         """Test addFailure on a TestProtocolClient with details."""
         self.protocol.addFailure(
             self.test, details=self.sample_tb_details)
-        self.assertEqual(
-            self.io.getvalue(),
+        self.assertThat([
             _b(("failure: %s [ multipart\n"
             "Content-Type: text/plain\n"
             "something\n"
             "F\r\nserialised\nform0\r\n"
             "Content-Type: text/x-traceback;charset=utf8,language=python\n"
-            "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n"
-            "]\n") % self.test.id()))
+            "traceback\n" + _remote_exception_str_chunked +
+            "]\n") % self.test.id()),
+            _b(("failure: %s [ multipart\n"
+            "Content-Type: text/plain\n"
+            "something\n"
+            "F\r\nserialised\nform0\r\n"
+            "Content-Type: text/x-traceback;language=python,charset=utf8\n"
+            "traceback\n" + _remote_exception_str_chunked +
+            "]\n") % self.test.id()),
+            ],
+            Contains(self.io.getvalue())),
 
     def test_add_error(self):
         """Test stopTest on a TestProtocolClient."""
@@ -1215,15 +1229,23 @@ class TestTestProtocolClient(unittest.TestCase):
         """Test stopTest on a TestProtocolClient with details."""
         self.protocol.addError(
             self.test, details=self.sample_tb_details)
-        self.assertEqual(
-            self.io.getvalue(),
+        self.assertThat([
             _b(("error: %s [ multipart\n"
             "Content-Type: text/plain\n"
             "something\n"
             "F\r\nserialised\nform0\r\n"
             "Content-Type: text/x-traceback;charset=utf8,language=python\n"
-            "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n"
-            "]\n") % self.test.id()))
+            "traceback\n" + _remote_exception_str_chunked +
+            "]\n") % self.test.id()),
+            _b(("error: %s [ multipart\n"
+            "Content-Type: text/plain\n"
+            "something\n"
+            "F\r\nserialised\nform0\r\n"
+            "Content-Type: text/x-traceback;language=python,charset=utf8\n"
+            "traceback\n" + _remote_exception_str_chunked +
+            "]\n") % self.test.id()),
+            ],
+            Contains(self.io.getvalue())),
 
     def test_add_expected_failure(self):
         """Test addExpectedFailure on a TestProtocolClient."""
@@ -1239,16 +1261,23 @@ class TestTestProtocolClient(unittest.TestCase):
         """Test addExpectedFailure on a TestProtocolClient with details."""
         self.protocol.addExpectedFailure(
             self.test, details=self.sample_tb_details)
-        self.assertEqual(
-            self.io.getvalue(),
+        self.assertThat([
             _b(("xfail: %s [ multipart\n"
             "Content-Type: text/plain\n"
             "something\n"
             "F\r\nserialised\nform0\r\n"
             "Content-Type: text/x-traceback;charset=utf8,language=python\n"
-            "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n"
-            "]\n") % self.test.id()))
-
+            "traceback\n" + _remote_exception_str_chunked +
+            "]\n") % self.test.id()),
+            _b(("xfail: %s [ multipart\n"
+            "Content-Type: text/plain\n"
+            "something\n"
+            "F\r\nserialised\nform0\r\n"
+            "Content-Type: text/x-traceback;language=python,charset=utf8\n"
+            "traceback\n" + _remote_exception_str_chunked +
+            "]\n") % self.test.id()),
+            ],
+            Contains(self.io.getvalue())),
 
     def test_add_skip(self):
         """Test addSkip on a TestProtocolClient."""
@@ -1324,14 +1353,10 @@ class TestTestProtocolClient(unittest.TestCase):
 
     def test_tags_both(self):
         self.protocol.tags(set(['quux']), set(['bar']))
-        self.assertEqual(_b("tags: quux -bar\n"), self.io.getvalue())
+        self.assertThat(
+            [b"tags: quux -bar\n", b"tags: -bar quux\n"],
+            Contains(self.io.getvalue()))
 
     def test_tags_gone(self):
         self.protocol.tags(set(), set(['bar']))
         self.assertEqual(_b("tags: -bar\n"), self.io.getvalue())
-
-
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
diff --git a/lib/subunit/python/subunit/tests/test_test_results.py b/lib/subunit/python/subunit/tests/test_test_results.py
index ff74b9a..44f95b3 100644
--- a/lib/subunit/python/subunit/tests/test_test_results.py
+++ b/lib/subunit/python/subunit/tests/test_test_results.py
@@ -564,9 +564,3 @@ class TestCsvResult(testtools.TestCase):
         stream = StringIO()
         subunit.test_results.CsvResult(stream)
         self.assertEqual([], self.parse_stream(stream))
-
-
-def test_suite():
-    loader = subunit.tests.TestUtil.TestLoader()
-    result = loader.loadTestsFromName(__name__)
-    return result
diff --git a/lib/subunit/runtests.py b/lib/subunit/runtests.py
deleted file mode 100755
index 8ecc6cd..0000000
--- a/lib/subunit/runtests.py
+++ /dev/null
@@ -1,138 +0,0 @@
-#!/usr/bin/env python
-# -*- Mode: python -*-
-#
-# Copyright (C) 2004 Canonical.com
-#       Author:      Robert Collins <robert.collins at canonical.com>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-#
-
-import unittest
-from subunit.tests.TestUtil import TestVisitor, TestSuite
-import subunit
-import sys
-import os
-import shutil
-import logging
-
-class ParameterisableTextTestRunner(unittest.TextTestRunner):
-    """I am a TextTestRunner whose result class is
-    parameterisable without further subclassing"""
-    def __init__(self, **args):
-        unittest.TextTestRunner.__init__(self, **args)
-        self._resultFactory=None
-    def resultFactory(self, *args):
-        """set or retrieve the result factory"""
-        if args:
-            self._resultFactory=args[0]
-            return self
-        if self._resultFactory is None:
-            self._resultFactory=unittest._TextTestResult
-        return self._resultFactory
-
-    def _makeResult(self):
-        return self.resultFactory()(self.stream, self.descriptions, self.verbosity)
-
-
-class EarlyStoppingTextTestResult(unittest._TextTestResult):
-    """I am a TextTestResult that can optionally stop at the first failure
-    or error"""
-
-    def addError(self, test, err):
-        unittest._TextTestResult.addError(self, test, err)
-        if self.stopOnError():
-            self.stop()
-
-    def addFailure(self, test, err):
-        unittest._TextTestResult.addError(self, test, err)
-        if self.stopOnFailure():
-            self.stop()
-
-    def stopOnError(self, *args):
-        """should this result indicate an abort when an error occurs?
-        TODO parameterise this"""
-        return True
-
-    def stopOnFailure(self, *args):
-        """should this result indicate an abort when a failure error occurs?
-        TODO parameterise this"""
-        return True
-
-
-def earlyStopFactory(*args, **kwargs):
-    """return a an early stopping text test result"""
-    result=EarlyStoppingTextTestResult(*args, **kwargs)
-    return result
-
-
-class ShellTests(subunit.ExecTestCase):
-
-    def test_sourcing(self):
-        """./shell/tests/test_source_library.sh"""
-
-    def test_functions(self):
-        """./shell/tests/test_function_output.sh"""
-
-
-def test_suite():
-    result = TestSuite()
-    result.addTest(subunit.test_suite())
-    result.addTest(ShellTests('test_sourcing'))
-    result.addTest(ShellTests('test_functions'))
-    return result
-
-
-class filteringVisitor(TestVisitor):
-    """I accrue all the testCases I visit that pass a regexp filter on id
-    into my suite
-    """
-
-    def __init__(self, filter):
-        import re
-        TestVisitor.__init__(self)
-        self._suite=None
-        self.filter=re.compile(filter)
-
-    def suite(self):
-        """answer the suite we are building"""
-        if self._suite is None:
-            self._suite=TestSuite()
-        return self._suite
-
-    def visitCase(self, aCase):
-        if self.filter.match(aCase.id()):
-            self.suite().addTest(aCase)
-
-
-def main(argv):
-    """To parameterise what tests are run, run this script like so:
-    python test_all.py REGEX
-    i.e.
-    python test_all.py .*Protocol.*
-    to run all tests with Protocol in their id."""
-    if len(argv) > 1:
-        pattern = argv[1]
-    else:
-        pattern = ".*"
-    visitor = filteringVisitor(pattern)
-    test_suite().visit(visitor)
-    runner = ParameterisableTextTestRunner(verbosity=2)
-    runner.resultFactory(unittest._TextTestResult)
-    if not runner.run(visitor.suite()).wasSuccessful():
-        return 1
-    return 0
-
-if __name__ == '__main__':
-    sys.exit(main(sys.argv))
diff --git a/lib/subunit/setup.py b/lib/subunit/setup.py
index 1a0b192..d42d3d7 100755
--- a/lib/subunit/setup.py
+++ b/lib/subunit/setup.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+import os.path
 try:
     # If the user has setuptools / distribute installed, use it
     from setuptools import setup
@@ -9,8 +10,12 @@ except ImportError:
 else:
     extra = {
         'install_requires': [
-            'testtools>=0.9.23',
-        ]
+            'extras',
+            'testtools>=0.9.34',
+        ],
+        'tests_require': [
+            'testscenarios',
+        ],
     }
 
 
@@ -31,6 +36,9 @@ VERSION = (
     or "0.0")
 
 
+relpath = os.path.dirname(__file__)
+if relpath:
+    os.chdir(relpath)
 setup(
     name='python-subunit',
     version=VERSION,
@@ -49,14 +57,18 @@ setup(
     packages=['subunit', 'subunit.tests'],
     package_dir={'subunit': 'python/subunit'},
     scripts = [
-        'filters/subunit2gtk',
-        'filters/subunit2junitxml',
-        'filters/subunit2pyunit',
+        'filters/subunit-1to2',
+        'filters/subunit-2to1',
         'filters/subunit-filter',
         'filters/subunit-ls',
         'filters/subunit-notify',
+        'filters/subunit-output',
         'filters/subunit-stats',
         'filters/subunit-tags',
+        'filters/subunit2csv',
+        'filters/subunit2gtk',
+        'filters/subunit2junitxml',
+        'filters/subunit2pyunit',
         'filters/tap2subunit',
     ],
     **extra
diff --git a/lib/testtools/.testr.conf b/lib/testtools/.testr.conf
index 8a65628..e695109 100644
--- a/lib/testtools/.testr.conf
+++ b/lib/testtools/.testr.conf
@@ -1,4 +1,4 @@
 [DEFAULT]
-test_command=${PYTHON:-python} -m subunit.run discover . $LISTOPT $IDOPTION
+test_command=${PYTHON:-python} -m subunit.run $LISTOPT $IDOPTION testtools.tests.test_suite
 test_id_option=--load-list $IDFILE
 test_list_option=--list
diff --git a/lib/testtools/LICENSE b/lib/testtools/LICENSE
index d59dc7c..21010cc 100644
--- a/lib/testtools/LICENSE
+++ b/lib/testtools/LICENSE
@@ -17,6 +17,7 @@ The testtools authors are:
  * Gavin Panella
  * Martin Pool
  * Vincent Ladeuil
+ * Nikola Đipanov
 
 and are collectively referred to as "testtools developers".
 
diff --git a/lib/testtools/MANIFEST.in b/lib/testtools/MANIFEST.in
index 7da191a..4619349 100644
--- a/lib/testtools/MANIFEST.in
+++ b/lib/testtools/MANIFEST.in
@@ -2,8 +2,8 @@ include LICENSE
 include Makefile
 include MANIFEST.in
 include NEWS
-include README
-include .bzrignore
+include README.rst
+include .gitignore
 graft doc
 graft doc/_static
 graft doc/_templates
diff --git a/lib/testtools/Makefile b/lib/testtools/Makefile
index b3e40ec..c637123 100644
--- a/lib/testtools/Makefile
+++ b/lib/testtools/Makefile
@@ -1,4 +1,4 @@
-# See README for copyright and licensing details.
+# Copyright (c) 2008-2013 testtools developers. See LICENSE for details.
 
 PYTHON=python
 SOURCES=$(shell find testtools -name "*.py")
@@ -21,11 +21,11 @@ prerelease:
 	-rm MANIFEST
 
 release:
-	./setup.py sdist upload --sign
+	./setup.py sdist bdist_wheel upload --sign
 	$(PYTHON) scripts/_lp_release.py
 
 snapshot: prerelease
-	./setup.py sdist
+	./setup.py sdist bdist_wheel
 
 ### Documentation ###
 
@@ -34,7 +34,7 @@ apidocs:
 	PYTHONWARNINGS='ignore::DeprecationWarning' \
 		pydoctor --make-html --add-package testtools \
 		--docformat=restructuredtext --project-name=testtools \
-		--project-url=https://launchpad.net/testtools
+		--project-url=https://github.com/testing-cabal/testtools
 
 doc/news.rst:
 	ln -s ../NEWS doc/news.rst
diff --git a/lib/testtools/NEWS b/lib/testtools/NEWS
index ac88fc1..5176486 100644
--- a/lib/testtools/NEWS
+++ b/lib/testtools/NEWS
@@ -3,15 +3,75 @@ testtools NEWS
 
 Changes and improvements to testtools_, grouped by release.
 
+
 NEXT
 ~~~~
 
+1.5.0
+~~~~~
+
+Improvements
+------------
+
+* When an import error happens ``testtools.run`` will now show the full
+  error rather than just the name of the module that failed to import.
+  (Robert Collins)
+
+1.4.0
+~~~~~
+
+Changes
+-------
+
+* ``testtools.TestCase`` now inherits from unittest2.TestCase, which
+  provides a ``setUpClass`` for upcalls on Python 2.6.
+  (Robert Collins, #1393283)
+
+1.3.0
+~~~~~
+
 Changes
 -------
 
+* Fixed our setup.py to use setup_requires to ensure the import dependencies
+  for testtools are present before setup.py runs (as setup.py imports testtools
+  to read out the version number). (Robert Collins)
+
+* Support setUpClass skipping with self.skipException. Previously this worked
+  with unittest from 2.7 and above but was not supported by testtools - it was
+  a happy accident. Since we now hard depend on unittest2, we need to invert
+  our exception lookup priorities to support it. Regular skips done through
+  raise self.skipException will continue to work, since they were always caught
+  in our code - its because the suite type being used to implement setUpClass
+  has changed that an issue occured.
+  (Robert Collins, #1393068)
+
+1.2.1
+~~~~~
+
+Changes
+-------
+
+* Correctly express our unittest2 dependency: we don't work with old releases.
+  (Robert Collins)
+
+1.2.0
+~~~~~
+
+Changes
+-------
+
+* Depends on unittest2 for discovery functionality and the ``TestProgram`` base
+  class. This brings in many fixes made to discovery where previously we were
+  only using the discovery package or the version in the release of Python
+  that the test execution was occuring on. (Robert Collins, #1271133)
+
 * Fixed unit tests which were failing under pypy due to a change in the way
   pypy formats tracebacks. (Thomi Richards)
 
+* Fixed the testtools test suite to run correctly when run via ``unit2``
+  or ``testtools.run discover``.
+
 * Make `testtools.content.text_content` error if anything other than text
   is given as content. (Thomi Richards)
 
@@ -235,7 +295,7 @@ experimental and we might need to break it if it turns out to be unsuitable.
 
 Improvements
 ------------
-* ``assertRaises`` works properly for exception classes that have custom
+* ``assertRaises`` works properly for exception classes that have custom 
   metaclasses
 
 * ``ConcurrentTestSuite`` was silently eating exceptions that propagate from
@@ -616,7 +676,7 @@ Improvements
 * API documentation corrections. (Raphaël Badin)
 
 * ``ConcurrentTestSuite`` now takes an optional ``wrap_result`` parameter
-  that can be used to wrap the ``ThreadsafeForwardingResult``s created by
+  that can be used to wrap the ``ThreadsafeForwardingResults`` created by
   the suite.  (Jonathan Lange)
 
 * ``Tagger`` added.  It's a new ``TestResult`` that tags all tests sent to
diff --git a/lib/testtools/README b/lib/testtools/README
deleted file mode 100644
index dbc685b..0000000
--- a/lib/testtools/README
+++ /dev/null
@@ -1,89 +0,0 @@
-=========
-testtools
-=========
-
-testtools is a set of extensions to the Python standard library's unit testing
-framework.
-
-These extensions have been derived from years of experience with unit testing
-in Python and come from many different sources.
-
-
-Documentation
--------------
-
-If you would like to learn more about testtools, consult our documentation in
-the 'doc/' directory.  You might like to start at 'doc/overview.rst' or
-'doc/for-test-authors.rst'.
-
-
-Licensing
----------
-
-This project is distributed under the MIT license and copyright is owned by
-Jonathan M. Lange and the testtools authors. See LICENSE for details.
-
-Some code in 'testtools/run.py' is taken from Python's unittest module, and is
-copyright Steve Purcell and the Python Software Foundation, it is distributed
-under the same license as Python, see LICENSE for details.
-
-
-Required Dependencies
----------------------
-
- * Python 2.6+ or 3.0+
-
-If you would like to use testtools for earlier Python's, please use testtools
-0.9.15.
-
-
-Optional Dependencies
----------------------
-
-If you would like to use our undocumented, unsupported Twisted support, then
-you will need Twisted.
-
-If you want to use ``fixtures`` then you can either install fixtures (e.g. from
-https://launchpad.net/python-fixtures or http://pypi.python.org/pypi/fixtures)
-or alternatively just make sure your fixture objects obey the same protocol.
-
-
-Bug reports and patches
------------------------
-
-Please report bugs using Launchpad at <https://bugs.launchpad.net/testtools>.
-Patches can also be submitted via Launchpad, or mailed to the author.  You can
-mail the author directly at jml at mumak.net.
-
-There's no mailing list for this project yet, however the testing-in-python
-mailing list may be a useful resource:
-
- * Address: testing-in-python at lists.idyll.org
- * Subscription link: http://lists.idyll.org/listinfo/testing-in-python
-
-
-History
--------
-
-testtools used to be called 'pyunit3k'.  The name was changed to avoid
-conflating the library with the Python 3.0 release (commonly referred to as
-'py3k').
-
-
-Thanks
-------
-
- * Canonical Ltd
- * Bazaar
- * Twisted Matrix Labs
- * Robert Collins
- * Andrew Bennetts
- * Benjamin Peterson
- * Jamu Kakar
- * James Westby
- * Martin [gz]
- * Michael Hudson-Doyle
- * Aaron Bentley
- * Christian Kampka
- * Gavin Panella
- * Martin Pool
diff --git a/lib/testtools/doc/for-framework-folk.rst b/lib/testtools/doc/for-framework-folk.rst
index ecc11f3..5c83ab1 100644
--- a/lib/testtools/doc/for-framework-folk.rst
+++ b/lib/testtools/doc/for-framework-folk.rst
@@ -14,13 +14,17 @@ unit-tested project, are trying to get one testing framework to play nicely
 with another or are hacking away at getting your test suite to run in parallel
 over a heterogenous cluster of machines, this guide is for you.
 
-This manual is a summary.  You can get details by consulting the `testtools
-API docs`_.
+This manual is a summary. You can get details by consulting the
+:doc:`testtools API docs </api>`.
 
 
 Extensions to TestCase
 ======================
 
+In addition to the ``TestCase`` specific methods, we have extensions for
+``TestSuite`` that also apply to ``TestCase`` (because ``TestCase`` and
+``TestSuite`` follow the Composite pattern).
+
 Custom exception handling
 -------------------------
 
@@ -46,9 +50,9 @@ provide a custom ``RunTest`` to a ``TestCase``.  The ``RunTest`` object can
 change everything about how the test executes.
 
 To work with ``testtools.TestCase``, a ``RunTest`` must have a factory that
-takes a test and an optional list of exception handlers.  Instances returned
-by the factory must have a ``run()`` method that takes an optional ``TestResult``
-object.
+takes a test and an optional list of exception handlers and an optional
+last_resort handler.  Instances returned by the factory must have a ``run()``
+method that takes an optional ``TestResult`` object.
 
 The default is ``testtools.runtest.RunTest``, which calls ``setUp``, the test
 method, ``tearDown`` and clean ups (see :ref:`addCleanup`) in the normal, vanilla
@@ -78,6 +82,15 @@ Test renaming
 instance to one with a new name.  This is helpful for implementing test
 parameterization.
 
+.. _force_failure:
+
+Delayed Test Failure
+--------------------
+
+Setting the ``testtools.TestCase.force_failure`` instance variable to True will
+cause ``testtools.RunTest`` to fail the test case after the test has finished.
+This is useful when you want to cause a test to fail, but don't want to
+prevent the remainder of the test code from being executed.
 
 Test placeholders
 =================
@@ -104,9 +117,174 @@ e.g.::
   I record an event                                                   [OK]
 
 
+Test instance decorators
+========================
+
+DecorateTestCaseResult
+----------------------
+
+This object calls out to your code when ``run`` / ``__call__`` are called and
+allows the result object that will be used to run the test to be altered. This
+is very useful when working with a test runner that doesn't know your test case
+requirements. For instance, it can be used to inject a ``unittest2`` compatible
+adapter when someone attempts to run your test suite with a ``TestResult`` that
+does not support ``addSkip`` or other ``unittest2`` methods. Similarly it can
+aid the migration to ``StreamResult``.
+
+e.g.::
+
+ >>> suite = TestSuite()
+ >>> suite = DecorateTestCaseResult(suite, ExtendedToOriginalDecorator)
+
 Extensions to TestResult
 ========================
 
+StreamResult
+------------
+
+``StreamResult`` is a new API for dealing with test case progress that supports
+concurrent and distributed testing without the various issues that
+``TestResult`` has such as buffering in multiplexers.
+
+The design has several key principles:
+
+* Nothing that requires up-front knowledge of all tests.
+
+* Deal with tests running in concurrent environments, potentially distributed
+  across multiple processes (or even machines). This implies allowing multiple
+  tests to be active at once, supplying time explicitly, being able to
+  differentiate between tests running in different contexts and removing any
+  assumption that tests are necessarily in the same process.
+
+* Make the API as simple as possible - each aspect should do one thing well.
+
+The ``TestResult`` API this is intended to replace has three different clients.
+
+* Each executing ``TestCase`` notifies the ``TestResult`` about activity.
+
+* The testrunner running tests uses the API to find out whether the test run
+  had errors, how many tests ran and so on.
+
+* Finally, each ``TestCase`` queries the ``TestResult`` to see whether the test
+  run should be aborted.
+
+With ``StreamResult`` we need to be able to provide a ``TestResult`` compatible
+adapter (``StreamToExtendedDecorator``) to allow incremental migration.
+However, we don't need to conflate things long term. So - we define three
+separate APIs, and merely mix them together to provide the
+``StreamToExtendedDecorator``. ``StreamResult`` is the first of these APIs -
+meeting the needs of ``TestCase`` clients. It handles events generated by
+running tests. See the API documentation for ``testtools.StreamResult`` for
+details.
+
+StreamSummary
+-------------
+
+Secondly we define the ``StreamSummary`` API which takes responsibility for
+collating errors, detecting incomplete tests and counting tests. This provides
+a compatible API with those aspects of ``TestResult``. Again, see the API
+documentation for ``testtools.StreamSummary``.
+
+TestControl
+-----------
+
+Lastly we define the ``TestControl`` API which is used to provide the
+``shouldStop`` and ``stop`` elements from ``TestResult``. Again, see the API
+documentation for ``testtools.TestControl``. ``TestControl`` can be paired with
+a ``StreamFailFast`` to trigger aborting a test run when a failure is observed.
+Aborting multiple workers in a distributed environment requires hooking
+whatever signalling mechanism the distributed environment has up to a
+``TestControl`` in each worker process.
+
+StreamTagger
+------------
+
+A ``StreamResult`` filter that adds or removes tags from events::
+
+    >>> from testtools import StreamTagger
+    >>> sink = StreamResult()
+    >>> result = StreamTagger([sink], set(['add']), set(['discard']))
+    >>> result.startTestRun()
+    >>> # Run tests against result here.
+    >>> result.stopTestRun()
+
+StreamToDict
+------------
+
+A simplified API for dealing with ``StreamResult`` streams. Each test is
+buffered until it completes and then reported as a trivial dict. This makes
+writing analysers very easy - you can ignore all the plumbing and just work
+with the result. e.g.::
+
+    >>> from testtools import StreamToDict
+    >>> def handle_test(test_dict):
+    ...     print(test_dict['id'])
+    >>> result = StreamToDict(handle_test)
+    >>> result.startTestRun()
+    >>> # Run tests against result here.
+    >>> # At stopTestRun() any incomplete buffered tests are announced.
+    >>> result.stopTestRun()
+
+ExtendedToStreamDecorator
+-------------------------
+
+This is a hybrid object that combines both the ``Extended`` and ``Stream``
+``TestResult`` APIs into one class, but only emits ``StreamResult`` events.
+This is useful when a ``StreamResult`` stream is desired, but you cannot
+be sure that the tests which will run have been updated to the ``StreamResult``
+API.
+
+StreamToExtendedDecorator
+-------------------------
+
+This is a simple converter that emits the ``ExtendedTestResult`` API in
+response to events from the ``StreamResult`` API. Useful when outputting
+``StreamResult`` events from a ``TestCase`` but the supplied ``TestResult``
+does not support the ``status`` and ``file`` methods.
+
+StreamToQueue
+-------------
+
+This is a ``StreamResult`` decorator for reporting tests from multiple threads
+at once. Each method submits an event to a supplied Queue object as a simple
+dict. See ``ConcurrentStreamTestSuite`` for a convenient way to use this.
+
+TimestampingStreamResult
+------------------------
+
+This is a ``StreamResult`` decorator for adding timestamps to events that lack
+them. This allows writing the simplest possible generators of events and
+passing the events via this decorator to get timestamped data. As long as
+no buffering/queueing or blocking happen before the timestamper sees the event
+the timestamp will be as accurate as if the original event had it.
+
+StreamResultRouter
+------------------
+
+This is a ``StreamResult`` which forwards events to an arbitrary set of target
+``StreamResult`` objects. Events that have no forwarding rule are passed onto
+an fallback ``StreamResult`` for processing. The mapping can be changed at
+runtime, allowing great flexibility and responsiveness to changes. Because
+The mapping can change dynamically and there could be the same recipient for
+two different maps, ``startTestRun`` and ``stopTestRun`` handling is fine
+grained and up to the user.
+
+If no fallback has been supplied, an unroutable event will raise an exception.
+
+For instance::
+
+    >>> router = StreamResultRouter()
+    >>> sink = doubles.StreamResult()
+    >>> router.add_rule(sink, 'route_code_prefix', route_prefix='0',
+    ...     consume_route=True)
+    >>> router.status(test_id='foo', route_code='0/1', test_status='uxsuccess')
+
+Would remove the ``0/`` from the route_code and forward the event like so::
+
+    >>> sink.status('test_id=foo', route_code='1', test_status='uxsuccess')
+
+See ``pydoc testtools.StreamResultRouter`` for details.
+
 TestResult.addSkip
 ------------------
 
@@ -215,12 +393,29 @@ ConcurrentTestSuite uses the helper to get a number of separate runnable
 objects with a run(result), runs them all in threads using the
 ThreadsafeForwardingResult to coalesce their activity.
 
+ConcurrentStreamTestSuite
+-------------------------
+
+A variant of ConcurrentTestSuite that uses the new StreamResult API instead of
+the TestResult API. ConcurrentStreamTestSuite coordinates running some number
+of test/suites concurrently, with one StreamToQueue per test/suite.
+
+Each test/suite gets given its own ExtendedToStreamDecorator +
+TimestampingStreamResult wrapped StreamToQueue instance, forwarding onto the
+StreamResult that ConcurrentStreamTestSuite.run was called with.
+
+ConcurrentStreamTestSuite is a thin shim and it is easy to implement your own
+specialised form if that is needed.
+
 FixtureSuite
 ------------
 
 A test suite that sets up a fixture_ before running any tests, and then tears
 it down after all of the tests are run. The fixture is *not* made available to
-any of the tests.
+any of the tests due to there being no standard channel for suites to pass
+information to the tests they contain (and we don't have enough data on what
+such a channel would need to achieve to design a good one yet - or even decide
+if it is a good idea).
 
 sorted_tests
 ------------
@@ -229,10 +424,30 @@ Given the composite structure of TestSuite / TestCase, sorting tests is
 problematic - you can't tell what functionality is embedded into custom Suite
 implementations. In order to deliver consistent test orders when using test
 discovery (see http://bugs.python.org/issue16709), testtools flattens and
-sorts tests that have the standard TestSuite, defines a new method sort_tests,
-which can be used by non-standard TestSuites to know when they should sort
-their tests.
+sorts tests that have the standard TestSuite, and defines a new method
+sort_tests, which can be used by non-standard TestSuites to know when they
+should sort their tests. An example implementation can be seen at
+``FixtureSuite.sorted_tests``.
+
+If there are duplicate test ids in a suite, ValueError will be raised.
+
+filter_by_ids
+-------------
+
+Similarly to ``sorted_tests`` running a subset of tests is problematic - the
+standard run interface provides no way to limit what runs. Rather than
+confounding the two problems (selection and execution) we defined a method
+that filters the tests in a suite (or a case) by their unique test id.
+If you a writing custom wrapping suites, consider implementing filter_by_ids
+to support this (though most wrappers that subclass ``unittest.TestSuite`` will
+work just fine [see ``testtools.testsuite.filter_by_ids`` for details.]
+
+Extensions to TestRunner
+========================
+
+To facilitate custom listing of tests, ``testtools.run.TestProgram`` attempts
+to call ``list`` on the ``TestRunner``, falling back to a generic
+implementation if it is not present.
 
-.. _`testtools API docs`: http://mumak.net/testtools/apidocs/
 .. _unittest: http://docs.python.org/library/unittest.html
 .. _fixture: http://pypi.python.org/pypi/fixtures
diff --git a/lib/testtools/doc/for-test-authors.rst b/lib/testtools/doc/for-test-authors.rst
index c9e6c6a..5deb7ce 100644
--- a/lib/testtools/doc/for-test-authors.rst
+++ b/lib/testtools/doc/for-test-authors.rst
@@ -11,7 +11,7 @@ automated testing already.
 If you are a test author of an unusually large or unusually unusual test
 suite, you might be interested in :doc:`for-framework-folk`.
 
-You might also be interested in the `testtools API docs`_.
+You might also be interested in the :doc:`testtools API docs </api>`.
 
 
 Introduction
@@ -163,7 +163,8 @@ The first argument to ``ExpectedException`` is the type of exception you
 expect to see raised.  The second argument is optional, and can be either a
 regular expression or a matcher. If it is a regular expression, the ``str()``
 of the raised exception must match the regular expression. If it is a matcher,
-then the raised exception object must match it.
+then the raised exception object must match it. The optional third argument
+``msg`` will cause the raised error to be annotated with that message.
 
 
 assertIn, assertNotIn
@@ -287,6 +288,60 @@ Which is roughly equivalent to::
       self.assertNotEqual(result, 50)
 
 
+``assert_that`` Function
+------------------------
+
+In addition to ``self.assertThat``, testtools also provides the ``assert_that``
+function in ``testtools.assertions`` This behaves like the method version does::
+
+    class TestSquare(TestCase):
+
+        def test_square():
+            result = square(7)
+            assert_that(result, Equals(49))
+
+        def test_square_silly():
+            result = square(7)
+            assert_that(result, Not(Equals(50)))
+
+
+Delayed Assertions
+~~~~~~~~~~~~~~~~~~
+
+A failure in the ``self.assertThat`` method will immediately fail the test: No
+more test code will be run after the assertion failure.
+
+The ``expectThat`` method behaves the same as ``assertThat`` with one
+exception: when failing the test it does so at the end of the test code rather
+than when the mismatch is detected. For example::
+
+  import subprocess
+
+  from testtools import TestCase
+  from testtools.matchers import Equals
+
+
+  class SomeProcessTests(TestCase):
+
+      def test_process_output(self):
+          process = subprocess.Popen(
+            ["my-app", "/some/path"],
+            stdout=subprocess.PIPE,
+            stderr=subprocess.PIPE
+          )
+
+          stdout, stderrr = process.communicate()
+
+          self.expectThat(process.returncode, Equals(0))
+          self.expectThat(stdout, Equals("Expected Output"))
+          self.expectThat(stderr, Equals(""))
+
+In this example, should the ``expectThat`` call fail, the failure will be
+recorded in the test result, but the test will continue as normal. If all
+three assertions fail, the test result will have three failures recorded, and
+the failure details for each failed assertion will be attached to the test
+result.
+
 Stock matchers
 --------------
 
@@ -407,7 +462,7 @@ example::
       except RuntimeError:
           exc_info = sys.exc_info()
       self.assertThat(exc_info, MatchesException(RuntimeError))
-      self.assertThat(exc_info, MatchesException(RuntimeError('bar'))
+      self.assertThat(exc_info, MatchesException(RuntimeError('bar')))
 
 Most of the time, you will want to uses `The raises helper`_ instead.
 
@@ -445,6 +500,18 @@ be able to do, if you think about it::
       self.assertThat('foo', MatchesRegex('fo+'))
 
 
+HasLength
+~~~~~~~~~
+
+Check the length of a collection.  The following assertion will fail::
+
+  self.assertThat([1, 2, 3], HasLength(2))
+
+But this one won't::
+
+  self.assertThat([1, 2, 3], HasLength(3))
+
+
 File- and path-related matchers
 -------------------------------
 
@@ -585,7 +652,7 @@ Used to add custom notes to a matcher.  For example::
   def test_annotate_example(self):
       result = 43
       self.assertThat(
-          result, Annotate("Not the answer to the Question!", Equals(42))
+          result, Annotate("Not the answer to the Question!", Equals(42)))
 
 Since the annotation is only ever displayed when there is a mismatch
 (e.g. when ``result`` does not equal 42), it's a good idea to phrase the note
@@ -613,7 +680,7 @@ matching. This can be used to aid in creating trivial matchers as functions, for
 example::
 
   def test_after_preprocessing_example(self):
-      def HasFileContent(content):
+      def PathHasFileContent(content):
           def _read(path):
               return open(path).read()
           return AfterPreprocessing(_read, Equals(content))
@@ -780,6 +847,35 @@ Which will produce the error message::
   MismatchError: 42 is not prime.
 
 
+MatchesPredicateWithParams
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Sometimes you can't use a trivial predicate and instead need to pass in some
+parameters each time. In that case, MatchesPredicateWithParams is your go-to
+tool for creating ad hoc matchers. MatchesPredicateWithParams takes a predicate
+function and message and returns a factory to produce matchers from that. The
+predicate needs to return a boolean (or any truthy object), and accept the
+object to match + whatever was passed into the factory.
+
+For example, you might have an ``divisible`` function and want to make a
+matcher based on it::
+
+  def test_divisible_numbers(self):
+      IsDivisibleBy = MatchesPredicateWithParams(
+          divisible, '{0} is not divisible by {1}')
+      self.assertThat(7, IsDivisibleBy(1))
+      self.assertThat(7, IsDivisibleBy(7))
+      self.assertThat(7, IsDivisibleBy(2))
+      # This will fail.
+
+Which will produce the error message::
+
+  Traceback (most recent call last):
+    File "...", line ..., in test_divisible
+      self.assertThat(7, IsDivisibleBy(2))
+  MismatchError: 7 is not divisible by 2.
+
+
 Raises
 ~~~~~~
 
@@ -838,9 +934,9 @@ returns a non-None value.  For example::
 
   def test_is_divisible_by_example(self):
       # This succeeds, since IsDivisibleBy(5).match(10) returns None.
-      self.assertThat(10, IsDivisbleBy(5))
+      self.assertThat(10, IsDivisibleBy(5))
       # This fails, since IsDivisibleBy(7).match(10) returns a mismatch.
-      self.assertThat(10, IsDivisbleBy(7))
+      self.assertThat(10, IsDivisibleBy(7))
 
 The mismatch is responsible for what sort of error message the failing test
 generates.  Here's an example mismatch::
@@ -1201,6 +1297,13 @@ Here are some tips for converting your Trial tests into testtools tests.
   ``AsynchronousDeferredRunTest`` does not.  If you rely on this behavior, use
   ``AsynchronousDeferredRunTestForBrokenTwisted``.
 
+force_failure
+-------------
+
+Setting the ``testtools.TestCase.force_failure`` instance variable to ``True``
+will cause the test to be marked as a failure, but won't stop the test code
+from running (see :ref:`force_failure`).
+
 
 Test helpers
 ============
@@ -1280,6 +1383,29 @@ details of certain variables don't actually matter.
 See pages 419-423 of `xUnit Test Patterns`_ by Gerard Meszaros for a detailed
 discussion of creation methods.
 
+Test attributes
+---------------
+
+Inspired by the ``nosetests`` ``attr`` plugin, testtools provides support for
+marking up test methods with attributes, which are then exposed in the test
+id and can be used when filtering tests by id. (e.g. via ``--load-list``)::
+
+  from testtools.testcase import attr, WithAttributes
+
+  class AnnotatedTests(WithAttributes, TestCase):
+
+      @attr('simple')
+      def test_one(self):
+          pass
+
+      @attr('more', 'than', 'one')
+      def test_two(self):
+          pass
+
+      @attr('or')
+      @attr('stacked')
+      def test_three(self):
+          pass
 
 General helpers
 ===============
@@ -1288,7 +1414,7 @@ Conditional imports
 -------------------
 
 Lots of the time we would like to conditionally import modules.  testtools
-needs to do this itself, and graciously extends the ability to its users.
+uses the small library extras to do this. This used to be part of testtools.
 
 Instead of::
 
@@ -1317,9 +1443,9 @@ You can do::
 Safe attribute testing
 ----------------------
 
-``hasattr`` is broken_ on many versions of Python.  testtools provides
-``safe_hasattr``, which can be used to safely test whether an object has a
-particular attribute.
+``hasattr`` is broken_ on many versions of Python. The helper ``safe_hasattr``
+can be used to safely test whether an object has a particular attribute. Like
+``try_import`` this used to be in testtools but is now in extras.
 
 
 Nullary callables
@@ -1354,7 +1480,6 @@ Here, ``repr(nullary)`` will be the same as ``repr(f)``.
 .. _doctest: http://docs.python.org/library/doctest.html
 .. _Deferred: http://twistedmatrix.com/documents/current/core/howto/defer.html
 .. _discover: http://pypi.python.org/pypi/discover
-.. _`testtools API docs`: http://mumak.net/testtools/apidocs/
 .. _Distutils: http://docs.python.org/library/distutils.html
 .. _`setup configuration`: http://docs.python.org/distutils/configfile.html
 .. _broken: http://chipaca.com/post/3210673069/hasattr-17-less-harmful
diff --git a/lib/testtools/doc/hacking.rst b/lib/testtools/doc/hacking.rst
index 663eeac..ccfc155 100644
--- a/lib/testtools/doc/hacking.rst
+++ b/lib/testtools/doc/hacking.rst
@@ -2,6 +2,13 @@
 Contributing to testtools
 =========================
 
+Bugs and patches
+----------------
+
+`File bugs <https://bugs.launchpad.net/testtools/+filebug>` on Launchpad, and
+`send patches <https://github.com/testing-cabal/testtools/>` on Github.
+
+
 Coding style
 ------------
 
@@ -49,6 +56,21 @@ is often useful to see all levels of the stack. To do this, add
 ``run_tests_with = FullStackRunTest`` to the top of a test's class definition.
 
 
+Discussion
+----------
+
+When submitting a patch, it will help the review process a lot if there's a
+clear explanation of what the change does and why you think the change is a
+good idea.  For crasher bugs, this is generally a no-brainer, but for UI bugs
+& API tweaks, the reason something is an improvement might not be obvious, so
+it's worth spelling out.
+
+If you are thinking of implementing a new feature, you might want to have that
+discussion on the [mailing list](testtools-dev at lists.launchpad.net) before the
+patch goes up for review.  This is not at all mandatory, but getting feedback
+early can help avoid dead ends.
+
+
 Documentation
 -------------
 
@@ -63,7 +85,7 @@ Source layout
 -------------
 
 The top-level directory contains the ``testtools/`` package directory, and
-miscellaneous files like ``README`` and ``setup.py``.
+miscellaneous files like ``README.rst`` and ``setup.py``.
 
 The ``testtools/`` directory is the Python package itself.  It is separated
 into submodules for internal clarity, but all public APIs should be “promoted”
@@ -78,13 +100,13 @@ Tests belong in ``testtools/tests/``.
 Committing to trunk
 -------------------
 
-Testtools is maintained using bzr, with its trunk at lp:testtools. This gives
-every contributor the ability to commit their work to their own branches.
-However permission must be granted to allow contributors to commit to the trunk
-branch.
+Testtools is maintained using git, with its master repo at
+https://github.com/testing-cabal/testtools. This gives every contributor the
+ability to commit their work to their own branches. However permission must be
+granted to allow contributors to commit to the trunk branch.
 
-Commit access to trunk is obtained by joining the testtools-committers
-Launchpad team. Membership in this team is contingent on obeying the testtools
+Commit access to trunk is obtained by joining the `testing-cabal`_, either as an
+Owner or a Committer. Commit access is contingent on obeying the testtools
 contribution policy, see `Copyright Assignment`_ above.
 
 
@@ -92,22 +114,34 @@ Code Review
 -----------
 
 All code must be reviewed before landing on trunk. The process is to create a
-branch in launchpad, and submit it for merging to lp:testtools. It will then
-be reviewed before it can be merged to trunk. It will be reviewed by someone:
+branch on Github, and make a pull request into trunk. It will then be reviewed
+before it can be merged to trunk. It will be reviewed by someone:
 
 * not the author
-* a committer (member of the `~testtools-committers`_ team)
+* a committer
 
-As a special exception, while the testtools committers team is small and prone
-to blocking, a merge request from a committer that has not been reviewed after
-24 hours may be merged by that committer. When the team is larger this policy
-will be revisited.
+As a special exception, since there are few testtools committers and thus
+reviews are prone to blocking, a pull request from a committer that has not been
+reviewed after 24 hours may be merged by that committer. When the team is larger
+this policy will be revisited.
 
 Code reviewers should look for the quality of what is being submitted,
 including conformance with this HACKING file.
 
 Changes which all users should be made aware of should be documented in NEWS.
 
+We are now in full backwards compatibility mode - no more releases < 1.0.0, and 
+breaking compatibility will require consensus on the testtools-dev mailing list.
+Exactly what constitutes a backwards incompatible change is vague, but coarsely:
+
+* adding required arguments or required calls to something that used to work
+* removing keyword or position arguments, removing methods, functions or modules
+* changing behaviour someone may have reasonably depended on
+
+Some things are not compatibility issues:
+
+* changes to _ prefixed methods, functions, modules, packages.
+
 
 NEWS management
 ---------------
@@ -119,35 +153,42 @@ branches, the bullet points are kept alphabetically sorted. The release NEXT is
 permanently present at the top of the list.
 
 
-Release tasks
--------------
+Releasing
+---------
+
+Prerequisites
++++++++++++++
+
+Membership in the testing-cabal org on github as committer.
+
+Membership in the pypi testtools project as maintainer.
+
+Membership in the https://launchpad.net/~testtools-committers.
+
+Tasks
++++++
 
 #. Choose a version number, say X.Y.Z
-#. Branch from trunk to testtools-X.Y.Z
-#. In testtools-X.Y.Z, ensure __init__ has version ``(X, Y, Z, 'final', 0)``
-#. Replace NEXT in NEWS with the version number X.Y.Z, adjusting the reST.
+#. In trunk, ensure __init__ has version ``(X, Y, Z, 'final', 0)``
+#. Under NEXT in NEWS add a heading with the version number X.Y.Z.
 #. Possibly write a blurb into NEWS.
-#. Replace any additional references to NEXT with the version being
-   released. (There should be none other than the ones in these release tasks
-   which should not be replaced).
 #. Commit the changes.
-#. Tag the release, bzr tag testtools-X.Y.Z
+#. Tag the release, ``git tag -s testtools-X.Y.Z``
 #. Run 'make release', this:
    #. Creates a source distribution and uploads to PyPI
    #. Ensures all Fix Committed bugs are in the release milestone
    #. Makes a release on Launchpad and uploads the tarball
    #. Marks all the Fix Committed bugs as Fix Released
    #. Creates a new milestone
-#. Merge the release branch testtools-X.Y.Z into trunk. Before the commit,
-   add a NEXT heading to the top of NEWS and bump the version in __init__.py
+#. Change __version__ in __init__.py to the probable next version.
    e.g. to ``(X, Y, Z+1, 'dev', 0)``.
-#. Push trunk to Launchpad
+#. Commit 'Opening X.Y.Z+1 for development.'
 #. If a new series has been created (e.g. 0.10.0), make the series on Launchpad.
+#. Push trunk to Github, ``git push --tags origin master``
 
 .. _PEP 8: http://www.python.org/dev/peps/pep-0008/
 .. _unittest: http://docs.python.org/library/unittest.html
-.. _~testtools-committers: https://launchpad.net/~testtools-committers
 .. _MIT license: http://www.opensource.org/licenses/mit-license.php
 .. _Sphinx: http://sphinx.pocoo.org/
 .. _restructuredtext: http://docutils.sourceforge.net/rst.html
-
+.. _testing-cabal: https://github.com/organizations/testing-cabal/
diff --git a/lib/testtools/doc/index.rst b/lib/testtools/doc/index.rst
index bac47e4..a6c05a9 100644
--- a/lib/testtools/doc/index.rst
+++ b/lib/testtools/doc/index.rst
@@ -25,7 +25,7 @@ Contents:
    for-framework-folk
    hacking
    Changes to testtools <news>
-   API reference documentation <http://mumak.net/testtools/apidocs/>
+   API reference documentation <api>
 
 Indices and tables
 ==================
diff --git a/lib/testtools/doc/overview.rst b/lib/testtools/doc/overview.rst
index cb72893..a01dc3d 100644
--- a/lib/testtools/doc/overview.rst
+++ b/lib/testtools/doc/overview.rst
@@ -5,10 +5,7 @@ testtools: tasteful testing for Python
 testtools is a set of extensions to the Python standard library's unit testing
 framework. These extensions have been derived from many years of experience
 with unit testing in Python and come from many different sources. testtools
-supports Python versions all the way back to Python 2.4. The next release of
-testtools will change that to support versions that are maintained by the
-Python community instead, to allow the use of modern language features within
-testtools.
+supports Python versions all the way back to Python 2.6.
 
 What better way to start than with a contrived code snippet?::
 
@@ -29,7 +26,7 @@ What better way to start than with a contrived code snippet?::
       def attach_log_file(self):
           self.addDetail(
               'log-file',
-              Content(UTF8_TEXT
+              Content(UTF8_TEXT,
                       lambda: open(self.server.logfile, 'r').readlines()))
 
       def test_server_is_cool(self):
@@ -96,7 +93,9 @@ Cross-Python compatibility
 --------------------------
 
 testtools gives you the very latest in unit testing technology in a way that
-will work with Python 2.6, 2.7 and 3.1.
+will work with Python 2.6, 2.7, 3.1 and 3.2.
 
 If you wish to use testtools with Python 2.4 or 2.5, then please use testtools
-0.9.15.
+0.9.15. Up to then we supported Python 2.4 and 2.5, but we found the
+constraints involved in not using the newer language features onerous as we
+added more support for versions post Python 3.
diff --git a/lib/testtools/scripts/_lp_release.py b/lib/testtools/scripts/_lp_release.py
index 20afd01..ac27e47 100644
--- a/lib/testtools/scripts/_lp_release.py
+++ b/lib/testtools/scripts/_lp_release.py
@@ -108,7 +108,8 @@ def get_release_notes_and_changelog(news_path):
         for line in news:
             line = line.strip()
             if state is None:
-                if is_heading_marker(line, '~'):
+                if (is_heading_marker(line, '~') and
+                    not last_line.startswith('NEXT')):
                     milestone_name = last_line
                     state = 'release-notes'
                 else:
@@ -222,7 +223,8 @@ def release_project(launchpad, project_name, next_milestone_name):
 
 
 def main(args):
-    launchpad = Launchpad.login_with(APP_NAME, SERVICE_ROOT, CACHE_DIR)
+    launchpad = Launchpad.login_with(
+        APP_NAME, SERVICE_ROOT, CACHE_DIR, credentials_file='.lp_creds')
     return release_project(launchpad, PROJECT_NAME, NEXT_MILESTONE_NAME)
 
 
diff --git a/lib/testtools/setup.cfg b/lib/testtools/setup.cfg
index 9f95add..3e210fa 100644
--- a/lib/testtools/setup.cfg
+++ b/lib/testtools/setup.cfg
@@ -2,3 +2,6 @@
 test_module = testtools.tests
 buffer=1
 catch=1
+
+[bdist_wheel]
+universal = 1
diff --git a/lib/testtools/setup.py b/lib/testtools/setup.py
index 7ecd6d2..95c3b67 100755
--- a/lib/testtools/setup.py
+++ b/lib/testtools/setup.py
@@ -1,22 +1,24 @@
 #!/usr/bin/env python
 """Distutils installer for testtools."""
 
-from distutils.core import setup
+from setuptools import setup
+from distutils.command.build_py import build_py
 import email
 import os
+import sys
 
 import testtools
+cmd_class = {}
+if getattr(testtools, 'TestCommand', None) is not None:
+    cmd_class['test'] = testtools.TestCommand
 
 
-def get_revno():
-    import bzrlib.errors
-    import bzrlib.workingtree
-    try:
-        t = bzrlib.workingtree.WorkingTree.open_containing(__file__)[0]
-    except (bzrlib.errors.NotBranchError, bzrlib.errors.NoWorkingTree):
-        return None
-    else:
-        return t.branch.revno()
+class testtools_build_py(build_py):
+    def build_module(self, module, module_file, package):
+        if sys.version_info >= (3,) and module == '_compat2x':
+            return
+        return build_py.build_module(self, module, module_file, package)
+cmd_class['build_py'] = testtools_build_py
 
 
 def get_version_from_pkg_info():
@@ -43,18 +45,10 @@ def get_version():
     pkg_info_version = get_version_from_pkg_info()
     if pkg_info_version:
         return pkg_info_version
-    revno = get_revno()
-    if revno is None:
-        # Apparently if we just say "snapshot" then distribute won't accept it
-        # as satisfying versioned dependencies. This is a problem for the
-        # daily build version.
-        return "snapshot-%s" % (version,)
-    if phase == 'alpha':
-        # No idea what the next version will be
-        return 'next-r%s' % revno
-    else:
-        # Preserve the version number but give it a revno prefix
-        return version + '-r%s' % revno
+    # Apparently if we just say "snapshot" then distribute won't accept it
+    # as satisfying versioned dependencies. This is a problem for the
+    # daily build version.
+    return "%s.0dev0" % (version,)
 
 
 def get_long_description():
@@ -62,11 +56,21 @@ def get_long_description():
         os.path.dirname(__file__), 'doc/overview.rst')
     return open(manual_path).read()
 
+# Since we import testtools in setup.py, our setup requirements are our install
+# requirements.
+deps = [
+    'extras',
+    # 'mimeparse' has not been uploaded by the maintainer with Python3 compat
+    # but someone kindly uploaded a fixed version as 'python-mimeparse'.
+    'python-mimeparse',
+    'unittest2>=0.8.0',
+    ]
+
 
 setup(name='testtools',
       author='Jonathan M. Lange',
       author_email='jml+testtools at mumak.net',
-      url='https://launchpad.net/testtools',
+      url='https://github.com/testing-cabal/testtools',
       description=('Extensions to the Python standard library unit testing '
                    'framework'),
       long_description=get_long_description(),
@@ -81,5 +85,8 @@ setup(name='testtools',
         'testtools.tests',
         'testtools.tests.matchers',
         ],
-      cmdclass={'test': testtools.TestCommand},
-      zip_safe=False)
+      cmdclass=cmd_class,
+      zip_safe=False,
+      install_requires=deps,
+      setup_requires=deps,
+      )
diff --git a/lib/testtools/testtools/__init__.py b/lib/testtools/testtools/__init__.py
index d722ce5..6e2df87 100644
--- a/lib/testtools/testtools/__init__.py
+++ b/lib/testtools/testtools/__init__.py
@@ -4,10 +4,14 @@
 
 __all__ = [
     'clone_test_with_new_id',
+    'CopyStreamResult',
     'ConcurrentTestSuite',
+    'ConcurrentStreamTestSuite',
+    'DecorateTestCaseResult',
     'ErrorHolder',
     'ExpectedException',
     'ExtendedToOriginalDecorator',
+    'ExtendedToStreamDecorator',
     'FixtureSuite',
     'iterate_tests',
     'MultipleExceptions',
@@ -25,54 +29,86 @@ __all__ = [
     'skip',
     'skipIf',
     'skipUnless',
+    'StreamFailFast',
+    'StreamResult',
+    'StreamResultRouter',
+    'StreamSummary',
+    'StreamTagger',
+    'StreamToDict',
+    'StreamToExtendedDecorator',
+    'StreamToQueue',
+    'TestControl',
     'ThreadsafeForwardingResult',
+    'TimestampingStreamResult',
     'try_import',
     'try_imports',
     ]
 
-from testtools.helpers import (
-    try_import,
-    try_imports,
-    )
-from testtools.matchers._impl import (
-    Matcher,
-    )
+# Compat - removal announced in 0.9.25.
+try:
+    from extras import (
+        try_import,
+        try_imports,
+        )
+except ImportError:
+    # Support reading __init__ for __version__ without extras, because pip does
+    # not support setup_requires.
+    pass
+else:
+
+    from testtools.matchers._impl import (
+        Matcher,
+        )
 # Shut up, pyflakes. We are importing for documentation, not for namespacing.
-Matcher
+    Matcher
 
-from testtools.runtest import (
-    MultipleExceptions,
-    RunTest,
-    )
-from testtools.testcase import (
-    ErrorHolder,
-    ExpectedException,
-    PlaceHolder,
-    TestCase,
-    clone_test_with_new_id,
-    run_test_with,
-    skip,
-    skipIf,
-    skipUnless,
-    )
-from testtools.testresult import (
-    ExtendedToOriginalDecorator,
-    MultiTestResult,
-    Tagger,
-    TestByTestResult,
-    TestResult,
-    TestResultDecorator,
-    TextTestResult,
-    ThreadsafeForwardingResult,
-    )
-from testtools.testsuite import (
-    ConcurrentTestSuite,
-    FixtureSuite,
-    iterate_tests,
-    )
-from testtools.distutilscmd import (
-    TestCommand,
-)
+    from testtools.runtest import (
+        MultipleExceptions,
+        RunTest,
+        )
+    from testtools.testcase import (
+        DecorateTestCaseResult,
+        ErrorHolder,
+        ExpectedException,
+        PlaceHolder,
+        TestCase,
+        clone_test_with_new_id,
+        run_test_with,
+        skip,
+        skipIf,
+        skipUnless,
+        )
+    from testtools.testresult import (
+        CopyStreamResult,
+        ExtendedToOriginalDecorator,
+        ExtendedToStreamDecorator,
+        MultiTestResult,
+        StreamFailFast,
+        StreamResult,
+        StreamResultRouter,
+        StreamSummary,
+        StreamTagger,
+        StreamToDict,
+        StreamToExtendedDecorator,
+        StreamToQueue,
+        Tagger,
+        TestByTestResult,
+        TestControl,
+        TestResult,
+        TestResultDecorator,
+        TextTestResult,
+        ThreadsafeForwardingResult,
+        TimestampingStreamResult,
+        )
+    from testtools.testsuite import (
+        ConcurrentTestSuite,
+        ConcurrentStreamTestSuite,
+        FixtureSuite,
+        iterate_tests,
+        )
+    from testtools.distutilscmd import (
+        TestCommand,
+        )
 
 # same format as sys.version_info: "A tuple containing the five components of
 # the version number: major, minor, micro, releaselevel, and serial. All
@@ -86,4 +122,4 @@ from testtools.distutilscmd import (
 # If the releaselevel is 'final', then the tarball will be major.minor.micro.
 # Otherwise it is major.minor.micro~$(revno).
 
-__version__ = (0, 9, 25, 'dev', 0)
+__version__ = (1, 5, 0, 'final', 0)
diff --git a/lib/testtools/testtools/compat.py b/lib/testtools/testtools/compat.py
index 375eca2..684c8b3 100644
--- a/lib/testtools/testtools/compat.py
+++ b/lib/testtools/testtools/compat.py
@@ -7,10 +7,8 @@ __all__ = [
     '_b',
     '_u',
     'advance_iterator',
-    'all',
     'BytesIO',
     'classtypes',
-    'isbaseexception',
     'istext',
     'str_is_unicode',
     'StringIO',
@@ -19,6 +17,7 @@ __all__ = [
     ]
 
 import codecs
+import io
 import linecache
 import locale
 import os
@@ -27,14 +26,14 @@ import sys
 import traceback
 import unicodedata
 
-from testtools.helpers import try_imports
+from extras import try_imports
 
 BytesIO = try_imports(['StringIO.StringIO', 'io.BytesIO'])
 StringIO = try_imports(['StringIO.StringIO', 'io.StringIO'])
 
 try:
     from testtools import _compat2x as _compat
-except SyntaxError:
+except (SyntaxError, ImportError):
     from testtools import _compat3x as _compat
 
 reraise = _compat.reraise
@@ -87,34 +86,6 @@ else:
 _u.__doc__ = __u_doc
 
 
-if sys.version_info > (2, 5):
-    all = all
-    _error_repr = BaseException.__repr__
-    def isbaseexception(exception):
-        """Return whether exception inherits from BaseException only"""
-        return (isinstance(exception, BaseException)
-            and not isinstance(exception, Exception))
-else:
-    def all(iterable):
-        """If contents of iterable all evaluate as boolean True"""
-        for obj in iterable:
-            if not obj:
-                return False
-        return True
-    def _error_repr(exception):
-        """Format an exception instance as Python 2.5 and later do"""
-        return exception.__class__.__name__ + repr(exception.args)
-    def isbaseexception(exception):
-        """Return whether exception would inherit from BaseException only
-
-        This approximates the hierarchy in Python 2.5 and later, compare the
-        difference between the diagrams at the bottom of the pages:
-        <http://docs.python.org/release/2.4.4/lib/module-exceptions.html>
-        <http://docs.python.org/release/2.5.4/lib/module-exceptions.html>
-        """
-        return isinstance(exception, (KeyboardInterrupt, SystemExit))
-
-
 # GZ 2011-08-24: Using isinstance checks like this encourages bad interfaces,
 #                there should be better ways to write code needing this.
 if not issubclass(getattr(builtins, "bytes", str), str):
@@ -169,7 +140,7 @@ def text_repr(text, multiline=None):
     prefix = repr(text[:0])[:-2]
     if multiline:
         # To escape multiline strings, split and process each line in turn,
-        # making sure that quotes are not escaped. 
+        # making sure that quotes are not escaped.
         if is_py3k:
             offset = len(prefix) + 1
             lines = []
@@ -215,14 +186,15 @@ def unicode_output_stream(stream):
     The wrapper only allows unicode to be written, not non-ascii bytestrings,
     which is a good thing to ensure sanity and sanitation.
     """
-    if sys.platform == "cli":
-        # Best to never encode before writing in IronPython
+    if (sys.platform == "cli" or
+        isinstance(stream, (io.TextIOWrapper, io.StringIO))):
+        # Best to never encode before writing in IronPython, or if it is
+        # already a TextIO [which in the io library has no encoding
+        # attribute).
         return stream
     try:
         writer = codecs.getwriter(stream.encoding or "")
     except (AttributeError, LookupError):
-        # GZ 2010-06-16: Python 3 StringIO ends up here, but probably needs
-        #                different handling as it doesn't want bytestrings
         return codecs.getwriter("ascii")(stream, "replace")
     if writer.__module__.rsplit(".", 1)[1].startswith("utf"):
         # The current stream has a unicode encoding so no error handler is needed
@@ -324,31 +296,33 @@ def _exception_to_text(evalue):
     return None
 
 
-# GZ 2010-05-23: This function is huge and horrible and I welcome suggestions
-#                on the best way to break it up
-_TB_HEADER = _u('Traceback (most recent call last):\n')
-def _format_exc_info(eclass, evalue, tb, limit=None):
-    """Format a stack trace and the exception information as unicode
+def _format_stack_list(stack_lines):
+    """Format 'stack_lines' and return a list of unicode strings.
 
-    Compatibility function for Python 2 which ensures each component of a
-    traceback is correctly decoded according to its origins.
-
-    Based on traceback.format_exception and related functions.
+    :param stack_lines: A list of filename, lineno, name, and line variables,
+        probably obtained by calling traceback.extract_tb or
+        traceback.extract_stack.
     """
     fs_enc = sys.getfilesystemencoding()
-    if tb:
-        list = [_TB_HEADER]
-        extracted_list = []
-        for filename, lineno, name, line in traceback.extract_tb(tb, limit):
+    extracted_list = []
+    for filename, lineno, name, line in stack_lines:
             extracted_list.append((
                 filename.decode(fs_enc, "replace"),
                 lineno,
                 name.decode("ascii", "replace"),
                 line and line.decode(
                     _get_source_encoding(filename), "replace")))
-        list.extend(traceback.format_list(extracted_list))
-    else:
-        list = []
+    return traceback.format_list(extracted_list)
+
+
+def _format_exception_only(eclass, evalue):
+    """Format the excption part of a traceback.
+
+    :param eclass: The type of the exception being formatted.
+    :param evalue: The exception instance.
+    :returns: A list of unicode strings.
+    """
+    list = []
     if evalue is None:
         # Is a (deprecated) string exception
         list.append((eclass + "\n").decode("ascii", "replace"))
@@ -377,6 +351,7 @@ def _format_exc_info(eclass, evalue, tb, limit=None):
                 else:
                     line = line.decode("ascii", "replace")
             if filename:
+                fs_enc = sys.getfilesystemencoding()
                 filename = filename.decode(fs_enc, "replace")
             evalue = eclass(msg, (filename, lineno, offset, line))
             list.extend(traceback.format_exception_only(eclass, evalue))
@@ -387,7 +362,24 @@ def _format_exc_info(eclass, evalue, tb, limit=None):
         list.append("%s: %s\n" % (sclass, svalue))
     elif svalue is None:
         # GZ 2010-05-24: Not a great fallback message, but keep for the moment
-        list.append("%s: <unprintable %s object>\n" % (sclass, sclass))
+        list.append(_u("%s: <unprintable %s object>\n" % (sclass, sclass)))
     else:
-        list.append("%s\n" % sclass)
+        list.append(_u("%s\n" % sclass))
     return list
+
+
+_TB_HEADER = _u('Traceback (most recent call last):\n')
+
+
+def _format_exc_info(eclass, evalue, tb, limit=None):
+    """Format a stack trace and the exception information as unicode
+
+    Compatibility function for Python 2 which ensures each component of a
+    traceback is correctly decoded according to its origins.
+
+    Based on traceback.format_exception and related functions.
+    """
+    return [_TB_HEADER] \
+        + _format_stack_list(traceback.extract_tb(tb, limit)) \
+        + _format_exception_only(eclass, evalue)
+
diff --git a/lib/testtools/testtools/content.py b/lib/testtools/testtools/content.py
index 8bd4a22..101b631 100644
--- a/lib/testtools/testtools/content.py
+++ b/lib/testtools/testtools/content.py
@@ -7,18 +7,29 @@ __all__ = [
     'Content',
     'content_from_file',
     'content_from_stream',
+    'json_content',
     'text_content',
     'TracebackContent',
     ]
 
 import codecs
+import inspect
 import json
 import os
 import sys
 import traceback
 
-from testtools import try_import
-from testtools.compat import _b, _format_exc_info, str_is_unicode, _u
+from extras import try_import
+
+from testtools.compat import (
+    _b,
+    _format_exception_only,
+    _format_stack_list,
+    _TB_HEADER,
+    _u,
+    istext,
+    str_is_unicode,
+)
 from testtools.content_type import ContentType, JSON, UTF8_TEXT
 
 
@@ -52,11 +63,11 @@ def _iter_chunks(stream, chunk_size, seek_offset=None, seek_whence=0):
 class Content(object):
     """A MIME-like Content object.
 
-    Content objects can be serialised to bytes using the iter_bytes method.
-    If the Content-Type is recognised by other code, they are welcome to
+    'Content' objects can be serialised to bytes using the iter_bytes method.
+    If the 'Content-Type' is recognised by other code, they are welcome to
     look for richer contents that mere byte serialisation - for example in
     memory object graphs etc. However, such code MUST be prepared to receive
-    a generic Content object that has been reconstructed from a byte stream.
+    a generic 'Content' object that has been reconstructed from a byte stream.
 
     :ivar content_type: The content type of this Content.
     """
@@ -102,28 +113,25 @@ class Content(object):
     def _iter_text(self):
         """Worker for iter_text - does the decoding."""
         encoding = self.content_type.parameters.get('charset', 'ISO-8859-1')
-        try:
-            # 2.5+
-            decoder = codecs.getincrementaldecoder(encoding)()
-            for bytes in self.iter_bytes():
-                yield decoder.decode(bytes)
-            final = decoder.decode(_b(''), True)
-            if final:
-                yield final
-        except AttributeError:
-            # < 2.5
-            bytes = ''.join(self.iter_bytes())
-            yield bytes.decode(encoding)
+        decoder = codecs.getincrementaldecoder(encoding)()
+        for bytes in self.iter_bytes():
+            yield decoder.decode(bytes)
+        final = decoder.decode(_b(''), True)
+        if final:
+            yield final
 
     def __repr__(self):
         return "<Content type=%r, value=%r>" % (
             self.content_type, _join_b(self.iter_bytes()))
 
 
-class TracebackContent(Content):
-    """Content object for tracebacks.
+class StackLinesContent(Content):
+    """Content object for stack lines.
+
+    This adapts a list of "preprocessed" stack lines into a 'Content' object.
+    The stack lines are most likely produced from ``traceback.extract_stack``
+    or ``traceback.extract_tb``.
 
-    This adapts an exc_info tuple to the Content interface.
     text/x-traceback;language=python is used for the mime type, in order to
     provide room for other languages to format their tracebacks differently.
     """
@@ -133,69 +141,117 @@ class TracebackContent(Content):
     # system-under-test is rarely unittest or testtools.
     HIDE_INTERNAL_STACK = True
 
-    def __init__(self, err, test):
-        """Create a TracebackContent for err."""
-        if err is None:
-            raise ValueError("err may not be None")
+    def __init__(self, stack_lines, prefix_content="", postfix_content=""):
+        """Create a StackLinesContent for ``stack_lines``.
+
+        :param stack_lines: A list of preprocessed stack lines, probably
+            obtained by calling ``traceback.extract_stack`` or
+            ``traceback.extract_tb``.
+        :param prefix_content: If specified, a unicode string to prepend to the
+            text content.
+        :param postfix_content: If specified, a unicode string to append to the
+            text content.
+        """
         content_type = ContentType('text', 'x-traceback',
             {"language": "python", "charset": "utf8"})
-        value = self._exc_info_to_unicode(err, test)
-        super(TracebackContent, self).__init__(
+        value = prefix_content + \
+            self._stack_lines_to_unicode(stack_lines) + \
+            postfix_content
+        super(StackLinesContent, self).__init__(
             content_type, lambda: [value.encode("utf8")])
 
-    def _exc_info_to_unicode(self, err, test):
-        """Converts a sys.exc_info()-style tuple of values into a string.
-
-        Copied from Python 2.7's unittest.TestResult._exc_info_to_string.
+    def _stack_lines_to_unicode(self, stack_lines):
+        """Converts a list of pre-processed stack lines into a unicode string.
         """
-        exctype, value, tb = err
-        # Skip test runner traceback levels
-        if self.HIDE_INTERNAL_STACK:
-            while tb and self._is_relevant_tb_level(tb):
-                tb = tb.tb_next
 
         # testtools customization. When str is unicode (e.g. IronPython,
         # Python 3), traceback.format_exception returns unicode. For Python 2,
         # it returns bytes. We need to guarantee unicode.
         if str_is_unicode:
-            format_exception = traceback.format_exception
+            format_stack_lines = traceback.format_list
         else:
-            format_exception = _format_exc_info
+            format_stack_lines = _format_stack_list
 
-        if (self.HIDE_INTERNAL_STACK and test.failureException
-            and isinstance(value, test.failureException)):
-            # Skip assert*() traceback levels
-            length = self._count_relevant_tb_levels(tb)
-            msgLines = format_exception(exctype, value, tb, length)
-        else:
-            msgLines = format_exception(exctype, value, tb)
-
-        if getattr(self, 'buffer', None):
-            output = sys.stdout.getvalue()
-            error = sys.stderr.getvalue()
-            if output:
-                if not output.endswith('\n'):
-                    output += '\n'
-                msgLines.append(STDOUT_LINE % output)
-            if error:
-                if not error.endswith('\n'):
-                    error += '\n'
-                msgLines.append(STDERR_LINE % error)
-        return ''.join(msgLines)
-
-    def _is_relevant_tb_level(self, tb):
-        return '__unittest' in tb.tb_frame.f_globals
-
-    def _count_relevant_tb_levels(self, tb):
-        length = 0
+        msg_lines = format_stack_lines(stack_lines)
+
+        return ''.join(msg_lines)
+
+
+def TracebackContent(err, test):
+    """Content object for tracebacks.
+
+    This adapts an exc_info tuple to the 'Content' interface.
+    'text/x-traceback;language=python' is used for the mime type, in order to
+    provide room for other languages to format their tracebacks differently.
+    """
+    if err is None:
+        raise ValueError("err may not be None")
+
+    exctype, value, tb = err
+    # Skip test runner traceback levels
+    if StackLinesContent.HIDE_INTERNAL_STACK:
+        while tb and '__unittest' in tb.tb_frame.f_globals:
+            tb = tb.tb_next
+
+    # testtools customization. When str is unicode (e.g. IronPython,
+    # Python 3), traceback.format_exception_only returns unicode. For Python 2,
+    # it returns bytes. We need to guarantee unicode.
+    if str_is_unicode:
+        format_exception_only = traceback.format_exception_only
+    else:
+        format_exception_only = _format_exception_only
+
+    limit = None
+    # Disabled due to https://bugs.launchpad.net/testtools/+bug/1188420
+    if (False
+        and StackLinesContent.HIDE_INTERNAL_STACK
+        and test.failureException
+        and isinstance(value, test.failureException)):
+        # Skip assert*() traceback levels
+        limit = 0
         while tb and not self._is_relevant_tb_level(tb):
-            length += 1
+            limit += 1
             tb = tb.tb_next
-        return length
+
+    prefix = _TB_HEADER
+    stack_lines = traceback.extract_tb(tb, limit)
+    postfix = ''.join(format_exception_only(exctype, value))
+
+    return StackLinesContent(stack_lines, prefix, postfix)
+
+
+def StacktraceContent(prefix_content="", postfix_content=""):
+    """Content object for stack traces.
+
+    This function will create and return a 'Content' object that contains a
+    stack trace.
+
+    The mime type is set to 'text/x-traceback;language=python', so other
+    languages can format their stack traces differently.
+
+    :param prefix_content: A unicode string to add before the stack lines.
+    :param postfix_content: A unicode string to add after the stack lines.
+    """
+    stack = inspect.stack()[1:]
+
+    if StackLinesContent.HIDE_INTERNAL_STACK:
+        limit = 1
+        while limit < len(stack) and '__unittest' not in stack[limit][0].f_globals:
+            limit += 1
+    else:
+        limit = -1
+
+    frames_only = [line[0] for line in stack[:limit]]
+    processed_stack = [ ]
+    for frame in reversed(frames_only):
+        filename, line, function, context, _ = inspect.getframeinfo(frame)
+        context = ''.join(context)
+        processed_stack.append((filename, line, function, context))
+    return StackLinesContent(processed_stack, prefix_content, postfix_content)
 
 
 def json_content(json_data):
-    """Create a JSON `Content` object from JSON-encodeable data."""
+    """Create a JSON Content object from JSON-encodeable data."""
     data = json.dumps(json_data)
     if str_is_unicode:
         # The json module perversely returns native str not bytes
@@ -204,10 +260,14 @@ def json_content(json_data):
 
 
 def text_content(text):
-    """Create a `Content` object from some text.
+    """Create a Content object from some text.
 
     This is useful for adding details which are short strings.
     """
+    if not istext(text):
+        raise TypeError(
+            "text_content must be given text, not '%s'." % type(text).__name__
+        )
     return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
 
 
@@ -220,9 +280,9 @@ def maybe_wrap(wrapper, func):
 
 def content_from_file(path, content_type=None, chunk_size=DEFAULT_CHUNK_SIZE,
                       buffer_now=False, seek_offset=None, seek_whence=0):
-    """Create a `Content` object from a file on disk.
+    """Create a Content object from a file on disk.
 
-    Note that unless 'read_now' is explicitly passed in as True, the file
+    Note that unless ``buffer_now`` is explicitly passed in as True, the file
     will only be read from when ``iter_bytes`` is called.
 
     :param path: The path to the file to be used as content.
@@ -233,31 +293,30 @@ def content_from_file(path, content_type=None, chunk_size=DEFAULT_CHUNK_SIZE,
     :param buffer_now: If True, read the file from disk now and keep it in
         memory. Otherwise, only read when the content is serialized.
     :param seek_offset: If non-None, seek within the stream before reading it.
-    :param seek_whence: If supplied, pass to stream.seek() when seeking.
+    :param seek_whence: If supplied, pass to ``stream.seek()`` when seeking.
     """
     if content_type is None:
         content_type = UTF8_TEXT
     def reader():
-        # This should be try:finally:, but python2.4 makes that hard. When
-        # We drop older python support we can make this use a context manager
-        # for maximum simplicity.
-        stream = open(path, 'rb')
-        for chunk in _iter_chunks(stream, chunk_size, seek_offset, seek_whence):
-            yield chunk
-        stream.close()
+        with open(path, 'rb') as stream:
+            for chunk in _iter_chunks(stream,
+                                      chunk_size,
+                                      seek_offset,
+                                      seek_whence):
+                yield chunk
     return content_from_reader(reader, content_type, buffer_now)
 
 
 def content_from_stream(stream, content_type=None,
                         chunk_size=DEFAULT_CHUNK_SIZE, buffer_now=False,
                         seek_offset=None, seek_whence=0):
-    """Create a `Content` object from a file-like stream.
+    """Create a Content object from a file-like stream.
 
-    Note that the stream will only be read from when ``iter_bytes`` is
-    called.
+    Note that unless ``buffer_now`` is explicitly passed in as True, the stream
+    will only be read from when ``iter_bytes`` is called.
 
     :param stream: A file-like object to read the content from. The stream
-        is not closed by this function or the content object it returns.
+        is not closed by this function or the 'Content' object it returns.
     :param content_type: The type of content. If not specified, defaults
         to UTF8-encoded text/plain.
     :param chunk_size: The size of chunks to read from the file.
@@ -265,7 +324,7 @@ def content_from_stream(stream, content_type=None,
     :param buffer_now: If True, reads from the stream right now. Otherwise,
         only reads when the content is serialized. Defaults to False.
     :param seek_offset: If non-None, seek within the stream before reading it.
-    :param seek_whence: If supplied, pass to stream.seek() when seeking.
+    :param seek_whence: If supplied, pass to ``stream.seek()`` when seeking.
     """
     if content_type is None:
         content_type = UTF8_TEXT
@@ -296,9 +355,9 @@ def attach_file(detailed, path, name=None, content_type=None,
 
     This is a convenience method wrapping around ``addDetail``.
 
-    Note that unless 'read_now' is explicitly passed in as True, the file
-    *must* exist when the test result is called with the results of this
-    test, after the test has been torn down.
+    Note that by default the contents of the file will be read immediately. If
+    ``buffer_now`` is False, then the file *must* exist when the test result is
+    called with the results of this test, after the test has been torn down.
 
     :param detailed: An object with details
     :param path: The path to the file to attach.
diff --git a/lib/testtools/testtools/content_type.py b/lib/testtools/testtools/content_type.py
index c491408..bbf314b 100644
--- a/lib/testtools/testtools/content_type.py
+++ b/lib/testtools/testtools/content_type.py
@@ -29,7 +29,7 @@ class ContentType(object):
     def __repr__(self):
         if self.parameters:
             params = '; '
-            params += ', '.join(
+            params += '; '.join(
                 sorted('%s="%s"' % (k, v) for k, v in self.parameters.items()))
         else:
             params = ''
diff --git a/lib/testtools/testtools/deferredruntest.py b/lib/testtools/testtools/deferredruntest.py
index cf33c06..d22c79f 100644
--- a/lib/testtools/testtools/deferredruntest.py
+++ b/lib/testtools/testtools/deferredruntest.py
@@ -89,14 +89,21 @@ class AsynchronousDeferredRunTest(_DeferredRunTest):
     This is highly experimental code.  Use at your own risk.
     """
 
-    def __init__(self, case, handlers=None, reactor=None, timeout=0.005,
-                 debug=False):
+    def __init__(self, case, handlers=None, last_resort=None, reactor=None,
+                 timeout=0.005, debug=False):
         """Construct an `AsynchronousDeferredRunTest`.
 
+        Please be sure to always use keyword syntax, not positional, as the
+        base class may add arguments in future - and for core code
+        compatibility with that we have to insert them before the local
+        parameters.
+
         :param case: The `TestCase` to run.
         :param handlers: A list of exception handlers (ExceptionType, handler)
             where 'handler' is a callable that takes a `TestCase`, a
             ``testtools.TestResult`` and the exception raised.
+        :param last_resort: Handler to call before re-raising uncatchable
+            exceptions (those for which there is no handler).
         :param reactor: The Twisted reactor to use.  If not given, we use the
             default reactor.
         :param timeout: The maximum time allowed for running a test.  The
@@ -105,7 +112,8 @@ class AsynchronousDeferredRunTest(_DeferredRunTest):
             to get information about unhandled Deferreds and left-over
             DelayedCalls.  Defaults to False.
         """
-        super(AsynchronousDeferredRunTest, self).__init__(case, handlers)
+        super(AsynchronousDeferredRunTest, self).__init__(
+            case, handlers, last_resort)
         if reactor is None:
             from twisted.internet import reactor
         self._reactor = reactor
@@ -119,8 +127,8 @@ class AsynchronousDeferredRunTest(_DeferredRunTest):
         # will be able to be assigned to a class variable *and* also be
         # invoked directly.
         class AsynchronousDeferredRunTestFactory:
-            def __call__(self, case, handlers=None):
-                return cls(case, handlers, reactor, timeout, debug)
+            def __call__(self, case, handlers=None, last_resort=None):
+                return cls(case, handlers, last_resort, reactor, timeout, debug)
         return AsynchronousDeferredRunTestFactory()
 
     @defer.deferredGenerator
diff --git a/lib/testtools/testtools/distutilscmd.py b/lib/testtools/testtools/distutilscmd.py
index 91e14ca..a4d79dc 100644
--- a/lib/testtools/testtools/distutilscmd.py
+++ b/lib/testtools/testtools/distutilscmd.py
@@ -26,7 +26,7 @@ class TestCommand(Command):
 
     def __init__(self, dist):
         Command.__init__(self, dist)
-        self.runner = TestToolsTestRunner(sys.stdout)
+        self.runner = TestToolsTestRunner(stdout=sys.stdout)
 
 
     def initialize_options(self):
diff --git a/lib/testtools/testtools/helpers.py b/lib/testtools/testtools/helpers.py
index 2595c1d..401d2cc 100644
--- a/lib/testtools/testtools/helpers.py
+++ b/lib/testtools/testtools/helpers.py
@@ -8,83 +8,12 @@ __all__ = [
 
 import sys
 
-
-def try_import(name, alternative=None, error_callback=None):
-    """Attempt to import ``name``.  If it fails, return ``alternative``.
-
-    When supporting multiple versions of Python or optional dependencies, it
-    is useful to be able to try to import a module.
-
-    :param name: The name of the object to import, e.g. ``os.path`` or
-        ``os.path.join``.
-    :param alternative: The value to return if no module can be imported.
-        Defaults to None.
-    :param error_callback: If non-None, a callable that is passed the ImportError
-        when the module cannot be loaded.
-    """
-    module_segments = name.split('.')
-    last_error = None
-    while module_segments:
-        module_name = '.'.join(module_segments)
-        try:
-            module = __import__(module_name)
-        except ImportError:
-            last_error = sys.exc_info()[1]
-            module_segments.pop()
-            continue
-        else:
-            break
-    else:
-        if last_error is not None and error_callback is not None:
-            error_callback(last_error)
-        return alternative
-    nonexistent = object()
-    for segment in name.split('.')[1:]:
-        module = getattr(module, segment, nonexistent)
-        if module is nonexistent:
-            if last_error is not None and error_callback is not None:
-                error_callback(last_error)
-            return alternative
-    return module
-
-
-_RAISE_EXCEPTION = object()
-def try_imports(module_names, alternative=_RAISE_EXCEPTION, error_callback=None):
-    """Attempt to import modules.
-
-    Tries to import the first module in ``module_names``.  If it can be
-    imported, we return it.  If not, we go on to the second module and try
-    that.  The process continues until we run out of modules to try.  If none
-    of the modules can be imported, either raise an exception or return the
-    provided ``alternative`` value.
-
-    :param module_names: A sequence of module names to try to import.
-    :param alternative: The value to return if no module can be imported.
-        If unspecified, we raise an ImportError.
-    :param error_callback: If None, called with the ImportError for *each*
-        module that fails to load.
-    :raises ImportError: If none of the modules can be imported and no
-        alternative value was specified.
-    """
-    module_names = list(module_names)
-    for module_name in module_names:
-        module = try_import(module_name, error_callback=error_callback)
-        if module:
-            return module
-    if alternative is _RAISE_EXCEPTION:
-        raise ImportError(
-            "Could not import any of: %s" % ', '.join(module_names))
-    return alternative
-
-
-def safe_hasattr(obj, attr, _marker=object()):
-    """Does 'obj' have an attribute 'attr'?
-
-    Use this rather than built-in hasattr, as the built-in swallows exceptions
-    in some versions of Python and behaves unpredictably with respect to
-    properties.
-    """
-    return getattr(obj, attr, _marker) is not _marker
+# Compat - removal announced in 0.9.25.
+from extras import (
+    safe_hasattr,
+    try_import,
+    try_imports,
+    )
 
 
 def map_values(function, dictionary):
diff --git a/lib/testtools/testtools/matchers/__init__.py b/lib/testtools/testtools/matchers/__init__.py
index ce949fd..771d814 100644
--- a/lib/testtools/testtools/matchers/__init__.py
+++ b/lib/testtools/testtools/matchers/__init__.py
@@ -16,6 +16,7 @@ __all__ = [
     'AfterPreprocessing',
     'AllMatch',
     'Annotate',
+    'AnyMatch',
     'Contains',
     'ContainsAll',
     'ContainedByDict',
@@ -28,6 +29,7 @@ __all__ = [
     'FileContains',
     'FileExists',
     'GreaterThan',
+    'HasLength',
     'HasPermissions',
     'Is',
     'IsInstance',
@@ -39,6 +41,7 @@ __all__ = [
     'MatchesException',
     'MatchesListwise',
     'MatchesPredicate',
+    'MatchesPredicateWithParams',
     'MatchesRegex',
     'MatchesSetwise',
     'MatchesStructure',
@@ -57,6 +60,7 @@ from ._basic import (
     EndsWith,
     Equals,
     GreaterThan,
+    HasLength,
     Is,
     IsInstance,
     LessThan,
@@ -98,9 +102,11 @@ from ._higherorder import (
     AfterPreprocessing,
     AllMatch,
     Annotate,
+    AnyMatch,
     MatchesAll,
     MatchesAny,
     MatchesPredicate,
+    MatchesPredicateWithParams,
     Not,
     )
 
diff --git a/lib/testtools/testtools/matchers/_basic.py b/lib/testtools/testtools/matchers/_basic.py
index 44a47c5..2d9f143 100644
--- a/lib/testtools/testtools/matchers/_basic.py
+++ b/lib/testtools/testtools/matchers/_basic.py
@@ -5,6 +5,7 @@ __all__ = [
     'EndsWith',
     'Equals',
     'GreaterThan',
+    'HasLength',
     'Is',
     'IsInstance',
     'LessThan',
@@ -24,7 +25,10 @@ from ..compat import (
     text_repr,
     )
 from ..helpers import list_subtract
-from ._higherorder import PostfixedMismatch
+from ._higherorder import (
+    MatchesPredicateWithParams,
+    PostfixedMismatch,
+    )
 from ._impl import (
     Matcher,
     Mismatch,
@@ -313,3 +317,10 @@ class MatchesRegex(object):
             pattern = pattern.encode("unicode_escape").decode("ascii")
             return Mismatch("%r does not match /%s/" % (
                     value, pattern.replace("\\\\", "\\")))
+
+
+def has_len(x, y):
+    return len(x) == y
+
+
+HasLength = MatchesPredicateWithParams(has_len, "len({0}) != {1}", "HasLength")
diff --git a/lib/testtools/testtools/matchers/_dict.py b/lib/testtools/testtools/matchers/_dict.py
index ff05199..b1ec915 100644
--- a/lib/testtools/testtools/matchers/_dict.py
+++ b/lib/testtools/testtools/matchers/_dict.py
@@ -241,7 +241,7 @@ class KeysEqual(Matcher):
         """
         super(KeysEqual, self).__init__()
         try:
-            self.expected = expected.keys()
+            self.expected = expected[0].keys()
         except AttributeError:
             self.expected = list(expected)
 
diff --git a/lib/testtools/testtools/matchers/_exception.py b/lib/testtools/testtools/matchers/_exception.py
index c120487..cd4c90b 100644
--- a/lib/testtools/testtools/matchers/_exception.py
+++ b/lib/testtools/testtools/matchers/_exception.py
@@ -10,8 +10,6 @@ import sys
 
 from testtools.compat import (
     classtypes,
-    _error_repr,
-    isbaseexception,
     istext,
     )
 from ._basic import MatchesRegex
@@ -22,6 +20,17 @@ from ._impl import (
     )
 
 
+_error_repr = BaseException.__repr__
+
+
+def _is_exception(exc):
+    return isinstance(exc, BaseException)
+
+
+def _is_user_exception(exc):
+    return isinstance(exc, Exception)
+
+
 class MatchesException(Matcher):
     """Match an exc_info tuple against an exception instance or type."""
 
@@ -44,7 +53,9 @@ class MatchesException(Matcher):
         if istext(value_re):
             value_re = AfterPreproccessing(str, MatchesRegex(value_re), False)
         self.value_re = value_re
-        self._is_instance = type(self.expected) not in classtypes() + (tuple,)
+        expected_type = type(self.expected)
+        self._is_instance = not any(issubclass(expected_type, class_type)
+                for class_type in classtypes() + (tuple,))
 
     def match(self, other):
         if type(other) != tuple:
@@ -101,9 +112,10 @@ class Raises(Matcher):
             else:
                 mismatch = None
             # The exception did not match, or no explicit matching logic was
-            # performed. If the exception is a non-user exception (that is, not
-            # a subclass of Exception on Python 2.5+) then propogate it.
-            if isbaseexception(exc_info[1]):
+            # performed. If the exception is a non-user exception then
+            # propagate it.
+            exception = exc_info[1]
+            if _is_exception(exception) and not _is_user_exception(exception):
                 del exc_info
                 raise
             return mismatch
diff --git a/lib/testtools/testtools/matchers/_higherorder.py b/lib/testtools/testtools/matchers/_higherorder.py
index 53c52b6..3570f57 100644
--- a/lib/testtools/testtools/matchers/_higherorder.py
+++ b/lib/testtools/testtools/matchers/_higherorder.py
@@ -4,6 +4,7 @@ __all__ = [
     'AfterPreprocessing',
     'AllMatch',
     'Annotate',
+    'AnyMatch',
     'MatchesAny',
     'MatchesAll',
     'Not',
@@ -287,3 +288,81 @@ class MatchesPredicate(Matcher):
     def match(self, x):
         if not self.predicate(x):
             return Mismatch(self.message % x)
+
+
+def MatchesPredicateWithParams(predicate, message, name=None):
+    """Match if a given parameterised function returns True.
+
+    It is reasonably common to want to make a very simple matcher based on a
+    function that you already have that returns True or False given some
+    arguments. This matcher makes it very easy to do so. e.g.::
+
+      HasLength = MatchesPredicate(
+          lambda x, y: len(x) == y, 'len({0}) is not {1}')
+      # This assertion will fail, as 'len([1, 2]) == 3' is False.
+      self.assertThat([1, 2], HasLength(3))
+
+    Note that unlike MatchesPredicate MatchesPredicateWithParams returns a
+    factory which you then customise to use by constructing an actual matcher
+    from it.
+
+    The predicate function should take the object to match as its first
+    parameter. Any additional parameters supplied when constructing a matcher
+    are supplied to the predicate as additional parameters when checking for a
+    match.
+
+    :param predicate: The predicate function.
+    :param message: A format string for describing mis-matches.
+    :param name: Optional replacement name for the matcher.
+    """
+    def construct_matcher(*args, **kwargs):
+        return _MatchesPredicateWithParams(
+            predicate, message, name, *args, **kwargs)
+    return construct_matcher
+
+
+class _MatchesPredicateWithParams(Matcher):
+
+    def __init__(self, predicate, message, name, *args, **kwargs):
+        """Create a ``MatchesPredicateWithParams`` matcher.
+
+        :param predicate: A function that takes an object to match and
+            additional params as given in ``*args`` and ``**kwargs``. The
+            result of the function will be interpreted as a boolean to
+            determine a match.
+        :param message: A message to describe a mismatch.  It will be formatted
+            with .format() and be given a tuple containing whatever was passed
+            to ``match()`` + ``*args`` in ``*args``, and whatever was passed to
+            ``**kwargs`` as its ``**kwargs``.
+
+            For instance, to format a single parameter::
+
+                "{0} is not a {1}"
+
+            To format a keyword arg::
+
+                "{0} is not a {type_to_check}"
+        :param name: What name to use for the matcher class. Pass None to use
+            the default.
+        """
+        self.predicate = predicate
+        self.message = message
+        self.name = name
+        self.args = args
+        self.kwargs = kwargs
+
+    def __str__(self):
+        args = [str(arg) for arg in self.args]
+        kwargs = ["%s=%s" % item for item in self.kwargs.items()]
+        args = ", ".join(args + kwargs)
+        if self.name is None:
+            name = 'MatchesPredicateWithParams(%r, %r)' % (
+                self.predicate, self.message)
+        else:
+            name = self.name
+        return '%s(%s)' % (name, args)
+
+    def match(self, x):
+        if not self.predicate(x, *self.args, **self.kwargs):
+            return Mismatch(
+                self.message.format(*((x,) + self.args), **self.kwargs))
diff --git a/lib/testtools/testtools/matchers/_impl.py b/lib/testtools/testtools/matchers/_impl.py
index 36e5ee0..19a93af 100644
--- a/lib/testtools/testtools/matchers/_impl.py
+++ b/lib/testtools/testtools/matchers/_impl.py
@@ -114,9 +114,7 @@ class MismatchError(AssertionError):
     # characters are in the matchee, matcher or mismatch.
 
     def __init__(self, matchee, matcher, mismatch, verbose=False):
-        # Have to use old-style upcalling for Python 2.4 and 2.5
-        # compatibility.
-        AssertionError.__init__(self)
+        super(MismatchError, self).__init__()
         self.matchee = matchee
         self.matcher = matcher
         self.mismatch = mismatch
diff --git a/lib/testtools/testtools/run.py b/lib/testtools/testtools/run.py
index c417bd0..48cc807 100755
--- a/lib/testtools/testtools/run.py
+++ b/lib/testtools/testtools/run.py
@@ -8,46 +8,93 @@ For instance, to run the testtools test suite.
  $ python -m testtools.run testtools.tests.test_suite
 """
 
-import os
-import unittest
+import argparse
+from functools import partial
+import os.path
+import unittest2 as unittest
 import sys
 
-from testtools import TextTestResult
+from extras import safe_hasattr
+
+from testtools import TextTestResult, testcase
 from testtools.compat import classtypes, istext, unicode_output_stream
-from testtools.testsuite import iterate_tests, sorted_tests
+from testtools.testsuite import filter_by_ids, iterate_tests, sorted_tests
 
 
 defaultTestLoader = unittest.defaultTestLoader
 defaultTestLoaderCls = unittest.TestLoader
-
-if getattr(defaultTestLoader, 'discover', None) is None:
-    try:
-        import discover
-        defaultTestLoader = discover.DiscoveringTestLoader()
-        defaultTestLoaderCls = discover.DiscoveringTestLoader
-        have_discover = True
-    except ImportError:
-        have_discover = False
-else:
-    have_discover = True
+have_discover = True
+discover_impl = unittest.loader
+
+# Kept for API compatibility, but no longer used.
+BUFFEROUTPUT = ""
+CATCHBREAK = ""
+FAILFAST = ""
+USAGE_AS_MAIN = ""
+
+def list_test(test):
+    """Return the test ids that would be run if test() was run.
+
+    When things fail to import they can be represented as well, though
+    we use an ugly hack (see http://bugs.python.org/issue19746 for details)
+    to determine that. The difference matters because if a user is
+    filtering tests to run on the returned ids, a failed import can reduce
+    the visible tests but it can be impossible to tell that the selected
+    test would have been one of the imported ones.
+
+    :return: A tuple of test ids that would run and error strings
+        describing things that failed to import.
+    """
+    unittest_import_strs = set([
+        'unittest2.loader.ModuleImportFailure.',
+        'unittest.loader.ModuleImportFailure.',
+        'discover.ModuleImportFailure.'
+        ])
+    test_ids = []
+    errors = []
+    for test in iterate_tests(test):
+        # Much ugly.
+        for prefix in unittest_import_strs:
+            if test.id().startswith(prefix):
+                errors.append(test.id()[len(prefix):])
+                break
+        else:
+            test_ids.append(test.id())
+    return test_ids, errors
 
 
 class TestToolsTestRunner(object):
     """ A thunk object to support unittest.TestProgram."""
 
-    def __init__(self, verbosity=None, failfast=None, buffer=None):
+    def __init__(self, verbosity=None, failfast=None, buffer=None,
+        stdout=None):
         """Create a TestToolsTestRunner.
 
         :param verbosity: Ignored.
         :param failfast: Stop running tests at the first failure.
         :param buffer: Ignored.
+        :param stdout: Stream to use for stdout.
         """
         self.failfast = failfast
+        if stdout is None:
+            stdout = sys.stdout
+        self.stdout = stdout
+
+    def list(self, test, loader):
+        """List the tests that would be run if test() was run."""
+        test_ids, _ = list_test(test)
+        for test_id in test_ids:
+            self.stdout.write('%s\n' % test_id)
+        errors = loader.errors
+        if errors:
+            for test_id in errors:
+                self.stdout.write('%s\n' % test_id)
+            sys.exit(2)
 
     def run(self, test):
         "Run the given test case or test suite."
         result = TextTestResult(
-            unicode_output_stream(sys.stdout), failfast=self.failfast)
+            unicode_output_stream(self.stdout), failfast=self.failfast)
         result.startTestRun()
         try:
             return test.run(result)
@@ -59,73 +106,23 @@ class TestToolsTestRunner(object):
 # Taken from python 2.7 and slightly modified for compatibility with
 # older versions. Delete when 2.7 is the oldest supported version.
 # Modifications:
-#  - Use have_discover to raise an error if the user tries to use
-#    discovery on an old version and doesn't have discover installed.
-#  - If --catch is given check that installHandler is available, as
-#    it won't be on old python versions.
-#  - print calls have been been made single-source python3 compatibile.
-#  - exception handling likewise.
-#  - The default help has been changed to USAGE_AS_MAIN and USAGE_FROM_MODULE
-#    removed.
-#  - A tweak has been added to detect 'python -m *.run' and use a
-#    better progName in that case.
-#  - self.module is more comprehensively set to None when being invoked from
-#    the commandline - __name__ is used as a sentinel value.
+#  - If --catch is given, check that installHandler is available, as
+#    it won't be on old python versions or python builds without signals.
 #  - --list has been added which can list tests (should be upstreamed).
 #  - --load-list has been added which can reduce the tests used (should be
 #    upstreamed).
-#  - The limitation of using getopt is declared to the user.
-#  - http://bugs.python.org/issue16709 is worked around, by sorting tests when
-#    discover is used.
-
-FAILFAST     = "  -f, --failfast   Stop on first failure\n"
-CATCHBREAK   = "  -c, --catch      Catch control-C and display results\n"
-BUFFEROUTPUT = "  -b, --buffer     Buffer stdout and stderr during test runs\n"
-
-USAGE_AS_MAIN = """\
-Usage: %(progName)s [options] [tests]
-
-Options:
-  -h, --help       Show this message
-  -v, --verbose    Verbose output
-  -q, --quiet      Minimal output
-  -l, --list       List tests rather than executing them.
-  --load-list      Specifies a file containing test ids, only tests matching
-                   those ids are executed.
-%(failfast)s%(catchbreak)s%(buffer)s
-Examples:
-  %(progName)s test_module               - run tests from test_module
-  %(progName)s module.TestClass          - run tests from module.TestClass
-  %(progName)s module.Class.test_method  - run specified test method
-
-All options must come before [tests].  [tests] can be a list of any number of
-test modules, classes and test methods.
-
-Alternative Usage: %(progName)s discover [options]
-
-Options:
-  -v, --verbose    Verbose output
-%(failfast)s%(catchbreak)s%(buffer)s  -s directory     Directory to start discovery ('.' default)
-  -p pattern       Pattern to match test files ('test*.py' default)
-  -t directory     Top level directory of project (default to
-                   start directory)
-  -l, --list       List tests rather than executing them.
-  --load-list      Specifies a file containing test ids, only tests matching
-                   those ids are executed.
-
-For test discovery all test modules must be importable from the top
-level directory of the project.
-"""
 
 
-class TestProgram(object):
+class TestProgram(unittest.TestProgram):
     """A command-line program that runs a set of tests; this is primarily
        for making test modules conveniently executable.
     """
-    USAGE = USAGE_AS_MAIN
 
     # defaults for testing
+    module=None
+    verbosity = 1
     failfast = catchbreak = buffer = progName = None
+    _discovery_parser = None
 
     def __init__(self, module=__name__, defaultTest=None, argv=None,
                     testRunner=None, testLoader=defaultTestLoader,
@@ -143,6 +140,7 @@ class TestProgram(object):
             argv = sys.argv
         if stdout is None:
             stdout = sys.stdout
+        self.stdout = stdout
 
         self.exit = exit
         self.failfast = failfast
@@ -150,6 +148,7 @@ class TestProgram(object):
         self.verbosity = verbosity
         self.buffer = buffer
         self.defaultTest = defaultTest
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
         self.listtests = False
         self.load_list = None
         self.testRunner = testRunner
@@ -162,6 +161,7 @@ class TestProgram(object):
             progName = os.path.basename(argv[0])
         self.progName = progName
         self.parseArgs(argv)
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
         if self.load_list:
             # TODO: preserve existing suites (like testresources does in
             # OptimisingTestSuite.add, but with a standard protocol).
@@ -173,178 +173,78 @@ class TestProgram(object):
             finally:
                 source.close()
             test_ids = set(line.strip().decode('utf-8') for line in lines)
-            filtered = unittest.TestSuite()
-            for test in iterate_tests(self.test):
-                if test.id() in test_ids:
-                    filtered.addTest(test)
-            self.test = filtered
+            self.test = filter_by_ids(self.test, test_ids)
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
         if not self.listtests:
             self.runTests()
         else:
-            for test in iterate_tests(self.test):
-                stdout.write('%s\n' % test.id())
-
-    def usageExit(self, msg=None):
-        if msg:
-            print(msg)
-        usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '',
-                 'buffer': ''}
-        if self.failfast != False:
-            usage['failfast'] = FAILFAST
-        if self.catchbreak != False:
-            usage['catchbreak'] = CATCHBREAK
-        if self.buffer != False:
-            usage['buffer'] = BUFFEROUTPUT
-        print(self.USAGE % usage)
-        sys.exit(2)
-
-    def parseArgs(self, argv):
-        if len(argv) > 1 and argv[1].lower() == 'discover':
-            self._do_discovery(argv[2:])
-            return
-
-        import getopt
-        long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer',
-            'list', 'load-list=']
-        try:
-            options, args = getopt.getopt(argv[1:], 'hHvqfcbl', long_opts)
-            for opt, value in options:
-                if opt in ('-h','-H','--help'):
-                    self.usageExit()
-                if opt in ('-q','--quiet'):
-                    self.verbosity = 0
-                if opt in ('-v','--verbose'):
-                    self.verbosity = 2
-                if opt in ('-f','--failfast'):
-                    if self.failfast is None:
-                        self.failfast = True
-                    # Should this raise an exception if -f is not valid?
-                if opt in ('-c','--catch'):
-                    if self.catchbreak is None:
-                        self.catchbreak = True
-                    # Should this raise an exception if -c is not valid?
-                if opt in ('-b','--buffer'):
-                    if self.buffer is None:
-                        self.buffer = True
-                    # Should this raise an exception if -b is not valid?
-                if opt in ('-l', '--list'):
-                    self.listtests = True
-                if opt == '--load-list':
-                    self.load_list = value
-            if len(args) == 0 and self.defaultTest is None:
-                # createTests will load tests from self.module
-                self.testNames = None
-            elif len(args) > 0:
-                self.testNames = args
+            runner = self._get_runner()
+            if safe_hasattr(runner, 'list'):
+                try:
+                    runner.list(self.test, loader=self.testLoader)
+                except TypeError:
+                    runner.list(self.test)
             else:
-                self.testNames = (self.defaultTest,)
-            self.createTests()
-        except getopt.error:
-            self.usageExit(sys.exc_info()[1])
-
-    def createTests(self):
-        if self.testNames is None:
-            self.test = self.testLoader.loadTestsFromModule(self.module)
-        else:
-            self.test = self.testLoader.loadTestsFromNames(self.testNames,
-                                                           self.module)
-
-    def _do_discovery(self, argv, Loader=defaultTestLoaderCls):
-        # handle command line args for test discovery
-        if not have_discover:
-            raise AssertionError("Unable to use discovery, must use python 2.7 "
-                    "or greater, or install the discover package.")
-        self.progName = '%s discover' % self.progName
-        import optparse
-        parser = optparse.OptionParser()
-        parser.prog = self.progName
-        parser.add_option('-v', '--verbose', dest='verbose', default=False,
-                          help='Verbose output', action='store_true')
-        if self.failfast != False:
-            parser.add_option('-f', '--failfast', dest='failfast', default=False,
-                              help='Stop on first fail or error',
-                              action='store_true')
-        if self.catchbreak != False:
-            parser.add_option('-c', '--catch', dest='catchbreak', default=False,
-                              help='Catch ctrl-C and display results so far',
-                              action='store_true')
-        if self.buffer != False:
-            parser.add_option('-b', '--buffer', dest='buffer', default=False,
-                              help='Buffer stdout and stderr during tests',
-                              action='store_true')
-        parser.add_option('-s', '--start-directory', dest='start', default='.',
-                          help="Directory to start discovery ('.' default)")
-        parser.add_option('-p', '--pattern', dest='pattern', default='test*.py',
-                          help="Pattern to match tests ('test*.py' default)")
-        parser.add_option('-t', '--top-level-directory', dest='top', default=None,
-                          help='Top level directory of project (defaults to start directory)')
-        parser.add_option('-l', '--list', dest='listtests', default=False, action="store_true",
-                          help='List tests rather than running them.')
-        parser.add_option('--load-list', dest='load_list', default=None,
-                          help='Specify a filename containing the test ids to use.')
-
-        options, args = parser.parse_args(argv)
-        if len(args) > 3:
-            self.usageExit()
-
-        for name, value in zip(('start', 'pattern', 'top'), args):
-            setattr(options, name, value)
-
-        # only set options from the parsing here
-        # if they weren't set explicitly in the constructor
-        if self.failfast is None:
-            self.failfast = options.failfast
-        if self.catchbreak is None:
-            self.catchbreak = options.catchbreak
-        if self.buffer is None:
-            self.buffer = options.buffer
-        self.listtests = options.listtests
-        self.load_list = options.load_list
-
-        if options.verbose:
-            self.verbosity = 2
-
-        start_dir = options.start
-        pattern = options.pattern
-        top_level_dir = options.top
-
-        loader = Loader()
-        # See http://bugs.python.org/issue16709
-        # While sorting here is intrusive, its better than being random.
-        # Rules for the sort:
-        # - standard suites are flattened, and the resulting tests sorted by
-        #   id.
-        # - non-standard suites are preserved as-is, and sorted into position
-        #   by the first test found by iterating the suite.
-        # We do this by a DSU process: flatten and grab a key, sort, strip the
-        # keys.
-        loaded = loader.discover(start_dir, pattern, top_level_dir)
-        self.test = sorted_tests(loaded)
+                for test in iterate_tests(self.test):
+                    self.stdout.write('%s\n' % test.id())
+        del self.testLoader.errors[:]
+
+    def _getParentArgParser(self):
+        parser = super(TestProgram, self)._getParentArgParser()
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
+        parser.add_argument('-l', '--list', dest='listtests', default=False,
+            action='store_true', help='List tests rather than executing them')
+        parser.add_argument('--load-list', dest='load_list', default=None,
+            help='Specifies a file containing test ids, only tests matching '
+                'those ids are executed')
+        return parser
+
+    def _do_discovery(self, argv, Loader=None):
+        super(TestProgram, self)._do_discovery(argv, Loader=Loader)
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
+        self.test = sorted_tests(self.test)
 
     def runTests(self):
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
         if (self.catchbreak
             and getattr(unittest, 'installHandler', None) is not None):
             unittest.installHandler()
+        testRunner = self._get_runner()
+        self.result = testRunner.run(self.test)
+        if self.exit:
+            sys.exit(not self.result.wasSuccessful())
+
+    def _get_runner(self):
+        # XXX: Local edit (see http://bugs.python.org/issue22860)
         if self.testRunner is None:
             self.testRunner = TestToolsTestRunner
-        if isinstance(self.testRunner, classtypes()):
+        try:
+            testRunner = self.testRunner(verbosity=self.verbosity,
+                                         failfast=self.failfast,
+                                         buffer=self.buffer,
+                                         stdout=self.stdout)
+        except TypeError:
+            # didn't accept the verbosity, buffer, failfast or stdout arguments
+            # Try with the prior contract
             try:
                 testRunner = self.testRunner(verbosity=self.verbosity,
                                              failfast=self.failfast,
                                              buffer=self.buffer)
             except TypeError:
-                # didn't accept the verbosity, buffer or failfast arguments
-                testRunner = self.testRunner()
-        else:
-            # it is assumed to be a TestRunner instance
-            testRunner = self.testRunner
-        self.result = testRunner.run(self.test)
-        if self.exit:
-            sys.exit(not self.result.wasSuccessful())
+                # Now try calling it with defaults
+                try:
+                    testRunner = self.testRunner()
+                except TypeError:
+                    # it is assumed to be a TestRunner instance
+                    testRunner = self.testRunner
+        return testRunner
+
+
+
 ################
 
 def main(argv, stdout):
-    program = TestProgram(argv=argv, testRunner=TestToolsTestRunner,
+    program = TestProgram(argv=argv, testRunner=partial(TestToolsTestRunner, stdout=stdout),
         stdout=stdout)
 
 if __name__ == '__main__':
diff --git a/lib/testtools/testtools/runtest.py b/lib/testtools/testtools/runtest.py
index 507ad87..a29cdd6 100644
--- a/lib/testtools/testtools/runtest.py
+++ b/lib/testtools/testtools/runtest.py
@@ -47,17 +47,23 @@ class RunTest(object):
         reporting of error/failure/skip etc.
     """
 
-    def __init__(self, case, handlers=None):
+    def __init__(self, case, handlers=None, last_resort=None):
         """Create a RunTest to run a case.
 
         :param case: A testtools.TestCase test case object.
         :param handlers: Exception handlers for this RunTest. These are stored
             in self.handlers and can be modified later if needed.
+        :param last_resort: A handler of last resort: any exception which is
+            not handled by handlers will cause the last resort handler to be
+            called as last_resort(exc_info), and then the exception will be
+            raised - aborting the test run as this is inside the runner
+            machinery rather than the confined context of the test.
         """
         self.case = case
         self.handlers = handlers or []
         self.exception_caught = object()
         self._exceptions = []
+        self.last_resort = last_resort or (lambda case, result, exc: None)
 
     def run(self, result=None):
         """Run self.case reporting activity to result.
@@ -106,12 +112,23 @@ class RunTest(object):
                     if isinstance(e, exc_class):
                         handler(self.case, self.result, e)
                         break
+                else:
+                    self.last_resort(self.case, self.result, e)
+                    raise e
         finally:
             result.stopTest(self.case)
         return result
 
     def _run_core(self):
         """Run the user supplied test code."""
+        test_method = self.case._get_test_method()
+        if getattr(test_method, '__unittest_skip__', False):
+            self.result.addSkip(
+                self.case,
+                reason=getattr(test_method, '__unittest_skip_why__', None)
+            )
+            return
+
         if self.exception_caught == self._run_user(self.case._run_setup,
             self.result):
             # Don't run the test method if we failed getting here.
@@ -135,6 +152,9 @@ class RunTest(object):
                         self._run_cleanups, self.result):
                         failed = True
                 finally:
+                    if getattr(self.case, 'force_failure', None):
+                        self._run_user(_raise_force_fail_error)
+                        failed = True
                     if not failed:
                         self.result.addSuccess(self.case,
                             details=self.case.getDetails())
@@ -167,8 +187,6 @@ class RunTest(object):
         """
         try:
             return fn(*args, **kwargs)
-        except KeyboardInterrupt:
-            raise
         except:
             return self._got_user_exception(sys.exc_info())
 
@@ -193,11 +211,15 @@ class RunTest(object):
             self.case.onException(exc_info, tb_label=tb_label)
         finally:
             del exc_info
-        for exc_class, handler in self.handlers:
-            if isinstance(e, exc_class):
-                self._exceptions.append(e)
-                return self.exception_caught
-        raise e
+        self._exceptions.append(e)
+        # Yes, this means we catch everything - we re-raise KeyBoardInterrupt
+        # etc later, after tearDown and cleanUp - since those may be cleaning up
+        # external processes.
+        return self.exception_caught
+
+
+def _raise_force_fail_error():
+    raise AssertionError("Forced Test Failure")
 
 
 # Signal that this is part of the testing framework, and that code from this
diff --git a/lib/testtools/testtools/testcase.py b/lib/testtools/testtools/testcase.py
index fc5f863..cc4d6eb 100644
--- a/lib/testtools/testtools/testcase.py
+++ b/lib/testtools/testtools/testcase.py
@@ -4,6 +4,7 @@
 
 __metaclass__ = type
 __all__ = [
+    'attr',
     'clone_test_with_new_id',
     'ExpectedException',
     'gather_details',
@@ -15,14 +16,19 @@ __all__ = [
     ]
 
 import copy
+import functools
 import itertools
 import sys
 import types
-import unittest
+
+from extras import (
+    safe_hasattr,
+    try_import,
+    )
+import unittest2 as unittest
 
 from testtools import (
     content,
-    try_import,
     )
 from testtools.compat import (
     advance_iterator,
@@ -51,8 +57,8 @@ wraps = try_import('functools.wraps')
 
 class TestSkipped(Exception):
     """Raised within TestCase.run() when a test is skipped."""
-testSkipped = try_import('unittest2.case.SkipTest', TestSkipped)
 TestSkipped = try_import('unittest.case.SkipTest', TestSkipped)
+TestSkipped = try_import('unittest2.case.SkipTest', TestSkipped)
 
 
 class _UnexpectedSuccess(Exception):
@@ -62,9 +68,9 @@ class _UnexpectedSuccess(Exception):
     module.
     """
 _UnexpectedSuccess = try_import(
-    'unittest2.case._UnexpectedSuccess', _UnexpectedSuccess)
-_UnexpectedSuccess = try_import(
     'unittest.case._UnexpectedSuccess', _UnexpectedSuccess)
+_UnexpectedSuccess = try_import(
+    'unittest2.case._UnexpectedSuccess', _UnexpectedSuccess)
 
 class _ExpectedFailure(Exception):
     """An expected failure occured.
@@ -73,9 +79,23 @@ class _ExpectedFailure(Exception):
     module.
     """
 _ExpectedFailure = try_import(
-    'unittest2.case._ExpectedFailure', _ExpectedFailure)
-_ExpectedFailure = try_import(
     'unittest.case._ExpectedFailure', _ExpectedFailure)
+_ExpectedFailure = try_import(
+    'unittest2.case._ExpectedFailure', _ExpectedFailure)
+
+
+# Copied from unittest before python 3.4 release. Used to maintain
+# compatibility with unittest sub-test feature. Users should not use this
+# directly.
+def _expectedFailure(func):
+    @functools.wraps(func)
+    def wrapper(*args, **kwargs):
+        try:
+            func(*args, **kwargs)
+        except Exception:
+            raise _ExpectedFailure(sys.exc_info())
+        raise _UnexpectedSuccess
+    return wrapper
 
 
 def run_test_with(test_runner, **kwargs):
@@ -106,9 +126,16 @@ def run_test_with(test_runner, **kwargs):
     def decorator(function):
         # Set an attribute on 'function' which will inform TestCase how to
         # make the runner.
-        function._run_test_with = (
-            lambda case, handlers=None:
-                test_runner(case, handlers=handlers, **kwargs))
+        def _run_test_with(case, handlers=None, last_resort=None):
+            try:
+                return test_runner(
+                    case, handlers=handlers, last_resort=last_resort,
+                    **kwargs)
+            except TypeError:
+                # Backwards compat: if we can't call the constructor
+                # with last_resort, try without that.
+                return test_runner(case, handlers=handlers, **kwargs)
+        function._run_test_with = _run_test_with
         return function
     return decorator
 
@@ -150,6 +177,8 @@ class TestCase(unittest.TestCase):
     :ivar exception_handlers: Exceptions to catch from setUp, runTest and
         tearDown. This list is able to be modified at any time and consists of
         (exception_class, handler(case, result, exception_value)) pairs.
+    :ivar force_failure: Force testtools.RunTest to fail the test after the
+        test has completed.
     :cvar run_tests_with: A factory to make the ``RunTest`` to run tests with.
         Defaults to ``RunTest``.  The factory is expected to take a test case
         and an optional list of exception handlers.
@@ -185,7 +214,12 @@ class TestCase(unittest.TestCase):
             runTest = getattr(
                 test_method, '_run_test_with', self.run_tests_with)
         self.__RunTest = runTest
+        if getattr(test_method, '__unittest_expecting_failure__', False):
+            setattr(self, self._testMethodName, _expectedFailure(test_method))
+        # Used internally for onException processing - used to gather extra
+        # data from exceptions.
         self.__exception_handlers = []
+        # Passed to RunTest to map exceptions to result actions
         self.exception_handlers = [
             (self.skipException, self._report_skip),
             (self.failureException, self._report_failure),
@@ -193,9 +227,6 @@ class TestCase(unittest.TestCase):
             (_UnexpectedSuccess, self._report_unexpected_success),
             (Exception, self._report_error),
             ]
-        if sys.version_info < (2, 6):
-            # Catch old-style string exceptions with None as the instance
-            self.exception_handlers.append((type(None), self._report_error))
 
     def __eq__(self, other):
         eq = getattr(unittest.TestCase, '__eq__', None)
@@ -318,9 +349,9 @@ class TestCase(unittest.TestCase):
 
     failUnlessEqual = assertEquals = assertEqual
 
-    def assertIn(self, needle, haystack):
+    def assertIn(self, needle, haystack, message=''):
         """Assert that needle is in haystack."""
-        self.assertThat(haystack, Contains(needle))
+        self.assertThat(haystack, Contains(needle), message)
 
     def assertIsNone(self, observed, message=''):
         """Assert that 'observed' is equal to None.
@@ -355,10 +386,10 @@ class TestCase(unittest.TestCase):
         matcher = Not(Is(expected))
         self.assertThat(observed, matcher, message)
 
-    def assertNotIn(self, needle, haystack):
+    def assertNotIn(self, needle, haystack, message=''):
         """Assert that needle is not in haystack."""
         matcher = Not(Contains(needle))
-        self.assertThat(haystack, matcher)
+        self.assertThat(haystack, matcher, message)
 
     def assertIsInstance(self, obj, klass, msg=None):
         if isinstance(klass, tuple):
@@ -397,19 +428,62 @@ class TestCase(unittest.TestCase):
         :param matcher: An object meeting the testtools.Matcher protocol.
         :raises MismatchError: When matcher does not match thing.
         """
+        mismatch_error = self._matchHelper(matchee, matcher, message, verbose)
+        if mismatch_error is not None:
+            raise mismatch_error
+
+    def addDetailUniqueName(self, name, content_object):
+        """Add a detail to the test, but ensure it's name is unique.
+
+        This method checks whether ``name`` conflicts with a detail that has
+        already been added to the test. If it does, it will modify ``name`` to
+        avoid the conflict.
+
+        For more details see pydoc testtools.TestResult.
+
+        :param name: The name to give this detail.
+        :param content_object: The content object for this detail. See
+            testtools.content for more detail.
+        """
+        existing_details = self.getDetails()
+        full_name = name
+        suffix = 1
+        while full_name in existing_details:
+            full_name = "%s-%d" % (name, suffix)
+            suffix += 1
+        self.addDetail(full_name, content_object)
+
+    def expectThat(self, matchee, matcher, message='', verbose=False):
+        """Check that matchee is matched by matcher, but delay the assertion failure.
+
+        This method behaves similarly to ``assertThat``, except that a failed
+        match does not exit the test immediately. The rest of the test code will
+        continue to run, and the test will be marked as failing after the test
+        has finished.
+
+        :param matchee: An object to match with matcher.
+        :param matcher: An object meeting the testtools.Matcher protocol.
+        :param message: If specified, show this message with any failed match.
+        """
+        mismatch_error = self._matchHelper(matchee, matcher, message, verbose)
+
+        if mismatch_error is not None:
+            self.addDetailUniqueName(
+                "Failed expectation",
+                content.StacktraceContent(
+                    postfix_content="MismatchError: " + str(mismatch_error)
+                )
+            )
+            self.force_failure = True
+
+    def _matchHelper(self, matchee, matcher, message, verbose):
         matcher = Annotate.if_message(message, matcher)
         mismatch = matcher.match(matchee)
         if not mismatch:
             return
-        existing_details = self.getDetails()
-        for (name, content) in mismatch.get_details().items():
-            full_name = name
-            suffix = 1
-            while full_name in existing_details:
-                full_name = "%s-%d" % (name, suffix)
-                suffix += 1
-            self.addDetail(full_name, content)
-        raise MismatchError(matchee, matcher, mismatch, verbose)
+        for (name, value) in mismatch.get_details().items():
+            self.addDetailUniqueName(name, value)
+        return MismatchError(matchee, matcher, mismatch, verbose)
 
     def defaultTestResult(self):
         return TestResult()
@@ -505,9 +579,12 @@ class TestCase(unittest.TestCase):
     def _report_traceback(self, exc_info, tb_label='traceback'):
         id_gen = self._traceback_id_gens.setdefault(
             tb_label, itertools.count(0))
-        tb_id = advance_iterator(id_gen)
-        if tb_id:
-            tb_label = '%s-%d' % (tb_label, tb_id)
+        while True:
+            tb_id = advance_iterator(id_gen)
+            if tb_id:
+                tb_label = '%s-%d' % (tb_label, tb_id)
+            if tb_label not in self.getDetails():
+                break
         self.addDetail(tb_label, content.TracebackContent(exc_info, self))
 
     @staticmethod
@@ -515,7 +592,14 @@ class TestCase(unittest.TestCase):
         result.addUnexpectedSuccess(self, details=self.getDetails())
 
     def run(self, result=None):
-        return self.__RunTest(self, self.exception_handlers).run(result)
+        try:
+            run_test = self.__RunTest(
+                self, self.exception_handlers, last_resort=self._report_error)
+        except TypeError:
+            # Backwards compat: if we can't call the constructor
+            # with last_resort, try without that.
+            run_test = self.__RunTest(self, self.exception_handlers)
+        return run_test.run(result)
 
     def _run_setup(self, result):
         """Run the setUp function for this test.
@@ -527,10 +611,12 @@ class TestCase(unittest.TestCase):
         ret = self.setUp()
         if not self.__setup_called:
             raise ValueError(
+                "In File: %s\n"
                 "TestCase.setUp was not called. Have you upcalled all the "
                 "way up the hierarchy from your setUp? e.g. Call "
                 "super(%s, self).setUp() from your setUp()."
-                % self.__class__.__name__)
+                % (sys.modules[self.__class__.__module__].__file__,
+                   self.__class__.__name__))
         return ret
 
     def _run_teardown(self, result):
@@ -543,19 +629,16 @@ class TestCase(unittest.TestCase):
         ret = self.tearDown()
         if not self.__teardown_called:
             raise ValueError(
+                "In File: %s\n"
                 "TestCase.tearDown was not called. Have you upcalled all the "
                 "way up the hierarchy from your tearDown? e.g. Call "
                 "super(%s, self).tearDown() from your tearDown()."
-                % self.__class__.__name__)
+                % (sys.modules[self.__class__.__module__].__file__,
+                   self.__class__.__name__))
         return ret
 
     def _get_test_method(self):
-        absent_attr = object()
-        # Python 2.5+
-        method_name = getattr(self, '_testMethodName', absent_attr)
-        if method_name is absent_attr:
-            # Python 2.4
-            method_name = getattr(self, '_TestCase__testMethodName')
+        method_name = getattr(self, '_testMethodName')
         return getattr(self, method_name)
 
     def _run_test_method(self, result):
@@ -578,8 +661,18 @@ class TestCase(unittest.TestCase):
         try:
             fixture.setUp()
         except:
-            gather_details(fixture.getDetails(), self.getDetails())
-            raise
+            exc_info = sys.exc_info()
+            try:
+                gather_details(fixture.getDetails(), self.getDetails())
+            except:
+                # Report the setUp exception, then raise the error during
+                # gather_details.
+                self._report_traceback(exc_info)
+                raise
+            else:
+                # Gather_details worked, so raise the exception setUp
+                # encountered.
+                reraise(*exc_info)
         else:
             self.addCleanup(fixture.cleanUp)
             self.addCleanup(
@@ -588,11 +681,24 @@ class TestCase(unittest.TestCase):
 
     def setUp(self):
         super(TestCase, self).setUp()
+        if self.__setup_called:
+            raise ValueError(
+                "In File: %s\n"
+                "TestCase.setUp was already called. Do not explicitly call "
+                "setUp from your tests. In your own setUp, use super to call "
+                "the base setUp."
+                % (sys.modules[self.__class__.__module__].__file__,))
         self.__setup_called = True
 
     def tearDown(self):
         super(TestCase, self).tearDown()
-        unittest.TestCase.tearDown(self)
+        if self.__teardown_called:
+            raise ValueError(
+                "In File: %s\n"
+                "TestCase.tearDown was already called. Do not explicitly call "
+                "tearDown from your tests. In your own tearDown, use super to "
+                "call the base tearDown."
+                % (sys.modules[self.__class__.__module__].__file__,))
         self.__teardown_called = True
 
 
@@ -606,7 +712,7 @@ class PlaceHolder(object):
     failureException = None
 
     def __init__(self, test_id, short_description=None, details=None,
-        outcome='addSuccess', error=None):
+        outcome='addSuccess', error=None, tags=None, timestamps=(None, None)):
         """Construct a `PlaceHolder`.
 
         :param test_id: The id of the placeholder test.
@@ -614,6 +720,9 @@ class PlaceHolder(object):
             test. If not provided, the id will be used instead.
         :param details: Outcome details as accepted by addSuccess etc.
         :param outcome: The outcome to call. Defaults to 'addSuccess'.
+        :param tags: Tags to report for the test.
+        :param timestamps: A two-tuple of timestamps for the test start and
+            finish. Each timestamp may be None to indicate it is not known.
         """
         self._test_id = test_id
         self._short_description = short_description
@@ -621,6 +730,9 @@ class PlaceHolder(object):
         self._outcome = outcome
         if error is not None:
             self._details['traceback'] = content.TracebackContent(error, self)
+        tags = tags or frozenset()
+        self._tags = frozenset(tags)
+        self._timestamps = timestamps
 
     def __call__(self, result=None):
         return self.run(result=result)
@@ -654,10 +766,16 @@ class PlaceHolder(object):
 
     def run(self, result=None):
         result = self._result(result)
+        if self._timestamps[0] is not None:
+            result.time(self._timestamps[0])
+        result.tags(self._tags, set())
         result.startTest(self)
+        if self._timestamps[1] is not None:
+            result.time(self._timestamps[1])
         outcome = getattr(result, self._outcome)
         outcome(self, details=self._details)
         result.stopTest(self)
+        result.tags(set(), self._tags)
 
     def shortDescription(self):
         if self._short_description is None:
@@ -680,9 +798,19 @@ def ErrorHolder(test_id, error, short_description=None, details=None):
         details=details, outcome='addError', error=error)
 
 
-# Python 2.4 did not know how to copy functions.
-if types.FunctionType not in copy._copy_dispatch:
-    copy._copy_dispatch[types.FunctionType] = copy._copy_immutable
+def _clone_test_id_callback(test, callback):
+    """Copy a `TestCase`, and make it call callback for its id().
+
+    This is only expected to be used on tests that have been constructed but
+    not executed.
+
+    :param test: A TestCase instance.
+    :param callback: A callable that takes no parameters and returns a string.
+    :return: A copy.copy of the test with id=callback.
+    """
+    newTest = copy.copy(test)
+    newTest.id = callback
+    return newTest
 
 
 def clone_test_with_new_id(test, new_id):
@@ -691,9 +819,45 @@ def clone_test_with_new_id(test, new_id):
     This is only expected to be used on tests that have been constructed but
     not executed.
     """
-    newTest = copy.copy(test)
-    newTest.id = lambda: new_id
-    return newTest
+    return _clone_test_id_callback(test, lambda: new_id)
+
+
+def attr(*args):
+    """Decorator for adding attributes to WithAttributes.
+
+    :param args: The name of attributes to add.
+    :return: A callable that when applied to a WithAttributes will
+        alter its id to enumerate the added attributes.
+    """
+    def decorate(fn):
+        if not safe_hasattr(fn, '__testtools_attrs'):
+            fn.__testtools_attrs = set()
+        fn.__testtools_attrs.update(args)
+        return fn
+    return decorate
+
+
+class WithAttributes(object):
+    """A mix-in class for modifying test id by attributes.
+
+    e.g.
+    >>> class MyTest(WithAttributes, TestCase):
+    ...    @attr('foo')
+    ...    def test_bar(self):
+    ...        pass
+    >>> MyTest('test_bar').id()
+    testtools.testcase.MyTest/test_bar[foo]
+    """
+
+    def id(self):
+        orig = super(WithAttributes, self).id()
+        # Depends on testtools.TestCase._get_test_method, be nice to support
+        # plain unittest.
+        fn = self._get_test_method()
+        attributes = getattr(fn, '__testtools_attrs', None)
+        if not attributes:
+            return orig
+        return orig + '[' + ','.join(sorted(attributes)) + ']'
 
 
 def skip(reason):
@@ -704,6 +868,12 @@ def skip(reason):
     @unittest.skip decorator.
     """
     def decorator(test_item):
+        # This attribute signals to RunTest._run_core that the entire test
+        # must be skipped - including setUp and tearDown. This makes us
+        # compatible with testtools.skip* functions, which set the same
+        # attributes.
+        test_item.__unittest_skip__ = True
+        test_item.__unittest_skip_why__ = reason
         if wraps is not None:
             @wraps(test_item)
             def skip_wrapper(*args, **kwargs):
@@ -716,7 +886,7 @@ def skip(reason):
 
 
 def skipIf(condition, reason):
-    """Skip a test if the condition is true."""
+    """A decorator to skip a test if the condition is true."""
     if condition:
         return skip(reason)
     def _id(obj):
@@ -725,7 +895,7 @@ def skipIf(condition, reason):
 
 
 def skipUnless(condition, reason):
-    """Skip a test unless the condition is true."""
+    """A decorator to skip a test unless the condition is true."""
     if not condition:
         return skip(reason)
     def _id(obj):
@@ -736,8 +906,6 @@ def skipUnless(condition, reason):
 class ExpectedException:
     """A context manager to handle expected exceptions.
 
-    In Python 2.5 or later::
-
       def test_foo(self):
           with ExpectedException(ValueError, 'fo.*'):
               raise ValueError('foo')
@@ -748,26 +916,33 @@ class ExpectedException:
     exception is raised, an AssertionError will be raised.
     """
 
-    def __init__(self, exc_type, value_re=None):
+    def __init__(self, exc_type, value_re=None, msg=None):
         """Construct an `ExpectedException`.
 
         :param exc_type: The type of exception to expect.
         :param value_re: A regular expression to match against the
             'str()' of the raised exception.
+        :param msg: An optional message explaining the failure.
         """
         self.exc_type = exc_type
         self.value_re = value_re
+        self.msg = msg
 
     def __enter__(self):
         pass
 
     def __exit__(self, exc_type, exc_value, traceback):
         if exc_type is None:
-            raise AssertionError('%s not raised.' % self.exc_type.__name__)
+            error_msg = '%s not raised.' % self.exc_type.__name__
+            if self.msg:
+                error_msg = error_msg + ' : ' + self.msg
+            raise AssertionError(error_msg)
         if exc_type != self.exc_type:
             return False
         if self.value_re:
             matcher = MatchesException(self.exc_type, self.value_re)
+            if self.msg:
+                matcher = Annotate(self.msg, matcher)
             mismatch = matcher.match((exc_type, exc_value, traceback))
             if mismatch:
                 raise AssertionError(mismatch.describe())
@@ -793,6 +968,55 @@ class Nullary(object):
         return repr(self._callable_object)
 
 
+class DecorateTestCaseResult(object):
+    """Decorate a TestCase and permit customisation of the result for runs."""
+
+    def __init__(self, case, callout, before_run=None, after_run=None):
+        """Construct a DecorateTestCaseResult.
+
+        :param case: The case to decorate.
+        :param callout: A callback to call when run/__call__/debug is called.
+            Must take a result parameter and return a result object to be used.
+            For instance: lambda result: result.
+        :param before_run: If set, call this with the decorated result before
+            calling into the decorated run/__call__ method.
+        :param before_run: If set, call this with the decorated result after
+            calling into the decorated run/__call__ method.
+        """
+        self.decorated = case
+        self.callout = callout
+        self.before_run = before_run
+        self.after_run = after_run
+
+    def _run(self, result, run_method):
+        result = self.callout(result)
+        if self.before_run:
+            self.before_run(result)
+        try:
+            return run_method(result)
+        finally:
+            if self.after_run:
+                self.after_run(result)
+
+    def run(self, result=None):
+        self._run(result, self.decorated.run)
+
+    def __call__(self, result=None):
+        self._run(result, self.decorated)
+
+    def __getattr__(self, name):
+        return getattr(self.decorated, name)
+
+    def __delattr__(self, name):
+        delattr(self.decorated, name)
+
+    def __setattr__(self, name, value):
+        if name in ('decorated', 'callout', 'before_run', 'after_run'):
+            self.__dict__[name] = value
+            return
+        setattr(self.decorated, name, value)
+
+
 # Signal that this is part of the testing framework, and that code from this
 # should not normally appear in tracebacks.
 __unittest = True
diff --git a/lib/testtools/testtools/testresult/__init__.py b/lib/testtools/testtools/testresult/__init__.py
index d37a772..5bf8f9c 100644
--- a/lib/testtools/testtools/testresult/__init__.py
+++ b/lib/testtools/testtools/testresult/__init__.py
@@ -3,23 +3,47 @@
 """Test result objects."""
 
 __all__ = [
+    'CopyStreamResult',
     'ExtendedToOriginalDecorator',
+    'ExtendedToStreamDecorator',
     'MultiTestResult',
+    'StreamFailFast',
+    'StreamResult',
+    'StreamResultRouter',
+    'StreamSummary',
+    'StreamTagger',
+    'StreamToDict',
+    'StreamToExtendedDecorator',
+    'StreamToQueue',
     'Tagger',
     'TestByTestResult',
+    'TestControl',
     'TestResult',
     'TestResultDecorator',
     'TextTestResult',
     'ThreadsafeForwardingResult',
+    'TimestampingStreamResult',
     ]
 
 from testtools.testresult.real import (
+    CopyStreamResult,
     ExtendedToOriginalDecorator,
+    ExtendedToStreamDecorator,
     MultiTestResult,
+    StreamFailFast,
+    StreamResult,
+    StreamResultRouter,
+    StreamSummary,
+    StreamTagger,
+    StreamToDict,
+    StreamToExtendedDecorator,
+    StreamToQueue,
     Tagger,
     TestByTestResult,
+    TestControl,
     TestResult,
     TestResultDecorator,
     TextTestResult,
     ThreadsafeForwardingResult,
+    TimestampingStreamResult,
     )
diff --git a/lib/testtools/testtools/testresult/doubles.py b/lib/testtools/testtools/testresult/doubles.py
index 1865e93..d86f7fa 100644
--- a/lib/testtools/testtools/testresult/doubles.py
+++ b/lib/testtools/testtools/testresult/doubles.py
@@ -6,6 +6,7 @@ __all__ = [
     'Python26TestResult',
     'Python27TestResult',
     'ExtendedTestResult',
+    'StreamResult',
     ]
 
 
@@ -148,3 +149,26 @@ class ExtendedTestResult(Python27TestResult):
 
     def wasSuccessful(self):
         return self._was_successful
+
+
+class StreamResult(object):
+    """A StreamResult implementation for testing.
+
+    All events are logged to _events.
+    """
+
+    def __init__(self):
+        self._events = []
+
+    def startTestRun(self):
+        self._events.append(('startTestRun',))
+
+    def stopTestRun(self):
+        self._events.append(('stopTestRun',))
+
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        self._events.append(('status', test_id, test_status, test_tags,
+            runnable, file_name, file_bytes, eof, mime_type, route_code,
+            timestamp))
diff --git a/lib/testtools/testtools/testresult/real.py b/lib/testtools/testtools/testresult/real.py
index 0a69872..1453041 100644
--- a/lib/testtools/testtools/testresult/real.py
+++ b/lib/testtools/testtools/testresult/real.py
@@ -5,24 +5,43 @@
 __metaclass__ = type
 __all__ = [
     'ExtendedToOriginalDecorator',
+    'ExtendedToStreamDecorator',
     'MultiTestResult',
+    'StreamFailFast',
+    'StreamResult',
+    'StreamSummary',
+    'StreamTagger',
+    'StreamToDict',
+    'StreamToExtendedDecorator',
+    'StreamToQueue',
     'Tagger',
+    'TestControl',
     'TestResult',
     'TestResultDecorator',
     'ThreadsafeForwardingResult',
+    'TimestampingStreamResult',
     ]
 
 import datetime
+from operator import methodcaller
 import sys
 import unittest
 
-from testtools.compat import all, str_is_unicode, _u
+from extras import safe_hasattr, try_import, try_imports
+parse_mime_type = try_import('mimeparse.parse_mime_type')
+Queue = try_imports(['Queue.Queue', 'queue.Queue'])
+
+from testtools.compat import str_is_unicode, _u, _b
 from testtools.content import (
+    Content,
     text_content,
     TracebackContent,
     )
-from testtools.helpers import safe_hasattr
+from testtools.content_type import ContentType
 from testtools.tags import TagContext
+# circular import
+# from testtools.testcase import PlaceHolder
+PlaceHolder = None
 
 # From http://docs.python.org/library/datetime.html
 _ZERO = datetime.timedelta(0)
@@ -243,6 +262,531 @@ class TestResult(unittest.TestResult):
         """
 
 
+class StreamResult(object):
+    """A test result for reporting the activity of a test run.
+
+    Typical use
+
+      >>> result = StreamResult()
+      >>> result.startTestRun()
+      >>> try:
+      ...     case.run(result)
+      ... finally:
+      ...     result.stopTestRun()
+
+    The case object will be either a TestCase or a TestSuite, and
+    generally make a sequence of calls like::
+
+      >>> result.status(self.id(), 'inprogress')
+      >>> result.status(self.id(), 'success')
+
+    General concepts
+
+    StreamResult is built to process events that are emitted by tests during a
+    test run or test enumeration. The test run may be running concurrently, and
+    even be spread out across multiple machines.
+
+    All events are timestamped to prevent network buffering or scheduling
+    latency causing false timing reports. Timestamps are datetime objects in
+    the UTC timezone.
+
+    A route_code is a unicode string that identifies where a particular test
+    run. This is optional in the API but very useful when multiplexing multiple
+    streams together as it allows identification of interactions between tests
+    that were run on the same hardware or in the same test process. Generally
+    actual tests never need to bother with this - it is added and processed
+    by StreamResult's that do multiplexing / run analysis. route_codes are
+    also used to route stdin back to pdb instances.
+
+    The StreamResult base class does no accounting or processing, rather it
+    just provides an empty implementation of every method, suitable for use
+    as a base class regardless of intent.
+    """
+
+    def startTestRun(self):
+        """Start a test run.
+
+        This will prepare the test result to process results (which might imply
+        connecting to a database or remote machine).
+        """
+
+    def stopTestRun(self):
+        """Stop a test run.
+
+        This informs the result that no more test updates will be received. At
+        this point any test ids that have started and not completed can be
+        considered failed-or-hung.
+        """
+
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        """Inform the result about a test status.
+
+        :param test_id: The test whose status is being reported. None to
+            report status about the test run as a whole.
+        :param test_status: The status for the test. There are two sorts of
+            status - interim and final status events. As many interim events
+            can be generated as desired, but only one final event. After a
+            final status event any further file or status events from the
+            same test_id+route_code may be discarded or associated with a new
+            test by the StreamResult. (But no exception will be thrown).
+
+            Interim states:
+              * None - no particular status is being reported, or status being
+                reported is not associated with a test (e.g. when reporting on
+                stdout / stderr chatter).
+              * inprogress - the test is currently running. Emitted by tests when
+                they start running and at any intermediary point they might
+                choose to indicate their continual operation.
+
+            Final states:
+              * exists - the test exists. This is used when a test is not being
+                executed. Typically this is when querying what tests could be run
+                in a test run (which is useful for selecting tests to run).
+              * xfail - the test failed but that was expected. This is purely
+                informative - the test is not considered to be a failure.
+              * uxsuccess - the test passed but was expected to fail. The test
+                will be considered a failure.
+              * success - the test has finished without error.
+              * fail - the test failed (or errored). The test will be considered
+                a failure.
+              * skip - the test was selected to run but chose to be skipped. E.g.
+                a test dependency was missing. This is purely informative - the
+                test is not considered to be a failure.
+
+        :param test_tags: Optional set of tags to apply to the test. Tags
+            have no intrinsic meaning - that is up to the test author.
+        :param runnable: Allows status reports to mark that they are for
+            tests which are not able to be explicitly run. For instance,
+            subtests will report themselves as non-runnable.
+        :param file_name: The name for the file_bytes. Any unicode string may
+            be used. While there is no semantic value attached to the name
+            of any attachment, the names 'stdout' and 'stderr' and 'traceback'
+            are recommended for use only for output sent to stdout, stderr and
+            tracebacks of exceptions. When file_name is supplied, file_bytes
+            must be a bytes instance.
+        :param file_bytes: A bytes object containing content for the named
+            file. This can just be a single chunk of the file - emitting
+            another file event with more later. Must be None unleses a
+            file_name is supplied.
+        :param eof: True if this chunk is the last chunk of the file, any
+            additional chunks with the same name should be treated as an error
+            and discarded. Ignored unless file_name has been supplied.
+        :param mime_type: An optional MIME type for the file. stdout and
+            stderr will generally be "text/plain; charset=utf8". If None,
+            defaults to application/octet-stream. Ignored unless file_name
+            has been supplied.
+        """
+
+
+def domap(*args, **kwargs):
+    return list(map(*args, **kwargs))
+
+
+class CopyStreamResult(StreamResult):
+    """Copies all event it receives to multiple results.
+
+    This provides an easy facility for combining multiple StreamResults.
+
+    For TestResult the equivalent class was ``MultiTestResult``.
+    """
+
+    def __init__(self, targets):
+        super(CopyStreamResult, self).__init__()
+        self.targets = targets
+
+    def startTestRun(self):
+        super(CopyStreamResult, self).startTestRun()
+        domap(methodcaller('startTestRun'), self.targets)
+
+    def stopTestRun(self):
+        super(CopyStreamResult, self).stopTestRun()
+        domap(methodcaller('stopTestRun'), self.targets)
+
+    def status(self, *args, **kwargs):
+        super(CopyStreamResult, self).status(*args, **kwargs)
+        domap(methodcaller('status', *args, **kwargs), self.targets)
+
+
+class StreamFailFast(StreamResult):
+    """Call the supplied callback if an error is seen in a stream.
+
+    An example callback::
+
+       def do_something():
+           pass
+    """
+
+    def __init__(self, on_error):
+        self.on_error = on_error
+
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        if test_status in ('uxsuccess', 'fail'):
+            self.on_error()
+
+
+class StreamResultRouter(StreamResult):
+    """A StreamResult that routes events.
+
+    StreamResultRouter forwards received events to another StreamResult object,
+    selected by a dynamic forwarding policy. Events where no destination is
+    found are forwarded to the fallback StreamResult, or an error is raised.
+
+    Typical use is to construct a router with a fallback and then either
+    create up front mapping rules, or create them as-needed from the fallback
+    handler::
+
+      >>> router = StreamResultRouter()
+      >>> sink = doubles.StreamResult()
+      >>> router.add_rule(sink, 'route_code_prefix', route_prefix='0',
+      ...     consume_route=True)
+      >>> router.status(test_id='foo', route_code='0/1', test_status='uxsuccess')
+
+    StreamResultRouter has no buffering.
+
+    When adding routes (and for the fallback) whether to call startTestRun and
+    stopTestRun or to not call them is controllable by passing
+    'do_start_stop_run'. The default is to call them for the fallback only.
+    If a route is added after startTestRun has been called, and
+    do_start_stop_run is True then startTestRun is called immediately on the
+    new route sink.
+
+    There is no a-priori defined lookup order for routes: if they are ambiguous
+    the behaviour is undefined. Only a single route is chosen for any event.
+    """
+
+    _policies = {}
+
+    def __init__(self, fallback=None, do_start_stop_run=True):
+        """Construct a StreamResultRouter with optional fallback.
+
+        :param fallback: A StreamResult to forward events to when no route
+            exists for them.
+        :param do_start_stop_run: If False do not pass startTestRun and
+            stopTestRun onto the fallback.
+        """
+        self.fallback = fallback
+        self._route_code_prefixes = {}
+        self._test_ids = {}
+        # Records sinks that should have do_start_stop_run called on them.
+        self._sinks = []
+        if do_start_stop_run and fallback:
+            self._sinks.append(fallback)
+        self._in_run = False
+
+    def startTestRun(self):
+        super(StreamResultRouter, self).startTestRun()
+        for sink in self._sinks:
+            sink.startTestRun()
+        self._in_run = True
+
+    def stopTestRun(self):
+        super(StreamResultRouter, self).stopTestRun()
+        for sink in self._sinks:
+            sink.stopTestRun()
+        self._in_run = False
+
+    def status(self, **kwargs):
+        route_code = kwargs.get('route_code', None)
+        test_id = kwargs.get('test_id', None)
+        if route_code is not None:
+            prefix = route_code.split('/')[0]
+        else:
+            prefix = route_code
+        if prefix in self._route_code_prefixes:
+            target, consume_route = self._route_code_prefixes[prefix]
+            if route_code is not None and consume_route:
+                route_code = route_code[len(prefix) + 1:]
+                if not route_code:
+                    route_code = None
+                kwargs['route_code'] = route_code
+        elif test_id in self._test_ids:
+            target = self._test_ids[test_id]
+        else:
+            target = self.fallback
+        target.status(**kwargs)
+
+    def add_rule(self, sink, policy, do_start_stop_run=False, **policy_args):
+        """Add a rule to route events to sink when they match a given policy.
+
+        :param sink: A StreamResult to receive events.
+        :param policy: A routing policy. Valid policies are
+            'route_code_prefix' and 'test_id'.
+        :param do_start_stop_run: If True then startTestRun and stopTestRun
+            events will be passed onto this sink.
+
+        :raises: ValueError if the policy is unknown
+        :raises: TypeError if the policy is given arguments it cannot handle.
+
+        ``route_code_prefix`` routes events based on a prefix of the route
+        code in the event. It takes a ``route_prefix`` argument to match on
+        (e.g. '0') and a ``consume_route`` argument, which, if True, removes
+        the prefix from the ``route_code`` when forwarding events.
+
+        ``test_id`` routes events based on the test id.  It takes a single
+        argument, ``test_id``.  Use ``None`` to select non-test events.
+        """
+        policy_method = StreamResultRouter._policies.get(policy, None)
+        if not policy_method:
+            raise ValueError("bad policy %r" % (policy,))
+        policy_method(self, sink, **policy_args)
+        if do_start_stop_run:
+            self._sinks.append(sink)
+        if self._in_run:
+            sink.startTestRun()
+
+    def _map_route_code_prefix(self, sink, route_prefix, consume_route=False):
+        if '/' in route_prefix:
+            raise TypeError(
+                "%r is more than one route step long" % (route_prefix,))
+        self._route_code_prefixes[route_prefix] = (sink, consume_route)
+    _policies['route_code_prefix'] = _map_route_code_prefix
+
+    def _map_test_id(self, sink, test_id):
+        self._test_ids[test_id] = sink
+    _policies['test_id'] = _map_test_id
+
+
+class StreamTagger(CopyStreamResult):
+    """Adds or discards tags from StreamResult events."""
+
+    def __init__(self, targets, add=None, discard=None):
+        """Create a StreamTagger.
+
+        :param targets: A list of targets to forward events onto.
+        :param add: Either None or an iterable of tags to add to each event.
+        :param discard: Either None or an iterable of tags to discard from each
+            event.
+        """
+        super(StreamTagger, self).__init__(targets)
+        self.add = frozenset(add or ())
+        self.discard = frozenset(discard or ())
+
+    def status(self, *args, **kwargs):
+        test_tags = kwargs.get('test_tags') or set()
+        test_tags.update(self.add)
+        test_tags.difference_update(self.discard)
+        kwargs['test_tags'] = test_tags or None
+        super(StreamTagger, self).status(*args, **kwargs)
+
+
+class StreamToDict(StreamResult):
+    """A specialised StreamResult that emits a callback as tests complete.
+
+    Top level file attachments are simply discarded. Hung tests are detected
+    by stopTestRun and notified there and then.
+
+    The callback is passed a dict with the following keys:
+
+      * id: the test id.
+      * tags: The tags for the test. A set of unicode strings.
+      * details: A dict of file attachments - ``testtools.content.Content``
+        objects.
+      * status: One of the StreamResult status codes (including inprogress) or
+        'unknown' (used if only file events for a test were received...)
+      * timestamps: A pair of timestamps - the first one received with this
+        test id, and the one in the event that triggered the notification.
+        Hung tests have a None for the second end event. Timestamps are not
+        compared - their ordering is purely order received in the stream.
+
+    Only the most recent tags observed in the stream are reported.
+    """
+
+    def __init__(self, on_test):
+        """Create a StreamToDict calling on_test on test completions.
+
+        :param on_test: A callback that accepts one parameter - a dict
+            describing a test.
+        """
+        super(StreamToDict, self).__init__()
+        self.on_test = on_test
+        if parse_mime_type is None:
+            raise ImportError("mimeparse module missing.")
+
+    def startTestRun(self):
+        super(StreamToDict, self).startTestRun()
+        self._inprogress = {}
+
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        super(StreamToDict, self).status(test_id, test_status,
+            test_tags=test_tags, runnable=runnable, file_name=file_name,
+            file_bytes=file_bytes, eof=eof, mime_type=mime_type,
+            route_code=route_code, timestamp=timestamp)
+        key = self._ensure_key(test_id, route_code, timestamp)
+        # update fields
+        if not key:
+            return
+        if test_status is not None:
+            self._inprogress[key]['status'] = test_status
+        self._inprogress[key]['timestamps'][1] = timestamp
+        case = self._inprogress[key]
+        if file_name is not None:
+            if file_name not in case['details']:
+                if mime_type is None:
+                    mime_type = 'application/octet-stream'
+                primary, sub, parameters = parse_mime_type(mime_type)
+                if 'charset' in parameters:
+                    if ',' in parameters['charset']:
+                        # testtools was emitting a bad encoding, workaround it,
+                        # Though this does lose data - probably want to drop
+                        # this in a few releases.
+                        parameters['charset'] = parameters['charset'][
+                            :parameters['charset'].find(',')]
+                content_type = ContentType(primary, sub, parameters)
+                content_bytes = []
+                case['details'][file_name] = Content(
+                    content_type, lambda:content_bytes)
+            case['details'][file_name].iter_bytes().append(file_bytes)
+        if test_tags is not None:
+            self._inprogress[key]['tags'] = test_tags
+        # notify completed tests.
+        if test_status not in (None, 'inprogress'):
+            self.on_test(self._inprogress.pop(key))
+
+    def stopTestRun(self):
+        super(StreamToDict, self).stopTestRun()
+        while self._inprogress:
+            case = self._inprogress.popitem()[1]
+            case['timestamps'][1] = None
+            self.on_test(case)
+
+    def _ensure_key(self, test_id, route_code, timestamp):
+        if test_id is None:
+            return
+        key = (test_id, route_code)
+        if key not in self._inprogress:
+            self._inprogress[key] = {
+                'id': test_id,
+                'tags': set(),
+                'details': {},
+                'status': 'unknown',
+                'timestamps': [timestamp, None]}
+        return key
+
+
+_status_map = {
+    'inprogress': 'addFailure',
+    'unknown': 'addFailure',
+    'success': 'addSuccess',
+    'skip': 'addSkip',
+    'fail': 'addFailure',
+    'xfail': 'addExpectedFailure',
+    'uxsuccess': 'addUnexpectedSuccess',
+    }
+
+
+def test_dict_to_case(test_dict):
+    """Convert a test dict into a TestCase object.
+
+    :param test_dict: A test dict as generated by StreamToDict.
+    :return: A PlaceHolder test object.
+    """
+    # Circular import.
+    global PlaceHolder
+    if PlaceHolder is None:
+        from testtools.testcase import PlaceHolder
+    outcome = _status_map[test_dict['status']]
+    return PlaceHolder(test_dict['id'], outcome=outcome,
+        details=test_dict['details'], tags=test_dict['tags'],
+        timestamps=test_dict['timestamps'])
+
+
+class StreamSummary(StreamToDict):
+    """A specialised StreamResult that summarises a stream.
+
+    The summary uses the same representation as the original
+    unittest.TestResult contract, allowing it to be consumed by any test
+    runner.
+    """
+
+    def __init__(self):
+        super(StreamSummary, self).__init__(self._gather_test)
+        self._handle_status = {
+            'success': self._success,
+            'skip': self._skip,
+            'exists': self._exists,
+            'fail': self._fail,
+            'xfail': self._xfail,
+            'uxsuccess': self._uxsuccess,
+            'unknown': self._incomplete,
+            'inprogress': self._incomplete,
+            }
+
+    def startTestRun(self):
+        super(StreamSummary, self).startTestRun()
+        self.failures = []
+        self.errors = []
+        self.testsRun = 0
+        self.skipped = []
+        self.expectedFailures = []
+        self.unexpectedSuccesses = []
+
+    def wasSuccessful(self):
+        """Return False if any failure has occured.
+
+        Note that incomplete tests can only be detected when stopTestRun is
+        called, so that should be called before checking wasSuccessful.
+        """
+        return (not self.failures and not self.errors)
+
+    def _gather_test(self, test_dict):
+        if test_dict['status'] == 'exists':
+            return
+        self.testsRun += 1
+        case = test_dict_to_case(test_dict)
+        self._handle_status[test_dict['status']](case)
+
+    def _incomplete(self, case):
+        self.errors.append((case, "Test did not complete"))
+
+    def _success(self, case):
+        pass
+
+    def _skip(self, case):
+        if 'reason' not in case._details:
+            reason = "Unknown"
+        else:
+            reason = case._details['reason'].as_text()
+        self.skipped.append((case, reason))
+
+    def _exists(self, case):
+        pass
+
+    def _fail(self, case):
+        message = _details_to_str(case._details, special="traceback")
+        self.errors.append((case, message))
+
+    def _xfail(self, case):
+        message = _details_to_str(case._details, special="traceback")
+        self.expectedFailures.append((case, message))
+
+    def _uxsuccess(self, case):
+        case._outcome = 'addUnexpectedSuccess'
+        self.unexpectedSuccesses.append(case)
+
+
+class TestControl(object):
+    """Controls a running test run, allowing it to be interrupted.
+
+    :ivar shouldStop: If True, tests should not run and should instead
+        return immediately. Similarly a TestSuite should check this between
+        each test and if set stop dispatching any new tests and return.
+    """
+
+    def __init__(self):
+        super(TestControl, self).__init__()
+        self.shouldStop = False
+
+    def stop(self):
+        """Indicate that tests should stop running."""
+        self.shouldStop = True
+
+
 class MultiTestResult(TestResult):
     """A test result that dispatches to many test results."""
 
@@ -737,6 +1281,241 @@ class ExtendedToOriginalDecorator(object):
         return self.decorated.wasSuccessful()
 
 
+class ExtendedToStreamDecorator(CopyStreamResult, StreamSummary, TestControl):
+    """Permit using old TestResult API code with new StreamResult objects.
+
+    This decorates a StreamResult and converts old (Python 2.6 / 2.7 /
+    Extended) TestResult API calls into StreamResult calls.
+
+    It also supports regular StreamResult calls, making it safe to wrap around
+    any StreamResult.
+    """
+
+    def __init__(self, decorated):
+        super(ExtendedToStreamDecorator, self).__init__([decorated])
+        # Deal with mismatched base class constructors.
+        TestControl.__init__(self)
+        self._started = False
+
+    def _get_failfast(self):
+        return len(self.targets) == 2
+    def _set_failfast(self, value):
+        if value:
+            if len(self.targets) == 2:
+                return
+            self.targets.append(StreamFailFast(self.stop))
+        else:
+            del self.targets[1:]
+    failfast = property(_get_failfast, _set_failfast)
+
+    def startTest(self, test):
+        if not self._started:
+            self.startTestRun()
+        self.status(test_id=test.id(), test_status='inprogress', timestamp=self._now())
+        self._tags = TagContext(self._tags)
+
+    def stopTest(self, test):
+        self._tags = self._tags.parent
+
+    def addError(self, test, err=None, details=None):
+        self._check_args(err, details)
+        self._convert(test, err, details, 'fail')
+    addFailure = addError
+
+    def _convert(self, test, err, details, status, reason=None):
+        if not self._started:
+            self.startTestRun()
+        test_id = test.id()
+        now = self._now()
+        if err is not None:
+            if details is None:
+                details = {}
+            details['traceback'] = TracebackContent(err, test)
+        if details is not None:
+            for name, content in details.items():
+                mime_type = repr(content.content_type)
+                file_bytes = None
+                for next_bytes in content.iter_bytes():
+                    if file_bytes is not None:
+                        self.status(file_name=name, file_bytes=file_bytes,
+                            mime_type=mime_type, test_id=test_id, timestamp=now)
+                    file_bytes = next_bytes
+                self.status(file_name=name, file_bytes=file_bytes, eof=True,
+                    mime_type=mime_type, test_id=test_id, timestamp=now)
+        if reason is not None:
+            self.status(file_name='reason', file_bytes=reason.encode('utf8'),
+                eof=True, mime_type="text/plain; charset=utf8",
+                test_id=test_id, timestamp=now)
+        self.status(test_id=test_id, test_status=status,
+            test_tags=self.current_tags, timestamp=now)
+
+    def addExpectedFailure(self, test, err=None, details=None):
+        self._check_args(err, details)
+        self._convert(test, err, details, 'xfail')
+
+    def addSkip(self, test, reason=None, details=None):
+        self._convert(test, None, details, 'skip', reason)
+
+    def addUnexpectedSuccess(self, test, details=None):
+        self._convert(test, None, details, 'uxsuccess')
+
+    def addSuccess(self, test, details=None):
+        self._convert(test, None, details, 'success')
+
+    def _check_args(self, err, details):
+        param_count = 0
+        if err is not None:
+            param_count += 1
+        if details is not None:
+            param_count += 1
+        if param_count != 1:
+            raise ValueError("Must pass only one of err '%s' and details '%s"
+                % (err, details))
+
+    def startTestRun(self):
+        super(ExtendedToStreamDecorator, self).startTestRun()
+        self._tags = TagContext()
+        self.shouldStop = False
+        self.__now = None
+        self._started = True
+
+    def stopTest(self, test):
+        self._tags = self._tags.parent
+
+    @property
+    def current_tags(self):
+        """The currently set tags."""
+        return self._tags.get_current_tags()
+
+    def tags(self, new_tags, gone_tags):
+        """Add and remove tags from the test.
+
+        :param new_tags: A set of tags to be added to the stream.
+        :param gone_tags: A set of tags to be removed from the stream.
+        """
+        self._tags.change_tags(new_tags, gone_tags)
+
+    def _now(self):
+        """Return the current 'test time'.
+
+        If the time() method has not been called, this is equivalent to
+        datetime.now(), otherwise its the last supplied datestamp given to the
+        time() method.
+        """
+        if self.__now is None:
+            return datetime.datetime.now(utc)
+        else:
+            return self.__now
+
+    def time(self, a_datetime):
+        self.__now = a_datetime
+
+    def wasSuccessful(self):
+        if not self._started:
+            self.startTestRun()
+        return super(ExtendedToStreamDecorator, self).wasSuccessful()
+
+
+class StreamToExtendedDecorator(StreamResult):
+    """Convert StreamResult API calls into ExtendedTestResult calls.
+
+    This will buffer all calls for all concurrently active tests, and
+    then flush each test as they complete.
+
+    Incomplete tests will be flushed as errors when the test run stops.
+
+    Non test file attachments are accumulated into a test called
+    'testtools.extradata' flushed at the end of the run.
+    """
+
+    def __init__(self, decorated):
+        # ExtendedToOriginalDecorator takes care of thunking details back to
+        # exceptions/reasons etc.
+        self.decorated = ExtendedToOriginalDecorator(decorated)
+        # StreamToDict buffers and gives us individual tests.
+        self.hook = StreamToDict(self._handle_tests)
+
+    def status(self, test_id=None, test_status=None, *args, **kwargs):
+        if test_status == 'exists':
+            return
+        self.hook.status(
+            test_id=test_id, test_status=test_status, *args, **kwargs)
+
+    def startTestRun(self):
+        self.decorated.startTestRun()
+        self.hook.startTestRun()
+
+    def stopTestRun(self):
+        self.hook.stopTestRun()
+        self.decorated.stopTestRun()
+
+    def _handle_tests(self, test_dict):
+        case = test_dict_to_case(test_dict)
+        case.run(self.decorated)
+
+
+class StreamToQueue(StreamResult):
+    """A StreamResult which enqueues events as a dict to a queue.Queue.
+
+    Events have their route code updated to include the route code
+    StreamToQueue was constructed with before they are submitted. If the event
+    route code is None, it is replaced with the StreamToQueue route code,
+    otherwise it is prefixed with the supplied code + a hyphen.
+
+    startTestRun and stopTestRun are forwarded to the queue. Implementors that
+    dequeue events back into StreamResult calls should take care not to call
+    startTestRun / stopTestRun on other StreamResult objects multiple times
+    (e.g. by filtering startTestRun and stopTestRun).
+
+    ``StreamToQueue`` is typically used by
+    ``ConcurrentStreamTestSuite``, which creates one ``StreamToQueue``
+    per thread, forwards status events to the the StreamResult that
+    ``ConcurrentStreamTestSuite.run()`` was called with, and uses the
+    stopTestRun event to trigger calling join() on the each thread.
+
+    Unlike ThreadsafeForwardingResult which this supercedes, no buffering takes
+    place - any event supplied to a StreamToQueue will be inserted into the
+    queue immediately.
+
+    Events are forwarded as a dict with a key ``event`` which is one of
+    ``startTestRun``, ``stopTestRun`` or ``status``. When ``event`` is
+    ``status`` the dict also has keys matching the keyword arguments
+    of ``StreamResult.status``, otherwise it has one other key ``result`` which
+    is the result that invoked ``startTestRun``.
+    """
+
+    def __init__(self, queue, routing_code):
+        """Create a StreamToQueue forwarding to target.
+
+        :param queue: A ``queue.Queue`` to receive events.
+        :param routing_code: The routing code to apply to messages.
+        """
+        super(StreamToQueue, self).__init__()
+        self.queue = queue
+        self.routing_code = routing_code
+
+    def startTestRun(self):
+        self.queue.put(dict(event='startTestRun', result=self))
+
+    def status(self, test_id=None, test_status=None, test_tags=None,
+        runnable=True, file_name=None, file_bytes=None, eof=False,
+        mime_type=None, route_code=None, timestamp=None):
+        self.queue.put(dict(event='status', test_id=test_id,
+            test_status=test_status, test_tags=test_tags, runnable=runnable,
+            file_name=file_name, file_bytes=file_bytes, eof=eof,
+            mime_type=mime_type, route_code=self.route_code(route_code),
+            timestamp=timestamp))
+
+    def stopTestRun(self):
+        self.queue.put(dict(event='stopTestRun', result=self))
+
+    def route_code(self, route_code):
+        """Adjust route_code on the way through."""
+        if route_code is None:
+            return self.routing_code
+        return self.routing_code + _u("/") + route_code
+
+
 class TestResultDecorator(object):
     """General pass-through decorator.
 
@@ -901,6 +1680,23 @@ class TestByTestResult(TestResult):
         self._details = details
 
 
+class TimestampingStreamResult(CopyStreamResult):
+    """A StreamResult decorator that assigns a timestamp when none is present.
+
+    This is convenient for ensuring events are timestamped.
+    """
+
+    def __init__(self, target):
+        super(TimestampingStreamResult, self).__init__([target])
+
+    def status(self, *args, **kwargs):
+        timestamp = kwargs.pop('timestamp', None)
+        if timestamp is None:
+            timestamp = datetime.datetime.now(utc)
+        super(TimestampingStreamResult, self).status(
+            *args, timestamp=timestamp, **kwargs)
+
+
 class _StringException(Exception):
     """An exception made from an arbitrary string."""
 
diff --git a/lib/testtools/testtools/tests/__init__.py b/lib/testtools/testtools/tests/__init__.py
index df9d44b..5e18e07 100644
--- a/lib/testtools/testtools/tests/__init__.py
+++ b/lib/testtools/testtools/tests/__init__.py
@@ -1,6 +1,7 @@
+# Copyright (c) 2008-2013 testtools developers. See LICENSE for details.
+
 """Tests for testtools itself."""
 
-# See README for copyright and licensing details.
 
 from unittest import TestSuite
 
@@ -8,6 +9,7 @@ from unittest import TestSuite
 def test_suite():
     from testtools.tests import (
         matchers,
+        test_assert_that,
         test_compat,
         test_content,
         test_content_type,
@@ -23,9 +25,11 @@ def test_suite():
         test_testcase,
         test_testresult,
         test_testsuite,
+        test_with_with,
         )
     modules = [
         matchers,
+        test_assert_that,
         test_compat,
         test_content,
         test_content_type,
@@ -41,6 +45,7 @@ def test_suite():
         test_testcase,
         test_testresult,
         test_testsuite,
+        test_with_with,
         ]
     suites = map(lambda x: x.test_suite(), modules)
     return TestSuite(suites)
diff --git a/lib/testtools/testtools/tests/helpers.py b/lib/testtools/testtools/tests/helpers.py
index ade2d96..f766da3 100644
--- a/lib/testtools/testtools/tests/helpers.py
+++ b/lib/testtools/testtools/tests/helpers.py
@@ -8,11 +8,10 @@ __all__ = [
 
 import sys
 
+from extras import safe_hasattr
+
 from testtools import TestResult
-from testtools.helpers import (
-    safe_hasattr,
-    )
-from testtools.content import TracebackContent
+from testtools.content import StackLinesContent
 from testtools import runtest
 
 
@@ -84,12 +83,12 @@ class LoggingResult(TestResult):
 
 
 def is_stack_hidden():
-    return TracebackContent.HIDE_INTERNAL_STACK
+    return StackLinesContent.HIDE_INTERNAL_STACK
 
 
 def hide_testtools_stack(should_hide=True):
-    result = TracebackContent.HIDE_INTERNAL_STACK
-    TracebackContent.HIDE_INTERNAL_STACK = should_hide
+    result = StackLinesContent.HIDE_INTERNAL_STACK
+    StackLinesContent.HIDE_INTERNAL_STACK = should_hide
     return result
 
 
diff --git a/lib/testtools/testtools/tests/matchers/test_basic.py b/lib/testtools/testtools/tests/matchers/test_basic.py
index 1109fa4..c53bc9e 100644
--- a/lib/testtools/testtools/tests/matchers/test_basic.py
+++ b/lib/testtools/testtools/tests/matchers/test_basic.py
@@ -19,6 +19,7 @@ from testtools.matchers._basic import (
     IsInstance,
     LessThan,
     GreaterThan,
+    HasLength,
     MatchesRegex,
     NotEquals,
     SameMembers,
@@ -35,8 +36,14 @@ class Test_BinaryMismatch(TestCase):
     _long_b = _b(_long_string)
     _long_u = _u(_long_string)
 
+    class CustomRepr(object):
+        def __init__(self, repr_string):
+            self._repr_string = repr_string
+        def __repr__(self):
+            return _u('<object ') + _u(self._repr_string) + _u('>')
+
     def test_short_objects(self):
-        o1, o2 = object(), object()
+        o1, o2 = self.CustomRepr('a'), self.CustomRepr('b')
         mismatch = _BinaryMismatch(o1, "!~", o2)
         self.assertEqual(mismatch.describe(), "%r !~ %r" % (o1, o2))
 
@@ -369,6 +376,21 @@ class TestMatchesRegex(TestCase, TestMatchersInterface):
         ]
 
 
+class TestHasLength(TestCase, TestMatchersInterface):
+
+    matches_matcher = HasLength(2)
+    matches_matches = [[1, 2]]
+    matches_mismatches = [[], [1], [3, 2, 1]]
+
+    str_examples = [
+        ("HasLength(2)", HasLength(2)),
+        ]
+
+    describe_examples = [
+        ("len([]) != 1", [], HasLength(1)),
+        ]
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)
diff --git a/lib/testtools/testtools/tests/matchers/test_dict.py b/lib/testtools/testtools/tests/matchers/test_dict.py
index c6e2c9c..00368dd 100644
--- a/lib/testtools/testtools/tests/matchers/test_dict.py
+++ b/lib/testtools/testtools/tests/matchers/test_dict.py
@@ -30,7 +30,7 @@ class TestMatchesAllDictInterface(TestCase, TestMatchersInterface):
         ]
 
 
-class TestKeysEqual(TestCase, TestMatchersInterface):
+class TestKeysEqualWithList(TestCase, TestMatchersInterface):
 
     matches_matcher = KeysEqual('foo', 'bar')
     matches_matches = [
@@ -60,6 +60,11 @@ class TestKeysEqual(TestCase, TestMatchersInterface):
                 % (matchee,)))
 
 
+class TestKeysEqualWithDict(TestKeysEqualWithList):
+
+    matches_matcher = KeysEqual({'foo': 3, 'bar': 4})
+
+
 class TestSubDictOf(TestCase, TestMatchersInterface):
 
     matches_matcher = _SubDictOf({'foo': 'bar', 'baz': 'qux'})
diff --git a/lib/testtools/testtools/tests/matchers/test_exception.py b/lib/testtools/testtools/tests/matchers/test_exception.py
index ef7185f..a74043a 100644
--- a/lib/testtools/testtools/tests/matchers/test_exception.py
+++ b/lib/testtools/testtools/tests/matchers/test_exception.py
@@ -163,12 +163,7 @@ class TestRaisesBaseTypes(TestCase):
         # Exception, it is propogated.
         match_keyb = Raises(MatchesException(KeyboardInterrupt))
         def raise_keyb_from_match():
-            if sys.version_info > (2, 5):
-                matcher = Raises(MatchesException(Exception))
-            else:
-                # On Python 2.4 KeyboardInterrupt is a StandardError subclass
-                # but should propogate from less generic exception matchers
-                matcher = Raises(MatchesException(EnvironmentError))
+            matcher = Raises(MatchesException(Exception))
             matcher.match(self.raiser)
         self.assertThat(raise_keyb_from_match, match_keyb)
 
diff --git a/lib/testtools/testtools/tests/matchers/test_higherorder.py b/lib/testtools/testtools/tests/matchers/test_higherorder.py
index c5cc44e..fb86b7f 100644
--- a/lib/testtools/testtools/tests/matchers/test_higherorder.py
+++ b/lib/testtools/testtools/tests/matchers/test_higherorder.py
@@ -18,6 +18,7 @@ from testtools.matchers._higherorder import (
     MatchesAny,
     MatchesAll,
     MatchesPredicate,
+    MatchesPredicateWithParams,
     Not,
     )
 from testtools.tests.helpers import FullStackRunTest
@@ -222,6 +223,32 @@ class TestMatchesPredicate(TestCase, TestMatchersInterface):
         ]
 
 
+def between(x, low, high):
+    return low < x < high
+
+
+class TestMatchesPredicateWithParams(TestCase, TestMatchersInterface):
+
+    matches_matcher = MatchesPredicateWithParams(
+        between, "{0} is not between {1} and {2}")(1, 9)
+    matches_matches = [2, 4, 6, 8]
+    matches_mismatches = [0, 1, 9, 10]
+
+    str_examples = [
+        ("MatchesPredicateWithParams(%r, %r)(%s)" % (
+            between, "{0} is not between {1} and {2}", "1, 2"),
+         MatchesPredicateWithParams(
+            between, "{0} is not between {1} and {2}")(1, 2)),
+        ("Between(1, 2)", MatchesPredicateWithParams(
+            between, "{0} is not between {1} and {2}", "Between")(1, 2)),
+        ]
+
+    describe_examples = [
+        ('1 is not between 2 and 3', 1, MatchesPredicateWithParams(
+            between, "{0} is not between {1} and {2}")(2, 3)),
+        ]
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)
diff --git a/lib/testtools/testtools/tests/test_compat.py b/lib/testtools/testtools/tests/test_compat.py
index b29dc34..84e57be 100644
--- a/lib/testtools/testtools/tests/test_compat.py
+++ b/lib/testtools/testtools/tests/test_compat.py
@@ -2,6 +2,7 @@
 
 """Tests for miscellaneous compatibility functions"""
 
+import io
 import linecache
 import os
 import sys
@@ -13,6 +14,9 @@ import testtools
 from testtools.compat import (
     _b,
     _detect_encoding,
+    _format_exc_info,
+    _format_exception_only,
+    _format_stack_list,
     _get_source_encoding,
     _u,
     reraise,
@@ -21,6 +25,9 @@ from testtools.compat import (
     unicode_output_stream,
     )
 from testtools.matchers import (
+    Equals,
+    Is,
+    IsInstance,
     MatchesException,
     Not,
     Raises,
@@ -106,7 +113,8 @@ class TestDetectEncoding(testtools.TestCase):
             '\xef\xbb\xbfThose should be latin-1 bytes"""\n'))
         self._check_encoding("utf-8", (
             "\xef\xbb\xbf# Is the coding: utf-8 or coding: euc-jp instead?\n",
-            '"""Module docstring say \xe2\x98\x86"""\n'))
+            '"""Module docstring say \xe2\x98\x86"""\n'),
+            possibly_invalid=True)
 
     def test_multiple_coding_comments(self):
         """Test only the first of multiple coding declarations counts"""
@@ -256,12 +264,30 @@ class TestUnicodeOutputStream(testtools.TestCase):
             newio = True
         sout = StringIO()
         soutwrapper = unicode_output_stream(sout)
-        if newio:
-            self.expectFailure("Python 3 StringIO expects text not bytes",
-                self.assertThat, lambda: soutwrapper.write(self.uni),
-                Not(Raises(MatchesException(TypeError))))
         soutwrapper.write(self.uni)
-        self.assertEqual("pa???n", sout.getvalue())
+        if newio:
+            self.assertEqual(self.uni, sout.getvalue())
+        else:
+            self.assertEqual("pa???n", sout.getvalue())
+
+    def test_io_stringio(self):
+        # io.StringIO only accepts unicode so should be returned as itself.
+        s = io.StringIO()
+        self.assertEqual(s, unicode_output_stream(s))
+
+    def test_io_bytesio(self):
+        # io.BytesIO only accepts bytes so should be wrapped.
+        bytes_io = io.BytesIO()
+        self.assertThat(bytes_io, Not(Is(unicode_output_stream(bytes_io))))
+        # Will error if s was not wrapped properly.
+        unicode_output_stream(bytes_io).write(_u('foo'))
+
+    def test_io_textwrapper(self):
+        # textwrapper is unicode, should be returned as itself.
+        text_io = io.TextIOWrapper(io.BytesIO())
+        self.assertThat(unicode_output_stream(text_io), Is(text_io))
+        # To be sure...
+        unicode_output_stream(text_io).write(_u('foo'))
 
 
 class TestTextRepr(testtools.TestCase):
@@ -427,6 +453,151 @@ class TestReraise(testtools.TestCase):
         self.assertRaises(CustomException, reraise, *_exc_info)
 
 
+class Python2CompatibilityTests(testtools.TestCase):
+
+    def setUp(self):
+        super(Python2CompatibilityTests, self).setUp()
+        if sys.version[0] >= '3':
+            self.skip("These tests are only applicable to python 2.")
+
+
+class TestExceptionFormatting(Python2CompatibilityTests):
+    """Test the _format_exception_only function."""
+
+    def _assert_exception_format(self, eclass, evalue, expected):
+        actual = _format_exception_only(eclass, evalue)
+        self.assertThat(actual, Equals(expected))
+        self.assertThat(''.join(actual), IsInstance(unicode))
+
+    def test_supports_string_exception(self):
+        self._assert_exception_format(
+            "String_Exception",
+            None,
+            [_u("String_Exception\n")]
+        )
+
+    def test_supports_regular_exception(self):
+        self._assert_exception_format(
+            RuntimeError,
+            RuntimeError("Something went wrong"),
+            [_u("RuntimeError: Something went wrong\n")]
+        )
+
+    def test_supports_unprintable_exceptions(self):
+        """Verify support for exception classes that raise an exception when
+        __unicode__ or __str__ is called.
+        """
+        class UnprintableException(Exception):
+
+            def __str__(self):
+                raise Exception()
+
+            def __unicode__(self):
+                raise Exception()
+
+        self._assert_exception_format(
+            UnprintableException,
+            UnprintableException("Foo"),
+            [_u("UnprintableException: <unprintable UnprintableException object>\n")]
+        )
+
+    def test_supports_exceptions_with_no_string_value(self):
+        class NoStringException(Exception):
+
+            def __str__(self):
+                return ""
+
+            def __unicode__(self):
+                return _u("")
+
+        self._assert_exception_format(
+            NoStringException,
+            NoStringException("Foo"),
+            [_u("NoStringException\n")]
+        )
+
+    def test_supports_strange_syntax_error(self):
+        """Test support for syntax errors with unusual number of arguments"""
+        self._assert_exception_format(
+            SyntaxError,
+            SyntaxError("Message"),
+            [_u("SyntaxError: Message\n")]
+        )
+
+    def test_supports_syntax_error(self):
+        self._assert_exception_format(
+            SyntaxError,
+            SyntaxError(
+                "Some Syntax Message",
+                (
+                    "/path/to/file",
+                    12,
+                    2,
+                    "This is the line of code",
+                )
+            ),
+            [
+                _u('  File "/path/to/file", line 12\n'),
+                _u('    This is the line of code\n'),
+                _u('     ^\n'),
+                _u('SyntaxError: Some Syntax Message\n'),
+            ]
+        )
+
+
+class StackListFormattingTests(Python2CompatibilityTests):
+    """Test the _format_stack_list function."""
+
+    def _assert_stack_format(self, stack_lines, expected_output):
+        actual = _format_stack_list(stack_lines)
+        self.assertThat(actual, Equals([expected_output]))
+
+    def test_single_complete_stack_line(self):
+        stack_lines = [(
+            '/path/to/filename',
+            12,
+            'func_name',
+            'some_code()',
+        )]
+        expected = \
+            _u('  File "/path/to/filename", line 12, in func_name\n' \
+               '    some_code()\n')
+
+        self._assert_stack_format(stack_lines, expected)
+
+    def test_single_stack_line_no_code(self):
+        stack_lines = [(
+            '/path/to/filename',
+            12,
+            'func_name',
+            None
+        )]
+        expected = _u('  File "/path/to/filename", line 12, in func_name\n')
+        self._assert_stack_format(stack_lines, expected)
+
+
+class FormatExceptionInfoTests(Python2CompatibilityTests):
+
+    def test_individual_functions_called(self):
+        self.patch(
+            testtools.compat,
+            '_format_stack_list',
+            lambda stack_list: [_u("format stack list called\n")]
+        )
+        self.patch(
+            testtools.compat,
+            '_format_exception_only',
+            lambda etype, evalue: [_u("format exception only called\n")]
+        )
+        result = _format_exc_info(None, None, None)
+        expected = [
+            _u("Traceback (most recent call last):\n"),
+            _u("format stack list called\n"),
+            _u("format exception only called\n"),
+        ]
+        self.assertThat(expected, Equals(result))
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)
diff --git a/lib/testtools/testtools/tests/test_content.py b/lib/testtools/testtools/tests/test_content.py
index bc72513..09feebd 100644
--- a/lib/testtools/testtools/tests/test_content.py
+++ b/lib/testtools/testtools/tests/test_content.py
@@ -5,12 +5,13 @@ import os
 import tempfile
 import unittest
 
-from testtools import TestCase
+from testtools import TestCase, skipUnless
 from testtools.compat import (
     _b,
     _u,
     BytesIO,
     StringIO,
+    str_is_unicode,
     )
 from testtools.content import (
     attach_file,
@@ -19,6 +20,8 @@ from testtools.content import (
     content_from_stream,
     JSON,
     json_content,
+    StackLinesContent,
+    StacktraceContent,
     TracebackContent,
     text_content,
     )
@@ -188,12 +191,71 @@ class TestContent(TestCase):
         expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
         self.assertEqual(expected, text_content(data))
 
+    @skipUnless(str_is_unicode, "Test only applies in python 3.")
+    def test_text_content_raises_TypeError_when_passed_bytes(self):
+        data = _b("Some Bytes")
+        self.assertRaises(TypeError, text_content, data)
+
+    def test_text_content_raises_TypeError_when_passed_non_text(self):
+        bad_values = (None, list(), dict(), 42, 1.23)
+        for value in bad_values:
+            self.assertThat(
+                lambda: text_content(value),
+                raises(
+                    TypeError("text_content must be given text, not '%s'." %
+                        type(value).__name__)
+                ),
+            )
+
     def test_json_content(self):
         data = {'foo': 'bar'}
         expected = Content(JSON, lambda: [_b('{"foo": "bar"}')])
         self.assertEqual(expected, json_content(data))
 
 
+class TestStackLinesContent(TestCase):
+
+    def _get_stack_line_and_expected_output(self):
+        stack_lines = [
+            ('/path/to/file', 42, 'some_function', 'print("Hello World")'),
+        ]
+        expected = '  File "/path/to/file", line 42, in some_function\n' \
+                   '    print("Hello World")\n'
+        return stack_lines, expected
+
+    def test_single_stack_line(self):
+        stack_lines, expected = self._get_stack_line_and_expected_output()
+        actual = StackLinesContent(stack_lines).as_text()
+
+        self.assertEqual(expected, actual)
+
+    def test_prefix_content(self):
+        stack_lines, expected = self._get_stack_line_and_expected_output()
+        prefix = self.getUniqueString() + '\n'
+        content = StackLinesContent(stack_lines, prefix_content=prefix)
+        actual = content.as_text()
+        expected = prefix  + expected
+
+        self.assertEqual(expected, actual)
+
+    def test_postfix_content(self):
+        stack_lines, expected = self._get_stack_line_and_expected_output()
+        postfix = '\n' + self.getUniqueString()
+        content = StackLinesContent(stack_lines, postfix_content=postfix)
+        actual = content.as_text()
+        expected = expected + postfix
+
+        self.assertEqual(expected, actual)
+
+    def test___init___sets_content_type(self):
+        stack_lines, expected = self._get_stack_line_and_expected_output()
+        content = StackLinesContent(stack_lines)
+        expected_content_type = ContentType("text", "x-traceback",
+            {"language": "python", "charset": "utf8"})
+
+        self.assertEqual(expected_content_type, content.content_type)
+
+
 class TestTracebackContent(TestCase):
 
     def test___init___None_errors(self):
@@ -210,6 +272,33 @@ class TestTracebackContent(TestCase):
         self.assertEqual(expected, ''.join(list(content.iter_text())))
 
 
+class TestStacktraceContent(TestCase):
+
+    def test___init___sets_ivars(self):
+        content = StacktraceContent()
+        content_type = ContentType("text", "x-traceback",
+            {"language": "python", "charset": "utf8"})
+
+        self.assertEqual(content_type, content.content_type)
+
+    def test_prefix_is_used(self):
+        prefix = self.getUniqueString()
+        actual = StacktraceContent(prefix_content=prefix).as_text()
+
+        self.assertTrue(actual.startswith(prefix))
+
+    def test_postfix_is_used(self):
+        postfix = self.getUniqueString()
+        actual = StacktraceContent(postfix_content=postfix).as_text()
+
+        self.assertTrue(actual.endswith(postfix))
+
+    def test_top_frame_is_skipped_when_no_stack_is_specified(self):
+        actual = StacktraceContent().as_text()
+
+        self.assertTrue('testtools/content.py' not in actual)
+
+
 class TestAttachFile(TestCase):
 
     def make_file(self, data):
diff --git a/lib/testtools/testtools/tests/test_content_type.py b/lib/testtools/testtools/tests/test_content_type.py
index ecb8e3a..2d34f95 100644
--- a/lib/testtools/testtools/tests/test_content_type.py
+++ b/lib/testtools/testtools/tests/test_content_type.py
@@ -43,7 +43,7 @@ class TestContentType(TestCase):
         content_type = ContentType(
             'text', 'plain', {'foo': 'bar', 'baz': 'qux'})
         self.assertThat(
-            repr(content_type), Equals('text/plain; baz="qux", foo="bar"'))
+            repr(content_type), Equals('text/plain; baz="qux"; foo="bar"'))
 
 
 class TestBuiltinContentTypes(TestCase):
diff --git a/lib/testtools/testtools/tests/test_deferredruntest.py b/lib/testtools/testtools/tests/test_deferredruntest.py
index 3373c06..997f6c8 100644
--- a/lib/testtools/testtools/tests/test_deferredruntest.py
+++ b/lib/testtools/testtools/tests/test_deferredruntest.py
@@ -5,6 +5,8 @@
 import os
 import signal
 
+from extras import try_import
+
 from testtools import (
     skipIf,
     TestCase,
@@ -13,7 +15,6 @@ from testtools import (
 from testtools.content import (
     text_content,
     )
-from testtools.helpers import try_import
 from testtools.matchers import (
     Equals,
     KeysEqual,
@@ -52,6 +53,12 @@ class X(object):
             self.calls.append('tearDown')
             super(X.Base, self).tearDown()
 
+    class BaseExceptionRaised(Base):
+        expected_calls = ['setUp', 'tearDown', 'clean-up']
+        expected_results = [('addError', SystemExit)]
+        def test_something(self):
+            raise SystemExit(0)
+
     class ErrorInSetup(Base):
         expected_calls = ['setUp', 'clean-up']
         expected_results = [('addError', RuntimeError)]
@@ -102,7 +109,10 @@ class X(object):
         def test_runner(self):
             result = ExtendedTestResult()
             test = self.test_factory('test_something', runTest=self.runner)
-            test.run(result)
+            if self.test_factory is X.BaseExceptionRaised:
+                self.assertRaises(SystemExit, test.run, result)
+            else:
+                test.run(result)
             self.assertEqual(test.calls, self.test_factory.expected_calls)
             self.assertResultsMatch(test, result)
 
@@ -117,6 +127,7 @@ def make_integration_tests():
         ]
 
     tests = [
+        X.BaseExceptionRaised,
         X.ErrorInSetup,
         X.ErrorInTest,
         X.ErrorInTearDown,
@@ -545,7 +556,7 @@ class TestAsynchronousDeferredRunTest(NeedsTwistedTestCase):
                 self.addCleanup(lambda: 3 / 0)
                 # Dirty the reactor.
                 from twisted.internet.protocol import ServerFactory
-                reactor.listenTCP(0, ServerFactory())
+                reactor.listenTCP(0, ServerFactory(), interface='127.0.0.1')
                 # Unhandled error.
                 defer.maybeDeferred(lambda: 2 / 0)
                 # Actual error.
@@ -760,7 +771,10 @@ class TestRunWithLogObservers(NeedsTwistedTestCase):
 
 
 def test_suite():
-    from unittest import TestLoader, TestSuite
-    return TestSuite(
-        [TestLoader().loadTestsFromName(__name__),
-         make_integration_tests()])
+    from unittest2 import TestLoader, TestSuite
+    return TestLoader().loadTestsFromName(__name__)
+
+
+def load_tests(loader, tests, pattern):
+    tests.addTest(make_integration_tests())
+    return tests
diff --git a/lib/testtools/testtools/tests/test_distutilscmd.py b/lib/testtools/testtools/tests/test_distutilscmd.py
index 59762df..fd0dd90 100644
--- a/lib/testtools/testtools/tests/test_distutilscmd.py
+++ b/lib/testtools/testtools/tests/test_distutilscmd.py
@@ -4,12 +4,13 @@
 
 from distutils.dist import Distribution
 
+from extras import try_import
+
 from testtools.compat import (
     _b,
     _u,
     BytesIO,
     )
-from testtools.helpers import try_import
 fixtures = try_import('fixtures')
 
 import testtools
@@ -60,8 +61,8 @@ class TestCommandTest(TestCase):
         dist.cmdclass = {'test': TestCommand}
         dist.command_options = {
             'test': {'test_module': ('command line', 'testtools.runexample')}}
-        cmd = dist.reinitialize_command('test')
         with fixtures.MonkeyPatch('sys.stdout', stdout.stream):
+            cmd = dist.reinitialize_command('test')
             dist.run_command('test')
         self.assertThat(
             stdout.getDetails()['stdout'].as_text(),
@@ -82,8 +83,8 @@ OK
             'test': {
                 'test_suite': (
                     'command line', 'testtools.runexample.test_suite')}}
-        cmd = dist.reinitialize_command('test')
         with fixtures.MonkeyPatch('sys.stdout', stdout.stream):
+            cmd = dist.reinitialize_command('test')
             dist.run_command('test')
         self.assertThat(
             stdout.getDetails()['stdout'].as_text(),
diff --git a/lib/testtools/testtools/tests/test_fixturesupport.py b/lib/testtools/testtools/tests/test_fixturesupport.py
index cff9eb4..e309045 100644
--- a/lib/testtools/testtools/tests/test_fixturesupport.py
+++ b/lib/testtools/testtools/tests/test_fixturesupport.py
@@ -2,13 +2,15 @@
 
 import unittest
 
+from extras import try_import
+
 from testtools import (
     TestCase,
     content,
     content_type,
     )
 from testtools.compat import _b, _u
-from testtools.helpers import try_import
+from testtools.matchers import Contains
 from testtools.testresult.doubles import (
     ExtendedTestResult,
     )
@@ -111,6 +113,32 @@ class TestFixtureSupport(TestCase):
         self.assertEqual(['content', 'traceback'], sorted(details))
         self.assertEqual('foobar', ''.join(details['content'].iter_text()))
 
+    def test_useFixture_original_exception_raised_if_gather_details_fails(self):
+        # In bug #1368440 it was reported that when a fixture fails setUp
+        # and gather_details errors on it, then the original exception that
+        # failed is not reported.
+        class BrokenFixture(fixtures.Fixture):
+            def getDetails(self):
+                raise AttributeError("getDetails broke")
+            def setUp(self):
+                fixtures.Fixture.setUp(self)
+                raise Exception("setUp broke")
+        fixture = BrokenFixture()
+        class SimpleTest(TestCase):
+            def test_foo(self):
+                self.useFixture(fixture)
+        result = ExtendedTestResult()
+        SimpleTest('test_foo').run(result)
+        self.assertEqual('addError', result._events[-2][0])
+        details = result._events[-2][2]
+        self.assertEqual(['traceback', 'traceback-1'], sorted(details))
+        self.assertThat(
+            ''.join(details['traceback'].iter_text()),
+            Contains('setUp broke'))
+        self.assertThat(
+            ''.join(details['traceback-1'].iter_text()),
+            Contains('getDetails broke'))
+
 
 def test_suite():
     from unittest import TestLoader
diff --git a/lib/testtools/testtools/tests/test_helpers.py b/lib/testtools/testtools/tests/test_helpers.py
index 98da534..848c2f0 100644
--- a/lib/testtools/testtools/tests/test_helpers.py
+++ b/lib/testtools/testtools/tests/test_helpers.py
@@ -1,196 +1,13 @@
 # Copyright (c) 2010-2012 testtools developers. See LICENSE for details.
 
 from testtools import TestCase
-from testtools.helpers import (
-    try_import,
-    try_imports,
-    )
-from testtools.matchers import (
-    Equals,
-    Is,
-    Not,
-    )
 from testtools.tests.helpers import (
     FullStackRunTest,
     hide_testtools_stack,
     is_stack_hidden,
-    safe_hasattr,
     )
 
 
-def check_error_callback(test, function, arg, expected_error_count,
-    expect_result):
-    """General test template for error_callback argument.
-
-    :param test: Test case instance.
-    :param function: Either try_import or try_imports.
-    :param arg: Name or names to import.
-    :param expected_error_count: Expected number of calls to the callback.
-    :param expect_result: Boolean for whether a module should
-        ultimately be returned or not.
-    """
-    cb_calls = []
-    def cb(e):
-        test.assertIsInstance(e, ImportError)
-        cb_calls.append(e)
-    try:
-        result = function(arg, error_callback=cb)
-    except ImportError:
-        test.assertFalse(expect_result)
-    else:
-        if expect_result:
-            test.assertThat(result, Not(Is(None)))
-        else:
-            test.assertThat(result, Is(None))
-    test.assertEquals(len(cb_calls), expected_error_count)
-
-
-class TestSafeHasattr(TestCase):
-
-    def test_attribute_not_there(self):
-        class Foo(object):
-            pass
-        self.assertEqual(False, safe_hasattr(Foo(), 'anything'))
-
-    def test_attribute_there(self):
-        class Foo(object):
-            pass
-        foo = Foo()
-        foo.attribute = None
-        self.assertEqual(True, safe_hasattr(foo, 'attribute'))
-
-    def test_property_there(self):
-        class Foo(object):
-            @property
-            def attribute(self):
-                return None
-        foo = Foo()
-        self.assertEqual(True, safe_hasattr(foo, 'attribute'))
-
-    def test_property_raises(self):
-        class Foo(object):
-            @property
-            def attribute(self):
-                1/0
-        foo = Foo()
-        self.assertRaises(ZeroDivisionError, safe_hasattr, foo, 'attribute')
-
-
-class TestTryImport(TestCase):
-
-    def test_doesnt_exist(self):
-        # try_import('thing', foo) returns foo if 'thing' doesn't exist.
-        marker = object()
-        result = try_import('doesntexist', marker)
-        self.assertThat(result, Is(marker))
-
-    def test_None_is_default_alternative(self):
-        # try_import('thing') returns None if 'thing' doesn't exist.
-        result = try_import('doesntexist')
-        self.assertThat(result, Is(None))
-
-    def test_existing_module(self):
-        # try_import('thing', foo) imports 'thing' and returns it if it's a
-        # module that exists.
-        result = try_import('os', object())
-        import os
-        self.assertThat(result, Is(os))
-
-    def test_existing_submodule(self):
-        # try_import('thing.another', foo) imports 'thing' and returns it if
-        # it's a module that exists.
-        result = try_import('os.path', object())
-        import os
-        self.assertThat(result, Is(os.path))
-
-    def test_nonexistent_submodule(self):
-        # try_import('thing.another', foo) imports 'thing' and returns foo if
-        # 'another' doesn't exist.
-        marker = object()
-        result = try_import('os.doesntexist', marker)
-        self.assertThat(result, Is(marker))
-
-    def test_object_from_module(self):
-        # try_import('thing.object') imports 'thing' and returns
-        # 'thing.object' if 'thing' is a module and 'object' is not.
-        result = try_import('os.path.join')
-        import os
-        self.assertThat(result, Is(os.path.join))
-
-    def test_error_callback(self):
-        # the error callback is called on failures.
-        check_error_callback(self, try_import, 'doesntexist', 1, False)
-
-    def test_error_callback_missing_module_member(self):
-        # the error callback is called on failures to find an object
-        # inside an existing module.
-        check_error_callback(self, try_import, 'os.nonexistent', 1, False)
-
-    def test_error_callback_not_on_success(self):
-        # the error callback is not called on success.
-        check_error_callback(self, try_import, 'os.path', 0, True)
-
-
-class TestTryImports(TestCase):
-
-    def test_doesnt_exist(self):
-        # try_imports('thing', foo) returns foo if 'thing' doesn't exist.
-        marker = object()
-        result = try_imports(['doesntexist'], marker)
-        self.assertThat(result, Is(marker))
-
-    def test_fallback(self):
-        result = try_imports(['doesntexist', 'os'])
-        import os
-        self.assertThat(result, Is(os))
-
-    def test_None_is_default_alternative(self):
-        # try_imports('thing') returns None if 'thing' doesn't exist.
-        e = self.assertRaises(
-            ImportError, try_imports, ['doesntexist', 'noreally'])
-        self.assertThat(
-            str(e),
-            Equals("Could not import any of: doesntexist, noreally"))
-
-    def test_existing_module(self):
-        # try_imports('thing', foo) imports 'thing' and returns it if it's a
-        # module that exists.
-        result = try_imports(['os'], object())
-        import os
-        self.assertThat(result, Is(os))
-
-    def test_existing_submodule(self):
-        # try_imports('thing.another', foo) imports 'thing' and returns it if
-        # it's a module that exists.
-        result = try_imports(['os.path'], object())
-        import os
-        self.assertThat(result, Is(os.path))
-
-    def test_nonexistent_submodule(self):
-        # try_imports('thing.another', foo) imports 'thing' and returns foo if
-        # 'another' doesn't exist.
-        marker = object()
-        result = try_imports(['os.doesntexist'], marker)
-        self.assertThat(result, Is(marker))
-
-    def test_fallback_submodule(self):
-        result = try_imports(['os.doesntexist', 'os.path'])
-        import os
-        self.assertThat(result, Is(os.path))
-
-    def test_error_callback(self):
-        # One error for every class that doesn't exist.
-        check_error_callback(self, try_imports,
-            ['os.doesntexist', 'os.notthiseither'],
-            2, False)
-        check_error_callback(self, try_imports,
-            ['os.doesntexist', 'os.notthiseither', 'os'],
-            2, True)
-        check_error_callback(self, try_imports,
-            ['os.path'],
-            0, True)
-
-
 class TestStackHiding(TestCase):
 
     run_tests_with = FullStackRunTest
diff --git a/lib/testtools/testtools/tests/test_run.py b/lib/testtools/testtools/tests/test_run.py
index 5971a4b..2babdf8 100644
--- a/lib/testtools/testtools/tests/test_run.py
+++ b/lib/testtools/testtools/tests/test_run.py
@@ -2,27 +2,41 @@
 
 """Tests for the test runner logic."""
 
+import doctest
 from unittest import TestSuite
+import sys
+from textwrap import dedent
 
+from extras import try_import
+fixtures = try_import('fixtures')
+testresources = try_import('testresources')
+import unittest2
+
+import testtools
+from testtools import TestCase, run, skipUnless
 from testtools.compat import (
     _b,
+    _u,
     StringIO,
     )
-from testtools.helpers import try_import
-fixtures = try_import('fixtures')
-
-import testtools
-from testtools import TestCase, run
-from testtools.matchers import Contains
+from testtools.matchers import (
+    Contains,
+    DocTestMatches,
+    MatchesRegex,
+    )
 
 
 if fixtures:
     class SampleTestFixture(fixtures.Fixture):
         """Creates testtools.runexample temporarily."""
 
-        def __init__(self):
-            self.package = fixtures.PythonPackage(
-            'runexample', [('__init__.py', _b("""
+        def __init__(self, broken=False):
+            """Create a SampleTestFixture.
+
+            :param broken: If True, the sample file will not be importable.
+            """
+            if not broken:
+                init_contents = _b("""\
 from testtools import TestCase
 
 class TestFoo(TestCase):
@@ -33,13 +47,90 @@ class TestFoo(TestCase):
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)
-"""))])
+""")
+            else:
+                init_contents = b"class not in\n"
+            self.package = fixtures.PythonPackage(
+            'runexample', [('__init__.py', init_contents)])
 
         def setUp(self):
             super(SampleTestFixture, self).setUp()
             self.useFixture(self.package)
             testtools.__path__.append(self.package.base)
             self.addCleanup(testtools.__path__.remove, self.package.base)
+            self.addCleanup(sys.modules.pop, 'testtools.runexample', None)
+
+
+if fixtures and testresources:
+    class SampleResourcedFixture(fixtures.Fixture):
+        """Creates a test suite that uses testresources."""
+
+        def __init__(self):
+            super(SampleResourcedFixture, self).__init__()
+            self.package = fixtures.PythonPackage(
+            'resourceexample', [('__init__.py', _b("""
+from fixtures import Fixture
+from testresources import (
+    FixtureResource,
+    OptimisingTestSuite,
+    ResourcedTestCase,
+    )
+from testtools import TestCase
+
+class Printer(Fixture):
+
+    def setUp(self):
+        super(Printer, self).setUp()
+        print('Setting up Printer')
+
+    def reset(self):
+        pass
+
+class TestFoo(TestCase, ResourcedTestCase):
+    # When run, this will print just one Setting up Printer, unless the
+    # OptimisingTestSuite is not honoured, when one per test case will print.
+    resources=[('res', FixtureResource(Printer()))]
+    def test_bar(self):
+        pass
+    def test_foo(self):
+        pass
+    def test_quux(self):
+        pass
+def test_suite():
+    from unittest import TestLoader
+    return OptimisingTestSuite(TestLoader().loadTestsFromName(__name__))
+"""))])
+
+        def setUp(self):
+            super(SampleResourcedFixture, self).setUp()
+            self.useFixture(self.package)
+            self.addCleanup(testtools.__path__.remove, self.package.base)
+            testtools.__path__.append(self.package.base)
+
+
+if fixtures:
+    class SampleLoadTestsPackage(fixtures.Fixture):
+        """Creates a test suite package using load_tests."""
+
+        def __init__(self):
+            super(SampleLoadTestsPackage, self).__init__()
+            self.package = fixtures.PythonPackage(
+            'discoverexample', [('__init__.py', _b("""
+from testtools import TestCase, clone_test_with_new_id
+
+class TestExample(TestCase):
+    def test_foo(self):
+        pass
+
+def load_tests(loader, tests, pattern):
+    tests.addTest(clone_test_with_new_id(tests._tests[1]._tests[0], "fred"))
+    return tests
+"""))])
+
+        def setUp(self):
+            super(SampleLoadTestsPackage, self).setUp()
+            self.useFixture(self.package)
+            self.addCleanup(sys.path.remove, self.package.base)
 
 
 class TestRun(TestCase):
@@ -49,14 +140,80 @@ class TestRun(TestCase):
         if fixtures is None:
             self.skipTest("Need fixtures")
 
+    def test_run_custom_list(self):
+        self.useFixture(SampleTestFixture())
+        tests = []
+        class CaptureList(run.TestToolsTestRunner):
+            def list(self, test):
+                tests.append(set([case.id() for case
+                    in testtools.testsuite.iterate_tests(test)]))
+        out = StringIO()
+        try:
+            program = run.TestProgram(
+                argv=['prog', '-l', 'testtools.runexample.test_suite'],
+                stdout=out, testRunner=CaptureList)
+        except SystemExit:
+            exc_info = sys.exc_info()
+            raise AssertionError("-l tried to exit. %r" % exc_info[1])
+        self.assertEqual([set(['testtools.runexample.TestFoo.test_bar',
+            'testtools.runexample.TestFoo.test_quux'])], tests)
+
+    def test_run_list_with_loader(self):
+        # list() is attempted with a loader first.
+        self.useFixture(SampleTestFixture())
+        tests = []
+        class CaptureList(run.TestToolsTestRunner):
+            def list(self, test, loader=None):
+                tests.append(set([case.id() for case
+                    in testtools.testsuite.iterate_tests(test)]))
+                tests.append(loader)
+        out = StringIO()
+        try:
+            program = run.TestProgram(
+                argv=['prog', '-l', 'testtools.runexample.test_suite'],
+                stdout=out, testRunner=CaptureList)
+        except SystemExit:
+            exc_info = sys.exc_info()
+            raise AssertionError("-l tried to exit. %r" % exc_info[1])
+        self.assertEqual([set(['testtools.runexample.TestFoo.test_bar',
+            'testtools.runexample.TestFoo.test_quux']), program.testLoader],
+            tests)
+
     def test_run_list(self):
         self.useFixture(SampleTestFixture())
         out = StringIO()
-        run.main(['prog', '-l', 'testtools.runexample.test_suite'], out)
+        try:
+            run.main(['prog', '-l', 'testtools.runexample.test_suite'], out)
+        except SystemExit:
+            exc_info = sys.exc_info()
+            raise AssertionError("-l tried to exit. %r" % exc_info[1])
         self.assertEqual("""testtools.runexample.TestFoo.test_bar
 testtools.runexample.TestFoo.test_quux
 """, out.getvalue())
 
+    def test_run_list_failed_import(self):
+        broken = self.useFixture(SampleTestFixture(broken=True))
+        out = StringIO()
+        # XXX: http://bugs.python.org/issue22811
+        unittest2.defaultTestLoader._top_level_dir = None
+        exc = self.assertRaises(
+            SystemExit,
+            run.main, ['prog', 'discover', '-l', broken.package.base, '*.py'], out)
+        self.assertEqual(2, exc.args[0])
+        self.assertThat(out.getvalue(), DocTestMatches("""\
+Failed to import test module: runexample
+Traceback (most recent call last):
+  File ".../loader.py", line ..., in _find_test_path
+    package = self._get_module_from_name(name)
+  File ".../loader.py", line ..., in _get_module_from_name
+    __import__(name)
+  File ".../runexample/__init__.py", line 1
+    class not in
+...^...
+SyntaxError: invalid syntax
+
+""", doctest.ELLIPSIS))
+
     def test_run_orders_tests(self):
         self.useFixture(SampleTestFixture())
         out = StringIO()
@@ -73,8 +230,13 @@ testtools.runexample.missingtest
 """))
         finally:
             f.close()
-        run.main(['prog', '-l', '--load-list', tempname,
-            'testtools.runexample.test_suite'], out)
+        try:
+            run.main(['prog', '-l', '--load-list', tempname,
+                'testtools.runexample.test_suite'], out)
+        except SystemExit:
+            exc_info = sys.exc_info()
+            raise AssertionError(
+                "-l --load-list tried to exit. %r" % exc_info[1])
         self.assertEqual("""testtools.runexample.TestFoo.test_bar
 """, out.getvalue())
 
@@ -94,11 +256,43 @@ testtools.runexample.missingtest
 """))
         finally:
             f.close()
-        run.main(['prog', '-l', '--load-list', tempname,
-            'testtools.runexample.test_suite'], out)
+        try:
+            run.main(['prog', '-l', '--load-list', tempname,
+                'testtools.runexample.test_suite'], out)
+        except SystemExit:
+            exc_info = sys.exc_info()
+            raise AssertionError(
+                "-l --load-list tried to exit. %r" % exc_info[1])
         self.assertEqual("""testtools.runexample.TestFoo.test_bar
 """, out.getvalue())
 
+    def test_load_list_preserves_custom_suites(self):
+        if testresources is None:
+            self.skipTest("Need testresources")
+        self.useFixture(SampleResourcedFixture())
+        # We load two tests, not loading one. Both share a resource, so we
+        # should see just one resource setup occur.
+        tempdir = self.useFixture(fixtures.TempDir())
+        tempname = tempdir.path + '/tests.list'
+        f = open(tempname, 'wb')
+        try:
+            f.write(_b("""
+testtools.resourceexample.TestFoo.test_bar
+testtools.resourceexample.TestFoo.test_foo
+"""))
+        finally:
+            f.close()
+        stdout = self.useFixture(fixtures.StringStream('stdout'))
+        with fixtures.MonkeyPatch('sys.stdout', stdout.stream):
+            try:
+                run.main(['prog', '--load-list', tempname,
+                    'testtools.resourceexample.test_suite'], stdout.stream)
+            except SystemExit:
+                # Evil resides in TestProgram.
+                pass
+        out = stdout.getDetails()['stdout'].as_text()
+        self.assertEqual(1, out.count('Setting up Printer'), "%r" % out)
+
     def test_run_failfast(self):
         stdout = self.useFixture(fixtures.StringStream('stdout'))
 
@@ -107,12 +301,44 @@ testtools.runexample.missingtest
                 self.fail('a')
             def test_b(self):
                 self.fail('b')
-        runner = run.TestToolsTestRunner(failfast=True)
         with fixtures.MonkeyPatch('sys.stdout', stdout.stream):
+            runner = run.TestToolsTestRunner(failfast=True)
             runner.run(TestSuite([Failing('test_a'), Failing('test_b')]))
         self.assertThat(
             stdout.getDetails()['stdout'].as_text(), Contains('Ran 1 test'))
 
+    def test_stdout_honoured(self):
+        self.useFixture(SampleTestFixture())
+        tests = []
+        out = StringIO()
+        exc = self.assertRaises(SystemExit, run.main,
+            argv=['prog', 'testtools.runexample.test_suite'],
+            stdout=out)
+        self.assertEqual((0,), exc.args)
+        self.assertThat(
+            out.getvalue(),
+            MatchesRegex(_u("""Tests running...
+
+Ran 2 tests in \\d.\\d\\d\\ds
+OK
+""")))
+
+    @skipUnless(fixtures, "fixtures not present")
+    def test_issue_16662(self):
+        # unittest's discover implementation didn't handle load_tests on
+        # packages. That is fixed pending commit, but we want to offer it
+        # to all testtools users regardless of Python version.
+        # See http://bugs.python.org/issue16662
+        pkg = self.useFixture(SampleLoadTestsPackage())
+        out = StringIO()
+        # XXX: http://bugs.python.org/issue22811
+        unittest2.defaultTestLoader._top_level_dir = None
+        self.assertEqual(None, run.main(
+            ['prog', 'discover', '-l', pkg.package.base], out))
+        self.assertEqual(dedent("""\
+            discoverexample.TestExample.test_foo
+            fred
+            """), out.getvalue())
 
 
 def test_suite():
diff --git a/lib/testtools/testtools/tests/test_runtest.py b/lib/testtools/testtools/tests/test_runtest.py
index afbb8ba..3ae8b13 100644
--- a/lib/testtools/testtools/tests/test_runtest.py
+++ b/lib/testtools/testtools/tests/test_runtest.py
@@ -34,6 +34,12 @@ class TestRunTest(TestCase):
         run = RunTest("bar", handlers)
         self.assertEqual(handlers, run.handlers)
 
+    def test__init____handlers_last_resort(self):
+        handlers = [("quux", "baz")]
+        last_resort = "foo"
+        run = RunTest("bar", handlers, last_resort)
+        self.assertEqual(last_resort, run.last_resort)
+
     def test_run_with_result(self):
         # test.run passes result down to _run_test_method.
         log = []
@@ -61,15 +67,19 @@ class TestRunTest(TestCase):
         run.run()
         self.assertEqual(['foo'], log)
 
-    def test__run_user_does_not_catch_keyboard(self):
-        case = self.make_case()
-        def raises():
-            raise KeyboardInterrupt("yo")
-        run = RunTest(case, None)
+    def test__run_prepared_result_does_not_mask_keyboard(self):
+        class Case(TestCase):
+            def test(self):
+                raise KeyboardInterrupt("go")
+        case = Case('test')
+        run = RunTest(case)
         run.result = ExtendedTestResult()
-        self.assertThat(lambda: run._run_user(raises),
+        self.assertThat(lambda: run._run_prepared_result(run.result),
             Raises(MatchesException(KeyboardInterrupt)))
-        self.assertEqual([], run.result._events)
+        self.assertEqual(
+            [('startTest', case), ('stopTest', case)], run.result._events)
+        # tearDown is still run though!
+        self.assertEqual(True, getattr(case, '_TestCase__teardown_called'))
 
     def test__run_user_calls_onException(self):
         case = self.make_case()
@@ -103,21 +113,43 @@ class TestRunTest(TestCase):
         self.assertEqual([], run.result._events)
         self.assertEqual([], log)
 
-    def test__run_user_uncaught_Exception_raised(self):
-        case = self.make_case()
+    def test__run_prepared_result_uncaught_Exception_raised(self):
         e = KeyError('Yo')
-        def raises():
-            raise e
+        class Case(TestCase):
+            def test(self):
+                raise e
+        case = Case('test')
         log = []
         def log_exc(self, result, err):
             log.append((result, err))
         run = RunTest(case, [(ValueError, log_exc)])
         run.result = ExtendedTestResult()
-        self.assertThat(lambda: run._run_user(raises),
+        self.assertThat(lambda: run._run_prepared_result(run.result),
             Raises(MatchesException(KeyError)))
-        self.assertEqual([], run.result._events)
+        self.assertEqual(
+            [('startTest', case), ('stopTest', case)], run.result._events)
         self.assertEqual([], log)
 
+    def test__run_prepared_result_uncaught_Exception_triggers_error(self):
+        # https://bugs.launchpad.net/testtools/+bug/1364188
+        # When something isn't handled, the test that was
+        # executing has errored, one way or another.
+        e = SystemExit(0)
+        class Case(TestCase):
+            def test(self):
+                raise e
+        case = Case('test')
+        log = []
+        def log_exc(self, result, err):
+            log.append((result, err))
+        run = RunTest(case, [], log_exc)
+        run.result = ExtendedTestResult()
+        self.assertThat(lambda: run._run_prepared_result(run.result),
+            Raises(MatchesException(SystemExit)))
+        self.assertEqual(
+            [('startTest', case), ('stopTest', case)], run.result._events)
+        self.assertEqual([(run.result, e)], log)
+
     def test__run_user_uncaught_Exception_from_exception_handler_raised(self):
         case = self.make_case()
         def broken_handler(exc_info):
diff --git a/lib/testtools/testtools/tests/test_spinner.py b/lib/testtools/testtools/tests/test_spinner.py
index 3d677bd..31110ca 100644
--- a/lib/testtools/testtools/tests/test_spinner.py
+++ b/lib/testtools/testtools/tests/test_spinner.py
@@ -5,11 +5,12 @@
 import os
 import signal
 
+from extras import try_import
+
 from testtools import (
     skipIf,
     TestCase,
     )
-from testtools.helpers import try_import
 from testtools.matchers import (
     Equals,
     Is,
@@ -231,7 +232,7 @@ class TestRunInReactor(NeedsTwistedTestCase):
         from twisted.internet.protocol import ServerFactory
         reactor = self.make_reactor()
         spinner = self.make_spinner(reactor)
-        port = reactor.listenTCP(0, ServerFactory())
+        port = reactor.listenTCP(0, ServerFactory(), interface='127.0.0.1')
         spinner.run(self.make_timeout(), lambda: None)
         results = spinner.get_junk()
         self.assertThat(results, Equals([port]))
@@ -244,14 +245,7 @@ class TestRunInReactor(NeedsTwistedTestCase):
         timeout = self.make_timeout()
         spinner = self.make_spinner(reactor)
         spinner.run(timeout, reactor.callInThread, time.sleep, timeout / 2.0)
-        # Python before 2.5 has a race condition with thread handling where
-        # join() does not remove threads from enumerate before returning - the
-        # thread being joined does the removal. This was fixed in Python 2.5
-        # but we still support 2.4, so we have to workaround the issue.
-        # http://bugs.python.org/issue1703448.
-        self.assertThat(
-            [thread for thread in threading.enumerate() if thread.isAlive()],
-            Equals(current_threads))
+        self.assertThat(list(threading.enumerate()), Equals(current_threads))
 
     def test_leftover_junk_available(self):
         # If 'run' is given a function that leaves the reactor dirty in some
@@ -261,7 +255,7 @@ class TestRunInReactor(NeedsTwistedTestCase):
         reactor = self.make_reactor()
         spinner = self.make_spinner(reactor)
         port = spinner.run(
-            self.make_timeout(), reactor.listenTCP, 0, ServerFactory())
+            self.make_timeout(), reactor.listenTCP, 0, ServerFactory(), interface='127.0.0.1')
         self.assertThat(spinner.get_junk(), Equals([port]))
 
     def test_will_not_run_with_previous_junk(self):
@@ -271,7 +265,7 @@ class TestRunInReactor(NeedsTwistedTestCase):
         reactor = self.make_reactor()
         spinner = self.make_spinner(reactor)
         timeout = self.make_timeout()
-        spinner.run(timeout, reactor.listenTCP, 0, ServerFactory())
+        spinner.run(timeout, reactor.listenTCP, 0, ServerFactory(), interface='127.0.0.1')
         self.assertThat(lambda: spinner.run(timeout, lambda: None),
             Raises(MatchesException(_spinner.StaleJunkError)))
 
@@ -282,7 +276,7 @@ class TestRunInReactor(NeedsTwistedTestCase):
         reactor = self.make_reactor()
         spinner = self.make_spinner(reactor)
         timeout = self.make_timeout()
-        port = spinner.run(timeout, reactor.listenTCP, 0, ServerFactory())
+        port = spinner.run(timeout, reactor.listenTCP, 0, ServerFactory(), interface='127.0.0.1')
         junk = spinner.clear_junk()
         self.assertThat(junk, Equals([port]))
         self.assertThat(spinner.get_junk(), Equals([]))
diff --git a/lib/testtools/testtools/tests/test_testcase.py b/lib/testtools/testtools/tests/test_testcase.py
index eca781b..185b726 100644
--- a/lib/testtools/testtools/tests/test_testcase.py
+++ b/lib/testtools/testtools/tests/test_testcase.py
@@ -8,6 +8,7 @@ import sys
 import unittest
 
 from testtools import (
+    DecorateTestCaseResult,
     ErrorHolder,
     MultipleExceptions,
     PlaceHolder,
@@ -23,15 +24,23 @@ from testtools.compat import (
     _b,
     _u,
     )
-from testtools.content import TracebackContent
+from testtools.content import (
+    text_content,
+    TracebackContent,
+    )
 from testtools.matchers import (
     Annotate,
     DocTestMatches,
     Equals,
+    HasLength,
     MatchesException,
     Raises,
     )
-from testtools.testcase import Nullary
+from testtools.testcase import (
+    attr,
+    Nullary,
+    WithAttributes,
+    )
 from testtools.testresult.doubles import (
     Python26TestResult,
     Python27TestResult,
@@ -42,12 +51,6 @@ from testtools.tests.helpers import (
     FullStackRunTest,
     LoggingResult,
     )
-try:
-    exec('from __future__ import with_statement')
-except SyntaxError:
-    pass
-else:
-    from testtools.tests.test_with_with import *
 
 
 class TestPlaceHolder(TestCase):
@@ -109,7 +112,8 @@ class TestPlaceHolder(TestCase):
         log = []
         test.run(LoggingResult(log))
         self.assertEqual(
-            [('startTest', test), ('addSuccess', test), ('stopTest', test)],
+            [('tags', set(), set()), ('startTest', test), ('addSuccess', test),
+             ('stopTest', test), ('tags', set(), set()),],
             log)
 
     def test_supplies_details(self):
@@ -118,9 +122,27 @@ class TestPlaceHolder(TestCase):
         result = ExtendedTestResult()
         test.run(result)
         self.assertEqual(
-            [('startTest', test),
+            [('tags', set(), set()),
+             ('startTest', test),
              ('addSuccess', test, details),
-             ('stopTest', test)],
+             ('stopTest', test),
+             ('tags', set(), set()),
+             ],
+            result._events)
+
+    def test_supplies_timestamps(self):
+        test = PlaceHolder('foo', details={}, timestamps=["A", "B"])
+        result = ExtendedTestResult()
+        test.run(result)
+        self.assertEqual(
+            [('time', "A"),
+             ('tags', set(), set()),
+             ('startTest', test),
+             ('time', "B"),
+             ('addSuccess', test),
+             ('stopTest', test),
+             ('tags', set(), set()),
+             ],
             result._events)
 
     def test_call_is_run(self):
@@ -141,6 +163,19 @@ class TestPlaceHolder(TestCase):
         # A PlaceHolder can be debugged.
         self.makePlaceHolder().debug()
 
+    def test_supports_tags(self):
+        result = ExtendedTestResult()
+        tags = set(['foo', 'bar'])
+        case = PlaceHolder("foo", tags=tags)
+        case.run(result)
+        self.assertEqual([
+            ('tags', tags, set()),
+            ('startTest', case),
+            ('addSuccess', case),
+            ('stopTest', case),
+            ('tags', set(), tags),
+            ], result._events)
+
 
 class TestErrorHolder(TestCase):
     # Note that these tests exist because ErrorHolder exists - it could be
@@ -194,9 +229,11 @@ class TestErrorHolder(TestCase):
         log = result._events
         test.run(result)
         self.assertEqual(
-            [('startTest', test),
+            [('tags', set(), set()),
+             ('startTest', test),
              ('addError', test, test._details),
-             ('stopTest', test)], log)
+             ('stopTest', test),
+             ('tags', set(), set())], log)
 
     def test_call_is_run(self):
         # A PlaceHolder can be called, in which case it behaves like run.
@@ -259,6 +296,19 @@ class TestAssertions(TestCase):
         # assertRaises asserts that a callable raises a particular exception.
         self.assertRaises(RuntimeError, self.raiseError, RuntimeError)
 
+    def test_assertRaises_exception_w_metaclass(self):
+        # assertRaises works when called for exceptions with custom metaclasses
+        class MyExMeta(type):
+            def __init__(cls, name, bases, dct):
+                """ Do some dummy metaclass stuff """
+                dct.update({'answer': 42})
+                type.__init__(cls, name, bases, dct)
+
+        class MyEx(Exception):
+            __metaclass__ = MyExMeta
+
+        self.assertRaises(MyEx, self.raiseError, MyEx)
+
     def test_assertRaises_fails_when_no_error_raised(self):
         # assertRaises raises self.failureException when it's passed a
         # callable that raises no error.
@@ -344,6 +394,16 @@ class TestAssertions(TestCase):
             '%r not in %r' % ('qux', 'foo bar baz'),
             self.assertIn, 'qux', 'foo bar baz')
 
+    def test_assertIn_failure_with_message(self):
+        # assertIn(needle, haystack) fails the test when 'needle' is not in
+        # 'haystack'.
+        self.assertFails('3 not in [0, 1, 2]: foo bar', self.assertIn, 3,
+                         [0, 1, 2], 'foo bar')
+        self.assertFails(
+            '%r not in %r: foo bar' % ('qux', 'foo bar baz'),
+            self.assertIn, 'qux', 'foo bar baz', 'foo bar')
+
+
     def test_assertNotIn_success(self):
         # assertNotIn(needle, haystack) asserts that 'needle' is not in
         # 'haystack'.
@@ -359,6 +419,18 @@ class TestAssertions(TestCase):
             "'foo bar baz' matches Contains('foo')",
             self.assertNotIn, 'foo', 'foo bar baz')
 
+
+    def test_assertNotIn_failure_with_message(self):
+        # assertNotIn(needle, haystack) fails the test when 'needle' is in
+        # 'haystack'.
+        self.assertFails('[1, 2, 3] matches Contains(3): foo bar', self.assertNotIn,
+            3, [1, 2, 3], 'foo bar')
+        self.assertFails(
+            "'foo bar baz' matches Contains('foo'): foo bar",
+            self.assertNotIn, 'foo', 'foo bar baz', "foo bar")
+
+
+
     def test_assertIsInstance(self):
         # assertIsInstance asserts that an object is an instance of a class.
 
@@ -513,6 +585,48 @@ class TestAssertions(TestCase):
         self.assertFails(
             expected, self.assertThat, matchee, matcher, verbose=True)
 
+    def test_expectThat_matches_clean(self):
+        class Matcher(object):
+            def match(self, foo):
+                return None
+        self.expectThat("foo", Matcher())
+
+    def test_expectThat_mismatch_fails_test(self):
+        class Test(TestCase):
+            def test(self):
+                self.expectThat("foo", Equals("bar"))
+        result = Test("test").run()
+        self.assertFalse(result.wasSuccessful())
+
+    def test_expectThat_does_not_exit_test(self):
+        class Test(TestCase):
+            marker = False
+            def test(self):
+                self.expectThat("foo", Equals("bar"))
+                Test.marker = True
+        result = Test("test").run()
+        self.assertFalse(result.wasSuccessful())
+        self.assertTrue(Test.marker)
+
+    def test_expectThat_adds_detail(self):
+        class Test(TestCase):
+            def test(self):
+                self.expectThat("foo", Equals("bar"))
+        test = Test("test")
+        result = test.run()
+        details = test.getDetails()
+        self.assertTrue("Failed expectation" in details)
+
+    def test__force_failure_fails_test(self):
+        class Test(TestCase):
+            def test_foo(self):
+                self.force_failure = True
+                self.remaining_code_run = True
+        test = Test('test_foo')
+        result = test.run()
+        self.assertFalse(result.wasSuccessful())
+        self.assertTrue(test.remaining_code_run)
+
     def get_error_string(self, e):
         """Get the string showing how 'e' would be formatted in test output.
 
@@ -609,6 +723,18 @@ class TestAssertions(TestCase):
         self.assertFails(expected_error, self.assertIsNotNone, None)
 
 
+    def test_fail_preserves_traceback_detail(self):
+        class Test(TestCase):
+            def test(self):
+                self.addDetail('traceback', text_content('foo'))
+                self.fail('bar')
+        test = Test('test')
+        result = ExtendedTestResult()
+        test.run(result)
+        self.assertEqual(set(['traceback', 'traceback-1']),
+            set(result._events[1][2].keys()))
+
+
 class TestAddCleanup(TestCase):
     """Tests for TestCase.addCleanup."""
 
@@ -777,6 +903,18 @@ class TestAddCleanup(TestCase):
             set(self.logging_result._events[1][2].keys()))
 
 
+class TestRunTestUsage(TestCase):
+
+    def test_last_resort_in_place(self):
+        class TestBase(TestCase):
+            def test_base_exception(self):
+                raise SystemExit(0)
+        result = ExtendedTestResult()
+        test = TestBase("test_base_exception")
+        self.assertRaises(SystemExit, test.run, result)
+        self.assertFalse(result.wasSuccessful())
+
+
 class TestWithDetails(TestCase):
 
     run_test_with = FullStackRunTest
@@ -874,6 +1012,28 @@ class TestExpectedFailure(TestWithDetails):
         self.assertDetailsProvided(case, "addUnexpectedSuccess",
             ["foo", "reason"])
 
+    @skipIf(not hasattr(unittest, 'expectedFailure'), 'Need py27+')
+    def test_unittest_expectedFailure_decorator_works_with_failure(self):
+        class ReferenceTest(TestCase):
+            @unittest.expectedFailure
+            def test_fails_expectedly(self):
+                self.assertEquals(1, 0)
+
+        test = ReferenceTest('test_fails_expectedly')
+        result = test.run()
+        self.assertEqual(True, result.wasSuccessful())
+
+    @skipIf(not hasattr(unittest, 'expectedFailure'), 'Need py27+')
+    def test_unittest_expectedFailure_decorator_works_with_success(self):
+        class ReferenceTest(TestCase):
+            @unittest.expectedFailure
+            def test_passes_unexpectedly(self):
+                self.assertEquals(1, 1)
+
+        test = ReferenceTest('test_passes_unexpectedly')
+        result = test.run()
+        self.assertEqual(False, result.wasSuccessful())
+
 
 class TestUniqueFactories(TestCase):
     """Tests for getUniqueString and getUniqueInteger."""
@@ -1039,11 +1199,32 @@ class TestDetailsProvided(TestWithDetails):
         self.assertDetailsProvided(Case("test"), "addFailure",
             ["foo", "foo-1", "traceback"])
 
+    def test_addDetailUniqueName_works(self):
+        content = self.get_content()
+        class Case(TestCase):
+            def test(self):
+                self.addDetailUniqueName("foo", content)
+                self.addDetailUniqueName("foo", content)
+        self.assertDetailsProvided(Case("test"), "addSuccess",
+            ["foo", "foo-1"])
+
 
 class TestSetupTearDown(TestCase):
 
     run_test_with = FullStackRunTest
 
+    def test_setUpCalledTwice(self):
+        class CallsTooMuch(TestCase):
+            def test_method(self):
+                self.setUp()
+        result = unittest.TestResult()
+        CallsTooMuch('test_method').run(result)
+        self.assertThat(result.errors, HasLength(1))
+        self.assertThat(result.errors[0][1],
+            DocTestMatches(
+                "...ValueError...File...testtools/tests/test_testcase.py...",
+                ELLIPSIS))
+
     def test_setUpNotCalled(self):
         class DoesnotcallsetUp(TestCase):
             def setUp(self):
@@ -1052,7 +1233,23 @@ class TestSetupTearDown(TestCase):
                 pass
         result = unittest.TestResult()
         DoesnotcallsetUp('test_method').run(result)
-        self.assertEqual(1, len(result.errors))
+        self.assertThat(result.errors, HasLength(1))
+        self.assertThat(result.errors[0][1],
+            DocTestMatches(
+                "...ValueError...File...testtools/tests/test_testcase.py...",
+                ELLIPSIS))
+
+    def test_tearDownCalledTwice(self):
+        class CallsTooMuch(TestCase):
+            def test_method(self):
+                self.tearDown()
+        result = unittest.TestResult()
+        CallsTooMuch('test_method').run(result)
+        self.assertThat(result.errors, HasLength(1))
+        self.assertThat(result.errors[0][1],
+            DocTestMatches(
+                "...ValueError...File...testtools/tests/test_testcase.py...",
+                ELLIPSIS))
 
     def test_tearDownNotCalled(self):
         class DoesnotcalltearDown(TestCase):
@@ -1062,7 +1259,17 @@ class TestSetupTearDown(TestCase):
                 pass
         result = unittest.TestResult()
         DoesnotcalltearDown('test_method').run(result)
-        self.assertEqual(1, len(result.errors))
+        self.assertThat(result.errors, HasLength(1))
+        self.assertThat(result.errors[0][1],
+            DocTestMatches(
+                "...ValueError...File...testtools/tests/test_testcase.py...",
+                ELLIPSIS))
+
+
+require_py27_minimum = skipIf(
+    sys.version < '2.7',
+    "Requires python 2.7 or greater"
+)
 
 
 class TestSkipping(TestCase):
@@ -1168,6 +1375,71 @@ class TestSkipping(TestCase):
         test.run(result)
         self.assertEqual('addSuccess', result._events[1][0])
 
+    def check_skip_decorator_does_not_run_setup(self, decorator, reason):
+        class SkippingTest(TestCase):
+
+            setup_ran = False
+
+            def setUp(self):
+                super(SkippingTest, self).setUp()
+                self.setup_ran = True
+
+            # Use the decorator passed to us:
+            @decorator
+            def test_skipped(self):
+                self.fail()
+
+        test = SkippingTest('test_skipped')
+        result = test.run()
+        self.assertTrue(result.wasSuccessful())
+        self.assertTrue(reason in result.skip_reasons, result.skip_reasons)
+        self.assertFalse(test.setup_ran)
+
+    def test_testtools_skip_decorator_does_not_run_setUp(self):
+        reason = self.getUniqueString()
+        self.check_skip_decorator_does_not_run_setup(
+            skip(reason),
+            reason
+        )
+
+    def test_testtools_skipIf_decorator_does_not_run_setUp(self):
+        reason = self.getUniqueString()
+        self.check_skip_decorator_does_not_run_setup(
+            skipIf(True, reason),
+            reason
+        )
+
+    def test_testtools_skipUnless_decorator_does_not_run_setUp(self):
+        reason = self.getUniqueString()
+        self.check_skip_decorator_does_not_run_setup(
+            skipUnless(False, reason),
+            reason
+        )
+
+    @require_py27_minimum
+    def test_unittest_skip_decorator_does_not_run_setUp(self):
+        reason = self.getUniqueString()
+        self.check_skip_decorator_does_not_run_setup(
+            unittest.skip(reason),
+            reason
+        )
+
+    @require_py27_minimum
+    def test_unittest_skipIf_decorator_does_not_run_setUp(self):
+        reason = self.getUniqueString()
+        self.check_skip_decorator_does_not_run_setup(
+            unittest.skipIf(True, reason),
+            reason
+        )
+
+    @require_py27_minimum
+    def test_unittest_skipUnless_decorator_does_not_run_setUp(self):
+        reason = self.getUniqueString()
+        self.check_skip_decorator_does_not_run_setup(
+            unittest.skipUnless(False, reason),
+            reason
+        )
+
 
 class TestOnException(TestCase):
 
@@ -1330,6 +1602,129 @@ class TestNullary(TestCase):
         self.assertRaises(ZeroDivisionError, wrapped)
 
 
+class Attributes(WithAttributes, TestCase):
+    @attr('foo')
+    def simple(self):
+        pass
+
+    # Not sorted here, forward or backwards.
+    @attr('foo', 'quux', 'bar')
+    def many(self):
+        pass
+
+    # Not sorted here, forward or backwards.
+    @attr('bar')
+    @attr('quux')
+    @attr('foo')
+    def decorated(self):
+        pass
+
+
+class TestAttributes(TestCase):
+
+    def test_simple_attr(self):
+        # Adding an attr to a test changes its id().
+        case = Attributes('simple')
+        self.assertEqual(
+            'testtools.tests.test_testcase.Attributes.simple[foo]',
+            case.id())
+
+    def test_multiple_attributes(self):
+        case = Attributes('many')
+        self.assertEqual(
+            'testtools.tests.test_testcase.Attributes.many[bar,foo,quux]',
+            case.id())
+
+    def test_multiple_attr_decorators(self):
+        case = Attributes('decorated')
+        self.assertEqual(
+            'testtools.tests.test_testcase.Attributes.decorated[bar,foo,quux]',
+            case.id())
+
+
+class TestDecorateTestCaseResult(TestCase):
+
+    def setUp(self):
+        super(TestDecorateTestCaseResult, self).setUp()
+        self.log = []
+
+    def make_result(self, result):
+        self.log.append(('result', result))
+        return LoggingResult(self.log)
+
+    def test___call__(self):
+        case = DecorateTestCaseResult(PlaceHolder('foo'), self.make_result)
+        case(None)
+        case('something')
+        self.assertEqual([('result', None),
+            ('tags', set(), set()),
+            ('startTest', case.decorated),
+            ('addSuccess', case.decorated),
+            ('stopTest', case.decorated),
+            ('tags', set(), set()),
+            ('result', 'something'),
+            ('tags', set(), set()),
+            ('startTest', case.decorated),
+            ('addSuccess', case.decorated),
+            ('stopTest', case.decorated),
+            ('tags', set(), set())
+            ], self.log)
+
+    def test_run(self):
+        case = DecorateTestCaseResult(PlaceHolder('foo'), self.make_result)
+        case.run(None)
+        case.run('something')
+        self.assertEqual([('result', None),
+            ('tags', set(), set()),
+            ('startTest', case.decorated),
+            ('addSuccess', case.decorated),
+            ('stopTest', case.decorated),
+            ('tags', set(), set()),
+            ('result', 'something'),
+            ('tags', set(), set()),
+            ('startTest', case.decorated),
+            ('addSuccess', case.decorated),
+            ('stopTest', case.decorated),
+            ('tags', set(), set())
+            ], self.log)
+
+    def test_before_after_hooks(self):
+        case = DecorateTestCaseResult(PlaceHolder('foo'), self.make_result,
+            before_run=lambda result: self.log.append('before'),
+            after_run=lambda result: self.log.append('after'))
+        case.run(None)
+        case(None)
+        self.assertEqual([
+            ('result', None),
+            'before',
+            ('tags', set(), set()),
+            ('startTest', case.decorated),
+            ('addSuccess', case.decorated),
+            ('stopTest', case.decorated),
+            ('tags', set(), set()),
+            'after',
+            ('result', None),
+            'before',
+            ('tags', set(), set()),
+            ('startTest', case.decorated),
+            ('addSuccess', case.decorated),
+            ('stopTest', case.decorated),
+            ('tags', set(), set()),
+            'after',
+            ], self.log)
+
+    def test_other_attribute(self):
+        orig = PlaceHolder('foo')
+        orig.thing = 'fred'
+        case = DecorateTestCaseResult(orig, self.make_result)
+        self.assertEqual('fred', case.thing)
+        self.assertRaises(AttributeError, getattr, case, 'other')
+        case.other = 'barbara'
+        self.assertEqual('barbara', orig.other)
+        del case.thing
+        self.assertRaises(AttributeError, getattr, orig, 'thing')
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)
diff --git a/lib/testtools/testtools/tests/test_testresult.py b/lib/testtools/testtools/tests/test_testresult.py
index 68fcc38..a8034b2 100644
--- a/lib/testtools/testtools/tests/test_testresult.py
+++ b/lib/testtools/testtools/tests/test_testresult.py
@@ -7,7 +7,9 @@ __metaclass__ = type
 import codecs
 import datetime
 import doctest
+from itertools import chain, combinations
 import os
+import re
 import shutil
 import sys
 import tempfile
@@ -15,17 +17,33 @@ import threading
 from unittest import TestSuite
 import warnings
 
+from extras import safe_hasattr, try_imports
+
+Queue = try_imports(['Queue.Queue', 'queue.Queue'])
+
 from testtools import (
+    CopyStreamResult,
     ExtendedToOriginalDecorator,
+    ExtendedToStreamDecorator,
     MultiTestResult,
     PlaceHolder,
+    StreamFailFast,
+    StreamResult,
+    StreamResultRouter,
+    StreamSummary,
+    StreamTagger,
+    StreamToDict,
+    StreamToExtendedDecorator,
+    StreamToQueue,
     Tagger,
     TestCase,
+    TestControl,
     TestResult,
     TestResultDecorator,
     TestByTestResult,
     TextTestResult,
     ThreadsafeForwardingResult,
+    TimestampingStreamResult,
     testresult,
     )
 from testtools.compat import (
@@ -44,13 +62,15 @@ from testtools.content import (
     TracebackContent,
     )
 from testtools.content_type import ContentType, UTF8_TEXT
-from testtools.helpers import safe_hasattr
 from testtools.matchers import (
+    AllMatch,
     Contains,
     DocTestMatches,
     Equals,
+    HasLength,
     MatchesAny,
     MatchesException,
+    MatchesRegex,
     Raises,
     )
 from testtools.tests.helpers import (
@@ -63,6 +83,7 @@ from testtools.testresult.doubles import (
     Python26TestResult,
     Python27TestResult,
     ExtendedTestResult,
+    StreamResult as LoggingStreamResult,
     )
 from testtools.testresult.real import (
     _details_to_str,
@@ -221,18 +242,21 @@ class TagsContract(Python27Contract):
     def test_no_tags_by_default(self):
         # Results initially have no tags.
         result = self.makeResult()
+        result.startTestRun()
         self.assertEqual(frozenset(), result.current_tags)
 
     def test_adding_tags(self):
         # Tags are added using 'tags' and thus become visible in
         # 'current_tags'.
         result = self.makeResult()
+        result.startTestRun()
         result.tags(set(['foo']), set())
         self.assertEqual(set(['foo']), result.current_tags)
 
     def test_removing_tags(self):
         # Tags are removed using 'tags'.
         result = self.makeResult()
+        result.startTestRun()
         result.tags(set(['foo']), set())
         result.tags(set(), set(['foo']))
         self.assertEqual(set(), result.current_tags)
@@ -240,6 +264,7 @@ class TagsContract(Python27Contract):
     def test_startTestRun_resets_tags(self):
         # startTestRun makes a new test run, and thus clears all the tags.
         result = self.makeResult()
+        result.startTestRun()
         result.tags(set(['foo']), set())
         result.startTestRun()
         self.assertEqual(set(), result.current_tags)
@@ -437,6 +462,12 @@ class TestAdaptedPython27TestResultContract(TestCase, DetailsContract):
         return ExtendedToOriginalDecorator(Python27TestResult())
 
 
+class TestAdaptedStreamResult(TestCase, DetailsContract):
+
+    def makeResult(self):
+        return ExtendedToStreamDecorator(StreamResult())
+
+
 class TestTestResultDecoratorContract(TestCase, StartTestRunContract):
 
     run_test_with = FullStackRunTest
@@ -445,6 +476,581 @@ class TestTestResultDecoratorContract(TestCase, StartTestRunContract):
         return TestResultDecorator(TestResult())
 
 
+# DetailsContract because ExtendedToStreamDecorator follows Python for
+# uxsuccess handling.
+class TestStreamToExtendedContract(TestCase, DetailsContract):
+
+    def makeResult(self):
+        return ExtendedToStreamDecorator(
+            StreamToExtendedDecorator(ExtendedTestResult()))
+
+
+class TestStreamResultContract(object):
+
+    def _make_result(self):
+        raise NotImplementedError(self._make_result)
+
+    def test_startTestRun(self):
+        result = self._make_result()
+        result.startTestRun()
+        result.stopTestRun()
+
+    def test_files(self):
+        # Test parameter combinations when files are being emitted.
+        result = self._make_result()
+        result.startTestRun()
+        self.addCleanup(result.stopTestRun)
+        now = datetime.datetime.now(utc)
+        inputs = list(dict(
+            eof=True,
+            mime_type="text/plain",
+            route_code=_u("1234"),
+            test_id=_u("foo"),
+            timestamp=now,
+            ).items())
+        param_dicts = self._power_set(inputs)
+        for kwargs in param_dicts:
+            result.status(file_name=_u("foo"), file_bytes=_b(""), **kwargs)
+            result.status(file_name=_u("foo"), file_bytes=_b("bar"), **kwargs)
+
+    def test_test_status(self):
+        # Tests non-file attachment parameter combinations.
+        result = self._make_result()
+        result.startTestRun()
+        self.addCleanup(result.stopTestRun)
+        now = datetime.datetime.now(utc)
+        args = [[_u("foo"), s] for s in ['exists', 'inprogress', 'xfail',
+            'uxsuccess', 'success', 'fail', 'skip']]
+        inputs = list(dict(
+            runnable=False,
+            test_tags=set(['quux']),
+            route_code=_u("1234"),
+            timestamp=now,
+            ).items())
+        param_dicts = self._power_set(inputs)
+        for kwargs in param_dicts:
+            for arg in args:
+                result.status(test_id=arg[0], test_status=arg[1], **kwargs)
+
+    def _power_set(self, iterable):
+        "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
+        s = list(iterable)
+        param_dicts = []
+        for ss in chain.from_iterable(combinations(s, r) for r in range(len(s)+1)):
+            param_dicts.append(dict(ss))
+        return param_dicts
+
+
+class TestBaseStreamResultContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamResult()
+
+
+class TestCopyStreamResultContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return CopyStreamResult([StreamResult(), StreamResult()])
+
+
+class TestDoubleStreamResultContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return LoggingStreamResult()
+
+
+class TestExtendedToStreamDecoratorContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return ExtendedToStreamDecorator(StreamResult())
+
+
+class TestStreamSummaryResultContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamSummary()
+
+
+class TestStreamTaggerContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamTagger([StreamResult()], add=set(), discard=set())
+
+
+class TestStreamToDictContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamToDict(lambda x:None)
+
+
+class TestStreamToExtendedDecoratorContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamToExtendedDecorator(ExtendedTestResult())
+
+
+class TestStreamToQueueContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        queue = Queue()
+        return StreamToQueue(queue, "foo")
+
+
+class TestStreamFailFastContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamFailFast(lambda:None)
+
+
+class TestStreamResultRouterContract(TestCase, TestStreamResultContract):
+
+    def _make_result(self):
+        return StreamResultRouter(StreamResult())
+
+
+class TestDoubleStreamResultEvents(TestCase):
+
+    def test_startTestRun(self):
+        result = LoggingStreamResult()
+        result.startTestRun()
+        self.assertEqual([('startTestRun',)], result._events)
+
+    def test_stopTestRun(self):
+        result = LoggingStreamResult()
+        result.startTestRun()
+        result.stopTestRun()
+        self.assertEqual([('startTestRun',), ('stopTestRun',)], result._events)
+
+    def test_file(self):
+        result = LoggingStreamResult()
+        result.startTestRun()
+        now = datetime.datetime.now(utc)
+        result.status(file_name="foo", file_bytes="bar", eof=True, mime_type="text/json",
+            test_id="id", route_code='abc', timestamp=now)
+        self.assertEqual(
+            [('startTestRun',),
+             ('status', 'id', None, None, True, 'foo', 'bar', True, 'text/json', 'abc', now)],
+            result._events)
+
+    def test_status(self):
+        result = LoggingStreamResult()
+        result.startTestRun()
+        now = datetime.datetime.now(utc)
+        result.status("foo", "success", test_tags=set(['tag']),
+            runnable=False, route_code='abc', timestamp=now)
+        self.assertEqual(
+            [('startTestRun',),
+             ('status', 'foo', 'success', set(['tag']), False, None, None, False, None, 'abc', now)],
+            result._events)
+
+
+class TestCopyStreamResultCopies(TestCase):
+
+    def setUp(self):
+        super(TestCopyStreamResultCopies, self).setUp()
+        self.target1 = LoggingStreamResult()
+        self.target2 = LoggingStreamResult()
+        self.targets = [self.target1._events, self.target2._events]
+        self.result = CopyStreamResult([self.target1, self.target2])
+
+    def test_startTestRun(self):
+        self.result.startTestRun()
+        self.assertThat(self.targets, AllMatch(Equals([('startTestRun',)])))
+
+    def test_stopTestRun(self):
+        self.result.startTestRun()
+        self.result.stopTestRun()
+        self.assertThat(self.targets,
+            AllMatch(Equals([('startTestRun',), ('stopTestRun',)])))
+
+    def test_status(self):
+        self.result.startTestRun()
+        now = datetime.datetime.now(utc)
+        self.result.status("foo", "success", test_tags=set(['tag']),
+            runnable=False, file_name="foo", file_bytes=b'bar', eof=True,
+            mime_type="text/json", route_code='abc', timestamp=now)
+        self.assertThat(self.targets,
+            AllMatch(Equals([('startTestRun',),
+                ('status', 'foo', 'success', set(['tag']), False, "foo",
+                 b'bar', True, "text/json", 'abc', now)
+                ])))
+
+
+class TestStreamTagger(TestCase):
+
+    def test_adding(self):
+        log = LoggingStreamResult()
+        result = StreamTagger([log], add=['foo'])
+        result.startTestRun()
+        result.status()
+        result.status(test_tags=set(['bar']))
+        result.status(test_tags=None)
+        result.stopTestRun()
+        self.assertEqual([
+            ('startTestRun',),
+            ('status', None, None, set(['foo']), True, None, None, False, None, None, None),
+            ('status', None, None, set(['foo', 'bar']), True, None, None, False, None, None, None),
+            ('status', None, None, set(['foo']), True, None, None, False, None, None, None),
+            ('stopTestRun',),
+            ], log._events)
+
+    def test_discarding(self):
+        log = LoggingStreamResult()
+        result = StreamTagger([log], discard=['foo'])
+        result.startTestRun()
+        result.status()
+        result.status(test_tags=None)
+        result.status(test_tags=set(['foo']))
+        result.status(test_tags=set(['bar']))
+        result.status(test_tags=set(['foo', 'bar']))
+        result.stopTestRun()
+        self.assertEqual([
+            ('startTestRun',),
+            ('status', None, None, None, True, None, None, False, None, None, None),
+            ('status', None, None, None, True, None, None, False, None, None, None),
+            ('status', None, None, None, True, None, None, False, None, None, None),
+            ('status', None, None, set(['bar']), True, None, None, False, None, None, None),
+            ('status', None, None, set(['bar']), True, None, None, False, None, None, None),
+            ('stopTestRun',),
+            ], log._events)
+
+
+class TestStreamToDict(TestCase):
+
+    def test_hung_test(self):
+        tests = []
+        result = StreamToDict(tests.append)
+        result.startTestRun()
+        result.status('foo', 'inprogress')
+        self.assertEqual([], tests)
+        result.stopTestRun()
+        self.assertEqual([
+            {'id': 'foo', 'tags': set(), 'details': {}, 'status': 'inprogress',
+             'timestamps': [None, None]}
+            ], tests)
+
+    def test_all_terminal_states_reported(self):
+        tests = []
+        result = StreamToDict(tests.append)
+        result.startTestRun()
+        result.status('success', 'success')
+        result.status('skip', 'skip')
+        result.status('exists', 'exists')
+        result.status('fail', 'fail')
+        result.status('xfail', 'xfail')
+        result.status('uxsuccess', 'uxsuccess')
+        self.assertThat(tests, HasLength(6))
+        self.assertEqual(
+            ['success', 'skip', 'exists', 'fail', 'xfail', 'uxsuccess'],
+            [test['id'] for test in tests])
+        result.stopTestRun()
+        self.assertThat(tests, HasLength(6))
+
+    def test_files_reported(self):
+        tests = []
+        result = StreamToDict(tests.append)
+        result.startTestRun()
+        result.status(file_name="some log.txt",
+            file_bytes=_b("1234 log message"), eof=True,
+            mime_type="text/plain; charset=utf8", test_id="foo.bar")
+        result.status(file_name="another file",
+            file_bytes=_b("""Traceback..."""), test_id="foo.bar")
+        result.stopTestRun()
+        self.assertThat(tests, HasLength(1))
+        test = tests[0]
+        self.assertEqual("foo.bar", test['id'])
+        self.assertEqual("unknown", test['status'])
+        details = test['details']
+        self.assertEqual(
+            _u("1234 log message"), details['some log.txt'].as_text())
+        self.assertEqual(
+            _b("Traceback..."),
+            _b('').join(details['another file'].iter_bytes()))
+        self.assertEqual(
+            "application/octet-stream", repr(details['another file'].content_type))
+
+    def test_bad_mime(self):
+        # Testtools was making bad mime types, this tests that the specific
+        # corruption is catered for.
+        tests = []
+        result = StreamToDict(tests.append)
+        result.startTestRun()
+        result.status(file_name="file", file_bytes=b'a',
+            mime_type='text/plain; charset=utf8, language=python',
+            test_id='id')
+        result.stopTestRun()
+        self.assertThat(tests, HasLength(1))
+        test = tests[0]
+        self.assertEqual("id", test['id'])
+        details = test['details']
+        self.assertEqual(_u("a"), details['file'].as_text())
+        self.assertEqual(
+            "text/plain; charset=\"utf8\"",
+            repr(details['file'].content_type))
+
+    def test_timestamps(self):
+        tests = []
+        result = StreamToDict(tests.append)
+        result.startTestRun()
+        result.status(test_id='foo', test_status='inprogress', timestamp="A")
+        result.status(test_id='foo', test_status='success', timestamp="B")
+        result.status(test_id='bar', test_status='inprogress', timestamp="C")
+        result.stopTestRun()
+        self.assertThat(tests, HasLength(2))
+        self.assertEqual(["A", "B"], tests[0]['timestamps'])
+        self.assertEqual(["C", None], tests[1]['timestamps'])
+
+
+class TestExtendedToStreamDecorator(TestCase):
+
+    def test_explicit_time(self):
+        log = LoggingStreamResult()
+        result = ExtendedToStreamDecorator(log)
+        result.startTestRun()
+        now = datetime.datetime.now(utc)
+        result.time(now)
+        result.startTest(self)
+        result.addSuccess(self)
+        result.stopTest(self)
+        result.stopTestRun()
+        self.assertEqual([
+            ('startTestRun',),
+            ('status',
+             'testtools.tests.test_testresult.TestExtendedToStreamDecorator.test_explicit_time',
+             'inprogress',
+             None,
+             True,
+             None,
+             None,
+             False,
+             None,
+             None,
+             now),
+            ('status',
+             'testtools.tests.test_testresult.TestExtendedToStreamDecorator.test_explicit_time',
+             'success',
+              set(),
+              True,
+              None,
+              None,
+              False,
+              None,
+              None,
+              now),
+             ('stopTestRun',)], log._events)
+
+    def test_wasSuccessful_after_stopTestRun(self):
+        log = LoggingStreamResult()
+        result = ExtendedToStreamDecorator(log)
+        result.startTestRun()
+        result.status(test_id='foo', test_status='fail')
+        result.stopTestRun()
+        self.assertEqual(False, result.wasSuccessful())
+
+
+class TestStreamFailFast(TestCase):
+
+    def test_inprogress(self):
+        result = StreamFailFast(self.fail)
+        result.status('foo', 'inprogress')
+
+    def test_exists(self):
+        result = StreamFailFast(self.fail)
+        result.status('foo', 'exists')
+
+    def test_xfail(self):
+        result = StreamFailFast(self.fail)
+        result.status('foo', 'xfail')
+
+    def test_uxsuccess(self):
+        calls = []
+        def hook():
+            calls.append("called")
+        result = StreamFailFast(hook)
+        result.status('foo', 'uxsuccess')
+        result.status('foo', 'uxsuccess')
+        self.assertEqual(['called', 'called'], calls)
+
+    def test_success(self):
+        result = StreamFailFast(self.fail)
+        result.status('foo', 'success')
+
+    def test_fail(self):
+        calls = []
+        def hook():
+            calls.append("called")
+        result = StreamFailFast(hook)
+        result.status('foo', 'fail')
+        result.status('foo', 'fail')
+        self.assertEqual(['called', 'called'], calls)
+
+    def test_skip(self):
+        result = StreamFailFast(self.fail)
+        result.status('foo', 'skip')
+
+
+class TestStreamSummary(TestCase):
+
+    def test_attributes(self):
+        result = StreamSummary()
+        result.startTestRun()
+        self.assertEqual([], result.failures)
+        self.assertEqual([], result.errors)
+        self.assertEqual([], result.skipped)
+        self.assertEqual([], result.expectedFailures)
+        self.assertEqual([], result.unexpectedSuccesses)
+        self.assertEqual(0, result.testsRun)
+
+    def test_startTestRun(self):
+        result = StreamSummary()
+        result.startTestRun()
+        result.failures.append('x')
+        result.errors.append('x')
+        result.skipped.append('x')
+        result.expectedFailures.append('x')
+        result.unexpectedSuccesses.append('x')
+        result.testsRun = 1
+        result.startTestRun()
+        self.assertEqual([], result.failures)
+        self.assertEqual([], result.errors)
+        self.assertEqual([], result.skipped)
+        self.assertEqual([], result.expectedFailures)
+        self.assertEqual([], result.unexpectedSuccesses)
+        self.assertEqual(0, result.testsRun)
+
+    def test_wasSuccessful(self):
+        # wasSuccessful returns False if any of
+        # failures/errors is non-empty.
+        result = StreamSummary()
+        result.startTestRun()
+        self.assertEqual(True, result.wasSuccessful())
+        result.failures.append('x')
+        self.assertEqual(False, result.wasSuccessful())
+        result.startTestRun()
+        result.errors.append('x')
+        self.assertEqual(False, result.wasSuccessful())
+        result.startTestRun()
+        result.skipped.append('x')
+        self.assertEqual(True, result.wasSuccessful())
+        result.startTestRun()
+        result.expectedFailures.append('x')
+        self.assertEqual(True, result.wasSuccessful())
+        result.startTestRun()
+        result.unexpectedSuccesses.append('x')
+        self.assertEqual(True, result.wasSuccessful())
+
+    def test_stopTestRun(self):
+        result = StreamSummary()
+        # terminal successful codes.
+        result.startTestRun()
+        result.status("foo", "inprogress")
+        result.status("foo", "success")
+        result.status("bar", "skip")
+        result.status("baz", "exists")
+        result.stopTestRun()
+        self.assertEqual(True, result.wasSuccessful())
+        # Existence is terminal but doesn't count as 'running' a test.
+        self.assertEqual(2, result.testsRun)
+
+    def test_stopTestRun_inprogress_test_fails(self):
+        # Tests inprogress at stopTestRun trigger a failure.
+        result = StreamSummary()
+        result.startTestRun()
+        result.status("foo", "inprogress")
+        result.stopTestRun()
+        self.assertEqual(False, result.wasSuccessful())
+        self.assertThat(result.errors, HasLength(1))
+        self.assertEqual("foo", result.errors[0][0].id())
+        self.assertEqual("Test did not complete", result.errors[0][1])
+        # interim state detection handles route codes - while duplicate ids in
+        # one run is undesirable, it may happen (e.g. with repeated tests).
+        result.startTestRun()
+        result.status("foo", "inprogress")
+        result.status("foo", "inprogress", route_code="A")
+        result.status("foo", "success", route_code="A")
+        result.stopTestRun()
+        self.assertEqual(False, result.wasSuccessful())
+
+    def test_status_skip(self):
+        # when skip is seen, a synthetic test is reported with reason captured
+        # from the 'reason' file attachment if any.
+        result = StreamSummary()
+        result.startTestRun()
+        result.status(file_name="reason",
+            file_bytes=_b("Missing dependency"), eof=True,
+            mime_type="text/plain; charset=utf8", test_id="foo.bar")
+        result.status("foo.bar", "skip")
+        self.assertThat(result.skipped, HasLength(1))
+        self.assertEqual("foo.bar", result.skipped[0][0].id())
+        self.assertEqual(_u("Missing dependency"), result.skipped[0][1])
+
+    def _report_files(self, result):
+        result.status(file_name="some log.txt",
+            file_bytes=_b("1234 log message"), eof=True,
+            mime_type="text/plain; charset=utf8", test_id="foo.bar")
+        result.status(file_name="traceback",
+            file_bytes=_b("""Traceback (most recent call last):
+  File "testtools/tests/test_testresult.py", line 607, in test_stopTestRun
+      AllMatch(Equals([('startTestRun',), ('stopTestRun',)])))
+testtools.matchers._impl.MismatchError: Differences: [
+[('startTestRun',), ('stopTestRun',)] != []
+[('startTestRun',), ('stopTestRun',)] != []
+]
+"""), eof=True, mime_type="text/plain; charset=utf8", test_id="foo.bar")
+
+    files_message = Equals(_u("""some log.txt: {{{1234 log message}}}
+
+Traceback (most recent call last):
+  File "testtools/tests/test_testresult.py", line 607, in test_stopTestRun
+      AllMatch(Equals([('startTestRun',), ('stopTestRun',)])))
+testtools.matchers._impl.MismatchError: Differences: [
+[('startTestRun',), ('stopTestRun',)] != []
+[('startTestRun',), ('stopTestRun',)] != []
+]
+"""))
+
+    def test_status_fail(self):
+        # when fail is seen, a synthetic test is reported with all files
+        # attached shown as the message.
+        result = StreamSummary()
+        result.startTestRun()
+        self._report_files(result)
+        result.status("foo.bar", "fail")
+        self.assertThat(result.errors, HasLength(1))
+        self.assertEqual("foo.bar", result.errors[0][0].id())
+        self.assertThat(result.errors[0][1], self.files_message)
+
+    def test_status_xfail(self):
+        # when xfail is seen, a synthetic test is reported with all files
+        # attached shown as the message.
+        result = StreamSummary()
+        result.startTestRun()
+        self._report_files(result)
+        result.status("foo.bar", "xfail")
+        self.assertThat(result.expectedFailures, HasLength(1))
+        self.assertEqual("foo.bar", result.expectedFailures[0][0].id())
+        self.assertThat(result.expectedFailures[0][1], self.files_message)
+
+    def test_status_uxsuccess(self):
+        # when uxsuccess is seen, a synthetic test is reported.
+        result = StreamSummary()
+        result.startTestRun()
+        result.status("foo.bar", "uxsuccess")
+        self.assertThat(result.unexpectedSuccesses, HasLength(1))
+        self.assertEqual("foo.bar", result.unexpectedSuccesses[0].id())
+
+
+class TestTestControl(TestCase):
+
+    def test_default(self):
+        self.assertEqual(False, TestControl().shouldStop)
+
+    def test_stop(self):
+        control = TestControl()
+        control.stop()
+        self.assertEqual(True, control.shouldStop)
+
+
 class TestTestResult(TestCase):
     """Tests for 'TestResult'."""
 
@@ -785,6 +1391,7 @@ class TestTextTestResult(TestCase):
             DocTestMatches("...\nFAILED (failures=1)\n", doctest.ELLIPSIS))
 
     def test_stopTestRun_shows_details(self):
+        self.skip("Disabled per bug 1188420")
         def run_tests():
             self.result.startTestRun()
             make_erroring_test().run(self.result)
@@ -1097,6 +1704,193 @@ class TestMergeTags(TestCase):
             expected, _merge_tags(current_tags, changing_tags))
 
 
+class TestStreamResultRouter(TestCase):
+
+    def test_start_stop_test_run_no_fallback(self):
+        result = StreamResultRouter()
+        result.startTestRun()
+        result.stopTestRun()
+
+    def test_no_fallback_errors(self):
+        self.assertRaises(Exception, StreamResultRouter().status, test_id='f')
+
+    def test_fallback_calls(self):
+        fallback = LoggingStreamResult()
+        result = StreamResultRouter(fallback)
+        result.startTestRun()
+        result.status(test_id='foo')
+        result.stopTestRun()
+        self.assertEqual([
+            ('startTestRun',),
+            ('status', 'foo', None, None, True, None, None, False, None, None,
+             None),
+            ('stopTestRun',),
+            ],
+            fallback._events)
+
+    def test_fallback_no_do_start_stop_run(self):
+        fallback = LoggingStreamResult()
+        result = StreamResultRouter(fallback, do_start_stop_run=False)
+        result.startTestRun()
+        result.status(test_id='foo')
+        result.stopTestRun()
+        self.assertEqual([
+            ('status', 'foo', None, None, True, None, None, False, None, None,
+             None)
+            ],
+            fallback._events)
+
+    def test_add_rule_bad_policy(self):
+        router = StreamResultRouter()
+        target = LoggingStreamResult()
+        self.assertRaises(ValueError, router.add_rule, target, 'route_code_prefixa',
+            route_prefix='0')
+
+    def test_add_rule_extra_policy_arg(self):
+        router = StreamResultRouter()
+        target = LoggingStreamResult()
+        self.assertRaises(TypeError, router.add_rule, target, 'route_code_prefix',
+            route_prefix='0', foo=1)
+
+    def test_add_rule_missing_prefix(self):
+        router = StreamResultRouter()
+        target = LoggingStreamResult()
+        self.assertRaises(TypeError, router.add_rule, target, 'route_code_prefix')
+
+    def test_add_rule_slash_in_prefix(self):
+        router = StreamResultRouter()
+        target = LoggingStreamResult()
+        self.assertRaises(TypeError, router.add_rule, target, 'route_code_prefix',
+            route_prefix='0/')
+
+    def test_add_rule_route_code_consume_False(self):
+        fallback = LoggingStreamResult()
+        target = LoggingStreamResult()
+        router = StreamResultRouter(fallback)
+        router.add_rule(target, 'route_code_prefix', route_prefix='0')
+        router.status(test_id='foo', route_code='0')
+        router.status(test_id='foo', route_code='0/1')
+        router.status(test_id='foo')
+        self.assertEqual([
+            ('status', 'foo', None, None, True, None, None, False, None, '0',
+             None),
+            ('status', 'foo', None, None, True, None, None, False, None, '0/1',
+             None),
+            ],
+            target._events)
+        self.assertEqual([
+            ('status', 'foo', None, None, True, None, None, False, None, None,
+             None),
+            ],
+            fallback._events)
+
+    def test_add_rule_route_code_consume_True(self):
+        fallback = LoggingStreamResult()
+        target = LoggingStreamResult()
+        router = StreamResultRouter(fallback)
+        router.add_rule(
+            target, 'route_code_prefix', route_prefix='0', consume_route=True)
+        router.status(test_id='foo', route_code='0') # -> None
+        router.status(test_id='foo', route_code='0/1') # -> 1
+        router.status(test_id='foo', route_code='1') # -> fallback as-is.
+        self.assertEqual([
+            ('status', 'foo', None, None, True, None, None, False, None, None,
+             None),
+            ('status', 'foo', None, None, True, None, None, False, None, '1',
+             None),
+            ],
+            target._events)
+        self.assertEqual([
+            ('status', 'foo', None, None, True, None, None, False, None, '1',
+             None),
+            ],
+            fallback._events)
+
+    def test_add_rule_test_id(self):
+        nontest = LoggingStreamResult()
+        test = LoggingStreamResult()
+        router = StreamResultRouter(test)
+        router.add_rule(nontest, 'test_id', test_id=None)
+        router.status(test_id='foo', file_name="bar", file_bytes=b'')
+        router.status(file_name="bar", file_bytes=b'')
+        self.assertEqual([
+            ('status', 'foo', None, None, True, 'bar', b'', False, None, None,
+             None),], test._events)
+        self.assertEqual([
+            ('status', None, None, None, True, 'bar', b'', False, None, None,
+             None),], nontest._events)
+
+    def test_add_rule_do_start_stop_run(self):
+        nontest = LoggingStreamResult()
+        router = StreamResultRouter()
+        router.add_rule(nontest, 'test_id', test_id=None, do_start_stop_run=True)
+        router.startTestRun()
+        router.stopTestRun()
+        self.assertEqual([
+            ('startTestRun',),
+            ('stopTestRun',),
+            ], nontest._events)
+
+    def test_add_rule_do_start_stop_run_after_startTestRun(self):
+        nontest = LoggingStreamResult()
+        router = StreamResultRouter()
+        router.startTestRun()
+        router.add_rule(nontest, 'test_id', test_id=None, do_start_stop_run=True)
+        router.stopTestRun()
+        self.assertEqual([
+            ('startTestRun',),
+            ('stopTestRun',),
+            ], nontest._events)
+
+
+class TestStreamToQueue(TestCase):
+
+    def make_result(self):
+        queue = Queue()
+        return queue, StreamToQueue(queue, "foo")
+
+    def test_status(self):
+        def check_event(event_dict, route=None, time=None):
+            self.assertEqual("status", event_dict['event'])
+            self.assertEqual("test", event_dict['test_id'])
+            self.assertEqual("fail", event_dict['test_status'])
+            self.assertEqual(set(["quux"]), event_dict['test_tags'])
+            self.assertEqual(False, event_dict['runnable'])
+            self.assertEqual("file", event_dict['file_name'])
+            self.assertEqual(_b("content"), event_dict['file_bytes'])
+            self.assertEqual(True, event_dict['eof'])
+            self.assertEqual("quux", event_dict['mime_type'])
+            self.assertEqual("test", event_dict['test_id'])
+            self.assertEqual(route, event_dict['route_code'])
+            self.assertEqual(time, event_dict['timestamp'])
+        queue, result = self.make_result()
+        result.status("test", "fail", test_tags=set(["quux"]), runnable=False,
+            file_name="file", file_bytes=_b("content"), eof=True,
+            mime_type="quux", route_code=None, timestamp=None)
+        self.assertEqual(1, queue.qsize())
+        a_time = datetime.datetime.now(utc)
+        result.status("test", "fail", test_tags=set(["quux"]), runnable=False,
+            file_name="file", file_bytes=_b("content"), eof=True,
+            mime_type="quux", route_code="bar", timestamp=a_time)
+        self.assertEqual(2, queue.qsize())
+        check_event(queue.get(False), route="foo", time=None)
+        check_event(queue.get(False), route="foo/bar", time=a_time)
+
+    def testStartTestRun(self):
+        queue, result = self.make_result()
+        result.startTestRun()
+        self.assertEqual(
+            {'event':'startTestRun', 'result':result}, queue.get(False))
+        self.assertTrue(queue.empty())
+
+    def testStopTestRun(self):
+        queue, result = self.make_result()
+        result.stopTestRun()
+        self.assertEqual(
+            {'event':'stopTestRun', 'result':result}, queue.get(False))
+        self.assertTrue(queue.empty())
+
+
 class TestExtendedToOriginalResultDecoratorBase(TestCase):
 
     def make_26_result(self):
@@ -1558,13 +2352,13 @@ class TestNonAsciiResults(TestCase):
 
     def _test_external_case(self, testline, coding="ascii", modulelevel="",
             suffix=""):
-        """Create and run a test case in a separate module"""
+        """Create and run a test case in a seperate module"""
         self._setup_external_case(testline, coding, modulelevel, suffix)
         return self._run_external_case()
 
     def _setup_external_case(self, testline, coding="ascii", modulelevel="",
             suffix=""):
-        """Create a test case in a separate module"""
+        """Create a test case in a seperate module"""
         _, prefix, self.modname = self.id().rsplit(".", 2)
         self.dir = tempfile.mkdtemp(prefix=prefix, suffix=suffix)
         self.addCleanup(shutil.rmtree, self.dir)
@@ -1580,7 +2374,7 @@ class TestNonAsciiResults(TestCase):
             "        %s\n" % (coding, modulelevel, testline))
 
     def _run_external_case(self):
-        """Run the prepared test case in a separate module"""
+        """Run the prepared test case in a seperate module"""
         sys.path.insert(0, self.dir)
         self.addCleanup(sys.path.remove, self.dir)
         module = __import__(self.modname)
@@ -1589,11 +2383,6 @@ class TestNonAsciiResults(TestCase):
         self._run(stream, module.Test())
         return stream.getvalue()
 
-    def _silence_deprecation_warnings(self):
-        """Shut up DeprecationWarning for this test only"""
-        warnings.simplefilter("ignore", DeprecationWarning)
-        self.addCleanup(warnings.filters.remove, warnings.filters[0])
-
     def _get_sample_text(self, encoding="unicode_internal"):
         if encoding is None and str_is_unicode:
            encoding = "unicode_internal"
@@ -1635,7 +2424,7 @@ class TestNonAsciiResults(TestCase):
         if sys.version_info > (3, 3):
             return MatchesAny(Contains("FileExistsError: "),
                               Contains("PermissionError: "))
-        elif os.name != "nt" or sys.version_info < (2, 5):
+        elif os.name != "nt":
             return Contains(self._as_output("OSError: "))
         else:
             return Contains(self._as_output("WindowsError: "))
@@ -1699,15 +2488,6 @@ class TestNonAsciiResults(TestCase):
             "UnprintableError: <unprintable UnprintableError object>\n"),
             textoutput)
 
-    def test_string_exception(self):
-        """Raise a string rather than an exception instance if supported"""
-        if sys.version_info > (2, 6):
-            self.skip("No string exceptions in Python 2.6 or later")
-        elif sys.version_info > (2, 5):
-            self._silence_deprecation_warnings()
-        textoutput = self._test_external_case(testline="raise 'plain str'")
-        self.assertIn(self._as_output("\nplain str\n"), textoutput)
-
     def test_non_ascii_dirname(self):
         """Script paths in the traceback can be non-ascii"""
         text, raw = self._get_sample_text(sys.getfilesystemencoding())
@@ -1737,9 +2517,6 @@ class TestNonAsciiResults(TestCase):
 
     def test_syntax_error_import_binary(self):
         """Importing a binary file shouldn't break SyntaxError formatting"""
-        if sys.version_info < (2, 5):
-            # Python 2.4 assumes the file is latin-1 and tells you off
-            self._silence_deprecation_warnings()
         self._setup_external_case("import bad")
         f = open(os.path.join(self.dir, "bad.py"), "wb")
         try:
@@ -1772,13 +2549,18 @@ class TestNonAsciiResults(TestCase):
         self._write_module("bad", "iso-8859-5",
             "# coding: iso-8859-5\n%% = 0 # %s\n" % text)
         textoutput = self._run_external_case()
-        self.assertIn(self._as_output(_u(
-            #'bad.py", line 2\n'
-            '    %% = 0 # %s\n'
-            + ' ' * self._error_on_character +
-            '   ^\n'
-            'SyntaxError: ') %
-            (text,)), textoutput)
+        self.assertThat(
+            textoutput,
+            MatchesRegex(
+                self._as_output(_u(
+                #'bad.py", line 2\n'
+                '.*%% = 0 # %s\n'
+                + ' ' * self._error_on_character +
+                '\\s*\\^\n'
+                'SyntaxError:.*') %
+                (text,)),
+            re.MULTILINE | re.DOTALL)
+        )
 
     def test_syntax_error_line_euc_jp(self):
         """Syntax error on a euc_jp line shows the line decoded"""
@@ -1804,13 +2586,17 @@ class TestNonAsciiResults(TestCase):
         textoutput = self._setup_external_case("import bad")
         self._write_module("bad", "utf-8", _u("\ufeff^ = 0 # %s\n") % text)
         textoutput = self._run_external_case()
-        self.assertIn(self._as_output(_u(
-            'bad.py", line 1\n'
-            '    ^ = 0 # %s\n'
-            + ' ' * self._error_on_character +
-            '   ^\n'
-            'SyntaxError: ') %
-            text), textoutput)
+        self.assertThat(
+            textoutput,
+            MatchesRegex(
+                self._as_output(_u(
+                    '.*bad.py", line 1\n'
+                    '\\s*\\^ = 0 # %s\n'
+                    + ' ' * self._error_on_character +
+                    '\\s*\\^\n'
+                    'SyntaxError:.*') % text),
+                re.M | re.S)
+        )
 
 
 class TestNonAsciiResultsWithUnittest(TestNonAsciiResults):
@@ -2090,6 +2876,38 @@ class TestTagger(TestCase):
              ], result._events)
 
 
+class TestTimestampingStreamResult(TestCase):
+
+    def test_startTestRun(self):
+        result = TimestampingStreamResult(LoggingStreamResult())
+        result.startTestRun()
+        self.assertEqual([('startTestRun',)], result.targets[0]._events)
+
+    def test_stopTestRun(self):
+        result = TimestampingStreamResult(LoggingStreamResult())
+        result.stopTestRun()
+        self.assertEqual([('stopTestRun',)], result.targets[0]._events)
+
+    def test_status_no_timestamp(self):
+        result = TimestampingStreamResult(LoggingStreamResult())
+        result.status(test_id="A", test_status="B", test_tags="C",
+            runnable="D", file_name="E", file_bytes=b"F", eof=True,
+            mime_type="G", route_code="H")
+        events = result.targets[0]._events
+        self.assertThat(events, HasLength(1))
+        self.assertThat(events[0], HasLength(11))
+        self.assertEqual(
+            ("status", "A", "B", "C", "D", "E", b"F", True, "G", "H"),
+            events[0][:10])
+        self.assertNotEqual(None, events[0][10])
+        self.assertIsInstance(events[0][10], datetime.datetime)
+
+    def test_status_timestamp(self):
+        result = TimestampingStreamResult(LoggingStreamResult())
+        result.status(timestamp="F")
+        self.assertEqual("F", result.targets[0]._events[0][10])
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)
diff --git a/lib/testtools/testtools/tests/test_testsuite.py b/lib/testtools/testtools/tests/test_testsuite.py
index 3fc837c..08cd777 100644
--- a/lib/testtools/testtools/tests/test_testsuite.py
+++ b/lib/testtools/testtools/tests/test_testsuite.py
@@ -4,17 +4,27 @@
 
 __metaclass__ = type
 
+import doctest
+from functools import partial
+import sys
 import unittest
+import unittest2
+
+from extras import try_import
 
 from testtools import (
     ConcurrentTestSuite,
+    ConcurrentStreamTestSuite,
     iterate_tests,
     PlaceHolder,
+    TestByTestResult,
     TestCase,
     )
-from testtools.helpers import try_import
+from testtools.compat import _b, _u
+from testtools.matchers import DocTestMatches
 from testtools.testsuite import FixtureSuite, iterate_tests, sorted_tests
 from testtools.tests.helpers import LoggingResult
+from testtools.testresult.doubles import StreamResult as LoggingStream
 
 FunctionFixture = try_import('fixtures.FunctionFixture')
 
@@ -26,8 +36,23 @@ class Sample(TestCase):
     def test_method2(self):
         pass
 
+
 class TestConcurrentTestSuiteRun(TestCase):
 
+    def test_broken_test(self):
+        log = []
+        def on_test(test, status, start_time, stop_time, tags, details):
+            log.append((test.id(), status, set(details.keys())))
+        class BrokenTest(object):
+            # Simple break - no result parameter to run()
+            def __call__(self):
+                pass
+            run = __call__
+        original_suite = unittest.TestSuite([BrokenTest()])
+        suite = ConcurrentTestSuite(original_suite, self.split_suite)
+        suite.run(TestByTestResult(on_test))
+        self.assertEqual([('broken-runner', 'error', set(['traceback']))], log)
+
     def test_trivial(self):
         log = []
         result = LoggingResult(log)
@@ -68,8 +93,149 @@ class TestConcurrentTestSuiteRun(TestCase):
         self.assertNotEqual([], result_log)
 
     def split_suite(self, suite):
-        tests = list(iterate_tests(suite))
-        return tests[0], tests[1]
+        return list(iterate_tests(suite))
+
+
+class TestConcurrentStreamTestSuiteRun(TestCase):
+
+    def test_trivial(self):
+        result = LoggingStream()
+        test1 = Sample('test_method1')
+        test2 = Sample('test_method2')
+        cases = lambda:[(test1, '0'), (test2, '1')]
+        suite = ConcurrentStreamTestSuite(cases)
+        suite.run(result)
+        def freeze(set_or_none):
+            if set_or_none is None:
+                return set_or_none
+            return frozenset(set_or_none)
+        # Ignore event order: we're testing the code is all glued together,
+        # which just means we can pump events through and they get route codes
+        # added appropriately.
+        self.assertEqual(set([
+            ('status',
+             'testtools.tests.test_testsuite.Sample.test_method1',
+             'inprogress',
+             None,
+             True,
+             None,
+             None,
+             False,
+             None,
+             '0',
+             None,
+             ),
+            ('status',
+             'testtools.tests.test_testsuite.Sample.test_method1',
+             'success',
+             frozenset(),
+             True,
+             None,
+             None,
+             False,
+             None,
+             '0',
+             None,
+             ),
+            ('status',
+             'testtools.tests.test_testsuite.Sample.test_method2',
+             'inprogress',
+             None,
+             True,
+             None,
+             None,
+             False,
+             None,
+             '1',
+             None,
+             ),
+            ('status',
+             'testtools.tests.test_testsuite.Sample.test_method2',
+             'success',
+             frozenset(),
+             True,
+             None,
+             None,
+             False,
+             None,
+             '1',
+             None,
+             ),
+            ]), set(event[0:3] + (freeze(event[3]),) + event[4:10] + (None,)
+                for event in result._events))
+
+    def test_broken_runner(self):
+        # If the object called breaks, the stream is informed about it
+        # regardless.
+        class BrokenTest(object):
+            # broken - no result parameter!
+            def __call__(self):
+                pass
+            def run(self):
+                pass
+        result = LoggingStream()
+        cases = lambda:[(BrokenTest(), '0')]
+        suite = ConcurrentStreamTestSuite(cases)
+        suite.run(result)
+        events = result._events
+        # Check the traceback loosely.
+        self.assertThat(events[1][6].decode('utf8'), DocTestMatches("""\
+Traceback (most recent call last):
+  File "...testtools/testsuite.py", line ..., in _run_test
+    test.run(process_result)
+TypeError: run() takes ...1 ...argument...2...given...
+""", doctest.ELLIPSIS))
+        events = [event[0:10] + (None,) for event in events]
+        events[1] = events[1][:6] + (None,) + events[1][7:]
+        self.assertEqual([
+            ('status', "broken-runner-'0'", 'inprogress', None, True, None, None, False, None, _u('0'), None),
+            ('status', "broken-runner-'0'", None, None, True, 'traceback', None,
+             True,
+             'text/x-traceback; charset="utf8"; language="python"',
+             '0',
+             None),
+             ('status', "broken-runner-'0'", 'fail', set(), True, None, None, False, None, _u('0'), None)
+            ], events)
+
+    def split_suite(self, suite):
+        tests = list(enumerate(iterate_tests(suite)))
+        return [(test, _u(str(pos))) for pos, test in tests]
+
+    def test_setupclass_skip(self):
+        # We should support setupclass skipping using cls.skipException.
+        # Because folk have used that.
+        class Skips(TestCase):
+            @classmethod
+            def setUpClass(cls):
+                raise cls.skipException('foo')
+            def test_notrun(self):
+                pass
+        # Test discovery uses the default suite from unittest2 (unless users
+        # deliberately change things, in which case they keep both pieces).
+        suite = unittest2.TestSuite([Skips("test_notrun")])
+        log = []
+        result = LoggingResult(log)
+        suite.run(result)
+        self.assertEqual(['addSkip'], [item[0] for item in log])
+
+    def test_setupclass_upcall(self):
+        # Note that this is kindof-a-case-test, kindof-suite, because
+        # setUpClass is linked between them.
+        class Simples(TestCase):
+            @classmethod
+            def setUpClass(cls):
+                super(Simples, cls).setUpClass()
+            def test_simple(self):
+                pass
+        # Test discovery uses the default suite from unittest2 (unless users
+        # deliberately change things, in which case they keep both pieces).
+        suite = unittest2.TestSuite([Simples("test_simple")])
+        log = []
+        result = LoggingResult(log)
+        suite.run(result)
+        self.assertEqual(
+            ['startTest', 'addSuccess', 'stopTest'],
+            [item[0] for item in log])
 
 
 class TestFixtureSuite(TestCase):
@@ -93,6 +259,19 @@ class TestFixtureSuite(TestCase):
         suite.run(LoggingResult([]))
         self.assertEqual(['setUp', 1, 2, 'tearDown'], log)
 
+    def test_fixture_suite_sort(self):
+        log = []
+        class Sample(TestCase):
+            def test_one(self):
+                log.append(1)
+            def test_two(self):
+                log.append(2)
+        fixture = FunctionFixture(
+            lambda: log.append('setUp'),
+            lambda fixture: log.append('tearDown'))
+        suite = FixtureSuite(fixture, [Sample('test_one'), Sample('test_one')])
+        self.assertRaises(ValueError, suite.sort_tests)
+
 
 class TestSortedTests(TestCase):
 
@@ -122,6 +301,13 @@ class TestSortedTests(TestCase):
         suite = sorted_tests(unittest.TestSuite([b, a]))
         self.assertEqual([a, b], list(iterate_tests(suite)))
 
+    def test_duplicate_simple_suites(self):
+        a = PlaceHolder('a')
+        b = PlaceHolder('b')
+        c = PlaceHolder('a')
+        self.assertRaises(
+            ValueError, sorted_tests, unittest.TestSuite([a, b, c]))
+
 
 def test_suite():
     from unittest import TestLoader
diff --git a/lib/testtools/testtools/tests/test_with_with.py b/lib/testtools/testtools/tests/test_with_with.py
index e06adeb..277b4d9 100644
--- a/lib/testtools/testtools/tests/test_with_with.py
+++ b/lib/testtools/testtools/tests/test_with_with.py
@@ -11,6 +11,7 @@ from testtools import (
 from testtools.matchers import (
     AfterPreprocessing,
     Equals,
+    EndsWith,
     )
 
 
@@ -71,3 +72,22 @@ class TestExpectedException(TestCase):
     def test_pass_on_raise_any_message(self):
         with ExpectedException(ValueError):
             raise ValueError('whatever')
+    
+    def test_annotate(self):
+        def die():
+            with ExpectedException(ValueError, msg="foo"):
+                pass
+        exc = self.assertRaises(AssertionError, die)
+        self.assertThat(exc.args[0], EndsWith(': foo'))
+
+    def test_annotated_matcher(self):
+        def die():
+            with ExpectedException(ValueError, 'bar', msg="foo"):
+                pass
+        exc = self.assertRaises(AssertionError, die)
+        self.assertThat(exc.args[0], EndsWith(': foo'))
+
+
+def test_suite():
+    from unittest import TestLoader
+    return TestLoader().loadTestsFromName(__name__)
diff --git a/lib/testtools/testtools/testsuite.py b/lib/testtools/testtools/testsuite.py
index 67ace56..ff8f878 100644
--- a/lib/testtools/testtools/testsuite.py
+++ b/lib/testtools/testtools/testsuite.py
@@ -5,16 +5,20 @@
 __metaclass__ = type
 __all__ = [
   'ConcurrentTestSuite',
+  'ConcurrentStreamTestSuite',
+  'filter_by_ids',
   'iterate_tests',
   'sorted_tests',
   ]
 
-from testtools.helpers import safe_hasattr, try_imports
-
-Queue = try_imports(['Queue.Queue', 'queue.Queue'])
-
+import sys
 import threading
 import unittest
+import unittest2
+
+from extras import safe_hasattr, try_imports
+
+Queue = try_imports(['Queue.Queue', 'queue.Queue'])
 
 import testtools
 
@@ -31,7 +35,7 @@ def iterate_tests(test_suite_or_case):
                 yield subtest
 
 
-class ConcurrentTestSuite(unittest.TestSuite):
+class ConcurrentTestSuite(unittest2.TestSuite):
     """A TestSuite whose run() calls out to a concurrency strategy."""
 
     def __init__(self, suite, make_tests, wrap_result=None):
@@ -98,12 +102,103 @@ class ConcurrentTestSuite(unittest.TestSuite):
 
     def _run_test(self, test, process_result, queue):
         try:
-            test.run(process_result)
+            try:
+                test.run(process_result)
+            except Exception as e:
+                # The run logic itself failed.
+                case = testtools.ErrorHolder(
+                    "broken-runner",
+                    error=sys.exc_info())
+                case.run(process_result)
         finally:
             queue.put(test)
 
 
-class FixtureSuite(unittest.TestSuite):
+class ConcurrentStreamTestSuite(object):
+    """A TestSuite whose run() parallelises."""
+
+    def __init__(self, make_tests):
+        """Create a ConcurrentTestSuite to execute tests returned by make_tests.
+
+        :param make_tests: A helper function that should return some number
+            of concurrently executable test suite / test case objects.
+            make_tests must take no parameters and return an iterable of
+            tuples. Each tuple must be of the form (case, route_code), where
+            case is a TestCase-like object with a run(result) method, and
+            route_code is either None or a unicode string.
+        """
+        super(ConcurrentStreamTestSuite, self).__init__()
+        self.make_tests = make_tests
+
+    def run(self, result):
+        """Run the tests concurrently.
+
+        This calls out to the provided make_tests helper to determine the
+        concurrency to use and to assign routing codes to each worker.
+
+        ConcurrentTestSuite provides no special mechanism to stop the tests
+        returned by make_tests, it is up to the made tests to honour the
+        shouldStop attribute on the result object they are run with, which will
+        be set if the test run is to be aborted.
+
+        The tests are run with an ExtendedToStreamDecorator wrapped around a
+        StreamToQueue instance. ConcurrentStreamTestSuite dequeues events from
+        the queue and forwards them to result. Tests can therefore be either
+        original unittest tests (or compatible tests), or new tests that emit
+        StreamResult events directly.
+
+        :param result: A StreamResult instance. The caller is responsible for
+            calling startTestRun on this instance prior to invoking suite.run,
+            and stopTestRun subsequent to the run method returning.
+        """
+        tests = self.make_tests()
+        try:
+            threads = {}
+            queue = Queue()
+            for test, route_code in tests:
+                to_queue = testtools.StreamToQueue(queue, route_code)
+                process_result = testtools.ExtendedToStreamDecorator(
+                    testtools.TimestampingStreamResult(to_queue))
+                runner_thread = threading.Thread(
+                    target=self._run_test,
+                    args=(test, process_result, route_code))
+                threads[to_queue] = runner_thread, process_result
+                runner_thread.start()
+            while threads:
+                event_dict = queue.get()
+                event = event_dict.pop('event')
+                if event == 'status':
+                    result.status(**event_dict)
+                elif event == 'stopTestRun':
+                    thread = threads.pop(event_dict['result'])[0]
+                    thread.join()
+                elif event == 'startTestRun':
+                    pass
+                else:
+                    raise ValueError('unknown event type %r' % (event,))
+        except:
+            for thread, process_result in threads.values():
+                # Signal to each TestControl in the ExtendedToStreamDecorator
+                # that the thread should stop running tests and cleanup
+                process_result.stop()
+            raise
+
+    def _run_test(self, test, process_result, route_code):
+        process_result.startTestRun()
+        try:
+            try:
+                test.run(process_result)
+            except Exception as e:
+                # The run logic itself failed.
+                case = testtools.ErrorHolder(
+                    "broken-runner-'%s'" % (route_code,),
+                    error=sys.exc_info())
+                case.run(process_result)
+        finally:
+            process_result.stopTestRun()
+
+
+class FixtureSuite(unittest2.TestSuite):
 
     def __init__(self, fixture, tests):
         super(FixtureSuite, self).__init__(tests)
@@ -147,8 +242,77 @@ def _flatten_tests(suite_or_case, unpack_outer=False):
         return [(suite_id, suite_or_case)]
 
 
+def filter_by_ids(suite_or_case, test_ids):
+    """Remove tests from suite_or_case where their id is not in test_ids.
+    
+    :param suite_or_case: A test suite or test case.
+    :param test_ids: Something that supports the __contains__ protocol.
+    :return: suite_or_case, unless suite_or_case was a case that itself
+        fails the predicate when it will return a new unittest.TestSuite with
+        no contents.
+
+    This helper exists to provide backwards compatability with older versions
+    of Python (currently all versions :)) that don't have a native
+    filter_by_ids() method on Test(Case|Suite).
+
+    For subclasses of TestSuite, filtering is done by:
+        - attempting to call suite.filter_by_ids(test_ids)
+        - if there is no method, iterating the suite and identifying tests to
+          remove, then removing them from _tests, manually recursing into
+          each entry.
+
+    For objects with an id() method - TestCases, filtering is done by:
+        - attempting to return case.filter_by_ids(test_ids)
+        - if there is no such method, checking for case.id() in test_ids
+          and returning case if it is, or TestSuite() if it is not.
+
+    For anything else, it is not filtered - it is returned as-is.
+
+    To provide compatability with this routine for a custom TestSuite, just
+    define a filter_by_ids() method that will return a TestSuite equivalent to
+    the original minus any tests not in test_ids.
+    Similarly to provide compatability for a custom TestCase that does
+    something unusual define filter_by_ids to return a new TestCase object
+    that will only run test_ids that are in the provided container. If none
+    would run, return an empty TestSuite().
+
+    The contract for this function does not require mutation - each filtered
+    object can choose to return a new object with the filtered tests. However
+    because existing custom TestSuite classes in the wild do not have this
+    method, we need a way to copy their state correctly which is tricky:
+    thus the backwards-compatible code paths attempt to mutate in place rather
+    than guessing how to reconstruct a new suite.
+    """
+    # Compatible objects
+    if safe_hasattr(suite_or_case, 'filter_by_ids'):
+        return suite_or_case.filter_by_ids(test_ids)
+    # TestCase objects.
+    if safe_hasattr(suite_or_case, 'id'):
+        if suite_or_case.id() in test_ids:
+            return suite_or_case
+        else:
+            return unittest.TestSuite()
+    # Standard TestSuites or derived classes [assumed to be mutable].
+    if isinstance(suite_or_case, unittest.TestSuite):
+        filtered = []
+        for item in suite_or_case:
+            filtered.append(filter_by_ids(item, test_ids))
+        suite_or_case._tests[:] = filtered
+    # Everything else:
+    return suite_or_case
+
+
 def sorted_tests(suite_or_case, unpack_outer=False):
     """Sort suite_or_case while preserving non-vanilla TestSuites."""
+    # Duplicate test id can induce TypeError in Python 3.3.
+    # Detect the duplicate test id, raise exception when found.
+    seen = set()
+    for test_case in iterate_tests(suite_or_case):
+        test_id = test_case.id()
+        if test_id not in seen:
+            seen.add(test_id)
+        else:
+            raise ValueError('Duplicate test id detected: %s' % (test_id,))
     tests = _flatten_tests(suite_or_case, unpack_outer=unpack_outer)
     tests.sort()
     return unittest.TestSuite([test for (sort_key, test) in tests])
diff --git a/third_party/pexpect/.gitignore b/third_party/pexpect/.gitignore
new file mode 100644
index 0000000..8777ca7
--- /dev/null
+++ b/third_party/pexpect/.gitignore
@@ -0,0 +1,9 @@
+*.pyc
+doc/_build
+tests/log
+build/
+dist/
+MANIFEST
+*~
+.coverage*
+htmlcov
diff --git a/third_party/pexpect/.travis.yml b/third_party/pexpect/.travis.yml
new file mode 100644
index 0000000..51c967d
--- /dev/null
+++ b/third_party/pexpect/.travis.yml
@@ -0,0 +1,23 @@
+language: python
+
+python:
+  - 2.7
+  - 3.3
+  - 3.4
+  - pypy
+
+install:
+  - export PYTHONIOENCODING=UTF8
+  - pip install coveralls pytest-cov ptyprocess
+
+script:
+    - ./tools/display-sighandlers.py
+    - ./tools/display-terminalinfo.py
+    - py.test --cov pexpect --cov-config .coveragerc
+
+after_success:
+  - coverage combine
+  - coveralls
+
+# Use new Travis stack, should be faster
+sudo: false
diff --git a/third_party/pexpect/ANSI.py b/third_party/pexpect/ANSI.py
new file mode 100644
index 0000000..ca1673b
--- /dev/null
+++ b/third_party/pexpect/ANSI.py
@@ -0,0 +1,7 @@
+import warnings
+
+warnings.warn("This module has been moved to pexpect.ANSI, please update imports.",
+                ImportWarning)
+del warnings
+
+from pexpect.ANSI import *  # analysis:ignore
\ No newline at end of file
diff --git a/third_party/pexpect/DEVELOPERS.rst b/third_party/pexpect/DEVELOPERS.rst
new file mode 100644
index 0000000..bf2bb9f
--- /dev/null
+++ b/third_party/pexpect/DEVELOPERS.rst
@@ -0,0 +1,12 @@
+To run the tests, use `py.test <http://pytest.org/latest/>`_::
+
+    py.test tests
+
+The tests are all located in the tests/ directory. To add a new unit
+test all you have to do is create the file in the tests/ directory with a
+filename in this format::
+
+    test_*.py
+
+New test case classes may wish to inherit from ``PexpectTestCase.PexpectTestCase``
+in the tests directory, which sets up some convenient functionality.
diff --git a/third_party/pexpect/FSM.py b/third_party/pexpect/FSM.py
new file mode 100644
index 0000000..4e1ab49
--- /dev/null
+++ b/third_party/pexpect/FSM.py
@@ -0,0 +1,7 @@
+import warnings
+
+warnings.warn("This module has been moved to pexpect.FSM, please update imports.",
+                ImportWarning)
+del warnings
+
+from pexpect.FSM import *  # analysis:ignore
\ No newline at end of file
diff --git a/third_party/pexpect/LICENSE b/third_party/pexpect/LICENSE
new file mode 100644
index 0000000..9e10acb
--- /dev/null
+++ b/third_party/pexpect/LICENSE
@@ -0,0 +1,19 @@
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2013-2014, Pexpect development team
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
diff --git a/third_party/pexpect/MANIFEST.in b/third_party/pexpect/MANIFEST.in
new file mode 100644
index 0000000..91fd411
--- /dev/null
+++ b/third_party/pexpect/MANIFEST.in
@@ -0,0 +1,8 @@
+recursive-include doc *
+prune doc/_build
+recursive-include examples *
+include README LICENSE INSTALL
+include setup.py pexpect.py fdpexpect.py pxssh.py screen.py ANSI.py FSM.py
+include test.env tools/testall.py
+recursive-include tests *
+global-exclude __pycache__ *.pyc *~
diff --git a/third_party/pexpect/README.rst b/third_party/pexpect/README.rst
new file mode 100644
index 0000000..dde7ade
--- /dev/null
+++ b/third_party/pexpect/README.rst
@@ -0,0 +1,53 @@
+.. image:: https://travis-ci.org/pexpect/pexpect.png?branch=master
+   :target: https://travis-ci.org/pexpect/pexpect
+   :align: right
+   :alt: Build status
+
+Pexpect is a Pure Python Expect-like module
+
+Pexpect makes Python a better tool for controlling other applications.
+
+Pexpect is a pure Python module for spawning child applications; controlling
+them; and responding to expected patterns in their output. Pexpect works like
+Don Libes' Expect. Pexpect allows your script to spawn a child application and
+control it as if a human were typing commands.
+
+Pexpect can be used for automating interactive applications such as ssh, ftp,
+passwd, telnet, etc. It can be used to a automate setup scripts for duplicating
+software package installations on different servers. It can be used for
+automated software testing. Pexpect is in the spirit of Don Libes' Expect, but
+Pexpect is pure Python. Unlike other Expect-like modules for Python, Pexpect
+does not require TCL or Expect nor does it require C extensions to be compiled.
+It should work on any platform that supports the standard Python pty module.
+The Pexpect interface was designed to be easy to use.
+
+If you want to work with the development version of the source code then please
+read the DEVELOPERS.rst document in the root of the source code tree.
+
+Free, open source, and all that good stuff.
+
+You can install Pexpect using pip::
+
+    pip install pexpect
+
+`Docs on ReadTheDocs <http://pexpect.readthedocs.org/>`_
+
+PEXPECT LICENSE
+
+    http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2013-2014, Pexpect development team
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+This license is approved by the OSI and FSF as GPL-compatible.
diff --git a/third_party/pexpect/doc/FAQ.rst b/third_party/pexpect/doc/FAQ.rst
new file mode 100644
index 0000000..bec1c35
--- /dev/null
+++ b/third_party/pexpect/doc/FAQ.rst
@@ -0,0 +1,136 @@
+FAQ
+===
+
+**Q: Why don't shell pipe and redirect (| and >) work when I spawn a command?**
+
+A: Remember that Pexpect does NOT interpret shell meta characters such as
+redirect, pipe, or wild cards (``>``, ``|``, or ``*``). That's done by a shell not
+the command you are spawning. This is a common mistake. If you want to run a
+command and pipe it through another command then you must also start a shell.
+For example::
+
+    child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
+    child.expect(pexpect.EOF)
+
+The second form of spawn (where you pass a list of arguments) is useful in
+situations where you wish to spawn a command and pass it its own argument list.
+This can make syntax more clear. For example, the following is equivalent to the
+previous example::
+
+    shell_cmd = 'ls -l | grep LOG > log_list.txt'
+    child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
+    child.expect(pexpect.EOF)
+
+**Q: Isn't there already a Python Expect?**
+
+A: Yes, there are several of them. They usually require you to compile C.
+I wanted something that was pure Python and preferably a single module
+that was simple to install. I also wanted something that was easy to use.
+This pure Python expect only became possible with the introduction of
+the pty module in the standard Python library. Previously, C extensions
+were required.
+
+**Q: The `before` and `after` properties sound weird.**
+
+A: This is how the -B and -A options in grep works, so that made it
+easier for me to remember. Whatever makes my life easier is what's best.
+Originally I was going to model Pexpect after Expect, but then I found
+that I didn't actually like the way Expect did some things. It was more
+confusing. The `after` property can be a little confusing at first,
+because it will actually include the matched string. The `after` means
+after the point of match, not after the matched string.
+
+**Q: Why not just use Expect?**
+
+A: I love it. It's great. I has bailed me out of some real jams, but I
+wanted something that would do 90% of what I need from Expect; be 10% of
+the size; and allow me to write my code in Python instead of TCL.
+Pexpect is not nearly as big as Expect, but Pexpect does everything I
+have ever used Expect for.
+
+.. _whynotpipe:
+
+**Q: Why not just use a pipe (popen())?**
+
+A: A pipe works fine for getting the output to non-interactive programs.
+If you just want to get the output from ls, uname, or ping then this
+works. Pipes do not work very well for interactive programs and pipes
+will almost certainly fail for most applications that ask for passwords
+such as telnet, ftp, or ssh.
+
+There are two reasons for this.
+
+* First an application may bypass stdout and print directly to its
+  controlling TTY. Something like SSH will do this when it asks you for
+  a password. This is why you cannot redirect the password prompt because
+  it does not go through stdout or stderr.
+
+* The second reason is because most applications are built using the C
+  Standard IO Library (anything that uses ``#include <stdio.h>``). One
+  of the features of the stdio library is that it buffers all input and
+  output. Normally output is line buffered when a program is printing to
+  a TTY (your terminal screen). Everytime the program prints a line-feed
+  the currently buffered data will get printed to your screen. The
+  problem comes when you connect a pipe. The stdio library is smart and
+  can tell that it is printing to a pipe instead of a TTY. In that case
+  it switches from line buffer mode to block buffered. In this mode the
+  currently buffered data is flushed when the buffer is full. This
+  causes most interactive programs to deadlock. Block buffering is more
+  efficient when writing to disks and pipes. Take the situation where a
+  program prints a message ``"Enter your user name:\n"`` and then waits
+  for you type type something. In block buffered mode, the stdio library
+  will not put the message into the pipe even though a linefeed is
+  printed. The result is that you never receive the message, yet the
+  child application will sit and wait for you to type a response. Don't
+  confuse the stdio lib's buffer with the pipe's buffer. The pipe buffer
+  is another area that can cause problems. You could flush the input
+  side of a pipe, whereas you have no control over the stdio library buffer.
+
+More information: the Standard IO library has three states for a
+``FILE *``. These are: _IOFBF for block buffered; _IOLBF for line buffered;
+and _IONBF for unbuffered. The STDIO lib will use block buffering when
+talking to a block file descriptor such as a pipe. This is usually not
+helpful for interactive programs. Short of recompiling your program to
+include fflush() everywhere or recompiling a custom stdio library there
+is not much a controlling application can do about this if talking over
+a pipe.
+
+The program may have put data in its output that remains unflushed
+because the output buffer is not full; then the program will go and
+deadlock while waiting for input -- because you never send it any
+because you are still waiting for its output (still stuck in the STDIO's
+output buffer).
+
+The answer is to use a pseudo-tty. A TTY device will force line
+buffering (as opposed to block buffering). Line buffering means that you
+will get each line when the child program sends a line feed. This
+corresponds to the way most interactive programs operate -- send a line
+of output then wait for a line of input.
+
+I put "answer" in quotes because it's ugly solution and because there is
+no POSIX standard for pseudo-TTY devices (even though they have a TTY
+standard...). What would make more sense to me would be to have some way
+to set a mode on a file descriptor so that it will tell the STDIO to be
+line-buffered. I have investigated, and I don't think there is a way to
+set the buffered state of a child process. The STDIO Library does not
+maintain any external state in the kernel or whatnot, so I don't think
+there is any way for you to alter it. I'm not quite sure how this
+line-buffered/block-buffered state change happens internally in the
+STDIO library. I think the STDIO lib looks at the file descriptor and
+decides to change behavior based on whether it's a TTY or a block file
+(see isatty()).
+
+I hope that this qualifies as helpful. Don't use a pipe to control
+another application.
+
+**Q: Can I do screen scraping with this thing?**
+
+A: That depends. If your application just does line-oriented output then
+this is easy. If it does screen-oriented output then it may work, but it
+could be hard. For example, trying to scrape data from the 'top' command
+would be hard. The top command repaints the text window.
+
+I am working on an ANSI / VT100 terminal emulator that will have methods
+to get characters from an arbitrary X,Y coordinate of the virtual screen.
+It works and you can play with it (see :mod:`pexpect.ANSI`), but I have
+no working examples at this time. 
diff --git a/third_party/pexpect/doc/Makefile b/third_party/pexpect/doc/Makefile
new file mode 100644
index 0000000..ced8a68
--- /dev/null
+++ b/third_party/pexpect/doc/Makefile
@@ -0,0 +1,153 @@
+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS    =
+SPHINXBUILD   = sphinx-build
+PAPER         =
+BUILDDIR      = _build
+
+# Internal variables.
+PAPEROPT_a4     = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+ALLSPHINXOPTS   = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+# the i18n builder cannot share the environment and doctrees with the others
+I18NSPHINXOPTS  = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
+
+.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
+
+help:
+	@echo "Please use \`make <target>' where <target> is one of"
+	@echo "  html       to make standalone HTML files"
+	@echo "  dirhtml    to make HTML files named index.html in directories"
+	@echo "  singlehtml to make a single large HTML file"
+	@echo "  pickle     to make pickle files"
+	@echo "  json       to make JSON files"
+	@echo "  htmlhelp   to make HTML files and a HTML help project"
+	@echo "  qthelp     to make HTML files and a qthelp project"
+	@echo "  devhelp    to make HTML files and a Devhelp project"
+	@echo "  epub       to make an epub"
+	@echo "  latex      to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+	@echo "  latexpdf   to make LaTeX files and run them through pdflatex"
+	@echo "  text       to make text files"
+	@echo "  man        to make manual pages"
+	@echo "  texinfo    to make Texinfo files"
+	@echo "  info       to make Texinfo files and run them through makeinfo"
+	@echo "  gettext    to make PO message catalogs"
+	@echo "  changes    to make an overview of all changed/added/deprecated items"
+	@echo "  linkcheck  to check all external links for integrity"
+	@echo "  doctest    to run all doctests embedded in the documentation (if enabled)"
+
+clean:
+	-rm -rf $(BUILDDIR)/*
+
+html:
+	$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
+	@echo
+	@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
+
+dirhtml:
+	$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
+	@echo
+	@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
+
+singlehtml:
+	$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
+	@echo
+	@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
+
+pickle:
+	$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
+	@echo
+	@echo "Build finished; now you can process the pickle files."
+
+json:
+	$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
+	@echo
+	@echo "Build finished; now you can process the JSON files."
+
+htmlhelp:
+	$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
+	@echo
+	@echo "Build finished; now you can run HTML Help Workshop with the" \
+	      ".hhp project file in $(BUILDDIR)/htmlhelp."
+
+qthelp:
+	$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
+	@echo
+	@echo "Build finished; now you can run "qcollectiongenerator" with the" \
+	      ".qhcp project file in $(BUILDDIR)/qthelp, like this:"
+	@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Pexpect.qhcp"
+	@echo "To view the help file:"
+	@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Pexpect.qhc"
+
+devhelp:
+	$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
+	@echo
+	@echo "Build finished."
+	@echo "To view the help file:"
+	@echo "# mkdir -p $$HOME/.local/share/devhelp/Pexpect"
+	@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Pexpect"
+	@echo "# devhelp"
+
+epub:
+	$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
+	@echo
+	@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
+
+latex:
+	$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+	@echo
+	@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
+	@echo "Run \`make' in that directory to run these through (pdf)latex" \
+	      "(use \`make latexpdf' here to do that automatically)."
+
+latexpdf:
+	$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+	@echo "Running LaTeX files through pdflatex..."
+	$(MAKE) -C $(BUILDDIR)/latex all-pdf
+	@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
+
+text:
+	$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
+	@echo
+	@echo "Build finished. The text files are in $(BUILDDIR)/text."
+
+man:
+	$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
+	@echo
+	@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
+
+texinfo:
+	$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
+	@echo
+	@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
+	@echo "Run \`make' in that directory to run these through makeinfo" \
+	      "(use \`make info' here to do that automatically)."
+
+info:
+	$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
+	@echo "Running Texinfo files through makeinfo..."
+	make -C $(BUILDDIR)/texinfo info
+	@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
+
+gettext:
+	$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
+	@echo
+	@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
+
+changes:
+	$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
+	@echo
+	@echo "The overview file is in $(BUILDDIR)/changes."
+
+linkcheck:
+	$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
+	@echo
+	@echo "Link check complete; look for any errors in the above output " \
+	      "or in $(BUILDDIR)/linkcheck/output.txt."
+
+doctest:
+	$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
+	@echo "Testing of doctests in the sources finished, look at the " \
+	      "results in $(BUILDDIR)/doctest/output.txt."
diff --git a/third_party/pexpect/doc/api/ANSI.rst b/third_party/pexpect/doc/api/ANSI.rst
new file mode 100644
index 0000000..064563d
--- /dev/null
+++ b/third_party/pexpect/doc/api/ANSI.rst
@@ -0,0 +1,14 @@
+ANSI - ANSI (VT100) terminal emulator
+=====================================
+
+.. automodule:: pexpect.ANSI
+
+.. autoclass:: term
+   :show-inheritance:
+
+.. autoclass:: ANSI
+   :show-inheritance:
+
+   .. automethod:: write_ch
+   .. automethod:: write
+   .. automethod:: process
\ No newline at end of file
diff --git a/third_party/pexpect/doc/api/fdpexpect.rst b/third_party/pexpect/doc/api/fdpexpect.rst
new file mode 100644
index 0000000..8321454
--- /dev/null
+++ b/third_party/pexpect/doc/api/fdpexpect.rst
@@ -0,0 +1,22 @@
+fdpexpect - use pexpect with a file descriptor
+==============================================
+
+.. automodule:: pexpect.fdpexpect
+
+fdspawn class
+-------------
+
+.. autoclass:: fdspawn
+   :show-inheritance:
+
+   .. automethod:: __init__
+   .. automethod:: isalive
+   .. automethod:: close
+
+   .. note::
+      :class:`fdspawn` inherits all of the methods of :class:`~pexpect.spawn`, 
+      but not all of them can be used, especially if the file descriptor is not
+      a terminal. Some methods may do nothing (e.g. :meth:`~fdspawn.kill`), while
+      others will raise an exception (e.g. :meth:`~fdspawn.terminate`).
+      This behaviour might be made more consistent in the future, so try to
+      avoid relying on it.
\ No newline at end of file
diff --git a/third_party/pexpect/doc/api/index.rst b/third_party/pexpect/doc/api/index.rst
new file mode 100644
index 0000000..1a6a6ae
--- /dev/null
+++ b/third_party/pexpect/doc/api/index.rst
@@ -0,0 +1,12 @@
+API documentation
+=================
+
+.. toctree::
+   :maxdepth: 2
+
+   pexpect
+   fdpexpect
+   replwrap
+   pxssh
+   screen
+   ANSI
diff --git a/third_party/pexpect/doc/api/pexpect.rst b/third_party/pexpect/doc/api/pexpect.rst
new file mode 100644
index 0000000..565f0ef
--- /dev/null
+++ b/third_party/pexpect/doc/api/pexpect.rst
@@ -0,0 +1,111 @@
+Core pexpect components
+=======================
+
+.. automodule:: pexpect
+
+spawn class
+-----------
+
+.. autoclass:: spawn
+
+   .. automethod:: __init__
+   .. automethod:: expect
+   .. automethod:: expect_exact
+   .. automethod:: expect_list
+   .. automethod:: compile_pattern_list
+   .. automethod:: send
+   .. automethod:: sendline
+   .. automethod:: write
+   .. automethod:: writelines
+   .. automethod:: sendcontrol
+   .. automethod:: sendeof
+   .. automethod:: sendintr
+   .. automethod:: read
+   .. automethod:: readline
+   .. automethod:: read_nonblocking
+   .. automethod:: eof
+   .. automethod:: interact
+
+   .. attribute:: logfile
+                  logfile_read
+                  logfile_send
+
+      Set these to a Python file object (or :data:`sys.stdout`) to log all
+      communication, data read from the child process, or data sent to the child
+      process.
+
+      .. note::
+
+         With a :class:`spawn` instance, the log files should be open for
+         writing binary data. With a :class:`spawnu` instance, they should
+         be open for writing unicode text.
+
+Controlling the child process
+`````````````````````````````
+
+.. class:: spawn
+
+   .. automethod:: kill
+   .. automethod:: terminate
+   .. automethod:: isalive
+   .. automethod:: wait
+   .. automethod:: close
+   .. automethod:: getwinsize
+   .. automethod:: setwinsize
+   .. automethod:: getecho
+   .. automethod:: setecho
+   .. automethod:: waitnoecho
+
+   .. attribute:: pid
+
+      The process ID of the child process.
+
+   .. attribute:: child_fd
+
+      The file descriptor used to communicate with the child process.
+
+.. _unicode:
+
+Handling unicode
+````````````````
+
+For backwards compatibility, :class:`spawn` can handle some Unicode: its
+send methods will encode arbitrary unicode as UTF-8 before sending it to the
+child process, and its expect methods can accept ascii-only unicode strings.
+However, for a proper unicode API to a subprocess, use this subclass:
+
+.. autoclass:: spawnu
+   :show-inheritance:
+
+There is also a :func:`runu` function, the unicode counterpart to :func:`run`.
+
+.. note::
+
+   Unicode handling with pexpect works the same way on Python 2 and 3, despite
+   the difference in names. I.e.:
+
+   - :class:`spawn` works with ``str`` on Python 2, and :class:`bytes` on Python 3,
+   - :class:`spawnu` works with ``unicode`` on Python 2, and :class:`str` on Python 3.
+
+run function
+------------
+
+.. autofunction:: run
+
+.. autofunction:: runu
+
+Exceptions
+----------
+
+.. autoclass:: EOF
+
+.. autoclass:: TIMEOUT
+
+.. autoclass:: ExceptionPexpect
+
+Utility functions
+-----------------
+
+.. autofunction:: which
+
+.. autofunction:: split_command_line
diff --git a/third_party/pexpect/doc/api/pxssh.rst b/third_party/pexpect/doc/api/pxssh.rst
new file mode 100644
index 0000000..b947f4b
--- /dev/null
+++ b/third_party/pexpect/doc/api/pxssh.rst
@@ -0,0 +1,34 @@
+pxssh - control an SSH session
+==============================
+
+.. automodule:: pexpect.pxssh
+
+.. autoclass:: ExceptionPxssh
+
+pxssh class
+-----------
+
+.. autoclass:: pxssh
+
+   .. automethod:: __init__
+
+   .. attribute:: PROMPT
+
+      The regex pattern to search for to find the prompt. If you call :meth:`login`
+      with ``auto_prompt_reset=False``, you must set this attribute manually.
+
+   .. attribute:: force_password
+
+      If this is set to True, public key authentication is disabled, forcing the
+      server to ask for a password. Note that the sysadmin can disable password
+      logins, in which case this won't work.
+
+   .. attribute:: options
+
+      The dictionary of user specified SSH options, eg, ``options = dict(StrictHostKeyChecking="no", UserKnownHostsFile="/dev/null")``
+
+   .. automethod:: login
+   .. automethod:: logout
+   .. automethod:: prompt
+   .. automethod:: sync_original_prompt
+   .. automethod:: set_unique_prompt
diff --git a/third_party/pexpect/doc/api/replwrap.rst b/third_party/pexpect/doc/api/replwrap.rst
new file mode 100644
index 0000000..bf44a94
--- /dev/null
+++ b/third_party/pexpect/doc/api/replwrap.rst
@@ -0,0 +1,26 @@
+replwrap - Control read-eval-print-loops
+========================================
+
+.. automodule:: pexpect.replwrap
+
+.. versionadded:: 3.3
+
+.. autoclass:: REPLWrapper
+
+   .. automethod:: run_command
+
+.. data:: PEXPECT_PROMPT
+
+   A string that can be used as a prompt, and is unlikely to be found in output.
+
+Using the objects above, it is easy to wrap a REPL. For instance, to use a
+Python shell::
+
+    py = REPLWrapper("python", ">>> ", "import sys; sys.ps1={!r}; sys.ps2={!r}")
+    py.run_command("4+7")
+
+Convenience functions are provided for Python and bash shells:
+
+.. autofunction:: python
+
+.. autofunction:: bash
diff --git a/third_party/pexpect/doc/api/screen.rst b/third_party/pexpect/doc/api/screen.rst
new file mode 100644
index 0000000..8268fb9
--- /dev/null
+++ b/third_party/pexpect/doc/api/screen.rst
@@ -0,0 +1,10 @@
+screen - manage a virtual 'screen'
+==================================
+
+.. automodule:: pexpect.screen
+
+.. autoclass:: screen
+   :members:
+
+   .. automethod:: __init__
+   .. automethod:: __str__
\ No newline at end of file
diff --git a/third_party/pexpect/doc/clean.css b/third_party/pexpect/doc/clean.css
new file mode 100644
index 0000000..e8d98dd
--- /dev/null
+++ b/third_party/pexpect/doc/clean.css
@@ -0,0 +1,103 @@
+
+body {
+	margin:0px;
+	padding:0px;
+	font-family:verdana, arial, helvetica, sans-serif; 
+	color:#333;
+	background-color:white;
+	}
+pre {
+  background: #eeeeee;
+  border: 1px solid #888888;
+  color: black;
+  padding: 1em;
+  white-space: pre;
+}
+h1 {
+	margin:5px 0px 5px 0px;
+	padding:0px;
+	font-size:20px;
+	line-height:28px;
+	font-weight:900;
+	color:#44f;
+	}
+h2 {
+	margin:5px 0px 5px 0px;
+	padding:0px;
+	font-size:17px;
+	line-height:28px;
+	font-weight:900;
+	color:#226;
+	}
+h3 {
+	margin:5px 0px 5px 0px;
+	padding:0px;
+	font-size:15px;
+	line-height:28px;
+	font-weight:900;
+	}
+p
+{
+	margin:0px 0px 16px 0px;
+	font:11px/20px verdana, arial, helvetica, sans-serif;
+	padding:0px;
+}
+table 
+{
+    font-size: 10pt; 
+	color: #000000;
+}
+td{border:1px solid #999;}
+
+table.pymenu {color: #000000; background-color: #99ccff}
+th.pymenu {color: #ffffff; background-color: #003366}
+
+.code 
+{
+	font-family: "Lucida Console", monospace; font-weight: bold;
+	color: #007700; background-color: #eeeeee
+}
+
+#Content>p {margin:0px;}
+#Content>p+p {text-indent:30px;}
+
+a {
+	text-decoration:none;
+	font-weight:600;
+	font-family:verdana, arial, helvetica, sans-serif;
+	color: #900;
+}
+//a:link {color:#09c;}
+//a x:visited {color:#07a;}
+a:hover {background-color:#ee0;}
+
+#Header {
+	margin:10px 0px 10px 0px;
+	padding:10px 0px 10px 20px;
+	/* For IE5/Win's benefit height = [correct height] + [top padding] + [top and bottom border widths] */
+	height:33px; /* 14px + 17px + 2px = 33px */
+	border-style:solid;
+	border-color:black;
+	border-width:1px 0px; /* top and bottom borders: 1px; left and right borders: 0px */
+	line-height:33px;
+	background-color:#eee;
+	height:66px; /* the correct height */
+	}
+
+#Content {
+	margin:0px 210px 50px 10px;
+	padding:10px;
+	}
+
+#Menu {
+	position:absolute;
+	top:100px;
+	right:20px;
+	width:172px;
+	padding:10px;
+	background-color:#eee;
+	border:1px solid #999; // dashed #999;
+	line-height:17px;
+	width:150px;
+	font-size:11px;
+	}
diff --git a/third_party/pexpect/doc/commonissues.rst b/third_party/pexpect/doc/commonissues.rst
new file mode 100644
index 0000000..26d0e2b
--- /dev/null
+++ b/third_party/pexpect/doc/commonissues.rst
@@ -0,0 +1,115 @@
+Common problems
+===============
+
+Threads
+-------
+
+On Linux (RH 8) you cannot spawn a child from a different thread and pass the
+handle back to a worker thread. The child is successfully spawned but you can't
+interact with it. The only way to make it work is to spawn and interact with the
+child all in the same thread. [Adam Kerrison]
+
+Timing issue with send() and sendline()
+---------------------------------------
+
+This problem has been addressed and should not affect most users.
+
+It is sometimes possible to read an echo of the string sent with
+:meth:`~pexpect.spawn.send` and :meth:`~pexpect.spawn.sendline`. If you call
+:meth:`~pexpect.spawn.send` and then immediately call :meth:`~pexpect.spawn.readline`,
+you may get part of your output echoed back. You may read back what you just
+wrote even if the child application does not explicitly echo it. Timing is
+critical. This could be a security issue when talking to an application that
+asks for a password; otherwise, this does not seem like a big deal. But why do
+TTYs do this?
+
+People usually report this when they are trying to control SSH or some other
+login. For example, if your code looks something like this::
+
+    child.expect ('[pP]assword:')
+    child.sendline (my_password)
+
+
+1. SSH prints "password:" prompt to the user.
+2. SSH turns off echo on the TTY device.
+3. SSH waits for user to enter a password.
+
+When scripting with Pexpect what can happen is that Pexpect will respond to the
+"password:" prompt before SSH has had time to turn off TTY echo. In other words,
+Pexpect sends the password between steps 1. and 2., so the password gets echoed
+back to the TTY. I would call this an SSH bug.
+
+Pexpect now automatically adds a short delay before sending data to a child
+process. This more closely mimics what happens in the usual human-to-app
+interaction. The delay can be tuned with the ``delaybeforesend`` attribute of the
+spawn class. In general, this fixes the problem for everyone and so this should
+not be an issue for most users. For some applications you might with to turn it
+off::
+
+    child = pexpect.spawn ("ssh user at example.com")
+    child.delaybeforesend = 0
+
+Timing issue with isalive()
+---------------------------
+
+Reading the state of :meth:`~pexpect.spawn.isalive` immediately after a child
+exits may sometimes return 1. This is a race condition. The child has closed its
+file descriptor, but has not yet fully exited before Pexpect's
+:meth:`~pexpect.spawn.isalive` executes. Addings a slight delay before the
+:meth:`~pexpect.spawn.isalive` call will help. For example::
+
+    child = pexpect.spawn('ls')
+    child.expect(pexpect.EOF)
+    time.sleep(0.1)
+    print child.isalive()
+
+Truncated output just before child exits
+----------------------------------------
+
+So far I have seen this only on older versions of Apple's MacOS X. If the child
+application quits it may not flush its output buffer. This means that your
+Pexpect application will receive an EOF even though it should have received a
+little more data before the child died. This is not generally a problem when
+talking to interactive child applications. One example where it is a problem is
+when trying to read output from a program like *ls*. You may receive most of the
+directory listing, but the last few lines will get lost before you receive an EOF.
+The reason for this is that *ls* runs; completes its task; and then exits. The
+buffer is not flushed before exit so the last few lines are lost. The following
+example demonstrates the problem::
+
+    child = pexpect.spawn('ls -l')
+    child.expect(pexpect.EOF)
+    print child.before       
+
+Controlling SSH on Solaris
+--------------------------
+
+Pexpect does not yet work perfectly on Solaris. One common problem is that SSH
+sometimes will not allow TTY password authentication. For example, you may
+expect SSH to ask you for a password using code like this::
+
+    child = pexpect.spawn('ssh user at example.com')
+    child.expect('password')
+    child.sendline('mypassword')
+
+You may see the following error come back from a spawned child SSH::
+
+    Permission denied (publickey,keyboard-interactive). 
+
+This means that SSH thinks it can't access the TTY to ask you for your password.
+The only solution I have found is to use public key authentication with SSH.
+This bypasses the need for a password. I'm not happy with this solution. The
+problem is due to poor support for Solaris Pseudo TTYs in the Python Standard
+Library.
+
+child does not receive full input, emits BEL
+--------------------------------------------
+
+You may notice when running for example cat(1) or base64(1), when sending a
+very long input line, that it is not fully received, and the BEL ('\a') may
+be found in output.
+
+By default the child terminal matches the parent, which is often in "canonical
+mode processing". You may wish to disable this mode. The exact limit of a line
+varies by operating system, and details of disabling canonical mode may be
+found in the docstring of :meth:`~pexpect.spawn.send`.
diff --git a/third_party/pexpect/doc/conf.py b/third_party/pexpect/doc/conf.py
new file mode 100644
index 0000000..3dd5d3e
--- /dev/null
+++ b/third_party/pexpect/doc/conf.py
@@ -0,0 +1,250 @@
+# -*- coding: utf-8 -*-
+#
+# Pexpect documentation build configuration file, created by
+# sphinx-quickstart on Tue Sep 17 11:05:11 2013.
+#
+# This file is execfile()d with the current directory set to its containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys, os
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+sys.path.insert(0, os.path.abspath('sphinxext'))
+
+# -- General configuration -----------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be extensions
+# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
+extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx',
+              'sphinx.ext.viewcode', 'github',  # for easy GitHub links
+              ]
+
+github_project_url = "https://github.com/pexpect/pexpect"
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8-sig'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = u'Pexpect'
+copyright = u'2013, Noah Spurrier and contributors'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = '3.3'
+# The full version, including alpha/beta/rc tags.
+release = '3.3'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+exclude_patterns = ['_build']
+
+# The reST default role (used for this markup: `text`) to use for all documents.
+#default_role = None
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+#add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+
+# -- Options for HTML output ---------------------------------------------------
+
+# The theme to use for HTML and HTML Help pages.  See the documentation for
+# a list of builtin themes.
+html_theme = 'default'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further.  For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {}
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = []
+
+# The name for this set of Sphinx documents.  If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar.  Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = ['_static']
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_domain_indices = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+#html_show_sourcelink = True
+
+# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
+#html_show_sphinx = True
+
+# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
+#html_show_copyright = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it.  The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# This is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = None
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'Pexpectdoc'
+
+
+# -- Options for LaTeX output --------------------------------------------------
+
+latex_elements = {
+# The paper size ('letterpaper' or 'a4paper').
+#'papersize': 'letterpaper',
+
+# The font size ('10pt', '11pt' or '12pt').
+#'pointsize': '10pt',
+
+# Additional stuff for the LaTeX preamble.
+#'preamble': '',
+}
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title, author, documentclass [howto/manual]).
+latex_documents = [
+  ('index', 'Pexpect.tex', u'Pexpect Documentation',
+   u'Noah Spurrier and contributors', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# If true, show page references after internal links.
+#latex_show_pagerefs = False
+
+# If true, show URL addresses after external links.
+#latex_show_urls = False
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_domain_indices = True
+
+
+# -- Options for manual page output --------------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+    ('index', 'pexpect', u'Pexpect Documentation',
+     [u'Noah Spurrier and contributors'], 1)
+]
+
+# If true, show URL addresses after external links.
+#man_show_urls = False
+
+
+# -- Options for Texinfo output ------------------------------------------------
+
+# Grouping the document tree into Texinfo files. List of tuples
+# (source start file, target name, title, author,
+#  dir menu entry, description, category)
+texinfo_documents = [
+  ('index', 'Pexpect', u'Pexpect Documentation',
+   u'Noah Spurrier and contributors', 'Pexpect', 'One line description of project.',
+   'Miscellaneous'),
+]
+
+# Documents to append as an appendix to all manuals.
+#texinfo_appendices = []
+
+# If false, no module index is generated.
+#texinfo_domain_indices = True
+
+# How to display URL addresses: 'footnote', 'no', or 'inline'.
+#texinfo_show_urls = 'footnote'
+
+
+# Example configuration for intersphinx: refer to the Python standard library.
+intersphinx_mapping = {'http://docs.python.org/3/': None}
diff --git a/third_party/pexpect/doc/examples.rst b/third_party/pexpect/doc/examples.rst
new file mode 100644
index 0000000..6338b5c
--- /dev/null
+++ b/third_party/pexpect/doc/examples.rst
@@ -0,0 +1,63 @@
+Examples
+========
+
+Under the distribution tarball directory you should find an "examples" directory.
+This is the best way to learn to use Pexpect. See the descriptions of Pexpect
+Examples.
+
+`topip.py <https://github.com/pexpect/pexpect/blob/master/examples/topip.py>`_
+  This runs `netstat` on a local or remote server. It calculates some simple
+  statistical information on the number of external inet connections. This can
+  be used to detect if one IP address is taking up an excessive number of
+  connections. It can also send an email alert if a given IP address exceeds a
+  threshold between runs of the script. This script can be used as a drop-in
+  Munin plugin or it can be used stand-alone from cron. I used this on a busy
+  web server that would sometimes get hit with denial of service attacks. This
+  made it easy to see if a script was opening many multiple connections. A
+  typical browser would open fewer than 10 connections at once. A script might
+  open over 100 simultaneous connections.
+
+`hive.py <https://github.com/pexpect/pexpect/blob/master/examples/hive.py>`_
+  This script creates SSH connections to a list of hosts that you provide.
+  Then you are given a command line prompt. Each shell command that you
+  enter is sent to all the hosts. The response from each host is collected
+  and printed. For example, you could connect to a dozen different
+  machines and reboot them all at once.
+
+`script.py <https://github.com/pexpect/pexpect/blob/master/examples/script.py>`_
+  This implements a command similar to the classic BSD "script" command.
+  This will start a subshell and log all input and output to a file.
+  This demonstrates the :meth:`~pexpect.spawn.interact` method of Pexpect.
+
+`ftp.py <https://github.com/pexpect/pexpect/blob/master/examples/ftp.py>`_
+  This demonstrates an FTP "bookmark". This connects to an ftp site;
+  does a few ftp tasks; and then gives the user interactive control over
+  the session. In this case the "bookmark" is to a directory on the
+  OpenBSD ftp server. It puts you in the i386 packages directory. You
+  can easily modify this for other sites. This demonstrates the
+  :meth:`~pexpect.spawn.interact` method of Pexpect.
+
+`monitor.py <https://github.com/pexpect/pexpect/blob/master/examples/monitor.py>`_
+  This runs a sequence of commands on a remote host using SSH. It runs a
+  simple system checks such as uptime and free to monitor the state of
+  the remote host.
+
+`passmass.py <https://github.com/pexpect/pexpect/blob/master/examples/passmass.py>`_
+  This will login to each given server and change the password of the
+  given user. This demonstrates scripting logins and passwords.
+
+`python.py <https://github.com/pexpect/pexpect/blob/master/examples/python.py>`_
+  This starts the python interpreter and prints the greeting message
+  backwards. It then gives the user iteractive control of Python. It's
+  pretty useless!
+
+`ssh_tunnel.py <https://github.com/pexpect/pexpect/blob/master/examples/ssh_tunnel.py>`_
+  This starts an SSH tunnel to a remote machine. It monitors the
+  connection and restarts the tunnel if it goes down.
+
+`uptime.py <https://github.com/pexpect/pexpect/blob/master/examples/uptime.py>`_
+  This will run the uptime command and parse the output into variables.
+  This demonstrates using a single regular expression to match the
+  output of a command and capturing different variable in match groups.
+  The grouping regular expression handles a wide variety of different
+  uptime formats. 
diff --git a/third_party/pexpect/doc/history.rst b/third_party/pexpect/doc/history.rst
new file mode 100644
index 0000000..c9d5640
--- /dev/null
+++ b/third_party/pexpect/doc/history.rst
@@ -0,0 +1,215 @@
+History
+=======
+
+Releases
+--------
+
+Version 4.0
+```````````
+
+* Integration with :mod:`asyncio`: passing ``async=True`` to :meth:`~.expect`,
+  :meth:`~.expect_exact` or :meth:`~.expect_list` will make them return a
+  coroutine. You can get the result using ``yield from``, or wrap it in an
+  :class:`asyncio.Task`. This allows the event loop to do other things while
+  waiting for output that matches a pattern.
+
+Version 3.4
+```````````
+* Fix regression that prevented executable, but unreadable files from
+  being found when not specified by absolute path -- such as
+  /usr/bin/sudo (:ghissue:`104`).
+* Fixed regression when executing pexpect with some prior releases of
+  the multiprocessing module where stdin has been closed (:ghissue:`86`).
+
+Version 3.3
+```````````
+
+* Added a mechanism to wrap REPLs, or shells, in an object which can conveniently
+  be used to send commands and wait for the output (:mod:`pexpect.replwrap`).
+* Fixed issue where pexpect would attempt to execute a directory because
+  it has the 'execute' bit set (:ghissue:`37`).
+* Removed the ``pexpect.psh`` module. This was never documented, and we found
+  no evidence that people use it. The new :mod:`pexpect.replwrap` module
+  provides a more flexible alternative.
+* Fixed ``TypeError: got <type 'str'> ('\r\n') as pattern`` in :meth:`spawnu.readline`
+  method (:ghissue:`67`).
+* Fixed issue where EOF was not correctly detected in :meth:`~.interact`, causing
+  a repeating loop of output on Linux, and blocking before EOF on BSD and
+  Solaris (:ghissue:`49`).
+* Several Solaris (SmartOS) bugfixes, preventing :exc:`IOError` exceptions, especially
+  when used with cron(1) (:ghissue:`44`).
+* Added new keyword argument ``echo=True`` for :class:`spawn`.  On SVR4-like
+  systems, the method :meth:`~.isatty` will always return *False*: the child pty
+  does not appear as a terminal.  Therefore, :meth:`~.setecho`, :meth:`~.getwinsize`,
+  :meth:`~.setwinsize`, and :meth:`~.waitnoecho` are not supported on those platforms.
+
+After this, we intend to start working on a bigger refactoring of the code, to
+be released as Pexpect 4. There may be more bugfix 3.x releases, however.
+
+Version 3.2
+```````````
+
+* Fix exception handling from :func:`select.select` on Python 2 (:ghpull:`38`).
+  This was accidentally broken in the previous release when it was fixed for
+  Python 3.
+* Removed a workaround for ``TIOCSWINSZ`` on very old systems, which was causing
+  issues on some BSD systems (:ghpull:`40`).
+* Fixed an issue with exception handling in :mod:`~pexpect.pxssh` (:ghpull:`43`)
+
+The documentation for :mod:`~pexpect.pxssh` was improved.
+
+Version 3.1
+```````````
+
+* Fix an issue that prevented importing pexpect on Python 3 when ``sys.stdout``
+  was reassigned (:ghissue:`30`).
+* Improve prompt synchronisation in :mod:`~pexpect.pxssh` (:ghpull:`28`).
+* Fix pickling exception instances (:ghpull:`34`).
+* Fix handling exceptions from :func:`select.select` on Python 3 (:ghpull:`33`).
+
+The examples have also been cleaned up somewhat - this will continue in future
+releases.
+
+Version 3.0
+```````````
+
+The new major version number doesn't indicate any deliberate API incompatibility.
+We have endeavoured to avoid breaking existing APIs. However, pexpect is under
+new maintenance after a long dormancy, so some caution is warranted.
+
+* A new :ref:`unicode API <unicode>` was introduced.
+* Python 3 is now supported, using a single codebase.
+* Pexpect now requires at least Python 2.6 or 3.2.
+* The modules other than pexpect, such as :mod:`pexpect.fdpexpect` and
+  :mod:`pexpect.pxssh`, were moved into the pexpect package. For now, wrapper
+  modules are installed to the old locations for backwards compatibility (e.g.
+  ``import pxssh`` will still work), but these will be removed at some point in
+  the future.
+* Ignoring ``SIGHUP`` is now optional - thanks to Kimmo Parviainen-Jalanko for
+  the patch.
+
+We also now have `docs on ReadTheDocs <http://pexpect.readthedocs.org/>`_,
+and `continuous integration on Travis CI <https://travis-ci.org/pexpect/pexpect>`_.
+
+Version 2.4
+```````````
+
+* Fix a bug regarding making the pty the controlling terminal when the process
+  spawning it is not, actually, a terminal (such as from cron)
+
+Version 2.3
+```````````
+
+* Fixed OSError exception when a pexpect object is cleaned up. Previously, you
+  might have seen this exception::
+
+      Exception exceptions.OSError: (10, 'No child processes')
+      in <bound method spawn.__del__ of <pexpect.spawn instance at 0xd248c>> ignored
+
+  You should not see that anymore. Thanks to Michael Surette.
+* Added support for buffering reads. This greatly improves speed when trying to
+  match long output from a child process. When you create an instance of the spawn
+  object you can then set a buffer size. For now you MUST do the following to turn
+  on buffering -- it may be on by default in future version::
+
+      child = pexpect.spawn ('my_command')
+      child.maxread=1000 # Sets buffer to 1000 characters.
+
+* I made a subtle change to the way TIMEOUT and EOF exceptions behave.
+  Previously you could either expect these states in which case pexpect
+  will not raise an exception, or you could just let pexpect raise an
+  exception when these states were encountered. If you expected the
+  states then the ``before`` property was set to everything before the
+  state was encountered, but if you let pexpect raise the exception then
+  ``before`` was not set. Now, the ``before`` property will get set either
+  way you choose to handle these states.
+* The spawn object now provides iterators for a *file-like interface*.
+  This makes Pexpect a more complete file-like object. You can now write
+  code like this::
+
+      child = pexpect.spawn ('ls -l')
+      for line in child:
+          print line
+
+* write and writelines() no longer return a value. Use send() if you need that
+  functionality. I did this to make the Spawn object more closely match a
+  file-like object.
+* Added the attribute ``exitstatus``. This will give the exit code returned
+  by the child process. This will be set to ``None`` while the child is still
+  alive. When ``isalive()`` returns 0 then ``exitstatus`` will be set.
+* Made a few more tweaks to ``isalive()`` so that it will operate more
+  consistently on different platforms. Solaris is the most difficult to support.
+* You can now put ``TIMEOUT`` in a list of expected patterns. This is just like
+  putting ``EOF`` in the pattern list. Expecting for a ``TIMEOUT`` may not be
+  used as often as ``EOF``, but this makes Pexpect more consistent.
+* Thanks to a suggestion and sample code from Chad J. Schroeder I added the ability
+  for Pexpect to operate on a file descriptor that is already open. This means that
+  Pexpect can be used to control streams such as those from serial port devices. Now,
+  you just pass the integer file descriptor as the "command" when constructing a
+  spawn open. For example on a Linux box with a modem on ttyS1::
+
+      fd = os.open("/dev/ttyS1", os.O_RDWR|os.O_NONBLOCK|os.O_NOCTTY)
+      m = pexpect.spawn(fd) # Note integer fd is used instead of usual string.
+      m.send("+++") # Escape sequence
+      m.send("ATZ0\r") # Reset modem to profile 0
+      rval = m.expect(["OK", "ERROR"])
+
+* ``read()`` was renamed to ``read_nonblocking()``. Added new ``read()`` method
+  that matches file-like object interface. In general, you should not notice
+  the difference except that ``read()`` no longer allows you to directly set the
+  timeout value. I hope this will not effect any existing code. Switching to
+  ``read_nonblocking()`` should fix existing code.
+* Changed the name of ``set_echo()`` to ``setecho()``.
+* Changed the name of ``send_eof()`` to ``sendeof()``.
+* Modified ``kill()`` so that it checks to make sure the pid ``isalive()``.
+* modified ``spawn()`` (really called from ``__spawn()``) so that it does not
+  raise an expection if ``setwinsize()`` fails. Some platforms such as Cygwin
+  do not like setwinsize. This was a constant problem and since it is not a
+  critical feature I decided to just silence the error.  Normally I don't like
+  to do that, but in this case I'm making an exception.
+* Added a method ``close()`` that does what you think. It closes the file
+  descriptor of the child application. It makes no attempt to actually kill the
+  child or wait for its status.
+* Add variables ``__version__`` and ``__revision__`` (from cvs) to the pexpect
+  modules.  This is mainly helpful to me so that I can make sure that I'm testing
+  with the right version instead of one already installed.
+* ``log_open()`` and ``log_close(`` have been removed. Now use ``setlog()``.
+  The ``setlog()`` method takes a file object. This is far more flexible than
+  the previous log method. Each time data is written to the file object it will
+  be flushed. To turn logging off simply call ``setlog()`` with None.
+* renamed the ``isAlive()`` method to ``isalive()`` to match the more typical
+  naming style in Python. Also the technique used to detect child process
+  status has been drastically modified. Previously I did some funky stuff
+  with signals which caused indigestion in other Python modules on some
+  platforms. It was a big headache. It still is, but I think it works
+  better now.
+* attribute ``matched`` renamed to ``after``
+* new attribute ``match``
+* The ``expect_eof()`` method is gone. You can now simply use the
+  ``expect()`` method to look for EOF.
+* **Pexpect works on OS X**, but the nature of the quirks cause many of the
+  tests to fail. See bugs. (Incomplete Child Output). The problem is more
+  than minor, but Pexpect is still more than useful for most tasks.
+* **Solaris**: For some reason, the *second* time a pty file descriptor is created and
+  deleted it never gets returned for use. It does not effect the first time
+  or the third time or any time after that. It's only the second time. This
+  is weird... This could be a file descriptor leak, or it could be some
+  peculiarity of how Solaris recycles them. I thought it was a UNIX requirement
+  for the OS to give you the lowest available filedescriptor number. In any case,
+  this should not be a problem unless you create hundreds of pexpect instances...
+  It may also be a pty module bug.
+
+
+Moves and forks
+---------------
+
+* Pexpect development used to be hosted on Sourceforge.
+* In 2011, Thomas Kluyver forked pexpect as 'pexpect-u', to support
+  Python 3. He later decided he had taken the wrong approach with this.
+* In 2012, Noah Spurrier, the original author of Pexpect, moved the
+  project to Github, but was still too busy to develop it much.
+* In 2013, Thomas Kluyver and Jeff Quast forked Pexpect again, intending
+  to call the new fork Pexpected. Noah Spurrier agreed to let them use
+  the name Pexpect, so Pexpect versions 3 and above are based on this
+  fork, which now lives `here on Github <https://github.com/pexpect/pexpect>`_.
+
diff --git a/third_party/pexpect/doc/index.rst b/third_party/pexpect/doc/index.rst
new file mode 100644
index 0000000..0bcf862
--- /dev/null
+++ b/third_party/pexpect/doc/index.rst
@@ -0,0 +1,50 @@
+Pexpect version |version|
+=========================
+
+.. image:: https://travis-ci.org/pexpect/pexpect.png?branch=master
+   :target: https://travis-ci.org/pexpect/pexpect
+   :align: right
+   :alt: Build status
+
+Pexpect makes Python a better tool for controlling other
+applications.
+
+Pexpect is a pure Python module for spawning child applications;
+controlling them; and responding to expected patterns in their output.
+Pexpect works like Don Libes' Expect. Pexpect allows your script to
+spawn a child application and control it as if a human were typing
+commands.
+
+Pexpect can be used for automating interactive applications such as
+ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
+scripts for duplicating software package installations on different
+servers. It can be used for automated software testing. Pexpect is in
+the spirit of Don Libes' Expect, but Pexpect is pure Python. Unlike
+other Expect-like modules for Python, Pexpect does not require TCL or
+Expect nor does it require C extensions to be compiled. It should work
+on any platform that supports the standard Python pty module. The
+Pexpect interface was designed to be easy to use.
+
+Contents:
+
+.. toctree::
+   :maxdepth: 2
+
+   install
+   overview
+   api/index
+   examples
+   FAQ
+   commonissues
+   history
+
+Pexpect is developed `on Github <http://github.com/pexpect/pexpect>`_. Please
+report `issues <https://github.com/pexpect/pexpect/issues>`_ there as well.
+
+Indices and tables
+==================
+
+* :ref:`genindex`
+* :ref:`modindex`
+* :ref:`search`
+
diff --git a/third_party/pexpect/doc/install.rst b/third_party/pexpect/doc/install.rst
new file mode 100644
index 0000000..297acf3
--- /dev/null
+++ b/third_party/pexpect/doc/install.rst
@@ -0,0 +1,20 @@
+Installation
+============
+
+Pexpect is on PyPI, and can be installed with standard tools::
+
+    pip install pexpect
+
+Or::
+
+    easy_install pexpect
+
+Requirements
+------------
+
+This version of Pexpect requires Python 2.6 or 3.2 or above. For older
+versions of Python, continue using Pexpect 2.4.
+
+Pexpect only works on POSIX systems, where the :mod:`pty` module
+is present in the standard library. It may be possible to run it on Windows
+using `Cygwin <http://www.cygwin.com/>`_.
diff --git a/third_party/pexpect/doc/make.bat b/third_party/pexpect/doc/make.bat
new file mode 100644
index 0000000..448f147
--- /dev/null
+++ b/third_party/pexpect/doc/make.bat
@@ -0,0 +1,190 @@
+ at ECHO OFF
+
+REM Command file for Sphinx documentation
+
+if "%SPHINXBUILD%" == "" (
+	set SPHINXBUILD=sphinx-build
+)
+set BUILDDIR=_build
+set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% .
+set I18NSPHINXOPTS=%SPHINXOPTS% .
+if NOT "%PAPER%" == "" (
+	set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
+	set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS%
+)
+
+if "%1" == "" goto help
+
+if "%1" == "help" (
+	:help
+	echo.Please use `make ^<target^>` where ^<target^> is one of
+	echo.  html       to make standalone HTML files
+	echo.  dirhtml    to make HTML files named index.html in directories
+	echo.  singlehtml to make a single large HTML file
+	echo.  pickle     to make pickle files
+	echo.  json       to make JSON files
+	echo.  htmlhelp   to make HTML files and a HTML help project
+	echo.  qthelp     to make HTML files and a qthelp project
+	echo.  devhelp    to make HTML files and a Devhelp project
+	echo.  epub       to make an epub
+	echo.  latex      to make LaTeX files, you can set PAPER=a4 or PAPER=letter
+	echo.  text       to make text files
+	echo.  man        to make manual pages
+	echo.  texinfo    to make Texinfo files
+	echo.  gettext    to make PO message catalogs
+	echo.  changes    to make an overview over all changed/added/deprecated items
+	echo.  linkcheck  to check all external links for integrity
+	echo.  doctest    to run all doctests embedded in the documentation if enabled
+	goto end
+)
+
+if "%1" == "clean" (
+	for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
+	del /q /s %BUILDDIR%\*
+	goto end
+)
+
+if "%1" == "html" (
+	%SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The HTML pages are in %BUILDDIR%/html.
+	goto end
+)
+
+if "%1" == "dirhtml" (
+	%SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
+	goto end
+)
+
+if "%1" == "singlehtml" (
+	%SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
+	goto end
+)
+
+if "%1" == "pickle" (
+	%SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can process the pickle files.
+	goto end
+)
+
+if "%1" == "json" (
+	%SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can process the JSON files.
+	goto end
+)
+
+if "%1" == "htmlhelp" (
+	%SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can run HTML Help Workshop with the ^
+.hhp project file in %BUILDDIR%/htmlhelp.
+	goto end
+)
+
+if "%1" == "qthelp" (
+	%SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; now you can run "qcollectiongenerator" with the ^
+.qhcp project file in %BUILDDIR%/qthelp, like this:
+	echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Pexpect.qhcp
+	echo.To view the help file:
+	echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Pexpect.ghc
+	goto end
+)
+
+if "%1" == "devhelp" (
+	%SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished.
+	goto end
+)
+
+if "%1" == "epub" (
+	%SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The epub file is in %BUILDDIR%/epub.
+	goto end
+)
+
+if "%1" == "latex" (
+	%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
+	goto end
+)
+
+if "%1" == "text" (
+	%SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The text files are in %BUILDDIR%/text.
+	goto end
+)
+
+if "%1" == "man" (
+	%SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The manual pages are in %BUILDDIR%/man.
+	goto end
+)
+
+if "%1" == "texinfo" (
+	%SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo.
+	goto end
+)
+
+if "%1" == "gettext" (
+	%SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Build finished. The message catalogs are in %BUILDDIR%/locale.
+	goto end
+)
+
+if "%1" == "changes" (
+	%SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.The overview file is in %BUILDDIR%/changes.
+	goto end
+)
+
+if "%1" == "linkcheck" (
+	%SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Link check complete; look for any errors in the above output ^
+or in %BUILDDIR%/linkcheck/output.txt.
+	goto end
+)
+
+if "%1" == "doctest" (
+	%SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
+	if errorlevel 1 exit /b 1
+	echo.
+	echo.Testing of doctests in the sources finished, look at the ^
+results in %BUILDDIR%/doctest/output.txt.
+	goto end
+)
+
+:end
diff --git a/third_party/pexpect/doc/overview.rst b/third_party/pexpect/doc/overview.rst
new file mode 100644
index 0000000..a04e389
--- /dev/null
+++ b/third_party/pexpect/doc/overview.rst
@@ -0,0 +1,240 @@
+API Overview
+============
+
+Pexpect can be used for automating interactive applications such as ssh, ftp,
+mencoder, passwd, etc. The Pexpect interface was designed to be easy to use.
+
+Here is an example of Pexpect in action::
+
+    # This connects to the openbsd ftp site and
+    # downloads the recursive directory listing.
+    import pexpect
+    child = pexpect.spawn('ftp ftp.openbsd.org')
+    child.expect('Name .*: ')
+    child.sendline('anonymous')
+    child.expect('Password:')
+    child.sendline('noah at example.com')
+    child.expect('ftp> ')
+    child.sendline('lcd /tmp')
+    child.expect('ftp> ')
+    child.sendline('cd pub/OpenBSD')
+    child.expect('ftp> ')
+    child.sendline('get README')
+    child.expect('ftp> ')
+    child.sendline('bye')
+
+Obviously you could write an ftp client using Python's own :mod:`ftplib` module,
+but this is just a demonstration. You can use this technique with any application.
+This is especially handy if you are writing automated test tools.
+
+There are two important methods in Pexpect -- :meth:`~pexpect.spawn.expect` and
+:meth:`~pexpect.spawn.send` (or :meth:`~pexpect.spawn.sendline` which is
+like :meth:`~pexpect.spawn.send` with a linefeed). The :meth:`~pexpect.spawn.expect`
+method waits for the child application to return a given string. The string you
+specify is a regular expression, so you can match complicated patterns. The
+:meth:`~pexpect.spawn.send` method writes a string to the child application.
+From the child's point of view it looks just like someone typed the text from a
+terminal. After each call to :meth:`~pexpect.spawn.expect` the ``before`` and ``after``
+properties will be set to the text printed by child application. The ``before``
+property will contain all text up to the expected string pattern. The ``after``
+string will contain the text that was matched by the expected pattern.
+The match property is set to the `re match object <http://docs.python.org/3/library/re#match-objects>`_.
+
+An example of Pexpect in action may make things more clear. This example uses
+ftp to login to the OpenBSD site; list files in a directory; and then pass
+interactive control of the ftp session to the human user::
+
+    import pexpect
+    child = pexpect.spawn ('ftp ftp.openbsd.org')
+    child.expect ('Name .*: ')
+    child.sendline ('anonymous')
+    child.expect ('Password:')
+    child.sendline ('noah at example.com')
+    child.expect ('ftp> ')
+    child.sendline ('ls /pub/OpenBSD/')
+    child.expect ('ftp> ')
+    print child.before   # Print the result of the ls command.
+    child.interact()     # Give control of the child to the user.
+
+Special EOF and TIMEOUT patterns
+--------------------------------
+
+There are two special patterns to match the End Of File (:class:`~pexpect.EOF`)
+or a Timeout condition (:class:`~pexpect.TIMEOUT`). You you can pass these
+patterns to :meth:`~pexpect.spawn.expect`. These patterns are not regular
+expressions. Use them like predefined constants.
+
+If the child has died and you have read all the child's output then ordinarily
+:meth:`~pexpect.spawn.expect` will raise an :class:`~pexpect.EOF` exception.
+You can read everything up to the EOF without generating an exception by using
+the EOF pattern expect. In this case everything the child has output will be
+available in the ``before`` property.
+
+The pattern given to :meth:`~pexpect.spawn.expect` may be a regular expression
+or it may also be a list of regular expressions. This allows you to match
+multiple optional responses. The :meth:`~pexpect.spawn.expect` method returns
+the index of the pattern that was matched. For example, say you wanted to login
+to a server. After entering a password you could get various responses from the
+server -- your password could be rejected; or you could be allowed in and asked
+for your terminal type; or you could be let right in and given a command prompt.
+The following code fragment gives an example of this::
+
+    child.expect('password:')
+    child.sendline(my_secret_password)
+    # We expect any of these three patterns...
+    i = child.expect (['Permission denied', 'Terminal type', '[#\$] '])
+    if i==0:
+        print('Permission denied on host. Can't login')
+        child.kill(0)
+    elif i==2:
+        print('Login OK... need to send terminal type.')
+        child.sendline('vt100')
+        child.expect('[#\$] ')
+    elif i==3:
+        print('Login OK.')
+        print('Shell command prompt', child.after)
+
+If nothing matches an expected pattern then :meth:`~pexpect.spawn.expect` will
+eventually raise a :class:`~pexpect.TIMEOUT` exception. The default time is 30
+seconds, but you can change this by passing a timeout argument to
+:meth:`~pexpect.spawn.expect`::
+
+    # Wait no more than 2 minutes (120 seconds) for password prompt.
+    child.expect('password:', timeout=120)
+
+Find the end of line -- CR/LF conventions
+-----------------------------------------
+
+Pexpect matches regular expressions a little differently than what you might be
+used to.
+
+The :regexp:`$` pattern for end of line match is useless. The :regexp:`$`
+matches the end of string, but Pexpect reads from the child one character at a
+time, so each character looks like the end of a line. Pexpect can't do a
+look-ahead into the child's output stream. In general you would have this
+situation when using regular expressions with any stream.
+
+.. note::
+
+  Pexpect does have an internal buffer, so reads are faster than one character
+  at a time, but from the user's perspective the regex patterns test happens
+  one character at a time.
+
+The best way to match the end of a line is to look for the newline: ``"\r\n"``
+(CR/LF). Yes, that does appear to be DOS-style. It may surprise some UNIX people
+to learn that terminal TTY device drivers (dumb, vt100, ANSI, xterm, etc.) all
+use the CR/LF combination to signify the end of line. Pexpect uses a Pseudo-TTY
+device to talk to the child application, so when the child app prints ``"\n"``
+you actually see ``"\r\n"``.
+
+UNIX uses just linefeeds to end lines of text, but not when it comes to TTY
+devices! TTY devices are more like the Windows world. Each line of text ends
+with a CR/LF combination. When you intercept data from a UNIX command from a
+TTY device you will find that the TTY device outputs a CR/LF combination. A
+UNIX command may only write a linefeed (``\n``), but the TTY device driver
+converts it to CR/LF. This means that your terminal will see lines end with
+CR/LF (hex ``0D 0A``). Since Pexpect emulates a terminal, to match ends of
+lines you have to expect the CR/LF combination::
+
+    child.expect('\r\n')
+
+If you just need to skip past a new line then ``expect('\n')`` by itself will
+work, but if you are expecting a specific pattern before the end of line then
+you need to explicitly look for the ``\r``. For example the following expects a
+word at the end of a line::
+
+    child.expect('\w+\r\n')
+
+But the following would both fail::
+
+    child.expect('\w+\n')
+
+And as explained before, trying to use :regexp:`$` to match the end of line
+would not work either::
+
+    child.expect ('\w+$')
+
+So if you need to explicitly look for the END OF LINE, you want to look for the
+CR/LF combination -- not just the LF and not the $ pattern.
+
+This problem is not limited to Pexpect. This problem happens any time you try
+to perform a regular expression match on a stream. Regular expressions need to
+look ahead. With a stream it is hard to look ahead because the process
+generating the stream may not be finished. There is no way to know if the
+process has paused momentarily or is finished and waiting for you. Pexpect must
+implicitly always do a NON greedy match (minimal) at the end of a input.
+
+Pexpect compiles all regular expressions with the :data:`re.DOTALL` flag.
+With the :data:`~re.DOTALL` flag, a ``"."`` will match a newline.
+
+Beware of + and * at the end of patterns
+----------------------------------------
+
+Remember that any time you try to match a pattern that needs look-ahead that
+you will always get a minimal match (non greedy). For example, the following
+will always return just one character::
+
+    child.expect ('.+')
+
+This example will match successfully, but will always return no characters::
+
+    child.expect ('.*')
+
+Generally any star * expression will match as little as possible.
+
+One thing you can do is to try to force a non-ambiguous character at the end of
+your :regexp:`\\d+` pattern. Expect that character to delimit the string. For
+example, you might try making the end of your pattern be :regexp:`\\D+` instead
+of :regexp:`\\D*`. Number digits alone would not satisfy the :regexp:`(\\d+)\\D+`
+pattern. You would need some numbers and at least one non-number at the end.
+
+
+Debugging
+---------
+
+If you get the string value of a :class:`pexpect.spawn` object you will get lots
+of useful debugging information. For debugging it's very useful to use the
+following pattern::
+
+    try:
+        i = child.expect ([pattern1, pattern2, pattern3, etc])
+    except:
+        print("Exception was thrown")
+        print("debug information:")
+        print(str(child))
+
+It is also useful to log the child's input and out to a file or the screen. The
+following will turn on logging and send output to stdout (the screen)::
+
+    child = pexpect.spawn(foo)
+    child.logfile = sys.stdout
+
+Exceptions
+----------
+
+:class:`~pexpect.EOF`
+
+Note that two flavors of EOF Exception may be thrown. They are virtually
+identical except for the message string. For practical purposes you should have
+no need to distinguish between them, but they do give a little extra information
+about what type of platform you are running. The two messages are:
+
+- "End Of File (EOF) in read(). Exception style platform."
+- "End Of File (EOF) in read(). Empty string style platform."
+
+Some UNIX platforms will throw an exception when you try to read from a file
+descriptor in the EOF state. Other UNIX platforms instead quietly return an
+empty string to indicate that the EOF state has been reached.
+
+If you wish to read up to the end of the child's output without generating an
+:class:`~pexpect.EOF` exception then use the ``expect(pexpect.EOF)`` method.
+
+:class:`~pexpect.TIMEOUT`
+
+The :meth:`~pexpect.spawn.expect` and :meth:`~pexpect.spawn.read` methods will
+also timeout if the child does not generate any output for a given amount of
+time. If this happens they will raise a :class:`~pexpect.TIMEOUT` exception.
+You can have these method ignore a timeout and block indefinitely by passing 
+``None`` for the timeout parameter::
+
+    child.expect(pexpect.EOF, timeout=None)
diff --git a/third_party/pexpect/doc/requirements.txt b/third_party/pexpect/doc/requirements.txt
new file mode 100644
index 0000000..57ebb2d
--- /dev/null
+++ b/third_party/pexpect/doc/requirements.txt
@@ -0,0 +1 @@
+ptyprocess
diff --git a/third_party/pexpect/doc/sphinxext/github.py b/third_party/pexpect/doc/sphinxext/github.py
new file mode 100644
index 0000000..519e146
--- /dev/null
+++ b/third_party/pexpect/doc/sphinxext/github.py
@@ -0,0 +1,155 @@
+"""Define text roles for GitHub
+
+* ghissue - Issue
+* ghpull - Pull Request
+* ghuser - User
+
+Adapted from bitbucket example here:
+https://bitbucket.org/birkenfeld/sphinx-contrib/src/tip/bitbucket/sphinxcontrib/bitbucket.py
+
+Authors
+-------
+
+* Doug Hellmann
+* Min RK
+"""
+#
+# Original Copyright (c) 2010 Doug Hellmann.  All rights reserved.
+#
+
+from docutils import nodes, utils
+from docutils.parsers.rst.roles import set_classes
+
+def make_link_node(rawtext, app, type, slug, options):
+    """Create a link to a github resource.
+
+    :param rawtext: Text being replaced with link node.
+    :param app: Sphinx application context
+    :param type: Link type (issues, changeset, etc.)
+    :param slug: ID of the thing to link to
+    :param options: Options dictionary passed to role func.
+    """
+
+    try:
+        base = app.config.github_project_url
+        if not base:
+            raise AttributeError
+        if not base.endswith('/'):
+            base += '/'
+    except AttributeError as err:
+        raise ValueError('github_project_url configuration value is not set (%s)' % str(err))
+
+    ref = base + type + '/' + slug + '/'
+    set_classes(options)
+    prefix = "#"
+    if type == 'pull':
+        prefix = "PR " + prefix
+    node = nodes.reference(rawtext, prefix + utils.unescape(slug), refuri=ref,
+                           **options)
+    return node
+
+def ghissue_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
+    """Link to a GitHub issue.
+
+    Returns 2 part tuple containing list of nodes to insert into the
+    document and a list of system messages.  Both are allowed to be
+    empty.
+
+    :param name: The role name used in the document.
+    :param rawtext: The entire markup snippet, with role.
+    :param text: The text marked with the role.
+    :param lineno: The line number where rawtext appears in the input.
+    :param inliner: The inliner instance that called us.
+    :param options: Directive options for customization.
+    :param content: The directive content for customization.
+    """
+
+    try:
+        issue_num = int(text)
+        if issue_num <= 0:
+            raise ValueError
+    except ValueError:
+        msg = inliner.reporter.error(
+            'GitHub issue number must be a number greater than or equal to 1; '
+            '"%s" is invalid.' % text, line=lineno)
+        prb = inliner.problematic(rawtext, rawtext, msg)
+        return [prb], [msg]
+    app = inliner.document.settings.env.app
+    #app.info('issue %r' % text)
+    if 'pull' in name.lower():
+        category = 'pull'
+    elif 'issue' in name.lower():
+        category = 'issues'
+    else:
+        msg = inliner.reporter.error(
+            'GitHub roles include "ghpull" and "ghissue", '
+            '"%s" is invalid.' % name, line=lineno)
+        prb = inliner.problematic(rawtext, rawtext, msg)
+        return [prb], [msg]
+    node = make_link_node(rawtext, app, category, str(issue_num), options)
+    return [node], []
+
+def ghuser_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
+    """Link to a GitHub user.
+
+    Returns 2 part tuple containing list of nodes to insert into the
+    document and a list of system messages.  Both are allowed to be
+    empty.
+
+    :param name: The role name used in the document.
+    :param rawtext: The entire markup snippet, with role.
+    :param text: The text marked with the role.
+    :param lineno: The line number where rawtext appears in the input.
+    :param inliner: The inliner instance that called us.
+    :param options: Directive options for customization.
+    :param content: The directive content for customization.
+    """
+    app = inliner.document.settings.env.app
+    #app.info('user link %r' % text)
+    ref = 'https://www.github.com/' + text
+    node = nodes.reference(rawtext, text, refuri=ref, **options)
+    return [node], []
+
+def ghcommit_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
+    """Link to a GitHub commit.
+
+    Returns 2 part tuple containing list of nodes to insert into the
+    document and a list of system messages.  Both are allowed to be
+    empty.
+
+    :param name: The role name used in the document.
+    :param rawtext: The entire markup snippet, with role.
+    :param text: The text marked with the role.
+    :param lineno: The line number where rawtext appears in the input.
+    :param inliner: The inliner instance that called us.
+    :param options: Directive options for customization.
+    :param content: The directive content for customization.
+    """
+    app = inliner.document.settings.env.app
+    #app.info('user link %r' % text)
+    try:
+        base = app.config.github_project_url
+        if not base:
+            raise AttributeError
+        if not base.endswith('/'):
+            base += '/'
+    except AttributeError as err:
+        raise ValueError('github_project_url configuration value is not set (%s)' % str(err))
+
+    ref = base + text
+    node = nodes.reference(rawtext, text[:6], refuri=ref, **options)
+    return [node], []
+
+
+def setup(app):
+    """Install the plugin.
+    
+    :param app: Sphinx application context.
+    """
+    app.info('Initializing GitHub plugin')
+    app.add_role('ghissue', ghissue_role)
+    app.add_role('ghpull', ghissue_role)
+    app.add_role('ghuser', ghuser_role)
+    app.add_role('ghcommit', ghcommit_role)
+    app.add_config_value('github_project_url', None, 'env')
+    return
diff --git a/third_party/pexpect/examples/README b/third_party/pexpect/examples/README
new file mode 100644
index 0000000..be21e96
--- /dev/null
+++ b/third_party/pexpect/examples/README
@@ -0,0 +1,89 @@
+This directory contains scripts that give examples of using Pexpect.
+
+hive.py
+    This script creates SSH connections to a list of hosts that
+    you provide. Then you are given a command line prompt. Each
+    shell command that you enter is sent to all the hosts. The
+    response from each host is collected and printed. For example,
+    you could connect to a dozen different machines and reboot
+    them all at once.
+
+script.py
+    This implements a command similar to the classic BSD "script" command.
+    This will start a subshell and log all input and output to a file.
+    This demonstrates the interact() method of Pexpect.
+
+fix_cvs_files.py
+    This is for cleaning up binary files improperly added to
+    CVS. This script scans the given path to find binary files;
+    checks with CVS to see if the sticky options are set to -kb;
+    finally if sticky options are not -kb then uses 'cvs admin'
+    to set the -kb option. 
+
+ftp.py
+    This demonstrates an FTP "bookmark".
+    This connects to an ftp site; does a few ftp commands; and then gives the user
+    interactive control over the session. In this case the "bookmark" is to a
+    directory on the OpenBSD ftp server. It puts you in the i386 packages
+    directory. You can easily modify this for other sites.
+    This demonstrates the interact() method of Pexpect.
+
+monitor.py
+    This runs a sequence of system status commands on a remote host using SSH.
+    It runs a simple system checks such as uptime and free to monitor
+    the state of the remote host.
+
+passmass.py
+    This will login to a list of hosts and change the password of the
+    given user. This demonstrates scripting logins; although, you could
+    more easily do this using the pxssh subclass of Pexpect.
+    See also the "hive.py" example script for a more general example
+    of scripting a collection of servers.
+
+python.py
+    This starts the python interpreter and prints the greeting message backwards.
+    It then gives the user interactive control of Python. It's pretty useless!
+
+rippy.py
+    This is a wizard for mencoder. It greatly simplifies the process of
+    ripping a DVD to mpeg4 format (XviD, DivX). It can transcode from any
+    video file to another. It has options for resampling the audio stream;
+    removing interlace artifacts, fitting to a target file size, etc.
+    There are lots of options, but the process is simple and easy to use.
+
+sshls.py
+    This lists a directory on a remote machine.
+
+ssh_tunnel.py
+    This starts an SSH tunnel to a remote machine. It monitors the connection
+    and restarts the tunnel if it goes down.
+
+uptime.py
+    This will run the uptime command and parse the output into python variables.
+    This demonstrates using a single regular expression to match the output
+    of a command and capturing different variable in match groups.
+    The regular expression takes into account a wide variety of different
+    formats for uptime output.
+
+df.py
+    This collects filesystem capacity info using the 'df' command.
+    Tuples of filesystem name and percentage are stored in a list.
+    A simple report is printed. Filesystems over 95% capacity are highlighted.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
diff --git a/third_party/pexpect/examples/astat.py b/third_party/pexpect/examples/astat.py
new file mode 100755
index 0000000..a083fe1
--- /dev/null
+++ b/third_party/pexpect/examples/astat.py
@@ -0,0 +1,99 @@
+#!/usr/bin/env python
+
+'''This runs Apache Status on the remote host and returns the number of requests per second.
+
+./astat.py [-s server_hostname] [-u username] [-p password]
+    -s : hostname of the remote server to login to.
+    -u : username to user for login.
+    -p : Password to user for login.
+
+Example:
+    This will print information about the given host:
+        ./astat.py -s www.example.com -u mylogin -p mypassword
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import os
+import sys
+import getopt
+import getpass
+import pxssh
+
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+
+def exit_with_usage():
+
+    print(globals()['__doc__'])
+    os._exit(1)
+
+
+def main():
+
+    ######################################################################
+    ## Parse the options, arguments, get ready, etc.
+    ######################################################################
+    try:
+        optlist, args = getopt.getopt(sys.argv[1:], 'h?s:u:p:', ['help','h','?'])
+    except Exception as e:
+        print(str(e))
+        exit_with_usage()
+    options = dict(optlist)
+    if len(args) > 1:
+        exit_with_usage()
+
+    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+        print("Help:")
+        exit_with_usage()
+
+    if '-s' in options:
+        hostname = options['-s']
+    else:
+        hostname = raw_input('hostname: ')
+    if '-u' in options:
+        username = options['-u']
+    else:
+        username = raw_input('username: ')
+    if '-p' in options:
+        password = options['-p']
+    else:
+        password = getpass.getpass('password: ')
+
+    #
+    # Login via SSH
+    #
+    p = pxssh.pxssh()
+    p.login(hostname, username, password)
+    p.sendline('apachectl status')
+    p.expect('([0-9]+\.[0-9]+)\s*requests/sec')
+    requests_per_second = p.match.groups()[0]
+    p.logout()
+    print(requests_per_second)
+
+if __name__ == "__main__":
+    main()
diff --git a/third_party/pexpect/examples/cgishell.cgi b/third_party/pexpect/examples/cgishell.cgi
new file mode 100755
index 0000000..23bef5f
--- /dev/null
+++ b/third_party/pexpect/examples/cgishell.cgi
@@ -0,0 +1,766 @@
+#!/usr/bin/python
+##!/usr/bin/env python
+"""CGI shell server
+
+This exposes a shell terminal on a web page.
+It uses AJAX to send keys and receive screen updates.
+The client web browser needs nothing but CSS and Javascript.
+
+    --hostname : sets the remote host name to open an ssh connection to.
+    --username : sets the user name to login with
+    --password : (optional) sets the password to login with
+    --port     : set the local port for the server to listen on
+    --watch    : show the virtual screen after each client request
+
+This project is probably not the most security concious thing I've ever built.
+This should be considered an experimental tool -- at best.
+"""
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+import sys,os
+sys.path.insert (0,os.getcwd()) # let local modules precede any installed modules
+import socket, random, string, traceback, cgi, time, getopt, getpass, threading, resource, signal
+import pxssh, pexpect, ANSI
+
+def exit_with_usage(exit_code=1):
+    print(globals()['__doc__'])
+    os._exit(exit_code)
+
+def client (command, host='localhost', port=-1):
+    """This sends a request to the server and returns the response.
+    If port <= 0 then host is assumed to be the filename of a Unix domain socket.
+    If port > 0 then host is an inet hostname.
+    """
+    if port <= 0:
+        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        s.connect(host)
+    else:
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        s.connect((host, port))
+    s.send(command)
+    data = s.recv (2500)
+    s.close()
+    return data
+
+def server (hostname, username, password, socket_filename='/tmp/server_sock', daemon_mode = True, verbose=False):
+    """This starts and services requests from a client.
+        If daemon_mode is True then this forks off a separate daemon process and returns the daemon's pid.
+        If daemon_mode is False then this does not return until the server is done.
+    """
+    if daemon_mode:
+        mypid_name = '/tmp/%d.pid' % os.getpid()
+        daemon_pid = daemonize(daemon_pid_filename=mypid_name)
+        time.sleep(1)
+        if daemon_pid != 0:
+            os.unlink(mypid_name)
+            return daemon_pid
+
+    virtual_screen = ANSI.ANSI (24,80) 
+    child = pxssh.pxssh()
+    try:
+        child.login (hostname, username, password, login_naked=True)
+    except:
+        return   
+    if verbose: print('login OK')
+    virtual_screen.write (child.before)
+    virtual_screen.write (child.after)
+
+    if os.path.exists(socket_filename): os.remove(socket_filename)
+    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+    s.bind(socket_filename)
+    os.chmod(socket_filename, 0o777)
+    if verbose: print('Listen')
+    s.listen(1)
+
+    r = roller (endless_poll, (child, child.PROMPT, virtual_screen))
+    r.start()
+    if verbose: print("started screen-poll-updater in background thread")
+    sys.stdout.flush()
+    try:
+        while True:
+            conn, addr = s.accept()
+            if verbose: print('Connected by', addr)
+            data = conn.recv(1024)
+            request = data.split(' ', 1)
+            if len(request)>1:
+                cmd = request[0].strip()
+                arg = request[1].strip()
+            else:
+                cmd = request[0].strip()
+                arg = ''
+
+            if cmd == 'exit':
+                r.cancel()
+                break
+            elif cmd == 'sendline':
+                child.sendline (arg)
+                time.sleep(0.1)
+                shell_window = str(virtual_screen)
+            elif cmd == 'send' or cmd=='xsend':
+                if cmd=='xsend':
+                    arg = arg.decode("hex")
+                child.send (arg)
+                time.sleep(0.1)
+                shell_window = str(virtual_screen)
+            elif cmd == 'cursor':
+                shell_window = '%x,%x' % (virtual_screen.cur_r, virtual_screen.cur_c)
+            elif cmd == 'refresh':
+                shell_window = str(virtual_screen)
+            elif cmd == 'hash':
+                shell_window = str(hash(str(virtual_screen)))
+
+            response = []
+            response.append (shell_window)
+            if verbose: print('\n'.join(response))
+            sent = conn.send('\n'.join(response))
+            if sent < len (response):
+                if verbose: print("Sent is too short. Some data was cut off.")
+            conn.close()
+    except e:
+        pass
+    r.cancel()
+    if verbose: print("cleaning up socket")
+    s.close()
+    if os.path.exists(socket_filename): os.remove(socket_filename)
+    if verbose: print("server done!")
+
+class roller (threading.Thread):
+    """This class continuously loops a function in a thread.
+        This is basically a thin layer around Thread with a
+        while loop and a cancel.
+    """
+    def __init__(self, function, args=[], kwargs={}):
+        threading.Thread.__init__(self)
+        self.function = function
+        self.args = args
+        self.kwargs = kwargs
+        self.finished = threading.Event()
+    def cancel(self):
+        """Stop the roller."""
+        self.finished.set()
+    def run(self):
+        while not self.finished.isSet():
+            self.function(*self.args, **self.kwargs)
+
+def endless_poll (child, prompt, screen, refresh_timeout=0.1):
+    """This keeps the screen updated with the output of the child.
+        This will be run in a separate thread. See roller class.
+    """
+    #child.logfile_read = screen
+    try:
+        s = child.read_nonblocking(4000, 0.1)
+        screen.write(s)
+    except:
+        pass
+
+def daemonize (stdin=None, stdout=None, stderr=None, daemon_pid_filename=None):
+    """This runs the current process in the background as a daemon.
+    The arguments stdin, stdout, stderr allow you to set the filename that the daemon reads and writes to.
+    If they are set to None then all stdio for the daemon will be directed to /dev/null.
+    If daemon_pid_filename is set then the pid of the daemon will be written to it as plain text
+    and the pid will be returned. If daemon_pid_filename is None then this will return None.
+    """
+    UMASK = 0
+    WORKINGDIR = "/"
+    MAXFD = 1024
+
+    # The stdio file descriptors are redirected to /dev/null by default.
+    if hasattr(os, "devnull"):
+        DEVNULL = os.devnull
+    else:
+        DEVNULL = "/dev/null"
+    if stdin is None: stdin = DEVNULL
+    if stdout is None: stdout = DEVNULL
+    if stderr is None: stderr = DEVNULL
+
+    try:
+        pid = os.fork() # fork first child
+    except OSError as e:
+        raise Exception("%s [%d]" % (e.strerror, e.errno))
+
+    if pid != 0: 
+        os.waitpid(pid,0)
+        if daemon_pid_filename is not None:
+            daemon_pid = int(file(daemon_pid_filename,'r').read())
+            return daemon_pid
+        else:
+            return None
+
+    # first child
+    os.setsid()
+    signal.signal(signal.SIGHUP, signal.SIG_IGN)
+
+    try:
+        pid = os.fork() # fork second child
+    except OSError as e:
+        raise Exception("%s [%d]" % (e.strerror, e.errno))
+
+    if pid != 0:
+        if daemon_pid_filename is not None:
+            file(daemon_pid_filename,'w').write(str(pid))
+        os._exit(0) # exit parent (the first child) of the second child.
+
+    # second child
+    os.chdir(WORKINGDIR)
+    os.umask(UMASK)
+
+    maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
+    if maxfd == resource.RLIM_INFINITY:
+        maxfd = MAXFD
+  
+    # close all file descriptors
+    for fd in range(0, maxfd):
+        try:
+            os.close(fd)
+        except OSError:   # fd wasn't open to begin with (ignored)
+            pass
+
+    os.open (DEVNULL, os.O_RDWR)  # standard input
+
+    # redirect standard file descriptors
+    si = open(stdin, 'r')
+    so = open(stdout, 'a+')
+    se = open(stderr, 'a+', 0)
+    os.dup2(si.fileno(), sys.stdin.fileno())
+    os.dup2(so.fileno(), sys.stdout.fileno())
+    os.dup2(se.fileno(), sys.stderr.fileno())
+
+    return 0
+
+def client_cgi ():
+    """This handles the request if this script was called as a cgi.
+    """
+    sys.stderr = sys.stdout
+    ajax_mode = False
+    TITLE="Shell"
+    SHELL_OUTPUT=""
+    SID="NOT"
+    print("Content-type: text/html;charset=utf-8\r\n")
+    try:
+        form = cgi.FieldStorage()
+        if 'ajax' in form:
+            ajax_mode = True
+            ajax_cmd = form['ajax'].value
+            SID=form['sid'].value
+            if ajax_cmd == 'send':
+                command = 'xsend'
+                arg = form['arg'].value.encode('hex')
+                result = client (command + ' ' + arg, '/tmp/'+SID)
+                print(result)
+            elif ajax_cmd == 'refresh':
+                command = 'refresh'
+                result = client (command, '/tmp/'+SID)
+                print(result)
+            elif ajax_cmd == 'cursor':
+                command = 'cursor'
+                result = client (command, '/tmp/'+SID)
+                print(result)
+            elif ajax_cmd == 'exit':
+                command = 'exit'
+                result = client (command, '/tmp/'+SID)
+                print(result)
+            elif ajax_cmd == 'hash':
+                command = 'hash'
+                result = client (command, '/tmp/'+SID)
+                print(result)
+        elif 'sid' not in form:
+            SID=random_sid()
+            print(LOGIN_HTML % locals());
+        else:
+            SID=form['sid'].value
+            if 'start_server' in form:
+                USERNAME = form['username'].value
+                PASSWORD = form['password'].value
+                dpid = server ('127.0.0.1', USERNAME, PASSWORD, '/tmp/'+SID)
+                SHELL_OUTPUT="daemon pid: " + str(dpid)
+            else:
+                if 'cli' in form:
+                    command = 'sendline ' + form['cli'].value
+                else:
+                    command = 'sendline'
+                SHELL_OUTPUT = client (command, '/tmp/'+SID)
+            print(CGISH_HTML % locals())
+    except:
+        tb_dump = traceback.format_exc()
+        if ajax_mode:
+            print(str(tb_dump))
+        else:
+            SHELL_OUTPUT=str(tb_dump)
+            print(CGISH_HTML % locals())
+
+def server_cli():
+    """This is the command line interface to starting the server.
+    This handles things if the script was not called as a CGI
+    (if you run it from the command line).
+    """
+    try:
+        optlist, args = getopt.getopt(sys.argv[1:], 'h?d', ['help','h','?', 'hostname=', 'username=', 'password=', 'port=', 'watch'])
+    except Exception as e:
+        print(str(e))
+        exit_with_usage()
+
+    command_line_options = dict(optlist)
+    options = dict(optlist)
+    # There are a million ways to cry for help. These are but a few of them.
+    if [elem for elem in command_line_options if elem in ['-h','--h','-?','--?','--help']]:
+        exit_with_usage(0)
+  
+    hostname = "127.0.0.1"
+    #port = 1664
+    username = os.getenv('USER')
+    password = ""
+    daemon_mode = False
+    if '-d' in options:
+        daemon_mode = True
+    if '--watch' in options:
+        watch_mode = True
+    else:
+        watch_mode = False
+    if '--hostname' in options:
+        hostname = options['--hostname']
+    if '--port' in options:
+        port = int(options['--port'])
+    if '--username' in options:
+        username = options['--username']
+    if '--password' in options:
+        password = options['--password']
+    else:
+        password = getpass.getpass('password: ')
+   
+    server (hostname, username, password, '/tmp/mysock', daemon_mode)
+
+def random_sid ():
+    a=random.randint(0,65535)
+    b=random.randint(0,65535)
+    return '%04x%04x.sid' % (a,b)
+
+def parse_host_connect_string (hcs):
+    """This parses a host connection string in the form
+    username:password at hostname:port. All fields are options expcet hostname. A
+    dictionary is returned with all four keys. Keys that were not included are
+    set to empty strings ''. Note that if your password has the '@' character
+    then you must backslash escape it.
+    """
+    if '@' in hcs:
+        p = re.compile (r'(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+    else:
+        p = re.compile (r'(?P<username>)(?P<password>)(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+    m = p.search (hcs)
+    d = m.groupdict()
+    d['password'] = d['password'].replace('\\@','@')
+    return d
+     
+def pretty_box (s, rows=24, cols=80):
+    """This puts an ASCII text box around the given string.
+    """
+    top_bot = '+' + '-'*cols + '+\n'
+    return top_bot + '\n'.join(['|'+line+'|' for line in s.split('\n')]) + '\n' + top_bot
+
+def main ():
+    if os.getenv('REQUEST_METHOD') is None:
+        server_cli()
+    else:
+        client_cgi()
+
+# It's mostly HTML and Javascript from here on out.
+CGISH_HTML="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+<head>
+<title>%(TITLE)s %(SID)s</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+<style type=text/css>
+a {color: #9f9; text-decoration: none}
+a:hover {color: #0f0}
+hr {color: #0f0}
+html,body,textarea,input,form
+{
+font-family: "Courier New", Courier, mono; 
+font-size: 8pt; 
+color: #0c0;
+background-color: #020;
+margin:0;
+padding:0;
+border:0;
+}
+input { background-color: #010; }
+textarea {
+border-width:1;
+border-style:solid;
+border-color:#0c0;
+padding:3;
+margin:3;
+}
+</style>
+
+<script language="JavaScript">
+function focus_first()
+{if (document.forms.length > 0)
+{var TForm = document.forms[0];
+for (i=0;i<TForm.length;i++){
+if ((TForm.elements[i].type=="text")||
+(TForm.elements[i].type=="textarea")||
+(TForm.elements[i].type.toString().charAt(0)=="s"))
+{document.forms[0].elements[i].focus();break;}}}}
+
+// JavaScript Virtual Keyboard
+// If you like this code then buy me a sandwich.
+// Noah Spurrier <noah at noah.org>
+var flag_shift=0;
+var flag_shiftlock=0;
+var flag_ctrl=0;
+var ButtonOnColor="#ee0";
+
+function init ()
+{
+    // hack to set quote key to show both single quote and double quote
+    document.form['quote'].value = "'" + '  "';
+    //refresh_screen();
+    poll();
+    document.form["cli"].focus();
+}
+function get_password ()
+{
+    var username = prompt("username?","");
+    var password = prompt("password?","");
+    start_server (username, password);
+}
+function multibrowser_ajax ()
+{
+    var xmlHttp = false;
+/*@cc_on @*/
+/*@if (@_jscript_version >= 5)
+    try
+    {
+        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
+    }
+    catch (e)
+    {
+        try
+        {
+            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
+        }
+        catch (e2)
+        {
+              xmlHttp = false;
+        }
+    }
+ at end @*/
+
+    if (!xmlHttp && typeof XMLHttpRequest != 'undefined')
+    {
+        xmlHttp = new XMLHttpRequest();
+    }
+    return xmlHttp;
+}
+function load_url_to_screen(url)
+{ 
+    xmlhttp = multibrowser_ajax();
+    //window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
+    xmlhttp.onreadystatechange = update_virtual_screen;
+    xmlhttp.open("GET", url);
+    xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
+    xmlhttp.send(null);
+}
+function update_virtual_screen()
+{
+    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
+    {
+        var screen_text = xmlhttp.responseText;
+        document.form["screen_text"].value = screen_text;
+        //var json_data = json_parse(xmlhttp.responseText);
+    }
+}
+function poll()
+{
+    refresh_screen();
+    timerID  = setTimeout("poll()", 2000);
+    // clearTimeout(timerID);
+}
+//function start_server (username, password)
+//{
+//    load_url_to_screen('cgishell.cgi?ajax=serverstart&username=' + escape(username) + '&password=' + escape(password);
+//}
+function refresh_screen()
+{
+    load_url_to_screen('cgishell.cgi?ajax=refresh&sid=%(SID)s');
+}
+function query_hash()
+{
+    load_url_to_screen('cgishell.cgi?ajax=hash&sid=%(SID)s');
+}
+function query_cursor()
+{
+    load_url_to_screen('cgishell.cgi?ajax=cursor&sid=%(SID)s');
+}
+function exit_server()
+{
+    load_url_to_screen('cgishell.cgi?ajax=exit&sid=%(SID)s');
+}
+function type_key (chars)
+{
+    var ch = '?';
+    if (flag_shiftlock || flag_shift)
+    {
+        ch = chars.substr(1,1);
+    }
+    else if (flag_ctrl)
+    {
+        ch = chars.substr(2,1);
+    }
+    else
+    {
+        ch = chars.substr(0,1);
+    }
+    load_url_to_screen('cgishell.cgi?ajax=send&sid=%(SID)s&arg=' + escape(ch));
+    if (flag_shift || flag_ctrl)
+    {
+        flag_shift = 0;
+        flag_ctrl = 0;
+    }
+    update_button_colors();
+}
+
+function key_shiftlock()
+{
+    flag_ctrl = 0;
+    flag_shift = 0;
+    if (flag_shiftlock)
+    {
+        flag_shiftlock = 0;
+    }
+    else
+    {
+        flag_shiftlock = 1;
+    }
+    update_button_colors();
+}
+
+function key_shift()
+{
+    if (flag_shift)
+    {
+        flag_shift = 0;
+    }
+    else
+    {
+        flag_ctrl = 0;
+        flag_shiftlock = 0;
+        flag_shift = 1;
+    }
+    update_button_colors(); 
+}
+function key_ctrl ()
+{
+    if (flag_ctrl)
+    {
+        flag_ctrl = 0;
+    }
+    else
+    {
+        flag_ctrl = 1;
+        flag_shiftlock = 0;
+        flag_shift = 0;
+    }
+    
+    update_button_colors();
+}
+function update_button_colors ()
+{
+    if (flag_ctrl)
+    {
+        document.form['Ctrl'].style.backgroundColor = ButtonOnColor;
+        document.form['Ctrl2'].style.backgroundColor = ButtonOnColor;
+    }
+    else
+    {
+        document.form['Ctrl'].style.backgroundColor = document.form.style.backgroundColor;
+        document.form['Ctrl2'].style.backgroundColor = document.form.style.backgroundColor;
+    }
+    if (flag_shift)
+    {
+        document.form['Shift'].style.backgroundColor = ButtonOnColor;
+        document.form['Shift2'].style.backgroundColor = ButtonOnColor;
+    }
+    else
+    {
+        document.form['Shift'].style.backgroundColor = document.form.style.backgroundColor;
+        document.form['Shift2'].style.backgroundColor = document.form.style.backgroundColor;
+    }
+    if (flag_shiftlock)
+    {
+        document.form['ShiftLock'].style.backgroundColor = ButtonOnColor;
+    }
+    else
+    {
+        document.form['ShiftLock'].style.backgroundColor = document.form.style.backgroundColor;
+    }
+    
+}
+function keyHandler(e)
+{
+    var pressedKey;
+    if (document.all)    { e = window.event; }
+    if (document.layers) { pressedKey = e.which; }
+    if (document.all)    { pressedKey = e.keyCode; }
+    pressedCharacter = String.fromCharCode(pressedKey);
+    type_key(pressedCharacter+pressedCharacter+pressedCharacter);
+    alert(pressedCharacter);
+//    alert(' Character = ' + pressedCharacter + ' [Decimal value = ' + pressedKey + ']');
+}
+//document.onkeypress = keyHandler;
+//if (document.layers)
+//    document.captureEvents(Event.KEYPRESS);
+//http://sniptools.com/jskeys
+//document.onkeyup = KeyCheck;       
+function KeyCheck(e)
+{
+    var KeyID = (window.event) ? event.keyCode : e.keyCode;
+    type_key(String.fromCharCode(KeyID));
+    e.cancelBubble = true;
+    window.event.cancelBubble = true;
+}
+</script>
+
+</head>
+
+<body onload="init()">
+<form id="form" name="form" action="/cgi-bin/cgishell.cgi" method="POST">
+<input name="sid" value="%(SID)s" type="hidden">
+<textarea name="screen_text" cols="81" rows="25">%(SHELL_OUTPUT)s</textarea>
+<hr noshade="1">
+ <input name="cli" id="cli" type="text" size="80"><br>
+<table border="0" align="left">
+<tr>
+<td width="86%%" align="center">    
+    <input name="submit" type="submit" value="Submit">
+    <input name="refresh" type="button" value="REFRESH" onclick="refresh_screen()">
+    <input name="refresh" type="button" value="CURSOR" onclick="query_cursor()">
+    <input name="hash" type="button" value="HASH" onclick="query_hash()">
+    <input name="exit" type="button" value="EXIT" onclick="exit_server()">
+    <br>
+    <input type="button" value="Esc" onclick="type_key('\\x1b\\x1b')" />
+    <input type="button" value="` ~" onclick="type_key('`~')" />
+    <input type="button" value="1!" onclick="type_key('1!')" />
+    <input type="button" value="2@" onclick="type_key('2@\\x00')" />
+    <input type="button" value="3#" onclick="type_key('3#')" />
+    <input type="button" value="4$" onclick="type_key('4$')" />
+    <input type="button" value="5%%" onclick="type_key('5%%')" />
+    <input type="button" value="6^" onclick="type_key('6^\\x1E')" />
+    <input type="button" value="7&" onclick="type_key('7&')" />
+    <input type="button" value="8*" onclick="type_key('8*')" />
+    <input type="button" value="9(" onclick="type_key('9(')" />
+    <input type="button" value="0)" onclick="type_key('0)')" />
+    <input type="button" value="-_" onclick="type_key('-_\\x1F')" />
+    <input type="button" value="=+" onclick="type_key('=+')" />
+    <input type="button" value="BkSp" onclick="type_key('\\x08\\x08\\x08')" />
+    <br>
+    <input type="button" value="Tab" onclick="type_key('\\t\\t')" />
+    <input type="button" value="Q" onclick="type_key('qQ\\x11')" />
+    <input type="button" value="W" onclick="type_key('wW\\x17')" />
+    <input type="button" value="E" onclick="type_key('eE\\x05')" />
+    <input type="button" value="R" onclick="type_key('rR\\x12')" />
+    <input type="button" value="T" onclick="type_key('tT\\x14')" />
+    <input type="button" value="Y" onclick="type_key('yY\\x19')" />
+    <input type="button" value="U" onclick="type_key('uU\\x15')" />
+    <input type="button" value="I" onclick="type_key('iI\\x09')" />
+    <input type="button" value="O" onclick="type_key('oO\\x0F')" />
+    <input type="button" value="P" onclick="type_key('pP\\x10')" />
+    <input type="button" value="[ {" onclick="type_key('[{\\x1b')" />
+    <input type="button" value="] }" onclick="type_key(']}\\x1d')" />
+    <input type="button" value="\\ |" onclick="type_key('\\\\|\\x1c')" />
+    <br>
+    <input type="button" id="Ctrl" value="Ctrl" onclick="key_ctrl()" />
+    <input type="button" value="A" onclick="type_key('aA\\x01')" />
+    <input type="button" value="S" onclick="type_key('sS\\x13')" />
+    <input type="button" value="D" onclick="type_key('dD\\x04')" />
+    <input type="button" value="F" onclick="type_key('fF\\x06')" />
+    <input type="button" value="G" onclick="type_key('gG\\x07')" />
+    <input type="button" value="H" onclick="type_key('hH\\x08')" />
+    <input type="button" value="J" onclick="type_key('jJ\\x0A')" />
+    <input type="button" value="K" onclick="type_key('kK\\x0B')" />
+    <input type="button" value="L" onclick="type_key('lL\\x0C')" />
+    <input type="button" value="; :" onclick="type_key(';:')" />
+    <input type="button" id="quote" value="'" onclick="type_key('\\x27\\x22')" />
+    <input type="button" value="Enter" onclick="type_key('\\n\\n')" />
+    <br>
+    <input type="button" id="ShiftLock" value="Caps Lock" onclick="key_shiftlock()" />
+    <input type="button" id="Shift" value="Shift" onclick="key_shift()"  />
+    <input type="button" value="Z" onclick="type_key('zZ\\x1A')" />
+    <input type="button" value="X" onclick="type_key('xX\\x18')" />
+    <input type="button" value="C" onclick="type_key('cC\\x03')" />
+    <input type="button" value="V" onclick="type_key('vV\\x16')" />
+    <input type="button" value="B" onclick="type_key('bB\\x02')" />
+    <input type="button" value="N" onclick="type_key('nN\\x0E')" />
+    <input type="button" value="M" onclick="type_key('mM\\x0D')" />
+    <input type="button" value=", <" onclick="type_key(',<')" />
+    <input type="button" value=". >" onclick="type_key('.>')" />
+    <input type="button" value="/ ?" onclick="type_key('/?')" />
+    <input type="button" id="Shift2" value="Shift" onclick="key_shift()" />
+    <input type="button" id="Ctrl2" value="Ctrl" onclick="key_ctrl()" />
+    <br>
+    <input type="button" value="        FINAL FRONTIER        " onclick="type_key('  ')" />
+</td>
+</tr>
+</table>  
+</form>
+</body>
+</html>
+"""
+
+LOGIN_HTML="""<html>
+<head>
+<title>Shell Login</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+<style type=text/css>
+a {color: #9f9; text-decoration: none}
+a:hover {color: #0f0}
+hr {color: #0f0}
+html,body,textarea,input,form
+{
+font-family: "Courier New", Courier, mono;
+font-size: 8pt;
+color: #0c0;
+background-color: #020;
+margin:3;
+padding:0;
+border:0;
+}
+input { background-color: #010; }
+input,textarea {
+border-width:1;
+border-style:solid;
+border-color:#0c0;
+padding:3;
+margin:3;
+}
+</style>
+<script language="JavaScript">
+function init ()
+{
+    document.login_form["username"].focus();
+}
+</script>
+</head>
+<body onload="init()">
+<form name="login_form" method="POST">
+<input name="start_server" value="1" type="hidden">
+<input name="sid" value="%(SID)s" type="hidden">
+username: <input name="username" type="text" size="30"><br>
+password: <input name="password" type="password" size="30"><br>
+<input name="submit" type="submit" value="enter">
+</form>
+<br>
+</body>
+</html>
+"""
+
+if __name__ == "__main__":
+    try:
+        main()
+    except Exception as e:
+        print(str(e))
+        tb_dump = traceback.format_exc()
+        print(str(tb_dump))
+
diff --git a/third_party/pexpect/examples/chess.py b/third_party/pexpect/examples/chess.py
new file mode 100755
index 0000000..421727d
--- /dev/null
+++ b/third_party/pexpect/examples/chess.py
@@ -0,0 +1,148 @@
+#!/usr/bin/env python
+
+'''This demonstrates controlling a screen oriented application (curses).
+It starts two instances of gnuchess and then pits them against each other.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import pexpect
+import ANSI
+
+REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
+REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
+
+class Chess:
+
+    def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
+        self.child = pexpect.spawn (engine)
+        self.term = ANSI.ANSI ()
+
+        self.child.expect ('Chess')
+        if self.child.after != 'Chess':
+            raise IOError('incompatible chess program')
+        self.term.process_list (self.before)
+        self.term.process_list (self.after)
+        self.last_computer_move = ''
+
+    def read_until_cursor (self, r,c):
+        while 1:
+            self.child.read(1, 60)
+            self.term.process (c)
+            if self.term.cur_r == r and self.term.cur_c == c:
+                return 1
+
+    def do_first_move (self, move):
+        self.child.expect ('Your move is')
+        self.child.sendline (move)
+        self.term.process_list (self.before)
+        self.term.process_list (self.after)
+        return move
+
+    def do_move (self, move):
+        self.read_until_cursor (19,60)
+        self.child.sendline (move)
+        return move
+
+    def get_first_computer_move (self):
+        self.child.expect ('My move is')
+        self.child.expect (REGEX_MOVE)
+        return self.child.after
+
+    def get_computer_move (self):
+        print('Here')
+        i = self.child.expect (['\[17;59H', '\[17;58H'])
+        print(i)
+        if i == 0:
+            self.child.expect (REGEX_MOVE)
+            if len(self.child.after) < 4:
+                self.child.after = self.child.after + self.last_computer_move[3]
+        if i == 1:
+            self.child.expect (REGEX_MOVE_PART)
+            self.child.after = self.last_computer_move[0] + self.child.after
+        print('', self.child.after)
+        self.last_computer_move = self.child.after
+        return self.child.after
+
+    def switch (self):
+        self.child.sendline ('switch')
+
+    def set_depth (self, depth):
+        self.child.sendline ('depth')
+        self.child.expect ('depth=')
+        self.child.sendline ('%d' % depth)
+
+    def quit(self):
+        self.child.sendline ('quit')
+import sys
+print('Starting...')
+white = Chess()
+white.child.echo = 1
+white.child.expect ('Your move is')
+white.set_depth(2)
+white.switch()
+
+move_white = white.get_first_computer_move()
+print('first move white:', move_white)
+
+white.do_move ('e7e5')
+move_white = white.get_computer_move()
+print('move white:', move_white)
+white.do_move ('f8c5')
+move_white = white.get_computer_move()
+print('move white:', move_white)
+white.do_move ('b8a6')
+move_white = white.get_computer_move()
+print('move white:', move_white)
+
+sys.exit(1)
+
+
+
+black = Chess()
+white = Chess()
+white.child.expect ('Your move is')
+white.switch()
+
+move_white = white.get_first_computer_move()
+print('first move white:', move_white)
+
+black.do_first_move (move_white)
+move_black = black.get_first_computer_move()
+print('first move black:', move_black)
+
+white.do_move (move_black)
+
+done = 0
+while not done:
+    move_white = white.get_computer_move()
+    print('move white:', move_white)
+
+    black.do_move (move_white)
+    move_black = black.get_computer_move()
+    print('move black:', move_black)
+
+    white.do_move (move_black)
+    print('tail of loop')
+
+g.quit()
diff --git a/third_party/pexpect/examples/chess2.py b/third_party/pexpect/examples/chess2.py
new file mode 100755
index 0000000..b92509e
--- /dev/null
+++ b/third_party/pexpect/examples/chess2.py
@@ -0,0 +1,153 @@
+#!/usr/bin/env python
+
+'''This demonstrates controlling a screen oriented application (curses).
+It starts two instances of gnuchess and then pits them against each other.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import pexpect
+import ANSI
+import sys
+import time
+
+class Chess:
+
+        def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
+                self.child = pexpect.spawn (engine)
+                self.term = ANSI.ANSI ()
+
+                #self.child.expect ('Chess')
+                #if self.child.after != 'Chess':
+                #        raise IOError, 'incompatible chess program'
+                #self.term.process_list (self.child.before)
+                #self.term.process_list (self.child.after)
+
+                self.last_computer_move = ''
+
+        def read_until_cursor (self, r,c, e=0):
+            '''Eventually something like this should move into the screen class or
+            a subclass. Maybe a combination of pexpect and screen...
+            '''
+            fout = open ('log','a')
+            while self.term.cur_r != r or self.term.cur_c != c:
+                try:
+                    k = self.child.read(1, 10)
+                except Exception as e:
+                    print('EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
+                    sys.stdout.flush()
+                self.term.process (k)
+                fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
+                fout.flush()
+                if e:
+                    sys.stdout.write (k)
+                    sys.stdout.flush()
+                if self.term.cur_r == r and self.term.cur_c == c:
+                    fout.close()
+                    return 1
+            print('DIDNT EVEN HIT.')
+            fout.close()
+            return 1
+
+        def expect_region (self):
+            '''This is another method that would be moved into the
+            screen class.
+            '''
+            pass
+        def do_scan (self):
+            fout = open ('log','a')
+            while 1:
+                c = self.child.read(1,10)
+                self.term.process (c)
+                fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
+                fout.flush()
+                sys.stdout.write (c)
+                sys.stdout.flush()
+
+        def do_move (self, move, e = 0):
+                time.sleep(1)
+                self.read_until_cursor (19,60, e)
+                self.child.sendline (move)
+
+        def wait (self, color):
+            while 1:
+                r = self.term.get_region (14,50,14,60)[0]
+                r = r.strip()
+                if r == color:
+                    return
+                time.sleep (1)
+
+        def parse_computer_move (self, s):
+                i = s.find ('is: ')
+                cm = s[i+3:i+9]
+                return cm
+        def get_computer_move (self, e = 0):
+                time.sleep(1)
+                self.read_until_cursor (19,60, e)
+                time.sleep(1)
+                r = self.term.get_region (17,50,17,62)[0]
+                cm = self.parse_computer_move (r)
+                return cm
+
+        def switch (self):
+                print('switching')
+                self.child.sendline ('switch')
+
+        def set_depth (self, depth):
+                self.child.sendline ('depth')
+                self.child.expect ('depth=')
+                self.child.sendline ('%d' % depth)
+
+        def quit(self):
+                self.child.sendline ('quit')
+
+def LOG (s):
+    print(s)
+    sys.stdout.flush ()
+    fout = open ('moves.log', 'a')
+    fout.write (s + '\n')
+    fout.close()
+
+print('Starting...')
+
+black = Chess()
+white = Chess()
+white.read_until_cursor (19,60,1)
+white.switch()
+
+done = 0
+while not done:
+    white.wait ('Black')
+    move_white = white.get_computer_move(1)
+    LOG ( 'move white:'+ move_white )
+
+    black.do_move (move_white)
+    black.wait ('White')
+    move_black = black.get_computer_move()
+    LOG ( 'move black:'+ move_black )
+
+    white.do_move (move_black, 1)
+
+g.quit()
+
+
diff --git a/third_party/pexpect/examples/chess3.py b/third_party/pexpect/examples/chess3.py
new file mode 100755
index 0000000..dc02663
--- /dev/null
+++ b/third_party/pexpect/examples/chess3.py
@@ -0,0 +1,157 @@
+#!/usr/bin/env python
+
+'''This demonstrates controlling a screen oriented application (curses).
+It starts two instances of gnuchess and then pits them against each other.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import pexpect
+import ANSI
+
+REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
+REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
+
+class Chess:
+
+    def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
+        self.child = pexpect.spawn (engine)
+        self.term = ANSI.ANSI ()
+
+#		self.child.expect ('Chess')
+    #	if self.child.after != 'Chess':
+    #		raise IOError, 'incompatible chess program'
+    #        self.term.process_list (self.before)
+    #        self.term.process_list (self.after)
+        self.last_computer_move = ''
+    def read_until_cursor (self, r,c):
+        fout = open ('log','a')
+        while 1:
+            k = self.child.read(1, 10)
+            self.term.process (k)
+            fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
+            fout.flush()
+            if self.term.cur_r == r and self.term.cur_c == c:
+                fout.close()
+                return 1
+            sys.stdout.write (k)
+            sys.stdout.flush()
+
+    def do_scan (self):
+        fout = open ('log','a')
+        while 1:
+            c = self.child.read(1,10)
+            self.term.process (c)
+            fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
+            fout.flush()
+            sys.stdout.write (c)
+            sys.stdout.flush()
+
+    def do_move (self, move):
+        self.read_until_cursor (19,60)
+        self.child.sendline (move)
+        return move
+
+    def get_computer_move (self):
+        print('Here')
+        i = self.child.expect (['\[17;59H', '\[17;58H'])
+        print(i)
+        if i == 0:
+            self.child.expect (REGEX_MOVE)
+            if len(self.child.after) < 4:
+                self.child.after = self.child.after + self.last_computer_move[3]
+        if i == 1:
+            self.child.expect (REGEX_MOVE_PART)
+            self.child.after = self.last_computer_move[0] + self.child.after
+        print('', self.child.after)
+        self.last_computer_move = self.child.after
+        return self.child.after
+
+    def switch (self):
+        self.child.sendline ('switch')
+
+    def set_depth (self, depth):
+        self.child.sendline ('depth')
+        self.child.expect ('depth=')
+        self.child.sendline ('%d' % depth)
+
+    def quit(self):
+        self.child.sendline ('quit')
+import sys
+print('Starting...')
+white = Chess()
+white.do_move('b2b4')
+white.read_until_cursor (19,60)
+c1 = white.term.get_abs(17,58)
+c2 = white.term.get_abs(17,59)
+c3 = white.term.get_abs(17,60)
+c4 = white.term.get_abs(17,61)
+fout = open ('log','a')
+fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
+fout.close()
+white.do_move('c2c4')
+white.read_until_cursor (19,60)
+c1 = white.term.get_abs(17,58)
+c2 = white.term.get_abs(17,59)
+c3 = white.term.get_abs(17,60)
+c4 = white.term.get_abs(17,61)
+fout = open ('log','a')
+fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
+fout.close()
+white.do_scan ()
+
+#white.do_move ('b8a6')
+#move_white = white.get_computer_move()
+#print 'move white:', move_white
+
+sys.exit(1)
+
+
+
+black = Chess()
+white = Chess()
+white.child.expect ('Your move is')
+white.switch()
+
+move_white = white.get_first_computer_move()
+print('first move white:', move_white)
+
+black.do_first_move (move_white)
+move_black = black.get_first_computer_move()
+print('first move black:', move_black)
+
+white.do_move (move_black)
+
+done = 0
+while not done:
+    move_white = white.get_computer_move()
+    print('move white:', move_white)
+
+    black.do_move (move_white)
+    move_black = black.get_computer_move()
+    print('move black:', move_black)
+
+    white.do_move (move_black)
+    print('tail of loop')
+
+g.quit()
diff --git a/third_party/pexpect/examples/df.py b/third_party/pexpect/examples/df.py
new file mode 100755
index 0000000..4faa038
--- /dev/null
+++ b/third_party/pexpect/examples/df.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+'''This collects filesystem capacity info using the 'df' command. Tuples of
+filesystem name and percentage are stored in a list. A simple report is
+printed. Filesystems over 95% capacity are highlighted. Note that this does not
+parse filesystem names after the first space, so names with spaces in them will
+be truncated. This will produce ambiguous results for automount filesystems on
+Apple OSX.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import pexpect
+
+child = pexpect.spawn ('df')
+
+# parse 'df' output into a list.
+pattern = "\n(\S+).*?([0-9]+)%"
+filesystem_list = []
+for dummy in range (0, 1000):
+    i = child.expect ([pattern, pexpect.EOF])
+    if i == 0:
+        filesystem_list.append (child.match.groups())
+    else:
+        break
+
+# Print report
+print()
+for m in filesystem_list:
+    s = "Filesystem %s is at %s%%" % (m[0], m[1])
+    # highlight filesystems over 95% capacity
+    if int(m[1]) > 95:
+        s = '! ' + s
+    else:
+        s = '  ' + s
+    print(s)
+
diff --git a/third_party/pexpect/examples/ftp.py b/third_party/pexpect/examples/ftp.py
new file mode 100755
index 0000000..a1c1343
--- /dev/null
+++ b/third_party/pexpect/examples/ftp.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+
+'''This demonstrates an FTP "bookmark". This connects to an ftp site; does a
+few ftp stuff; and then gives the user interactive control over the session. In
+this case the "bookmark" is to a directory on the OpenBSD ftp server. It puts
+you in the i386 packages directory. You can easily modify this for other sites.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import pexpect
+import sys
+
+# Note that, for Python 3 compatibility reasons, we are using spawnu and
+# importing unicode_literals (above). spawnu accepts Unicode input and
+# unicode_literals makes all string literals in this script Unicode by default.
+child = pexpect.spawnu('ftp ftp.openbsd.org')
+
+child.expect('(?i)name .*: ')
+child.sendline('anonymous')
+child.expect('(?i)password')
+child.sendline('pexpect at sourceforge.net')
+child.expect('ftp> ')
+child.sendline('cd /pub/OpenBSD/3.7/packages/i386')
+child.expect('ftp> ')
+child.sendline('bin')
+child.expect('ftp> ')
+child.sendline('prompt')
+child.expect('ftp> ')
+child.sendline('pwd')
+child.expect('ftp> ')
+print("Escape character is '^]'.\n")
+sys.stdout.write (child.after)
+sys.stdout.flush()
+child.interact() # Escape character defaults to ^]
+# At this point this script blocks until the user presses the escape character
+# or until the child exits. The human user and the child should be talking
+# to each other now.
+
+# At this point the script is running again.
+print('Left interactve mode.')
+
+# The rest is not strictly necessary. This just demonstrates a few functions.
+# This makes sure the child is dead; although it would be killed when Python exits.
+if child.isalive():
+    child.sendline('bye') # Try to ask ftp child to exit.
+    child.close()
+# Print the final state of the child. Normally isalive() should be FALSE.
+if child.isalive():
+    print('Child did not exit gracefully.')
+else:
+    print('Child exited gracefully.')
+
diff --git a/third_party/pexpect/examples/hive.py b/third_party/pexpect/examples/hive.py
new file mode 100755
index 0000000..00ddbea
--- /dev/null
+++ b/third_party/pexpect/examples/hive.py
@@ -0,0 +1,466 @@
+#!/usr/bin/env python
+
+'''hive -- Hive Shell
+
+This lets you ssh to a group of servers and control them as if they were one.
+Each command you enter is sent to each host in parallel. The response of each
+host is collected and printed. In normal synchronous mode Hive will wait for
+each host to return the shell command line prompt. The shell prompt is used to
+sync output.
+
+Example:
+
+    $ hive.py --sameuser --samepass host1.example.com host2.example.net
+    username: myusername
+    password:
+    connecting to host1.example.com - OK
+    connecting to host2.example.net - OK
+    targetting hosts: 192.168.1.104 192.168.1.107
+    CMD (? for help) > uptime
+    =======================================================================
+    host1.example.com
+    -----------------------------------------------------------------------
+    uptime
+    23:49:55 up 74 days,  5:14,  2 users,  load average: 0.15, 0.05, 0.01
+    =======================================================================
+    host2.example.net
+    -----------------------------------------------------------------------
+    uptime
+    23:53:02 up 1 day, 13:36,  2 users,  load average: 0.50, 0.40, 0.46
+    =======================================================================
+
+Other Usage Examples:
+
+1. You will be asked for your username and password for each host.
+
+    hive.py host1 host2 host3 ... hostN
+
+2. You will be asked once for your username and password.
+   This will be used for each host.
+
+    hive.py --sameuser --samepass host1 host2 host3 ... hostN
+
+3. Give a username and password on the command-line:
+
+    hive.py user1:pass2 at host1 user2:pass2 at host2 ... userN:passN at hostN
+
+You can use an extended host notation to specify username, password, and host
+instead of entering auth information interactively. Where you would enter a
+host name use this format:
+
+    username:password at host
+
+This assumes that ':' is not part of the password. If your password contains a
+':' then you can use '\\:' to indicate a ':' and '\\\\' to indicate a single
+'\\'. Remember that this information will appear in the process listing. Anyone
+on your machine can see this auth information. This is not secure.
+
+This is a crude script that begs to be multithreaded. But it serves its
+purpose.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+# TODO add feature to support username:password at host combination
+# TODO add feature to log each host output in separate file
+
+import sys
+import os
+import re
+import optparse
+import time
+import getpass
+import readline
+import atexit
+try:
+    import pexpect
+    import pxssh
+except ImportError:
+    sys.stderr.write("You do not have 'pexpect' installed.\n")
+    sys.stderr.write("On Ubuntu you need the 'python-pexpect' package.\n")
+    sys.stderr.write("    aptitude -y install python-pexpect\n")
+    exit(1)
+
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+
+histfile = os.path.join(os.environ["HOME"], ".hive_history")
+try:
+    readline.read_history_file(histfile)
+except IOError:
+    pass
+atexit.register(readline.write_history_file, histfile)
+
+CMD_HELP='''Hive commands are preceded by a colon : (just think of vi).
+
+:target name1 name2 name3 ...
+
+    set list of hosts to target commands
+
+:target all
+
+    reset list of hosts to target all hosts in the hive.
+
+:to name command
+
+    send a command line to the named host. This is similar to :target, but
+    sends only one command and does not change the list of targets for future
+    commands.
+
+:sync
+
+    set mode to wait for shell prompts after commands are run. This is the
+    default. When Hive first logs into a host it sets a special shell prompt
+    pattern that it can later look for to synchronize output of the hosts. If
+    you 'su' to another user then it can upset the synchronization. If you need
+    to run something like 'su' then use the following pattern:
+
+    CMD (? for help) > :async
+    CMD (? for help) > sudo su - root
+    CMD (? for help) > :prompt
+    CMD (? for help) > :sync
+
+:async
+
+    set mode to not expect command line prompts (see :sync). Afterwards
+    commands are send to target hosts, but their responses are not read back
+    until :sync is run. This is useful to run before commands that will not
+    return with the special shell prompt pattern that Hive uses to synchronize.
+
+:refresh
+
+    refresh the display. This shows the last few lines of output from all hosts.
+    This is similar to resync, but does not expect the promt. This is useful
+    for seeing what hosts are doing during long running commands.
+
+:resync
+
+    This is similar to :sync, but it does not change the mode. It looks for the
+    prompt and thus consumes all input from all targetted hosts.
+
+:prompt
+
+    force each host to reset command line prompt to the special pattern used to
+    synchronize all the hosts. This is useful if you 'su' to a different user
+    where Hive would not know the prompt to match.
+
+:send my text
+
+    This will send the 'my text' wihtout a line feed to the targetted hosts.
+    This output of the hosts is not automatically synchronized.
+
+:control X
+
+    This will send the given control character to the targetted hosts.
+    For example, ":control c" will send ASCII 3.
+
+:exit
+
+    This will exit the hive shell.
+
+'''
+
+def login (args, cli_username=None, cli_password=None):
+
+    # I have to keep a separate list of host names because Python dicts are not ordered.
+    # I want to keep the same order as in the args list.
+    host_names = []
+    hive_connect_info = {}
+    hive = {}
+    # build up the list of connection information (hostname, username, password, port)
+    for host_connect_string in args:
+        hcd = parse_host_connect_string (host_connect_string)
+        hostname = hcd['hostname']
+        port     = hcd['port']
+        if port == '':
+            port = None
+        if len(hcd['username']) > 0:
+            username = hcd['username']
+        elif cli_username is not None:
+            username = cli_username
+        else:
+            username = raw_input('%s username: ' % hostname)
+        if len(hcd['password']) > 0:
+            password = hcd['password']
+        elif cli_password is not None:
+            password = cli_password
+        else:
+            password = getpass.getpass('%s password: ' % hostname)
+        host_names.append(hostname)
+        hive_connect_info[hostname] = (hostname, username, password, port)
+    # build up the list of hive connections using the connection information.
+    for hostname in host_names:
+        print('connecting to', hostname)
+        try:
+            fout = file("log_"+hostname, "w")
+            hive[hostname] = pxssh.pxssh()
+            # Disable host key checking.
+            hive[hostname].SSH_OPTS = (hive[hostname].SSH_OPTS
+                    + " -o 'StrictHostKeyChecking=no'"
+                    + " -o 'UserKnownHostsFile /dev/null' ")
+            hive[hostname].force_password = True
+            hive[hostname].login(*hive_connect_info[hostname])
+            print(hive[hostname].before)
+            hive[hostname].logfile = fout
+            print('- OK')
+        except Exception as e:
+            print('- ERROR', end=' ')
+            print(str(e))
+            print('Skipping', hostname)
+            hive[hostname] = None
+    return host_names, hive
+
+def main ():
+
+    global options, args, CMD_HELP
+
+    rows = 24
+    cols = 80
+
+    if options.sameuser:
+        cli_username = raw_input('username: ')
+    else:
+        cli_username = None
+
+    if options.samepass:
+        cli_password = getpass.getpass('password: ')
+    else:
+        cli_password = None
+
+    host_names, hive = login(args, cli_username, cli_password)
+
+    synchronous_mode = True
+    target_hostnames = host_names[:]
+    print('targetting hosts:', ' '.join(target_hostnames))
+    while True:
+        cmd = raw_input('CMD (? for help) > ')
+        cmd = cmd.strip()
+        if cmd=='?' or cmd==':help' or cmd==':h':
+            print(CMD_HELP)
+            continue
+        elif cmd==':refresh':
+            refresh (hive, target_hostnames, timeout=0.5)
+            for hostname in target_hostnames:
+                print('/' + '=' * (cols - 2))
+                print('| ' + hostname)
+                print('\\' + '-' * (cols - 2))
+                if hive[hostname] is None:
+                    print('# DEAD: %s' % hostname)
+                else:
+                    print(hive[hostname].before)
+            print('#' * 79)
+            continue
+        elif cmd==':resync':
+            resync (hive, target_hostnames, timeout=0.5)
+            for hostname in target_hostnames:
+                print('/' + '=' * (cols - 2))
+                print('| ' + hostname)
+                print('\\' + '-' * (cols - 2))
+                if hive[hostname] is None:
+                    print('# DEAD: %s' % hostname)
+                else:
+                    print(hive[hostname].before)
+            print('#' * 79)
+            continue
+        elif cmd==':sync':
+            synchronous_mode = True
+            resync (hive, target_hostnames, timeout=0.5)
+            continue
+        elif cmd==':async':
+            synchronous_mode = False
+            continue
+        elif cmd==':prompt':
+            for hostname in target_hostnames:
+                try:
+                    if hive[hostname] is not None:
+                        hive[hostname].set_unique_prompt()
+                except Exception as e:
+                    print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                    print(str(e))
+                    hive[hostname] = None
+            continue
+        elif cmd[:5] == ':send':
+            cmd, txt = cmd.split(None,1)
+            for hostname in target_hostnames:
+                try:
+                    if hive[hostname] is not None:
+                        hive[hostname].send(txt)
+                except Exception as e:
+                    print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                    print(str(e))
+                    hive[hostname] = None
+            continue
+        elif cmd[:3] == ':to':
+            cmd, hostname, txt = cmd.split(None,2)
+            print('/' + '=' * (cols - 2))
+            print('| ' + hostname)
+            print('\\' + '-' * (cols - 2))
+            if hive[hostname] is None:
+                print('# DEAD: %s' % hostname)
+                continue
+            try:
+                hive[hostname].sendline (txt)
+                hive[hostname].prompt(timeout=2)
+                print(hive[hostname].before)
+            except Exception as e:
+                print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                print(str(e))
+                hive[hostname] = None
+            continue
+        elif cmd[:7] == ':expect':
+            cmd, pattern = cmd.split(None,1)
+            print('looking for', pattern)
+            try:
+                for hostname in target_hostnames:
+                    if hive[hostname] is not None:
+                        hive[hostname].expect(pattern)
+                        print(hive[hostname].before)
+            except Exception as e:
+                print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                print(str(e))
+                hive[hostname] = None
+            continue
+        elif cmd[:7] == ':target':
+            target_hostnames = cmd.split()[1:]
+            if len(target_hostnames) == 0 or target_hostnames[0] == all:
+                target_hostnames = host_names[:]
+            print('targetting hosts:', ' '.join(target_hostnames))
+            continue
+        elif cmd == ':exit' or cmd == ':q' or cmd == ':quit':
+            break
+        elif cmd[:8] == ':control' or cmd[:5] == ':ctrl' :
+            cmd, c = cmd.split(None,1)
+            if ord(c)-96 < 0 or ord(c)-96 > 255:
+                print('/' + '=' * (cols - 2))
+                print('| Invalid character. Must be [a-zA-Z], @, [, ], \\, ^, _, or ?')
+                print('\\' + '-' * (cols - 2))
+                continue
+            for hostname in target_hostnames:
+                try:
+                    if hive[hostname] is not None:
+                        hive[hostname].sendcontrol(c)
+                except Exception as e:
+                    print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                    print(str(e))
+                    hive[hostname] = None
+            continue
+        elif cmd == ':esc':
+            for hostname in target_hostnames:
+                if hive[hostname] is not None:
+                    hive[hostname].send(chr(27))
+            continue
+        #
+        # Run the command on all targets in parallel
+        #
+        for hostname in target_hostnames:
+            try:
+                if hive[hostname] is not None:
+                    hive[hostname].sendline (cmd)
+            except Exception as e:
+                print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                print(str(e))
+                hive[hostname] = None
+
+        #
+        # print the response for each targeted host.
+        #
+        if synchronous_mode:
+            for hostname in target_hostnames:
+                try:
+                    print('/' + '=' * (cols - 2))
+                    print('| ' + hostname)
+                    print('\\' + '-' * (cols - 2))
+                    if hive[hostname] is None:
+                        print('# DEAD: %s' % hostname)
+                    else:
+                        hive[hostname].prompt(timeout=2)
+                        print(hive[hostname].before)
+                except Exception as e:
+                    print("Had trouble communicating with %s, so removing it from the target list." % hostname)
+                    print(str(e))
+                    hive[hostname] = None
+            print('#' * 79)
+
+def refresh (hive, hive_names, timeout=0.5):
+
+    '''This waits for the TIMEOUT on each host.
+    '''
+
+    # TODO This is ideal for threading.
+    for hostname in hive_names:
+        if hive[hostname] is not None:
+            hive[hostname].expect([pexpect.TIMEOUT,pexpect.EOF],timeout=timeout)
+
+def resync (hive, hive_names, timeout=2, max_attempts=5):
+
+    '''This waits for the shell prompt for each host in an effort to try to get
+    them all to the same state. The timeout is set low so that hosts that are
+    already at the prompt will not slow things down too much. If a prompt match
+    is made for a hosts then keep asking until it stops matching. This is a
+    best effort to consume all input if it printed more than one prompt. It's
+    kind of kludgy. Note that this will always introduce a delay equal to the
+    timeout for each machine. So for 10 machines with a 2 second delay you will
+    get AT LEAST a 20 second delay if not more. '''
+
+    # TODO This is ideal for threading.
+    for hostname in hive_names:
+        if hive[hostname] is not None:
+            for attempts in range(0, max_attempts):
+                if not hive[hostname].prompt(timeout=timeout):
+                    break
+
+def parse_host_connect_string (hcs):
+
+    '''This parses a host connection string in the form
+    username:password at hostname:port. All fields are options expcet hostname. A
+    dictionary is returned with all four keys. Keys that were not included are
+    set to empty strings ''. Note that if your password has the '@' character
+    then you must backslash escape it. '''
+
+    if '@' in hcs:
+        p = re.compile (r'(?P<username>[^@:]*)(:?)(?P<password>.*)(?!\\)@(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+    else:
+        p = re.compile (r'(?P<username>)(?P<password>)(?P<hostname>[^:]*):?(?P<port>[0-9]*)')
+    m = p.search (hcs)
+    d = m.groupdict()
+    d['password'] = d['password'].replace('\\@','@')
+    return d
+
+if __name__ == '__main__':
+    start_time = time.time()
+    parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), usage=globals()['__doc__'], version='$Id: hive.py 533 2012-10-20 02:19:33Z noah $',conflict_handler="resolve")
+    parser.add_option ('-v', '--verbose', action='store_true', default=False, help='verbose output')
+    parser.add_option ('--samepass', action='store_true', default=False, help='Use same password for each login.')
+    parser.add_option ('--sameuser', action='store_true', default=False, help='Use same username for each login.')
+    (options, args) = parser.parse_args()
+    if len(args) < 1:
+        parser.error ('missing argument')
+    if options.verbose: print(time.asctime())
+    main()
+    if options.verbose: print(time.asctime())
+    if options.verbose: print('TOTAL TIME IN MINUTES:', end=' ')
+    if options.verbose: print((time.time() - start_time) / 60.0)
diff --git a/third_party/pexpect/examples/monitor.py b/third_party/pexpect/examples/monitor.py
new file mode 100755
index 0000000..9cb0eaf
--- /dev/null
+++ b/third_party/pexpect/examples/monitor.py
@@ -0,0 +1,229 @@
+#!/usr/bin/env python
+
+''' This runs a sequence of commands on a remote host using SSH. It runs a
+simple system checks such as uptime and free to monitor the state of the remote
+host.
+
+./monitor.py [-s server_hostname] [-u username] [-p password]
+    -s : hostname of the remote server to login to.
+    -u : username to user for login.
+    -p : Password to user for login.
+
+Example:
+    This will print information about the given host:
+        ./monitor.py -s www.example.com -u mylogin -p mypassword
+
+It works like this:
+    Login via SSH (This is the hardest part).
+    Run and parse 'uptime'.
+    Run 'iostat'.
+    Run 'vmstat'.
+    Run 'netstat'
+    Run 'free'.
+    Exit the remote host.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import os, sys, re, getopt, getpass
+import pexpect
+
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+
+#
+# Some constants.
+#
+COMMAND_PROMPT = '[#$] ' ### This is way too simple for industrial use -- we will change is ASAP.
+TERMINAL_PROMPT = '(?i)terminal type\?'
+TERMINAL_TYPE = 'vt100'
+# This is the prompt we get if SSH does not have the remote host's public key stored in the cache.
+SSH_NEWKEY = '(?i)are you sure you want to continue connecting'
+
+def exit_with_usage():
+
+    print(globals()['__doc__'])
+    os._exit(1)
+
+def main():
+
+    global COMMAND_PROMPT, TERMINAL_PROMPT, TERMINAL_TYPE, SSH_NEWKEY
+    ######################################################################
+    ## Parse the options, arguments, get ready, etc.
+    ######################################################################
+    try:
+        optlist, args = getopt.getopt(sys.argv[1:], 'h?s:u:p:', ['help','h','?'])
+    except Exception as e:
+        print(str(e))
+        exit_with_usage()
+    options = dict(optlist)
+    if len(args) > 1:
+        exit_with_usage()
+
+    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+        print("Help:")
+        exit_with_usage()
+
+    if '-s' in options:
+        host = options['-s']
+    else:
+        host = raw_input('hostname: ')
+    if '-u' in options:
+        user = options['-u']
+    else:
+        user = raw_input('username: ')
+    if '-p' in options:
+        password = options['-p']
+    else:
+        password = getpass.getpass('password: ')
+
+    #
+    # Login via SSH
+    #
+    child = pexpect.spawn('ssh -l %s %s'%(user, host))
+    i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
+    if i == 0: # Timeout
+        print('ERROR! could not login with SSH. Here is what SSH said:')
+        print(child.before, child.after)
+        print(str(child))
+        sys.exit (1)
+    if i == 1: # In this case SSH does not have the public key cached.
+        child.sendline ('yes')
+        child.expect ('(?i)password')
+    if i == 2:
+        # This may happen if a public key was setup to automatically login.
+        # But beware, the COMMAND_PROMPT at this point is very trivial and
+        # could be fooled by some output in the MOTD or login message.
+        pass
+    if i == 3:
+        child.sendline(password)
+        # Now we are either at the command prompt or
+        # the login process is asking for our terminal type.
+        i = child.expect ([COMMAND_PROMPT, TERMINAL_PROMPT])
+        if i == 1:
+            child.sendline (TERMINAL_TYPE)
+            child.expect (COMMAND_PROMPT)
+    #
+    # Set command prompt to something more unique.
+    #
+    COMMAND_PROMPT = "\[PEXPECT\]\$ "
+    child.sendline ("PS1='[PEXPECT]\$ '") # In case of sh-style
+    i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
+    if i == 0:
+        print("# Couldn't set sh-style prompt -- trying csh-style.")
+        child.sendline ("set prompt='[PEXPECT]\$ '")
+        i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
+        if i == 0:
+            print("Failed to set command prompt using sh or csh style.")
+            print("Response was:")
+            print(child.before)
+            sys.exit (1)
+
+    # Now we should be at the command prompt and ready to run some commands.
+    print('---------------------------------------')
+    print('Report of commands run on remote host.')
+    print('---------------------------------------')
+
+    # Run uname.
+    child.sendline ('uname -a')
+    child.expect (COMMAND_PROMPT)
+    print(child.before)
+    if 'linux' in child.before.lower():
+        LINUX_MODE = 1
+    else:
+        LINUX_MODE = 0
+
+    # Run and parse 'uptime'.
+    child.sendline ('uptime')
+    child.expect('up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
+    duration, users, av1, av5, av15 = child.match.groups()
+    days = '0'
+    hours = '0'
+    mins = '0'
+    if 'day' in duration:
+        child.match = re.search('([0-9]+)\s+day',duration)
+        days = str(int(child.match.group(1)))
+    if ':' in duration:
+        child.match = re.search('([0-9]+):([0-9]+)',duration)
+        hours = str(int(child.match.group(1)))
+        mins = str(int(child.match.group(2)))
+    if 'min' in duration:
+        child.match = re.search('([0-9]+)\s+min',duration)
+        mins = str(int(child.match.group(1)))
+    print()
+    print('Uptime: %s days, %s users, %s (1 min), %s (5 min), %s (15 min)' % (
+        duration, users, av1, av5, av15))
+    child.expect (COMMAND_PROMPT)
+
+    # Run iostat.
+    child.sendline ('iostat')
+    child.expect (COMMAND_PROMPT)
+    print(child.before)
+
+    # Run vmstat.
+    child.sendline ('vmstat')
+    child.expect (COMMAND_PROMPT)
+    print(child.before)
+
+    # Run free.
+    if LINUX_MODE:
+        child.sendline ('free') # Linux systems only.
+        child.expect (COMMAND_PROMPT)
+        print(child.before)
+
+    # Run df.
+    child.sendline ('df')
+    child.expect (COMMAND_PROMPT)
+    print(child.before)
+
+    # Run lsof.
+    child.sendline ('lsof')
+    child.expect (COMMAND_PROMPT)
+    print(child.before)
+
+#    # Run netstat
+#    child.sendline ('netstat')
+#    child.expect (COMMAND_PROMPT)
+#    print child.before
+
+#    # Run MySQL show status.
+#    child.sendline ('mysql -p -e "SHOW STATUS;"')
+#    child.expect (PASSWORD_PROMPT_MYSQL)
+#    child.sendline (password_mysql)
+#    child.expect (COMMAND_PROMPT)
+#    print
+#    print child.before
+
+    # Now exit the remote host.
+    child.sendline ('exit')
+    index = child.expect([pexpect.EOF, "(?i)there are stopped jobs"])
+    if index==1:
+        child.sendline("exit")
+        child.expect(EOF)
+
+if __name__ == "__main__":
+    main()
diff --git a/third_party/pexpect/examples/passmass.py b/third_party/pexpect/examples/passmass.py
new file mode 100755
index 0000000..8f5e21d
--- /dev/null
+++ b/third_party/pexpect/examples/passmass.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python
+
+'''Change passwords on the named machines. passmass host1 host2 host3 . . .
+Note that login shell prompt on remote machine must end in # or $.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import pexpect
+import sys, getpass
+
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+
+USAGE = '''passmass host1 host2 host3 . . .'''
+COMMAND_PROMPT = '[$#] '
+TERMINAL_PROMPT = r'Terminal type\?'
+TERMINAL_TYPE = 'vt100'
+SSH_NEWKEY = r'Are you sure you want to continue connecting \(yes/no\)\?'
+
+def login(host, user, password):
+
+    child = pexpect.spawn('ssh -l %s %s'%(user, host))
+    fout = file ("LOG.TXT","wb")
+    child.setlog (fout)
+
+    i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, '[Pp]assword: '])
+    if i == 0: # Timeout
+        print('ERROR!')
+        print('SSH could not login. Here is what SSH said:')
+        print(child.before, child.after)
+        sys.exit (1)
+    if i == 1: # SSH does not have the public key. Just accept it.
+        child.sendline ('yes')
+        child.expect ('[Pp]assword: ')
+    child.sendline(password)
+    # Now we are either at the command prompt or
+    # the login process is asking for our terminal type.
+    i = child.expect (['Permission denied', TERMINAL_PROMPT, COMMAND_PROMPT])
+    if i == 0:
+        print('Permission denied on host:', host)
+        sys.exit (1)
+    if i == 1:
+        child.sendline (TERMINAL_TYPE)
+        child.expect (COMMAND_PROMPT)
+    return child
+
+# (current) UNIX password:
+def change_password(child, user, oldpassword, newpassword):
+
+    child.sendline('passwd')
+    i = child.expect(['[Oo]ld [Pp]assword', '.current.*password', '[Nn]ew [Pp]assword'])
+    # Root does not require old password, so it gets to bypass the next step.
+    if i == 0 or i == 1:
+        child.sendline(oldpassword)
+        child.expect('[Nn]ew [Pp]assword')
+    child.sendline(newpassword)
+    i = child.expect(['[Nn]ew [Pp]assword', '[Rr]etype', '[Rr]e-enter'])
+    if i == 0:
+        print('Host did not like new password. Here is what it said...')
+        print(child.before)
+        child.send (chr(3)) # Ctrl-C
+        child.sendline('') # This should tell remote passwd command to quit.
+        return
+    child.sendline(newpassword)
+
+def main():
+
+    if len(sys.argv) <= 1:
+        print(USAGE)
+        return 1
+
+    user = raw_input('Username: ')
+    password = getpass.getpass('Current Password: ')
+    newpassword = getpass.getpass('New Password: ')
+    newpasswordconfirm = getpass.getpass('Confirm New Password: ')
+    if newpassword != newpasswordconfirm:
+        print('New Passwords do not match.')
+        return 1
+
+    for host in sys.argv[1:]:
+        child = login(host, user, password)
+        if child == None:
+            print('Could not login to host:', host)
+            continue
+        print('Changing password on host:', host)
+        change_password(child, user, password, newpassword)
+        child.expect(COMMAND_PROMPT)
+        child.sendline('exit')
+
+if __name__ == '__main__':
+    main()
diff --git a/third_party/pexpect/examples/python.py b/third_party/pexpect/examples/python.py
new file mode 100755
index 0000000..44c15e1
--- /dev/null
+++ b/third_party/pexpect/examples/python.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+'''This starts the python interpreter; captures the startup message; then gives
+the user interactive control over the session. Why? For fun...
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import pexpect
+
+# Don't do this unless you like being John Malkovich
+# c = pexpect.spawnu('/usr/bin/env python ./python.py')
+
+# Note that, for Python 3 compatibility reasons, we are using spawnu and
+# importing unicode_literals (above). spawnu accepts Unicode input and
+# unicode_literals makes all string literals in this script Unicode by default.
+c = pexpect.spawnu('/usr/bin/env python')
+
+c.expect('>>>')
+print('And now for something completely different...')
+print(''.join(reversed((c.before))))
+print('Yes, it\'s python, but it\'s backwards.')
+print()
+print('Escape character is \'^]\'.')
+print(c.after, end=' ')
+c.interact()
+c.kill(1)
+print('is alive:', c.isalive())
+
diff --git a/third_party/pexpect/examples/script.py b/third_party/pexpect/examples/script.py
new file mode 100755
index 0000000..c8b9496
--- /dev/null
+++ b/third_party/pexpect/examples/script.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python
+
+'''This spawns a sub-shell (bash) and gives the user interactive control. The
+entire shell session is logged to a file called script.log. This behaves much
+like the classic BSD command 'script'.
+
+./script.py [-a] [-c command] {logfilename}
+
+    logfilename : This is the name of the log file. Default is script.log.
+    -a : Append to log file. Default is to overwrite log file.
+    -c : spawn command. Default is to spawn the sh shell.
+
+Example:
+
+    This will start a bash shell and append to the log named my_session.log:
+
+        ./script.py -a -c bash my_session.log
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import os, sys, time, getopt
+import signal, fcntl, termios, struct
+import pexpect
+
+global_pexpect_instance = None # Used by signal handler
+
+def exit_with_usage():
+
+    print(globals()['__doc__'])
+    os._exit(1)
+
+def main():
+
+    ######################################################################
+    # Parse the options, arguments, get ready, etc.
+    ######################################################################
+    try:
+        optlist, args = getopt.getopt(sys.argv[1:], 'h?ac:', ['help','h','?'])
+    except Exception as e:
+        print(str(e))
+        exit_with_usage()
+    options = dict(optlist)
+    if len(args) > 1:
+        exit_with_usage()
+
+    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+        print("Help:")
+        exit_with_usage()
+
+    if len(args) == 1:
+        script_filename = args[0]
+    else:
+        script_filename = "script.log"
+    if '-a' in options:
+        fout = open(script_filename, "ab")
+    else:
+        fout = open(script_filename, "wb")
+    if '-c' in options:
+        command = options['-c']
+    else:
+        command = "sh"
+
+    # Begin log with date/time in the form CCCCyymm.hhmmss
+    fout.write ('# %4d%02d%02d.%02d%02d%02d \n' % time.localtime()[:-3])
+
+    ######################################################################
+    # Start the interactive session
+    ######################################################################
+    p = pexpect.spawn(command)
+    p.logfile = fout
+    global global_pexpect_instance
+    global_pexpect_instance = p
+    signal.signal(signal.SIGWINCH, sigwinch_passthrough)
+
+    print("Script recording started. Type ^] (ASCII 29) to escape from the script shell.")
+    p.interact(chr(29))
+    fout.close()
+    return 0
+
+def sigwinch_passthrough (sig, data):
+
+    # Check for buggy platforms (see pexpect.setwinsize()).
+    if 'TIOCGWINSZ' in dir(termios):
+        TIOCGWINSZ = termios.TIOCGWINSZ
+    else:
+        TIOCGWINSZ = 1074295912 # assume
+    s = struct.pack ("HHHH", 0, 0, 0, 0)
+    a = struct.unpack ('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ , s))
+    global global_pexpect_instance
+    global_pexpect_instance.setwinsize(a[0],a[1])
+
+if __name__ == "__main__":
+    main()
diff --git a/third_party/pexpect/examples/ssh_tunnel.py b/third_party/pexpect/examples/ssh_tunnel.py
new file mode 100755
index 0000000..d761911
--- /dev/null
+++ b/third_party/pexpect/examples/ssh_tunnel.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+
+'''This starts an SSH tunnel to a given host. If the SSH process ever dies then
+this script will detect that and restart it. I use this under Cygwin to keep
+open encrypted tunnels to port 25 (SMTP), port 143 (IMAP4), and port 110
+(POP3). I set my mail client to talk to localhost and I keep this script
+running in the background.
+
+Note that this is a rather stupid script at the moment because it just looks to
+see if any ssh process is running. It should really make sure that our specific
+ssh process is running. The problem is that ssh is missing a very useful
+feature. It has no way to report the process id of the background daemon that
+it creates with the -f command. This would be a really useful script if I could
+figure a way around this problem.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import print_function
+
+from __future__ import absolute_import
+
+import pexpect
+import getpass
+import time
+
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+
+# SMTP:25 IMAP4:143 POP3:110
+tunnel_command = 'ssh -C -N -f -L 25:127.0.0.1:25 -L 143:127.0.0.1:143 -L 110:127.0.0.1:110 %(user)@%(host)'
+host = raw_input('Hostname: ')
+user = raw_input('Username: ')
+X = getpass.getpass('Password: ')
+
+def get_process_info ():
+
+    # This seems to work on both Linux and BSD, but should otherwise be considered highly UNportable.
+
+    ps = pexpect.run ('ps ax -O ppid')
+    pass
+
+def start_tunnel ():
+
+    try:
+        ssh_tunnel = pexpect.spawn (tunnel_command % globals())
+        ssh_tunnel.expect ('password:')
+        time.sleep (0.1)
+        ssh_tunnel.sendline (X)
+        time.sleep (60) # Cygwin is slow to update process status.
+        ssh_tunnel.expect (pexpect.EOF)
+
+    except Exception as e:
+        print(str(e))
+
+def main ():
+
+    while True:
+        ps = pexpect.spawn ('ps')
+        time.sleep (1)
+        index = ps.expect (['/usr/bin/ssh', pexpect.EOF, pexpect.TIMEOUT])
+        if index == 2:
+            print('TIMEOUT in ps command...')
+            print(str(ps))
+            time.sleep (13)
+        if index == 1:
+            print(time.asctime(), end=' ')
+            print('restarting tunnel')
+            start_tunnel ()
+            time.sleep (11)
+            print('tunnel OK')
+        else:
+            # print 'tunnel OK'
+            time.sleep (7)
+
+if __name__ == '__main__':
+
+    main ()
+
+# This was for older SSH versions that didn't have -f option
+#tunnel_command = 'ssh -C -n -L 25:%(host)s:25 -L 110:%(host)s:110 %(user)s@%(host)s -f nothing.sh'
+#nothing_script = '''#!/bin/sh
+#while true; do sleep 53; done
+#'''
+
diff --git a/third_party/pexpect/examples/table_test.html b/third_party/pexpect/examples/table_test.html
new file mode 100644
index 0000000..5dba0ec
--- /dev/null
+++ b/third_party/pexpect/examples/table_test.html
@@ -0,0 +1,106 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+<title>TEST</title>
+</head>
+<style type="text/css">
+a {color: #9f9; text-decoration: none}
+a:hover {color: #0f0}
+hr {color: #0f0}
+html,table,body,textarea,input,form
+{
+font-family: "Courier New", Courier, mono; 
+font-size: 8pt; 
+color: #0c0;
+background-color: #020;
+margin:0;
+padding:0;
+border:0;
+}
+input { background-color: #010; }
+textarea {
+border-width:1;
+border-style:solid;
+border-color:#0c0;
+padding:3;
+margin:3;
+}
+</style>
+<script>
+var foo="" +
+" 123456789012345678901234567890123456789012345 789012345678901234567890123456789"+
+"0 2345678901234567890123456789012345678901234 6 89012345678901234567890123456789"+
+"01 34567890123456789012345678901234567890123 567 9012345678901234567890123456789"+
+"012 456789012345678901234567890123456789012 45678 012345678901234567890123456789"+
+"0123 5678901234567890123456789012345678901 3456789 12345678901234567890123456789"+
+"01234 67890123456789012345678901234567890 234567890 2345678901234567890123456789"+
+"012345 789012345678901234567890123456789 12345678901 345678901234567890123456789"+
+"0123456 8901234567890123456789012345678 0123456789012 45678901234567890123456789"+
+"01234567 90123456789012345678901234567 901234567890123 5678901234567890123456789"+
+"012345678 012345678901234567890123456 89012345678901234 678901234567890123456789"+
+"0123456789 1234567890123456789012345 7890123456789012345 78901234567890123456789"+
+"01234567890 23456789012345678901234 678901234567890123456 8901234567890123456789"+
+"012345678901 345678901234567890123 56789012345678901234567 901234567890123456789"+
+"0123456789012 4567890123456789012 4567890123456789012345678 0123456789012345678 "+
+"01234567890123 56789012345678901 345678901234567890123456789 12345678901234567 9"+
+"012345678901234 678901234567890 23456789012  567  01234567890 234567890123456 89"+
+"0123456789012345 7890123456789 123457789012  567  012345678901 3456789012345 789"+
+"01234567890123456 89012345678 012345678901234567890123456789012 45678901234 6789"+
+"012345678901234567 901234567 90123456789 12345678901 34567890123 567890123 56789"+
+"0123456789012345678 0123456 8901234567890  3456789  2345678901234 6789012 456789"+
+"01234567890123456789 12345 7890123456789012       0123456789012345 78901 3456789"+
+"012345678901234567890 234 67890123456789012345678901234567890123456 890 23456789"+
+"0123456789012345678901 3 5678901234567890123456789012345678901234567 9 123456789"+
+"01234567890123456789012 456789012345678901234567890123456789012345678 0123456789";
+function start2()
+{
+    // get the reference for the body
+    //var mybody = document.getElementsByTagName("body")[0];
+    var mybody = document.getElementById("replace_me");
+    var myroot = document.getElementById("a_parent");
+    mytable = document.createElement("table");
+    mytablebody = document.createElement("tbody");
+    mytable.setAttribute("border","0");
+    mytable.setAttribute("cellspacing","0");
+    mytable.setAttribute("cellpadding","0");
+    for(var j = 0; j < 24; j++)
+    {
+        mycurrent_row = document.createElement("tr");
+        for(var i = 0; i < 80; i++)
+        {
+            mycurrent_cell = document.createElement("td");
+            offset = (j*80)+i;
+            currenttext = document.createTextNode(foo.substring(offset,offset+1));
+            mycurrent_cell.appendChild(currenttext);
+            mycurrent_row.appendChild(mycurrent_cell);
+        }
+        mytablebody.appendChild(mycurrent_row);
+    }
+    mytable.appendChild(mytablebody);
+    myroot.replaceChild(mytable,mybody);
+    //mybody.appendChild(mytable);
+}
+</script>
+<body onload="start2();">
+<table align="LEFT" border="0" cellspacing="0" cellpadding="0">
+<div id="a_parent">
+<span id="replace_me">
+<tr align="left" valign="left">
+    <td>/</td>
+    <td>h</td>
+    <td>o</td>
+    <td>m</td>
+    <td>e</td>
+    <td>/</td>
+    <td>n</td>
+    <td>o</td>
+    <td>a</td>
+    <td>h</td>
+    <td>/</td>
+    <td> </td>
+</tr>
+</table>
+</span>
+</div>
+</body>
+</html>
\ No newline at end of file
diff --git a/third_party/pexpect/examples/topip.py b/third_party/pexpect/examples/topip.py
new file mode 100755
index 0000000..25bd100
--- /dev/null
+++ b/third_party/pexpect/examples/topip.py
@@ -0,0 +1,299 @@
+#!/usr/bin/env python
+
+''' This runs netstat on a local or remote server. It calculates some simple
+statistical information on the number of external inet connections. It groups
+by IP address. This can be used to detect if one IP address is taking up an
+excessive number of connections. It can also send an email alert if a given IP
+address exceeds a threshold between runs of the script. This script can be used
+as a drop-in Munin plugin or it can be used stand-alone from cron. I used this
+on a busy web server that would sometimes get hit with denial of service
+attacks. This made it easy to see if a script was opening many multiple
+connections. A typical browser would open fewer than 10 connections at once.
+A script might open over 100 simultaneous connections.
+
+./topip.py [-s server_hostname] [-u username] [-p password]
+        {-a from_addr,to_addr} {-n N} {-v} {--ipv6}
+
+    -s : hostname of the remote server to login to.
+    -u : username to user for login.
+    -p : password to user for login.
+    -n : print stddev for the the number of the top 'N' ipaddresses.
+    -v : verbose - print stats and list of top ipaddresses.
+    -a : send alert if stddev goes over 20.
+    -l : to log message to /var/log/topip.log
+    --ipv6 : this parses netstat output that includes ipv6 format.
+        Note that this actually only works with ipv4 addresses, but for
+        versions of netstat that print in ipv6 format.
+    --stdev=N : Where N is an integer. This sets the trigger point
+        for alerts and logs. Default is to trigger if the
+        max value is over 5 standard deviations.
+
+Example:
+
+    This will print stats for the top IP addresses connected to the given host:
+
+        ./topip.py -s www.example.com -u mylogin -p mypassword -n 10 -v
+
+    This will send an alert email if the maxip goes over the stddev trigger
+    value and the the current top ip is the same as the last top ip
+    (/tmp/topip.last):
+
+        ./topip.py -s www.example.com -u mylogin -p mypassword \\
+                -n 10 -v -a alert at example.com,user at example.com
+
+    This will print the connection stats for the localhost in Munin format:
+
+        ./topip.py
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
+
+# See http://pexpect.sourceforge.net/
+import pexpect
+import pxssh
+import os
+import sys
+import time
+import getopt
+import pickle
+import getpass
+import smtplib
+from pprint import pprint
+
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+
+TOPIP_LOG_FILE = '/var/log/topip.log'
+TOPIP_LAST_RUN_STATS = '/var/run/topip.last'
+
+def exit_with_usage():
+
+    print(globals()['__doc__'])
+    os._exit(1)
+
+def stats(r):
+
+    '''This returns a dict of the median, average, standard deviation,
+    min and max of the given sequence.
+
+    >>> from topip import stats
+    >>> print stats([5,6,8,9])
+    {'med': 8, 'max': 9, 'avg': 7.0, 'stddev': 1.5811388300841898, 'min': 5}
+    >>> print stats([1000,1006,1008,1014])
+    {'med': 1008, 'max': 1014, 'avg': 1007.0, 'stddev': 5.0, 'min': 1000}
+    >>> print stats([1,3,4,5,18,16,4,3,3,5,13])
+    {'med': 4, 'max': 18, 'avg': 6.8181818181818183, 'stddev': 5.6216817577237475, 'min': 1}
+    >>> print stats([1,3,4,5,18,16,4,3,3,5,13,14,5,6,7,8,7,6,6,7,5,6,4,14,7])
+    {'med': 6, 'max': 18, 'avg': 7.0800000000000001, 'stddev': 4.3259218670706474, 'min': 1}
+    '''
+
+    total = sum(r)
+    avg = float(total)/float(len(r))
+    sdsq = sum([(i-avg)**2 for i in r])
+    s = sorted(list(r))
+    return dict(list(zip(['med', 'avg', 'stddev', 'min', 'max'],
+        (s[len(s)//2], avg, (sdsq/len(r))**.5, min(r), max(r)))))
+
+def send_alert (message, subject, addr_from, addr_to, smtp_server='localhost'):
+
+    '''This sends an email alert.
+    '''
+
+    message = ( 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n'
+            % (addr_from, addr_to, subject) + message )
+    server = smtplib.SMTP(smtp_server)
+    server.sendmail(addr_from, addr_to, message)
+    server.quit()
+
+def main():
+
+    # Parse the options, arguments, etc.
+    try:
+        optlist, args = getopt.getopt(sys.argv[1:],
+                'h?valqs:u:p:n:', ['help','h','?','ipv6','stddev='])
+    except Exception as e:
+        print(str(e))
+        exit_with_usage()
+    options = dict(optlist)
+
+    munin_flag = False
+    if len(args) > 0:
+        if args[0] == 'config':
+            print('graph_title Netstat Connections per IP')
+            print('graph_vlabel Socket connections per IP')
+            print('connections_max.label max')
+            print('connections_max.info Maximum number of connections per IP')
+            print('connections_avg.label avg')
+            print('connections_avg.info Average number of connections per IP')
+            print('connections_stddev.label stddev')
+            print('connections_stddev.info Standard deviation')
+            return 0
+        elif args[0] != '':
+            print(args, len(args))
+            return 0
+            exit_with_usage()
+    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
+        print('Help:')
+        exit_with_usage()
+    if '-s' in options:
+        hostname = options['-s']
+    else:
+        # if host was not specified then assume localhost munin plugin.
+        munin_flag = True
+        hostname = 'localhost'
+    # If localhost then don't ask for username/password.
+    if hostname != 'localhost' and hostname != '127.0.0.1':
+        if '-u' in options:
+            username = options['-u']
+        else:
+            username = raw_input('username: ')
+        if '-p' in options:
+            password = options['-p']
+        else:
+            password = getpass.getpass('password: ')
+        use_localhost = False
+    else:
+        use_localhost = True
+
+    if '-l' in options:
+        log_flag = True
+    else:
+        log_flag = False
+    if '-n' in options:
+        average_n = int(options['-n'])
+    else:
+        average_n = None
+    if '-v' in options:
+        verbose = True
+    else:
+        verbose = False
+    if '-a' in options:
+        alert_flag = True
+        (alert_addr_from, alert_addr_to) = tuple(options['-a'].split(','))
+    else:
+        alert_flag = False
+    if '--ipv6' in options:
+        ipv6_flag = True
+    else:
+        ipv6_flag = False
+    if '--stddev' in options:
+        stddev_trigger = float(options['--stddev'])
+    else:
+        stddev_trigger = 5
+
+    if ipv6_flag:
+        netstat_pattern = '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+::ffff:(\S+):(\S+)\s+.*?\r'
+    else:
+        netstat_pattern = '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(?:::ffff:)*(\S+):(\S+)\s+.*?\r'
+        #netstat_pattern = '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+):(\S+)\s+.*?\r'
+
+    # run netstat (either locally or via SSH).
+    if use_localhost:
+        p = pexpect.spawn('netstat -n -t')
+        PROMPT = pexpect.TIMEOUT
+    else:
+        p = pxssh.pxssh()
+        p.login(hostname, username, password)
+        p.sendline('netstat -n -t')
+        PROMPT = p.PROMPT
+
+    # For each matching netstat_pattern put the ip address in the list.
+    ip_list = {}
+    try:
+        while 1:
+            i = p.expect([PROMPT, netstat_pattern])
+            if i == 0:
+                break
+            k = p.match.groups()[4].decode('utf-8')
+            if k in ip_list:
+                ip_list[k] = ip_list[k] + 1
+            else:
+                ip_list[k] = 1
+    except:
+        pass
+
+    # remove a few common, uninteresting addresses from the dictionary.
+    ip_list = dict([ (key,value) for key,value in ip_list.items() if '192.168.' not in key])
+    ip_list = dict([ (key,value) for key,value in ip_list.items() if '127.0.0.1' not in key])
+
+    ip_list = list(ip_list.items())
+    if len(ip_list) < 1:
+        if verbose: print('Warning: no networks connections worth looking at.')
+        return 0
+    ip_list.sort(key=lambda x:x[1])
+
+    # generate some stats for the ip addresses found.
+    if average_n is not None and average_n <= 1:
+        average_n = None
+    # Reminder: the * unary operator treats the list elements as arguments.
+    zipped = zip(*ip_list[0:average_n])
+    s = stats(list(zipped)[1])
+    s['maxip'] = ip_list[0]
+
+    # print munin-style or verbose results for the stats.
+    if munin_flag:
+        print('connections_max.value', s['max'])
+        print('connections_avg.value', s['avg'])
+        print('connections_stddev.value', s['stddev'])
+        return 0
+    if verbose:
+        pprint (s)
+        print()
+        pprint (ip_list[0:average_n])
+
+    # load the stats from the last run.
+    try:
+        last_stats = pickle.load(file(TOPIP_LAST_RUN_STATS))
+    except:
+        last_stats = {'maxip':None}
+
+    if ( s['maxip'][1] > (s['stddev'] * stddev_trigger)
+            and s['maxip']==last_stats['maxip'] ):
+        if verbose: print('The maxip has been above trigger for two consecutive samples.')
+        if alert_flag:
+            if verbose: print('SENDING ALERT EMAIL')
+            send_alert(str(s), 'ALERT on %s'
+                    % hostname, alert_addr_from, alert_addr_to)
+        if log_flag:
+            if verbose: print('LOGGING THIS EVENT')
+            fout = file(TOPIP_LOG_FILE,'a')
+            #dts = time.strftime('%Y:%m:%d:%H:%M:%S', time.localtime())
+            dts = time.asctime()
+            fout.write ('%s - %d connections from %s\n'
+                    % (dts,s['maxip'][1],str(s['maxip'][0])))
+            fout.close()
+
+    # save state to TOPIP_LAST_RUN_STATS
+    try:
+        pickle.dump(s, file(TOPIP_LAST_RUN_STATS,'w'))
+        os.chmod (TOPIP_LAST_RUN_STATS, 0o664)
+    except:
+        pass
+    # p.logout()
+
+if __name__ == '__main__':
+    main()
diff --git a/third_party/pexpect/examples/uptime.py b/third_party/pexpect/examples/uptime.py
new file mode 100755
index 0000000..659edfc
--- /dev/null
+++ b/third_party/pexpect/examples/uptime.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+
+'''This displays uptime information using uptime. This is redundant,
+but it demonstrates expecting for a regular expression that uses subgroups.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import pexpect
+import re
+
+# There are many different styles of uptime results. I try to parse them all. Yeee!
+# Examples from different machines:
+# [x86] Linux 2.4 (Redhat 7.3)
+#  2:06pm  up 63 days, 18 min,  3 users,  load average: 0.32, 0.08, 0.02
+# [x86] Linux 2.4.18-14 (Redhat 8.0)
+#  3:07pm  up 29 min,  1 user,  load average: 2.44, 2.51, 1.57
+# [PPC - G4] MacOS X 10.1 SERVER Edition
+# 2:11PM  up 3 days, 13:50, 3 users, load averages: 0.01, 0.00, 0.00
+# [powerpc] Darwin v1-58.corefa.com 8.2.0 Darwin Kernel Version 8.2.0
+# 10:35  up 18:06, 4 users, load averages: 0.52 0.47 0.36
+# [Sparc - R220] Sun Solaris (8)
+#  2:13pm  up 22 min(s),  1 user,  load average: 0.02, 0.01, 0.01
+# [x86] Linux 2.4.18-14 (Redhat 8)
+# 11:36pm  up 4 days, 17:58,  1 user,  load average: 0.03, 0.01, 0.00
+# AIX jwdir 2 5 0001DBFA4C00
+#  09:43AM   up  23:27,  1 user,  load average: 0.49, 0.32, 0.23
+# OpenBSD box3 2.9 GENERIC#653 i386
+#  6:08PM  up 4 days, 22:26, 1 user, load averages: 0.13, 0.09, 0.08
+
+# Note that, for Python 3 compatibility reasons, we are using spawnu and
+# importing unicode_literals (above). spawnu accepts Unicode input and
+# unicode_literals makes all string literals in this script Unicode by default.
+p = pexpect.spawnu('uptime')
+
+# This parses uptime output into the major groups using regex group matching.
+p.expect('up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
+duration, users, av1, av5, av15 = p.match.groups()
+
+# The duration is a little harder to parse because of all the different
+# styles of uptime. I'm sure there is a way to do this all at once with
+# one single regex, but I bet it would be hard to read and maintain.
+# If anyone wants to send me a version using a single regex I'd be happy to see it.
+days = '0'
+hours = '0'
+mins = '0'
+if 'day' in duration:
+    p.match = re.search('([0-9]+)\s+day',duration)
+    days = str(int(p.match.group(1)))
+if ':' in duration:
+    p.match = re.search('([0-9]+):([0-9]+)',duration)
+    hours = str(int(p.match.group(1)))
+    mins = str(int(p.match.group(2)))
+if 'min' in duration:
+    p.match = re.search('([0-9]+)\s+min',duration)
+    mins = str(int(p.match.group(1)))
+
+# Print the parsed fields in CSV format.
+print('days, hours, minutes, users, cpu avg 1 min, cpu avg 5 min, cpu avg 15 min')
+print('%s, %s, %s, %s, %s, %s, %s' % (days, hours, mins, users, av1, av5, av15))
+
diff --git a/third_party/pexpect/fdpexpect.py b/third_party/pexpect/fdpexpect.py
new file mode 100644
index 0000000..26db4c1
--- /dev/null
+++ b/third_party/pexpect/fdpexpect.py
@@ -0,0 +1,7 @@
+import warnings
+
+warnings.warn("This module has been moved to pexpect.fdpexpect, please update imports.",
+                ImportWarning)
+del warnings
+
+from pexpect.fdpexpect import *  # analysis:ignore
\ No newline at end of file
diff --git a/third_party/pexpect/notes/my_forkpty.py b/third_party/pexpect/notes/my_forkpty.py
new file mode 100644
index 0000000..f2bef23
--- /dev/null
+++ b/third_party/pexpect/notes/my_forkpty.py
@@ -0,0 +1,89 @@
+import os, fcntl, termios
+import time
+
+def my_forkpty():
+
+    (master_fd, slave_fd) = os.openpty()
+
+    if (master_fd < 0  or  slave_fd < 0):
+        raise ExceptionPexpect("Forkpty failed")
+
+    # slave_name = ptsname(master_fd);
+
+    pid = os.fork();
+    if pid == -1:
+        raise ExceptionPexpect("Forkpty failed")
+    elif pid == 0: # Child
+        if hasattr(termios, 'TIOCNOTTY'):
+        #        Some platforms require an explicit detach of the
+        #        current controlling tty before closing stdin, stdout, stderr.
+        #        OpenBSD says that this is obsolete, but doesn't hurt.
+            try:
+                fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
+            except:
+                pass
+            else: #if fd >= 0:
+                fcntl.ioctl(fd, termios.TIOCNOTTY, 0)
+                os.close(fd)
+
+        # The setsid() system call will place the process into its own session
+        # which has the effect of disassociating it from the controlling terminal.
+        # This is known to be true for OpenBSD.
+        os.setsid()
+        # except:            return posix_error();
+
+        # Verify that we are disconnected from the controlling tty.
+        try:
+            fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
+            os.close(fd)
+            raise ExceptionPexpect("Forkpty failed")
+        except:
+            pass
+        if 'TIOCSCTTY' in dir(termios):
+            # Make the pseudo terminal the controlling terminal for this process
+            # (the process must not currently have a controlling terminal).
+            if fcntl.ioctl(slave_fd, termios.TIOCSCTTY, '') < 0:
+                raise ExceptionPexpect("Forkpty failed")
+
+#        # Verify that we can open to the slave pty file. */
+#        fd = os.open(slave_name, os.O_RDWR);
+#        if fd < 0:
+#            raise ExceptionPexpect("Forkpty failed")
+#        else:
+#            os.close(fd);
+
+        # Verify that we now have a controlling tty.
+        fd = os.open("/dev/tty", os.O_WRONLY)
+        if fd < 0:
+            raise ExceptionPexpect("This process could not get a controlling tty.")
+        else:
+            os.close(fd)
+
+        os.close(master_fd)
+        os.dup2(slave_fd, 0)
+        os.dup2(slave_fd, 1)
+        os.dup2(slave_fd, 2)
+        if slave_fd > 2:
+            os.close(slave_fd)
+        pid = 0
+
+    else:
+        # PARENT 
+        os.close(slave_fd);
+
+    if pid == -1:
+        raise ExceptionPexpect("This process could not get a controlling tty.")
+#    if (pid == 0)
+#        PyOS_AfterFork();
+
+    return (pid, master_fd)
+
+pid, fd = my_forkpty ()
+if pid == 0: # child
+    print 'I am not a robot!'
+else:
+    print '(pid, fd) = (%d, %d)' % (pid, fd)
+    time.sleep(1) # Give the child a chance to print.
+    print 'Robots always say:', os.read(fd,100)
+    os.close(fd)
+
diff --git a/third_party/pexpect/notes/notes.txt b/third_party/pexpect/notes/notes.txt
new file mode 100644
index 0000000..a793587b
--- /dev/null
+++ b/third_party/pexpect/notes/notes.txt
@@ -0,0 +1,50 @@
+
+####################
+#
+#        NOTES
+#
+####################
+
+##    def send_human(self, text, delay_min = 0, delay_max = 1):
+##        pass
+##    def spawn2(self, command, args):
+##        """return pid, fd_stdio, fd_stderr
+##        """
+##        pass
+
+
+# Reason for double fork:
+# http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC15
+# Reason for ptys:
+# http://www.erlenstar.demon.co.uk/unix/faq_4.html#SEC52
+
+# Nonblocking on Win32?
+# Reasearch this as a way to maybe make pipe work for Win32.
+# http://groups.google.com/groups?q=setraw+tty&hl=en&selm=uvgpvisvk.fsf%40roundpoint.com&rnum=7
+# 
+#    if istty:
+#        if os.name=='posix':
+#            import tty
+#            tty.setraw(sys.stdin.fileno())
+#        elif os.name=='nt':
+#            import win32file, win32con
+#            hstdin = win32file._get_osfhandle(sys.stdin.fileno())
+#            modes = (win32file.GetConsoleMode(hstdin)
+#                     & ~(win32con.ENABLE_LINE_INPUT
+#                         |win32con.ENABLE_ECHO_INPUT))
+#            win32file.SetConsoleMode(hstdin, modes)
+
+# Basic documentation:
+#       Explain use of lists of patterns and return index.
+#       Explain exceptions for non-handled special cases like EOF
+
+# Test bad fork
+# Test ENOENT. In other words, no more TTY devices.
+
+#GLOBAL_SIGCHLD_RECEIVED = 0
+#def childdied (signum, frame):
+#    print 'Signal handler called with signal', signum
+#    frame.f_globals['pexpect'].GLOBAL_SIGCHLD_RECEIVED = 1
+#    print str(frame.f_globals['pexpect'].GLOBAL_SIGCHLD_RECEIVED)
+#    GLOBAL_SIGCHLD_RECEIVED = 1
+
diff --git a/third_party/pexpect/notes/posixmodule.c.diff b/third_party/pexpect/notes/posixmodule.c.diff
new file mode 100644
index 0000000..3bea1f9
--- /dev/null
+++ b/third_party/pexpect/notes/posixmodule.c.diff
@@ -0,0 +1,233 @@
+*** Python-2.2.1.orig/Modules/posixmodule.c	Tue Mar 12 16:38:31 2002
+--- Python-2.2.1/Modules/posixmodule.c	Tue May 21 01:16:29 2002
+***************
+*** 1904,1910 ****
+  }
+  #endif
+  
+! #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
+  #ifdef HAVE_PTY_H
+  #include <pty.h>
+  #else
+--- 1904,1913 ----
+  }
+  #endif
+  
+! #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(sun)
+! #ifdef sun
+! #include <sys/stropts.h>
+! #endif
+  #ifdef HAVE_PTY_H
+  #include <pty.h>
+  #else
+***************
+*** 1914,1920 ****
+  #endif /* HAVE_PTY_H */
+  #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
+  
+! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
+  static char posix_openpty__doc__[] =
+  "openpty() -> (master_fd, slave_fd)\n\
+  Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
+--- 1917,1923 ----
+  #endif /* HAVE_PTY_H */
+  #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
+  
+! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(sun)
+  static char posix_openpty__doc__[] =
+  "openpty() -> (master_fd, slave_fd)\n\
+  Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
+***************
+*** 1925,1932 ****
+  	int master_fd, slave_fd;
+  #ifndef HAVE_OPENPTY
+  	char * slave_name;
+  #endif
+! 
+  	if (!PyArg_ParseTuple(args, ":openpty"))
+  		return NULL;
+  
+--- 1928,1941 ----
+  	int master_fd, slave_fd;
+  #ifndef HAVE_OPENPTY
+  	char * slave_name;
++ #ifdef sun
++         void *sig_saved;
+  #endif
+! #endif
+! #if !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) && defined(sun)
+!         extern char *ptsname();
+! #endif
+!         
+  	if (!PyArg_ParseTuple(args, ":openpty"))
+  		return NULL;
+  
+***************
+*** 1933,1939 ****
+  #ifdef HAVE_OPENPTY
+  	if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
+  		return posix_error();
+! #else
+  	slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
+  	if (slave_name == NULL)
+  		return posix_error();
+--- 1942,1948 ----
+  #ifdef HAVE_OPENPTY
+  	if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
+  		return posix_error();
+! #elif HAVE__GETPTY
+  	slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
+  	if (slave_name == NULL)
+  		return posix_error();
+***************
+*** 1941,1946 ****
+--- 1950,1966 ----
+  	slave_fd = open(slave_name, O_RDWR);
+  	if (slave_fd < 0)
+  		return posix_error();
++ #else
++         master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);  /* open master */
++         sig_saved = signal(SIGCHLD, SIG_DFL);
++         grantpt(master_fd);                     /* change permission of   slave */
++         unlockpt(master_fd);                    /* unlock slave */
++         signal(SIGCHLD,sig_saved);
++         slave_name = ptsname(master_fd);         /* get name of slave */
++         slave_fd = open(slave_name, O_RDWR);    /* open slave */
++         ioctl(slave_fd, I_PUSH, "ptem");       /* push ptem */
++         ioctl(slave_fd, I_PUSH, "ldterm");     /* push ldterm*/
++         ioctl(slave_fd, I_PUSH, "ttcompat");     /* push ttcompat*/
+  #endif /* HAVE_OPENPTY */
+  
+  	return Py_BuildValue("(ii)", master_fd, slave_fd);
+***************
+*** 1948,1954 ****
+  }
+  #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
+  
+! #ifdef HAVE_FORKPTY
+  static char posix_forkpty__doc__[] =
+  "forkpty() -> (pid, master_fd)\n\
+  Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
+--- 1968,1974 ----
+  }
+  #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
+  
+! #if defined(HAVE_FORKPTY) || defined(sun)
+  static char posix_forkpty__doc__[] =
+  "forkpty() -> (pid, master_fd)\n\
+  Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
+***************
+*** 1959,1968 ****
+--- 1979,2067 ----
+  posix_forkpty(PyObject *self, PyObject *args)
+  {
+  	int master_fd, pid;
++ #if defined(sun)
++         int slave;
++ 	char * slave_name;
++         void *sig_saved;
++         int fd;
++ #endif
+  	
+  	if (!PyArg_ParseTuple(args, ":forkpty"))
+  		return NULL;
++ #if defined(sun)
++         master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);  /* open master */
++         sig_saved = signal(SIGCHLD, SIG_DFL);
++         grantpt(master_fd);                     /* change permission of   slave */
++         unlockpt(master_fd);                    /* unlock slave */
++         signal(SIGCHLD,sig_saved);
++         slave_name = ptsname(master_fd);         /* get name of slave */
++         slave = open(slave_name, O_RDWR);    /* open slave */
++         ioctl(slave, I_PUSH, "ptem");       /* push ptem */
++         ioctl(slave, I_PUSH, "ldterm");     /* push ldterm*/
++         ioctl(slave, I_PUSH, "ttcompat");     /* push ttcompat*/
++         if (master_fd < 0 || slave < 0)
++         {
++             return posix_error();
++         }
++ 	switch (pid = fork()) {
++ 	case -1:	
++             return posix_error();
++ 	case 0:
++             /* First disconnect from the old controlling tty. */
++ #ifdef TIOCNOTTY
++             fd = open("/dev/tty", O_RDWR | O_NOCTTY);
++             if (fd >= 0) {
++ 		(void) ioctl(fd, TIOCNOTTY, NULL);
++ 		close(fd);
++             }
++ #endif /* TIOCNOTTY */
++             if (setsid() < 0)
++ 		return posix_error();
++             
++             /*
++              * Verify that we are successfully disconnected from the controlling
++              * tty.
++              */
++             fd = open("/dev/tty", O_RDWR | O_NOCTTY);
++             if (fd >= 0) {
++ 		return posix_error();
++ 		close(fd);
++             }
++             /* Make it our controlling tty. */
++ #ifdef TIOCSCTTY
++             if (ioctl(slave, TIOCSCTTY, NULL) < 0)
++ 		return posix_error();
++ #endif /* TIOCSCTTY */
++             fd = open(slave_name, O_RDWR);
++             if (fd < 0) {
++ 		return posix_error();
++             } else {
++ 		close(fd);
++             }
++             /* Verify that we now have a controlling tty. */
++             fd = open("/dev/tty", O_WRONLY);
++             if (fd < 0)
++ 		return posix_error();
++             else {
++ 		close(fd);
++             }
++             (void) close(master_fd);
++             (void) dup2(slave, 0);
++             (void) dup2(slave, 1);
++             (void) dup2(slave, 2);
++             if (slave > 2)
++                 (void) close(slave);
++             pid = 0;
++             break;
++           defautlt:
++             /*
++              * parent
++              */
++             (void) close(slave);
++ 	}
++ #else
+  	pid = forkpty(&master_fd, NULL, NULL, NULL);
++ #endif
+  	if (pid == -1)
+  		return posix_error();
+  	if (pid == 0)
+***************
+*** 5607,5616 ****
+  #ifdef HAVE_FORK
+  	{"fork",	posix_fork, METH_VARARGS, posix_fork__doc__},
+  #endif /* HAVE_FORK */
+! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
+  	{"openpty",	posix_openpty, METH_VARARGS, posix_openpty__doc__},
+  #endif /* HAVE_OPENPTY || HAVE__GETPTY */
+! #ifdef HAVE_FORKPTY
+  	{"forkpty",	posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
+  #endif /* HAVE_FORKPTY */
+  #ifdef HAVE_GETEGID
+--- 5706,5715 ----
+  #ifdef HAVE_FORK
+  	{"fork",	posix_fork, METH_VARARGS, posix_fork__doc__},
+  #endif /* HAVE_FORK */
+! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(sun)
+  	{"openpty",	posix_openpty, METH_VARARGS, posix_openpty__doc__},
+  #endif /* HAVE_OPENPTY || HAVE__GETPTY */
+! #if defined(HAVE_FORKPTY) || defined(sun)
+  	{"forkpty",	posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
+  #endif /* HAVE_FORKPTY */
+  #ifdef HAVE_GETEGID
diff --git a/third_party/pexpect/pexpect/ANSI.py b/third_party/pexpect/pexpect/ANSI.py
new file mode 100644
index 0000000..1cd2e90
--- /dev/null
+++ b/third_party/pexpect/pexpect/ANSI.py
@@ -0,0 +1,351 @@
+'''This implements an ANSI (VT100) terminal emulator as a subclass of screen.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+# references:
+#     http://en.wikipedia.org/wiki/ANSI_escape_code
+#     http://www.retards.org/terminals/vt102.html
+#     http://vt100.net/docs/vt102-ug/contents.html
+#     http://vt100.net/docs/vt220-rm/
+#     http://www.termsys.demon.co.uk/vtansi.htm
+
+from . import screen
+from . import FSM
+import string
+
+#
+# The 'Do.*' functions are helper functions for the ANSI class.
+#
+def DoEmit (fsm):
+
+    screen = fsm.memory[0]
+    screen.write_ch(fsm.input_symbol)
+
+def DoStartNumber (fsm):
+
+    fsm.memory.append (fsm.input_symbol)
+
+def DoBuildNumber (fsm):
+
+    ns = fsm.memory.pop()
+    ns = ns + fsm.input_symbol
+    fsm.memory.append (ns)
+
+def DoBackOne (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_back ()
+
+def DoBack (fsm):
+
+    count = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    screen.cursor_back (count)
+
+def DoDownOne (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_down ()
+
+def DoDown (fsm):
+
+    count = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    screen.cursor_down (count)
+
+def DoForwardOne (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_forward ()
+
+def DoForward (fsm):
+
+    count = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    screen.cursor_forward (count)
+
+def DoUpReverse (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_up_reverse()
+
+def DoUpOne (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_up ()
+
+def DoUp (fsm):
+
+    count = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    screen.cursor_up (count)
+
+def DoHome (fsm):
+
+    c = int(fsm.memory.pop())
+    r = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    screen.cursor_home (r,c)
+
+def DoHomeOrigin (fsm):
+
+    c = 1
+    r = 1
+    screen = fsm.memory[0]
+    screen.cursor_home (r,c)
+
+def DoEraseDown (fsm):
+
+    screen = fsm.memory[0]
+    screen.erase_down()
+
+def DoErase (fsm):
+
+    arg = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    if arg == 0:
+        screen.erase_down()
+    elif arg == 1:
+        screen.erase_up()
+    elif arg == 2:
+        screen.erase_screen()
+
+def DoEraseEndOfLine (fsm):
+
+    screen = fsm.memory[0]
+    screen.erase_end_of_line()
+
+def DoEraseLine (fsm):
+
+    arg = int(fsm.memory.pop())
+    screen = fsm.memory[0]
+    if arg == 0:
+        screen.erase_end_of_line()
+    elif arg == 1:
+        screen.erase_start_of_line()
+    elif arg == 2:
+        screen.erase_line()
+
+def DoEnableScroll (fsm):
+
+    screen = fsm.memory[0]
+    screen.scroll_screen()
+
+def DoCursorSave (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_save_attrs()
+
+def DoCursorRestore (fsm):
+
+    screen = fsm.memory[0]
+    screen.cursor_restore_attrs()
+
+def DoScrollRegion (fsm):
+
+    screen = fsm.memory[0]
+    r2 = int(fsm.memory.pop())
+    r1 = int(fsm.memory.pop())
+    screen.scroll_screen_rows (r1,r2)
+
+def DoMode (fsm):
+
+    screen = fsm.memory[0]
+    mode = fsm.memory.pop() # Should be 4
+    # screen.setReplaceMode ()
+
+def DoLog (fsm):
+
+    screen = fsm.memory[0]
+    fsm.memory = [screen]
+    fout = open ('log', 'a')
+    fout.write (fsm.input_symbol + ',' + fsm.current_state + '\n')
+    fout.close()
+
+class term (screen.screen):
+
+    '''This class is an abstract, generic terminal.
+    This does nothing. This is a placeholder that
+    provides a common base class for other terminals
+    such as an ANSI terminal. '''
+
+    def __init__ (self, r=24, c=80, *args, **kwargs):
+
+        screen.screen.__init__(self, r,c,*args,**kwargs)
+
+class ANSI (term):
+    '''This class implements an ANSI (VT100) terminal.
+    It is a stream filter that recognizes ANSI terminal
+    escape sequences and maintains the state of a screen object. '''
+
+    def __init__ (self, r=24,c=80,*args,**kwargs):
+
+        term.__init__(self,r,c,*args,**kwargs)
+
+        #self.screen = screen (24,80)
+        self.state = FSM.FSM ('INIT',[self])
+        self.state.set_default_transition (DoLog, 'INIT')
+        self.state.add_transition_any ('INIT', DoEmit, 'INIT')
+        self.state.add_transition ('\x1b', 'INIT', None, 'ESC')
+        self.state.add_transition_any ('ESC', DoLog, 'INIT')
+        self.state.add_transition ('(', 'ESC', None, 'G0SCS')
+        self.state.add_transition (')', 'ESC', None, 'G1SCS')
+        self.state.add_transition_list ('AB012', 'G0SCS', None, 'INIT')
+        self.state.add_transition_list ('AB012', 'G1SCS', None, 'INIT')
+        self.state.add_transition ('7', 'ESC', DoCursorSave, 'INIT')
+        self.state.add_transition ('8', 'ESC', DoCursorRestore, 'INIT')
+        self.state.add_transition ('M', 'ESC', DoUpReverse, 'INIT')
+        self.state.add_transition ('>', 'ESC', DoUpReverse, 'INIT')
+        self.state.add_transition ('<', 'ESC', DoUpReverse, 'INIT')
+        self.state.add_transition ('=', 'ESC', None, 'INIT') # Selects application keypad.
+        self.state.add_transition ('#', 'ESC', None, 'GRAPHICS_POUND')
+        self.state.add_transition_any ('GRAPHICS_POUND', None, 'INIT')
+        self.state.add_transition ('[', 'ESC', None, 'ELB')
+        # ELB means Escape Left Bracket. That is ^[[
+        self.state.add_transition ('H', 'ELB', DoHomeOrigin, 'INIT')
+        self.state.add_transition ('D', 'ELB', DoBackOne, 'INIT')
+        self.state.add_transition ('B', 'ELB', DoDownOne, 'INIT')
+        self.state.add_transition ('C', 'ELB', DoForwardOne, 'INIT')
+        self.state.add_transition ('A', 'ELB', DoUpOne, 'INIT')
+        self.state.add_transition ('J', 'ELB', DoEraseDown, 'INIT')
+        self.state.add_transition ('K', 'ELB', DoEraseEndOfLine, 'INIT')
+        self.state.add_transition ('r', 'ELB', DoEnableScroll, 'INIT')
+        self.state.add_transition ('m', 'ELB', self.do_sgr, 'INIT')
+        self.state.add_transition ('?', 'ELB', None, 'MODECRAP')
+        self.state.add_transition_list (string.digits, 'ELB', DoStartNumber, 'NUMBER_1')
+        self.state.add_transition_list (string.digits, 'NUMBER_1', DoBuildNumber, 'NUMBER_1')
+        self.state.add_transition ('D', 'NUMBER_1', DoBack, 'INIT')
+        self.state.add_transition ('B', 'NUMBER_1', DoDown, 'INIT')
+        self.state.add_transition ('C', 'NUMBER_1', DoForward, 'INIT')
+        self.state.add_transition ('A', 'NUMBER_1', DoUp, 'INIT')
+        self.state.add_transition ('J', 'NUMBER_1', DoErase, 'INIT')
+        self.state.add_transition ('K', 'NUMBER_1', DoEraseLine, 'INIT')
+        self.state.add_transition ('l', 'NUMBER_1', DoMode, 'INIT')
+        ### It gets worse... the 'm' code can have infinite number of
+        ### number;number;number before it. I've never seen more than two,
+        ### but the specs say it's allowed. crap!
+        self.state.add_transition ('m', 'NUMBER_1', self.do_sgr, 'INIT')
+        ### LED control. Same implementation problem as 'm' code.
+        self.state.add_transition ('q', 'NUMBER_1', self.do_decsca, 'INIT')
+
+        # \E[?47h switch to alternate screen
+        # \E[?47l restores to normal screen from alternate screen.
+        self.state.add_transition_list (string.digits, 'MODECRAP', DoStartNumber, 'MODECRAP_NUM')
+        self.state.add_transition_list (string.digits, 'MODECRAP_NUM', DoBuildNumber, 'MODECRAP_NUM')
+        self.state.add_transition ('l', 'MODECRAP_NUM', self.do_modecrap, 'INIT')
+        self.state.add_transition ('h', 'MODECRAP_NUM', self.do_modecrap, 'INIT')
+
+#RM   Reset Mode                Esc [ Ps l                   none
+        self.state.add_transition (';', 'NUMBER_1', None, 'SEMICOLON')
+        self.state.add_transition_any ('SEMICOLON', DoLog, 'INIT')
+        self.state.add_transition_list (string.digits, 'SEMICOLON', DoStartNumber, 'NUMBER_2')
+        self.state.add_transition_list (string.digits, 'NUMBER_2', DoBuildNumber, 'NUMBER_2')
+        self.state.add_transition_any ('NUMBER_2', DoLog, 'INIT')
+        self.state.add_transition ('H', 'NUMBER_2', DoHome, 'INIT')
+        self.state.add_transition ('f', 'NUMBER_2', DoHome, 'INIT')
+        self.state.add_transition ('r', 'NUMBER_2', DoScrollRegion, 'INIT')
+        ### It gets worse... the 'm' code can have infinite number of
+        ### number;number;number before it. I've never seen more than two,
+        ### but the specs say it's allowed. crap!
+        self.state.add_transition ('m', 'NUMBER_2', self.do_sgr, 'INIT')
+        ### LED control. Same problem as 'm' code.
+        self.state.add_transition ('q', 'NUMBER_2', self.do_decsca, 'INIT')
+        self.state.add_transition (';', 'NUMBER_2', None, 'SEMICOLON_X')
+
+        # Create a state for 'q' and 'm' which allows an infinite number of ignored numbers
+        self.state.add_transition_any ('SEMICOLON_X', DoLog, 'INIT')
+        self.state.add_transition_list (string.digits, 'SEMICOLON_X', DoStartNumber, 'NUMBER_X')
+        self.state.add_transition_list (string.digits, 'NUMBER_X', DoBuildNumber, 'NUMBER_X')
+        self.state.add_transition_any ('NUMBER_X', DoLog, 'INIT')
+        self.state.add_transition ('m', 'NUMBER_X', self.do_sgr, 'INIT')
+        self.state.add_transition ('q', 'NUMBER_X', self.do_decsca, 'INIT')
+        self.state.add_transition (';', 'NUMBER_X', None, 'SEMICOLON_X')
+
+    def process (self, c):
+        """Process a single character. Called by :meth:`write`."""
+        if isinstance(c, bytes):
+            c = self._decode(c)
+        self.state.process(c)
+
+    def process_list (self, l):
+
+        self.write(l)
+
+    def write (self, s):
+        """Process text, writing it to the virtual screen while handling
+        ANSI escape codes.
+        """
+        if isinstance(s, bytes):
+            s = self._decode(s)
+        for c in s:
+            self.process(c)
+
+    def flush (self):
+        pass
+
+    def write_ch (self, ch):
+        '''This puts a character at the current cursor position. The cursor
+        position is moved forward with wrap-around, but no scrolling is done if
+        the cursor hits the lower-right corner of the screen. '''
+
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)
+
+        #\r and \n both produce a call to cr() and lf(), respectively.
+        ch = ch[0]
+
+        if ch == u'\r':
+            self.cr()
+            return
+        if ch == u'\n':
+            self.crlf()
+            return
+        if ch == chr(screen.BS):
+            self.cursor_back()
+            return
+        self.put_abs(self.cur_r, self.cur_c, ch)
+        old_r = self.cur_r
+        old_c = self.cur_c
+        self.cursor_forward()
+        if old_c == self.cur_c:
+            self.cursor_down()
+            if old_r != self.cur_r:
+                self.cursor_home (self.cur_r, 1)
+            else:
+                self.scroll_up ()
+                self.cursor_home (self.cur_r, 1)
+                self.erase_line()
+
+    def do_sgr (self, fsm):
+        '''Select Graphic Rendition, e.g. color. '''
+        screen = fsm.memory[0]
+        fsm.memory = [screen]
+
+    def do_decsca (self, fsm):
+        '''Select character protection attribute. '''
+        screen = fsm.memory[0]
+        fsm.memory = [screen]
+
+    def do_modecrap (self, fsm):
+        '''Handler for \x1b[?<number>h and \x1b[?<number>l. If anyone
+        wanted to actually use these, they'd need to add more states to the
+        FSM rather than just improve or override this method. '''
+        screen = fsm.memory[0]
+        fsm.memory = [screen]
diff --git a/third_party/pexpect/pexpect/FSM.py b/third_party/pexpect/pexpect/FSM.py
new file mode 100644
index 0000000..46b392e
--- /dev/null
+++ b/third_party/pexpect/pexpect/FSM.py
@@ -0,0 +1,334 @@
+#!/usr/bin/env python
+
+'''This module implements a Finite State Machine (FSM). In addition to state
+this FSM also maintains a user defined "memory". So this FSM can be used as a
+Push-down Automata (PDA) since a PDA is a FSM + memory.
+
+The following describes how the FSM works, but you will probably also need to
+see the example function to understand how the FSM is used in practice.
+
+You define an FSM by building tables of transitions. For a given input symbol
+the process() method uses these tables to decide what action to call and what
+the next state will be. The FSM has a table of transitions that associate:
+
+        (input_symbol, current_state) --> (action, next_state)
+
+Where "action" is a function you define. The symbols and states can be any
+objects. You use the add_transition() and add_transition_list() methods to add
+to the transition table. The FSM also has a table of transitions that
+associate:
+
+        (current_state) --> (action, next_state)
+
+You use the add_transition_any() method to add to this transition table. The
+FSM also has one default transition that is not associated with any specific
+input_symbol or state. You use the set_default_transition() method to set the
+default transition.
+
+When an action function is called it is passed a reference to the FSM. The
+action function may then access attributes of the FSM such as input_symbol,
+current_state, or "memory". The "memory" attribute can be any object that you
+want to pass along to the action functions. It is not used by the FSM itself.
+For parsing you would typically pass a list to be used as a stack.
+
+The processing sequence is as follows. The process() method is given an
+input_symbol to process. The FSM will search the table of transitions that
+associate:
+
+        (input_symbol, current_state) --> (action, next_state)
+
+If the pair (input_symbol, current_state) is found then process() will call the
+associated action function and then set the current state to the next_state.
+
+If the FSM cannot find a match for (input_symbol, current_state) it will then
+search the table of transitions that associate:
+
+        (current_state) --> (action, next_state)
+
+If the current_state is found then the process() method will call the
+associated action function and then set the current state to the next_state.
+Notice that this table lacks an input_symbol. It lets you define transitions
+for a current_state and ANY input_symbol. Hence, it is called the "any" table.
+Remember, it is always checked after first searching the table for a specific
+(input_symbol, current_state).
+
+For the case where the FSM did not match either of the previous two cases the
+FSM will try to use the default transition. If the default transition is
+defined then the process() method will call the associated action function and
+then set the current state to the next_state. This lets you define a default
+transition as a catch-all case. You can think of it as an exception handler.
+There can be only one default transition.
+
+Finally, if none of the previous cases are defined for an input_symbol and
+current_state then the FSM will raise an exception. This may be desirable, but
+you can always prevent this just by defining a default transition.
+
+Noah Spurrier 20020822
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+class ExceptionFSM(Exception):
+
+    '''This is the FSM Exception class.'''
+
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        return 'ExceptionFSM: ' + str(self.value)
+
+class FSM:
+
+    '''This is a Finite State Machine (FSM).
+    '''
+
+    def __init__(self, initial_state, memory=None):
+
+        '''This creates the FSM. You set the initial state here. The "memory"
+        attribute is any object that you want to pass along to the action
+        functions. It is not used by the FSM. For parsing you would typically
+        pass a list to be used as a stack. '''
+
+        # Map (input_symbol, current_state) --> (action, next_state).
+        self.state_transitions = {}
+        # Map (current_state) --> (action, next_state).
+        self.state_transitions_any = {}
+        self.default_transition = None
+
+        self.input_symbol = None
+        self.initial_state = initial_state
+        self.current_state = self.initial_state
+        self.next_state = None
+        self.action = None
+        self.memory = memory
+
+    def reset (self):
+
+        '''This sets the current_state to the initial_state and sets
+        input_symbol to None. The initial state was set by the constructor
+        __init__(). '''
+
+        self.current_state = self.initial_state
+        self.input_symbol = None
+
+    def add_transition (self, input_symbol, state, action=None, next_state=None):
+
+        '''This adds a transition that associates:
+
+                (input_symbol, current_state) --> (action, next_state)
+
+        The action may be set to None in which case the process() method will
+        ignore the action and only set the next_state. The next_state may be
+        set to None in which case the current state will be unchanged.
+
+        You can also set transitions for a list of symbols by using
+        add_transition_list(). '''
+
+        if next_state is None:
+            next_state = state
+        self.state_transitions[(input_symbol, state)] = (action, next_state)
+
+    def add_transition_list (self, list_input_symbols, state, action=None, next_state=None):
+
+        '''This adds the same transition for a list of input symbols.
+        You can pass a list or a string. Note that it is handy to use
+        string.digits, string.whitespace, string.letters, etc. to add
+        transitions that match character classes.
+
+        The action may be set to None in which case the process() method will
+        ignore the action and only set the next_state. The next_state may be
+        set to None in which case the current state will be unchanged. '''
+
+        if next_state is None:
+            next_state = state
+        for input_symbol in list_input_symbols:
+            self.add_transition (input_symbol, state, action, next_state)
+
+    def add_transition_any (self, state, action=None, next_state=None):
+
+        '''This adds a transition that associates:
+
+                (current_state) --> (action, next_state)
+
+        That is, any input symbol will match the current state.
+        The process() method checks the "any" state associations after it first
+        checks for an exact match of (input_symbol, current_state).
+
+        The action may be set to None in which case the process() method will
+        ignore the action and only set the next_state. The next_state may be
+        set to None in which case the current state will be unchanged. '''
+
+        if next_state is None:
+            next_state = state
+        self.state_transitions_any [state] = (action, next_state)
+
+    def set_default_transition (self, action, next_state):
+
+        '''This sets the default transition. This defines an action and
+        next_state if the FSM cannot find the input symbol and the current
+        state in the transition list and if the FSM cannot find the
+        current_state in the transition_any list. This is useful as a final
+        fall-through state for catching errors and undefined states.
+
+        The default transition can be removed by setting the attribute
+        default_transition to None. '''
+
+        self.default_transition = (action, next_state)
+
+    def get_transition (self, input_symbol, state):
+
+        '''This returns (action, next state) given an input_symbol and state.
+        This does not modify the FSM state, so calling this method has no side
+        effects. Normally you do not call this method directly. It is called by
+        process().
+
+        The sequence of steps to check for a defined transition goes from the
+        most specific to the least specific.
+
+        1. Check state_transitions[] that match exactly the tuple,
+            (input_symbol, state)
+
+        2. Check state_transitions_any[] that match (state)
+            In other words, match a specific state and ANY input_symbol.
+
+        3. Check if the default_transition is defined.
+            This catches any input_symbol and any state.
+            This is a handler for errors, undefined states, or defaults.
+
+        4. No transition was defined. If we get here then raise an exception.
+        '''
+
+        if (input_symbol, state) in self.state_transitions:
+            return self.state_transitions[(input_symbol, state)]
+        elif state in self.state_transitions_any:
+            return self.state_transitions_any[state]
+        elif self.default_transition is not None:
+            return self.default_transition
+        else:
+            raise ExceptionFSM ('Transition is undefined: (%s, %s).' %
+                (str(input_symbol), str(state)) )
+
+    def process (self, input_symbol):
+
+        '''This is the main method that you call to process input. This may
+        cause the FSM to change state and call an action. This method calls
+        get_transition() to find the action and next_state associated with the
+        input_symbol and current_state. If the action is None then the action
+        is not called and only the current state is changed. This method
+        processes one complete input symbol. You can process a list of symbols
+        (or a string) by calling process_list(). '''
+
+        self.input_symbol = input_symbol
+        (self.action, self.next_state) = self.get_transition (self.input_symbol, self.current_state)
+        if self.action is not None:
+            self.action (self)
+        self.current_state = self.next_state
+        self.next_state = None
+
+    def process_list (self, input_symbols):
+
+        '''This takes a list and sends each element to process(). The list may
+        be a string or any iterable object. '''
+
+        for s in input_symbols:
+            self.process (s)
+
+##############################################################################
+# The following is an example that demonstrates the use of the FSM class to
+# process an RPN expression. Run this module from the command line. You will
+# get a prompt > for input. Enter an RPN Expression. Numbers may be integers.
+# Operators are * / + - Use the = sign to evaluate and print the expression.
+# For example:
+#
+#    167 3 2 2 * * * 1 - =
+#
+# will print:
+#
+#    2003
+##############################################################################
+
+import sys
+import string
+
+PY3 = (sys.version_info[0] >= 3)
+
+#
+# These define the actions.
+# Note that "memory" is a list being used as a stack.
+#
+
+def BeginBuildNumber (fsm):
+    fsm.memory.append (fsm.input_symbol)
+
+def BuildNumber (fsm):
+    s = fsm.memory.pop ()
+    s = s + fsm.input_symbol
+    fsm.memory.append (s)
+
+def EndBuildNumber (fsm):
+    s = fsm.memory.pop ()
+    fsm.memory.append (int(s))
+
+def DoOperator (fsm):
+    ar = fsm.memory.pop()
+    al = fsm.memory.pop()
+    if fsm.input_symbol == '+':
+        fsm.memory.append (al + ar)
+    elif fsm.input_symbol == '-':
+        fsm.memory.append (al - ar)
+    elif fsm.input_symbol == '*':
+        fsm.memory.append (al * ar)
+    elif fsm.input_symbol == '/':
+        fsm.memory.append (al / ar)
+
+def DoEqual (fsm):
+    print(str(fsm.memory.pop()))
+
+def Error (fsm):
+    print('That does not compute.')
+    print(str(fsm.input_symbol))
+
+def main():
+
+    '''This is where the example starts and the FSM state transitions are
+    defined. Note that states are strings (such as 'INIT'). This is not
+    necessary, but it makes the example easier to read. '''
+
+    f = FSM ('INIT', [])
+    f.set_default_transition (Error, 'INIT')
+    f.add_transition_any  ('INIT', None, 'INIT')
+    f.add_transition      ('=',               'INIT',            DoEqual,          'INIT')
+    f.add_transition_list (string.digits,     'INIT',            BeginBuildNumber, 'BUILDING_NUMBER')
+    f.add_transition_list (string.digits,     'BUILDING_NUMBER', BuildNumber,      'BUILDING_NUMBER')
+    f.add_transition_list (string.whitespace, 'BUILDING_NUMBER', EndBuildNumber,   'INIT')
+    f.add_transition_list ('+-*/',            'INIT',            DoOperator,       'INIT')
+
+    print()
+    print('Enter an RPN Expression.')
+    print('Numbers may be integers. Operators are * / + -')
+    print('Use the = sign to evaluate and print the expression.')
+    print('For example: ')
+    print('    167 3 2 2 * * * 1 - =')
+    inputstr = (input if PY3 else raw_input)('> ')  # analysis:ignore
+    f.process_list(inputstr)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/third_party/pexpect/pexpect/__init__.py b/third_party/pexpect/pexpect/__init__.py
new file mode 100644
index 0000000..c906e89
--- /dev/null
+++ b/third_party/pexpect/pexpect/__init__.py
@@ -0,0 +1,232 @@
+'''Pexpect is a Python module for spawning child applications and controlling
+them automatically. Pexpect can be used for automating interactive applications
+such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
+scripts for duplicating software package installations on different servers. It
+can be used for automated software testing. Pexpect is in the spirit of Don
+Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
+require TCL and Expect or require C extensions to be compiled. Pexpect does not
+use C, Expect, or TCL extensions. It should work on any platform that supports
+the standard Python pty module. The Pexpect interface focuses on ease of use so
+that simple tasks are easy.
+
+There are two main interfaces to the Pexpect system; these are the function,
+run() and the class, spawn. The spawn class is more powerful. The run()
+function is simpler than spawn, and is good for quickly calling program. When
+you call the run() function it executes a given program and then returns the
+output. This is a handy replacement for os.system().
+
+For example::
+
+    pexpect.run('ls -la')
+
+The spawn class is the more powerful interface to the Pexpect system. You can
+use this to spawn a child program then interact with it by sending input and
+expecting responses (waiting for patterns in the child's output).
+
+For example::
+
+    child = pexpect.spawn('scp foo user at example.com:.')
+    child.expect('Password:')
+    child.sendline(mypassword)
+
+This works even for commands that ask for passwords or other input outside of
+the normal stdio streams. For example, ssh reads input directly from the TTY
+device which bypasses stdin.
+
+Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
+Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
+vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
+Jacques-Etienne Baudoux, Geoffrey Marshall, Francisco Lourenco, Glen Mabey,
+Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume
+Chazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, John
+Spiegel, Jan Grant, and Shane Kerr. Let me know if I forgot anyone.
+
+Pexpect is free, open source, and all that good stuff.
+http://pexpect.sourceforge.net/
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+import sys
+import types
+
+from .exceptions import ExceptionPexpect, EOF, TIMEOUT
+from .utils import split_command_line, which, is_executable_file
+from .pty_spawn import spawn, spawnu, PY3
+from .expect import Expecter, searcher_re, searcher_string
+
+__version__ = '3.3'
+__revision__ = ''
+__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'spawnu', 'run', 'runu',
+           'which', 'split_command_line', '__version__', '__revision__']
+
+def run(command, timeout=30, withexitstatus=False, events=None,
+        extra_args=None, logfile=None, cwd=None, env=None):
+
+    '''
+    This function runs the given command; waits for it to finish; then
+    returns all output as a string. STDERR is included in output. If the full
+    path to the command is not given then the path is searched.
+
+    Note that lines are terminated by CR/LF (\\r\\n) combination even on
+    UNIX-like systems because this is the standard for pseudottys. If you set
+    'withexitstatus' to true, then run will return a tuple of (command_output,
+    exitstatus). If 'withexitstatus' is false then this returns just
+    command_output.
+
+    The run() function can often be used instead of creating a spawn instance.
+    For example, the following code uses spawn::
+
+        from pexpect import *
+        child = spawn('scp foo user at example.com:.')
+        child.expect('(?i)password')
+        child.sendline(mypassword)
+
+    The previous code can be replace with the following::
+
+        from pexpect import *
+        run('scp foo user at example.com:.', events={'(?i)password': mypassword})
+
+    **Examples**
+
+    Start the apache daemon on the local machine::
+
+        from pexpect import *
+        run("/usr/local/apache/bin/apachectl start")
+
+    Check in a file using SVN::
+
+        from pexpect import *
+        run("svn ci -m 'automatic commit' my_file.py")
+
+    Run a command and capture exit status::
+
+        from pexpect import *
+        (command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
+
+    The following will run SSH and execute 'ls -l' on the remote machine. The
+    password 'secret' will be sent if the '(?i)password' pattern is ever seen::
+
+        run("ssh username at machine.example.com 'ls -l'",
+            events={'(?i)password':'secret\\n'})
+
+    This will start mencoder to rip a video from DVD. This will also display
+    progress ticks every 5 seconds as it runs. For example::
+
+        from pexpect import *
+        def print_ticks(d):
+            print d['event_count'],
+        run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
+            events={TIMEOUT:print_ticks}, timeout=5)
+
+    The 'events' argument should be either a dictionary or a tuple list that
+    contains patterns and responses. Whenever one of the patterns is seen
+    in the command output, run() will send the associated response string.
+    So, run() in the above example can be also written as:
+    
+        run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
+            events=[(TIMEOUT,print_ticks)], timeout=5)
+
+    Use a tuple list for events if the command output requires a delicate
+    control over what pattern should be matched, since the tuple list is passed
+    to pexpect() as its pattern list, with the order of patterns preserved.
+
+    Note that you should put newlines in your string if Enter is necessary.
+
+    Like the example above, the responses may also contain callback functions.
+    Any callback is a function that takes a dictionary as an argument.
+    The dictionary contains all the locals from the run() function, so you can
+    access the child spawn object or any other variable defined in run()
+    (event_count, child, and extra_args are the most useful). A callback may
+    return True to stop the current run process.  Otherwise run() continues
+    until the next event. A callback may also return a string which will be
+    sent to the child. 'extra_args' is not used by directly run(). It provides
+    a way to pass data to a callback function through run() through the locals
+    dictionary passed to a callback.
+    '''
+    return _run(command, timeout=timeout, withexitstatus=withexitstatus,
+                events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
+                env=env, _spawn=spawn)
+
+def runu(command, timeout=30, withexitstatus=False, events=None,
+        extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
+    """This offers the same interface as :func:`run`, but using unicode.
+
+    Like :class:`spawnu`, you can pass ``encoding`` and ``errors`` parameters,
+    which will be used for both input and output.
+    """
+    return _run(command, timeout=timeout, withexitstatus=withexitstatus,
+                events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
+                env=env, _spawn=spawnu, **kwargs)
+
+def _run(command, timeout, withexitstatus, events, extra_args, logfile, cwd,
+         env, _spawn, **kwargs):
+    if timeout == -1:
+        child = _spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env,
+                        **kwargs)
+    else:
+        child = _spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
+                cwd=cwd, env=env, **kwargs)
+    if isinstance(events, list):
+        patterns= [x for x,y in events]
+        responses = [y for x,y in events]
+    elif isinstance(events, dict):
+        patterns = list(events.keys())
+        responses = list(events.values())
+    else:
+        # This assumes EOF or TIMEOUT will eventually cause run to terminate.
+        patterns = None
+        responses = None
+    child_result_list = []
+    event_count = 0
+    while True:
+        try:
+            index = child.expect(patterns)
+            if isinstance(child.after, child.allowed_string_types):
+                child_result_list.append(child.before + child.after)
+            else:
+                # child.after may have been a TIMEOUT or EOF,
+                # which we don't want appended to the list.
+                child_result_list.append(child.before)
+            if isinstance(responses[index], child.allowed_string_types):
+                child.send(responses[index])
+            elif isinstance(responses[index], types.FunctionType):
+                callback_result = responses[index](locals())
+                sys.stdout.flush()
+                if isinstance(callback_result, child.allowed_string_types):
+                    child.send(callback_result)
+                elif callback_result:
+                    break
+            else:
+                raise TypeError('The callback must be a string or function.')
+            event_count = event_count + 1
+        except TIMEOUT:
+            child_result_list.append(child.before)
+            break
+        except EOF:
+            child_result_list.append(child.before)
+            break
+    child_result = child.string_type().join(child_result_list)
+    if withexitstatus:
+        child.close()
+        return (child_result, child.exitstatus)
+    else:
+        return child_result
+
+# vim: set shiftround expandtab tabstop=4 shiftwidth=4 ft=python autoindent :
diff --git a/third_party/pexpect/pexpect/async.py b/third_party/pexpect/pexpect/async.py
new file mode 100644
index 0000000..50eae3b
--- /dev/null
+++ b/third_party/pexpect/pexpect/async.py
@@ -0,0 +1,70 @@
+import asyncio
+import errno
+
+from pexpect import EOF
+
+ at asyncio.coroutine
+def expect_async(expecter, timeout=None):
+    # First process data that was previously read - if it maches, we don't need
+    # async stuff.    
+    idx = expecter.new_data(expecter.spawn.buffer)
+    expecter.spawn.buffer = expecter.spawn.string_type()
+    if idx:
+        return idx
+
+    transport, pw = yield from asyncio.get_event_loop()\
+        .connect_read_pipe(lambda: PatternWaiter(expecter), expecter.spawn)
+
+    try:
+        return (yield from asyncio.wait_for(pw.fut, timeout))
+    except asyncio.TimeoutError as e:
+        transport.pause_reading()
+        return expecter.timeout(e)
+
+class PatternWaiter(asyncio.Protocol):
+    def __init__(self, expecter):
+        self.expecter = expecter
+        self.fut = asyncio.Future()
+    
+    def found(self, result):
+        if not self.fut.done():
+            self.fut.set_result(result)
+    
+    def error(self, exc):
+        if not self.fut.done():
+            self.fut.set_exception(exc)
+    
+    def data_received(self, data):
+        spawn = self.expecter.spawn
+        s = spawn._coerce_read_string(data)
+        spawn._log(s, 'read')
+
+        if self.fut.done():
+            spawn.buffer += data
+            return
+
+        try:
+            index = self.expecter.new_data(data)
+            if index is not None:
+                # Found a match
+                self.found(index)
+        except Exception as e:
+            self.expecter.errored()
+            self.error(e)
+    
+    def eof_received(self):
+        # N.B. If this gets called, async will close the pipe (the spawn object)
+        # for us
+        try:
+            index = self.expecter.eof()
+        except EOF as e:
+            self.error(e)
+        else:
+            self.found(index)
+    
+    def connection_lost(self, exc):
+        if isinstance(exc, OSError) and exc.errno == errno.EIO:
+            # We may get here without eof_received being called, e.g on Linux
+            self.eof_received()
+        elif exc is not None:
+            self.error(exc)
\ No newline at end of file
diff --git a/third_party/pexpect/pexpect/bashrc.sh b/third_party/pexpect/pexpect/bashrc.sh
new file mode 100644
index 0000000..99a3ac2
--- /dev/null
+++ b/third_party/pexpect/pexpect/bashrc.sh
@@ -0,0 +1,5 @@
+source /etc/bash.bashrc
+source ~/.bashrc
+
+# Reset PS1 so pexpect can find it
+PS1="$"
diff --git a/third_party/pexpect/pexpect/exceptions.py b/third_party/pexpect/pexpect/exceptions.py
new file mode 100644
index 0000000..cb360f0
--- /dev/null
+++ b/third_party/pexpect/pexpect/exceptions.py
@@ -0,0 +1,35 @@
+"""Exception classes used by Pexpect"""
+
+import traceback
+import sys
+
+class ExceptionPexpect(Exception):
+    '''Base class for all exceptions raised by this module.
+    '''
+
+    def __init__(self, value):
+        super(ExceptionPexpect, self).__init__(value)
+        self.value = value
+
+    def __str__(self):
+        return str(self.value)
+
+    def get_trace(self):
+        '''This returns an abbreviated stack trace with lines that only concern
+        the caller. In other words, the stack trace inside the Pexpect module
+        is not included. '''
+
+        tblist = traceback.extract_tb(sys.exc_info()[2])
+        tblist = [item for item in tblist if ('pexpect/__init__' not in item[0])
+                                           and ('pexpect/expect' not in item[0])]
+        tblist = traceback.format_list(tblist)
+        return ''.join(tblist)
+
+
+class EOF(ExceptionPexpect):
+    '''Raised when EOF is read from a child.
+    This usually means the child has exited.'''
+
+
+class TIMEOUT(ExceptionPexpect):
+    '''Raised when a read time exceeds the timeout. '''
diff --git a/third_party/pexpect/pexpect/expect.py b/third_party/pexpect/pexpect/expect.py
new file mode 100644
index 0000000..6fde9e8
--- /dev/null
+++ b/third_party/pexpect/pexpect/expect.py
@@ -0,0 +1,297 @@
+import time
+
+from .exceptions import EOF, TIMEOUT
+
+class Expecter(object):
+    def __init__(self, spawn, searcher, searchwindowsize=-1):
+        self.spawn = spawn
+        self.searcher = searcher
+        if searchwindowsize == -1:
+            searchwindowsize = spawn.searchwindowsize
+        self.searchwindowsize = searchwindowsize
+    
+    def new_data(self, data):
+        spawn = self.spawn
+        searcher = self.searcher
+
+        incoming = spawn.buffer + data
+        freshlen = len(data)
+        index = searcher.search(incoming, freshlen, self.searchwindowsize)
+        if index >= 0:
+            spawn.buffer = incoming[searcher.end:]
+            spawn.before = incoming[: searcher.start]
+            spawn.after = incoming[searcher.start: searcher.end]
+            spawn.match = searcher.match
+            spawn.match_index = index
+            # Found a match
+            return index
+    
+        spawn.buffer = incoming
+    
+    def eof(self, err=None):
+        spawn = self.spawn
+        from . import EOF
+
+        spawn.before = spawn.buffer
+        spawn.buffer = spawn.string_type()
+        spawn.after = EOF
+        index = self.searcher.eof_index
+        if index >= 0:
+            spawn.match = EOF
+            spawn.match_index = index
+            return index
+        else:
+            spawn.match = None
+            spawn.match_index = None
+            msg = str(spawn)
+            if err is not None:
+                msg = str(err) + '\n' + msg
+            raise EOF(msg)
+    
+    def timeout(self, err=None):
+        spawn = self.spawn
+        from . import TIMEOUT
+
+        spawn.before = spawn.buffer
+        spawn.after = TIMEOUT
+        index = self.searcher.timeout_index
+        if index >= 0:
+            spawn.match = TIMEOUT
+            spawn.match_index = index
+            return index
+        else:
+            spawn.match = None
+            spawn.match_index = None
+            msg = str(spawn)
+            if err is not None:
+                msg = str(err) + '\n' + msg
+            raise TIMEOUT(msg)
+
+    def errored(self):
+        spawn = self.spawn
+        spawn.before = spawn.buffer
+        spawn.after = None
+        spawn.match = None
+        spawn.match_index = None
+    
+    def expect_loop(self, timeout=-1):
+        """Blocking expect"""
+        spawn = self.spawn
+        from . import EOF, TIMEOUT
+
+        if timeout is not None:
+            end_time = time.time() + timeout
+
+        try:
+            incoming = spawn.buffer
+            spawn.buffer = spawn.string_type()  # Treat buffer as new data
+            while True:
+                idx = self.new_data(incoming)
+                # Keep reading until exception or return.
+                if idx is not None:
+                    return idx
+                # No match at this point
+                if (timeout is not None) and (timeout < 0):
+                    return self.timeout()
+                # Still have time left, so read more data
+                incoming = spawn.read_nonblocking(spawn.maxread, timeout)
+                time.sleep(0.0001)
+                if timeout is not None:
+                    timeout = end_time - time.time()
+        except EOF as e:
+            return self.eof(e)
+        except TIMEOUT as e:
+            return self.timeout(e)
+        except:
+            self.errored()
+            raise
+
+
+class searcher_string(object):
+    '''This is a plain string search helper for the spawn.expect_any() method.
+    This helper class is for speed. For more powerful regex patterns
+    see the helper class, searcher_re.
+
+    Attributes:
+
+        eof_index     - index of EOF, or -1
+        timeout_index - index of TIMEOUT, or -1
+
+    After a successful match by the search() method the following attributes
+    are available:
+
+        start - index into the buffer, first byte of match
+        end   - index into the buffer, first byte after match
+        match - the matching string itself
+
+    '''
+
+    def __init__(self, strings):
+        '''This creates an instance of searcher_string. This argument 'strings'
+        may be a list; a sequence of strings; or the EOF or TIMEOUT types. '''
+
+        self.eof_index = -1
+        self.timeout_index = -1
+        self._strings = []
+        for n, s in enumerate(strings):
+            if s is EOF:
+                self.eof_index = n
+                continue
+            if s is TIMEOUT:
+                self.timeout_index = n
+                continue
+            self._strings.append((n, s))
+
+    def __str__(self):
+        '''This returns a human-readable string that represents the state of
+        the object.'''
+
+        ss = [(ns[0], '    %d: "%s"' % ns) for ns in self._strings]
+        ss.append((-1, 'searcher_string:'))
+        if self.eof_index >= 0:
+            ss.append((self.eof_index, '    %d: EOF' % self.eof_index))
+        if self.timeout_index >= 0:
+            ss.append((self.timeout_index,
+                '    %d: TIMEOUT' % self.timeout_index))
+        ss.sort()
+        ss = list(zip(*ss))[1]
+        return '\n'.join(ss)
+
+    def search(self, buffer, freshlen, searchwindowsize=None):
+        '''This searches 'buffer' for the first occurence of one of the search
+        strings.  'freshlen' must indicate the number of bytes at the end of
+        'buffer' which have not been searched before. It helps to avoid
+        searching the same, possibly big, buffer over and over again.
+
+        See class spawn for the 'searchwindowsize' argument.
+
+        If there is a match this returns the index of that string, and sets
+        'start', 'end' and 'match'. Otherwise, this returns -1. '''
+
+        first_match = None
+
+        # 'freshlen' helps a lot here. Further optimizations could
+        # possibly include:
+        #
+        # using something like the Boyer-Moore Fast String Searching
+        # Algorithm; pre-compiling the search through a list of
+        # strings into something that can scan the input once to
+        # search for all N strings; realize that if we search for
+        # ['bar', 'baz'] and the input is '...foo' we need not bother
+        # rescanning until we've read three more bytes.
+        #
+        # Sadly, I don't know enough about this interesting topic. /grahn
+
+        for index, s in self._strings:
+            if searchwindowsize is None:
+                # the match, if any, can only be in the fresh data,
+                # or at the very end of the old data
+                offset = -(freshlen + len(s))
+            else:
+                # better obey searchwindowsize
+                offset = -searchwindowsize
+            n = buffer.find(s, offset)
+            if n >= 0 and (first_match is None or n < first_match):
+                first_match = n
+                best_index, best_match = index, s
+        if first_match is None:
+            return -1
+        self.match = best_match
+        self.start = first_match
+        self.end = self.start + len(self.match)
+        return best_index
+
+
+class searcher_re(object):
+    '''This is regular expression string search helper for the
+    spawn.expect_any() method. This helper class is for powerful
+    pattern matching. For speed, see the helper class, searcher_string.
+
+    Attributes:
+
+        eof_index     - index of EOF, or -1
+        timeout_index - index of TIMEOUT, or -1
+
+    After a successful match by the search() method the following attributes
+    are available:
+
+        start - index into the buffer, first byte of match
+        end   - index into the buffer, first byte after match
+        match - the re.match object returned by a succesful re.search
+
+    '''
+
+    def __init__(self, patterns):
+        '''This creates an instance that searches for 'patterns' Where
+        'patterns' may be a list or other sequence of compiled regular
+        expressions, or the EOF or TIMEOUT types.'''
+
+        self.eof_index = -1
+        self.timeout_index = -1
+        self._searches = []
+        for n, s in zip(list(range(len(patterns))), patterns):
+            if s is EOF:
+                self.eof_index = n
+                continue
+            if s is TIMEOUT:
+                self.timeout_index = n
+                continue
+            self._searches.append((n, s))
+
+    def __str__(self):
+        '''This returns a human-readable string that represents the state of
+        the object.'''
+
+        #ss = [(n, '    %d: re.compile("%s")' %
+        #    (n, repr(s.pattern))) for n, s in self._searches]
+        ss = list()
+        for n, s in self._searches:
+            try:
+                ss.append((n, '    %d: re.compile("%s")' % (n, s.pattern)))
+            except UnicodeEncodeError:
+                # for test cases that display __str__ of searches, dont throw
+                # another exception just because stdout is ascii-only, using
+                # repr()
+                ss.append((n, '    %d: re.compile(%r)' % (n, s.pattern)))
+        ss.append((-1, 'searcher_re:'))
+        if self.eof_index >= 0:
+            ss.append((self.eof_index, '    %d: EOF' % self.eof_index))
+        if self.timeout_index >= 0:
+            ss.append((self.timeout_index, '    %d: TIMEOUT' %
+                self.timeout_index))
+        ss.sort()
+        ss = list(zip(*ss))[1]
+        return '\n'.join(ss)
+
+    def search(self, buffer, freshlen, searchwindowsize=None):
+        '''This searches 'buffer' for the first occurence of one of the regular
+        expressions. 'freshlen' must indicate the number of bytes at the end of
+        'buffer' which have not been searched before.
+
+        See class spawn for the 'searchwindowsize' argument.
+
+        If there is a match this returns the index of that string, and sets
+        'start', 'end' and 'match'. Otherwise, returns -1.'''
+
+        first_match = None
+        # 'freshlen' doesn't help here -- we cannot predict the
+        # length of a match, and the re module provides no help.
+        if searchwindowsize is None:
+            searchstart = 0
+        else:
+            searchstart = max(0, len(buffer) - searchwindowsize)
+        for index, s in self._searches:
+            match = s.search(buffer, searchstart)
+            if match is None:
+                continue
+            n = match.start()
+            if first_match is None or n < first_match:
+                first_match = n
+                the_match = match
+                best_index = index
+        if first_match is None:
+            return -1
+        self.start = first_match
+        self.match = the_match
+        self.end = self.match.end()
+        return best_index
\ No newline at end of file
diff --git a/third_party/pexpect/pexpect/fdpexpect.py b/third_party/pexpect/pexpect/fdpexpect.py
new file mode 100644
index 0000000..96ca2e1
--- /dev/null
+++ b/third_party/pexpect/pexpect/fdpexpect.py
@@ -0,0 +1,86 @@
+'''This is like pexpect, but it will work with any file descriptor that you
+pass it. You are reponsible for opening and close the file descriptor.
+This allows you to use Pexpect with sockets and named pipes (FIFOs).
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from .spawnbase import SpawnBase
+from .exceptions import ExceptionPexpect
+import os
+
+__all__ = ['fdspawn']
+
+class fdspawn(SpawnBase):
+    '''This is like pexpect.spawn but allows you to supply your own open file
+    descriptor. For example, you could use it to read through a file looking
+    for patterns, or to control a modem or serial device. '''
+
+    def __init__ (self, fd, args=None, timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
+        '''This takes a file descriptor (an int) or an object that support the
+        fileno() method (returning an int). All Python file-like objects
+        support fileno(). '''
+
+        if type(fd) != type(0) and hasattr(fd, 'fileno'):
+            fd = fd.fileno()
+
+        if type(fd) != type(0):
+            raise ExceptionPexpect('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
+
+        try: # make sure fd is a valid file descriptor
+            os.fstat(fd)
+        except OSError:
+            raise ExceptionPexpect('The fd argument is not a valid file descriptor.')
+
+        self.args = None
+        self.command = None
+        SpawnBase.__init__(self, timeout, maxread, searchwindowsize, logfile)
+        self.child_fd = fd
+        self.own_fd = False
+        self.closed = False
+        self.name = '<file descriptor %d>' % fd
+
+    def close (self):
+        """Close the file descriptor.
+
+        Calling this method a second time does nothing, but if the file
+        descriptor was closed elsewhere, :class:`OSError` will be raised.
+        """
+        if self.child_fd == -1:
+            return
+
+        self.flush()
+        os.close(self.child_fd)
+        self.child_fd = -1
+        self.closed = True
+
+    def isalive (self):
+        '''This checks if the file descriptor is still valid. If :func:`os.fstat`
+        does not raise an exception then we assume it is alive. '''
+
+        if self.child_fd == -1:
+            return False
+        try:
+            os.fstat(self.child_fd)
+            return True
+        except:
+            return False
+
+    def terminate (self, force=False):  # pragma: no cover
+        raise ExceptionPexpect('This method is not valid for file descriptors.')
diff --git a/third_party/pexpect/pexpect/pty_spawn.py b/third_party/pexpect/pexpect/pty_spawn.py
new file mode 100644
index 0000000..0663926
--- /dev/null
+++ b/third_party/pexpect/pexpect/pty_spawn.py
@@ -0,0 +1,819 @@
+import os
+import sys
+import time
+import select
+import re
+import pty
+import tty
+import termios
+import errno
+import signal
+from contextlib import contextmanager
+
+import ptyprocess
+from ptyprocess.ptyprocess import use_native_pty_fork
+
+from .exceptions import ExceptionPexpect, EOF, TIMEOUT
+from .spawnbase import SpawnBase, SpawnBaseUnicode
+from .utils import which, split_command_line
+
+ at contextmanager
+def _wrap_ptyprocess_err():
+    """Turn ptyprocess errors into our own ExceptionPexpect errors"""
+    try:
+        yield
+    except ptyprocess.PtyProcessError as e:
+        raise ExceptionPexpect(*e.args)
+
+PY3 = (sys.version_info[0] >= 3)
+
+class spawn(SpawnBase):
+    '''This is the main class interface for Pexpect. Use this class to start
+    and control child applications. '''
+    ptyprocess_class = ptyprocess.PtyProcess
+
+    # This is purely informational now - changing it has no effect
+    use_native_pty_fork = use_native_pty_fork
+
+    def __init__(self, command, args=[], timeout=30, maxread=2000,
+                 searchwindowsize=None, logfile=None, cwd=None, env=None,
+                 ignore_sighup=True, echo=True, preexec_fn=None):
+        '''This is the constructor. The command parameter may be a string that
+        includes a command and any arguments to the command. For example::
+
+            child = pexpect.spawn('/usr/bin/ftp')
+            child = pexpect.spawn('/usr/bin/ssh user at example.com')
+            child = pexpect.spawn('ls -latr /tmp')
+
+        You may also construct it with a list of arguments like so::
+
+            child = pexpect.spawn('/usr/bin/ftp', [])
+            child = pexpect.spawn('/usr/bin/ssh', ['user at example.com'])
+            child = pexpect.spawn('ls', ['-latr', '/tmp'])
+
+        After this the child application will be created and will be ready to
+        talk to. For normal use, see expect() and send() and sendline().
+
+        Remember that Pexpect does NOT interpret shell meta characters such as
+        redirect, pipe, or wild cards (``>``, ``|``, or ``*``). This is a
+        common mistake.  If you want to run a command and pipe it through
+        another command then you must also start a shell. For example::
+
+            child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"')
+            child.expect(pexpect.EOF)
+
+        The second form of spawn (where you pass a list of arguments) is useful
+        in situations where you wish to spawn a command and pass it its own
+        argument list. This can make syntax more clear. For example, the
+        following is equivalent to the previous example::
+
+            shell_cmd = 'ls -l | grep LOG > logs.txt'
+            child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
+            child.expect(pexpect.EOF)
+
+        The maxread attribute sets the read buffer size. This is maximum number
+        of bytes that Pexpect will try to read from a TTY at one time. Setting
+        the maxread size to 1 will turn off buffering. Setting the maxread
+        value higher may help performance in cases where large amounts of
+        output are read back from the child. This feature is useful in
+        conjunction with searchwindowsize.
+
+        The searchwindowsize attribute sets the how far back in the incoming
+        seach buffer Pexpect will search for pattern matches. Every time
+        Pexpect reads some data from the child it will append the data to the
+        incoming buffer. The default is to search from the beginning of the
+        incoming buffer each time new data is read from the child. But this is
+        very inefficient if you are running a command that generates a large
+        amount of data where you want to match. The searchwindowsize does not
+        affect the size of the incoming data buffer. You will still have
+        access to the full buffer after expect() returns.
+
+        The logfile member turns on or off logging. All input and output will
+        be copied to the given file object. Set logfile to None to stop
+        logging. This is the default. Set logfile to sys.stdout to echo
+        everything to standard output. The logfile is flushed after each write.
+
+        Example log input and output to a file::
+
+            child = pexpect.spawn('some_command')
+            fout = open('mylog.txt','wb')
+            child.logfile = fout
+
+        Example log to stdout::
+
+            # In Python 2:
+            child = pexpect.spawn('some_command')
+            child.logfile = sys.stdout
+
+            # In Python 3, spawnu should be used to give str to stdout:
+            child = pexpect.spawnu('some_command')
+            child.logfile = sys.stdout
+
+        The logfile_read and logfile_send members can be used to separately log
+        the input from the child and output sent to the child. Sometimes you
+        don't want to see everything you write to the child. You only want to
+        log what the child sends back. For example::
+
+            child = pexpect.spawn('some_command')
+            child.logfile_read = sys.stdout
+
+        Remember to use spawnu instead of spawn for the above code if you are
+        using Python 3.
+
+        To separately log output sent to the child use logfile_send::
+
+            child.logfile_send = fout
+
+        If ``ignore_sighup`` is True, the child process will ignore SIGHUP
+        signals. For now, the default is True, to preserve the behaviour of
+        earlier versions of Pexpect, but you should pass this explicitly if you
+        want to rely on it.
+
+        The delaybeforesend helps overcome a weird behavior that many users
+        were experiencing. The typical problem was that a user would expect() a
+        "Password:" prompt and then immediately call sendline() to send the
+        password. The user would then see that their password was echoed back
+        to them. Passwords don't normally echo. The problem is caused by the
+        fact that most applications print out the "Password" prompt and then
+        turn off stdin echo, but if you send your password before the
+        application turned off echo, then you get your password echoed.
+        Normally this wouldn't be a problem when interacting with a human at a
+        real keyboard. If you introduce a slight delay just before writing then
+        this seems to clear up the problem. This was such a common problem for
+        many users that I decided that the default pexpect behavior should be
+        to sleep just before writing to the child application. 1/20th of a
+        second (50 ms) seems to be enough to clear up the problem. You can set
+        delaybeforesend to 0 to return to the old behavior. Most Linux machines
+        don't like this to be below 0.03. I don't know why.
+
+        Note that spawn is clever about finding commands on your path.
+        It uses the same logic that "which" uses to find executables.
+
+        If you wish to get the exit status of the child you must call the
+        close() method. The exit or signal status of the child will be stored
+        in self.exitstatus or self.signalstatus. If the child exited normally
+        then exitstatus will store the exit return code and signalstatus will
+        be None. If the child was terminated abnormally with a signal then
+        signalstatus will store the signal value and exitstatus will be None.
+        If you need more detail you can also read the self.status member which
+        stores the status returned by os.waitpid. You can interpret this using
+        os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG.
+
+        The echo attribute may be set to False to disable echoing of input.
+        As a pseudo-terminal, all input echoed by the "keyboard" (send()
+        or sendline()) will be repeated to output.  For many cases, it is
+        not desirable to have echo enabled, and it may be later disabled
+        using setecho(False) followed by waitnoecho().  However, for some
+        platforms such as Solaris, this is not possible, and should be
+        disabled immediately on spawn.
+        
+        If preexec_fn is given, it will be called in the child process before
+        launching the given command. This is useful to e.g. reset inherited
+        signal handlers.
+        '''
+        super(spawn, self).__init__(timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize,
+                                    logfile=logfile)
+        self.STDIN_FILENO = pty.STDIN_FILENO
+        self.STDOUT_FILENO = pty.STDOUT_FILENO
+        self.STDERR_FILENO = pty.STDERR_FILENO
+        self.cwd = cwd
+        self.env = env
+        self.echo = echo
+        self.ignore_sighup = ignore_sighup
+        self.__irix_hack = sys.platform.lower().startswith('irix')
+        if command is None:
+            self.command = None
+            self.args = None
+            self.name = '<pexpect factory incomplete>'
+        else:
+            self._spawn(command, args, preexec_fn)
+
+    def __str__(self):
+        '''This returns a human-readable string that represents the state of
+        the object. '''
+
+        s = []
+        s.append(repr(self))
+        s.append('command: ' + str(self.command))
+        s.append('args: %r' % (self.args,))
+        s.append('searcher: %r' % (self.searcher,))
+        s.append('buffer (last 100 chars): %r' % (
+                self.buffer[-100:] if self.buffer else self.buffer,))
+        s.append('before (last 100 chars): %r' % (
+                self.before[-100:] if self.before else self.before,))
+        s.append('after: %r' % (self.after,))
+        s.append('match: %r' % (self.match,))
+        s.append('match_index: ' + str(self.match_index))
+        s.append('exitstatus: ' + str(self.exitstatus))
+        s.append('flag_eof: ' + str(self.flag_eof))
+        s.append('pid: ' + str(self.pid))
+        s.append('child_fd: ' + str(self.child_fd))
+        s.append('closed: ' + str(self.closed))
+        s.append('timeout: ' + str(self.timeout))
+        s.append('delimiter: ' + str(self.delimiter))
+        s.append('logfile: ' + str(self.logfile))
+        s.append('logfile_read: ' + str(self.logfile_read))
+        s.append('logfile_send: ' + str(self.logfile_send))
+        s.append('maxread: ' + str(self.maxread))
+        s.append('ignorecase: ' + str(self.ignorecase))
+        s.append('searchwindowsize: ' + str(self.searchwindowsize))
+        s.append('delaybeforesend: ' + str(self.delaybeforesend))
+        s.append('delayafterclose: ' + str(self.delayafterclose))
+        s.append('delayafterterminate: ' + str(self.delayafterterminate))
+        return '\n'.join(s)
+
+    def _spawn(self, command, args=[], preexec_fn=None):
+        '''This starts the given command in a child process. This does all the
+        fork/exec type of stuff for a pty. This is called by __init__. If args
+        is empty then command will be parsed (split on spaces) and args will be
+        set to parsed arguments. '''
+
+        # The pid and child_fd of this object get set by this method.
+        # Note that it is difficult for this method to fail.
+        # You cannot detect if the child process cannot start.
+        # So the only way you can tell if the child process started
+        # or not is to try to read from the file descriptor. If you get
+        # EOF immediately then it means that the child is already dead.
+        # That may not necessarily be bad because you may have spawned a child
+        # that performs some task; creates no stdout output; and then dies.
+
+        # If command is an int type then it may represent a file descriptor.
+        if isinstance(command, type(0)):
+            raise ExceptionPexpect('Command is an int type. ' +
+                    'If this is a file descriptor then maybe you want to ' +
+                    'use fdpexpect.fdspawn which takes an existing ' +
+                    'file descriptor instead of a command string.')
+
+        if not isinstance(args, type([])):
+            raise TypeError('The argument, args, must be a list.')
+
+        if args == []:
+            self.args = split_command_line(command)
+            self.command = self.args[0]
+        else:
+            # Make a shallow copy of the args list.
+            self.args = args[:]
+            self.args.insert(0, command)
+            self.command = command
+
+        command_with_path = which(self.command)
+        if command_with_path is None:
+            raise ExceptionPexpect('The command was not found or was not ' +
+                    'executable: %s.' % self.command)
+        self.command = command_with_path
+        self.args[0] = self.command
+
+        self.name = '<' + ' '.join(self.args) + '>'
+
+        assert self.pid is None, 'The pid member must be None.'
+        assert self.command is not None, 'The command member must not be None.'
+
+        kwargs = {'echo': self.echo, 'preexec_fn': preexec_fn}
+        if self.ignore_sighup:
+            def preexec_wrapper():
+                "Set SIGHUP to be ignored, then call the real preexec_fn"
+                signal.signal(signal.SIGHUP, signal.SIG_IGN)
+                if preexec_fn is not None:
+                    preexec_fn()
+            kwargs['preexec_fn'] = preexec_wrapper
+
+        self.ptyproc = self.ptyprocess_class.spawn(self.args, env=self.env,
+                                                   cwd=self.cwd, **kwargs)
+
+        self.pid = self.ptyproc.pid
+        self.child_fd = self.ptyproc.fd
+
+
+        self.terminated = False
+        self.closed = False
+
+    def close(self, force=True):
+        '''This closes the connection with the child application. Note that
+        calling close() more than once is valid. This emulates standard Python
+        behavior with files. Set force to True if you want to make sure that
+        the child is terminated (SIGKILL is sent if the child ignores SIGHUP
+        and SIGINT). '''
+
+        self.flush()
+        self.ptyproc.close()
+        self.isalive()  # Update exit status from ptyproc
+        self.child_fd = -1
+
+    def isatty(self):
+        '''This returns True if the file descriptor is open and connected to a
+        tty(-like) device, else False.
+
+        On SVR4-style platforms implementing streams, such as SunOS and HP-UX,
+        the child pty may not appear as a terminal device.  This means
+        methods such as setecho(), setwinsize(), getwinsize() may raise an
+        IOError. '''
+
+        return os.isatty(self.child_fd)
+
+    def waitnoecho(self, timeout=-1):
+        '''This waits until the terminal ECHO flag is set False. This returns
+        True if the echo mode is off. This returns False if the ECHO flag was
+        not set False before the timeout. This can be used to detect when the
+        child is waiting for a password. Usually a child application will turn
+        off echo mode when it is waiting for the user to enter a password. For
+        example, instead of expecting the "password:" prompt you can wait for
+        the child to set ECHO off::
+
+            p = pexpect.spawn('ssh user at example.com')
+            p.waitnoecho()
+            p.sendline(mypassword)
+
+        If timeout==-1 then this method will use the value in self.timeout.
+        If timeout==None then this method to block until ECHO flag is False.
+        '''
+
+        if timeout == -1:
+            timeout = self.timeout
+        if timeout is not None:
+            end_time = time.time() + timeout
+        while True:
+            if not self.getecho():
+                return True
+            if timeout < 0 and timeout is not None:
+                return False
+            if timeout is not None:
+                timeout = end_time - time.time()
+            time.sleep(0.1)
+
+    def getecho(self):
+        '''This returns the terminal echo mode. This returns True if echo is
+        on or False if echo is off. Child applications that are expecting you
+        to enter a password often set ECHO False. See waitnoecho().
+
+        Not supported on platforms where ``isatty()`` returns False.  '''
+        return self.ptyproc.getecho()
+
+    def setecho(self, state):
+        '''This sets the terminal echo mode on or off. Note that anything the
+        child sent before the echo will be lost, so you should be sure that
+        your input buffer is empty before you call setecho(). For example, the
+        following will work as expected::
+
+            p = pexpect.spawn('cat') # Echo is on by default.
+            p.sendline('1234') # We expect see this twice from the child...
+            p.expect(['1234']) # ... once from the tty echo...
+            p.expect(['1234']) # ... and again from cat itself.
+            p.setecho(False) # Turn off tty echo
+            p.sendline('abcd') # We will set this only once (echoed by cat).
+            p.sendline('wxyz') # We will set this only once (echoed by cat)
+            p.expect(['abcd'])
+            p.expect(['wxyz'])
+
+        The following WILL NOT WORK because the lines sent before the setecho
+        will be lost::
+
+            p = pexpect.spawn('cat')
+            p.sendline('1234')
+            p.setecho(False) # Turn off tty echo
+            p.sendline('abcd') # We will set this only once (echoed by cat).
+            p.sendline('wxyz') # We will set this only once (echoed by cat)
+            p.expect(['1234'])
+            p.expect(['1234'])
+            p.expect(['abcd'])
+            p.expect(['wxyz'])
+
+
+        Not supported on platforms where ``isatty()`` returns False.
+        '''
+        return self.ptyproc.setecho(state)
+
+        self.echo = state
+
+    def read_nonblocking(self, size=1, timeout=-1):
+        '''This reads at most size characters from the child application. It
+        includes a timeout. If the read does not complete within the timeout
+        period then a TIMEOUT exception is raised. If the end of file is read
+        then an EOF exception will be raised. If a log file was set using
+        setlog() then all data will also be written to the log file.
+
+        If timeout is None then the read may block indefinitely.
+        If timeout is -1 then the self.timeout value is used. If timeout is 0
+        then the child is polled and if there is no data immediately ready
+        then this will raise a TIMEOUT exception.
+
+        The timeout refers only to the amount of time to read at least one
+        character. This is not effected by the 'size' parameter, so if you call
+        read_nonblocking(size=100, timeout=30) and only one character is
+        available right away then one character will be returned immediately.
+        It will not wait for 30 seconds for another 99 characters to come in.
+
+        This is a wrapper around os.read(). It uses select.select() to
+        implement the timeout. '''
+
+        if self.closed:
+            raise ValueError('I/O operation on closed file.')
+
+        if timeout == -1:
+            timeout = self.timeout
+
+        # Note that some systems such as Solaris do not give an EOF when
+        # the child dies. In fact, you can still try to read
+        # from the child_fd -- it will block forever or until TIMEOUT.
+        # For this case, I test isalive() before doing any reading.
+        # If isalive() is false, then I pretend that this is the same as EOF.
+        if not self.isalive():
+            # timeout of 0 means "poll"
+            r, w, e = self.__select([self.child_fd], [], [], 0)
+            if not r:
+                self.flag_eof = True
+                raise EOF('End Of File (EOF). Braindead platform.')
+        elif self.__irix_hack:
+            # Irix takes a long time before it realizes a child was terminated.
+            # FIXME So does this mean Irix systems are forced to always have
+            # FIXME a 2 second delay when calling read_nonblocking? That sucks.
+            r, w, e = self.__select([self.child_fd], [], [], 2)
+            if not r and not self.isalive():
+                self.flag_eof = True
+                raise EOF('End Of File (EOF). Slow platform.')
+
+        r, w, e = self.__select([self.child_fd], [], [], timeout)
+
+        if not r:
+            if not self.isalive():
+                # Some platforms, such as Irix, will claim that their
+                # processes are alive; timeout on the select; and
+                # then finally admit that they are not alive.
+                self.flag_eof = True
+                raise EOF('End of File (EOF). Very slow platform.')
+            else:
+                raise TIMEOUT('Timeout exceeded.')
+
+        if self.child_fd in r:
+            return super(spawn, self).read_nonblocking(size)
+
+        raise ExceptionPexpect('Reached an unexpected state.')  # pragma: no cover
+
+    def write(self, s):
+        '''This is similar to send() except that there is no return value.
+        '''
+
+        self.send(s)
+
+    def writelines(self, sequence):
+        '''This calls write() for each element in the sequence. The sequence
+        can be any iterable object producing strings, typically a list of
+        strings. This does not add line separators. There is no return value.
+        '''
+
+        for s in sequence:
+            self.write(s)
+
+    def send(self, s):
+        '''Sends string ``s`` to the child process, returning the number of
+        bytes written. If a logfile is specified, a copy is written to that
+        log.
+
+        The default terminal input mode is canonical processing unless set
+        otherwise by the child process. This allows backspace and other line
+        processing to be performed prior to transmitting to the receiving
+        program. As this is buffered, there is a limited size of such buffer.
+
+        On Linux systems, this is 4096 (defined by N_TTY_BUF_SIZE). All
+        other systems honor the POSIX.1 definition PC_MAX_CANON -- 1024
+        on OSX, 256 on OpenSolaris, 255 on FreeBSD.
+
+        This value may be discovered using fpathconf(3)::
+
+        >>> from os import fpathconf
+        >>> print(fpathconf(0, 'PC_MAX_CANON'))
+        256
+
+        On such a system, only 256 bytes may be received per line. Any
+        subsequent bytes received will be discarded. BEL (``'\a'``) is then
+        sent to output if IMAXBEL (termios.h) is set by the tty driver.
+        This is usually enabled by default.  Linux does not honor this as
+        an option -- it behaves as though it is always set on.
+
+        Canonical input processing may be disabled altogether by executing
+        a shell, then stty(1), before executing the final program::
+
+        >>> bash = pexpect.spawn('/bin/bash', echo=False)
+        >>> bash.sendline('stty -icanon')
+        >>> bash.sendline('base64')
+        >>> bash.sendline('x' * 5000)
+        '''
+
+        time.sleep(self.delaybeforesend)
+
+        s = self._coerce_send_string(s)
+        self._log(s, 'send')
+
+        return self._send(s)
+
+    def _send(self, s):
+        return os.write(self.child_fd, s)
+
+    def sendline(self, s=''):
+        '''Wraps send(), sending string ``s`` to child process, with
+        ``os.linesep`` automatically appended. Returns number of bytes
+        written.  Only a limited number of bytes may be sent for each
+        line in the default terminal mode, see docstring of :meth:`send`.
+        '''
+
+        n = self.send(s)
+        n = n + self.send(self.linesep)
+        return n
+
+    def _log_control(self, byte):
+        """Write control characters to the appropriate log files"""
+        self._log(byte, 'send')
+
+    def sendcontrol(self, char):
+        '''Helper method that wraps send() with mnemonic access for sending control
+        character to the child (such as Ctrl-C or Ctrl-D).  For example, to send
+        Ctrl-G (ASCII 7, bell, '\a')::
+
+            child.sendcontrol('g')
+
+        See also, sendintr() and sendeof().
+        '''
+        n, byte = self.ptyproc.sendcontrol(char)
+        self._log_control(byte)
+        return n
+
+    def sendeof(self):
+        '''This sends an EOF to the child. This sends a character which causes
+        the pending parent output buffer to be sent to the waiting child
+        program without waiting for end-of-line. If it is the first character
+        of the line, the read() in the user program returns 0, which signifies
+        end-of-file. This means to work as expected a sendeof() has to be
+        called at the beginning of a line. This method does not send a newline.
+        It is the responsibility of the caller to ensure the eof is sent at the
+        beginning of a line. '''
+
+        n, byte = self.ptyproc.sendeof()
+        self._log_control(byte)
+
+    def sendintr(self):
+        '''This sends a SIGINT to the child. It does not require
+        the SIGINT to be the first character on a line. '''
+
+        n, byte = self.ptyproc.sendintr()
+        self._log_control(byte)
+
+    @property
+    def flag_eof(self):
+        return self.ptyproc.flag_eof
+
+    @flag_eof.setter
+    def flag_eof(self, value):
+        self.ptyproc.flag_eof = value
+
+    def eof(self):
+        '''This returns True if the EOF exception was ever raised.
+        '''
+        return self.flag_eof
+
+    def terminate(self, force=False):
+        '''This forces a child process to terminate. It starts nicely with
+        SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
+        returns True if the child was terminated. This returns False if the
+        child could not be terminated. '''
+
+        if not self.isalive():
+            return True
+        try:
+            self.kill(signal.SIGHUP)
+            time.sleep(self.delayafterterminate)
+            if not self.isalive():
+                return True
+            self.kill(signal.SIGCONT)
+            time.sleep(self.delayafterterminate)
+            if not self.isalive():
+                return True
+            self.kill(signal.SIGINT)
+            time.sleep(self.delayafterterminate)
+            if not self.isalive():
+                return True
+            if force:
+                self.kill(signal.SIGKILL)
+                time.sleep(self.delayafterterminate)
+                if not self.isalive():
+                    return True
+                else:
+                    return False
+            return False
+        except OSError:
+            # I think there are kernel timing issues that sometimes cause
+            # this to happen. I think isalive() reports True, but the
+            # process is dead to the kernel.
+            # Make one last attempt to see if the kernel is up to date.
+            time.sleep(self.delayafterterminate)
+            if not self.isalive():
+                return True
+            else:
+                return False
+
+    def wait(self):
+        '''This waits until the child exits. This is a blocking call. This will
+        not read any data from the child, so this will block forever if the
+        child has unread output and has terminated. In other words, the child
+        may have printed output then called exit(), but, the child is
+        technically still alive until its output is read by the parent. '''
+
+        ptyproc = self.ptyproc
+        with _wrap_ptyprocess_err():
+            exitstatus = ptyproc.wait()
+        self.status = ptyproc.status
+        self.exitstatus = ptyproc.exitstatus
+        self.signalstatus = ptyproc.signalstatus
+        self.terminated = True
+
+        return exitstatus
+
+    def isalive(self):
+        '''This tests if the child process is running or not. This is
+        non-blocking. If the child was terminated then this will read the
+        exitstatus or signalstatus of the child. This returns True if the child
+        process appears to be running or False if not. It can take literally
+        SECONDS for Solaris to return the right status. '''
+
+        ptyproc = self.ptyproc
+        with _wrap_ptyprocess_err():
+            alive = ptyproc.isalive()
+
+        if not alive:
+            self.status = ptyproc.status
+            self.exitstatus = ptyproc.exitstatus
+            self.signalstatus = ptyproc.signalstatus
+            self.terminated = True
+
+        return alive
+
+    def kill(self, sig):
+
+        '''This sends the given signal to the child application. In keeping
+        with UNIX tradition it has a misleading name. It does not necessarily
+        kill the child unless you send the right signal. '''
+
+        # Same as os.kill, but the pid is given for you.
+        if self.isalive():
+            os.kill(self.pid, sig)
+
+    def getwinsize(self):
+        '''This returns the terminal window size of the child tty. The return
+        value is a tuple of (rows, cols). '''
+        return self.ptyproc.getwinsize()
+
+    def setwinsize(self, rows, cols):
+        '''This sets the terminal window size of the child tty. This will cause
+        a SIGWINCH signal to be sent to the child. This does not change the
+        physical window size. It changes the size reported to TTY-aware
+        applications like vi or curses -- applications that respond to the
+        SIGWINCH signal. '''
+        return self.ptyproc.setwinsize(rows, cols)
+
+
+    def interact(self, escape_character=chr(29),
+            input_filter=None, output_filter=None):
+
+        '''This gives control of the child process to the interactive user (the
+        human at the keyboard). Keystrokes are sent to the child process, and
+        the stdout and stderr output of the child process is printed. This
+        simply echos the child stdout and child stderr to the real stdout and
+        it echos the real stdin to the child stdin. When the user types the
+        escape_character this method will stop. The default for
+        escape_character is ^]. This should not be confused with ASCII 27 --
+        the ESC character. ASCII 29 was chosen for historical merit because
+        this is the character used by 'telnet' as the escape character. The
+        escape_character will not be sent to the child process.
+
+        You may pass in optional input and output filter functions. These
+        functions should take a string and return a string. The output_filter
+        will be passed all the output from the child process. The input_filter
+        will be passed all the keyboard input from the user. The input_filter
+        is run BEFORE the check for the escape_character.
+
+        Note that if you change the window size of the parent the SIGWINCH
+        signal will not be passed through to the child. If you want the child
+        window size to change when the parent's window size changes then do
+        something like the following example::
+
+            import pexpect, struct, fcntl, termios, signal, sys
+            def sigwinch_passthrough (sig, data):
+                s = struct.pack("HHHH", 0, 0, 0, 0)
+                a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(),
+                    termios.TIOCGWINSZ , s))
+                global p
+                p.setwinsize(a[0],a[1])
+            # Note this 'p' global and used in sigwinch_passthrough.
+            p = pexpect.spawn('/bin/bash')
+            signal.signal(signal.SIGWINCH, sigwinch_passthrough)
+            p.interact()
+        '''
+
+        # Flush the buffer.
+        self.write_to_stdout(self.buffer)
+        self.stdout.flush()
+        self.buffer = self.string_type()
+        mode = tty.tcgetattr(self.STDIN_FILENO)
+        tty.setraw(self.STDIN_FILENO)
+        if PY3:
+            escape_character = escape_character.encode('latin-1')
+        try:
+            self.__interact_copy(escape_character, input_filter, output_filter)
+        finally:
+            tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
+
+    def __interact_writen(self, fd, data):
+        '''This is used by the interact() method.
+        '''
+
+        while data != b'' and self.isalive():
+            n = os.write(fd, data)
+            data = data[n:]
+
+    def __interact_read(self, fd):
+        '''This is used by the interact() method.
+        '''
+
+        return os.read(fd, 1000)
+
+    def __interact_copy(self, escape_character=None,
+            input_filter=None, output_filter=None):
+
+        '''This is used by the interact() method.
+        '''
+
+        while self.isalive():
+            r, w, e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
+            if self.child_fd in r:
+                try:
+                    data = self.__interact_read(self.child_fd)
+                except OSError as err:
+                    if err.args[0] == errno.EIO:
+                        # Linux-style EOF
+                        break
+                    raise
+                if data == b'':
+                    # BSD-style EOF
+                    break
+                if output_filter:
+                    data = output_filter(data)
+                if self.logfile is not None:
+                    self.logfile.write(data)
+                    self.logfile.flush()
+                os.write(self.STDOUT_FILENO, data)
+            if self.STDIN_FILENO in r:
+                data = self.__interact_read(self.STDIN_FILENO)
+                if input_filter:
+                    data = input_filter(data)
+                i = data.rfind(escape_character)
+                if i != -1:
+                    data = data[:i]
+                    self.__interact_writen(self.child_fd, data)
+                    break
+                self.__interact_writen(self.child_fd, data)
+
+    def __select(self, iwtd, owtd, ewtd, timeout=None):
+
+        '''This is a wrapper around select.select() that ignores signals. If
+        select.select raises a select.error exception and errno is an EINTR
+        error then it is ignored. Mainly this is used to ignore sigwinch
+        (terminal resize). '''
+
+        # if select() is interrupted by a signal (errno==EINTR) then
+        # we loop back and enter the select() again.
+        if timeout is not None:
+            end_time = time.time() + timeout
+        while True:
+            try:
+                return select.select(iwtd, owtd, ewtd, timeout)
+            except select.error:
+                err = sys.exc_info()[1]
+                if err.args[0] == errno.EINTR:
+                    # if we loop back we have to subtract the
+                    # amount of time we already waited.
+                    if timeout is not None:
+                        timeout = end_time - time.time()
+                        if timeout < 0:
+                            return([], [], [])
+                else:
+                    # something else caused the select.error, so
+                    # this actually is an exception.
+                    raise
+
+
+class spawnu(SpawnBaseUnicode, spawn):
+    """Works like spawn, but accepts and returns unicode strings.
+
+    Extra parameters:
+
+    :param encoding: The encoding to use for communications (default: 'utf-8')
+    :param errors: How to handle encoding/decoding errors; one of 'strict'
+                   (the default), 'ignore', or 'replace', as described
+                   for :meth:`~bytes.decode` and :meth:`~str.encode`.
+    """
+    ptyprocess_class = ptyprocess.PtyProcessUnicode
+
+    def _send(self, s):
+        return os.write(self.child_fd, s.encode(self.encoding, self.errors))
+
+    def _log_control(self, byte):
+        s = byte.decode(self.encoding, 'replace')
+        self._log(s, 'send')
diff --git a/third_party/pexpect/pexpect/pxssh.py b/third_party/pexpect/pexpect/pxssh.py
new file mode 100644
index 0000000..71f56a0
--- /dev/null
+++ b/third_party/pexpect/pexpect/pxssh.py
@@ -0,0 +1,403 @@
+'''This class extends pexpect.spawn to specialize setting up SSH connections.
+This adds methods for login, logout, and expecting the shell prompt.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from pexpect import ExceptionPexpect, TIMEOUT, EOF, spawn
+import time
+import os
+
+__all__ = ['ExceptionPxssh', 'pxssh']
+
+# Exception classes used by this module.
+class ExceptionPxssh(ExceptionPexpect):
+    '''Raised for pxssh exceptions.
+    '''
+
+class pxssh (spawn):
+    '''This class extends pexpect.spawn to specialize setting up SSH
+    connections. This adds methods for login, logout, and expecting the shell
+    prompt. It does various tricky things to handle many situations in the SSH
+    login process. For example, if the session is your first login, then pxssh
+    automatically accepts the remote certificate; or if you have public key
+    authentication setup then pxssh won't wait for the password prompt.
+
+    pxssh uses the shell prompt to synchronize output from the remote host. In
+    order to make this more robust it sets the shell prompt to something more
+    unique than just $ or #. This should work on most Borne/Bash or Csh style
+    shells.
+
+    Example that runs a few commands on a remote server and prints the result::
+
+        import pxssh
+        import getpass
+        try:
+            s = pxssh.pxssh()
+            hostname = raw_input('hostname: ')
+            username = raw_input('username: ')
+            password = getpass.getpass('password: ')
+            s.login(hostname, username, password)
+            s.sendline('uptime')   # run a command
+            s.prompt()             # match the prompt
+            print(s.before)        # print everything before the prompt.
+            s.sendline('ls -l')
+            s.prompt()
+            print(s.before)
+            s.sendline('df')
+            s.prompt()
+            print(s.before)
+            s.logout()
+        except pxssh.ExceptionPxssh as e:
+            print("pxssh failed on login.")
+            print(e)
+
+    Example showing how to specify SSH options::
+
+        import pxssh
+        s = pxssh.pxssh(options={
+                            "StrictHostKeyChecking": "no",
+                            "UserKnownHostsFile": "/dev/null"})
+        ...
+
+    Note that if you have ssh-agent running while doing development with pxssh
+    then this can lead to a lot of confusion. Many X display managers (xdm,
+    gdm, kdm, etc.) will automatically start a GUI agent. You may see a GUI
+    dialog box popup asking for a password during development. You should turn
+    off any key agents during testing. The 'force_password' attribute will turn
+    off public key authentication. This will only work if the remote SSH server
+    is configured to allow password logins. Example of using 'force_password'
+    attribute::
+
+            s = pxssh.pxssh()
+            s.force_password = True
+            hostname = raw_input('hostname: ')
+            username = raw_input('username: ')
+            password = getpass.getpass('password: ')
+            s.login (hostname, username, password)
+    '''
+
+    def __init__ (self, timeout=30, maxread=2000, searchwindowsize=None,
+                    logfile=None, cwd=None, env=None, ignore_sighup=True, echo=True,
+                    options={}):
+
+        spawn.__init__(self, None, timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, logfile=logfile, cwd=cwd, env=env, ignore_sighup=ignore_sighup, echo=echo)
+
+        self.name = '<pxssh>'
+
+        #SUBTLE HACK ALERT! Note that the command that SETS the prompt uses a
+        #slightly different string than the regular expression to match it. This
+        #is because when you set the prompt the command will echo back, but we
+        #don't want to match the echoed command. So if we make the set command
+        #slightly different than the regex we eliminate the problem. To make the
+        #set command different we add a backslash in front of $. The $ doesn't
+        #need to be escaped, but it doesn't hurt and serves to make the set
+        #prompt command different than the regex.
+
+        # used to match the command-line prompt
+        self.UNIQUE_PROMPT = "\[PEXPECT\][\$\#] "
+        self.PROMPT = self.UNIQUE_PROMPT
+
+        # used to set shell command-line prompt to UNIQUE_PROMPT.
+        self.PROMPT_SET_SH = "PS1='[PEXPECT]\$ '"
+        self.PROMPT_SET_CSH = "set prompt='[PEXPECT]\$ '"
+        self.SSH_OPTS = ("-o'RSAAuthentication=no'"
+                + " -o 'PubkeyAuthentication=no'")
+# Disabling host key checking, makes you vulnerable to MITM attacks.
+#                + " -o 'StrictHostKeyChecking=no'"
+#                + " -o 'UserKnownHostsFile /dev/null' ")
+        # Disabling X11 forwarding gets rid of the annoying SSH_ASKPASS from
+        # displaying a GUI password dialog. I have not figured out how to
+        # disable only SSH_ASKPASS without also disabling X11 forwarding.
+        # Unsetting SSH_ASKPASS on the remote side doesn't disable it! Annoying!
+        #self.SSH_OPTS = "-x -o'RSAAuthentication=no' -o 'PubkeyAuthentication=no'"
+        self.force_password = False
+
+        # User defined SSH options, eg,
+        # ssh.otions = dict(StrictHostKeyChecking="no",UserKnownHostsFile="/dev/null")
+        self.options = options
+
+    def levenshtein_distance(self, a, b):
+        '''This calculates the Levenshtein distance between a and b.
+        '''
+
+        n, m = len(a), len(b)
+        if n > m:
+            a,b = b,a
+            n,m = m,n
+        current = range(n+1)
+        for i in range(1,m+1):
+            previous, current = current, [i]+[0]*n
+            for j in range(1,n+1):
+                add, delete = previous[j]+1, current[j-1]+1
+                change = previous[j-1]
+                if a[j-1] != b[i-1]:
+                    change = change + 1
+                current[j] = min(add, delete, change)
+        return current[n]
+
+    def try_read_prompt(self, timeout_multiplier):
+        '''This facilitates using communication timeouts to perform
+        synchronization as quickly as possible, while supporting high latency
+        connections with a tunable worst case performance. Fast connections
+        should be read almost immediately. Worst case performance for this
+        method is timeout_multiplier * 3 seconds.
+        '''
+
+        # maximum time allowed to read the first response
+        first_char_timeout = timeout_multiplier * 0.5
+
+        # maximum time allowed between subsequent characters
+        inter_char_timeout = timeout_multiplier * 0.1
+
+        # maximum time for reading the entire prompt
+        total_timeout = timeout_multiplier * 3.0
+
+        prompt = b''
+        begin = time.time()
+        expired = 0.0
+        timeout = first_char_timeout
+
+        while expired < total_timeout:
+            try:
+                prompt += self.read_nonblocking(size=1, timeout=timeout)
+                expired = time.time() - begin # updated total time expired
+                timeout = inter_char_timeout
+            except TIMEOUT:
+                break
+
+        return prompt
+
+    def sync_original_prompt (self, sync_multiplier=1.0):
+        '''This attempts to find the prompt. Basically, press enter and record
+        the response; press enter again and record the response; if the two
+        responses are similar then assume we are at the original prompt.
+        This can be a slow function. Worst case with the default sync_multiplier
+        can take 12 seconds. Low latency connections are more likely to fail
+        with a low sync_multiplier. Best case sync time gets worse with a
+        high sync multiplier (500 ms with default). '''
+
+        # All of these timing pace values are magic.
+        # I came up with these based on what seemed reliable for
+        # connecting to a heavily loaded machine I have.
+        self.sendline()
+        time.sleep(0.1)
+
+        try:
+            # Clear the buffer before getting the prompt.
+            self.try_read_prompt(sync_multiplier)
+        except TIMEOUT:
+            pass
+
+        self.sendline()
+        x = self.try_read_prompt(sync_multiplier)
+
+        self.sendline()
+        a = self.try_read_prompt(sync_multiplier)
+
+        self.sendline()
+        b = self.try_read_prompt(sync_multiplier)
+
+        ld = self.levenshtein_distance(a,b)
+        len_a = len(a)
+        if len_a == 0:
+            return False
+        if float(ld)/len_a < 0.4:
+            return True
+        return False
+
+    ### TODO: This is getting messy and I'm pretty sure this isn't perfect.
+    ### TODO: I need to draw a flow chart for this.
+    def login (self, server, username, password='', terminal_type='ansi',
+                original_prompt=r"[#$]", login_timeout=10, port=None,
+                auto_prompt_reset=True, ssh_key=None, quiet=True,
+                sync_multiplier=1, check_local_ip=True):
+        '''This logs the user into the given server.
+
+        It uses
+        'original_prompt' to try to find the prompt right after login. When it
+        finds the prompt it immediately tries to reset the prompt to something
+        more easily matched. The default 'original_prompt' is very optimistic
+        and is easily fooled. It's more reliable to try to match the original
+        prompt as exactly as possible to prevent false matches by server
+        strings such as the "Message Of The Day". On many systems you can
+        disable the MOTD on the remote server by creating a zero-length file
+        called :file:`~/.hushlogin` on the remote server. If a prompt cannot be found
+        then this will not necessarily cause the login to fail. In the case of
+        a timeout when looking for the prompt we assume that the original
+        prompt was so weird that we could not match it, so we use a few tricks
+        to guess when we have reached the prompt. Then we hope for the best and
+        blindly try to reset the prompt to something more unique. If that fails
+        then login() raises an :class:`ExceptionPxssh` exception.
+
+        In some situations it is not possible or desirable to reset the
+        original prompt. In this case, pass ``auto_prompt_reset=False`` to
+        inhibit setting the prompt to the UNIQUE_PROMPT. Remember that pxssh
+        uses a unique prompt in the :meth:`prompt` method. If the original prompt is
+        not reset then this will disable the :meth:`prompt` method unless you
+        manually set the :attr:`PROMPT` attribute.
+        '''
+
+        ssh_options = ''.join([" -o '%s=%s'" % (o, v) for (o, v) in self.options.items()])
+        if quiet:
+            ssh_options = ssh_options + ' -q'
+        if not check_local_ip:
+            ssh_options = ssh_options + " -o'NoHostAuthenticationForLocalhost=yes'"
+        if self.force_password:
+            ssh_options = ssh_options + ' ' + self.SSH_OPTS
+        if port is not None:
+            ssh_options = ssh_options + ' -p %s'%(str(port))
+        if ssh_key is not None:
+            try:
+                os.path.isfile(ssh_key)
+            except:
+                raise ExceptionPxssh('private ssh key does not exist')
+            ssh_options = ssh_options + ' -i %s' % (ssh_key)
+        cmd = "ssh %s -l %s %s" % (ssh_options, username, server)
+
+        # This does not distinguish between a remote server 'password' prompt
+        # and a local ssh 'passphrase' prompt (for unlocking a private key).
+        spawn._spawn(self, cmd)
+        i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT, "(?i)connection closed by remote host"], timeout=login_timeout)
+
+        # First phase
+        if i==0:
+            # New certificate -- always accept it.
+            # This is what you get if SSH does not have the remote host's
+            # public key stored in the 'known_hosts' cache.
+            self.sendline("yes")
+            i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT])
+        if i==2: # password or passphrase
+            self.sendline(password)
+            i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT])
+        if i==4:
+            self.sendline(terminal_type)
+            i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT])
+
+        # Second phase
+        if i==0:
+            # This is weird. This should not happen twice in a row.
+            self.close()
+            raise ExceptionPxssh('Weird error. Got "are you sure" prompt twice.')
+        elif i==1: # can occur if you have a public key pair set to authenticate.
+            ### TODO: May NOT be OK if expect() got tricked and matched a false prompt.
+            pass
+        elif i==2: # password prompt again
+            # For incorrect passwords, some ssh servers will
+            # ask for the password again, others return 'denied' right away.
+            # If we get the password prompt again then this means
+            # we didn't get the password right the first time.
+            self.close()
+            raise ExceptionPxssh('password refused')
+        elif i==3: # permission denied -- password was bad.
+            self.close()
+            raise ExceptionPxssh('permission denied')
+        elif i==4: # terminal type again? WTF?
+            self.close()
+            raise ExceptionPxssh('Weird error. Got "terminal type" prompt twice.')
+        elif i==5: # Timeout
+            #This is tricky... I presume that we are at the command-line prompt.
+            #It may be that the shell prompt was so weird that we couldn't match
+            #it. Or it may be that we couldn't log in for some other reason. I
+            #can't be sure, but it's safe to guess that we did login because if
+            #I presume wrong and we are not logged in then this should be caught
+            #later when I try to set the shell prompt.
+            pass
+        elif i==6: # Connection closed by remote host
+            self.close()
+            raise ExceptionPxssh('connection closed')
+        else: # Unexpected
+            self.close()
+            raise ExceptionPxssh('unexpected login response')
+        if not self.sync_original_prompt(sync_multiplier):
+            self.close()
+            raise ExceptionPxssh('could not synchronize with original prompt')
+        # We appear to be in.
+        # set shell prompt to something unique.
+        if auto_prompt_reset:
+            if not self.set_unique_prompt():
+                self.close()
+                raise ExceptionPxssh('could not set shell prompt '
+                                     '(recieved: %r, expected: %r).' % (
+                                         self.before, self.PROMPT,))
+        return True
+
+    def logout (self):
+        '''Sends exit to the remote shell.
+
+        If there are stopped jobs then this automatically sends exit twice.
+        '''
+        self.sendline("exit")
+        index = self.expect([EOF, "(?i)there are stopped jobs"])
+        if index==1:
+            self.sendline("exit")
+            self.expect(EOF)
+        self.close()
+
+    def prompt(self, timeout=-1):
+        '''Match the next shell prompt.
+
+        This is little more than a short-cut to the :meth:`~pexpect.spawn.expect`
+        method. Note that if you called :meth:`login` with
+        ``auto_prompt_reset=False``, then before calling :meth:`prompt` you must
+        set the :attr:`PROMPT` attribute to a regex that it will use for
+        matching the prompt.
+
+        Calling :meth:`prompt` will erase the contents of the :attr:`before`
+        attribute even if no prompt is ever matched. If timeout is not given or
+        it is set to -1 then self.timeout is used.
+
+        :return: True if the shell prompt was matched, False if the timeout was
+                 reached.
+        '''
+
+        if timeout == -1:
+            timeout = self.timeout
+        i = self.expect([self.PROMPT, TIMEOUT], timeout=timeout)
+        if i==1:
+            return False
+        return True
+
+    def set_unique_prompt(self):
+        '''This sets the remote prompt to something more unique than ``#`` or ``$``.
+        This makes it easier for the :meth:`prompt` method to match the shell prompt
+        unambiguously. This method is called automatically by the :meth:`login`
+        method, but you may want to call it manually if you somehow reset the
+        shell prompt. For example, if you 'su' to a different user then you
+        will need to manually reset the prompt. This sends shell commands to
+        the remote host to set the prompt, so this assumes the remote host is
+        ready to receive commands.
+
+        Alternatively, you may use your own prompt pattern. In this case you
+        should call :meth:`login` with ``auto_prompt_reset=False``; then set the
+        :attr:`PROMPT` attribute to a regular expression. After that, the
+        :meth:`prompt` method will try to match your prompt pattern.
+        '''
+
+        self.sendline("unset PROMPT_COMMAND")
+        self.sendline(self.PROMPT_SET_SH) # sh-style
+        i = self.expect ([TIMEOUT, self.PROMPT], timeout=10)
+        if i == 0: # csh-style
+            self.sendline(self.PROMPT_SET_CSH)
+            i = self.expect([TIMEOUT, self.PROMPT], timeout=10)
+            if i == 0:
+                return False
+        return True
+
+# vi:ts=4:sw=4:expandtab:ft=python:
diff --git a/third_party/pexpect/pexpect/replwrap.py b/third_party/pexpect/pexpect/replwrap.py
new file mode 100644
index 0000000..7b0e823
--- /dev/null
+++ b/third_party/pexpect/pexpect/replwrap.py
@@ -0,0 +1,113 @@
+"""Generic wrapper for read-eval-print-loops, a.k.a. interactive shells
+"""
+import os.path
+import signal
+import sys
+import re
+
+import pexpect
+
+PY3 = (sys.version_info[0] >= 3)
+
+if PY3:
+    def u(s): return s
+else:
+    def u(s): return s.decode('utf-8')
+
+PEXPECT_PROMPT = u('[PEXPECT_PROMPT>')
+PEXPECT_CONTINUATION_PROMPT = u('[PEXPECT_PROMPT+')
+
+class REPLWrapper(object):
+    """Wrapper for a REPL.
+
+    :param cmd_or_spawn: This can either be an instance of :class:`pexpect.spawn`
+      in which a REPL has already been started, or a str command to start a new
+      REPL process.
+    :param str orig_prompt: The prompt to expect at first.
+    :param str prompt_change: A command to change the prompt to something more
+      unique. If this is ``None``, the prompt will not be changed. This will
+      be formatted with the new and continuation prompts as positional
+      parameters, so you can use ``{}`` style formatting to insert them into
+      the command.
+    :param str new_prompt: The more unique prompt to expect after the change.
+    :param str extra_init_cmd: Commands to do extra initialisation, such as
+      disabling pagers.
+    """
+    def __init__(self, cmd_or_spawn, orig_prompt, prompt_change,
+                 new_prompt=PEXPECT_PROMPT,
+                 continuation_prompt=PEXPECT_CONTINUATION_PROMPT,
+                 extra_init_cmd=None):
+        if isinstance(cmd_or_spawn, str):
+            self.child = pexpect.spawnu(cmd_or_spawn, echo=False)
+        else:
+            self.child = cmd_or_spawn
+        if self.child.echo:
+            # Existing spawn instance has echo enabled, disable it
+            # to prevent our input from being repeated to output.
+            self.child.setecho(False)
+            self.child.waitnoecho()
+
+        if prompt_change is None:
+            self.prompt = orig_prompt
+        else:
+            self.set_prompt(orig_prompt,
+                        prompt_change.format(new_prompt, continuation_prompt))
+            self.prompt = new_prompt
+        self.continuation_prompt = continuation_prompt
+
+        self._expect_prompt()
+
+        if extra_init_cmd is not None:
+            self.run_command(extra_init_cmd)
+
+    def set_prompt(self, orig_prompt, prompt_change):
+        self.child.expect(orig_prompt)
+        self.child.sendline(prompt_change)
+
+    def _expect_prompt(self, timeout=-1):
+        return self.child.expect_exact([self.prompt, self.continuation_prompt],
+                                       timeout=timeout)
+
+    def run_command(self, command, timeout=-1):
+        """Send a command to the REPL, wait for and return output.
+
+        :param str command: The command to send. Trailing newlines are not needed.
+          This should be a complete block of input that will trigger execution;
+          if a continuation prompt is found after sending input, :exc:`ValueError`
+          will be raised.
+        :param int timeout: How long to wait for the next prompt. -1 means the
+          default from the :class:`pexpect.spawn` object (default 30 seconds).
+          None means to wait indefinitely.
+        """
+        # Split up multiline commands and feed them in bit-by-bit
+        cmdlines = command.splitlines()
+        # splitlines ignores trailing newlines - add it back in manually
+        if command.endswith('\n'):
+            cmdlines.append('')
+        if not cmdlines:
+            raise ValueError("No command was given")
+
+        self.child.sendline(cmdlines[0])
+        for line in cmdlines[1:]:
+            self._expect_prompt(timeout=1)
+            self.child.sendline(line)
+
+        # Command was fully submitted, now wait for the next prompt
+        if self._expect_prompt(timeout=timeout) == 1:
+            # We got the continuation prompt - command was incomplete
+            self.child.kill(signal.SIGINT)
+            self._expect_prompt(timeout=1)
+            raise ValueError("Continuation prompt found - input was incomplete:\n"
+                             + command)
+        return self.child.before
+
+def python(command="python"):
+    """Start a Python shell and return a :class:`REPLWrapper` object."""
+    return REPLWrapper(command, u(">>> "), u("import sys; sys.ps1={0!r}; sys.ps2={1!r}"))
+
+def bash(command="bash"):
+    """Start a bash shell and return a :class:`REPLWrapper` object."""
+    bashrc = os.path.join(os.path.dirname(__file__), 'bashrc.sh')
+    child = pexpect.spawnu(command, ['--rcfile', bashrc], echo=False)
+    return REPLWrapper(child, u'\$', u("PS1='{0}' PS2='{1}' PROMPT_COMMAND=''"),
+                       extra_init_cmd="export PAGER=cat")
diff --git a/third_party/pexpect/pexpect/screen.py b/third_party/pexpect/pexpect/screen.py
new file mode 100644
index 0000000..efe9ee5
--- /dev/null
+++ b/third_party/pexpect/pexpect/screen.py
@@ -0,0 +1,424 @@
+'''This implements a virtual screen. This is used to support ANSI terminal
+emulation. The screen representation and state is implemented in this class.
+Most of the methods are inspired by ANSI screen control codes. The
+:class:`~pexpect.ANSI.ANSI` class extends this class to add parsing of ANSI
+escape codes.
+
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+import codecs
+import copy
+import sys
+
+NUL = 0    # Fill character; ignored on input.
+ENQ = 5    # Transmit answerback message.
+BEL = 7    # Ring the bell.
+BS  = 8    # Move cursor left.
+HT  = 9    # Move cursor to next tab stop.
+LF = 10    # Line feed.
+VT = 11    # Same as LF.
+FF = 12    # Same as LF.
+CR = 13    # Move cursor to left margin or newline.
+SO = 14    # Invoke G1 character set.
+SI = 15    # Invoke G0 character set.
+XON = 17   # Resume transmission.
+XOFF = 19  # Halt transmission.
+CAN = 24   # Cancel escape sequence.
+SUB = 26   # Same as CAN.
+ESC = 27   # Introduce a control sequence.
+DEL = 127  # Fill character; ignored on input.
+SPACE = u' ' # Space or blank character.
+
+PY3 = (sys.version_info[0] >= 3)
+if PY3:
+    unicode = str
+
+def constrain (n, min, max):
+
+    '''This returns a number, n constrained to the min and max bounds. '''
+
+    if n < min:
+        return min
+    if n > max:
+        return max
+    return n
+
+class screen:
+    '''This object maintains the state of a virtual text screen as a
+    rectangluar array. This maintains a virtual cursor position and handles
+    scrolling as characters are added. This supports most of the methods needed
+    by an ANSI text screen. Row and column indexes are 1-based (not zero-based,
+    like arrays).
+
+    Characters are represented internally using unicode. Methods that accept
+    input characters, when passed 'bytes' (which in Python 2 is equivalent to
+    'str'), convert them from the encoding specified in the 'encoding'
+    parameter to the constructor. Methods that return screen contents return
+    unicode strings, with the exception of __str__() under Python 2. Passing
+    ``encoding=None`` limits the API to only accept unicode input, so passing
+    bytes in will raise :exc:`TypeError`.
+    '''
+    def __init__(self, r=24, c=80, encoding='latin-1', encoding_errors='replace'):
+        '''This initializes a blank screen of the given dimensions.'''
+
+        self.rows = r
+        self.cols = c
+        self.encoding = encoding
+        self.encoding_errors = encoding_errors
+        if encoding is not None:
+            self.decoder = codecs.getincrementaldecoder(encoding)(encoding_errors)            
+        else:
+            self.decoder = None
+        self.cur_r = 1
+        self.cur_c = 1
+        self.cur_saved_r = 1
+        self.cur_saved_c = 1
+        self.scroll_row_start = 1
+        self.scroll_row_end = self.rows
+        self.w = [ [SPACE] * self.cols for _ in range(self.rows)]
+
+    def _decode(self, s):
+        '''This converts from the external coding system (as passed to
+        the constructor) to the internal one (unicode). '''
+        if self.decoder is not None:
+            return self.decoder.decode(s)
+        else:
+            raise TypeError("This screen was constructed with encoding=None, "
+                            "so it does not handle bytes.")
+
+    def _unicode(self):
+        '''This returns a printable representation of the screen as a unicode
+        string (which, under Python 3.x, is the same as 'str'). The end of each
+        screen line is terminated by a newline.'''
+
+        return u'\n'.join ([ u''.join(c) for c in self.w ])
+
+    if PY3:
+        __str__ = _unicode
+    else:
+        __unicode__ = _unicode
+
+        def __str__(self):
+            '''This returns a printable representation of the screen. The end of
+            each screen line is terminated by a newline. '''
+            encoding = self.encoding or 'ascii'
+            return self._unicode().encode(encoding, 'replace')
+
+    def dump (self):
+        '''This returns a copy of the screen as a unicode string. This is similar to
+        __str__/__unicode__ except that lines are not terminated with line
+        feeds.'''
+
+        return u''.join ([ u''.join(c) for c in self.w ])
+
+    def pretty (self):
+        '''This returns a copy of the screen as a unicode string with an ASCII
+        text box around the screen border. This is similar to
+        __str__/__unicode__ except that it adds a box.'''
+
+        top_bot = u'+' + u'-'*self.cols + u'+\n'
+        return top_bot + u'\n'.join([u'|'+line+u'|' for line in unicode(self).split(u'\n')]) + u'\n' + top_bot
+
+    def fill (self, ch=SPACE):
+
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)
+
+        self.fill_region (1,1,self.rows,self.cols, ch)
+
+    def fill_region (self, rs,cs, re,ce, ch=SPACE):
+
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)
+
+        rs = constrain (rs, 1, self.rows)
+        re = constrain (re, 1, self.rows)
+        cs = constrain (cs, 1, self.cols)
+        ce = constrain (ce, 1, self.cols)
+        if rs > re:
+            rs, re = re, rs
+        if cs > ce:
+            cs, ce = ce, cs
+        for r in range (rs, re+1):
+            for c in range (cs, ce + 1):
+                self.put_abs (r,c,ch)
+
+    def cr (self):
+        '''This moves the cursor to the beginning (col 1) of the current row.
+        '''
+
+        self.cursor_home (self.cur_r, 1)
+
+    def lf (self):
+        '''This moves the cursor down with scrolling.
+        '''
+
+        old_r = self.cur_r
+        self.cursor_down()
+        if old_r == self.cur_r:
+            self.scroll_up ()
+            self.erase_line()
+
+    def crlf (self):
+        '''This advances the cursor with CRLF properties.
+        The cursor will line wrap and the screen may scroll.
+        '''
+
+        self.cr ()
+        self.lf ()
+
+    def newline (self):
+        '''This is an alias for crlf().
+        '''
+
+        self.crlf()
+
+    def put_abs (self, r, c, ch):
+        '''Screen array starts at 1 index.'''
+
+        r = constrain (r, 1, self.rows)
+        c = constrain (c, 1, self.cols)
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)[0]
+        else:
+            ch = ch[0]
+        self.w[r-1][c-1] = ch
+
+    def put (self, ch):
+        '''This puts a characters at the current cursor position.
+        '''
+
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)
+
+        self.put_abs (self.cur_r, self.cur_c, ch)
+
+    def insert_abs (self, r, c, ch):
+        '''This inserts a character at (r,c). Everything under
+        and to the right is shifted right one character.
+        The last character of the line is lost.
+        '''
+
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)
+
+        r = constrain (r, 1, self.rows)
+        c = constrain (c, 1, self.cols)
+        for ci in range (self.cols, c, -1):
+            self.put_abs (r,ci, self.get_abs(r,ci-1))
+        self.put_abs (r,c,ch)
+
+    def insert (self, ch):
+
+        if isinstance(ch, bytes):
+            ch = self._decode(ch)
+
+        self.insert_abs (self.cur_r, self.cur_c, ch)
+
+    def get_abs (self, r, c):
+
+        r = constrain (r, 1, self.rows)
+        c = constrain (c, 1, self.cols)
+        return self.w[r-1][c-1]
+
+    def get (self):
+
+        self.get_abs (self.cur_r, self.cur_c)
+
+    def get_region (self, rs,cs, re,ce):
+        '''This returns a list of lines representing the region.
+        '''
+
+        rs = constrain (rs, 1, self.rows)
+        re = constrain (re, 1, self.rows)
+        cs = constrain (cs, 1, self.cols)
+        ce = constrain (ce, 1, self.cols)
+        if rs > re:
+            rs, re = re, rs
+        if cs > ce:
+            cs, ce = ce, cs
+        sc = []
+        for r in range (rs, re+1):
+            line = u''
+            for c in range (cs, ce + 1):
+                ch = self.get_abs (r,c)
+                line = line + ch
+            sc.append (line)
+        return sc
+
+    def cursor_constrain (self):
+        '''This keeps the cursor within the screen area.
+        '''
+
+        self.cur_r = constrain (self.cur_r, 1, self.rows)
+        self.cur_c = constrain (self.cur_c, 1, self.cols)
+
+    def cursor_home (self, r=1, c=1): # <ESC>[{ROW};{COLUMN}H
+
+        self.cur_r = r
+        self.cur_c = c
+        self.cursor_constrain ()
+
+    def cursor_back (self,count=1): # <ESC>[{COUNT}D (not confused with down)
+
+        self.cur_c = self.cur_c - count
+        self.cursor_constrain ()
+
+    def cursor_down (self,count=1): # <ESC>[{COUNT}B (not confused with back)
+
+        self.cur_r = self.cur_r + count
+        self.cursor_constrain ()
+
+    def cursor_forward (self,count=1): # <ESC>[{COUNT}C
+
+        self.cur_c = self.cur_c + count
+        self.cursor_constrain ()
+
+    def cursor_up (self,count=1): # <ESC>[{COUNT}A
+
+        self.cur_r = self.cur_r - count
+        self.cursor_constrain ()
+
+    def cursor_up_reverse (self): # <ESC> M   (called RI -- Reverse Index)
+
+        old_r = self.cur_r
+        self.cursor_up()
+        if old_r == self.cur_r:
+            self.scroll_up()
+
+    def cursor_force_position (self, r, c): # <ESC>[{ROW};{COLUMN}f
+        '''Identical to Cursor Home.'''
+
+        self.cursor_home (r, c)
+
+    def cursor_save (self): # <ESC>[s
+        '''Save current cursor position.'''
+
+        self.cursor_save_attrs()
+
+    def cursor_unsave (self): # <ESC>[u
+        '''Restores cursor position after a Save Cursor.'''
+
+        self.cursor_restore_attrs()
+
+    def cursor_save_attrs (self): # <ESC>7
+        '''Save current cursor position.'''
+
+        self.cur_saved_r = self.cur_r
+        self.cur_saved_c = self.cur_c
+
+    def cursor_restore_attrs (self): # <ESC>8
+        '''Restores cursor position after a Save Cursor.'''
+
+        self.cursor_home (self.cur_saved_r, self.cur_saved_c)
+
+    def scroll_constrain (self):
+        '''This keeps the scroll region within the screen region.'''
+
+        if self.scroll_row_start <= 0:
+            self.scroll_row_start = 1
+        if self.scroll_row_end > self.rows:
+            self.scroll_row_end = self.rows
+
+    def scroll_screen (self): # <ESC>[r
+        '''Enable scrolling for entire display.'''
+
+        self.scroll_row_start = 1
+        self.scroll_row_end = self.rows
+
+    def scroll_screen_rows (self, rs, re): # <ESC>[{start};{end}r
+        '''Enable scrolling from row {start} to row {end}.'''
+
+        self.scroll_row_start = rs
+        self.scroll_row_end = re
+        self.scroll_constrain()
+
+    def scroll_down (self): # <ESC>D
+        '''Scroll display down one line.'''
+
+        # Screen is indexed from 1, but arrays are indexed from 0.
+        s = self.scroll_row_start - 1
+        e = self.scroll_row_end - 1
+        self.w[s+1:e+1] = copy.deepcopy(self.w[s:e])
+
+    def scroll_up (self): # <ESC>M
+        '''Scroll display up one line.'''
+
+        # Screen is indexed from 1, but arrays are indexed from 0.
+        s = self.scroll_row_start - 1
+        e = self.scroll_row_end - 1
+        self.w[s:e] = copy.deepcopy(self.w[s+1:e+1])
+
+    def erase_end_of_line (self): # <ESC>[0K -or- <ESC>[K
+        '''Erases from the current cursor position to the end of the current
+        line.'''
+
+        self.fill_region (self.cur_r, self.cur_c, self.cur_r, self.cols)
+
+    def erase_start_of_line (self): # <ESC>[1K
+        '''Erases from the current cursor position to the start of the current
+        line.'''
+
+        self.fill_region (self.cur_r, 1, self.cur_r, self.cur_c)
+
+    def erase_line (self): # <ESC>[2K
+        '''Erases the entire current line.'''
+
+        self.fill_region (self.cur_r, 1, self.cur_r, self.cols)
+
+    def erase_down (self): # <ESC>[0J -or- <ESC>[J
+        '''Erases the screen from the current line down to the bottom of the
+        screen.'''
+
+        self.erase_end_of_line ()
+        self.fill_region (self.cur_r + 1, 1, self.rows, self.cols)
+
+    def erase_up (self): # <ESC>[1J
+        '''Erases the screen from the current line up to the top of the
+        screen.'''
+
+        self.erase_start_of_line ()
+        self.fill_region (self.cur_r-1, 1, 1, self.cols)
+
+    def erase_screen (self): # <ESC>[2J
+        '''Erases the screen with the background color.'''
+
+        self.fill ()
+
+    def set_tab (self): # <ESC>H
+        '''Sets a tab at the current position.'''
+
+        pass
+
+    def clear_tab (self): # <ESC>[g
+        '''Clears tab at the current position.'''
+
+        pass
+
+    def clear_all_tabs (self): # <ESC>[3g
+        '''Clears all tabs.'''
+
+        pass
+
+#        Insert line             Esc [ Pn L
+#        Delete line             Esc [ Pn M
+#        Delete character        Esc [ Pn P
+#        Scrolling region        Esc [ Pn(top);Pn(bot) r
+
diff --git a/third_party/pexpect/pexpect/spawnbase.py b/third_party/pexpect/pexpect/spawnbase.py
new file mode 100644
index 0000000..d79c5c0
--- /dev/null
+++ b/third_party/pexpect/pexpect/spawnbase.py
@@ -0,0 +1,484 @@
+import codecs
+import os
+import sys
+import re
+import errno
+from .exceptions import ExceptionPexpect, EOF, TIMEOUT
+from .expect import Expecter, searcher_string, searcher_re
+
+PY3 = (sys.version_info[0] >= 3)
+
+class SpawnBase(object):
+    """A base class providing the backwards-compatible spawn API for Pexpect.
+
+    This should not be instantiated directly: use :class:`pexpect.spawn` or :class:`pexpect.fdpexpect.fdspawn`."""
+    string_type = bytes
+    if PY3:
+        allowed_string_types = (bytes, str)
+        linesep = os.linesep.encode('ascii')
+        crlf = '\r\n'.encode('ascii')
+
+        @staticmethod
+        def write_to_stdout(b):
+            try:
+                return sys.stdout.buffer.write(b)
+            except AttributeError:
+                # If stdout has been replaced, it may not have .buffer
+                return sys.stdout.write(b.decode('ascii', 'replace'))
+    else:
+        allowed_string_types = (basestring,)  # analysis:ignore
+        linesep = os.linesep
+        crlf = '\r\n'
+        write_to_stdout = sys.stdout.write
+
+    encoding = None
+    pid = None
+    flag_eof = False
+
+    def __init__(self, timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
+        self.stdin = sys.stdin
+        self.stdout = sys.stdout
+        self.stderr = sys.stderr
+
+        self.searcher = None
+        self.ignorecase = False
+        self.before = None
+        self.after = None
+        self.match = None
+        self.match_index = None
+        self.terminated = True
+        self.exitstatus = None
+        self.signalstatus = None
+        # status returned by os.waitpid
+        self.status = None
+        # the child file descriptor is initially closed
+        self.child_fd = -1
+        self.timeout = timeout
+        self.delimiter = EOF
+        self.logfile = logfile
+        # input from child (read_nonblocking)
+        self.logfile_read = None
+        # output to send (send, sendline)
+        self.logfile_send = None
+        # max bytes to read at one time into buffer
+        self.maxread = maxread
+        # This is the read buffer. See maxread.
+        self.buffer = self.string_type()
+        # Data before searchwindowsize point is preserved, but not searched.
+        self.searchwindowsize = searchwindowsize
+        # Delay used before sending data to child. Time in seconds.
+        # Most Linux machines don't like this to be below 0.03 (30 ms).
+        self.delaybeforesend = 0.05
+        # Used by close() to give kernel time to update process status.
+        # Time in seconds.
+        self.delayafterclose = 0.1
+        # Used by terminate() to give kernel time to update process status.
+        # Time in seconds.
+        self.delayafterterminate = 0.1
+        self.softspace = False
+        self.name = '<' + repr(self) + '>'
+        self.closed = True
+
+    def _log(self, s, direction):
+        if self.logfile is not None:
+            self.logfile.write(s)
+            self.logfile.flush()
+        second_log = self.logfile_send if (direction=='send') else self.logfile_read
+        if second_log is not None:
+            second_log.write(s)
+            second_log.flush()
+
+    @staticmethod
+    def _coerce_expect_string(s):
+        if not isinstance(s, bytes):
+            return s.encode('ascii')
+        return s
+
+    @staticmethod
+    def _coerce_send_string(s):
+        if not isinstance(s, bytes):
+            return s.encode('utf-8')
+        return s
+
+    @staticmethod
+    def _coerce_read_string(s):
+        return s
+
+    def read_nonblocking(self, size=1, timeout=None):
+        """This reads data from the file descriptor.
+
+        This is a simple implementation suitable for a regular file. Subclasses using ptys or pipes should override it.
+
+        The timeout parameter is ignored.
+        """
+
+        try:
+            s = os.read(self.child_fd, size)
+        except OSError as err:
+            if err.args[0] == errno.EIO:
+                # Linux-style EOF
+                self.flag_eof = True
+                raise EOF('End Of File (EOF). Exception style platform.')
+            raise
+        if s == b'':
+            # BSD-style EOF
+            self.flag_eof = True
+            raise EOF('End Of File (EOF). Empty string style platform.')
+
+        s = self._coerce_read_string(s)
+        self._log(s, 'read')
+        return s
+
+    def _pattern_type_err(self, pattern):
+        raise TypeError('got {badtype} ({badobj!r}) as pattern, must be one'
+                        ' of: {goodtypes}, pexpect.EOF, pexpect.TIMEOUT'\
+                        .format(badtype=type(pattern),
+                                badobj=pattern,
+                                goodtypes=', '.join([str(ast)\
+                                    for ast in self.allowed_string_types])
+                                )
+                        )
+
+    def compile_pattern_list(self, patterns):
+        '''This compiles a pattern-string or a list of pattern-strings.
+        Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of
+        those. Patterns may also be None which results in an empty list (you
+        might do this if waiting for an EOF or TIMEOUT condition without
+        expecting any pattern).
+
+        This is used by expect() when calling expect_list(). Thus expect() is
+        nothing more than::
+
+             cpl = self.compile_pattern_list(pl)
+             return self.expect_list(cpl, timeout)
+
+        If you are using expect() within a loop it may be more
+        efficient to compile the patterns first and then call expect_list().
+        This avoid calls in a loop to compile_pattern_list()::
+
+             cpl = self.compile_pattern_list(my_pattern)
+             while some_condition:
+                ...
+                i = self.expect_list(cpl, timeout)
+                ...
+        '''
+
+        if patterns is None:
+            return []
+        if not isinstance(patterns, list):
+            patterns = [patterns]
+
+        # Allow dot to match \n
+        compile_flags = re.DOTALL
+        if self.ignorecase:
+            compile_flags = compile_flags | re.IGNORECASE
+        compiled_pattern_list = []
+        for idx, p in enumerate(patterns):
+            if isinstance(p, self.allowed_string_types):
+                p = self._coerce_expect_string(p)
+                compiled_pattern_list.append(re.compile(p, compile_flags))
+            elif p is EOF:
+                compiled_pattern_list.append(EOF)
+            elif p is TIMEOUT:
+                compiled_pattern_list.append(TIMEOUT)
+            elif isinstance(p, type(re.compile(''))):
+                compiled_pattern_list.append(p)
+            else:
+                self._pattern_type_err(p)
+        return compiled_pattern_list
+
+    def expect(self, pattern, timeout=-1, searchwindowsize=-1, async=False):
+        '''This seeks through the stream until a pattern is matched. The
+        pattern is overloaded and may take several types. The pattern can be a
+        StringType, EOF, a compiled re, or a list of any of those types.
+        Strings will be compiled to re types. This returns the index into the
+        pattern list. If the pattern was not a list this returns index 0 on a
+        successful match. This may raise exceptions for EOF or TIMEOUT. To
+        avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern
+        list. That will cause expect to match an EOF or TIMEOUT condition
+        instead of raising an exception.
+
+        If you pass a list of patterns and more than one matches, the first
+        match in the stream is chosen. If more than one pattern matches at that
+        point, the leftmost in the pattern list is chosen. For example::
+
+            # the input is 'foobar'
+            index = p.expect(['bar', 'foo', 'foobar'])
+            # returns 1('foo') even though 'foobar' is a "better" match
+
+        Please note, however, that buffering can affect this behavior, since
+        input arrives in unpredictable chunks. For example::
+
+            # the input is 'foobar'
+            index = p.expect(['foobar', 'foo'])
+            # returns 0('foobar') if all input is available at once,
+            # but returs 1('foo') if parts of the final 'bar' arrive late
+
+        After a match is found the instance attributes 'before', 'after' and
+        'match' will be set. You can see all the data read before the match in
+        'before'. You can see the data that was matched in 'after'. The
+        re.MatchObject used in the re match will be in 'match'. If an error
+        occurred then 'before' will be set to all the data read so far and
+        'after' and 'match' will be None.
+
+        If timeout is -1 then timeout will be set to the self.timeout value.
+
+        A list entry may be EOF or TIMEOUT instead of a string. This will
+        catch these exceptions and return the index of the list entry instead
+        of raising the exception. The attribute 'after' will be set to the
+        exception type. The attribute 'match' will be None. This allows you to
+        write code like this::
+
+                index = p.expect(['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
+                if index == 0:
+                    do_something()
+                elif index == 1:
+                    do_something_else()
+                elif index == 2:
+                    do_some_other_thing()
+                elif index == 3:
+                    do_something_completely_different()
+
+        instead of code like this::
+
+                try:
+                    index = p.expect(['good', 'bad'])
+                    if index == 0:
+                        do_something()
+                    elif index == 1:
+                        do_something_else()
+                except EOF:
+                    do_some_other_thing()
+                except TIMEOUT:
+                    do_something_completely_different()
+
+        These two forms are equivalent. It all depends on what you want. You
+        can also just expect the EOF if you are waiting for all output of a
+        child to finish. For example::
+
+                p = pexpect.spawn('/bin/ls')
+                p.expect(pexpect.EOF)
+                print p.before
+
+        If you are trying to optimize for speed then see expect_list().
+
+        On Python 3.4, or Python 3.3 with asyncio installed, passing
+        ``async=True``  will make this return an :mod:`asyncio` coroutine,
+        which you can yield from to get the same result that this method would
+        normally give directly. So, inside a coroutine, you can replace this code::
+
+            index = p.expect(patterns)
+
+        With this non-blocking form::
+
+            index = yield from p.expect(patterns, async=True)
+        '''
+
+        compiled_pattern_list = self.compile_pattern_list(pattern)
+        return self.expect_list(compiled_pattern_list,
+                timeout, searchwindowsize, async)
+
+    def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1,
+                    async=False):
+        '''This takes a list of compiled regular expressions and returns the
+        index into the pattern_list that matched the child output. The list may
+        also contain EOF or TIMEOUT(which are not compiled regular
+        expressions). This method is similar to the expect() method except that
+        expect_list() does not recompile the pattern list on every call. This
+        may help if you are trying to optimize for speed, otherwise just use
+        the expect() method.  This is called by expect(). If timeout==-1 then
+        the self.timeout value is used. If searchwindowsize==-1 then the
+        self.searchwindowsize value is used.
+
+        Like :meth:`expect`, passing ``async=True`` will make this return an
+        asyncio coroutine.
+        '''
+        if timeout == -1:
+            timeout = self.timeout
+
+        exp = Expecter(self, searcher_re(pattern_list), searchwindowsize)
+        if async:
+            from .async import expect_async
+            return expect_async(exp, timeout)
+        else:
+            return exp.expect_loop(timeout)
+
+    def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1,
+                     async=False):
+
+        '''This is similar to expect(), but uses plain string matching instead
+        of compiled regular expressions in 'pattern_list'. The 'pattern_list'
+        may be a string; a list or other sequence of strings; or TIMEOUT and
+        EOF.
+
+        This call might be faster than expect() for two reasons: string
+        searching is faster than RE matching and it is possible to limit the
+        search to just the end of the input buffer.
+
+        This method is also useful when you don't want to have to worry about
+        escaping regular expression characters that you want to match.
+
+        Like :meth:`expect`, passing ``async=True`` will make this return an
+        asyncio coroutine.
+        '''
+        if timeout == -1:
+            timeout = self.timeout
+
+        if (isinstance(pattern_list, self.allowed_string_types) or
+                pattern_list in (TIMEOUT, EOF)):
+            pattern_list = [pattern_list]
+
+        def prepare_pattern(pattern):
+            if pattern in (TIMEOUT, EOF):
+                return pattern
+            if isinstance(pattern, self.allowed_string_types):
+                return self._coerce_expect_string(pattern)
+            self._pattern_type_err(pattern)
+
+        try:
+            pattern_list = iter(pattern_list)
+        except TypeError:
+            self._pattern_type_err(pattern_list)
+        pattern_list = [prepare_pattern(p) for p in pattern_list]
+
+        exp = Expecter(self, searcher_string(pattern_list), searchwindowsize)
+        if async:
+            from .async import expect_async
+            return expect_async(exp, timeout)
+        else:
+            return exp.expect_loop(timeout)
+
+    def expect_loop(self, searcher, timeout=-1, searchwindowsize=-1):
+        '''This is the common loop used inside expect. The 'searcher' should be
+        an instance of searcher_re or searcher_string, which describes how and
+        what to search for in the input.
+
+        See expect() for other arguments, return value and exceptions. '''
+
+        exp = Expecter(self, searcher, searchwindowsize)
+        return exp.expect_loop(timeout)
+
+    def read(self, size=-1):
+        '''This reads at most "size" bytes from the file (less if the read hits
+        EOF before obtaining size bytes). If the size argument is negative or
+        omitted, read all data until EOF is reached. The bytes are returned as
+        a string object. An empty string is returned when EOF is encountered
+        immediately. '''
+
+        if size == 0:
+            return self.string_type()
+        if size < 0:
+            # delimiter default is EOF
+            self.expect(self.delimiter)
+            return self.before
+
+        # I could have done this more directly by not using expect(), but
+        # I deliberately decided to couple read() to expect() so that
+        # I would catch any bugs early and ensure consistant behavior.
+        # It's a little less efficient, but there is less for me to
+        # worry about if I have to later modify read() or expect().
+        # Note, it's OK if size==-1 in the regex. That just means it
+        # will never match anything in which case we stop only on EOF.
+        cre = re.compile(self._coerce_expect_string('.{%d}' % size), re.DOTALL)
+        # delimiter default is EOF
+        index = self.expect([cre, self.delimiter])
+        if index == 0:
+            ### FIXME self.before should be ''. Should I assert this?
+            return self.after
+        return self.before
+
+    def readline(self, size=-1):
+        '''This reads and returns one entire line. The newline at the end of
+        line is returned as part of the string, unless the file ends without a
+        newline. An empty string is returned if EOF is encountered immediately.
+        This looks for a newline as a CR/LF pair (\\r\\n) even on UNIX because
+        this is what the pseudotty device returns. So contrary to what you may
+        expect you will receive newlines as \\r\\n.
+
+        If the size argument is 0 then an empty string is returned. In all
+        other cases the size argument is ignored, which is not standard
+        behavior for a file-like object. '''
+
+        if size == 0:
+            return self.string_type()
+        # delimiter default is EOF
+        index = self.expect([self.crlf, self.delimiter])
+        if index == 0:
+            return self.before + self.crlf
+        else:
+            return self.before
+
+    def __iter__(self):
+        '''This is to support iterators over a file-like object.
+        '''
+        return iter(self.readline, self.string_type())
+
+    def readlines(self, sizehint=-1):
+        '''This reads until EOF using readline() and returns a list containing
+        the lines thus read. The optional 'sizehint' argument is ignored.
+        Remember, because this reads until EOF that means the child
+        process should have closed its stdout. If you run this method on
+        a child that is still running with its stdout open then this
+        method will block until it timesout.'''
+
+        lines = []
+        while True:
+            line = self.readline()
+            if not line:
+                break
+            lines.append(line)
+        return lines
+
+    def fileno(self):
+        '''Expose file descriptor for a file-like interface
+        '''
+        return self.child_fd
+
+    def flush(self):
+        '''This does nothing. It is here to support the interface for a
+        File-like object. '''
+        pass
+
+    def isatty(self):
+        """Overridden in subclass using tty"""
+        return False
+
+    # For 'with spawn(...) as child:'
+    def __enter__(self):
+        return self
+    
+    def __exit__(self, etype, evalue, tb):
+        # We rely on subclasses to implement close(). If they don't, it's not
+        # clear what a context manager should do.
+        self.close()
+
+class SpawnBaseUnicode(SpawnBase):
+    if PY3:
+        string_type = str
+        allowed_string_types = (str, )
+        linesep = os.linesep
+        crlf = '\r\n'
+    else:
+        string_type = unicode
+        allowed_string_types = (unicode, )
+        linesep = os.linesep.decode('ascii')
+        crlf = '\r\n'.decode('ascii')
+    # This can handle unicode in both Python 2 and 3
+    write_to_stdout = sys.stdout.write
+
+    def __init__(self, *args, **kwargs):
+        self.encoding = kwargs.pop('encoding', 'utf-8')
+        self.errors = kwargs.pop('errors', 'strict')
+        self._decoder = codecs.getincrementaldecoder(self.encoding)(errors=self.errors)
+        super(SpawnBaseUnicode, self).__init__(*args, **kwargs)
+
+    @staticmethod
+    def _coerce_expect_string(s):
+        return s
+
+    @staticmethod
+    def _coerce_send_string(s):
+        return s
+
+    def _coerce_read_string(self, s):
+        return self._decoder.decode(s, final=False)
\ No newline at end of file
diff --git a/third_party/pexpect/pexpect/utils.py b/third_party/pexpect/pexpect/utils.py
new file mode 100644
index 0000000..737f0ed
--- /dev/null
+++ b/third_party/pexpect/pexpect/utils.py
@@ -0,0 +1,112 @@
+import os
+import sys
+import stat
+
+
+def is_executable_file(path):
+    """Checks that path is an executable regular file, or a symlink towards one.
+
+    This is roughly ``os.path isfile(path) and os.access(path, os.X_OK)``.
+    """
+    # follow symlinks,
+    fpath = os.path.realpath(path)
+
+    if not os.path.isfile(fpath):
+        # non-files (directories, fifo, etc.)
+        return False
+
+    mode = os.stat(fpath).st_mode
+
+    if (sys.platform.startswith('sunos')
+            and os.getuid() == 0):
+        # When root on Solaris, os.X_OK is True for *all* files, irregardless
+        # of their executability -- instead, any permission bit of any user,
+        # group, or other is fine enough.
+        #
+        # (This may be true for other "Unix98" OS's such as HP-UX and AIX)
+        return bool(mode & (stat.S_IXUSR |
+                            stat.S_IXGRP |
+                            stat.S_IXOTH))
+
+    return os.access(fpath, os.X_OK)
+
+
+def which(filename):
+    '''This takes a given filename; tries to find it in the environment path;
+    then checks if it is executable. This returns the full path to the filename
+    if found and executable. Otherwise this returns None.'''
+
+    # Special case where filename contains an explicit path.
+    if os.path.dirname(filename) != '' and is_executable_file(filename):
+        return filename
+    if 'PATH' not in os.environ or os.environ['PATH'] == '':
+        p = os.defpath
+    else:
+        p = os.environ['PATH']
+    pathlist = p.split(os.pathsep)
+    for path in pathlist:
+        ff = os.path.join(path, filename)
+        if is_executable_file(ff):
+            return ff
+    return None
+
+
+def split_command_line(command_line):
+
+    '''This splits a command line into a list of arguments. It splits arguments
+    on spaces, but handles embedded quotes, doublequotes, and escaped
+    characters. It's impossible to do this with a regular expression, so I
+    wrote a little state machine to parse the command line. '''
+
+    arg_list = []
+    arg = ''
+
+    # Constants to name the states we can be in.
+    state_basic = 0
+    state_esc = 1
+    state_singlequote = 2
+    state_doublequote = 3
+    # The state when consuming whitespace between commands.
+    state_whitespace = 4
+    state = state_basic
+
+    for c in command_line:
+        if state == state_basic or state == state_whitespace:
+            if c == '\\':
+                # Escape the next character
+                state = state_esc
+            elif c == r"'":
+                # Handle single quote
+                state = state_singlequote
+            elif c == r'"':
+                # Handle double quote
+                state = state_doublequote
+            elif c.isspace():
+                # Add arg to arg_list if we aren't in the middle of whitespace.
+                if state == state_whitespace:
+                    # Do nothing.
+                    None
+                else:
+                    arg_list.append(arg)
+                    arg = ''
+                    state = state_whitespace
+            else:
+                arg = arg + c
+                state = state_basic
+        elif state == state_esc:
+            arg = arg + c
+            state = state_basic
+        elif state == state_singlequote:
+            if c == r"'":
+                state = state_basic
+            else:
+                arg = arg + c
+        elif state == state_doublequote:
+            if c == r'"':
+                state = state_basic
+            else:
+                arg = arg + c
+
+    if arg != '':
+        arg_list.append(arg)
+    return arg_list
diff --git a/third_party/pexpect/pxssh.py b/third_party/pexpect/pxssh.py
new file mode 100644
index 0000000..1849769
--- /dev/null
+++ b/third_party/pexpect/pxssh.py
@@ -0,0 +1,7 @@
+import warnings
+
+warnings.warn("This module has been moved to pexpect.pxssh, please update imports.",
+                ImportWarning)
+del warnings
+
+from pexpect.pxssh import *  # analysis:ignore
\ No newline at end of file
diff --git a/third_party/pexpect/screen.py b/third_party/pexpect/screen.py
new file mode 100644
index 0000000..0358c41
--- /dev/null
+++ b/third_party/pexpect/screen.py
@@ -0,0 +1,7 @@
+import warnings
+
+warnings.warn("This module has been moved to pexpect.screen, please update imports.",
+                ImportWarning)
+del warnings
+
+from pexpect.screen import *  # analysis:ignore
\ No newline at end of file
diff --git a/third_party/pexpect/setup.cfg b/third_party/pexpect/setup.cfg
new file mode 100644
index 0000000..ae62686
--- /dev/null
+++ b/third_party/pexpect/setup.cfg
@@ -0,0 +1,2 @@
+[pytest]
+norecursedirs = .git
diff --git a/third_party/pexpect/setup.py b/third_party/pexpect/setup.py
new file mode 100644
index 0000000..126749a
--- /dev/null
+++ b/third_party/pexpect/setup.py
@@ -0,0 +1,65 @@
+from distutils.core import setup
+import os
+import re
+
+with open(os.path.join(os.path.dirname(__file__), 'pexpect', '__init__.py'), 'r') as f:
+    for line in f:
+        version_match = re.search(r"__version__ = ['\"]([^'\"]*)['\"]", line)
+        if version_match:
+            version = version_match.group(1)
+            break
+    else:
+        raise Exception("couldn't find version number")
+
+long_description = """
+Pexpect is a pure Python module for spawning child applications; controlling
+them; and responding to expected patterns in their output. Pexpect works like
+Don Libes' Expect. Pexpect allows your script to spawn a child application and
+control it as if a human were typing commands.
+
+Pexpect can be used for automating interactive applications such as ssh, ftp,
+passwd, telnet, etc. It can be used to a automate setup scripts for duplicating
+software package installations on different servers. It can be used for
+automated software testing. Pexpect is in the spirit of Don Libes' Expect, but
+Pexpect is pure Python. Unlike other Expect-like modules for Python, Pexpect
+does not require TCL or Expect nor does it require C extensions to be compiled.
+It should work on any platform that supports the standard Python pty module.
+The Pexpect interface was designed to be easy to use.
+"""
+
+setup (name='pexpect',
+    version=version,
+    py_modules=['pxssh', 'fdpexpect', 'FSM', 'screen', 'ANSI'],
+    packages=['pexpect'],
+    description='Pexpect allows easy control of interactive console applications.',
+    long_description=long_description,
+    author='Noah Spurrier; Thomas Kluyver; Jeff Quast',
+    author_email='noah at noah.org; thomas at kluyver.me.uk; contact at jeffquast.com',
+    url='http://pexpect.readthedocs.org/',
+    license='ISC license',
+    platforms='UNIX',
+    classifiers = [
+        'Development Status :: 5 - Production/Stable',
+        'Environment :: Console',
+        'Intended Audience :: Developers',
+        'Intended Audience :: System Administrators',
+        'License :: OSI Approved :: ISC License (ISCL)',
+        'Operating System :: POSIX',
+        'Operating System :: MacOS :: MacOS X',
+        'Programming Language :: Python',
+        'Programming Language :: Python :: 2.6',
+        'Programming Language :: Python :: 2.7',
+        'Programming Language :: Python :: 3',
+        'Topic :: Software Development',
+        'Topic :: Software Development :: Libraries :: Python Modules',
+        'Topic :: Software Development :: Quality Assurance',
+        'Topic :: Software Development :: Testing',
+        'Topic :: System',
+        'Topic :: System :: Archiving :: Packaging',
+        'Topic :: System :: Installation/Setup',
+        'Topic :: System :: Shells',
+        'Topic :: System :: Software Distribution',
+        'Topic :: Terminals',
+    ],
+    install_requires=['ptyprocess'],
+)
diff --git a/third_party/pexpect/tests/PexpectTestCase.py b/third_party/pexpect/tests/PexpectTestCase.py
new file mode 100644
index 0000000..307437e
--- /dev/null
+++ b/third_party/pexpect/tests/PexpectTestCase.py
@@ -0,0 +1,108 @@
+
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+from __future__ import print_function
+
+import contextlib
+import unittest
+import signal
+import sys
+import os
+
+
+class PexpectTestCase(unittest.TestCase):
+    def setUp(self):
+        self.PYTHONBIN = sys.executable
+        self.original_path = os.getcwd()
+        tests_dir = os.path.dirname(__file__)
+        self.project_dir = project_dir = os.path.dirname(tests_dir)
+
+        # all tests are executed in this folder; there are many auxiliary
+        # programs in this folder executed by spawn().
+        os.chdir(tests_dir)
+
+        # If the pexpect raises an exception after fork(), but before
+        # exec(), our test runner *also* forks.  We prevent this by
+        # storing our pid and asserting equality on tearDown.
+        self.pid = os.getpid()
+
+        coverage_rc = os.path.join(project_dir, '.coveragerc')
+        os.environ['COVERAGE_PROCESS_START'] = coverage_rc
+        os.environ['COVERAGE_FILE'] = os.path.join(project_dir, '.coverage')
+        print('\n', self.id(), end=' ')
+        sys.stdout.flush()
+
+        # some build agents will ignore SIGHUP and SIGINT, which python
+        # inherits.  This causes some of the tests related to terminate()
+        # to fail.  We set them to the default handlers that they should
+        # be, and restore them back to their SIG_IGN value on tearDown.
+        #
+        # I'm not entirely convinced they need to be restored, only our
+        # test runner is affected.
+        self.restore_ignored_signals = [
+            value for value in (signal.SIGHUP, signal.SIGINT,)
+            if signal.getsignal(value) == signal.SIG_IGN]
+        if signal.SIGHUP in self.restore_ignored_signals:
+            # sighup should be set to default handler
+            signal.signal(signal.SIGHUP, signal.SIG_DFL)
+        if signal.SIGINT in self.restore_ignored_signals:
+            # SIGINT should be set to signal.default_int_handler
+            signal.signal(signal.SIGINT, signal.default_int_handler)
+        unittest.TestCase.setUp(self)
+
+    def tearDown(self):
+        # restore original working folder
+        os.chdir(self.original_path)
+
+        if self.pid != os.getpid():
+            # The build server pattern-matches phrase 'Test runner has forked!'
+            print("Test runner has forked! This means a child process raised "
+                  "an exception before exec() in a test case, the error is "
+                  "more than likely found above this line in stderr.",
+                  file=sys.stderr)
+            exit(1)
+
+        # restore signal handlers
+        for signal_value in self.restore_ignored_signals:
+            signal.signal(signal_value, signal.SIG_IGN)
+
+    if sys.version_info < (2, 7):
+        # We want to use these methods, which are new/improved in 2.7, but
+        # we are still supporting 2.6 for the moment. This section can be
+        # removed when we drop Python 2.6 support.
+        @contextlib.contextmanager
+        def assertRaises(self, excClass):
+            try:
+                yield
+            except Exception as e:
+                assert isinstance(e, excClass)
+            else:
+                raise AssertionError("%s was not raised" % excClass)
+
+        @contextlib.contextmanager
+        def assertRaisesRegexp(self, excClass, pattern):
+            import re
+            try:
+                yield
+            except Exception as e:
+                assert isinstance(e, excClass)
+                assert re.match(pattern, str(e))
+            else:
+                raise AssertionError("%s was not raised" % excClass)
diff --git a/third_party/pexpect/tests/README b/third_party/pexpect/tests/README
new file mode 100644
index 0000000..ef5b613
--- /dev/null
+++ b/third_party/pexpect/tests/README
@@ -0,0 +1,8 @@
+
+The best way to run these tests is from the directory above this one. Run:
+
+    py.test
+
+To run a specific test file:
+
+    py.test tests/test_constructor.py
diff --git a/third_party/pexpect/tests/TESTDATA.txt b/third_party/pexpect/tests/TESTDATA.txt
new file mode 100644
index 0000000..adb9c01
--- /dev/null
+++ b/third_party/pexpect/tests/TESTDATA.txt
@@ -0,0 +1,8 @@
+This is test data.
+    One
+    2
+    THREE
+    IV
+    .....
+    110
+This is the end of test data: END
diff --git a/third_party/pexpect/tests/__init__.py b/third_party/pexpect/tests/__init__.py
new file mode 100755
index 0000000..f717d82
--- /dev/null
+++ b/third_party/pexpect/tests/__init__.py
@@ -0,0 +1,25 @@
+
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+# __init__.py
+# The mere presence of this file makes the dir a package.
+pass
+
diff --git a/third_party/pexpect/tests/adhoc.py b/third_party/pexpect/tests/adhoc.py
new file mode 100755
index 0000000..0813d34
--- /dev/null
+++ b/third_party/pexpect/tests/adhoc.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+from  __future__ import print_function
+
+import pexpect
+import time
+
+p = pexpect.spawn ('./a.out')
+print(p.exitstatus)
+p.expect (pexpect.EOF)
+print(p.before)
+time.sleep(1)
+print('exitstatus:', p.exitstatus)
+print('isalive',p.isalive())
+print('exitstatus',p.exitstatus)
+print('isalive',p.isalive())
+print('exitstatus',p.exitstatus)
+
diff --git a/third_party/pexpect/tests/alarm_die.py b/third_party/pexpect/tests/alarm_die.py
new file mode 100644
index 0000000..a1519ab
--- /dev/null
+++ b/third_party/pexpect/tests/alarm_die.py
@@ -0,0 +1,5 @@
+import signal, time
+
+signal.alarm(1) # Schedule SIGALRM in 1s
+
+time.sleep(6)
\ No newline at end of file
diff --git a/third_party/pexpect/tests/bambi.vt b/third_party/pexpect/tests/bambi.vt
new file mode 100644
index 0000000..853f099
--- /dev/null
+++ b/third_party/pexpect/tests/bambi.vt
@@ -0,0 +1,417 @@
+[?25l
+
+
+                         The
+                         The Adventures
+                         The Adventures of
+                         The Adventures of BAMBI
+                                                            
+                     Number
+                     Number One :
+                     Number One : BAMBI
+                     Number One : BAMBI versus
+                     Number One : BAMBI versus GODZILLA
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+                                                            
+
+ ,| *./\|(/|/
+   /| |/`|',/| \|/
+ \/_,| /| /, /<|\\/\|/|\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /   .:::. .  /
+  \___________/
+    |\      |\
+    | >     | \
+   / /      |  \
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /   .:::. .  /
+  \___________/
+    \|      |\
+     \      | \
+    / \    /  |
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /   .:::. .  /
+  \___________/
+    \\      \|
+     \\      \ 
+     | \     |\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /   .:::. .  /
+  \___________/
+    \\      \|
+     >\      \
+    / |     /|
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    |\      |\
+    | >     | \
+   / /      |  \
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \|      |\
+     \      | \
+    / \    /  |
+          
+         
+--,_^_   
+.    .\  
+__--___o
+--_    
+.  \    
+__  \/|  
+|\(  .|
+| \\ /
+  | "
+--_    
+.  \    
+__  \/|  
+|\(  .|
+| \\ /
+  | "
+--_    
+.  \    
+__  \/|  
+|\(  .|
+| \\ /
+  | "
+--_    
+.  \    
+__  \/|  
+|\(  .|
+| \\ /
+  | "
+--,_^_  
+.    .\  
+__--___o
+|\        
+| \      
+  |      
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /     
+  \___________/     
+    \\      \|
+     \\      \ 
+     | \     |\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     >\      \
+    / |     /|
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    |\      |\
+    | >     | \
+   / /      |  \
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \|      |\
+     \      | \
+    / \    /  |
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     \\      \ 
+     | \     |\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     >\      \
+    / |     /|
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    |\      |\
+    | >     | \
+   / /      |  \
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \|      |\
+     \      | \
+    / \    /  |
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     \\      \ 
+     | \     |\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     >\      \
+    / |     /|
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    |\      |\
+    | >     | \
+   / /      |  \
+        
+         
+--,_^_   
+.    .\  
+__--___o
+--_     
+.  \     
+__  \/|  
+|\(  .| 
+| \\ /
+|  \"
+--_      
+.  \     
+__  \/|   
+|\(  .|
+| \\ /
+|  \"
+--,_^_
+.    .\ 
+__--___o
+|\       
+| \      
+|  \     
+--,_^_
+.    .\ 
+__--___o
+|\       
+| \      
+|  \     
+--_      
+.  \     
+__  \/|   
+|\(  .|
+| \\ /
+|  \"
+--_      
+.  \     
+__  \/|   
+|\(  .|
+| \\ /
+|  \"
+--_      
+.  \     
+__  \/|   
+|\(  .|
+| \\ /
+|  \"
+--,_^_
+.    .\ 
+__--___o
+|\      
+| \     
+|  \    
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /    
+  \___________/     
+    \|      |\
+     \      | \
+    / \    /  |
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     \\      \ 
+     | \     |\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     >\      \
+    / |     /|
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    |\      |\
+    | >     | \
+   / /      |  \
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \|      |\
+     \      | \
+    / \    /  |
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     \\      \ 
+     | \     |\
+              _^_
+ o           /  .\
+  \,--------;   __o
+  /  .:::.. .  /
+  \___________/
+    \\      \|
+     >\      \
+    / |     /|
+                     
+ o                  
+  \,----------,_^_   
+  /  .:::.. .    .\  
+  \___________--___o
+    \\      \|
+     >\      \
+    / |     /|
+                     
+ o          
+  \,----------_    
+  /  .:::.. .  \    
+  \___________  \/|  
+    \\      \|(  .|
+     >\      \ \ /
+    / |     /|  "
+
+`'     `.__________________________________/M
+( /   (/M
+(-.___)M
+/     \/)M
+_______.--'\M
+,____________/\M
+(           /   )M
+(            )  /M
+\           \    )M
+(                 )M
+(    (            /M
+\    (             )M
+(     \             )M
+(    (              /M
+
+\    (        )      )M
+
+(     (        /      )M
+
+(     \         )     /M
+
+\    (          )      )M
+
+(                )      )M
+
+(                )      /M
+
+\                /       )M
+
+(                  )      )
+  /  >      | \
+                             
+                             
+          T          T          T
+          T H          T H          T H
+          T H E          T H E          T H E           T H E 
+          T H E   E          T H E   E          T H E   E
+          T H E   E N          T H E   E N          T H E   E N
+          T H E   E N D          T H E   E N D
+         ===============
+ 
+[?25h
diff --git a/third_party/pexpect/tests/depricated_test_filedescriptor.py b/third_party/pexpect/tests/depricated_test_filedescriptor.py
new file mode 100755
index 0000000..6b0ef3e
--- /dev/null
+++ b/third_party/pexpect/tests/depricated_test_filedescriptor.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+import PexpectTestCase
+import os
+
+class ExpectTestCase(PexpectTestCase.PexpectTestCase):
+    def setUp(self):
+        print(self.id())
+        PexpectTestCase.PexpectTestCase.setUp(self)
+
+    def test_fd (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = pexpect.spawn (fd)
+        s.expect ('This is the end of test data:')
+        s.expect (pexpect.EOF)
+        assert s.before == ' END\n'
+
+    def test_maxread (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = pexpect.spawn (fd)
+        s.maxread = 100
+        s.expect('2')
+        s.expect ('This is the end of test data:')
+        s.expect (pexpect.EOF)
+        assert s.before == ' END\n'
+
+    def test_fd_isalive (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = pexpect.spawn (fd)
+        assert s.isalive()
+        os.close (fd)
+        assert not s.isalive()
+
+    def test_fd_isatty (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = pexpect.spawn (fd)
+        assert not s.isatty()
+        os.close(fd)
+
+###    def test_close_does_not_close_fd (self):
+###        '''Calling close() on a pexpect.spawn object should not
+###                close the underlying file descriptor.
+###        '''
+###        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+###        s = pexpect.spawn (fd)
+###        try:
+###            s.close()
+###            self.fail('Expected an Exception.')
+###        except pexpect.ExceptionPexpect, e:
+###            pass
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(ExpectTestCase, 'test')
+
+#fout = open('delete_me_1','wb')
+#fout.write(the_old_way)
+#fout.close
+#fout = open('delete_me_2', 'wb')
+#fout.write(the_new_way)
+#fout.close
diff --git a/third_party/pexpect/tests/echo_w_prompt.py b/third_party/pexpect/tests/echo_w_prompt.py
new file mode 100644
index 0000000..3c80553
--- /dev/null
+++ b/third_party/pexpect/tests/echo_w_prompt.py
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+from __future__ import print_function
+
+try:
+    raw_input
+except NameError:
+    raw_input = input
+
+while True:
+    try:
+        a = raw_input('<in >')
+    except EOFError:
+        print('<eof>')
+        break
+    print('<out>', a, sep='')
diff --git a/third_party/pexpect/tests/echo_wait.py b/third_party/pexpect/tests/echo_wait.py
new file mode 100755
index 0000000..e152059
--- /dev/null
+++ b/third_party/pexpect/tests/echo_wait.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import time
+import termios
+import sys
+
+# a dumb PAM will print the password prompt first then set ECHO
+# False. What it should do it set ECHO False first then print the
+# prompt. Otherwise, if we see the password prompt and type out
+# password real fast before it turns off ECHO then some or all of
+# our password might be visibly echod back to us. Sounds unlikely?
+# It happens.
+
+print("fake password:")
+sys.stdout.flush()
+time.sleep(3)
+attr = termios.tcgetattr(sys.stdout)
+attr[3] = attr[3] & ~termios.ECHO
+termios.tcsetattr(sys.stdout, termios.TCSANOW, attr)
+time.sleep(12)
+attr[3] = attr[3] | termios.ECHO
+termios.tcsetattr(sys.stdout, termios.TCSANOW, attr)
+time.sleep(2)
diff --git a/third_party/pexpect/tests/exit1.py b/third_party/pexpect/tests/exit1.py
new file mode 100755
index 0000000..587b8ad
--- /dev/null
+++ b/third_party/pexpect/tests/exit1.py
@@ -0,0 +1,24 @@
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import os, sys
+
+print("Hello")
+sys.stdout.flush()
+os._exit(1)
diff --git a/third_party/pexpect/tests/exit667.c b/third_party/pexpect/tests/exit667.c
new file mode 100644
index 0000000..962b9fd
--- /dev/null
+++ b/third_party/pexpect/tests/exit667.c
@@ -0,0 +1,26 @@
+/*
+    PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+*/
+
+#include <stdio.h>
+int main ()
+{
+    printf ("Hello world!\n");
+    exit(7);
+}
diff --git a/third_party/pexpect/tests/fakessh/ssh b/third_party/pexpect/tests/fakessh/ssh
new file mode 100755
index 0000000..28eedc4
--- /dev/null
+++ b/third_party/pexpect/tests/fakessh/ssh
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+from __future__ import print_function
+
+import getpass
+import sys
+PY3 = (sys.version_info[0] >= 3)
+if not PY3:
+    input = raw_input
+
+print("Mock SSH client for tests. Do not enter real security info.")
+
+pw = getpass.getpass('password:')
+if pw != 's3cret':
+    print('Permission denied!')
+    sys.exit(1)
+
+prompt = "$"
+while True:
+    cmd = input(prompt)
+    if cmd.startswith('PS1='):
+        prompt = eval(cmd[4:]).replace('\$', '$')
+    elif cmd == 'ping':
+        print('pong')
+    elif cmd.startswith('ls'):
+        print('file1.py', 'file2.html', sep='\t')
+    elif cmd == 'echo $?':
+        print(0)
+    elif cmd in ('exit', 'logout'):
+        break
\ No newline at end of file
diff --git a/third_party/pexpect/tests/getch.py b/third_party/pexpect/tests/getch.py
new file mode 100755
index 0000000..41e3224
--- /dev/null
+++ b/third_party/pexpect/tests/getch.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import sys, tty, termios
+
+if hasattr(sys.stdin, 'buffer'):
+    # Python 3: we want to read raw bytes
+    stdin = sys.stdin.buffer
+else:
+    stdin = sys.stdin
+
+def main():
+    print('READY')
+    while True:
+        try:
+            val = ord(stdin.read(1))
+        except KeyboardInterrupt:
+            val = 3
+        sys.stdout.write('%d<STOP>\r\n' % (val,))
+        if val == 0:
+            # StopIteration equivalent is ctrl+' ' (\x00, NUL)
+            break
+
+if __name__ == '__main__':
+    fd = sys.stdin.fileno()
+    old_settings = termios.tcgetattr(fd)
+    try:
+        tty.setraw(sys.stdin.fileno())
+        main()
+    finally:
+        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
diff --git a/third_party/pexpect/tests/globe.vt b/third_party/pexpect/tests/globe.vt
new file mode 100644
index 0000000..85dc78d
--- /dev/null
+++ b/third_party/pexpect/tests/globe.vt
@@ -0,0 +1,690 @@
+              _-o#&&*''''?d:>b\_
+          _o/"`''  '',, dMF9MMMMMHo_
+       .o&#'        `"MbHMMMMMMMMMMMHo.
+     .o"" '         vodM*$&&HMMMMMMMMMM?.
+    ,'              $M&ood,~'`(&##MMMMMMH\
+   /               ,MMMMMMM#b?#bobMMMMHMMML
+  &              ?MMMMMMMMMMMMMMMMM7MMM$R*Hk
+ ?$.            :MMMMMMMMMMMMMMMMMMM/HMMM|`*L
+|               |MMMMMMMMMMMMMMMMMMMMbMH'   T,
+$H#:            `*MMMMMMMMMMMMMMMMMMMMb#}'  `?
+]MMH#             ""*""""*#MMMMMMMMMMMMM'    -
+MMMMMb_                   |MMMMMMMMMMMP'     :
+HMMMMMMMHo                 `MMMMMMMMMT       .
+?MMMMMMMMP                  9MMMMMMMM}       -
+-?MMMMMMM                  |MMMMMMMMM?,d-    '
+ :|MMMMMM-                 `MMMMMMMT .M|.   :
+  .9MMM[                    &MMMMM*' `'    .
+   :9MMk                    `MMM#"        -
+     &M}                     `          .-
+      `&.                             .
+        `~,   .                     ./
+            . _                  .-
+              '`--._,dd###pp=""'
+              _v->#H#P? "':o<>\_
+          .,dP` `''  "'-o.+H6&MMMHo_
+        oHMH9'         `?&bHMHMMMMMMHo.
+      oMP"' '           ooMP*#&HMMMMMMM?.
+    ,M*          -     `*MSdob//`^&##MMMH\
+   d*'                .,MMMMMMH#o>#ooMMMMMb
+  HM-                :HMMMMMMMMMMMMMMM&HM[R\
+ d"Z\.               9MMMMMMMMMMMMMMMMM[HMM|:
+-H    -              MMMMMMMMMMMMMMMMMMMbMP' :
+:??Mb#               `9MMMMMMMMMMMMMMMMMMH#! .
+: MMMMH#,              "*""""`#HMMMMMMMMMMH  -
+||MMMMMM6\.                    {MMMMMMMMMH'  :
+:|MMMMMMMMMMHo                 `9MMMMMMMM'   .
+. HMMMMMMMMMMP'                 !MMMMMMMM    `
+- `#MMMMMMMMM                   HMMMMMMM*,/  :
+ :  ?MMMMMMMF                   HMMMMMM',P' :
+  .  HMMMMR'                    {MMMMP' ^' -
+   : `HMMMT                     iMMH'     .'
+    -.`HMH                               .
+      -:*H                            . '
+        -`\,,    .                  .-
+          ' .  _                 .-`
+              '`~\.__,obb#q==~'''
+              .ovr:HMM#?:`' >b\_
+          .,:&Hi' `'   "' \\|&bSMHo_
+        oHMMM#*}          `?&dMMMMMMHo.
+     .dMMMH"''''           ,oHH*&&9MMMM?.
+    ,MMM*'                 `*M\bd<|"*&#MH\
+   dHH?'                   :MMMMMM#bd#odMML
+  H' |\                  `dMMMMMMMMMMMMMM9Mk
+ JL/"7+,.                `MMMMMMMMMMMMMMMH9ML
+-`Hp     '               |MMMMMMMMMMMMMMMMHH|:
+:  \\#M#d?                `HMMMMMMMMMMMMMMMMH.
+.   JMMMMM##,              ``*""'"*#MMMMMMMMH
+-. ,MMMMMMMM6o_                    |MMMMMMMM':
+:  |MMMMMMMMMMMMMb\                 TMMMMMMT :
+.   ?MMMMMMMMMMMMM'                 :MMMMMM|.`
+-    ?HMMMMMMMMMM:                  HMMMMMM\|:
+ :     9MMMMMMMMH'                 `MMMMMP.P.
+  .    `MMMMMMT''                   HMMM*''-
+   -    TMMMMM'                     MM*'  -
+    '.   HMM#                            -
+      -. `9M:                          .'
+        -. `b,,    .                . '
+          '-\   .,               .-`
+              '-:b~\\_,oddq==--"
+              _oo##'9MMHb':'-,o_
+          .oH":HH$' ""'  "' -\7*R&o_
+       .oHMMMHMH#9:          "\bMMMMHo.
+      dMMMMMM*""'`'           .oHM"H9MM?.
+    ,MMMMMM'                   "HLbd<|?&H\
+   JMMH#H'                     |MMMMM#b>bHb
+  :MH  ."\                   `|MMMMMMMMMMMM&
+ .:M:d-"|:b..                 9MMMMMMMMMMMMM+
+:  "*H|      -                &MMMMMMMMMMMMMH:
+.    `LvdHH#d?                `?MMMMMMMMMMMMMb
+:      iMMMMMMH#b               `"*"'"#HMMMMMM
+.   . ,MMMMMMMMMMb\.                   {MMMMMH
+-     |MMMMMMMMMMMMMMHb,               `MMMMM|
+:      |MMMMMMMMMMMMMMH'                &MMMM,
+-       `#MMMMMMMMMMMM                 |MMMM6-
+ :        `MMMMMMMMMM+                 ]MMMT/
+  .       `MMMMMMMP"                   HMM*`
+   -       |MMMMMH'                   ,M#'-
+    '.     :MMMH|                       .-
+      .     |MM                        -
+       ` .   `#?..    .             ..'
+           -.     _.             .-
+              '-|.#qo__,,ob=~~-''
+              _ooppH[`MMMD::--\_
+          _oHMR":&M&. ""'  "'  /&\\_
+        oHMMMMMHMMH#9,         `"<MMHo.
+      oHMMMMMMMM*""'``           .dMP#M?.
+    .dMMMMMMMM*                   `H\do?&\
+   -iMMMHH#H'                      &MMMHb#?
+  : ZMM'   7-.                   `{MMMMMMMMH
+ .  .M6_d|"`$|v..                 9MMMMMMMMML
+-    `'*H#       :                |MMMMMMMMMM:
+:        *\\dHM#dd.                ?MMMMMMMMMb
+-          |MMMMMMM##\              `"*""?HMMM
+:      .  |MMMMMMMMMMMo\.                 {MMM
+.         {MMMMMMMMMMMMMMMHo.             `MMM
+-          ?MMMMMMMMMMMMMMM*'             -MMP
+:           `#MMMMMMMMMMMMT               dMM'
+ -            |MMMMMMMMMMH'              -MMT
+  :           `MMMMMMMM"'                JMP
+   -           MMMMMMH'                 ,H?
+    '.         HMMM#'                    :
+      .        ?MM-                   . '
+        -.      *M:..               .-
+           - .      _.           .-
+              '-.~-dHb__\ov+~~-`
+              .oo>#&H6?*MMH:-.._
+          .oHMMMR:"&MZ\ `"'  "  |$-_
+       ..dMMMMMMMMdMMM#9\        `'HHo.
+     . ,dMMMMMMMMMMM"`' `           ?MP?.
+    . |MMMMMMMMMMM'                 `"$b&\
+   -  |MMMMHH##M'                     HMMH?
+  -   TTMM|    >..                   \MMMMMH
+ :     |MM\,#-""$~b\.                `MMMMMM+
+.       ``"H&#        -               &MMMMMM|
+:            *\v,#MHddc.              `9MMMMMb
+.               MMMMMMMM##\             `"":HM
+-          .  .HMMMMMMMMMMRo_.              |M
+:             |MMMMMMMMMMMMMMMM#\           :M
+-              `HMMMMMMMMMMMMMMM'           |T
+:               `*HMMMMMMMMMMMM'            H'
+ :                 MMMMMMMMMMM|            |T
+  .                MMMMMMMM?'             ./
+  `.               MMMMMMH'              ./
+    -.            |MMMH#'                .
+      .           `MM*                . '
+        -.         #M: .    .       .-
+          ` .         .,         .-
+              '-.-~ooHH__,,v~--`
+              _ood>H&H&Z?#M#b-\.
+          .\HMMMMMR?`\M6b."`' ''``v.
+       .. .MMMMMMMMMMHMMM#&.      ``~o.
+     .   ,HMMMMMMMMMMMM*"'-`          &b.
+    .   .MMMMMMMMMMMMH'               `"&\
+   -     RMMMMM#H##R'                   4Mb
+  -      |7MMM'    ?::                 `|MMb
+ /         HMM__#|`"\>?v..              `MMML
+.           `"'#Hd|       `              9MMM:
+-                |\,\?HH#bbL             `9MMb
+:                   !MMMMMMMH#b,          `""T
+.              .   ,MMMMMMMMMMMbo.           |
+:                  4MMMMMMMMMMMMMMMHo        |
+:                   ?MMMMMMMMMMMMMMM?        :
+-.                   `#MMMMMMMMMMMM:        .-
+ :                     |MMMMMMMMMM?         .
+  -                    JMMMMMMMT'          :
+  `.                   MMMMMMH'           -
+    -.                |MMM#*`            -
+      .               HMH'            . '
+        -.            #H:.          .-
+          ` .           .\       .-
+              '-..-+oodHL_,--/-`
+              _,\?dZkMHF&$*q#b..
+          .//9MMMMMMM?:'HM\\"`-''`..
+       ..`  :MMMMMMMMMMHMMMMH?_    `-\
+     .     .dMMMMMMMMMMMMMM'"'"       `\.
+    .      |MMMMMMMMMMMMMR              \\
+   -        T9MMMMMHH##M"                `?
+  :          (9MMM'    !':.               &k
+ .:            HMM\_?p "":-b\.            `ML
+-                "'"H&#,       :           |M|
+:                     ?\,\dMH#b#.           9b
+:                        |MMMMMMM##,        `*
+:                   .   +MMMMMMMMMMMo_       -
+:                       HMMMMMMMMMMMMMM#,    :
+:                        9MMMMMMMMMMMMMH'    .
+: .                       *HMMMMMMMMMMP     .'
+ :                          MMMMMMMMMH'     .
+  -                        :MMMMMMM'`      .
+  `.                       9MMMMM*'       -
+    -.                    {MMM#'         :
+      -                  |MM"          .'
+       `.                &M'..  .   ..'
+          ' .             ._     .-
+              '-. -voboo#&:,-.-`
+              _oo:\bk99M[<$$+b\.
+           .$*"MMMMMMMM[:"\Mb\?^" .
+       . '`    HMMMMMMMMMMHMMMM+?.  `.
+     .        .HMMMMMMMMMMMMMMP"''     .
+    .         `MMMMMMMMMMMMMM|         -`.
+   -           `&MMMMMMHH##H:             :
+  :             `(*MMM}    `|\             |
+ : `-              ?MMb__#|""`|+v..         \
+.                    `''*H#b       -        :|
+:                         `*\v,#M#b#,        \
+.                             9MMMMMMHb.     :
+:                        .   #MMMMMMMMMb\    -
+-                           .HMMMMMMMMMMMMb  :
+:                            `MMMMMMMMMMMMH  .
+-:  .                         `#MMMMMMMMMP   '
+ :                              ]MMMMMMMH'  :
+  -                            ,MMMMMM?'   .
+  `:                           HMMMMH"    -
+    -.                       .HMM#*     .-
+     `.                     .HH*'     .
+       `-.                  &R".    .-
+           -.               ._   .-
+              '-. .voodoodc?..-`
+              _\oo\?ddk9MRbS>v\_
+          ..:>*""MMMMMMMMM:?|H?$?-.
+       ..- -     "HMMMMMMMMMMHMMMH\_-.
+     .            dMMMMMMMMMMMMMMT"    .
+    .             TMMMMMMMMMMMMMM       `.
+   -               `&HMMMMMM#H#H:         .
+  -                 `\7HMMH     |\.        .
+ :    `                 HMM\_?c`""+?\..     :
+-                         "``#&#|      .     -
+:                              `?,\#MHdb.    .
+:                                 |MMMMMH#.  :
+:                            .   ,HMMMMMMMb, -
+: '                              4MMMMMMMMMMH`
+:   .                             9MMMMMMMMMT-
+:.`                               `#MMMMMMMH '
+ :      '                           HMMMMMH':
+  -                                |MMMMH" -
+  `:                              |MMMH*' .'
+    '?                           dMM#'   .
+      \.                       .dH"    .'
+        -.                    ,M'-  ..'
+          ` .                .. ..-`
+              '-. .\ooooboo<^.-`
+              _o,:o?\?dM&MHcc~,.
+          ..^':&#""HMMMMMMMM$:?&&?.
+        .`  -`      'HMMMMMMMMMHMMMp\.
+     . '             |MMMMMMMMMMMMMM"' .
+    .                `9MMMMMMMMMMMMM    -.
+   -                   `*9MMMMMHH##[      .
+  -                     `\Z9MMM    `~\     .
+ :       '|                 ?MMb_?p""-?v..  :
+-                             `"'*&#,    -   .
+:                                  `?,oHH#?  .
+--                                    |MMMMH,:
+:                                 .  |MMMMMM6,
+:   -                                |MMMMMMMM
+?                                     HMMMMMMP
+-- . '                                |HMMMMM'
+ :.`     .  '                          JMMMM+
+  \                                   ,MMMP:
+   :                                 |MMH?:
+    -:\.                            dM#" .
+       \                          ,H*' .'
+        -.                       d':..'
+          ` .                  .,.-
+              '-.. .\oooodov~^-`
+              _o\:,??\??MR9#cb\_
+          .v/''':&#""#HMMMMMMM$?*d\.
+       ..~' - -`      `"#MMMMMMMMMMMHv.
+     .-'                 HMMMMMMMMMMMR!.
+    :                    `9MMMMMMMMMMM| -.
+   .                       `*9MMMMMH##|   .
+  -                          `(#MMH   `:,  .
+ :           '|                 `HMb_>/"|\,.:
+.'                                `"'#&b   - .
+:                                      ?\oHH?.
+:                                        !MMM&
+:  .                                  .  HMMMM
+/.      -                               -MMMMM
+\`.                                      9MMMP
+:. .  . -                                |MMM'
+ \... '                                  .MMT
+  &.                                    .dMP
+   \,                                  .HM*
+    \. `\.                            ,H&'
+     `- `| -                        ,&':
+       `.                         ,/\ '
+          '-..                  _.-
+              "---.._\o,oov+--'"
+              _,d?,:?o?:?HM>#b\_
+          ..H*"''`'H#*"**MMMMMM6$$v_
+        v//"   - ``      `'#MMMMMMMMHo.
+      /"`                   |MMMMMMMMMM:.
+    ,>                       `HMMMMMMMMH:.
+   :                           `#HMMMMHH\ -
+  '                              `Z#MM,  `,:
+ :               '\                 ?HH_>:`\,
+:                                     "'*&| `:
+.                                         <\Hb
+:                                           MM
+:                                        . iMM
+Mb\.                                       {MM
+::.`-       -                              !MP
+`&.   .  .  -                              :M'
+ 9H,  \  '                                 |T
+  HM?                                     ,P
+   *ML                                   ??
+    :&.   `o                           .d'
+      ':  |T                          /"
+        -.                         .<''
+          `...                  ..-
+              "`-=.,_,,,oov-~.-`
+              _,oc>?_:b?o?HH#b\_
+          .v/99*""" '*H#""*HMMMMMZ,_
+        oH* /"   -   '      "`#MMMMM#o.
+     ./*>-                     `MMMMMMMb
+    ,b/'                        `#MMMMMMM\
+   :'                             ``HMMMMb:
+  /-                                `|&MH `\
+ /                   `-.               |Hb??\
+,-  '                                    "`&,.
+1                                           \}
+!.                                           T
+$,.                                        . 1
+?`M??.                                       M
+?.::| '\        -                            ?
+ M?&.    .   .  -                           ,'
+ 9MMH\   ..  '           `                  .
+  HMMM#.                                   :'
+   9#MMb                                 ..
+    -:"#     `b.                        .-
+      . `    {!                        /
+        -                           ,-'
+          ' .                    .-
+             ```^==\_.,,,ov--\-`
+              _\o##??,:io??$#b\_
+          .oH#"H9*""" "`#H*"*#MMMHo_
+        oHMM- -'    -  ''     ``*HMMHo.
+      dM#S>-`                     ?MMMM?.
+    ,&&,/'                         "#MMMH\
+   d?-"                              `*HMMb
+  H?                                   "ZHb:
+ /:                        \              H?L
+|:|   .                                    `*:
+:?:                                          \
+>"                                           :
+M|\,_                                        |
+!|":HH?-'.                                   :
+:^'_:?"\ `--         -                       .
+- |ML?b      .   ..  -                       -
+ :HMMMMH\    \               `              :
+  >MMMMMM#.                                .
+   ^M*HMMM|                               -
+    `. `"#+     `?v                     .`
+      .   `-    +?'                    -
+       ` .                          ..'
+           - .                   .-
+              "`\b=p?.._\\vv---`
+              _,o#bH\??::?o?cbo_
+          .o#MH#**SH""' "`*H#"*#MHo_
+        oHMMMH^  ^"    -  `      '*HHo.
+     .dMMM#">>-                     `HM?.
+    ,MH:R_o/                         `*MH\
+   dMM' '                               "ML
+  HMR! '                                 `#k
+ d&'.                          -.          `L
+:M ::     `                                 `-
+/| !|                                        -
+k.$-"                                        :
+}9R:!,,_.                                    .
+\::\':`*M#\-'.                               -
+: "''..:"!`\  '-          -                  `
+-   ,HMb.H|      .    _   -                 .'
+ : ,MMMMMMMb.    ..                         .
+  .`HMMMMMMMM?                             .
+  `.`9M#*HMMMM                            :
+    -.'   "##*      `b,                  .
+      .      `     ,/'                 .'
+       ` .                          ..'
+           - .                  ..-
+              "`*#d##c.._\v----`
+              _,o#&oHb?\o::d?>\_
+          .oHHMMM#**$M""` "`*HH"#&o_
+        oHMMMMMMD' .''    -  '    ``bo.
+     .dMMMMMH*'/|-                   `\b.
+    ,MMMM?T|_o/                        `\\
+   dMMMMP  ''                            `|
+  HMMMH& -                                `\
+ /MH7' :                          --        :
+-:MM  {.      .                              .
+:i?' .!&                                     .
+:{, o| '                                     :
+-T?9M\:-'o,_                                 .
+: \?::``"`?9MHo./..                          -
+.  '"`'^ _.`"!"^.  `-         -              `
+-      ,bMM?.M\       .    .  -      .      .'
+ :   .oMMMMMMMMb.    ..   `                 .
+  .  `HMMMMMMMMMMb                         -
+   -   9MH*#HMMMMH                        .'
+    '.  '   `"*##'      `b.              :
+      .         `     .d''             .'
+        -.                          . '
+           -.                    .-`
+              "`*##H###:._\--.-`
+              _oo#H&d#b?\b:_>>\_
+          .oHMMMMMMH*"*9R"'-``*#P\-_
+        oHMMMMMMMMM$  ."       '   `^-
+     .dMMMMMMMMH*",?-                 '\.
+    ,MMMMMMM:?}.,d'                     `.
+   dMMMMMMMH  /''                         :
+  HMMMMMMM&' -                             -
+ dPTMMP>' :                           -.    :
+|? -MM}  .\                                  .
+J' ::*'  -$L                                 .
+:  ?b .,H- '                                 :
+-  |6.&MP:: !.,_.                            -
+:   `\:: "' "`:"MM#,-^,            -         :
+-     ````:' _.:"?``\   `-                   .
+:         .?bMML.]#        -   _  `      .  .'
+ -      .o#MMMMMMMMH\     \.          .     .
+  -     `HMMMMMMMMMMMH                     :
+  `.     `HMM#*#MMMMMH'                   -
+    -.     '    ``##*'      i+           :
+      -            `'     v/'          .'
+       `-                           ..'
+          ' .                    .-
+              "`*##HMH##:__,-.-`
+              _oo##Mbb&bo??o_>\_
+          .oHMMMMMMMMM**#?M*' "?*&..
+        oHMMMMMMMMMMMM4  `"      -  `.
+     .dMMMMMMMMMMMM#"\?.-              .
+    ,MMMMMMMMMM}"9:_,d'                 -.
+   dMMMMMMMMMMM|  ^''                     .
+  &MMMMMMMMMMH\  -                         .
+ :{M*"MMMPT"' :                         `-. :
+.'M'  'MMM.  -T,       .                     .
+- k   i:?''  -|&                             .
+: `  -o&  .,H- "                             :
+-     `M:`HMP|:'!.o._.                       .
+:      "<:::'<^ '"``9MH#,-^ .                -
+-         '''``''._.`"?`^|   ^        -      :
+:              ?#dMM_.M?       .   .  -    ..'
+ :          ,ddMMMMMMMMMb.    ..   '        .
+  .         TMMMMMMMMMMMMM,                :
+   -         ?MMH**#MMMMMH'               :
+    '.        '     "`##*'      &.       :
+      -.               `'    ,~"       .'
+        -.                          ..'
+          ` .                    .-
+             ```*##HMMMH#<:,..-`
+              _,dd#HMb&dHo?\?:\_
+          .oHMMMMMMMMMMMH***9P'`"\v.
+        oHMMMMMMMMMMMMMMM>  `'      -.
+     .dMMMMMMMMMMMMMMMH*'|~-'          .
+    ,MMMMMMMMMMMMM6>`H._,&              -.
+   dMMMMMMMMMMMMMMM|  `"                  .
+  H*MMMMMMMMMMMMMH&. -                     .
+ d' HMM""&MMMPT'' :.                      `.-
+,'  MP   `TMMM,   |:        .                -
+|   #:    ? *"   : &L                        :
+!   `'   /?H   ,#r `'                        :
+.         ?M: HMM^<~->,o._                   :
+:          `9:::'`*-``':`9MHb,|-,         '  :
+.             `"''':' :_ ""!"^.  `|          :
+`.                 _dbHM6_|H.      .   . '  .'
+ \              _odHMMMMMMMMH,    ..  `     :
+ `-             |MMMMMMMMMMMMM|            :
+  `.             9MMH**#MMMMMH'           :
+    -.            '     "?##"      d     :
+      .                    '    ,/"    .'
+       `..                          ..'
+          `  .                   .-
+              '`"#HHMMMMM#<>..-`
+              _oo##bHMb&d#bd,>\_
+          .oHMMMMMMMMMMMMMM***9R"-..
+        oHMMMMMMMMMMMMMMMMMH\  ?   `-.
+     .dMMMMMMMMMMMMMMMMMMM#".}-'       .
+    ,MMMMMMMMMMMMMMMMM6/`H _o}          -.
+   dMMMMMMMMMMMMMMMMMMML  `''             .
+  HbP*HMMMMMMMMMMMMMMM*: -                 ,
+ dMH' `MMMP'"HMMMR'T"  :                    :
+|H'   -MR'   `?MMMb    P,       .            .
+1&     *|     |.`*"  .-`&|                   .
+M'      "    |\&|  .,#~ "'                   :
+T             :HL.|HMH\c~`|v,\_              :
+|              `"|:::':`-`` '"MM#\-'.       -:
+%                 ``'``'`' :_ '?'`| ``.      :
+||,                     ,#dMM?.M?      .  .` -
+ ?\                 .,odMMMMMMMMM?    \  `  :
+  /                 |MMMMMMMMMMMMM:        .'
+  `.                 TMMH#*9MMMMM*        :
+    -.               `      "*#*'    ,:  .
+      .                       `   .v'' .'
+       `.                           ..'
+          '- .                   .-
+              "`\+HHMMMMMMHr~.-`
+              _,,>#b&HMHd&&bb>\_
+          _oHMMMMMMMMMMMMMMMMH**H:.
+        oHMMMMMMMMMMMMMMMMMMMM#v`?  `.
+     .dMMMMMMMMMMMMMMMMMMMMMMH*`+|     .
+    ,MMMMMMMMMMMMMMMMMMMMMb|?+.,H       -.
+   ddHMMMMMMMMMMMMMMMMMMMMMb  `'          .
+  HMMkZ**HMMMMMMMMMMMMMMMMH\  -   .        :
+ dTMMM*  `9MMMP'"*MMMMPT"` ..               :
+|M6H''    4MP'   `"HMMM|   !|.      .        .
+1MHp'      #L      $ *"'  .-:&.              .
+MMM'        "     q:H.  .o#-``'              :
+MM'                ?H?.|MMH::::-o,_.         -
+M[                  `*?:::'|` `"`:9MH\~-.    `
+&M.                     ""'`'^'.:.`?'`. '|  -:
+`M|d,                       .dbHM[.1?     .. :
+ 9||| .                  _obMMMMMMMMH,   .  :
+  H.^                    MMMMMMMMMMMM}     -
+   \                     |MMH#*HMMMMH'    .'
+    .                    `      `#*'   ,:-
+     `                           '' .-'.
+       `.                           .-
+          '- .                   .-`
+              '`\bqHMMMMMMHHb--`
+              .,:,#&6dHHHb&##o\_
+          .oHHMMMMMMMMMMMMMMMMMH*\,.
+        oHMMMMMMMMMMMMMMMMMMMMMMHb:'-.
+     .dMMMMMMMMMMMMMMMMMMMMMMMMMH|\/'  .
+    ,&HMMMMMMMMMMMMMMMMMMMMMMM/"&.,d.   -.
+   dboMMHMMMMMMMMMMMMMMMMMMMMMML `'       .
+  HMHMMM$Z***MMMMMMMMMMMMMMMMMM|.-         .
+ dMM}MMMM#'  `9MMMH?"`MMMMR'T'  _           :
+|MMMbM#''     |MM"    ``MMMH.   <_           .
+dMMMM#&        *&.     .?`*"   .'&:          .
+MMMMMH-         `'    -v/H   .dD "'  '       :
+MMMM*                  `*M: 4MM*::-!v,_      :
+MMMM                     `*?::" "'``"?9Mb::. :
+&MMM,                       `"'"'|"._ "?`| - :
+`MMM}.H                          ,#dM[_H   ..:
+ 9MMi`M: .                   .ooHMMMMMMM,  ..
+  9Mb `-                     1MMMMMMMMMM|  :
+   ?M                        |MM#*#MMMM*  .
+    -.                       `     |#"' ,'
+      .                            -" v`
+        -.                          .-
+           - .                   . `
+              '-*#d#HHMMMMHH#"-'
+              _,<_:&S6dHHHb&bb\_
+          .odHMMMMMMMMMMMMMMMMMMM}-_
+       .oHMMMMMMMMMMMMMMMMMMMMMMMM#d:.
+      ?9MMMMMMMMMMMMMMMMMMMMMMMMMMMH-$ .
+    ,::dHMMMMMMMMMMMMMMMMMMMMMMMMH:\.?? -.
+   dMdboHMMHMMMMMMMMMMMMMMMMMMMMMMH, '    .
+  HMMMM7MMMb$R***MMMMMMMMMMMMMMMMMH\ -     .
+ dMMMMM/MMMMM*   `$MMMM*'"*MMMM?&'  .       :
+|MMMMMMb1H*'       HMP'    '9MMM|   &.    .  .
+dMMMMMMM##~`       `#\      |.`*"  .-9.      :
+9MMMMMMMM*           `     |v7?  .,H `' `    :
+SMMMMMMH'                   '9M_-MMH::-\v_   :
+:HMMMMM                       `\_:"'|'`':9Mv\.
+-|MMMMM,                         ""`'`':.`?\ \
+`:MMMMM}.d}                         .?bM6,|  |
+ :?MMM6  M|  .                   .,oHMMMMM| /
+  .?MMM- `'                      &MMMMMMMM|.
+   -`HM-                         HMH#*MMM?:
+    '.                           '   `#*:`
+      -                              -'/
+       ` .                          . '
+          ` .                    . `
+              '--##HH#HMMMHH#""`
+              _o,d_?dZdoHHHb#b\_
+          .vdMMMMMMMMMMMMMMMMMMMMH\.
+       .,HHMMMMMMMMMMMMMMMMMMMMMMMMH&,.
+      /?RMMMMMMMMMMMMMMMMMMMMMMMMMMMMH|..
+    ,\?>`T#RMMMMMMMMMMMMMMMMMMMMMMMM6`\|/
+   dMMbd#ooHMMMHMMMMMMMMMMMMMMMMMMMMMH,`' '
+  HMMMMMMMTMMMMb$ZP**HMMMMMMMMMMMMMMMM|.   :
+ dMMMMMMMM}$MMMMMH'   `HMMMH?"`MMMM?T' .    :
+|MMMMMMMMMMoMH*''      `MM?    ``MMM|  +\    .
+1MMMMMMMMMMMb#/         ?#?      |`#"  -T:   :
+*'HMMMMMMMMMM*'           "     ~?&  .?} ' ' .
+- 4MMMMMMMMP"                    `M? HMTc:\\.:
+: `MMMMMMM[                       "#:::`>`"?M{
+.  |MMMMMMH.                        ``'``'_`:-
+-  |MMMMMMM|.dD                         ,#Mb\'
+ :  *MMMMM: iM|  .                   _oHMMMM:
+  .  ?MMMM'  "'                     ,MMMMMMP
+   :  `HMH                          JM#*MMT
+    -.  '                           `   #'
+      .                                /
+        -.            -              .'
+           -.                    . `
+              '--=&&MH##HMHH#"""
+              .-:?,Z?:&$dHH##b\_
+           ,:bqRMMMMMMMMMMMMMMMMMHo.
+        .?HHHMMMMMMMMMMMMMMMMMMMMMMMHo.
+      -o/*M9MMMMMMMMMMMMMMMMMMMMMMMMMMMv
+    .:H\b\'|?#HHMMMMMMMMMMMMMMMMMMMMMM6?Z\
+   .?MMMHbdbbodMMMMHMMMMMMMMMMMMMMMMMMMM\':
+  :MMMMMMMMMMM7MMMMb?6P**#MMMMMMMMMMMMMMM_ :
+ \MMMMMMMMMMMMb^MMMMMM?   `*MMMM*"`MMMR<' . -
+.1MMMMMMMMMMMMMb]M#""       9MR'   `?MMb  \. :
+-MMMMMMMMMMMMMMMH##|`        *&.     |`*' .\ .
+-?""*MMMMMMMMMMMMM'            '    |?b  ,}" :
+:    MMMMMMMMMMH'                    `M_|M}r\?
+.    `MMMMMMMMM'                      `$_:`'"H
+-     TMMMMMMMM,                        '"``::
+:     {MMMMMMMM| oH|                      .#M-
+ :    `9MMMMMM' .MP   .                 ,oMMT
+  .     HMMMMP'  `'                    ,MMMP
+   -     `MMH'                         HH9*
+    '.    `                           ` .'
+      -                               . '
+       ` .               -          .-
+          ` .                    .-
+              ' -==pHMMH##HH#"""
+              _..-:b&::&?&&##bo_
+          ...?-#&9MMMMMMMMMMMMMMMHo_
+       .. .1&#MMHMMMMMMMMMMMMMMMMMMMHo.
+     .  .o/##R9MMMMMMMMMMMMMMMMMMMMMMMM?.
+    .- |MSd?|'`$?#HMMMMMMMMMMMMMMMMMMMMMH\
+   -  dMMMMHbd##oodMMMM#MMMMMMMMMMMMMMMMMH:
+  - JMMMMMMMMMMMMM7HMMMH$SR***MMMMMMMMMMMMb>
+ : {MMMMMMMMMMMMMMM`9MMMMMH'  ``HMMM?"*MM[| :
+- |MMMMMMMMMMMMMMMMM<MH*''      `MM'   'HM? |.
+: `MMMMMMMMMMMMMMMMMM##H-'       `#,  ` |`? /|
+.  ?"""?HMMMMMMMMMMMMMH'           "    v& .}?
+-       |MMMMMMMMMMMP'                  `H:&H&
+i       `9MMMMMMMMMT                     `|?"?
+:         MMMMMMMMMH                       "`\
+:         MMMMMMMMMH-.dH                    ,|
+ :        ?MMMMMMM?  {M' .                .dT
+  .        ?MMMMMR'  `'                  ,MP
+   -        `HMM#'                      .&*
+    '.        '                         `.
+      -                               . '
+       `..                          .-
+           -.                    .-
+              '-.==p##HMMHp&#"""
+              _v---:?&?:?&?&#b\_
+          ..' i: #M$MMMMMMMMMMMMMHo_
+       ..   -]M#HMHMMMMMMMMMMMMMMMMMHo.
+     .     ooP*&6&MMMMMMMMMMMMMMMMMMMMM?.
+    . -   &Rbbd-/`?:##HMMMMMMMMMMMMMMMMMH?
+   -    ,HMMMMM#od#boodMMMMHMMMMMMMMMMMMMMb
+  -   iMMMMMMMMMMMMMMM[*MMMH&$R***MMMMMMMMMb
+ :   |MMMMMMMMMMMMMMMMML"MMMMMM'  ``MMMP"HMM:
+.    HMMMMMMMMMMMMMMMMMMb/MH""      `MR   *M,|
+:    TMMMMMMMMMMMMMMMMMMMMd#&`       `D.   ?|\
+.     `*"'"*HMMMMMMMMMMMMMMP'          '  -d,J
+:           |MMMMMMMMMMMMP'                ||M
+M,           ?MMMMMMMMMM|                  `\?
+&|            HMMMMMMMMM}                   ``
+`L           .MMMMMMMMMMP ,d|                :
+ *.           ?MMMMMMMF' .MP                /
+  |            TMMMMMM'  `"'               /
+  `.            `MMMP'                   ./
+    -.           `                       .
+      .                               . '
+        - .                         .-
+           -\                   ..-
+              '-..=p####HMH&="""
+              _vo~^'':&b::d,#b\_
+          ..`" `:v +9P]MMMMMMMMMMHo_
+        ,-     ?Mb#MMMMMMMMMMMMMMMMMHo.
+     . "     ,ooM*&&&HMMMMMMMMMMMMMMMMHb.
+    .   -    99Soo?|'`*?##HMMMMMMMMMMMMMH\
+   -       .HMMMMMM#od#boodMMMMHMMMMMMMMMMb
+  -      :MMMMMMMMMMMMMMMM67HMMH&$R**HMMMMMb
+ :      .MMMMMMMMMMMMMMMMMMM/HMMMMM|  `9MM'HL
+:       {MMMMMMMMMMMMMMMMMMMM\MM*''    `H[ `9|
+|       `HMMMMMMMMMMMMMMMMMMMMb##|      `F. :?
+H        `"*"'"`#MMMMMMMMMMMMMMM?         '  k
+M.               MMMMMMMMMMMMM"'             H
+MMH.             `HMMMMMMMMMM:               |
+&MM|              `MMMMMMMMMM,               -
+`MM|              dMMMMMMMMMM|.oH            :
+ 9ML              `HMMMMMMM?  dH'           -
+  Hi               |MMMMMMP   "'           .'
+   T.               `MMM#'                -
+    `.               `                  .`
+     `                                 -
+       `.. .                        ..'
+           ...                   .-
+              '-. //######M#b~""
+              _ooq=""''$b$_&?b\_
+          .-`^"  "'o |&M:MMMMMMMMHo_
+        o/'      -$Mb#MMMMMMMMMMMMMMHo.
+      /'        .ooHP*&R&MMMMMMMMMMMMMM?.
+    .'          `MRbod?|'`+?##9MMMMMMMMMH\
+  .`          .,MMMMMMH#od##obdMMMMHMMMMMMb
+  -          ?MMMMMMMMMMMMMMMMM$HMMH$ZP*HMMb
+ ?          |MMMMMMMMMMMMMMMMMMM|9MMMMP  "M6\
+.-          dMMMMMMMMMMMMMMMMMMMMb]M*'    |R |
+1|          `HMMMMMMMMMMMMMMMMMMMMMd#|     ?,:
+MH,          ``*""'"*#MMMMMMMMMMMMMM*       '`
+MM6_                 |MMMMMMMMMMMMH"         :
+MMMMMb.               "MMMMMMMMMMT           -
+&MMMMM'                |MMMMMMMMMH           `
+!MMMMb                .HMMMMMMMMM+.?&        :
+ TMMMM                 *MMMMMMMP  dH' .     :
+  9MM'                 `MMMMMMP'  "'       .
+   9ML                  `MMM#'            -
+    `H                   `               :
+     `\.                               .'
+       `-\  .                       .-
+          ' ._                   .-`
+              '-\. ,b#####p&**^`
diff --git a/third_party/pexpect/tests/interact.py b/third_party/pexpect/tests/interact.py
new file mode 100755
index 0000000..9f8e672
--- /dev/null
+++ b/third_party/pexpect/tests/interact.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+try:
+    # This allows coverage to measure code run in this process
+    import coverage
+    coverage.process_startup()
+except ImportError:
+    pass
+
+from utils import no_coverage_env
+import pexpect
+import sys
+
+
+def main():
+    p = pexpect.spawn(sys.executable + ' echo_w_prompt.py',
+                      env=no_coverage_env())
+    p.interact()
+    print("Escaped interact")
+
+if __name__ == '__main__':
+    main()
diff --git a/third_party/pexpect/tests/interact_unicode.py b/third_party/pexpect/tests/interact_unicode.py
new file mode 100644
index 0000000..f4c1f55
--- /dev/null
+++ b/third_party/pexpect/tests/interact_unicode.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+'''
+Just like interact.py, but using spawnu instead of spawn
+'''
+try:
+    # This allows coverage to measure code run in this process
+    import coverage
+    coverage.process_startup()
+except ImportError:
+    pass
+
+from utils import no_coverage_env
+import pexpect
+import sys
+
+
+def main():
+    p = pexpect.spawnu(sys.executable + ' echo_w_prompt.py',
+                       env=no_coverage_env())
+    p.interact()
+    print("Escaped interact")
+
+if __name__ == '__main__':
+    main()
diff --git a/third_party/pexpect/tests/list100.py b/third_party/pexpect/tests/list100.py
new file mode 100644
index 0000000..3b2b6ec
--- /dev/null
+++ b/third_party/pexpect/tests/list100.py
@@ -0,0 +1,2 @@
+# -*- coding: utf-8 -*-
+print(list(range(100)))
diff --git a/third_party/pexpect/tests/needs_kill.py b/third_party/pexpect/tests/needs_kill.py
new file mode 100755
index 0000000..810236bd
--- /dev/null
+++ b/third_party/pexpect/tests/needs_kill.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+"""This script can only be killed by SIGKILL."""
+import signal, time
+
+# Ignore interrupt, hangup and continue signals - only SIGKILL will work
+signal.signal(signal.SIGINT, signal.SIG_IGN)
+signal.signal(signal.SIGHUP, signal.SIG_IGN)
+signal.signal(signal.SIGCONT, signal.SIG_IGN)
+
+print('READY')
+while True:
+    time.sleep(10)
+    
\ No newline at end of file
diff --git a/third_party/pexpect/tests/pexpectTest.py b/third_party/pexpect/tests/pexpectTest.py
new file mode 100755
index 0000000..6839a2f
--- /dev/null
+++ b/third_party/pexpect/tests/pexpectTest.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+import time
+import pexpect
+import sys
+
+def getProcessResults(cmd, timeLimit=20):
+  '''
+  executes 'cmd' as a child process and returns the child's output,
+  the duration of execution, and the process exit status. Aborts if
+  child process does not generate output for 'timeLimit' seconds.
+  '''
+  output = ""
+  startTime = time.time()
+  child = pexpect.spawn(cmd, timeout=10)
+  child.logfile = sys.stdout
+
+  while 1:
+    try:
+      # read_nonblocking will add to 'outout' one byte at a time
+      # newlines can show up as '\r\n' so we kill any '\r's which
+      # will mess up the formatting for the viewer
+      output += child.read_nonblocking(timeout=timeLimit).replace("\r","")
+    except pexpect.EOF as e:
+      print(str(e))
+      # process terminated normally
+      break
+    except pexpect.TIMEOUT as e:
+      print(str(e))
+      output += "\nProcess aborted by FlashTest after %s seconds.\n" % timeLimit
+      print(child.isalive())
+      child.kill(9)
+      break
+
+  endTime = time.time()
+  child.close(force=True)
+
+  duration = endTime - startTime
+  exitStatus = child.exitstatus
+
+  return (output, duration, exitStatus)
+
+cmd = "./ticker.py"
+
+result, duration, exitStatus = getProcessResults(cmd)
+
+print("result: %s" % result)
+print("duration: %s" % duration)
+print("exit-status: %s" % exitStatus)
+
diff --git a/third_party/pexpect/tests/platform_checks/CSIGNALTEST/test.c b/third_party/pexpect/tests/platform_checks/CSIGNALTEST/test.c
new file mode 100644
index 0000000..86bcc17
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/CSIGNALTEST/test.c
@@ -0,0 +1,90 @@
+/* I built this with "gcc -lutil test.c -otest" */
+#include <sys/types.h>  /* include this before any other sys headers */
+#include <sys/wait.h>   /* header for waitpid() and various macros */
+#include <signal.h>     /* header for signal functions */
+#include <stdio.h>      /* header for fprintf() */
+#include <unistd.h>     /* header for fork() */
+#ifdef LINUX
+#include <pty.h>
+#else
+#include <util.h>	/* header for forkpty, compile with -lutil */
+#endif
+
+void sig_chld(int);  /* prototype for our SIGCHLD handler */
+
+int main() 
+{
+    struct sigaction act;
+    int pid;
+    int fdm;
+    char slave_name [20];
+
+
+    /* Assign sig_chld as our SIGCHLD handler.
+       We don't want to block any other signals in this example 
+       We're only interested in children that have terminated, not ones
+       which have been stopped (eg user pressing control-Z at terminal).
+       Finally, make these values effective. If we were writing a real 
+       application, we would save the old value instead of passing NULL.
+     */
+    act.sa_handler = sig_chld;
+    sigemptyset(&act.sa_mask);
+    act.sa_flags = SA_NOCLDSTOP;
+    if (sigaction(SIGCHLD, &act, NULL) < 0) 
+    {
+        fprintf(stderr, "sigaction failed\n");
+        return 1;
+    }
+
+    /* Do the Fork thing. 
+    */
+    pid = forkpty (&fdm, slave_name, NULL, NULL);
+    /* pid = fork(); */
+
+    switch (pid)
+    {
+	    case -1:
+		fprintf(stderr, "fork failed\n");
+		return 1;
+	    break;
+
+	    case 0: /* Child process. */     
+		printf ("This child output will cause trouble.\n");
+		_exit(7);
+	    break;
+
+	    default: /* Parent process. */
+		sleep(1);
+		printf ("Child pid: %d\n", pid); 
+		sleep(10);  /* let child finish -- crappy way to avoid race. */
+	    break;
+    }
+
+    return 0;
+}
+ 
+void sig_chld(int signo) 
+{
+    int status, wpid, child_val;
+
+    printf ("In sig_chld signal handler.\n");
+
+    /* Wait for any child without blocking */
+    wpid = waitpid (-1, & status, WNOHANG);
+    printf ("\tWaitpid found status for pid: %d\n", wpid);
+    if (wpid < 0)
+    {
+        fprintf(stderr, "\twaitpid failed\n");
+        return;
+    }
+    printf("\tWaitpid status: %d\n", status);
+
+    if (WIFEXITED(status)) /* did child exit normally? */
+    {
+        child_val = WEXITSTATUS(status); 
+        printf("\tchild exited normally with status %d\n", child_val);
+    }
+    printf ("End of sig_chld.\n");
+}
+
+
diff --git a/third_party/pexpect/tests/platform_checks/README b/third_party/pexpect/tests/platform_checks/README
new file mode 100644
index 0000000..d50f94c
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/README
@@ -0,0 +1,2 @@
+These scripts are for checking how the underlying system behaves, not for
+testing Pexpect itself.
\ No newline at end of file
diff --git a/third_party/pexpect/tests/platform_checks/check.py b/third_party/pexpect/tests/platform_checks/check.py
new file mode 100755
index 0000000..b41755a
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/check.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+import signal
+import os
+import time
+import pty
+
+def signal_handler (signum, frame):
+    print 'Signal handler called with signal:', signum
+    print 'signal.SIGCHLD=', signal.SIGKILL
+
+# First thing we do is set up a handler for SIGCHLD.
+signal.signal (signal.SIGCHLD, signal.SIG_IGN)
+
+print 'PART 1 -- Test signal handling with empty pipe.'
+# Create a child process for us to kill.
+try:
+    pid, fd = pty.fork()
+except Exception as e:
+    print str(e)
+
+if pid == 0:
+#    os.write (sys.stdout.fileno(), 'This is a test.\n This is a test.')
+    time.sleep(10000)
+
+print 'Sending SIGKILL to child pid:', pid
+os.kill (pid, signal.SIGKILL)
+
+# SIGCHLD should interrupt sleep.
+# Note that this is a race.
+# It is possible that the signal handler will get called
+# before we try to sleep, but this has not happened yet.
+# But in that case we can only tell by order of printed output.
+print 'Entering sleep...'
+try:
+    time.sleep(10)
+except:
+    print 'sleep was interrupted by signal.'
+
+# Just for fun let's see if the process is alive.
+try:
+    os.kill(pid, 0)
+    print 'Child is alive. This is ambiguous because it may be a Zombie.'
+except OSError as e:
+    print 'Child appears to be dead.'
+
+print 'PART 2 -- Test signal handling with full pipe.'
+# Create a child process for us to kill.
+try:
+    pid, fd = pty.fork()
+except Exception as e:
+    print str(e)
+
+if pid == 0:
+    os.write (sys.stdout.fileno(), 'This is a test.\n This is a test.')
+    time.sleep(10000)
+
+print 'Sending SIGKILL to child pid:', pid
+os.kill (pid, signal.SIGKILL)
+
+# SIGCHLD should interrupt sleep.
+# Note that this is a race.
+# It is possible that the signal handler will get called
+# before we try to sleep, but this has not happened yet.
+# But in that case we can only tell by order of printed output.
+print 'Entering sleep...'
+try:
+    time.sleep(10)
+except:
+    print 'sleep was interrupted by signal.'
+
+# Just for fun let's see if the process is alive.
+try:
+    os.kill(pid, 0)
+    print 'Child is alive. This is ambiguous because it may be a Zombie.'
+except OSError as e:
+    print 'Child appears to be dead.'
+
diff --git a/third_party/pexpect/tests/platform_checks/check2.py b/third_party/pexpect/tests/platform_checks/check2.py
new file mode 100755
index 0000000..0a7d5f8
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/check2.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+import signal
+import os
+import time
+
+def signal_handler (signum, frame):
+	print 'Signal handler called with signal:', signum
+	print 'signal.SIGCHLD=', signal.SIGKILL
+
+# Create a child process for us to kill.
+pid = os.fork()
+if pid == 0:
+	time.sleep(10000)
+
+#signal.signal (signal.SIGCHLD, signal.SIG_IGN)
+signal.signal (signal.SIGCHLD, signal_handler)
+
+print 'Sending SIGKILL to child pid:', pid
+os.kill (pid, signal.SIGKILL)
+
+# SIGCHLD should interrupt sleep.
+# Note that this is a race.
+# It is possible that the signal handler will get called
+# before we try to sleep, but this has not happened yet.
+# But in that case we can only tell by order of printed output.
+interrupted = 0
+try:
+	time.sleep(10)
+except:
+	print 'sleep was interrupted by signal.'
+	interrupted = 1
+
+if not interrupted:
+	print 'ERROR. Signal did not interrupt sleep.'
+else:
+	print 'Signal interrupted sleep. This is good.'
+
+# Let's see if the process is alive.
+try:
+	os.kill(pid, 0)
+	print 'Child is alive. This is ambiguous because it may be a Zombie.'
+except OSError as e:
+	print 'Child appears to be dead.'
+
diff --git a/third_party/pexpect/tests/platform_checks/check_control_terminal.py b/third_party/pexpect/tests/platform_checks/check_control_terminal.py
new file mode 100755
index 0000000..9598fd7
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/check_control_terminal.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+
+import termios, fcntl, struct, os, sys
+
+def getwinsize():	
+	s = struct.pack("HHHH", 0, 0, 0, 0)
+	x = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, s)
+	rows, cols = struct.unpack("HHHH", x)[:2]
+	return rows, cols
+
+def setwinsize(r,c):
+	# Assume ws_xpixel and ws_ypixel are zero.
+	s = struct.pack("HHHH", r,c,0,0)
+	x = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCSWINSZ, s)
+print 'stdin tty:', os.ttyname(0)
+print 'stdout tty:', os.ttyname(1)
+print 'controlling terminal:', os.ctermid() 
+print 'winsize %d,%d' % getwinsize()
+print 'ENDTEST'
diff --git a/third_party/pexpect/tests/platform_checks/check_handler.py b/third_party/pexpect/tests/platform_checks/check_handler.py
new file mode 100755
index 0000000..c0633eb
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/check_handler.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+import signal
+import os
+import time
+import pty
+import sys
+import fcntl
+import tty
+GLOBAL_SIGCHLD_RECEIVED = 0
+                                                                                 
+def nonblock (fd):                                                           
+	# if O_NDELAY is set read() returns 0 (ambiguous with EOF).
+	# if O_NONBLOCK is set read() returns -1 and sets errno to EAGAIN
+	original_flags = fcntl.fcntl (fd, fcntl.F_GETFL, 0)
+	flags = original_flags | os.O_NONBLOCK
+	fcntl.fcntl(fd, fcntl.F_SETFL, flags)
+	return original_flags
+
+def signal_handler (signum, frame):
+	print '<HANDLER>'
+	global GLOBAL_SIGCHLD_RECEIVED
+	status = os.waitpid (-1, os.WNOHANG)
+	if status[0] == 0:
+		print 'No process for waitpid:', status
+	else:
+		print 'Status:', status
+	print 'WIFEXITED(status):', os.WIFEXITED(status[1])
+	print 'WEXITSTATUS(status):', os.WEXITSTATUS(status[1]) 
+	GLOBAL_SIGCHLD_RECEIVED = 1
+
+def main ():
+	signal.signal (signal.SIGCHLD, signal_handler)
+	pid, fd = pty.fork()
+	if pid == 0:
+		os.write (sys.stdout.fileno(), 'This is a test.\nThis is a test.')
+		time.sleep(10000)
+	nonblock (fd)
+	tty.setraw(fd) #STDIN_FILENO)
+	print 'Sending SIGKILL to child pid:', pid
+	time.sleep(2)
+	os.kill (pid, signal.SIGKILL)
+
+	print 'Entering to sleep...'
+	try:
+		time.sleep(2)
+	except:
+		print 'Sleep interrupted'
+	try:
+		os.kill(pid, 0)
+		print '\tChild is alive. This is ambiguous because it may be a Zombie.'
+	except OSError as e:
+		print '\tChild appears to be dead.'
+#		print str(e)
+	print
+	print 'Reading from master fd:', os.read (fd, 1000)
+
+
+
+if __name__ == '__main__':
+	main ()
diff --git a/third_party/pexpect/tests/platform_checks/check_read.py b/third_party/pexpect/tests/platform_checks/check_read.py
new file mode 100755
index 0000000..9598aa7
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/check_read.py
@@ -0,0 +1,35 @@
+import os
+
+filename = os.tmpnam()
+print 'filename:', filename
+
+fd_out = os.open(filename, os.O_CREAT | os.O_WRONLY)
+print 'fd_out:', fd_out
+os.write (fd_out, 'This is a test.\n')
+os.close(fd_out)
+print
+print 'testing read on good fd...'
+fd_in = os.open (filename, os.O_RDONLY)
+print 'fd_in:', fd_in
+while 1:
+	data_in = os.read(fd_in, 1)
+	print 'data_in:', data_in
+	if data_in == '':
+		print 'data_in was empty'
+		break #sys.exit(1)
+os.close(fd_in)
+print
+print
+print 'testing read on closed fd...'
+fd_in = os.open ('test_read.py', os.O_RDONLY)
+print 'fd_in:', fd_in
+while 1:
+	data_in = os.read(fd_in, 1)
+	print 'data_in:', data_in
+	if data_in == '':
+		print 'data_in was empty'
+		break
+os.close(fd_in)
+d = os.read(fd_in, 1) # fd_in should be closed now...
+if s == '':
+	print 'd is empty. good.'
diff --git a/third_party/pexpect/tests/platform_checks/check_signals.py b/third_party/pexpect/tests/platform_checks/check_signals.py
new file mode 100755
index 0000000..d45bc8a
--- /dev/null
+++ b/third_party/pexpect/tests/platform_checks/check_signals.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+import signal
+import os
+import time
+import pty
+import sys
+GLOBAL_SIGCHLD_RECEIVED = 0
+
+def signal_handler (signum, frame):
+    print '<HANDLER>'
+    global GLOBAL_SIGCHLD_RECEIVED
+    status = os.waitpid (-1, os.WNOHANG)
+    print 'WIFEXITED(status):', os.WIFEXITED(status)
+    print 'WEXITSTATUS(status):', os.WEXITSTATUS(status) 
+    GLOBAL_SIGCHLD_RECEIVED = 1
+
+def main ():
+#	sig_test ('SIG_IGN', 'ptyfork', 'yes')
+	sig_test ('handler', 'ptyfork', 'yes')
+#	sig_test ('SIG_IGN', 'ptyfork', 'no')
+#	sig_test ('handler', 'ptyfork', 'no')
+#	sig_test ('SIG_IGN', 'osfork', 'yes')
+#	sig_test ('handler', 'osfork', 'yes')
+#	sig_test ('SIG_IGN', 'osfork', 'no')
+#	sig_test ('handler', 'osfork', 'no')
+
+def sig_test (sig_handler_type, fork_type, child_output):
+	print 'Testing with:'
+	print '\tsig_handler_type:', sig_handler_type
+	print '\tfork_type:', fork_type
+	print '\tchild_output:', child_output
+
+	if sig_handler_type == 'SIG_IGN':
+		signal.signal (signal.SIGCHLD, signal.SIG_IGN)
+	else:
+		signal.signal (signal.SIGCHLD, signal_handler)
+	pid = -1
+	fd = -1
+	if fork_type == 'ptyfork':
+		pid, fd = pty.fork()
+	else:
+		pid = os.fork()
+
+	if pid == 0:
+		if child_output == 'yes':
+			os.write (sys.stdout.fileno(), 'This is a test.\nThis is a test.')
+		time.sleep(10000)
+
+	#print 'Sending SIGKILL to child pid:', pid
+	time.sleep(2)
+	os.kill (pid, signal.SIGKILL)
+
+	#print 'Entering to sleep...'
+	try:
+		time.sleep(2)
+	except:
+		pass
+	try:
+		os.kill(pid, 0)
+		print '\tChild is alive. This is ambiguous because it may be a Zombie.'
+	except OSError as e:
+		print '\tChild appears to be dead.'
+#		print str(e)
+	print
+
+if __name__ == '__main__':
+	main ()
diff --git a/third_party/pexpect/tests/qa.py b/third_party/pexpect/tests/qa.py
new file mode 100755
index 0000000..f50e3ed
--- /dev/null
+++ b/third_party/pexpect/tests/qa.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import commands
+import signal
+
+signal.signal(signal.SIGCHLD, signal.SIG_IGN)
+print(commands.getoutput('/bin/ls -l'))
+
diff --git a/third_party/pexpect/tests/sigwinch_report.py b/third_party/pexpect/tests/sigwinch_report.py
new file mode 100755
index 0000000..626d424
--- /dev/null
+++ b/third_party/pexpect/tests/sigwinch_report.py
@@ -0,0 +1,49 @@
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+from  __future__ import print_function
+
+import signal, time, struct, fcntl, termios, sys
+
+def getwinsize():
+    '''This returns the window size of the child tty.
+    The return value is a tuple of (rows, cols).
+    '''
+    if 'TIOCGWINSZ' in dir(termios):
+        TIOCGWINSZ = termios.TIOCGWINSZ
+    else:
+        TIOCGWINSZ = 1074295912 # Assume
+    s = struct.pack('HHHH', 0, 0, 0, 0)
+    x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
+    return struct.unpack('HHHH', x)[0:2]
+
+def handler(signum, frame):
+    print('signal')
+    sys.stdout.flush()
+    print('SIGWINCH:', getwinsize ())
+    sys.stdout.flush()
+
+print("setting handler for SIGWINCH")
+signal.signal(signal.SIGWINCH, handler)
+print("READY")
+
+while 1:
+    sys.stdout.flush()
+    time.sleep(1)
+
diff --git a/third_party/pexpect/tests/sleep_for.py b/third_party/pexpect/tests/sleep_for.py
new file mode 100755
index 0000000..9027105
--- /dev/null
+++ b/third_party/pexpect/tests/sleep_for.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+
+from  __future__ import print_function
+
+import time
+import sys
+
+def main():
+    """
+        This script sleeps for the number of seconds (float) specified by the
+        command line argument.
+    """
+    if len(sys.argv) < 2:
+        print("Usage: %s seconds_to_sleep" % (sys.argv[0],))
+        sys.exit(1)
+    timeout = float(sys.argv[1])
+    print("READY")
+    time.sleep(timeout)
+    print("END")
+
+if __name__ == '__main__':
+    main()
diff --git a/third_party/pexpect/tests/swapcase_echo.py b/third_party/pexpect/tests/swapcase_echo.py
new file mode 100755
index 0000000..aaac442
--- /dev/null
+++ b/third_party/pexpect/tests/swapcase_echo.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import sys, time
+while True:
+    x = raw_input ()
+    time.sleep(1) # without this delay the test would fail about 75% of the time. Why?
+    print(x.swapcase())
+    sys.stdout.flush()
diff --git a/third_party/pexpect/tests/test_FSM.py b/third_party/pexpect/tests/test_FSM.py
new file mode 100644
index 0000000..3425fd2
--- /dev/null
+++ b/third_party/pexpect/tests/test_FSM.py
@@ -0,0 +1,34 @@
+import io
+import sys
+import unittest
+try:
+    import builtins
+except ImportError:
+    import __builtin__ as builtins
+
+PY3 = (sys.version_info[0] >= 3)
+input_name = 'input' if PY3 else 'raw_input'
+
+from pexpect import FSM
+
+class FSMTestCase(unittest.TestCase):
+    def test_run_fsm(self):
+        def _input(prompt):
+            return "167 3 2 2 * * * 1 - ="
+        orig_input = getattr(builtins, input_name)
+        orig_stdout = sys.stdout
+        setattr(builtins, input_name, _input)
+        sys.stdout = sio = (io.StringIO if PY3 else io.BytesIO)()
+        
+        try:
+            FSM.main()
+        finally:
+            setattr(builtins, input_name, orig_input)
+            sys.stdout = orig_stdout
+        
+        printed = sio.getvalue()
+        assert '2003' in printed, printed
+        
+        
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file
diff --git a/third_party/pexpect/tests/test_ansi.py b/third_party/pexpect/tests/test_ansi.py
new file mode 100755
index 0000000..a9d445e
--- /dev/null
+++ b/third_party/pexpect/tests/test_ansi.py
@@ -0,0 +1,228 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+from pexpect import ANSI
+import unittest
+from . import PexpectTestCase
+import sys
+
+PY3 = (sys.version_info[0] >= 3)
+
+write_target = 'I\'ve got a ferret sticking up my nose.                           \n' +\
+'(He\'s got a ferret sticking up his nose.)                        \n' +\
+'How it got there I can\'t tell                                    \n' +\
+'But now it\'s there it hurts like hell                            \n' +\
+'And what is more it radically affects my sense of smell.         \n' +\
+'(His sense of smell.)                                            '
+
+write_text = 'I\'ve got a ferret sticking up my nose.\n' + \
+'(He\'s got a ferret sticking up his nose.)\n' + \
+'How it got there I can\'t tell\n' + \
+'But now it\'s there it hurts like hell\n' + \
+'And what is more it radically affects my sense of smell.\n' + \
+'(His sense of smell.)\n' + \
+'I can see a bare-bottomed mandril.\n' + \
+'(Slyly eyeing his other nostril.)\n' + \
+'If it jumps inside there too I really don\'t know what to do\n' + \
+'I\'ll be the proud posessor of a kind of nasal zoo.\n' + \
+'(A nasal zoo.)\n' + \
+'I\'ve got a ferret sticking up my nose.\n' + \
+'(And what is worst of all it constantly explodes.)\n' + \
+'"Ferrets don\'t explode," you say\n' + \
+'But it happened nine times yesterday\n' + \
+'And I should know for each time I was standing in the way.\n' + \
+'I\'ve got a ferret sticking up my nose.\n' + \
+'(He\'s got a ferret sticking up his nose.)\n' + \
+'How it got there I can\'t tell\n' + \
+'But now it\'s there it hurts like hell\n' + \
+'And what is more it radically affects my sense of smell.\n' + \
+'(His sense of smell.)'
+
+tetris_target='                           XX            XXXX    XX                             \n' +\
+'                           XXXXXX    XXXXXXXX    XX                             \n' +\
+'                           XXXXXX    XXXXXXXX    XX                             \n' +\
+'                           XX  XX    XX  XXXX    XX                             \n' +\
+'                           XXXXXX  XXXX  XXXX    XX                             \n' +\
+'                           XXXXXXXXXX    XXXX    XX                             \n' +\
+'                           XX  XXXXXX      XX    XX                             \n' +\
+'                           XXXXXX          XX    XX                             \n' +\
+'                           XXXX    XXXXXX  XX    XX                             \n' +\
+'                           XXXXXX    XXXX  XX    XX                             \n' +\
+'                           XX  XX    XXXX  XX    XX                             \n' +\
+'                           XX  XX      XX  XX    XX                             \n' +\
+'                           XX  XX    XXXX  XXXX  XX                             \n' +\
+'                           XXXXXXXX  XXXX  XXXX  XX                             \n' +\
+'                           XXXXXXXXXXXXXX  XXXXXXXX                             \n' +\
+'                           XX    XXXXXXXX  XX    XX                             \n' +\
+'                           XXXXXXXXXXXXXX  XX    XX                             \n' +\
+'                           XX  XXXX    XXXXXX    XX                             \n' +\
+'                           XXXXXX          XXXXXXXX                             \n' +\
+'                           XXXXXXXXXX      XX    XX                             \n' +\
+'                           XXXXXXXXXXXXXXXXXXXXXXXX                             \n' +\
+'                                                                                \n' +\
+'  J->LEFT  K->ROTATE  L->RIGHT  SPACE->DROP  P->PAUSE  Q->QUIT                  \n' +\
+'                                                                                '
+
+torture_target='+--------------------------------------------------------------------------------+\n' +\
+'|a`opqrs`      This is the       `srqpo`a                                        |\n' +\
+'|VT100 series Torture Test Demonstration.                                        |\n' +\
+'|VT100 series Torture Test Demonstration.                                        |\n' +\
+'|This is a normal line __________________________________________________y_      |\n' +\
+'|This is a bold line (normal unless the Advanced Video Option is installed)      |\n' +\
+'|This line is underlined _ "       "       "       "       "       "    _y_      |\n' +\
+'|This is a blinking line _ "       "       "       "       "       "    _y_      |\n' +\
+'|This is inverse video _ (underlined if no AVO and cursor is underline) _y_      |\n' +\
+'|Normal gjpqy Underline   Blink   Underline+Blink gjpqy                          |\n' +\
+'|Bold   gjpqy Underline   Blink   Underline+Blink gjpqy                          |\n' +\
+'|Inverse      Underline   Blink   Underline+Blink                                |\n' +\
+'|Bold+Inverse Underline   Blink   Underline+Blink                                |\n' +\
+'|This is double width                                                            |\n' +\
+'|This is double height                                                           |\n' +\
+'|This is double height                                                           |\n' +\
+'|_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ioy                                        |\n' +\
+'|_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ioy                                        |\n' +\
+'|_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ioy                                        |\n' +\
+'|`abcdefghijklmnopqrstuvwxyz{|}~ lqwqk                                           |\n' +\
+'|`abcdefghijklmnopqrstuvwxyz{|}~ tqnqu                                           |\n' +\
+'|`abcdefghijklmnopqrstuvwxyz{|}~ tqnqu                                           |\n' +\
+'|`abcdefghijklmnopqrstuvwxyz{|}~ mqvqj                                           |\n' +\
+'|   This test created by Joe Smith, 8-May-85                                     |\n' +\
+'|                                                                                |\n' +\
+'+--------------------------------------------------------------------------------+\n'
+
+class ansiTestCase (PexpectTestCase.PexpectTestCase):
+    def test_write (self):
+        s = ANSI.ANSI (6,65)
+        s.fill('.')
+        s.cursor_home()
+        for c in write_text:
+            s.write (c)
+        assert str(s) == write_target
+
+    def test_torturet (self):
+        s = ANSI.ANSI (24,80)
+        with open('torturet.vt') as f:
+            sample_text = f.read()
+        for c in sample_text:
+            s.process (c)
+        assert s.pretty() == torture_target, 'processed: \n' + s.pretty() + '\nexpected:\n' + torture_target
+
+    def test_tetris (self):
+        s = ANSI.ANSI (24,80)
+        with open('tetris.data') as f:
+            tetris_text = f.read()
+        for c in tetris_text:
+            s.process (c)
+        assert str(s) == tetris_target
+
+    def test_lines(self):
+        s = ANSI.ANSI(5, 5)
+        s.write('a'*6 + '\n')
+        s.write('ab\bcd\n')
+        s.write('ab\rcd\n')
+        assert str(s) == ('aaaaa\n'
+                          'a    \n'
+                          'acd  \n'
+                          'cd   \n'
+                          '     ')
+
+    def test_number_x(self):
+        """Test the FSM state used to handle more than 2 numeric parameters."""
+        class TestANSI(ANSI.ANSI):
+            captured_memory = None
+            def do_sgr(self, fsm):
+                assert self.captured_memory is None
+                self.captured_memory = fsm.memory
+
+        s = TestANSI(1, 20)
+        s.write('\x1b[0;1;32;45mtest')
+        assert str(s) == ('test                ')
+        assert s.captured_memory is not None
+        assert s.captured_memory == [s, '0', '1', '32', '45']
+
+    def test_fsm_memory(self):
+        """Test the FSM stack/memory does not have numbers left on it
+        after some sequences with numbers are passed in."""
+        s = ANSI.ANSI(1, 20)
+        s.write('\x1b[0;1;2;3m\x1b[4;5;6;7q\x1b[?8h\x1b[?9ltest')
+        assert str(s) == ('test                ')
+        assert s.state.memory == [s]
+
+    def test_utf8_bytes(self):
+        """Test that when bytes are passed in containing UTF-8 encoded
+        characters, where the encoding of each character consists of
+        multiple bytes, the characters are correctly decoded.
+        Incremental decoding is also tested."""
+        s = ANSI.ANSI(2, 10, encoding='utf-8')
+        # This is the UTF-8 encoding of the UCS character "HOURGLASS"
+        # followed by the UTF-8 encoding of the UCS character
+        # "KEYBOARD".  These characters can't be encoded in cp437 or
+        # latin-1.  The "KEYBOARD" character is split into two
+        # separate writes.
+        s.write(b'\xe2\x8c\x9b')
+        s.write(b'\xe2\x8c')
+        s.write(b'\xa8')
+        if PY3:
+            assert str(s) == u'\u231b\u2328        \n          '
+        else:
+            assert unicode(s) == u'\u231b\u2328        \n          '
+            assert str(s) == b'\xe2\x8c\x9b\xe2\x8c\xa8        \n          '
+        assert s.dump() == u'\u231b\u2328                  '
+        assert s.pretty() == u'+----------+\n|\u231b\u2328        |\n|          |\n+----------+\n'
+        assert s.get_abs(1, 1) == u'\u231b'
+        assert s.get_region(1, 1, 1, 5) == [u'\u231b\u2328   ']
+
+    def test_unicode(self):
+        """Test passing in of a unicode string."""
+        s = ANSI.ANSI(2, 10, encoding="utf-8")
+        s.write(u'\u231b\u2328')
+        if PY3:
+            assert str(s) == u'\u231b\u2328        \n          '
+        else:
+            assert unicode(s) == u'\u231b\u2328        \n          '
+            assert str(s) == b'\xe2\x8c\x9b\xe2\x8c\xa8        \n          '
+        assert s.dump() == u'\u231b\u2328                  '
+        assert s.pretty() == u'+----------+\n|\u231b\u2328        |\n|          |\n+----------+\n'
+        assert s.get_abs(1, 1) == u'\u231b'
+        assert s.get_region(1, 1, 1, 5) == [u'\u231b\u2328   ']
+
+    def test_decode_error(self):
+        """Test that default handling of decode errors replaces the
+        invalid characters."""
+        s = ANSI.ANSI(2, 10, encoding="ascii")
+        s.write(b'\xff') # a non-ASCII character
+        # In unicode, the non-ASCII character is replaced with
+        # REPLACEMENT CHARACTER.
+        if PY3:
+            assert str(s) == u'\ufffd         \n          '
+        else:
+            assert unicode(s) == u'\ufffd         \n          '
+            assert str(s) == b'?         \n          '
+        assert s.dump() == u'\ufffd                   '
+        assert s.pretty() == u'+----------+\n|\ufffd         |\n|          |\n+----------+\n'
+        assert s.get_abs(1, 1) == u'\ufffd'
+        assert s.get_region(1, 1, 1, 5) == [u'\ufffd    ']
+
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(ansiTestCase,'test')
+
diff --git a/third_party/pexpect/tests/test_async.py b/third_party/pexpect/tests/test_async.py
new file mode 100644
index 0000000..ce75572
--- /dev/null
+++ b/third_party/pexpect/tests/test_async.py
@@ -0,0 +1,51 @@
+try:
+    import asyncio
+except ImportError:
+    asyncio = None
+
+import sys
+import unittest
+
+import pexpect
+from .PexpectTestCase import PexpectTestCase
+
+def run(coro):
+    return asyncio.get_event_loop().run_until_complete(coro)
+
+ at unittest.skipIf(asyncio is None, "Requires asyncio")
+class AsyncTests(PexpectTestCase):
+    def test_simple_expect(self):
+        p = pexpect.spawn('cat')
+        p.sendline('Hello asyncio')
+        coro = p.expect(['Hello', pexpect.EOF] , async=True)
+        assert run(coro) == 0
+        print('Done')
+
+    def test_timeout(self):
+        p = pexpect.spawn('cat')
+        coro = p.expect('foo', timeout=1, async=True)
+        with self.assertRaises(pexpect.TIMEOUT):
+            run(coro)
+        
+        p = pexpect.spawn('cat')
+        coro = p.expect(['foo', pexpect.TIMEOUT], timeout=1, async=True)
+        assert run(coro) == 1
+
+    def test_eof(self):
+        p = pexpect.spawn('cat')
+        p.sendline('Hi')
+        coro = p.expect(pexpect.EOF, async=True)
+        p.sendeof()
+        assert run(coro) == 0
+
+        p = pexpect.spawn('cat')
+        p.sendeof()
+        coro = p.expect('Blah', async=True)
+        with self.assertRaises(pexpect.EOF):
+            run(coro)
+    
+    def test_expect_exact(self):
+        p = pexpect.spawn('%s list100.py' % sys.executable)
+        assert run(p.expect_exact(b'5', async=True)) == 0
+        assert run(p.expect_exact(['wpeok', b'11'], async=True)) == 1
+        assert run(p.expect_exact([b'foo', pexpect.EOF], async=True)) == 1
diff --git a/third_party/pexpect/tests/test_command_list_split.py b/third_party/pexpect/tests/test_command_list_split.py
new file mode 100755
index 0000000..370f46e
--- /dev/null
+++ b/third_party/pexpect/tests/test_command_list_split.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+from . import PexpectTestCase
+
+class SplitCommandLineTestCase(PexpectTestCase.PexpectTestCase):
+    def testSplitSizes(self):
+        assert len(pexpect.split_command_line(r'')) == 0
+        assert len(pexpect.split_command_line(r'one')) == 1
+        assert len(pexpect.split_command_line(r'one two')) == 2
+        assert len(pexpect.split_command_line(r'one  two')) == 2
+        assert len(pexpect.split_command_line(r'one   two')) == 2
+        assert len(pexpect.split_command_line(r'one\ one')) == 1
+        assert len(pexpect.split_command_line('\'one one\'')) == 1
+        assert len(pexpect.split_command_line(r'one\"one')) == 1
+        assert len(pexpect.split_command_line(r'This\' is a\'\ test')) == 3
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(SplitCommandLineTestCase,'test')
diff --git a/third_party/pexpect/tests/test_constructor.py b/third_party/pexpect/tests/test_constructor.py
new file mode 100755
index 0000000..98c473a
--- /dev/null
+++ b/third_party/pexpect/tests/test_constructor.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+from . import PexpectTestCase
+
+class TestCaseConstructor(PexpectTestCase.PexpectTestCase):
+    def test_constructor (self):
+        '''This tests that the constructor will work and give
+        the same results for different styles of invoking __init__().
+        This assumes that the root directory / is static during the test.
+        '''
+        p1 = pexpect.spawn('uname -m -n -p -r -s -v')
+        p2 = pexpect.spawn('uname', ['-m', '-n', '-p', '-r', '-s', '-v'])
+        p1.expect(pexpect.EOF)
+        p2.expect(pexpect.EOF)
+        assert p1.before == p2.before
+
+    def test_named_parameters (self):
+        '''This tests that named parameters work.
+        '''
+        p = pexpect.spawn ('/bin/ls',timeout=10)
+        p = pexpect.spawn (timeout=10, command='/bin/ls')
+        p = pexpect.spawn (args=[], command='/bin/ls')
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(TestCaseConstructor,'test')
+
diff --git a/third_party/pexpect/tests/test_ctrl_chars.py b/third_party/pexpect/tests/test_ctrl_chars.py
new file mode 100755
index 0000000..10d03db
--- /dev/null
+++ b/third_party/pexpect/tests/test_ctrl_chars.py
@@ -0,0 +1,125 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+from __future__ import print_function
+
+import pexpect
+import unittest
+from . import PexpectTestCase
+import time
+import sys
+
+from ptyprocess import ptyprocess
+ptyprocess._make_eof_intr()
+
+if sys.version_info[0] >= 3:
+    def byte(i):
+        return bytes([i])
+else:
+    byte = chr
+
+class TestCtrlChars(PexpectTestCase.PexpectTestCase):
+
+    def test_control_chars(self):
+        '''This tests that we can send all 256 8-bit characters to a child
+        process.'''
+        child = pexpect.spawn('python getch.py', echo=False, timeout=5)
+        child.expect('READY')
+        for i in range(1, 256):
+            child.send(byte(i))
+            child.expect ('%d<STOP>' % (i,))
+
+        # This needs to be last, as getch.py exits on \x00
+        child.send(byte(0))
+        child.expect('0<STOP>')
+        child.expect(pexpect.EOF)
+        assert not child.isalive()
+        assert child.exitstatus == 0
+
+    def test_sendintr (self):
+        child = pexpect.spawn('python getch.py', echo=False, timeout=5)
+        child.expect('READY')
+        child.sendintr()
+        child.expect(str(ord(ptyprocess._INTR)) + '<STOP>')
+
+        child.send(byte(0))
+        child.expect('0<STOP>')
+        child.expect(pexpect.EOF)
+        assert not child.isalive()
+        assert child.exitstatus == 0
+
+    def test_sendeof(self):
+        child = pexpect.spawn('python getch.py', echo=False, timeout=5)
+        child.expect('READY')
+        child.sendeof()
+        child.expect(str(ord(ptyprocess._EOF)) + '<STOP>')
+
+        child.send(byte(0))
+        child.expect('0<STOP>')
+        child.expect(pexpect.EOF)
+        assert not child.isalive()
+        assert child.exitstatus == 0
+
+    def test_bad_sendcontrol_chars (self):
+        '''This tests that sendcontrol will return 0 for an unknown char. '''
+
+        child = pexpect.spawn('python getch.py', echo=False, timeout=5)
+        child.expect('READY')
+        assert 0 == child.sendcontrol('1')
+
+    def test_sendcontrol(self):
+        '''This tests that we can send all special control codes by name.
+        '''
+        child = pexpect.spawn('python getch.py', echo=False, timeout=5)
+        child.expect('READY')
+        for ctrl in 'abcdefghijklmnopqrstuvwxyz':
+            assert child.sendcontrol(ctrl) == 1
+            val = ord(ctrl) - ord('a') + 1
+            child.expect_exact(str(val)+'<STOP>')
+
+        # escape character
+        assert child.sendcontrol('[') == 1
+        child.expect('27<STOP>')
+        assert child.sendcontrol('\\') == 1
+        child.expect('28<STOP>')
+        # telnet escape character
+        assert child.sendcontrol(']') == 1
+        child.expect('29<STOP>')
+        assert child.sendcontrol('^') == 1
+        child.expect('30<STOP>')
+        # irc protocol uses this to underline ...
+        assert child.sendcontrol('_') == 1
+        child.expect('31<STOP>')
+        # the real "backspace is delete"
+        assert child.sendcontrol('?') == 1
+        child.expect('127<STOP>')
+
+        # NUL, same as ctrl + ' '
+        assert child.sendcontrol('@') == 1
+        child.expect('0<STOP>')
+        child.expect(pexpect.EOF)
+        assert not child.isalive()
+        assert child.exitstatus == 0
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(TestCtrlChars,'test')
+
diff --git a/third_party/pexpect/tests/test_destructor.py b/third_party/pexpect/tests/test_destructor.py
new file mode 100755
index 0000000..d27b6f6
--- /dev/null
+++ b/third_party/pexpect/tests/test_destructor.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+from . import PexpectTestCase
+import gc
+import platform
+import time
+
+class TestCaseDestructor(PexpectTestCase.PexpectTestCase):
+    def test_destructor (self):
+        if platform.python_implementation() != 'CPython':
+            # Details of garbage collection are different on other implementations
+            return 'SKIP'
+        gc.collect()
+        time.sleep(3)
+        p1 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p2 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p3 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p4 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        fd_t1 = (p1.child_fd,p2.child_fd,p3.child_fd,p4.child_fd)
+        p1.expect(pexpect.EOF)
+        p2.expect(pexpect.EOF)
+        p3.expect(pexpect.EOF)
+        p4.expect(pexpect.EOF)
+        p1.kill(9)
+        p2.kill(9)
+        p3.kill(9)
+        p4.kill(9)
+        p1 = None
+        p2 = None
+        p3 = None
+        p4 = None
+        gc.collect()
+        time.sleep(3) # Some platforms are slow at gc... Solaris!
+
+        p1 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p2 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p3 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p4 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        fd_t2 = (p1.child_fd,p2.child_fd,p3.child_fd,p4.child_fd)
+        p1.kill(9)
+        p2.kill(9)
+        p3.kill(9)
+        p4.kill(9)
+        del (p1)
+        del (p2)
+        del (p3)
+        del (p4)
+        gc.collect()
+        time.sleep(3)
+
+        p1 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p2 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p3 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        p4 = pexpect.spawn('%s hello_world.py' % self.PYTHONBIN)
+        fd_t3 = (p1.child_fd,p2.child_fd,p3.child_fd,p4.child_fd)
+
+        assert (fd_t1 == fd_t2 == fd_t3), "pty file descriptors not properly garbage collected (fd_t1,fd_t2,fd_t3)=(%s,%s,%s)" % (str(fd_t1),str(fd_t2),str(fd_t3))
+
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(TestCaseDestructor,'test')
+
diff --git a/third_party/pexpect/tests/test_dotall.py b/third_party/pexpect/tests/test_dotall.py
new file mode 100755
index 0000000..68aef3f
--- /dev/null
+++ b/third_party/pexpect/tests/test_dotall.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+import re
+from . import PexpectTestCase
+
+testdata = 'BEGIN\nHello world\nEND'
+class TestCaseDotall(PexpectTestCase.PexpectTestCase):
+    def test_dotall (self):
+        p = pexpect.spawn('echo "%s"' % testdata)
+        i = p.expect ([b'BEGIN(.*)END', pexpect.EOF])
+        assert i==0, 'DOTALL does not seem to be working.'
+
+    def test_precompiled (self):
+        p = pexpect.spawn('echo "%s"' % testdata)
+        pat = re.compile(b'BEGIN(.*)END') # This overrides the default DOTALL.
+        i = p.expect ([pat, pexpect.EOF])
+        assert i==1, 'Precompiled pattern to override DOTALL does not seem to be working.'
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(TestCaseDotall,'test')
+
diff --git a/third_party/pexpect/tests/test_expect.py b/third_party/pexpect/tests/test_expect.py
new file mode 100755
index 0000000..3f4c9d8
--- /dev/null
+++ b/third_party/pexpect/tests/test_expect.py
@@ -0,0 +1,583 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import multiprocessing
+import unittest
+import subprocess
+import time
+import signal
+import sys
+import os
+
+import pexpect
+from . import PexpectTestCase
+from .utils import no_coverage_env
+
+# Many of these test cases blindly assume that sequential directory
+# listings of the /bin directory will yield the same results.
+# This may not be true, but seems adequate for testing now.
+# I should fix this at some point.
+
+FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
+def hex_dump(src, length=16):
+    result=[]
+    for i in xrange(0, len(src), length):
+       s = src[i:i+length]
+       hexa = ' '.join(["%02X"%ord(x) for x in s])
+       printable = s.translate(FILTER)
+       result.append("%04X   %-*s   %s\n" % (i, length*3, hexa, printable))
+    return ''.join(result)
+
+def hex_diff(left, right):
+        diff = ['< %s\n> %s' % (_left, _right,) for _left, _right in zip(
+            hex_dump(left).splitlines(), hex_dump(right).splitlines())
+            if _left != _right]
+        return '\n' + '\n'.join(diff,)
+
+
+class ExpectTestCase (PexpectTestCase.PexpectTestCase):
+
+    def test_expect_basic (self):
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        p.sendline (b'Hello')
+        p.sendline (b'there')
+        p.sendline (b'Mr. Python')
+        p.expect (b'Hello')
+        p.expect (b'there')
+        p.expect (b'Mr. Python')
+        p.sendeof ()
+        p.expect (pexpect.EOF)
+
+    def test_expect_exact_basic (self):
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        p.sendline (b'Hello')
+        p.sendline (b'there')
+        p.sendline (b'Mr. Python')
+        p.expect_exact (b'Hello')
+        p.expect_exact (b'there')
+        p.expect_exact (b'Mr. Python')
+        p.sendeof ()
+        p.expect_exact (pexpect.EOF)
+
+    def test_expect_ignore_case(self):
+        '''This test that the ignorecase flag will match patterns
+        even if case is different using the regex (?i) directive.
+        '''
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        p.sendline (b'HELLO')
+        p.sendline (b'there')
+        p.expect (b'(?i)hello')
+        p.expect (b'(?i)THERE')
+        p.sendeof ()
+        p.expect (pexpect.EOF)
+
+    def test_expect_ignore_case_flag(self):
+        '''This test that the ignorecase flag will match patterns
+        even if case is different using the ignorecase flag.
+        '''
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        p.ignorecase = True
+        p.sendline (b'HELLO')
+        p.sendline (b'there')
+        p.expect (b'hello')
+        p.expect (b'THERE')
+        p.sendeof ()
+        p.expect (pexpect.EOF)
+
+    def test_expect_order (self):
+        '''This tests that patterns are matched in the same order as given in the pattern_list.
+
+        (Or does it?  Doesn't it also pass if expect() always chooses
+        (one of the) the leftmost matches in the input? -- grahn)
+        ... agreed! -jquast, the buffer ptr isn't forwarded on match, see first two test cases
+        '''
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        self._expect_order(p)
+
+    def test_expect_order_exact (self):
+        '''Like test_expect_order(), but using expect_exact().
+        '''
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        p.expect = p.expect_exact
+        self._expect_order(p)
+
+    def _expect_order (self, p):
+        p.sendline (b'1234')
+        p.sendline (b'abcd')
+        p.sendline (b'wxyz')
+        p.sendline (b'7890')
+        p.sendeof ()
+        index = p.expect ([
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF,
+            b'7890' ])
+        assert index == 0, (index, p.before, p.after)
+        index = p.expect ([
+            b'54321',
+            pexpect.TIMEOUT,
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF], timeout=5)
+        assert index == 3, (index, p.before, p.after)
+        index = p.expect ([
+            b'54321',
+            pexpect.TIMEOUT,
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF], timeout=5)
+        assert index == 4, (index, p.before, p.after)
+        index = p.expect ([
+            pexpect.EOF,
+            b'abcd',
+            b'wxyz',
+            b'7890' ])
+        assert index == 3, (index, p.before, p.after)
+
+        index = p.expect ([
+            b'abcd',
+            b'wxyz',
+            b'7890',
+            pexpect.EOF])
+        assert index == 3, (index, p.before, p.after)
+
+    def test_expect_setecho_off(self):
+        '''This tests that echo may be toggled off.
+        '''
+        p = pexpect.spawn('cat', echo=True, timeout=5)
+        try:
+            self._expect_echo_toggle(p)
+        except IOError:
+            if sys.platform.lower().startswith('sunos'):
+                if hasattr(unittest, 'SkipTest'):
+                    raise unittest.SkipTest("Not supported on this platform.")
+                return 'skip'
+            raise
+
+    def test_expect_setecho_off_exact(self):
+        p = pexpect.spawn('cat', echo=True, timeout=5)
+        p.expect = p.expect_exact
+        try:
+            self._expect_echo_toggle(p)
+        except IOError:
+            if sys.platform.lower().startswith('sunos'):
+                if hasattr(unittest, 'SkipTest'):
+                    raise unittest.SkipTest("Not supported on this platform.")
+                return 'skip'
+            raise
+
+    def test_waitnoecho(self):
+        " Tests setecho(False) followed by waitnoecho() "
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        try:
+            p.setecho(False)
+            p.waitnoecho()
+        except IOError:
+            if sys.platform.lower().startswith('sunos'):
+                if hasattr(unittest, 'SkipTest'):
+                    raise unittest.SkipTest("Not supported on this platform.")
+                return 'skip'
+            raise
+
+    def test_waitnoecho_order(self):
+
+        ''' This tests that we can wait on a child process to set echo mode.
+        For example, this tests that we could wait for SSH to set ECHO False
+        when asking of a password. This makes use of an external script
+        echo_wait.py. '''
+
+        p1 = pexpect.spawn('%s echo_wait.py' % self.PYTHONBIN)
+        start = time.time()
+        try:
+            p1.waitnoecho(timeout=10)
+        except IOError:
+            if sys.platform.lower().startswith('sunos'):
+                if hasattr(unittest, 'SkipTest'):
+                    raise unittest.SkipTest("Not supported on this platform.")
+                return 'skip'
+            raise
+
+
+        end_time = time.time() - start
+        assert end_time < 10 and end_time > 2, "waitnoecho did not set ECHO off in the expected window of time."
+
+        # test that we actually timeout and return False if ECHO is never set off.
+        p1 = pexpect.spawn('cat')
+        start = time.time()
+        retval = p1.waitnoecho(timeout=4)
+        end_time = time.time() - start
+        assert end_time > 3, "waitnoecho should have waited longer than 2 seconds. retval should be False, retval=%d"%retval
+        assert retval==False, "retval should be False, retval=%d"%retval
+
+        # This one is mainly here to test default timeout for code coverage.
+        p1 = pexpect.spawn('%s echo_wait.py' % self.PYTHONBIN)
+        start = time.time()
+        p1.waitnoecho()
+        end_time = time.time() - start
+        assert end_time < 10, "waitnoecho did not set ECHO off in the expected window of time."
+
+    def test_expect_echo (self):
+        '''This tests that echo is on by default.
+        '''
+        p = pexpect.spawn('cat', echo=True, timeout=5)
+        self._expect_echo(p)
+
+    def test_expect_echo_exact (self):
+        '''Like test_expect_echo(), but using expect_exact().
+        '''
+        p = pexpect.spawn('cat', echo=True, timeout=5)
+        p.expect = p.expect_exact
+        self._expect_echo(p)
+
+    def _expect_echo (self, p):
+        p.sendline (b'1234') # Should see this twice (once from tty echo and again from cat).
+        index = p.expect ([
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF,
+            pexpect.TIMEOUT])
+        assert index == 0, "index="+str(index)+"\n"+p.before
+        index = p.expect ([
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF])
+        assert index == 0, "index="+str(index)
+
+    def _expect_echo_toggle(self, p):
+        p.sendline (b'1234') # Should see this twice (once from tty echo and again from cat).
+        index = p.expect ([
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF,
+            pexpect.TIMEOUT])
+        assert index == 0, "index="+str(index)+"\n"+p.before
+        index = p.expect ([
+            b'1234',
+            b'abcd',
+            b'wxyz',
+            pexpect.EOF])
+        assert index == 0, "index="+str(index)
+        p.setecho(0) # Turn off tty echo
+        p.waitnoecho()
+        p.sendline (b'abcd') # Now, should only see this once.
+        p.sendline (b'wxyz') # Should also be only once.
+        index = p.expect ([
+            pexpect.EOF,
+            pexpect.TIMEOUT,
+            b'abcd',
+            b'wxyz',
+            b'1234'])
+        assert index == 2, "index="+str(index)
+        index = p.expect ([
+            pexpect.EOF,
+            b'abcd',
+            b'wxyz',
+            b'7890'])
+        assert index == 2, "index="+str(index)
+        p.setecho(1) # Turn on tty echo
+        p.sendline (b'7890') # Should see this twice.
+        index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890'])
+        assert index == 3, "index="+str(index)
+        index = p.expect ([pexpect.EOF,b'abcd',b'wxyz',b'7890'])
+        assert index == 3, "index="+str(index)
+        p.sendeof()
+
+    def test_expect_index (self):
+        '''This tests that mixed list of regex strings, TIMEOUT, and EOF all
+        return the correct index when matched.
+        '''
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        self._expect_index(p)
+
+    def test_expect_index_exact (self):
+        '''Like test_expect_index(), but using expect_exact().
+        '''
+        p = pexpect.spawn('cat', echo=False, timeout=5)
+        p.expect = p.expect_exact
+        self._expect_index(p)
+
+    def _expect_index (self, p):
+        p.sendline (b'1234')
+        index = p.expect ([b'abcd',b'wxyz',b'1234',pexpect.EOF])
+        assert index == 2, "index="+str(index)
+        p.sendline (b'abcd')
+        index = p.expect ([pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF])
+        assert index == 1, "index="+str(index)+str(p)
+        p.sendline (b'wxyz')
+        index = p.expect ([b'54321',pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF])
+        assert index == 3, "index="+str(index) # Expect 'wxyz'
+        p.sendline (b'$*!@?')
+        index = p.expect ([b'54321',pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF],
+                          timeout=1)
+        assert index == 1, "index="+str(index) # Expect TIMEOUT
+        p.sendeof ()
+        index = p.expect ([b'54321',pexpect.TIMEOUT,b'abcd',b'wxyz',b'1234',pexpect.EOF])
+        assert index == 5, "index="+str(index) # Expect EOF
+
+    def test_expect (self):
+        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
+                stdout=subprocess.PIPE).communicate()[0].rstrip()
+        p = pexpect.spawn('ls -l /bin')
+        the_new_way = b''
+        while 1:
+            i = p.expect ([b'\n', pexpect.EOF])
+            the_new_way = the_new_way + p.before
+            if i == 1:
+                break
+        the_new_way = the_new_way.rstrip()
+        the_new_way = the_new_way.replace(b'\r\n', b'\n'
+                ).replace(b'\r', b'\n').replace(b'\n\n', b'\n').rstrip()
+        the_old_way = the_old_way.replace(b'\r\n', b'\n'
+                ).replace(b'\r', b'\n').replace(b'\n\n', b'\n').rstrip()
+        assert the_old_way == the_new_way, hex_diff(the_old_way, the_new_way)
+
+    def test_expect_exact (self):
+        the_old_way = subprocess.Popen(args=['ls', '-l', '/bin'],
+                stdout=subprocess.PIPE).communicate()[0].rstrip()
+        p = pexpect.spawn('ls -l /bin')
+        the_new_way = b''
+        while 1:
+            i = p.expect_exact ([b'\n', pexpect.EOF])
+            the_new_way = the_new_way + p.before
+            if i == 1:
+                break
+        the_new_way = the_new_way.replace(b'\r\n', b'\n'
+                ).replace(b'\r', b'\n').replace(b'\n\n', b'\n').rstrip()
+        the_old_way = the_old_way.replace(b'\r\n', b'\n'
+                ).replace(b'\r', b'\n').replace(b'\n\n', b'\n').rstrip()
+        assert the_old_way == the_new_way, hex_diff(the_old_way, the_new_way)
+        p = pexpect.spawn('echo hello.?world')
+        i = p.expect_exact(b'.?')
+        self.assertEqual(p.before, b'hello')
+        self.assertEqual(p.after, b'.?')
+
+    def test_expect_eof (self):
+        the_old_way = subprocess.Popen(args=['/bin/ls', '-l', '/bin'],
+                stdout=subprocess.PIPE).communicate()[0].rstrip()
+        p = pexpect.spawn('/bin/ls -l /bin')
+        p.expect(pexpect.EOF) # This basically tells it to read everything. Same as pexpect.run() function.
+        the_new_way = p.before
+        the_new_way = the_new_way.replace(b'\r\n', b'\n'
+                ).replace(b'\r', b'\n').replace(b'\n\n', b'\n').rstrip()
+        the_old_way = the_old_way.replace(b'\r\n', b'\n'
+                ).replace(b'\r', b'\n').replace(b'\n\n', b'\n').rstrip()
+        assert the_old_way == the_new_way, hex_diff(the_old_way, the_new_way)
+
+    def test_expect_timeout (self):
+        p = pexpect.spawn('cat', timeout=5)
+        p.expect(pexpect.TIMEOUT) # This tells it to wait for timeout.
+        self.assertEqual(p.after, pexpect.TIMEOUT)
+
+    def test_unexpected_eof (self):
+        p = pexpect.spawn('ls -l /bin')
+        try:
+            p.expect('_Z_XY_XZ') # Probably never see this in ls output.
+        except pexpect.EOF:
+            pass
+        else:
+            self.fail ('Expected an EOF exception.')
+
+    def _before_after(self, p):
+        p.timeout = 5
+
+        p.expect(b'5')
+        self.assertEqual(p.after, b'5')
+        assert p.before.startswith(b'[0, 1, 2'), p.before
+
+        p.expect(b'50')
+        self.assertEqual(p.after, b'50')
+        assert p.before.startswith(b', 6, 7, 8'), p.before[:20]
+        assert p.before.endswith(b'48, 49, '), p.before[-20:]
+
+        p.expect(pexpect.EOF)
+        self.assertEqual(p.after, pexpect.EOF)
+        assert p.before.startswith(b', 51, 52'), p.before[:20]
+        assert p.before.endswith(b', 99]\r\n'), p.before[-20:]
+
+    def test_before_after(self):
+        '''This tests expect() for some simple before/after things.
+        '''
+        p = pexpect.spawn('%s -Wi list100.py' % self.PYTHONBIN, env=no_coverage_env())
+        self._before_after(p)
+
+    def test_before_after_exact(self):
+        '''This tests some simple before/after things, for
+        expect_exact(). (Grahn broke it at one point.)
+        '''
+        p = pexpect.spawn('%s -Wi list100.py' % self.PYTHONBIN, env=no_coverage_env())
+        # mangle the spawn so we test expect_exact() instead
+        p.expect = p.expect_exact
+        self._before_after(p)
+
+    def _ordering(self, p):
+        p.timeout = 5
+        p.expect(b'>>> ')
+
+        p.sendline('list(range(4*3))')
+        self.assertEqual(p.expect([b'5,', b'5,']), 0)
+        p.expect(b'>>> ')
+
+        p.sendline(b'list(range(4*3))')
+        self.assertEqual(p.expect([b'7,', b'5,']), 1)
+        p.expect(b'>>> ')
+
+        p.sendline(b'list(range(4*3))')
+        self.assertEqual(p.expect([b'5,', b'7,']), 0)
+        p.expect(b'>>> ')
+
+        p.sendline(b'list(range(4*5))')
+        self.assertEqual(p.expect([b'2,', b'12,']), 0)
+        p.expect(b'>>> ')
+
+        p.sendline(b'list(range(4*5))')
+        self.assertEqual(p.expect([b'12,', b'2,']), 1)
+
+    def test_ordering(self):
+        '''This tests expect() for which pattern is returned
+        when many may eventually match. I (Grahn) am a bit
+        confused about what should happen, but this test passes
+        with pexpect 2.1.
+        '''
+        p = pexpect.spawn(self.PYTHONBIN)
+        self._ordering(p)
+
+    def test_ordering_exact(self):
+        '''This tests expect_exact() for which pattern is returned
+        when many may eventually match. I (Grahn) am a bit
+        confused about what should happen, but this test passes
+        for the expect() method with pexpect 2.1.
+        '''
+        p = pexpect.spawn(self.PYTHONBIN)
+        # mangle the spawn so we test expect_exact() instead
+        p.expect = p.expect_exact
+        self._ordering(p)
+
+    def _greed(self, expect):
+        # End at the same point: the one with the earliest start should win
+        self.assertEqual(expect([b'3, 4', b'2, 3, 4']), 1)
+
+        # Start at the same point: first pattern passed wins
+        self.assertEqual(expect([b'5,', b'5, 6']), 0)
+
+        # Same pattern passed twice: first instance wins
+        self.assertEqual(expect([b'7, 8', b'7, 8, 9', b'7, 8']), 0)
+
+    def _greed_read1(self, expect):
+        # Here, one has an earlier start and a later end. When processing
+        # one character at a time, the one that finishes first should win,
+        # because we don't know about the other match when it wins.
+        # If maxread > 1, this behaviour is currently undefined, although in
+        # most cases the one that starts first will win.
+        self.assertEqual(expect([b'1, 2, 3', b'2,']), 1)
+
+    def test_greed(self):
+        p = pexpect.spawn(self.PYTHONBIN + ' list100.py')
+        self._greed(p.expect)
+
+        p = pexpect.spawn(self.PYTHONBIN + ' list100.py', maxread=1)
+        self._greed_read1(p.expect)
+
+    def test_greed_exact(self):
+        p = pexpect.spawn(self.PYTHONBIN + ' list100.py')
+        self._greed(p.expect_exact)
+
+        p = pexpect.spawn(self.PYTHONBIN + ' list100.py', maxread=1)
+        self._greed_read1(p.expect_exact)
+
+    def test_bad_arg(self):
+        p = pexpect.spawn('cat')
+        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
+            p.expect(1)
+        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
+            p.expect([1, b'2'])
+        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
+            p.expect_exact(1)
+        with self.assertRaisesRegexp(TypeError, '.*must be one of'):
+            p.expect_exact([1, b'2'])
+
+    def test_timeout_none(self):
+        p = pexpect.spawn('echo abcdef', timeout=None)
+        p.expect('abc')
+        p.expect_exact('def')
+        p.expect(pexpect.EOF)
+
+    def test_signal_handling(self):
+        '''
+            This tests the error handling of a signal interrupt (usually a
+            SIGWINCH generated when a window is resized), but in this test, we
+            are substituting an ALARM signal as this is much easier for testing
+            and is treated the same as a SIGWINCH.
+
+            To ensure that the alarm fires during the expect call, we are
+            setting the signal to alarm after 1 second while the spawned process
+            sleeps for 2 seconds prior to sending the expected output.
+        '''
+        def noop(x, y):
+            pass
+        signal.signal(signal.SIGALRM, noop)
+
+        p1 = pexpect.spawn('%s sleep_for.py 2' % self.PYTHONBIN, timeout=5)
+        p1.expect('READY')
+        signal.alarm(1)
+        p1.expect('END')
+
+    def test_stdin_closed(self):
+        '''
+        Ensure pexpect continues to operate even when stdin is closed
+        '''
+        class Closed_stdin_proc(multiprocessing.Process):
+            def run(self):
+                sys.__stdin__.close()
+                cat = pexpect.spawn('cat')
+                cat.sendeof()
+                cat.expect(pexpect.EOF)
+
+        proc = Closed_stdin_proc()
+        proc.start()
+        proc.join()
+        assert proc.exitcode == 0
+
+    def test_stdin_stdout_closed(self):
+        '''
+        Ensure pexpect continues to operate even when stdin and stdout is closed
+        '''
+        class Closed_stdin_stdout_proc(multiprocessing.Process):
+            def run(self):
+                sys.__stdin__.close()
+                sys.__stdout__.close()
+                cat = pexpect.spawn('cat')
+                cat.sendeof()
+                cat.expect(pexpect.EOF)
+
+        proc = Closed_stdin_stdout_proc()
+        proc.start()
+        proc.join()
+        assert proc.exitcode == 0
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(ExpectTestCase, 'test')
diff --git a/third_party/pexpect/tests/test_filedescriptor.py b/third_party/pexpect/tests/test_filedescriptor.py
new file mode 100755
index 0000000..d9164e1
--- /dev/null
+++ b/third_party/pexpect/tests/test_filedescriptor.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+from pexpect import fdpexpect
+import unittest
+from . import PexpectTestCase
+import os
+
+class ExpectTestCase(PexpectTestCase.PexpectTestCase):
+    def setUp(self):
+        print(self.id())
+        PexpectTestCase.PexpectTestCase.setUp(self)
+
+    def test_fd (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = fdpexpect.fdspawn (fd)
+        s.expect(b'This is the end of test data:')
+        s.expect(pexpect.EOF)
+        self.assertEqual(s.before, b' END\n')
+
+    def test_maxread (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = fdpexpect.fdspawn (fd)
+        s.maxread = 100
+        s.expect('2')
+        s.expect ('This is the end of test data:')
+        s.expect (pexpect.EOF)
+        self.assertEqual(s.before, b' END\n')
+
+    def test_fd_isalive (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = fdpexpect.fdspawn(fd)
+        assert s.isalive()
+        os.close(fd)
+        assert not s.isalive(), "Should not be alive after close()"
+
+    def test_fd_isatty (self):
+        fd = os.open ('TESTDATA.txt', os.O_RDONLY)
+        s = fdpexpect.fdspawn (fd)
+        assert not s.isatty()
+        s.close()
+
+    def test_fileobj(self):
+        f = open('TESTDATA.txt', 'r')
+        s = fdpexpect.fdspawn(f)  # Should get the fileno from the file handle
+        s.expect('2')
+        s.close()
+        assert not s.isalive()
+        s.close()  # Smoketest - should be able to call this again
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(ExpectTestCase, 'test')
diff --git a/third_party/pexpect/tests/test_interact.py b/third_party/pexpect/tests/test_interact.py
new file mode 100755
index 0000000..06fc44a
--- /dev/null
+++ b/third_party/pexpect/tests/test_interact.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import os
+import pexpect
+import unittest
+import sys
+from . import PexpectTestCase
+
+
+class InteractTestCase (PexpectTestCase.PexpectTestCase):
+    def setUp(self):
+        super(InteractTestCase, self).setUp()
+        self.env = env = os.environ.copy()
+
+        # Ensure 'import pexpect' works in subprocess interact*.py
+        if 'PYTHONPATH' in env:
+            env['PYTHONPATH'] = os.pathsep.join((self.project_dir,
+                                                    env['PYTHONPATH']))
+        else:
+            env['PYTHONPATH'] = self.project_dir
+
+        self.interact_py = ' '.join((sys.executable,
+                                     'interact.py',))
+        self.interact_ucs_py = ' '.join((sys.executable,
+                                         'interact_unicode.py',))
+
+    def test_interact_escape(self):
+        " Ensure `escape_character' value exits interactive mode. "
+        p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)
+        p.expect('<in >')
+        p.sendcontrol(']')  # chr(29), the default `escape_character'
+                            # value of pexpect.interact().
+        p.expect_exact('Escaped interact')
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+        assert p.exitstatus == 0
+
+    def test_interact_spawn_eof(self):
+        " Ensure subprocess receives EOF and exit. "
+        p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)
+        p.expect('<in >')
+        p.sendline(b'alpha')
+        p.sendline(b'beta')
+        p.expect(b'<out>alpha')
+        p.expect(b'<out>beta')
+        p.sendeof()
+        p.expect_exact('<eof>')
+        p.expect_exact('Escaped interact')
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+        assert p.exitstatus == 0
+
+    def test_interact_spawnu_eof(self):
+        " Ensure subprocess receives unicode, EOF, and exit. "
+        p = pexpect.spawnu(self.interact_ucs_py, timeout=5, env=self.env)
+        p.expect('<in >')
+        p.sendline('É‘lpha')
+        p.sendline('Î’eta')
+        p.expect('<out>É‘lpha')
+        p.expect('<out>Î’eta')
+        p.sendeof()
+        p.expect_exact('<eof>')
+        p.expect_exact('Escaped interact')
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+        assert p.exitstatus == 0
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(InteractTestCase, 'test')
+
diff --git a/third_party/pexpect/tests/test_isalive.py b/third_party/pexpect/tests/test_isalive.py
new file mode 100755
index 0000000..5168a52
--- /dev/null
+++ b/third_party/pexpect/tests/test_isalive.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+import signal
+import sys
+import time
+from . import PexpectTestCase
+
+class IsAliveTestCase(PexpectTestCase.PexpectTestCase):
+
+    def test_expect_wait (self):
+        '''This tests that calling wait on a finished process works as expected.
+        '''
+        p = pexpect.spawn('sleep 3')
+        assert p.isalive()
+        p.wait()
+        assert not p.isalive()
+
+        p = pexpect.spawn('sleep 3')
+        assert p.isalive()
+        p.kill(9)
+        time.sleep(1)
+        with self.assertRaises(pexpect.ExceptionPexpect):
+            p.wait()
+
+    def test_signal_wait(self):
+        '''Test calling wait with a process terminated by a signal.'''
+        if not hasattr(signal, 'SIGALRM'):
+            return 'SKIP'
+        p = pexpect.spawn(sys.executable, ['alarm_die.py'])
+        p.wait()
+        assert p.exitstatus is None
+        self.assertEqual(p.signalstatus, signal.SIGALRM)
+
+    def test_expect_isalive_dead_after_normal_termination (self):
+        p = pexpect.spawn('ls', timeout=15)
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+
+    def test_expect_isalive_dead_after_SIGHUP(self):
+        p = pexpect.spawn('cat', timeout=5, ignore_sighup=False)
+        assert p.isalive()
+        force = False
+        if sys.platform.lower().startswith('sunos'):
+            # On Solaris (SmartOs), and only when executed from cron(1), SIGKILL
+            # is required to end the sub-process. This is done using force=True
+            force = True
+        assert p.terminate(force) == True
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+
+    def test_expect_isalive_dead_after_SIGINT(self):
+        p = pexpect.spawn('cat', timeout=5)
+        assert p.isalive()
+        force = False
+        if sys.platform.lower().startswith('sunos'):
+            # On Solaris (SmartOs), and only when executed from cron(1), SIGKILL
+            # is required to end the sub-process. This is done using force=True
+            force = True
+        assert p.terminate(force) == True
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+
+    def test_expect_isalive_dead_after_SIGKILL(self):
+        p = pexpect.spawn('cat', timeout=5)
+        assert p.isalive()
+        p.kill(9)
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+
+    def test_forced_terminate(self):
+        p = pexpect.spawn(sys.executable, ['needs_kill.py'])
+        p.expect('READY')
+        assert p.terminate(force=True) == True
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+
+### Some platforms allow this. Some reset status after call to waitpid.
+### probably not necessary, isalive() returns early when terminate is False.
+    def test_expect_isalive_consistent_multiple_calls (self):
+        '''This tests that multiple calls to isalive() return same value.
+        '''
+        p = pexpect.spawn('cat')
+        assert p.isalive()
+        assert p.isalive()
+        p.kill(9)
+        p.expect(pexpect.EOF)
+        assert not p.isalive()
+        assert not p.isalive()
+
+if __name__ == '__main__':
+    unittest.main()
+
+suite = unittest.makeSuite(IsAliveTestCase, 'test')
+
diff --git a/third_party/pexpect/tests/test_log.py b/third_party/pexpect/tests/test_log.py
new file mode 100755
index 0000000..4ad2256
--- /dev/null
+++ b/third_party/pexpect/tests/test_log.py
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+'''
+PEXPECT LICENSE
+
+    This license is approved by the OSI and FSF as GPL-compatible.
+        http://opensource.org/licenses/isc-license.txt
+
+    Copyright (c) 2012, Noah Spurrier <noah at noah.org>
+    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
+    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
+    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
+    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+'''
+import pexpect
+import unittest
+import os
+import tempfile
+from . import PexpectTestCase
+
+# the program cat(1) may display ^D\x08\x08 when \x04 (EOF, Ctrl-D) is sent
+_CAT_EOF = b'^D\x08\x08'
+
+class TestCaseLog(PexpectTestCase.PexpectTestCase):
+
+    def test_log (self):
+        log_message = 'This is a test.'
+        filename = tempfile.mktemp()
+        mylog = open(filename, 'wb')
+        p = pexpect.spawn('echo', [log_message])
+        p.logfile = mylog
+        p.expect(pexpect.EOF)
+        p.logfile = None
+        mylog.close()
+        with open(filename, 'rb') as f:
+            lf = f.read()
+        os.unlink(filename)
+        self.assertEqual(lf.rstrip(), log_message.encode('ascii'))
+
+    def test_log_logfile_read (self):
+        log_message = 'This is a test.'
+        filename = tempfile.mktemp()
+        mylog = open(filename, 'wb')
+        p = pexpect.spawn('cat')
+        p.logfile_read = mylog
+        p.sendline(log_message)
+        p.sendeof()
+        p.expect(pexpect.EOF)
+        p.logfile = None
+        mylog.close()
+        with open(filename, 'rb') as f:
+            lf = f.read()
+        os.unlink (filename)
+        lf = lf.replace(_CAT_EOF, b'')
+        self.assertEqual(lf, b'This is a test.\r\nThis is a test.\r\n')
+
+    def test_log_logfile_send (self):
+        log_message = b'This is a test.'
+        filename = tempfile.mktemp()
+        mylog = open (filena