From d2903507d81dbe92ca84771a78a102102a2ae690 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 11 Aug 2014 20:03:14 -0300 Subject: [PATCH] Moved freeze_includes() to genscript --HG-- branch : cx_freeze-support --- _pytest/cx_freeze_support.py | 56 ---------------------------- _pytest/genscript.py | 57 ++++++++++++++++++++++++++++- pytest.py | 1 - testing/cx_freeze/runtests_setup.py | 2 +- testing/test_genscript.py | 10 +++++ 5 files changed, 67 insertions(+), 59 deletions(-) delete mode 100644 _pytest/cx_freeze_support.py diff --git a/_pytest/cx_freeze_support.py b/_pytest/cx_freeze_support.py deleted file mode 100644 index fbe438bf0..000000000 --- a/_pytest/cx_freeze_support.py +++ /dev/null @@ -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 diff --git a/_pytest/genscript.py b/_pytest/genscript.py index 25e8e6a0f..f7dbba36c 100755 --- a/_pytest/genscript.py +++ b/_pytest/genscript.py @@ -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 diff --git a/pytest.py b/pytest.py index b84cb2a17..6c25c6195 100644 --- a/pytest.py +++ b/pytest.py @@ -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 diff --git a/testing/cx_freeze/runtests_setup.py b/testing/cx_freeze/runtests_setup.py index fa2373890..a2874a655 100644 --- a/testing/cx_freeze/runtests_setup.py +++ b/testing/cx_freeze/runtests_setup.py @@ -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()}}, ) diff --git a/testing/test_genscript.py b/testing/test_genscript.py index 02bbabb6f..1c65fec14 100644 --- a/testing/test_genscript.py +++ b/testing/test_genscript.py @@ -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 +