def run_file(file, globals=None, locals=None, is_module=False):
    module_name = None
    entry_point_fn = None
    if is_module:
        file, _, entry_point_fn = file.partition(':')
        module_name = file
        filename = get_fullname(file)
        if filename is None:
            sys.stderr.write("No module named %s\n" % file)
            return
        else:
            file = filename

    if os.path.isdir(file):
        new_target = os.path.join(file, '__main__.py')
        if os.path.isfile(new_target):
            file = new_target

    if globals is None:
        m = save_main_module(file, 'pydev_run_in_console')

        globals = m.__dict__
        try:
            globals['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

    if locals is None:
        locals = globals

    if not is_module:
        sys.path.insert(0, os.path.split(file)[0])

    print('Running %s' % file)
    try:
        if not is_module:
            pydev_imports.execfile(file, globals, locals)  # execute the script
        else:
            # treat ':' as a seperator between module and entry point function
            # if there is no entry point we run we same as with -m switch. Otherwise we perform
            # an import and execute the entry point
            if entry_point_fn:
                mod = __import__(module_name,
                                 level=0,
                                 fromlist=[entry_point_fn],
                                 globals=globals,
                                 locals=locals)
                func = getattr(mod, entry_point_fn)
                func()
            else:
                # Run with the -m switch
                import runpy
                if hasattr(runpy, '_run_module_as_main'):
                    runpy._run_module_as_main(module_name)
                else:
                    runpy.run_module(module_name)
    except:
        traceback.print_exc()

    return globals
def run_file(file, globals=None, locals=None, is_module=False):
    module_name = None
    entry_point_fn = None
    if is_module:
        file, _,  entry_point_fn = file.partition(':')
        module_name = file
        filename = get_fullname(file)
        if filename is None:
            sys.stderr.write("No module named %s\n" % file)
            return
        else:
            file = filename

    if os.path.isdir(file):
        new_target = os.path.join(file, '__main__.py')
        if os.path.isfile(new_target):
            file = new_target

    if globals is None:
        m = save_main_module(file, 'pydev_run_in_console')

        globals = m.__dict__
        try:
            globals['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

    if locals is None:
        locals = globals

    if not is_module:
        sys.path.insert(0, os.path.split(file)[0])

    print('Running %s' % file)
    try:
        if not is_module:
            pydev_imports.execfile(file, globals, locals)  # execute the script
        else:
            # treat ':' as a seperator between module and entry point function
            # if there is no entry point we run we same as with -m switch. Otherwise we perform
            # an import and execute the entry point
            if entry_point_fn:
                mod = __import__(module_name, level=0, fromlist=[entry_point_fn], globals=globals, locals=locals)
                func = getattr(mod, entry_point_fn)
                func()
            else:
                # Run with the -m switch
                import runpy
                if hasattr(runpy, '_run_module_as_main'):
                    runpy._run_module_as_main(module_name)
                else:
                    runpy.run_module(module_name)
    except:
        traceback.print_exc()

    return globals
Example #3
0
def runfile(filename, args=None, wdir=None, is_module=False, global_vars=None):
    """
    Run filename
    args: command line arguments (string)
    wdir: working directory
    """
    try:
        if hasattr(filename, 'decode'):
            filename = filename.decode('utf-8')
    except (UnicodeError, TypeError):
        pass

    global __umd__
    if os.environ.get("PYDEV_UMD_ENABLED", "").lower() == "true":
        if __umd__ is None:
            namelist = os.environ.get("PYDEV_UMD_NAMELIST", None)
            if namelist is not None:
                namelist = namelist.split(',')
            __umd__ = UserModuleDeleter(namelist=namelist)
        else:
            verbose = os.environ.get("PYDEV_UMD_VERBOSE", "").lower() == "true"
            __umd__.run(verbose=verbose)

    if global_vars is None:
        m = save_main_module(filename, 'pydev_umd')
        global_vars = _get_interpreter_globals()
        global_vars.update(m.__dict__)
        try:
            global_vars['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

    local_vars = global_vars

    module_name = None
    entry_point_fn = None
    if is_module:
        file, _, entry_point_fn = filename.partition(':')
        module_name = file
        filename = get_fullname(file)
        if filename is None:
            sys.stderr.write("No module named %s\n" % file)
            return
    else:
        # The file being run must be in the pythonpath (even if it was not before)
        sys.path.insert(0, os.path.split(rPath(filename))[0])

    global_vars['__file__'] = filename

    sys.argv = [filename]
    if args is not None:
        for arg in args:
            sys.argv.append(arg)

    if wdir is not None:
        try:
            if hasattr(wdir, 'decode'):
                wdir = wdir.decode('utf-8')
        except (UnicodeError, TypeError):
            pass
        os.chdir(wdir)

    try:
        if not is_module:
            pydev_imports.execfile(filename, global_vars,
                                   local_vars)  # execute the script
        else:
            # treat ':' as a seperator between module and entry point function
            # if there is no entry point we run we same as with -m switch. Otherwise we perform
            # an import and execute the entry point
            if entry_point_fn:
                mod = __import__(module_name,
                                 level=0,
                                 fromlist=[entry_point_fn],
                                 globals=global_vars,
                                 locals=local_vars)
                func = getattr(mod, entry_point_fn)
                func()
            else:
                # Run with the -m switch
                import runpy
                if hasattr(runpy, '_run_module_as_main'):
                    runpy._run_module_as_main(module_name)
                else:
                    runpy.run_module(module_name)
    finally:
        sys.argv = ['']
        interpreter_globals = _get_interpreter_globals()
        interpreter_globals.update(global_vars)
def runfile(filename, args=None, wdir=None, is_module=False, global_vars=None):
    """
    Run filename
    args: command line arguments (string)
    wdir: working directory
    """
    try:
        if hasattr(filename, 'decode'):
            filename = filename.decode('utf-8')
    except (UnicodeError, TypeError):
        pass

    global __umd__
    if os.environ.get("PYDEV_UMD_ENABLED", "").lower() == "true":
        if __umd__ is None:
            namelist = os.environ.get("PYDEV_UMD_NAMELIST", None)
            if namelist is not None:
                namelist = namelist.split(',')
            __umd__ = UserModuleDeleter(namelist=namelist)
        else:
            verbose = os.environ.get("PYDEV_UMD_VERBOSE", "").lower() == "true"
            __umd__.run(verbose=verbose)

    if global_vars is None:
        m = save_main_module(filename, 'pydev_umd')
        global_vars = m.__dict__
        try:
            global_vars['__builtins__'] = __builtins__
        except NameError:
            pass  # Not there on Jython...

    local_vars = global_vars

    module_name = None
    entry_point_fn = None
    if is_module:
        file, _, entry_point_fn = filename.partition(':')
        module_name = file
        filename = get_fullname(file)
        if filename is None:
            sys.stderr.write("No module named %s\n" % file)
            return
    else:
        # The file being run must be in the pythonpath (even if it was not before)
        sys.path.insert(0, os.path.split(rPath(filename))[0])

    global_vars['__file__'] = filename

    sys.argv = [filename]
    if args is not None:
        for arg in args:
            sys.argv.append(arg)

    if wdir is not None:
        try:
            if hasattr(wdir, 'decode'):
                wdir = wdir.decode('utf-8')
        except (UnicodeError, TypeError):
            pass
        os.chdir(wdir)

    try:
        if not is_module:
            pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
        else:
            # treat ':' as a seperator between module and entry point function
            # if there is no entry point we run we same as with -m switch. Otherwise we perform
            # an import and execute the entry point
            if entry_point_fn:
                mod = __import__(module_name, level=0, fromlist=[entry_point_fn], globals=global_vars, locals=local_vars)
                func = getattr(mod, entry_point_fn)
                func()
            else:
                # Run with the -m switch
                import runpy
                if hasattr(runpy, '_run_module_as_main'):
                    runpy._run_module_as_main(module_name)
                else:
                    runpy.run_module(module_name)
    finally:
        sys.argv = ['']
        interpreter_globals = _get_interpreter_globals()
        interpreter_globals.update(global_vars)