Moved freeze_includes() to genscript

--HG--
branch : cx_freeze-support
This commit is contained in:
Bruno Oliveira 2014-08-11 20:03:14 -03:00
parent 3c649cf91d
commit d2903507d8
5 changed files with 67 additions and 59 deletions

View File

@ -1,56 +0,0 @@
"""
Package to support embedding pytest runner into executable files.
.. note:: Since we are imported into pytest namespace, we use local imports to
be as cheap as possible.
"""
def includes():
"""
Returns a list of module names used by py.test that should be
included by cx_freeze.
"""
import py
import _pytest
result = list(_iter_all_modules(py))
result += list(_iter_all_modules(_pytest))
# builtin files imported by pytest using py.std implicit mechanism;
# should be removed if https://bitbucket.org/hpk42/pytest/pull-request/185
# gets merged
result += [
'argparse',
'shlex',
'warnings',
'types',
]
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',
...
]
"""
import pkgutil
import os
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

View File

@ -1,6 +1,12 @@
""" generate a single-file self-contained version of pytest """
import py
import os
import sys
import pkgutil
import py
import _pytest
def find_toplevel(name):
for syspath in py.std.sys.path:
@ -78,3 +84,52 @@ def pytest_cmdline_main(config):
tw.line("generated pytest standalone script: %s" % genscript,
bold=True)
return 0
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))
# builtin files imported by pytest using py.std implicit mechanism;
# should be removed if https://bitbucket.org/hpk42/pytest/pull-request/185
# gets merged
result += [
'argparse',
'shlex',
'warnings',
'types',
]
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

View File

@ -13,7 +13,6 @@ if __name__ == '__main__': # if run as a script or by 'python -m pytest'
from _pytest.config import main, UsageError, _preloadplugins, cmdline
from _pytest import __version__
from _pytest import cx_freeze_support
_preloadplugins() # to populate pytest.* namespace so help(pytest) works

View File

@ -10,6 +10,6 @@ if __name__ == '__main__':
version="0.1",
description="exemple of how embedding py.test into an executable using cx_freeze",
executables=[Executable("runtests_script.py")],
options={"build_exe": {'includes': pytest.cx_freeze_support.includes()}},
options={"build_exe": {'includes': pytest.freeze_includes()}},
)

View File

@ -36,3 +36,13 @@ def test_gen(testdir, anypython, standalone):
result = standalone.run(anypython, testdir, p)
assert result.ret != 0
def test_freeze_includes():
"""
Smoke test for freeze_includes(), to ensure that it works across all
supported python versions.
"""
includes = pytest.freeze_includes()
assert len(includes) > 1
assert '_pytest.genscript' in includes