| 1 |
""" (deprecated) generate a single-file self-contained version of pytest """ |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import pkgutil |
| 5 |
|
| 6 |
import py |
| 7 |
import _pytest |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
def find_toplevel(name): |
| 12 |
for syspath in sys.path: |
| 13 |
base = py.path.local(syspath) |
| 14 |
lib = base/name |
| 15 |
if lib.check(dir=1): |
| 16 |
return lib |
| 17 |
mod = base.join("%s.py" % name) |
| 18 |
if mod.check(file=1): |
| 19 |
return mod |
| 20 |
raise LookupError(name) |
| 21 |
|
| 22 |
def pkgname(toplevel, rootpath, path): |
| 23 |
parts = path.parts()[len(rootpath.parts()):] |
| 24 |
return '.'.join([toplevel] + [x.purebasename for x in parts]) |
| 25 |
|
| 26 |
def pkg_to_mapping(name): |
| 27 |
toplevel = find_toplevel(name) |
| 28 |
name2src = {} |
| 29 |
if toplevel.check(file=1): |
| 30 |
name2src[toplevel.purebasename] = toplevel.read() |
| 31 |
else: |
| 32 |
for pyfile in toplevel.visit('*.py'): |
| 33 |
pkg = pkgname(name, toplevel, pyfile) |
| 34 |
name2src[pkg] = pyfile.read() |
| 35 |
|
| 36 |
|
| 37 |
assert name2src, "no source code found for %r at %r" %(name, toplevel) |
| 38 |
return name2src |
| 39 |
|
| 40 |
def compress_mapping(mapping): |
| 41 |
import base64, pickle, zlib |
| 42 |
data = pickle.dumps(mapping, 2) |
| 43 |
data = zlib.compress(data, 9) |
| 44 |
data = base64.encodestring(data) |
| 45 |
data = data.decode('ascii') |
| 46 |
return data |
| 47 |
|
| 48 |
|
| 49 |
def compress_packages(names): |
| 50 |
mapping = {} |
| 51 |
for name in names: |
| 52 |
mapping.update(pkg_to_mapping(name)) |
| 53 |
return compress_mapping(mapping) |
| 54 |
|
| 55 |
def generate_script(entry, packages): |
| 56 |
data = compress_packages(packages) |
| 57 |
tmpl = py.path.local(__file__).dirpath().join('standalonetemplate.py') |
| 58 |
exe = tmpl.read() |
| 59 |
exe = exe.replace('@SOURCES@', data) |
| 60 |
exe = exe.replace('@ENTRY@', entry) |
| 61 |
return exe |
| 62 |
|
| 63 |
|
| 64 |
def pytest_addoption(parser): |
| 65 |
group = parser.getgroup("debugconfig") |
| 66 |
group.addoption("--genscript", action="store", default=None, |
| 67 |
dest="genscript", metavar="path", |
| 68 |
help="create standalone pytest script at given target path.") |
| 69 |
|
| 70 |
def pytest_cmdline_main(config): |
| 71 |
import _pytest.config |
| 72 |
genscript = config.getvalue("genscript") |
| 73 |
if genscript: |
| 74 |
tw = _pytest.config.create_terminal_writer(config) |
| 75 |
tw.line("WARNING: usage of genscript is deprecated.", |
| 76 |
red=True) |
| 77 |
deps = ['py', '_pytest', 'pytest'] |
| 78 |
if sys.version_info < (2,7): |
| 79 |
deps.append("argparse") |
| 80 |
tw.line("generated script will run on python2.6-python3.3++") |
| 81 |
else: |
| 82 |
tw.line("WARNING: generated script will not run on python2.6 " |
| 83 |
"due to 'argparse' dependency. Use python2.6 " |
| 84 |
"to generate a python2.6 compatible script", red=True) |
| 85 |
script = generate_script( |
| 86 |
'import pytest; raise SystemExit(pytest.cmdline.main())', |
| 87 |
deps, |
| 88 |
) |
| 89 |
genscript = py.path.local(genscript) |
| 90 |
genscript.write(script) |
| 91 |
tw.line("generated pytest standalone script: %s" % genscript, |
| 92 |
bold=True) |
| 93 |
return 0 |
| 94 |
|
| 95 |
|
| 96 |
def pytest_namespace(): |
| 97 |
return {'freeze_includes': freeze_includes} |
| 98 |
|
| 99 |
|
| 100 |
def freeze_includes(): |
| 101 |
""" |
| 102 |
Returns a list of module names used by py.test that should be |
| 103 |
included by cx_freeze. |
| 104 |
""" |
| 105 |
result = list(_iter_all_modules(py)) |
| 106 |
result += list(_iter_all_modules(_pytest)) |
| 107 |
return result |
| 108 |
|
| 109 |
|
| 110 |
def _iter_all_modules(package, prefix=''): |
| 111 |
""" |
| 112 |
Iterates over the names of all modules that can be found in the given |
| 113 |
package, recursively. |
| 114 |
|
| 115 |
Example: |
| 116 |
_iter_all_modules(_pytest) -> |
| 117 |
['_pytest.assertion.newinterpret', |
| 118 |
'_pytest.capture', |
| 119 |
'_pytest.core', |
| 120 |
... |
| 121 |
] |
| 122 |
""" |
| 123 |
if type(package) is not str: |
| 124 |
path, prefix = package.__path__[0], package.__name__ + '.' |
| 125 |
else: |
| 126 |
path = package |
| 127 |
for _, name, is_package in pkgutil.iter_modules([path]): |
| 128 |
if is_package: |
| 129 |
for m in _iter_all_modules(os.path.join(path, name), prefix=name + '.'): |
| 130 |
yield prefix + m |
| 131 |
else: |
| 132 |
yield prefix + name |