add script to generate standalone py.test

--HG--
branch : trunk
This commit is contained in:
Ralf Schmitt 2009-12-27 23:03:04 +01:00
parent 9239d2dd06
commit cc82f1601c
3 changed files with 98 additions and 1 deletions

View File

@ -0,0 +1,42 @@
#! /usr/bin/env python
import os
import cPickle
import zlib
import base64
import sys
def main():
here = os.path.dirname(os.path.abspath(__file__))
outfile = os.path.join(here, "py.test")
infile = outfile+"-in"
os.chdir(os.path.dirname(here))
files = []
for dirpath, dirnames, filenames in os.walk("py"):
for f in filenames:
if not f.endswith(".py"):
continue
fn = os.path.join(dirpath, f)
files.append(fn)
name2src = {}
for f in files:
k = f.replace("/", ".")[:-3]
name2src[k] = open(f, "rb").read()
data = cPickle.dumps(name2src, 2)
data = zlib.compress(data, 9)
data = base64.encodestring(data)
exe = open(infile, "rb").read()
exe = exe.replace("@SOURCES@", data)
open(outfile, "wb").write(exe)
os.chmod(outfile, 493) # 0755
sys.stdout.write("generated %s\n" % outfile)
if __name__=="__main__":
main()

50
bin-for-dist/py.test-in Executable file
View File

@ -0,0 +1,50 @@
#! /usr/bin/env python
sources = """
@SOURCES@"""
import sys
import cPickle
import base64
import zlib
import imp
sources = cPickle.loads(zlib.decompress(base64.decodestring(sources)))
class DictImporter(object):
sources = sources
def find_module(self, fullname, path=None):
if fullname in self.sources:
return self
if fullname+'.__init__' in self.sources:
return self
return None
def load_module(self, fullname):
# print "load_module:", fullname
import new
try:
s = self.sources[fullname]
is_pkg = False
except KeyError:
s = self.sources[fullname+'.__init__']
is_pkg = True
co = compile(s, fullname, 'exec')
module = sys.modules.setdefault(fullname, new.module(fullname))
module.__file__ = "%s/%s" % (__file__, fullname)
module.__loader__ = self
if is_pkg:
module.__path__ = [fullname]
exec co in module.__dict__
return sys.modules[fullname]
importer = DictImporter()
sys.meta_path.append(importer)
if __name__ == "__main__":
import py
py.cmdline.pytest()

View File

@ -41,7 +41,12 @@ class Conftest(object):
if path is None: if path is None:
raise ValueError("missing default conftest.") raise ValueError("missing default conftest.")
dp = path.dirpath() dp = path.dirpath()
if dp == path: if dp == path:
if not defaultconftestpath.check(): # zip file, single-file py.test?
import defaultconftest
if self._onimport:
self._onimport(defaultconftest)
return [defaultconftest]
return [self.importconftest(defaultconftestpath)] return [self.importconftest(defaultconftestpath)]
clist = self.getconftestmodules(dp) clist = self.getconftestmodules(dp)
conftestpath = path.join("conftest.py") conftestpath = path.join("conftest.py")