standalonetemplate.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #! /usr/bin/env python
  2. # Hi There!
  3. # You may be wondering what this giant blob of binary data here is, you might
  4. # even be worried that we're up to something nefarious (good for you for being
  5. # paranoid!). This is a base64 encoding of a zip file, this zip file contains
  6. # a fully functional basic pytest script.
  7. #
  8. # Pytest is a thing that tests packages, pytest itself is a package that some-
  9. # one might want to install, especially if they're looking to run tests inside
  10. # some package they want to install. Pytest has a lot of code to collect and
  11. # execute tests, and other such sort of "tribal knowledge" that has been en-
  12. # coded in its code base. Because of this we basically include a basic copy
  13. # of pytest inside this blob. We do this because it let's you as a maintainer
  14. # or application developer who wants people who don't deal with python much to
  15. # easily run tests without installing the complete pytest package.
  16. #
  17. # If you're wondering how this is created: you can create it yourself if you
  18. # have a complete pytest installation by using this command on the command-
  19. # line: ``py.test --genscript=runtests.py``.
  20. sources = """
  21. @SOURCES@"""
  22. import sys
  23. import base64
  24. import zlib
  25. class DictImporter(object):
  26. def __init__(self, sources):
  27. self.sources = sources
  28. def find_module(self, fullname, path=None):
  29. if fullname == "argparse" and sys.version_info >= (2,7):
  30. # we were generated with <python2.7 (which pulls in argparse)
  31. # but we are running now on a stdlib which has it, so use that.
  32. return None
  33. if fullname in self.sources:
  34. return self
  35. if fullname + '.__init__' in self.sources:
  36. return self
  37. return None
  38. def load_module(self, fullname):
  39. # print "load_module:", fullname
  40. from types import ModuleType
  41. try:
  42. s = self.sources[fullname]
  43. is_pkg = False
  44. except KeyError:
  45. s = self.sources[fullname + '.__init__']
  46. is_pkg = True
  47. co = compile(s, fullname, 'exec')
  48. module = sys.modules.setdefault(fullname, ModuleType(fullname))
  49. module.__file__ = "%s/%s" % (__file__, fullname)
  50. module.__loader__ = self
  51. if is_pkg:
  52. module.__path__ = [fullname]
  53. do_exec(co, module.__dict__) # noqa
  54. return sys.modules[fullname]
  55. def get_source(self, name):
  56. res = self.sources.get(name)
  57. if res is None:
  58. res = self.sources.get(name + '.__init__')
  59. return res
  60. if __name__ == "__main__":
  61. try:
  62. import pkg_resources # noqa
  63. except ImportError:
  64. sys.stderr.write("ERROR: setuptools not installed\n")
  65. sys.exit(2)
  66. if sys.version_info >= (3, 0):
  67. exec("def do_exec(co, loc): exec(co, loc)\n")
  68. import pickle
  69. sources = sources.encode("ascii") # ensure bytes
  70. sources = pickle.loads(zlib.decompress(base64.decodebytes(sources)))
  71. else:
  72. import cPickle as pickle
  73. exec("def do_exec(co, loc): exec co in loc\n")
  74. sources = pickle.loads(zlib.decompress(base64.decodestring(sources)))
  75. importer = DictImporter(sources)
  76. sys.meta_path.insert(0, importer)
  77. entry = "@ENTRY@"
  78. do_exec(entry, locals()) # noqa