pastebin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. """ submit failure or test session information to a pastebin service. """
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6. import sys
  7. import tempfile
  8. import six
  9. import pytest
  10. def pytest_addoption(parser):
  11. group = parser.getgroup("terminal reporting")
  12. group._addoption(
  13. "--pastebin",
  14. metavar="mode",
  15. action="store",
  16. dest="pastebin",
  17. default=None,
  18. choices=["failed", "all"],
  19. help="send failed|all info to bpaste.net pastebin service.",
  20. )
  21. @pytest.hookimpl(trylast=True)
  22. def pytest_configure(config):
  23. if config.option.pastebin == "all":
  24. tr = config.pluginmanager.getplugin("terminalreporter")
  25. # if no terminal reporter plugin is present, nothing we can do here;
  26. # this can happen when this function executes in a slave node
  27. # when using pytest-xdist, for example
  28. if tr is not None:
  29. # pastebin file will be utf-8 encoded binary file
  30. config._pastebinfile = tempfile.TemporaryFile("w+b")
  31. oldwrite = tr._tw.write
  32. def tee_write(s, **kwargs):
  33. oldwrite(s, **kwargs)
  34. if isinstance(s, six.text_type):
  35. s = s.encode("utf-8")
  36. config._pastebinfile.write(s)
  37. tr._tw.write = tee_write
  38. def pytest_unconfigure(config):
  39. if hasattr(config, "_pastebinfile"):
  40. # get terminal contents and delete file
  41. config._pastebinfile.seek(0)
  42. sessionlog = config._pastebinfile.read()
  43. config._pastebinfile.close()
  44. del config._pastebinfile
  45. # undo our patching in the terminal reporter
  46. tr = config.pluginmanager.getplugin("terminalreporter")
  47. del tr._tw.__dict__["write"]
  48. # write summary
  49. tr.write_sep("=", "Sending information to Paste Service")
  50. pastebinurl = create_new_paste(sessionlog)
  51. tr.write_line("pastebin session-log: %s\n" % pastebinurl)
  52. def create_new_paste(contents):
  53. """
  54. Creates a new paste using bpaste.net service.
  55. :contents: paste contents as utf-8 encoded bytes
  56. :returns: url to the pasted contents
  57. """
  58. import re
  59. if sys.version_info < (3, 0):
  60. from urllib import urlopen, urlencode
  61. else:
  62. from urllib.request import urlopen
  63. from urllib.parse import urlencode
  64. params = {"code": contents, "lexer": "text", "expiry": "1week"}
  65. url = "https://bpaste.net"
  66. response = urlopen(url, data=urlencode(params).encode("ascii")).read()
  67. m = re.search(r'href="/raw/(\w+)"', response.decode("utf-8"))
  68. if m:
  69. return "%s/show/%s" % (url, m.group(1))
  70. else:
  71. return "bad response: " + response
  72. def pytest_terminal_summary(terminalreporter):
  73. import _pytest.config
  74. if terminalreporter.config.option.pastebin != "failed":
  75. return
  76. tr = terminalreporter
  77. if "failed" in tr.stats:
  78. terminalreporter.write_sep("=", "Sending information to Paste Service")
  79. for rep in terminalreporter.stats.get("failed"):
  80. try:
  81. msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc
  82. except AttributeError:
  83. msg = tr._getfailureheadline(rep)
  84. tw = _pytest.config.create_terminal_writer(
  85. terminalreporter.config, stringio=True
  86. )
  87. rep.toterminal(tw)
  88. s = tw.stringio.getvalue()
  89. assert len(s)
  90. pastebinurl = create_new_paste(s)
  91. tr.write_line("%s --> %s" % (msg, pastebinurl))