fix genscript by copying the new implementation from the genscript package
--HG-- branch : trunk
This commit is contained in:
parent
2a579217b8
commit
5d798feaf0
|
@ -1,10 +1,53 @@
|
||||||
#! /usr/bin/env python
|
import py
|
||||||
"""
|
import pickle
|
||||||
generate standalone test script to be distributed along with an application.
|
import zlib
|
||||||
"""
|
import base64
|
||||||
|
|
||||||
|
def find_toplevel(name):
|
||||||
|
for syspath in py.std.sys.path:
|
||||||
|
base = py.path.local(syspath)
|
||||||
|
lib = base/name
|
||||||
|
if lib.check(dir=1):
|
||||||
|
return lib
|
||||||
|
raise LookupError(name)
|
||||||
|
|
||||||
|
def pkgname(toplevel, rootpath, path):
|
||||||
|
parts = path.parts()[len(rootpath.parts()):]
|
||||||
|
return '.'.join([toplevel] + [x.purebasename for x in parts])
|
||||||
|
|
||||||
|
def pkg_to_mapping(name):
|
||||||
|
toplevel = find_toplevel(name)
|
||||||
|
name2src = {}
|
||||||
|
for pyfile in toplevel.visit('*.py'):
|
||||||
|
pkg = pkgname(name, toplevel, pyfile)
|
||||||
|
name2src[pkg] = pyfile.read()
|
||||||
|
return name2src
|
||||||
|
|
||||||
|
|
||||||
|
def compress_mapping(mapping):
|
||||||
|
data = pickle.dumps(mapping, 2)
|
||||||
|
data = zlib.compress(data, 9)
|
||||||
|
data = base64.encodestring(data)
|
||||||
|
data = data.decode('ascii')
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def compress_packages(names):
|
||||||
|
mapping = {}
|
||||||
|
for name in names:
|
||||||
|
mapping.update(pkg_to_mapping(name))
|
||||||
|
return compress_mapping(mapping)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_script(entry, packages):
|
||||||
|
data = compress_packages(packages)
|
||||||
|
tmpl = py.path.local(__file__).dirpath().join('standalonetemplate.py')
|
||||||
|
exe = tmpl.read()
|
||||||
|
exe = exe.replace('@SOURCES@', data)
|
||||||
|
exe = exe.replace('@ENTRY@', entry)
|
||||||
|
return exe
|
||||||
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
group = parser.getgroup("debugconfig")
|
group = parser.getgroup("debugconfig")
|
||||||
group.addoption("--genscript", action="store", default=None,
|
group.addoption("--genscript", action="store", default=None,
|
||||||
|
@ -14,56 +57,11 @@ def pytest_addoption(parser):
|
||||||
def pytest_cmdline_main(config):
|
def pytest_cmdline_main(config):
|
||||||
genscript = config.getvalue("genscript")
|
genscript = config.getvalue("genscript")
|
||||||
if genscript:
|
if genscript:
|
||||||
import py
|
script = generate_script(
|
||||||
mydir = py.path.local(__file__).dirpath()
|
'import py; py.test.cmdline.main()',
|
||||||
infile = mydir.join("standalonetemplate.py")
|
['py', 'pytest'],
|
||||||
pybasedir = py.path.local(py.__file__).dirpath().dirpath()
|
)
|
||||||
|
|
||||||
genscript = py.path.local(genscript)
|
genscript = py.path.local(genscript)
|
||||||
main(pybasedir, outfile=genscript, infile=infile)
|
genscript.write(script)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def main(pybasedir, outfile, infile):
|
|
||||||
import base64
|
|
||||||
import zlib
|
|
||||||
try:
|
|
||||||
import pickle
|
|
||||||
except Importerror:
|
|
||||||
import cPickle as pickle
|
|
||||||
|
|
||||||
outfile = str(outfile)
|
|
||||||
infile = str(infile)
|
|
||||||
assert os.path.isabs(outfile)
|
|
||||||
os.chdir(str(pybasedir))
|
|
||||||
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(os.sep, ".")[:-3]
|
|
||||||
name2src[k] = open(f, "r").read()
|
|
||||||
|
|
||||||
data = pickle.dumps(name2src, 2)
|
|
||||||
data = zlib.compress(data, 9)
|
|
||||||
data = base64.encodestring(data)
|
|
||||||
data = data.decode("ascii")
|
|
||||||
|
|
||||||
exe = open(infile, "r").read()
|
|
||||||
exe = exe.replace("@SOURCES@", data)
|
|
||||||
|
|
||||||
open(outfile, "w").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)
|
|
||||||
|
|
|
@ -59,5 +59,5 @@ if __name__ == "__main__":
|
||||||
importer = DictImporter(sources)
|
importer = DictImporter(sources)
|
||||||
sys.meta_path.append(importer)
|
sys.meta_path.append(importer)
|
||||||
|
|
||||||
import py
|
entry = "@ENTRY@"
|
||||||
py.cmdline.pytest()
|
do_exec(entry, locals())
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import py, os, sys
|
import py, os, sys
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
pytestmark = py.test.mark.xfail(run=False,
|
|
||||||
reason="XXX needs refactoring after pylib/pytest split")
|
|
||||||
|
|
||||||
def pytest_funcarg__standalone(request):
|
def pytest_funcarg__standalone(request):
|
||||||
return request.cached_setup(scope="module", setup=lambda: Standalone(request))
|
return request.cached_setup(scope="module", setup=lambda: Standalone(request))
|
||||||
|
@ -20,7 +18,6 @@ class Standalone:
|
||||||
testdir.chdir()
|
testdir.chdir()
|
||||||
return testdir._run(anypython, self.script, *args)
|
return testdir._run(anypython, self.script, *args)
|
||||||
|
|
||||||
@pytestmark # XXX bug in application of global markers to generated functions?
|
|
||||||
def test_gen(testdir, anypython, standalone):
|
def test_gen(testdir, anypython, standalone):
|
||||||
result = standalone.run(anypython, testdir, '-h')
|
result = standalone.run(anypython, testdir, '-h')
|
||||||
assert result.ret == 0
|
assert result.ret == 0
|
||||||
|
|
Loading…
Reference in New Issue