forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirstack.py
575 lines (482 loc) · 16.6 KB
/
dirstack.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# -*- coding: utf-8 -*-
"""Directory stack and associated utilities for the xonsh shell."""
import os
import glob
import argparse
import builtins
import subprocess
from xonsh.lazyasd import lazyobject
from xonsh.tools import get_sep
from xonsh.events import events
from xonsh.platform import ON_WINDOWS
DIRSTACK = []
"""A list containing the currently remembered directories."""
_unc_tempDrives = {}
""" drive: sharePath for temp drive letters we create for UNC mapping"""
def _unc_check_enabled() -> bool:
r"""Check whether CMD.EXE is enforcing no-UNC-as-working-directory check.
Check can be disabled by setting {HKCU, HKLM}/SOFTWARE\Microsoft\Command Processor\DisableUNCCheck:REG_DWORD=1
Returns:
True if `CMD.EXE` is enforcing the check (default Windows situation)
False if check is explicitly disabled.
"""
if not ON_WINDOWS:
return
import winreg
wval = None
try:
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER, r"software\microsoft\command processor"
)
wval, wtype = winreg.QueryValueEx(key, "DisableUNCCheck")
winreg.CloseKey(key)
except OSError:
pass
if wval is None:
try:
key2 = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE, r"software\microsoft\command processor"
)
wval, wtype = winreg.QueryValueEx(key2, "DisableUNCCheck")
winreg.CloseKey(key2)
except OSError as e: # NOQA
pass
return False if wval else True
def _is_unc_path(some_path) -> bool:
"""True if path starts with 2 backward (or forward, due to python path hacking) slashes."""
return (
len(some_path) > 1
and some_path[0] == some_path[1]
and some_path[0] in (os.sep, os.altsep)
)
def _unc_map_temp_drive(unc_path) -> str:
r"""Map a new temporary drive letter for each distinct share,
unless `CMD.EXE` is not insisting on non-UNC working directory.
Emulating behavior of `CMD.EXE` `pushd`, create a new mapped drive (starting from Z: towards A:, skipping existing
drive letters) for each new UNC path user selects.
Args:
unc_path: the path specified by user. Assumed to be a UNC path of form \\<server>\share...
Returns:
a replacement for `unc_path` to be used as the actual new working directory.
Note that the drive letter may be a the same as one already mapped if the server and share portion of `unc_path`
is the same as one still active on the stack.
"""
global _unc_tempDrives
assert unc_path[1] in (os.sep, os.altsep), "unc_path is UNC form of path"
if not _unc_check_enabled():
return unc_path
else:
unc_share, rem_path = os.path.splitdrive(unc_path)
unc_share = unc_share.casefold()
for d in _unc_tempDrives:
if _unc_tempDrives[d] == unc_share:
return os.path.join(d, rem_path)
for dord in range(ord("z"), ord("a"), -1):
d = chr(dord) + ":"
if not os.path.isdir(d): # find unused drive letter starting from z:
subprocess.check_output(
["NET", "USE", d, unc_share], universal_newlines=True
)
_unc_tempDrives[d] = unc_share
return os.path.join(d, rem_path)
def _unc_unmap_temp_drive(left_drive, cwd):
"""Unmap a temporary drive letter if it is no longer needed.
Called after popping `DIRSTACK` and changing to new working directory, so we need stack *and*
new current working directory to be sure drive letter no longer needed.
Args:
left_drive: driveletter (and colon) of working directory we just left
cwd: full path of new current working directory
"""
global _unc_tempDrives
if left_drive not in _unc_tempDrives: # if not one we've mapped, don't unmap it
return
for p in DIRSTACK + [cwd]: # if still in use , don't unmap it.
if p.casefold().startswith(left_drive):
return
_unc_tempDrives.pop(left_drive)
subprocess.check_output(
["NET", "USE", left_drive, "/delete"], universal_newlines=True
)
events.doc(
"on_chdir",
"""
on_chdir(olddir: str, newdir: str) -> None
Fires when the current directory is changed for any reason.
""",
)
def _get_cwd():
try:
return os.getcwd()
except (OSError, FileNotFoundError):
return None
def _change_working_directory(newdir, follow_symlinks=False):
env = builtins.__xonsh__.env
old = env["PWD"]
new = os.path.join(old, newdir)
absnew = os.path.abspath(new)
if follow_symlinks:
absnew = os.path.realpath(absnew)
try:
os.chdir(absnew)
except (OSError, FileNotFoundError):
if new.endswith(get_sep()):
new = new[:-1]
if os.path.basename(new) == "..":
env["PWD"] = new
else:
if old is not None:
env["OLDPWD"] = old
if new is not None:
env["PWD"] = absnew
# Fire event if the path actually changed
if old != env["PWD"]:
events.on_chdir.fire(olddir=old, newdir=env["PWD"])
def _try_cdpath(apath):
# NOTE: this CDPATH implementation differs from the bash one.
# In bash if a CDPATH is set, an unqualified local folder
# is considered after all CDPATHs, example:
# CDPATH=$HOME/src (with src/xonsh/ inside)
# $ cd xonsh -> src/xonsh (with xonsh/xonsh)
# a second $ cd xonsh has no effects, to move in the nested xonsh
# in bash a full $ cd ./xonsh is needed.
# In xonsh a relative folder is always preferred.
env = builtins.__xonsh__.env
cdpaths = env.get("CDPATH")
for cdp in cdpaths:
globber = builtins.__xonsh__.expand_path(os.path.join(cdp, apath))
for cdpath_prefixed_path in glob.iglob(globber):
return cdpath_prefixed_path
return apath
def cd(args, stdin=None):
"""Changes the directory.
If no directory is specified (i.e. if `args` is None) then this
changes to the current user's home directory.
"""
env = builtins.__xonsh__.env
oldpwd = env.get("OLDPWD", None)
cwd = env["PWD"]
follow_symlinks = False
if len(args) > 0 and args[0] == "-P":
follow_symlinks = True
del args[0]
if len(args) == 0:
d = os.path.expanduser("~")
elif len(args) == 1:
d = os.path.expanduser(args[0])
if not os.path.isdir(d):
if d == "-":
if oldpwd is not None:
d = oldpwd
else:
return "", "cd: no previous directory stored\n", 1
elif d.startswith("-"):
try:
num = int(d[1:])
except ValueError:
return "", "cd: Invalid destination: {0}\n".format(d), 1
if num == 0:
return None, None, 0
elif num < 0:
return "", "cd: Invalid destination: {0}\n".format(d), 1
elif num > len(DIRSTACK):
e = "cd: Too few elements in dirstack ({0} elements)\n"
return "", e.format(len(DIRSTACK)), 1
else:
d = DIRSTACK[num - 1]
else:
d = _try_cdpath(d)
else:
return (
"",
(
"cd takes 0 or 1 arguments, not {0}. An additional `-P` "
"flag can be passed in first position to follow symlinks."
"\n".format(len(args))
),
1,
)
if not os.path.exists(d):
return "", "cd: no such file or directory: {0}\n".format(d), 1
if not os.path.isdir(d):
return "", "cd: {0} is not a directory\n".format(d), 1
if not os.access(d, os.X_OK):
return "", "cd: permission denied: {0}\n".format(d), 1
if (
ON_WINDOWS
and _is_unc_path(d)
and _unc_check_enabled()
and (not env.get("AUTO_PUSHD"))
):
return (
"",
"cd: can't cd to UNC path on Windows, unless $AUTO_PUSHD set or reg entry "
+ r"HKCU\SOFTWARE\MICROSOFT\Command Processor\DisableUNCCheck:DWORD = 1"
+ "\n",
1,
)
# now, push the directory onto the dirstack if AUTO_PUSHD is set
if cwd is not None and env.get("AUTO_PUSHD"):
pushd(["-n", "-q", cwd])
if ON_WINDOWS and _is_unc_path(d):
d = _unc_map_temp_drive(d)
_change_working_directory(d, follow_symlinks)
return None, None, 0
@lazyobject
def pushd_parser():
parser = argparse.ArgumentParser(prog="pushd")
parser.add_argument("dir", nargs="?")
parser.add_argument(
"-n",
dest="cd",
help="Suppresses the normal change of directory when"
" adding directories to the stack, so that only the"
" stack is manipulated.",
action="store_false",
)
parser.add_argument(
"-q",
dest="quiet",
help="Do not call dirs, regardless of $PUSHD_SILENT",
action="store_true",
)
return parser
def pushd(args, stdin=None):
r"""xonsh command: pushd
Adds a directory to the top of the directory stack, or rotates the stack,
making the new top of the stack the current working directory.
On Windows, if the path is a UNC path (begins with `\\<server>\<share>`) and if the `DisableUNCCheck` registry
value is not enabled, creates a temporary mapped drive letter and sets the working directory there, emulating
behavior of `PUSHD` in `CMD.EXE`
"""
global DIRSTACK
try:
args = pushd_parser.parse_args(args)
except SystemExit:
return None, None, 1
env = builtins.__xonsh__.env
pwd = env["PWD"]
if env.get("PUSHD_MINUS", False):
BACKWARD = "-"
FORWARD = "+"
else:
BACKWARD = "+"
FORWARD = "-"
if args.dir is None:
try:
new_pwd = DIRSTACK.pop(0)
except IndexError:
e = "pushd: Directory stack is empty\n"
return None, e, 1
elif os.path.isdir(args.dir):
new_pwd = args.dir
else:
try:
num = int(args.dir[1:])
except ValueError:
e = "Invalid argument to pushd: {0}\n"
return None, e.format(args.dir), 1
if num < 0:
e = "Invalid argument to pushd: {0}\n"
return None, e.format(args.dir), 1
if num > len(DIRSTACK):
e = "Too few elements in dirstack ({0} elements)\n"
return None, e.format(len(DIRSTACK)), 1
elif args.dir.startswith(FORWARD):
if num == len(DIRSTACK):
new_pwd = None
else:
new_pwd = DIRSTACK.pop(len(DIRSTACK) - 1 - num)
elif args.dir.startswith(BACKWARD):
if num == 0:
new_pwd = None
else:
new_pwd = DIRSTACK.pop(num - 1)
else:
e = "Invalid argument to pushd: {0}\n"
return None, e.format(args.dir), 1
if new_pwd is not None:
if ON_WINDOWS and _is_unc_path(new_pwd):
new_pwd = _unc_map_temp_drive(new_pwd)
if args.cd:
DIRSTACK.insert(0, os.path.expanduser(pwd))
_change_working_directory(new_pwd)
else:
DIRSTACK.insert(0, os.path.expanduser(new_pwd))
maxsize = env.get("DIRSTACK_SIZE")
if len(DIRSTACK) > maxsize:
DIRSTACK = DIRSTACK[:maxsize]
if not args.quiet and not env.get("PUSHD_SILENT"):
return dirs([], None)
return None, None, 0
@lazyobject
def popd_parser():
parser = argparse.ArgumentParser(prog="popd")
parser.add_argument("dir", nargs="?")
parser.add_argument(
"-n",
dest="cd",
help="Suppresses the normal change of directory when"
" adding directories to the stack, so that only the"
" stack is manipulated.",
action="store_false",
)
parser.add_argument(
"-q",
dest="quiet",
help="Do not call dirs, regardless of $PUSHD_SILENT",
action="store_true",
)
return parser
def popd(args, stdin=None):
"""
xonsh command: popd
Removes entries from the directory stack.
"""
global DIRSTACK
try:
args = pushd_parser.parse_args(args)
except SystemExit:
return None, None, 1
env = builtins.__xonsh__.env
if env.get("PUSHD_MINUS"):
BACKWARD = "-"
FORWARD = "+"
else:
BACKWARD = "-"
FORWARD = "+"
if args.dir is None:
try:
new_pwd = DIRSTACK.pop(0)
except IndexError:
e = "popd: Directory stack is empty\n"
return None, e, 1
else:
try:
num = int(args.dir[1:])
except ValueError:
e = "Invalid argument to popd: {0}\n"
return None, e.format(args.dir), 1
if num < 0:
e = "Invalid argument to popd: {0}\n"
return None, e.format(args.dir), 1
if num > len(DIRSTACK):
e = "Too few elements in dirstack ({0} elements)\n"
return None, e.format(len(DIRSTACK)), 1
elif args.dir.startswith(FORWARD):
if num == len(DIRSTACK):
new_pwd = DIRSTACK.pop(0)
else:
new_pwd = None
DIRSTACK.pop(len(DIRSTACK) - 1 - num)
elif args.dir.startswith(BACKWARD):
if num == 0:
new_pwd = DIRSTACK.pop(0)
else:
new_pwd = None
DIRSTACK.pop(num - 1)
else:
e = "Invalid argument to popd: {0}\n"
return None, e.format(args.dir), 1
if new_pwd is not None:
e = None
if args.cd:
env = builtins.__xonsh__.env
pwd = env["PWD"]
_change_working_directory(new_pwd)
if ON_WINDOWS:
drive, rem_path = os.path.splitdrive(pwd)
_unc_unmap_temp_drive(drive.casefold(), new_pwd)
if not args.quiet and not env.get("PUSHD_SILENT"):
return dirs([], None)
return None, None, 0
@lazyobject
def dirs_parser():
parser = argparse.ArgumentParser(prog="dirs")
parser.add_argument("N", nargs="?")
parser.add_argument(
"-c",
dest="clear",
help="Clears the directory stack by deleting all of" " the entries.",
action="store_true",
)
parser.add_argument(
"-p",
dest="print_long",
help="Print the directory stack with one entry per" " line.",
action="store_true",
)
parser.add_argument(
"-v",
dest="verbose",
help="Print the directory stack with one entry per"
" line, prefixing each entry with its index in the"
" stack.",
action="store_true",
)
parser.add_argument(
"-l",
dest="long",
help="Produces a longer listing; the default listing"
" format uses a tilde to denote the home directory.",
action="store_true",
)
return parser
def dirs(args, stdin=None):
"""xonsh command: dirs
Displays the list of currently remembered directories. Can also be used
to clear the directory stack.
"""
global DIRSTACK
try:
args = dirs_parser.parse_args(args)
except SystemExit:
return None, None
env = builtins.__xonsh__.env
dirstack = [os.path.expanduser(env["PWD"])] + DIRSTACK
if env.get("PUSHD_MINUS"):
BACKWARD = "-"
FORWARD = "+"
else:
BACKWARD = "-"
FORWARD = "+"
if args.clear:
DIRSTACK = []
return None, None, 0
if args.long:
o = dirstack
else:
d = os.path.expanduser("~")
o = [i.replace(d, "~") for i in dirstack]
if args.verbose:
out = ""
pad = len(str(len(o) - 1))
for (ix, e) in enumerate(o):
blanks = " " * (pad - len(str(ix)))
out += "\n{0}{1} {2}".format(blanks, ix, e)
out = out[1:]
elif args.print_long:
out = "\n".join(o)
else:
out = " ".join(o)
N = args.N
if N is not None:
try:
num = int(N[1:])
except ValueError:
e = "Invalid argument to dirs: {0}\n"
return None, e.format(N), 1
if num < 0:
e = "Invalid argument to dirs: {0}\n"
return None, e.format(len(o)), 1
if num >= len(o):
e = "Too few elements in dirstack ({0} elements)\n"
return None, e.format(len(o)), 1
if N.startswith(BACKWARD):
idx = num
elif N.startswith(FORWARD):
idx = len(o) - 1 - num
else:
e = "Invalid argument to dirs: {0}\n"
return None, e.format(N), 1
out = o[idx]
return out + "\n", None, 0