Пример #1
0
def source_alias(args, stdin=None):
    """Executes the contents of the provided files in the current context.
    If sourced file isn't found in cwd, search for file along $PATH to source
    instead.
    """
    env = builtins.__xonsh_env__
    encoding = env.get('XONSH_ENCODING')
    errors = env.get('XONSH_ENCODING_ERRORS')
    for i, fname in enumerate(args):
        fpath = fname
        if not os.path.isfile(fpath):
            fpath = locate_binary(fname)
            if fpath is None:
                if env.get('XONSH_DEBUG'):
                    print('source: {}: No such file'.format(fname),
                          file=sys.stderr)
                if i == 0:
                    raise RuntimeError('must source at least one file, ' +
                                       fname + 'does not exist.')
                break
        with open(fpath, 'r', encoding=encoding, errors=errors) as fp:
            src = fp.read()
        if not src.endswith('\n'):
            src += '\n'
        ctx = builtins.__xonsh_ctx__
        updates = {'__file__': fpath, '__name__': os.path.abspath(fpath)}
        with env.swap(ARGS=args[i + 1:]), swap_values(ctx, updates):
            builtins.execx(src, 'exec', ctx, filename=fpath)
Пример #2
0
def source_alias(args, stdin=None):
    """Executes the contents of the provided files in the current context.
    If sourced file isn't found in cwd, search for file along $PATH to source
    instead.
    """
    env = builtins.__xonsh_env__
    encoding = env.get('XONSH_ENCODING')
    errors = env.get('XONSH_ENCODING_ERRORS')
    for i, fname in enumerate(args):
        fpath = fname
        if not os.path.isfile(fpath):
            fpath = locate_binary(fname)
            if fpath is None:
                if env.get('XONSH_DEBUG'):
                    print('source: {}: No such file'.format(fname), file=sys.stderr)
                if i == 0:
                    raise RuntimeError('must source at least one file, ' + fname +
                                       'does not exist.')
                break
        with open(fpath, 'r', encoding=encoding, errors=errors) as fp:
            src = fp.read()
        if not src.endswith('\n'):
            src += '\n'
        ctx = builtins.__xonsh_ctx__
        updates = {'__file__': fpath, '__name__': os.path.abspath(fpath)}
        with env.swap(ARGS=args[i+1:]), swap_values(ctx, updates):
            builtins.execx(src, 'exec', ctx, filename=fpath)
Пример #3
0
def test_swap_values():
    orig = {"x": 1}
    updates = {"x": 42, "y": 43}
    with swap_values(orig, updates):
        assert orig["x"] == 42
        assert orig["y"] == 43
    assert orig["x"] == 1
    assert "y" not in orig
Пример #4
0
def test_swap_values():
    orig = {'x': 1}
    updates = {'x': 42, 'y': 43}
    with swap_values(orig, updates):
        assert orig['x'] == 42
        assert orig['y'] == 43
    assert orig['x'] == 1
    assert 'y' not in orig
Пример #5
0
def test_swap_values():
    orig = {"x": 1}
    updates = {"x": 42, "y": 43}
    with swap_values(orig, updates):
        assert orig["x"] == 42
        assert orig["y"] == 43
    assert orig["x"] == 1
    assert "y" not in orig
Пример #6
0
def test_swap_values():
    orig = {'x': 1}
    updates = {'x': 42, 'y': 43}
    with swap_values(orig, updates):
        assert orig['x'] == 42
        assert orig['y'] == 43
    assert orig['x'] == 1
    assert 'y' not in orig
Пример #7
0
def source_alias(args, stdin=None):
    """Executes the contents of the provided files in the current context.
    If sourced file isn't found in cwd, search for file along $PATH to source
    instead.
    """
    env = builtins.__xonsh_env__
    encoding = env.get("XONSH_ENCODING")
    errors = env.get("XONSH_ENCODING_ERRORS")
    for i, fname in enumerate(args):
        fpath = fname
        if not os.path.isfile(fpath):
            fpath = locate_binary(fname)
            if fpath is None:
                if env.get("XONSH_DEBUG"):
                    print("source: {}: No such file".format(fname),
                          file=sys.stderr)
                if i == 0:
                    raise RuntimeError("must source at least one file, " +
                                       fname + "does not exist.")
                break
        _, fext = os.path.splitext(fpath)
        if fext and fext != ".xsh" and fext != ".py":
            raise RuntimeError(
                "attempting to source non-xonsh file! If you are "
                "trying to source a file in another language, "
                "then please use the appropriate source command. "
                "For example, source-bash script.sh")
        with open(fpath, "r", encoding=encoding, errors=errors) as fp:
            src = fp.read()
        if not src.endswith("\n"):
            src += "\n"
        ctx = builtins.__xonsh_ctx__
        updates = {"__file__": fpath, "__name__": os.path.abspath(fpath)}
        with env.swap(**make_args_env(args[i + 1:])), swap_values(
                ctx, updates):
            try:
                builtins.execx(src, "exec", ctx, filename=fpath)
            except Exception:
                print_color(
                    "{RED}You may be attempting to source non-xonsh file! "
                    "{NO_COLOR}If you are trying to source a file in "
                    "another language, then please use the appropriate "
                    "source command. For example, {GREEN}source-bash "
                    "script.sh{NO_COLOR}",
                    file=sys.stderr,
                )
                raise
Пример #8
0
def source_alias(args, stdin=None):
    """Executes the contents of the provided files in the current context.
    If sourced file isn't found in cwd, search for file along $PATH to source
    instead.
    """
    env = builtins.__xonsh__.env
    encoding = env.get("XONSH_ENCODING")
    errors = env.get("XONSH_ENCODING_ERRORS")
    for i, fname in enumerate(args):
        fpath = fname
        if not os.path.isfile(fpath):
            fpath = locate_binary(fname)
            if fpath is None:
                if env.get("XONSH_DEBUG"):
                    print("source: {}: No such file".format(fname), file=sys.stderr)
                if i == 0:
                    raise RuntimeError(
                        "must source at least one file, " + fname + "does not exist."
                    )
                break
        _, fext = os.path.splitext(fpath)
        if fext and fext != ".xsh" and fext != ".py":
            raise RuntimeError(
                "attempting to source non-xonsh file! If you are "
                "trying to source a file in another language, "
                "then please use the appropriate source command. "
                "For example, source-bash script.sh"
            )
        with open(fpath, "r", encoding=encoding, errors=errors) as fp:
            src = fp.read()
        if not src.endswith("\n"):
            src += "\n"
        ctx = builtins.__xonsh__.ctx
        updates = {"__file__": fpath, "__name__": os.path.abspath(fpath)}
        with env.swap(**make_args_env(args[i + 1 :])), swap_values(ctx, updates):
            try:
                builtins.execx(src, "exec", ctx, filename=fpath)
            except Exception:
                print_color(
                    "{RED}You may be attempting to source non-xonsh file! "
                    "{NO_COLOR}If you are trying to source a file in "
                    "another language, then please use the appropriate "
                    "source command. For example, {GREEN}source-bash "
                    "script.sh{NO_COLOR}",
                    file=sys.stderr,
                )
                raise
Пример #9
0
def source_alias(args, stdin=None):
    """Executes the contents of the provided files in the current context.
    If sourced file isn't found in cwd, search for file along $PATH to source
    instead.
    """
    env = builtins.__xonsh_env__
    encoding = env.get('XONSH_ENCODING')
    errors = env.get('XONSH_ENCODING_ERRORS')
    for i, fname in enumerate(args):
        fpath = fname
        if not os.path.isfile(fpath):
            fpath = locate_binary(fname)
            if fpath is None:
                if env.get('XONSH_DEBUG'):
                    print('source: {}: No such file'.format(fname),
                          file=sys.stderr)
                if i == 0:
                    raise RuntimeError('must source at least one file, ' +
                                       fname + 'does not exist.')
                break
        _, fext = os.path.splitext(fpath)
        if fext and fext != '.xsh' and fext != '.py':
            raise RuntimeError(
                'attempting to source non-xonsh file! If you are '
                'trying to source a file in another language, '
                'then please use the appropriate source command. '
                'For example, source-bash script.sh')
        with open(fpath, 'r', encoding=encoding, errors=errors) as fp:
            src = fp.read()
        if not src.endswith('\n'):
            src += '\n'
        ctx = builtins.__xonsh_ctx__
        updates = {'__file__': fpath, '__name__': os.path.abspath(fpath)}
        with env.swap(**make_args_env(args[i + 1:])), swap_values(
                ctx, updates):
            try:
                builtins.execx(src, 'exec', ctx, filename=fpath)
            except Exception:
                print_color(
                    '{RED}You may be attempting to source non-xonsh file! '
                    '{NO_COLOR}If you are trying to source a file in '
                    'another language, then please use the appropriate '
                    'source command. For example, {GREEN}source-bash '
                    'script.sh{NO_COLOR}',
                    file=sys.stderr)
                raise
Пример #10
0
def xonsh_script_run_control(filename, ctx, env, execer=None, login=True):
    """Loads a xonsh file and applies it as a run control."""
    if execer is None:
        return False
    updates = {'__file__': filename, '__name__': os.path.abspath(filename)}
    try:
        with swap_values(ctx, updates):
            run_script_with_cache(filename, execer, ctx)
        loaded = True
    except SyntaxError as err:
        msg = 'syntax error in xonsh run control file {0!r}: {1!s}'
        print_exception(msg.format(filename, err))
        loaded = False
    except Exception as err:
        msg = 'error running xonsh run control file {0!r}: {1!s}'
        print_exception(msg.format(filename, err))
        loaded = False
    return loaded
Пример #11
0
def source_alias(args, stdin=None):
    """Executes the contents of the provided files in the current context.
    If sourced file isn't found in cwd, search for file along $PATH to source
    instead.
    """
    env = builtins.__xonsh_env__
    encoding = env.get('XONSH_ENCODING')
    errors = env.get('XONSH_ENCODING_ERRORS')
    for i, fname in enumerate(args):
        fpath = fname
        if not os.path.isfile(fpath):
            fpath = locate_binary(fname)
            if fpath is None:
                if env.get('XONSH_DEBUG'):
                    print('source: {}: No such file'.format(fname), file=sys.stderr)
                if i == 0:
                    raise RuntimeError('must source at least one file, ' + fname +
                                       'does not exist.')
                break
        _, fext = os.path.splitext(fpath)
        if fext and fext != '.xsh' and fext != '.py':
            raise RuntimeError('attempting to source non-xonsh file! If you are '
                               'trying to source a file in another language, '
                               'then please use the appropriate source command. '
                               'For example, source-bash script.sh')
        with open(fpath, 'r', encoding=encoding, errors=errors) as fp:
            src = fp.read()
        if not src.endswith('\n'):
            src += '\n'
        ctx = builtins.__xonsh_ctx__
        updates = {'__file__': fpath, '__name__': os.path.abspath(fpath)}
        with env.swap(ARGS=args[i+1:]), swap_values(ctx, updates):
            try:
                builtins.execx(src, 'exec', ctx, filename=fpath)
            except Exception:
                print_color('{RED}You may be attempting to source non-xonsh file! '
                            '{NO_COLOR}If you are trying to source a file in '
                            'another language, then please use the appropriate '
                            'source command. For example, {GREEN}source-bash '
                            'script.sh{NO_COLOR}', file=sys.stderr)
                raise