File: bashisms.py

package info (click to toggle)
xonsh 0.9.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,524 kB
  • sloc: python: 41,584; makefile: 156; sh: 41; xml: 17
file content (149 lines) | stat: -rw-r--r-- 3,884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Bash-like interface extensions for xonsh."""
import shlex
import sys
import re
import builtins


__all__ = ()


@events.on_transform_command
def bash_preproc(cmd, **kw):
    bang_previous = {
        "!": lambda x: x,
        "$": lambda x: shlex.split(x)[-1],
        "^": lambda x: shlex.split(x)[0],
        "*": lambda x: " ".join(shlex.split(x)[1:]),
    }

    def replace_bang(m):
        arg = m.group(1)
        inputs = __xonsh__.history.inps

        # Dissect the previous command.
        if arg in bang_previous:
            try:
                return bang_previous[arg](inputs[-1])
            except IndexError:
                print("xonsh: no history for '!{}'".format(arg))
                return ""

        # Look back in history for a matching command.
        else:
            try:
                return next((x for x in reversed(inputs) if x.startswith(arg)))
            except StopIteration:
                print("xonsh: no previous commands match '!{}'".format(arg))
                return ""

    return re.sub(r"!([!$^*]|[\w]+)", replace_bang, cmd)


def alias(args, stdin=None):
    ret = 0

    if args:
        for arg in args:
            if "=" in arg:
                # shlex.split to remove quotes, e.g. "foo='echo hey'" into
                # "foo=echo hey"
                name, cmd = shlex.split(arg)[0].split("=", 1)
                aliases[name] = shlex.split(cmd)
            elif arg in aliases:
                print("{}={}".format(arg, aliases[arg]))
            else:
                print("alias: {}: not found".format(arg), file=sys.stderr)
                ret = 1
    else:
        for alias, cmd in aliases.items():
            print("{}={}".format(alias, cmd))

    return ret


aliases["alias"] = alias
builtins.__xonsh__.env["THREAD_SUBPROCS"] = False


def _unset(args):
    if not args:
        print("Usage: unset ENV_VARIABLE", file=sys.stderr)

    for v in args:
        try:
            __xonsh__.env.pop(v)
        except KeyError:
            print(f"{v} not found", file=sys.stderr)


aliases["unset"] = _unset


def _export(args):
    if not args:
        print("Usage: export ENV_VARIABLE=VALUE", file=sys.stderr)

    for eq in args:
        if "=" in eq:
            name, val = shlex.split(eq)[0].split("=", 1)
            __xonsh__.env[name] = val
        else:
            print(f"{eq} equal sign not found", file=sys.stderr)


aliases["export"] = _export


def _set(args):
    arg = args[0]
    if arg == "-e":
        __xonsh__.env["RAISE_SUBPROC_ERROR"] = True
    elif arg == "+e":
        __xonsh__.env["RAISE_SUBPROC_ERROR"] = False
    elif arg == "-x":
        __xonsh__.env["XONSH_TRACE_SUBPROC"] = True
    elif arg == "+x":
        __xonsh__.env["XONSH_TRACE_SUBPROC"] = False
    else:
        print(
            "Not supported in xontrib bashisms.\nPRs are welcome - https://github.com/xonsh/xonsh/blob/master/xontrib/bashisms.py",
            file=sys.stderr,
        )


aliases["set"] = _set


def _shopt(args):

    supported_shopt = ["DOTGLOB"]

    args_len = len(args)
    if args_len == 0:
        for so in supported_shopt:
            onoff = "on" if so in __xonsh__.env and __xonsh__.env[so] else "off"
            print(f"dotglob\t{onoff}")
        return
    elif args_len < 2 or args[0] in ["-h", "--help"]:
        print(f'Usage: shopt <-s|-u> <{"|".join(supported_shopt).lower()}>')
        return

    opt = args[0]
    optname = args[1]

    if opt == "-s" and optname == "dotglob":
        __xonsh__.env["DOTGLOB"] = True
    elif opt == "-u" and optname == "dotglob":
        __xonsh__.env["DOTGLOB"] = False
    else:
        print(
            "Not supported in xontrib bashisms.\nPRs are welcome - https://github.com/xonsh/xonsh/blob/master/xontrib/bashisms.py",
            file=sys.stderr,
        )


aliases["shopt"] = _shopt


aliases["complete"] = "completer list"