2014-01-18 19:31:33 +08:00
|
|
|
""" generate a single-file self-contained version of pytest """
|
2014-08-12 07:03:14 +08:00
|
|
|
import os
|
2013-07-26 14:59:31 +08:00
|
|
|
import sys
|
2014-08-12 07:03:14 +08:00
|
|
|
import pkgutil
|
|
|
|
|
|
|
|
import py
|
|
|
|
import _pytest
|
|
|
|
|
2010-10-16 09:10:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
def find_toplevel(name):
|
2014-08-01 06:13:40 +08:00
|
|
|
for syspath in sys.path:
|
2010-10-16 09:10:14 +08:00
|
|
|
base = py.path.local(syspath)
|
|
|
|
lib = base/name
|
|
|
|
if lib.check(dir=1):
|
|
|
|
return lib
|
2010-11-13 18:10:45 +08:00
|
|
|
mod = base.join("%s.py" % name)
|
|
|
|
if mod.check(file=1):
|
|
|
|
return mod
|
2010-10-16 09:10:14 +08:00
|
|
|
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 = {}
|
2010-11-13 18:10:45 +08:00
|
|
|
if toplevel.check(file=1): # module
|
|
|
|
name2src[toplevel.purebasename] = toplevel.read()
|
|
|
|
else: # package
|
|
|
|
for pyfile in toplevel.visit('*.py'):
|
|
|
|
pkg = pkgname(name, toplevel, pyfile)
|
|
|
|
name2src[pkg] = pyfile.read()
|
2015-05-04 20:42:01 +08:00
|
|
|
# with wheels py source code might be not be installed
|
|
|
|
# and the resulting genscript is useless, just bail out.
|
|
|
|
assert name2src, "no source code found for %r at %r" %(name, toplevel)
|
2010-10-16 09:10:14 +08:00
|
|
|
return name2src
|
|
|
|
|
|
|
|
def compress_mapping(mapping):
|
2014-08-01 16:12:53 +08:00
|
|
|
import base64, pickle, zlib
|
2014-08-01 06:13:40 +08:00
|
|
|
data = pickle.dumps(mapping, 2)
|
|
|
|
data = zlib.compress(data, 9)
|
|
|
|
data = base64.encodestring(data)
|
2010-10-16 09:10:14 +08:00
|
|
|
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
|
|
|
|
|
2009-12-31 02:10:49 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2010-01-03 19:41:29 +08:00
|
|
|
group = parser.getgroup("debugconfig")
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption("--genscript", action="store", default=None,
|
|
|
|
dest="genscript", metavar="path",
|
2014-01-18 19:31:33 +08:00
|
|
|
help="create standalone pytest script at given target path.")
|
2009-12-31 02:10:49 +08:00
|
|
|
|
2010-09-26 22:23:43 +08:00
|
|
|
def pytest_cmdline_main(config):
|
2015-07-19 03:39:55 +08:00
|
|
|
import _pytest.config
|
2009-12-31 02:10:49 +08:00
|
|
|
genscript = config.getvalue("genscript")
|
|
|
|
if genscript:
|
2015-07-19 03:39:55 +08:00
|
|
|
tw = _pytest.config.create_terminal_writer(config)
|
2015-04-29 22:40:52 +08:00
|
|
|
deps = ['py', 'pluggy', '_pytest', 'pytest']
|
2013-07-26 14:59:31 +08:00
|
|
|
if sys.version_info < (2,7):
|
|
|
|
deps.append("argparse")
|
2014-09-05 15:50:40 +08:00
|
|
|
tw.line("generated script will run on python2.6-python3.3++")
|
2013-07-26 14:59:31 +08:00
|
|
|
else:
|
|
|
|
tw.line("WARNING: generated script will not run on python2.6 "
|
2014-09-05 15:50:40 +08:00
|
|
|
"due to 'argparse' dependency. Use python2.6 "
|
|
|
|
"to generate a python2.6 compatible script", red=True)
|
2010-10-16 09:10:14 +08:00
|
|
|
script = generate_script(
|
2014-01-18 19:31:33 +08:00
|
|
|
'import pytest; raise SystemExit(pytest.cmdline.main())',
|
2013-07-26 14:59:31 +08:00
|
|
|
deps,
|
2010-10-16 09:10:14 +08:00
|
|
|
)
|
2010-01-18 23:48:20 +08:00
|
|
|
genscript = py.path.local(genscript)
|
2010-10-16 09:10:14 +08:00
|
|
|
genscript.write(script)
|
2013-07-26 14:59:31 +08:00
|
|
|
tw.line("generated pytest standalone script: %s" % genscript,
|
|
|
|
bold=True)
|
2010-09-26 22:23:43 +08:00
|
|
|
return 0
|
2014-08-12 07:03:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_namespace():
|
|
|
|
return {'freeze_includes': freeze_includes}
|
|
|
|
|
|
|
|
|
|
|
|
def freeze_includes():
|
|
|
|
"""
|
|
|
|
Returns a list of module names used by py.test that should be
|
|
|
|
included by cx_freeze.
|
|
|
|
"""
|
|
|
|
result = list(_iter_all_modules(py))
|
|
|
|
result += list(_iter_all_modules(_pytest))
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def _iter_all_modules(package, prefix=''):
|
|
|
|
"""
|
|
|
|
Iterates over the names of all modules that can be found in the given
|
|
|
|
package, recursively.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
_iter_all_modules(_pytest) ->
|
|
|
|
['_pytest.assertion.newinterpret',
|
|
|
|
'_pytest.capture',
|
|
|
|
'_pytest.core',
|
|
|
|
...
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
if type(package) is not str:
|
|
|
|
path, prefix = package.__path__[0], package.__name__ + '.'
|
|
|
|
else:
|
|
|
|
path = package
|
|
|
|
for _, name, is_package in pkgutil.iter_modules([path]):
|
|
|
|
if is_package:
|
|
|
|
for m in _iter_all_modules(os.path.join(path, name), prefix=name + '.'):
|
|
|
|
yield prefix + m
|
|
|
|
else:
|
|
|
|
yield prefix + name
|