move standalone script to become a plugin offering "--genscript",
adjust paths accordingly and add CHANGELOG entry. --HG-- branch : trunk
This commit is contained in:
@@ -8,7 +8,7 @@ from py.impl.test.outcome import Skipped
|
||||
|
||||
default_plugins = (
|
||||
"default runner capture terminal mark skipping tmpdir monkeypatch "
|
||||
"recwarn pdb pastebin unittest helpconfig nose assertion").split()
|
||||
"recwarn pdb pastebin unittest helpconfig nose assertion genscript").split()
|
||||
|
||||
def check_old_use(mod, modname):
|
||||
clsname = modname[len('pytest_'):].capitalize() + "Plugin"
|
||||
|
||||
63
py/plugin/pytest_genscript.py
Executable file
63
py/plugin/pytest_genscript.py
Executable file
@@ -0,0 +1,63 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
import os
|
||||
import zlib
|
||||
import base64
|
||||
import sys
|
||||
try:
|
||||
import pickle
|
||||
except Importerror:
|
||||
import cPickle as pickle
|
||||
|
||||
def pytest_addoption(parser):
|
||||
group = parser.getgroup("general")
|
||||
group.addoption("--genscript", action="store", default=None,
|
||||
dest="genscript", metavar="path",
|
||||
help="create standalone py.test script at given target path.")
|
||||
|
||||
def pytest_configure(config):
|
||||
genscript = config.getvalue("genscript")
|
||||
if genscript:
|
||||
import py
|
||||
mydir = py.path.local(__file__).dirpath()
|
||||
infile = mydir.join("standalonetemplate.py")
|
||||
pybasedir = py.path.local(py.__file__).dirpath().dirpath()
|
||||
main(pybasedir, outfile=genscript, infile=infile)
|
||||
raise SystemExit(0)
|
||||
|
||||
def main(pybasedir, outfile, infile):
|
||||
os.chdir(str(pybasedir))
|
||||
outfile = str(outfile)
|
||||
infile = str(infile)
|
||||
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 = pickle.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 standalone py.test at %r, have fun!\n" % outfile)
|
||||
|
||||
if __name__=="__main__":
|
||||
dn = os.path.dirname
|
||||
here = os.path.abspath(dn(__file__)) # py/plugin/
|
||||
pybasedir = dn(dn(here))
|
||||
outfile = os.path.join(os.getcwd(), "py.test-standalone")
|
||||
infile = os.path.join(here, 'standalonetemplate.py')
|
||||
main(pybasedir, outfile, infile)
|
||||
64
py/plugin/standalonetemplate.py
Executable file
64
py/plugin/standalonetemplate.py
Executable file
@@ -0,0 +1,64 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
sources = """
|
||||
@SOURCES@"""
|
||||
|
||||
import sys
|
||||
import base64
|
||||
import zlib
|
||||
import imp
|
||||
|
||||
if sys.version_info >= (3,0):
|
||||
exec("def do_exec(co, loc): exec(co, loc)\n")
|
||||
import pickle
|
||||
sources = sources.encode("ascii") # ensure bytes
|
||||
sources = pickle.loads(zlib.decompress(base64.decodebytes(sources)))
|
||||
else:
|
||||
import cPickle as pickle
|
||||
exec("def do_exec(co, loc): exec co in loc\n")
|
||||
sources = pickle.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
|
||||
from types import ModuleType
|
||||
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, ModuleType(fullname))
|
||||
module.__file__ = "%s/%s" % (__file__, fullname)
|
||||
module.__loader__ = self
|
||||
if is_pkg:
|
||||
module.__path__ = [fullname]
|
||||
|
||||
do_exec(co, module.__dict__)
|
||||
return sys.modules[fullname]
|
||||
|
||||
def get_source(self, name):
|
||||
res = self.sources.get(name)
|
||||
if res is None:
|
||||
res = self.sources.get(name+'.__init__')
|
||||
return res
|
||||
|
||||
|
||||
|
||||
importer = DictImporter()
|
||||
|
||||
sys.meta_path.append(importer)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import py
|
||||
py.cmdline.pytest()
|
||||
Reference in New Issue
Block a user