Beispiel #1
0
def _custom_import_modpath(modpath):
    import xdoctest.static_analysis as static
    dpath, rel_modpath = static.split_modpath(modpath)
    modname = static.modpath_to_modname(modpath)
    with PythonPathContext(dpath, index=0):
        module = import_module_from_name(modname)
    return module
Beispiel #2
0
def _pkgutil_import_modpath(modpath):  # nocover
    import six
    import xdoctest.static_analysis as static
    dpath, rel_modpath = static.split_modpath(modpath)
    modname = static.modpath_to_modname(modpath)
    if six.PY2:  # nocover
        import imp
        module = imp.load_source(modname, modpath)
    elif sys.version_info[0:2] <= (3, 4):  # nocover
        assert sys.version_info[0:2] <= (3, 2), '3.0 to 3.2 is not supported'
        from importlib.machinery import SourceFileLoader
        module = SourceFileLoader(modname, modpath).load_module()
    else:
        import importlib.util
        spec = importlib.util.spec_from_file_location(modname, modpath)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
    return module
Beispiel #3
0
def split_modpath(modpath):
    """
    Splits the modpath into the dir that must be in PYTHONPATH for the module
    to be imported and the modulepath relative to this directory.

    Args:
        modpath (str): module filepath

    Returns:
        str: directory

    Example:
        >>> from xdoctest import static_analysis
        >>> modpath = static_analysis.__file__
        >>> modpath = modpath.replace('.pyc', '.py')
        >>> dpath, rel_modpath = split_modpath(modpath)
        >>> assert join(dpath, rel_modpath) == modpath
        >>> assert rel_modpath == join('xdoctest', 'static_analysis.py')
    """
    from xdoctest import static_analysis as static
    return static.split_modpath(modpath)
Beispiel #4
0
def import_module_from_path(modpath):
    """
    Args:
        modpath (str): path to the module

    References:
        https://stackoverflow.com/questions/67631/import-module-given-path

    Example:
        >>> from xdoctest import utils
        >>> modpath = utils.__file__
        >>> module = import_module_from_path(modpath)
        >>> assert module is utils
    """
    # the importlib version doesnt work in pytest
    import xdoctest.static_analysis as static
    dpath, rel_modpath = static.split_modpath(modpath)
    modname = static.modpath_to_modname(modpath)
    with PythonPathContext(dpath):
        try:
            module = import_module_from_name(modname)
        except Exception:
            print('Failed to import modname={} with modpath={}'.format(
                modname, modpath))
            raise
    # TODO: use this implementation once pytest fixes importlib
    # if six.PY2:  # nocover
    #     import imp
    #     module = imp.load_source(modname, modpath)
    # elif sys.version_info[0:2] <= (3, 4):  # nocover
    #     assert sys.version_info[0:2] <= (3, 2), '3.0 to 3.2 is not supported'
    #     from importlib.machinery import SourceFileLoader
    #     module = SourceFileLoader(modname, modpath).load_module()
    # else:
    #     import importlib.util
    #     spec = importlib.util.spec_from_file_location(modname, modpath)
    #     module = importlib.util.module_from_spec(spec)
    #     spec.loader.exec_module(module)
    return module
Beispiel #5
0
def make_default_module_maintest(modpath, test_code=None, argv=None,
                                 force_full=False):
    """
    Args:
        modname (str):  module name

    Returns:
        str: text source code

    CommandLine:
        python -m utool.util_autogen --test-make_default_module_maintest

    References:
        http://legacy.python.org/dev/peps/pep-0338/

    Example:
        >>> import sys, ubelt as ub
        >>> sys.path.append(ub.truepath('~/local/vim/rc/'))
        >>> from pyvim_funcs import *
        >>> import pyvim_funcs
        >>> modpath = pyvim_funcs.__file__
        >>> argv = None
        >>> text = make_default_module_maintest(modpath)
        >>> print(text)
    """
    # if not use_modrun:
    #     if ub.WIN32:
    #         augpath = 'set PYTHONPATH=%PYTHONPATH%' + os.pathsep + moddir
    #     else:
    #         augpath = 'export PYTHONPATH=$PYTHONPATH' + os.pathsep + moddir
    #     cmdline = augpath + '\n' + cmdline
    import ubelt as ub
    from xdoctest import static_analysis as static

    modname = static.modpath_to_modname(modpath)
    moddir, rel_modpath = static.split_modpath(modpath)
    if not force_full:
        info = ub.cmd('python -c "import sys; print(sys.path)"')
        default_path = eval(info['out'], {})
        is_importable = static.is_modname_importable(modname, exclude=['.'],
                                                     sys_path=default_path)
    if not force_full and is_importable:
        cmdline = 'python -m ' + modname
    else:
        if ub.WIN32:
            modpath = ub.compressuser(modpath, home='%HOME%')
            cmdline = 'python -B ' + modpath.replace('\\', '/')
        else:
            modpath = ub.compressuser(modpath, home='~')
            cmdline = 'python ' + modpath

    if test_code is None:
        test_code = ub.codeblock(
            r'''
            import xdoctest
            xdoctest.doctest_module(__file__)
            ''')
        if argv is None:
            argv = ['all']

    if argv is None:
        argv = []

    cmdline_ = ub.indent(cmdline + ' ' + ' '.join(argv), ' ' * 8).lstrip(' ')
    test_code = ub.indent(test_code, ' ' * 4).lstrip(' ')
    text = ub.codeblock(
        r'''
        if __name__ == '__main__':
            {rr}"""
            CommandLine:
                {cmdline_}
            """
            {test_code}
        '''
    ).format(cmdline_=cmdline_, test_code=test_code, rr='{r}')
    text = text.format(r='r' if '\\' in text else '')
    return text