diff --git a/astropy_helpers/commands/build_sphinx.py b/astropy_helpers/commands/build_sphinx.py index c52115af..4517e92c 100644 --- a/astropy_helpers/commands/build_sphinx.py +++ b/astropy_helpers/commands/build_sphinx.py @@ -10,8 +10,11 @@ from distutils import log from distutils.cmd import DistutilsOptionError +import sphinx from sphinx.setup_command import BuildDoc as SphinxBuildDoc +from ..utils import minversion + PY3 = sys.version_info[0] >= 3 @@ -146,6 +149,12 @@ def run(self): subproccode[i] = repr(val) subproccode = ''.join(subproccode) + # This is a quick gross hack, but it ensures that the code grabbed from + # SphinxBuildDoc.run will work in Python 2 if it uses the print + # function + if minversion(sphinx, '1.3'): + subproccode = 'from __future__ import print_function' + subproccode + if self.no_intersphinx: # the confoverrides variable in sphinx.setup_command.BuildDoc can # be used to override the conf.py ... but this could well break diff --git a/astropy_helpers/sphinx/ext/automodapi.py b/astropy_helpers/sphinx/ext/automodapi.py index bb5b1ec6..19b628d3 100644 --- a/astropy_helpers/sphinx/ext/automodapi.py +++ b/astropy_helpers/sphinx/ext/automodapi.py @@ -171,8 +171,6 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None, sphinx markup. """ - env = app.builder.env - spl = _automodapirex.split(sourcestr) if len(spl) > 1: # automodsumm is in this document @@ -313,6 +311,7 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None, f.write('\n**NEW DOC**\n\n') f.write(ustr) else: + env = app.builder.env # Determine the filename associated with this doc (specifically # the extension) filename = docname + os.path.splitext(env.doc2path(docname))[1] diff --git a/astropy_helpers/sphinx/ext/tests/test_automodsumm.py b/astropy_helpers/sphinx/ext/tests/test_automodsumm.py index cd8afa33..aec7039b 100644 --- a/astropy_helpers/sphinx/ext/tests/test_automodsumm.py +++ b/astropy_helpers/sphinx/ext/tests/test_automodsumm.py @@ -66,7 +66,8 @@ def warn(self, msg, loc): automodsumm_to_autosummary_lines generate_automodsumm_docs process_automodsumm_generation - setup""" + setup +""" def test_ams_to_asmry(tmpdir): @@ -97,7 +98,8 @@ def test_ams_to_asmry(tmpdir): .. autosummary:: :p: - pilot""" + pilot +""" def test_ams_cython(tmpdir, cython_testpackage): diff --git a/astropy_helpers/utils.py b/astropy_helpers/utils.py index bf5bc2b8..9c053414 100644 --- a/astropy_helpers/utils.py +++ b/astropy_helpers/utils.py @@ -592,3 +592,70 @@ def delete(self): delattr(self, private_name) return property(get, set, delete) + + +def minversion(module, version, inclusive=True, version_path='__version__'): + """ + Returns `True` if the specified Python module satisfies a minimum version + requirement, and `False` if not. + + By default this uses `pkg_resources.parse_version` to do the version + comparison if available. Otherwise it falls back on + `distutils.version.LooseVersion`. + + Parameters + ---------- + + module : module or `str` + An imported module of which to check the version, or the name of + that module (in which case an import of that module is attempted-- + if this fails `False` is returned). + + version : `str` + The version as a string that this module must have at a minimum (e.g. + ``'0.12'``). + + inclusive : `bool` + The specified version meets the requirement inclusively (i.e. ``>=``) + as opposed to strictly greater than (default: `True`). + + version_path : `str` + A dotted attribute path to follow in the module for the version. + Defaults to just ``'__version__'``, which should work for most Python + modules. + + Examples + -------- + + >>> import astropy + >>> minversion(astropy, '0.4.4') + True + """ + + if isinstance(module, types.ModuleType): + module_name = module.__name__ + elif isinstance(module, six.string_types): + module_name = module + try: + module = resolve_name(module_name) + except ImportError: + return False + else: + raise ValueError('module argument must be an actual imported ' + 'module, or the import name of the module; ' + 'got {0!r}'.format(module)) + + if '.' not in version_path: + have_version = getattr(module, version_path) + else: + have_version = resolve_name('.'.join([module.__name__, version_path])) + + try: + from pkg_resources import parse_version + except ImportError: + from distutils.version import LooseVersion as parse_version + + if inclusive: + return parse_version(have_version) >= parse_version(version) + else: + return parse_version(have_version) > parse_version(version)