forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.py
160 lines (139 loc) · 4.59 KB
/
aliases.py
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
150
151
152
153
154
155
156
157
158
159
160
"""Aliases for the xonsh shell."""
import os
import shlex
import builtins
import subprocess
from warnings import warn
from argparse import ArgumentParser
from xonsh.dirstack import cd, pushd, popd, dirs
from xonsh.jobs import jobs, fg, bg, kill_all_jobs
from xonsh.timings import timeit_alias
from xonsh.tools import ON_MAC, ON_WINDOWS, XonshError
from xonsh.history import main as history_alias
from xonsh.replay import main as replay_main
from xonsh.environ import locate_binary
def exit(args, stdin=None): # pylint:disable=redefined-builtin,W0622
"""Sends signal to exit shell."""
builtins.__xonsh_exit__ = True
kill_all_jobs()
print() # gimme a newline
return None, None
def source_bash(args, stdin=None):
"""Implements bash's source builtin."""
import tempfile
env = builtins.__xonsh_env__
denv = env.detype()
with tempfile.NamedTemporaryFile(mode='w+t') as f:
args = ' '.join(args)
inp = 'source {0}\nenv >> {1}\n'.format(args, f.name)
try:
subprocess.check_output(['bash'],
input=inp,
env=denv,
stderr=subprocess.PIPE,
universal_newlines=True)
except subprocess.CalledProcessError:
return None, 'could not source {0}\n'.format(args)
f.seek(0)
exported = f.read()
items = [l.split('=', 1) for l in exported.splitlines() if '=' in l]
newenv = dict(items)
for k, v in newenv.items():
if k in env and v == denv[k]:
continue # no change from original
env[k] = v
return
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"""
for fname in args:
if not os.path.isfile(fname):
fname = locate_binary(fname, cwd=None)[:-1]
with open(fname, 'r') as fp:
execx(fp.read(), 'exec', builtins.__xonsh_ctx__)
def xexec(args, stdin=None):
"""Replaces current process with command specified and passes in the
current xonsh environment.
"""
env = builtins.__xonsh_env__
denv = env.detype()
if len(args) > 0:
try:
os.execvpe(args[0], args, denv)
except FileNotFoundError as e:
return 'xonsh: ' + e.args[1] + ': ' + args[0] + '\n'
else:
return 'xonsh: exec: no args specified\n'
_BANG_N_PARSER = None
def bang_n(args, stdin=None):
"""Re-runs the nth command as specified in the argument."""
global _BANG_N_PARSER
if _BANG_N_PARSER is None:
parser = _BANG_N_PARSER = ArgumentParser('!n', usage='!n <n>',
description="Re-runs the nth command as specified in the argument.")
parser.add_argument('n', type=int, help='the command to rerun, may be negative')
else:
parser = _BANG_N_PARSER
ns = parser.parse_args(args)
hist = builtins.__xonsh_history__
nhist = len(hist)
n = nhist + ns.n if ns.n < 0 else ns.n
if n < 0 or n >= nhist:
raise IndexError('n out of range, {0} for history len {1}'.format(ns.n, nhist))
cmd = hist.inps[n]
if cmd.startswith('!'):
raise XonshError('xonsh: error: recursive call to !n')
builtins.execx(cmd)
def bang_bang(args, stdin=None):
"""Re-runs the last command. Just a wrapper around bang_n."""
return bang_n(['-1'])
DEFAULT_ALIASES = {
'cd': cd,
'pushd': pushd,
'popd': popd,
'dirs': dirs,
'jobs': jobs,
'fg': fg,
'bg': bg,
'EOF': exit,
'exit': exit,
'quit': exit,
'xexec': xexec,
'source': source_alias,
'source-bash': source_bash,
'history': history_alias,
'replay': replay_main,
'!!': bang_bang,
'!n': bang_n,
'timeit': timeit_alias,
'scp-resume': ['rsync', '--partial', '-h', '--progress', '--rsh=ssh'],
'ipynb': ['ipython', 'notebook', '--no-browser'],
}
if ON_WINDOWS:
# Borrow builtin commands from cmd.exe.
WINDOWS_CMD_ALIASES = {
'cls',
'copy',
'del',
'dir',
'erase',
'md',
'mkdir',
'mklink',
'move',
'rd',
'ren',
'rename',
'rmdir',
'time',
'type',
'vol'
}
for alias in WINDOWS_CMD_ALIASES:
DEFAULT_ALIASES[alias] = ['cmd', '/c', alias]
DEFAULT_ALIASES['which'] = ['where']
elif ON_MAC:
DEFAULT_ALIASES['ls'] = ['ls', '-G']
else:
DEFAULT_ALIASES['grep'] = ['grep', '--color=auto']
DEFAULT_ALIASES['ls'] = ['ls', '--color=auto', '-v']