From 5d798feaf0aac4c3b0582609fde0962c3bcb2c43 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 16 Oct 2010 03:10:14 +0200 Subject: [PATCH] fix genscript by copying the new implementation from the genscript package --HG-- branch : trunk --- pytest/plugin/genscript.py | 112 ++++++++++++++-------------- pytest/plugin/standalonetemplate.py | 4 +- testing/plugin/test_genscript.py | 3 - 3 files changed, 57 insertions(+), 62 deletions(-) diff --git a/pytest/plugin/genscript.py b/pytest/plugin/genscript.py index bd72aadc5..2b379294e 100755 --- a/pytest/plugin/genscript.py +++ b/pytest/plugin/genscript.py @@ -1,10 +1,53 @@ -#! /usr/bin/env python -""" -generate standalone test script to be distributed along with an application. -""" +import py +import pickle +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): group = parser.getgroup("debugconfig") group.addoption("--genscript", action="store", default=None, @@ -14,56 +57,11 @@ def pytest_addoption(parser): def pytest_cmdline_main(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() + script = generate_script( + 'import py; py.test.cmdline.main()', + ['py', 'pytest'], + ) + genscript = py.path.local(genscript) - main(pybasedir, outfile=genscript, infile=infile) + genscript.write(script) 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) diff --git a/pytest/plugin/standalonetemplate.py b/pytest/plugin/standalonetemplate.py index a41f7ca4d..f3f2c5b3f 100755 --- a/pytest/plugin/standalonetemplate.py +++ b/pytest/plugin/standalonetemplate.py @@ -59,5 +59,5 @@ if __name__ == "__main__": importer = DictImporter(sources) sys.meta_path.append(importer) - import py - py.cmdline.pytest() + entry = "@ENTRY@" + do_exec(entry, locals()) diff --git a/testing/plugin/test_genscript.py b/testing/plugin/test_genscript.py index c4e514ce1..bb4e555c0 100644 --- a/testing/plugin/test_genscript.py +++ b/testing/plugin/test_genscript.py @@ -1,8 +1,6 @@ import py, os, sys import subprocess -pytestmark = py.test.mark.xfail(run=False, - reason="XXX needs refactoring after pylib/pytest split") def pytest_funcarg__standalone(request): return request.cached_setup(scope="module", setup=lambda: Standalone(request)) @@ -20,7 +18,6 @@ class Standalone: testdir.chdir() return testdir._run(anypython, self.script, *args) -@pytestmark # XXX bug in application of global markers to generated functions? def test_gen(testdir, anypython, standalone): result = standalone.run(anypython, testdir, '-h') assert result.ret == 0