-
Notifications
You must be signed in to change notification settings - Fork 8
/
edir.py
658 lines (562 loc) · 22.5 KB
/
edir.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env python3
'''
Program to rename, remove, or copy files and directories using your
editor. Will use git to action the rename and remove if run within a git
repository.
'''
# Author: Mark Blakeney, May 2019.
from __future__ import annotations
import argparse
import itertools
import os
import re
import shlex
import shutil
import subprocess
import sys
import tempfile
from collections import OrderedDict
from pathlib import Path
from typing import Callable, Sequence
from platformdirs import user_config_path
# Some constants
PROG = Path(sys.argv[0]).stem
CNFFILE = user_config_path() / f'{PROG}-flags.conf'
EDITOR = PROG.upper() + '_EDITOR'
SUFFIX = '.sh'
SEP = os.sep
# The temp dir we will use in the dir of each target move
TEMPDIR = '.tmp-' + PROG
# Define action verbs and ANSI escape sequences for action colors
# Refer https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
ACTIONS = {
'remove': (('\033[31m', '\033[30;41m'), # Red
('Removing', 'Remove', 'Removed')),
'copy': (('\033[32m', '\033[30;42m'), # Green
('Copying', 'Copy', 'Copied')),
'rename': (('\033[33m', '\033[30;43m'), # Yellow
('Renaming', 'Rename', 'Renamed')),
}
COLOR_reset = '\033[39;49m'
args = argparse.Namespace()
gitfiles = set()
counts = [0, 0]
EDITORS = {'Windows': 'notepad', 'Darwin': 'open -e', 'default': 'vim'}
def get_default_editor() -> str:
'Return default editor for this system'
from platform import system
return EDITORS.get(system(), EDITORS['default'])
def editfile(filename: Path) -> None:
'Run the editor command'
# Use explicit user defined editor or choose system default
editor = os.getenv(EDITOR) or os.getenv('EDITOR') or get_default_editor()
editcmd = shlex.split(editor) + [str(filename)]
# Run the editor ..
if sys.stdin.isatty():
res = subprocess.run(editcmd)
else:
with open('/dev/tty') as tty:
res = subprocess.run(editcmd, stdin=tty)
# Check if editor returned error
if res.returncode != 0:
sys.exit(f'ERROR: {editor} returned error {res.returncode}')
def log(action: str, msg: str, error: str | None = None, *,
prompt: bool = False) -> None:
'Output message with appropriate color'
counts[bool(error)] += 1
colors, tense = ACTIONS[action]
if error:
out = sys.stderr
msg = f'{tense[1]} {msg} ERROR: {error}'
elif args.quiet and not prompt:
return
else:
out = sys.stdout
msg = f'{tense[0] if prompt else tense[2]} {msg}'
if not args.no_color:
msg = colors[bool(error and not args.no_invert_color)] \
+ msg + COLOR_reset
print(msg, file=out)
def run(cmd: Sequence[str]) -> tuple[str, str]:
'Run given command and return (stdout, stderr) strings'
stdout = ''
stderr = ''
try:
res = subprocess.run(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
except Exception as e:
stderr = str(e)
else:
if res.stdout:
stdout = res.stdout.strip()
if res.stderr:
stderr = res.stderr.strip()
return stdout, stderr
def remove(path: Path, git: bool = False, trash: bool = False,
recurse: bool = False) -> str | None:
'Delete given file/directory'
if not recurse and not path.is_symlink() and path.is_dir() and \
any(path.iterdir()):
return 'Directory not empty'
if git:
cmd = 'git rm -f'.split()
if recurse:
cmd.append('-r')
out, err = run(cmd + [str(path)])
return f'git error: {err}' if err else None
if trash:
out, err = run(args.trash_program + [str(path)])
return f'{shlex.join(args.trash_program)} error: {err}' if err else None
if recurse and not path.is_symlink():
try:
shutil.rmtree(str(path))
except Exception as e:
return str(e)
else:
try:
if not path.is_symlink() and path.is_dir():
path.rmdir()
else:
path.unlink()
except Exception as e:
return str(e)
def rename(pathsrc: Path, pathdest: Path, is_git: bool = False) -> str:
'Rename given pathsrc to pathdest'
if is_git:
out, err = run('git mv -f'.split() + [str(pathsrc), str(pathdest)])
if err:
err = f'git mv error: {err}'
else:
try:
shutil.move(str(pathsrc), str(pathdest))
except Exception as e:
# Remove any trailing colon and specific file since we may
# be renaming a temp file.
err = str(e).split(':')[0]
else:
err = ''
return err
def create_prompt(options: str) -> str:
'Create a string of options + keys for user to choose from'
letters = []
words = []
for arg in options.split():
subwords = []
for subarg in arg.split('/'):
letters.append(subarg[0])
subwords.append(f'({subarg[0].upper()}){subarg[1:]}')
words.append('/'.join(subwords))
return ', '.join(words) + ': [' + '|'.join(letters) + ']? '
class Fpath:
'Class to manage each instance of a file/dir'
paths = []
tempdirs = set()
def __init__(self, path: Path):
'Class constructor'
self.path = path
self.newpath = None
self.temppath = None
self.note = ''
self.copies: list[Path] = []
try:
self.is_dir = path.is_dir() and not path.is_symlink()
except Exception as e:
sys.exit(f'ERROR: Can not read {path}: {e}')
self.appdash = SEP if self.is_dir else ''
self.diagrepr = str(self.path)
self.is_git = self.diagrepr in gitfiles
self.linerepr = self.diagrepr if self.path.is_absolute() \
else f'.{SEP}{self.diagrepr}'
if self.is_dir and not self.diagrepr.endswith(SEP):
self.linerepr += SEP
self.diagrepr += SEP
@staticmethod
def inc_path(path: Path) -> Path:
'Find next unique file name'
# Iterate forever, there can only be a finite number of existing
# paths
name = path.name
for c in itertools.count():
if not path.is_symlink() and not path.exists():
break
path = path.with_name(name + ('~' if c <= 0 else f'~{c}'))
return path
def copy(self, pathdest: Path) -> str | None:
'Copy given pathsrc to pathdest'
if self.is_dir:
func: Callable = shutil.copytree
else:
func = shutil.copy2
# copytree() will create the parent dir[s], but copy2() will not
try:
pathdest.parent.mkdir(parents=True, exist_ok=True)
except Exception as e:
return str(e)
try:
func(str(self.newpath), str(pathdest)) # type:ignore
except Exception as e:
return str(e)
def rename_temp(self) -> str | None:
'Move this path to a temp place in advance of final move'
if not self.newpath:
return None
tempdir = self.newpath.parent / TEMPDIR
try:
tempdir.mkdir(parents=True, exist_ok=True)
except Exception as e:
# Remove any trailing colon and specific file since we are
# renaming a temp file.
return str(e).split(':')[0]
self.tempdirs.add(tempdir)
temppath = self.inc_path(tempdir / self.newpath.name)
if not (err := rename(self.path, temppath, self.is_git)):
self.temppath = temppath
return err
def restore_temp(self) -> str | None:
'Restore temp path to final destination'
if not self.temppath or not self.newpath:
return None
self.newpath = self.inc_path(self.newpath)
return rename(self.temppath, self.newpath, self.is_git)
def sort_name(self) -> str:
'Return name for sort'
return str(self.path)
def sort_time(self) -> float:
'Return time for sort'
try:
ret = self.path.lstat().st_mtime
except Exception:
ret = 0
return ret
def sort_size(self) -> int:
'Return size for sort'
try:
ret = self.path.lstat().st_size
except Exception:
ret = 0
return ret
def is_recursive(self) -> bool:
'Return True if directory and we can view it and contains children'
if not self.is_dir:
return False
try:
return any(self.path.iterdir())
except Exception:
return False
def log_pending_changes(self) -> None:
'Log all pending changes for this path'
if not self.newpath:
log('remove', f'"{self.diagrepr}"{self.note}', prompt=True)
elif self.newpath != self.path:
log('rename', f'"{self.diagrepr}" to '
f'"{self.newpath}{self.appdash}"', prompt=True)
for c in self.copies:
log('copy', f'"{self.diagrepr}" to '
f'"{c}{self.appdash}"{self.note}', prompt=True)
@classmethod
def remove_temps(cls) -> None:
'Remove all the temp dirs we created in rename_temp() above'
for p in cls.tempdirs:
remove(p, git=False, trash=False, recurse=True)
cls.tempdirs.clear()
@classmethod
def append(cls, path: Path) -> None:
'Add a single file/dir to the list of paths'
# Filter out files/dirs if asked
if args.files:
if path.is_dir():
return
elif args.dirs:
if not path.is_dir():
return
# Filter out links if asked
if args.nolinks and path.is_symlink():
return
cls.paths.append(cls(path))
@classmethod
def add(cls, name: str, expand: bool) -> None:
'Add file[s]/dir[s] to the list of paths'
path = Path(name)
if not path.exists():
sys.exit(f'ERROR: {name} does not exist')
if expand and path.is_dir():
for child in sorted(path.iterdir()):
if args.all or not child.name.startswith('.'):
cls.append(child)
else:
cls.append(path)
@classmethod
def writefile(cls, fpath: Path) -> None:
'Write the file for user to edit'
with fpath.open('w') as fp:
# Ensure consistent width for line numbers
num_width = len(str(len(cls.paths)))
fp.writelines(f'{i:0{num_width}} {p.linerepr}\n'
for i, p in enumerate(cls.paths, 1))
@classmethod
def readfile(cls, fpath: Path) -> None:
'Read the list of files/dirs as edited by user'
# Reset all the read path changes to null state
for p in cls.paths:
p.newpath = None
p.copies.clear()
# Now read file and record changes
with fpath.open() as fp:
for count, line in enumerate(fp, 1):
# Skip blank or commented lines
rawline = line.rstrip('\n\r')
line = rawline.lstrip()
if not line or line[0] == '#':
continue
try:
n, pathstr = line.split(maxsplit=1)
except Exception:
sys.exit(f'ERROR: line {count} invalid:\n{rawline}')
try:
num = int(n)
except Exception:
sys.exit(f'ERROR: line {count} number {n} invalid:'
f'\n{rawline}')
if num <= 0 or num > len(cls.paths):
sys.exit(f'ERROR: line {count} number {num} '
f'out of range:\n{rawline}')
path = cls.paths[num - 1]
if len(pathstr) > 1:
pathstr = pathstr.rstrip(SEP)
newpath = Path(pathstr)
if path.newpath:
if newpath != path.path and newpath not in path.copies:
path.copies.append(newpath)
else:
path.newpath = newpath
@classmethod
def get_path_changes(cls) -> list:
'Get a list of change paths from the user'
prompt = create_prompt('proceed/yes edit restart quit[default]') \
if args.interactive else None
with tempfile.TemporaryDirectory() as fdir:
# Create a temp file for the user to edit then read the lines back
fpath = Path(fdir, f'{PROG}{args.suffix}')
restart = True
while True:
if restart:
restart = False
cls.writefile(fpath)
# Invoke editor on list of paths
editfile(fpath)
# Read the changed paths
cls.readfile(fpath)
# Reduce paths to those that were removed or changed by the user
paths = [p for p in cls.paths
if p.path != p.newpath or p.copies]
if not paths:
return []
# Lazy eval the next path value
for p in paths:
p.note = ' recursively' if p.is_recursive() else ''
if not prompt:
return paths
# Prompt user with pending changes if required
for p in paths:
p.log_pending_changes()
while True:
try:
ans = input(prompt).strip().lower()
except KeyboardInterrupt:
print()
return []
if not ans or ans == 'q':
return []
elif ans in 'py':
return paths
elif ans == 'e':
break
elif ans == 'r':
restart = True
break
else:
print(f'Invalid answer "{ans}".')
def main() -> int:
'Main code'
global args
# Process command line options
opt = argparse.ArgumentParser(description=__doc__,
epilog='Note you can set default starting options in '
f'{CNFFILE}. The negation options (i.e. the --no-* options '
'and their shortforms) allow you to temporarily override your '
'defaults.')
opt.add_argument('-i', '--interactive', action='store_true',
help='prompt with summary of changes and allow re-edit before '
'proceeding')
opt.add_argument('-I', '--no-interactive', dest='interactive',
action='store_false',
help='negate the -i/--interactive option')
opt.add_argument('-a', '--all', action='store_true',
help='include all (including hidden) files')
opt.add_argument('-A', '--no-all', dest='all', action='store_false',
help='negate the -a/--all option')
opt.add_argument('-r', '--recurse', action='store_true',
help='recursively remove any files and directories in '
'removed directories')
opt.add_argument('-R', '--no-recurse', dest='recurse', action='store_false',
help='negate the -r/--recurse option')
opt.add_argument('-q', '--quiet', action='store_true',
help='do not print successful rename/remove/copy actions')
opt.add_argument('-Q', '--no-quiet', dest='quiet', action='store_false',
help='negate the -q/--quiet option')
opt.add_argument('-G', '--no-git', dest='git',
action='store_const', const=0,
help='do not use git if invoked within a git repository')
opt.add_argument('-g', '--git', dest='git',
action='store_const', const=1,
help='negate the --no-git option and DO use automatic git')
opt.add_argument('-t', '--trash', action='store_true',
help='use trash program to do deletions')
opt.add_argument('-T', '--no-trash', dest='trash', action='store_false',
help='negate the -t/--trash option')
opt.add_argument('--trash-program', default='trash-put',
help='trash program to use, default="%(default)s"')
opt.add_argument('-c', '--no-color', action='store_true',
help='do not color rename/remove/copy messages')
opt.add_argument('-C', '--no-invert-color', action='store_true',
help='do not invert the color to highlight error messages')
opt.add_argument('-d', '--dirnames', action='store_true',
help='edit given directory names directly, not their contents')
grp = opt.add_mutually_exclusive_group()
grp.add_argument('-F', '--files', action='store_true',
help='only show/edit files')
grp.add_argument('-D', '--dirs', action='store_true',
help='only show/edit directories')
opt.add_argument('-L', '--nolinks', action='store_true',
help='ignore all symlinks')
opt.add_argument('-N', '--sort-name', dest='sort',
action='store_const', const=1,
help='sort paths in file by name, alphabetically')
opt.add_argument('-M', '--sort-time', dest='sort',
action='store_const', const=2,
help='sort paths in file by time, oldest first')
opt.add_argument('-S', '--sort-size', dest='sort',
action='store_const', const=3,
help='sort paths in file by size, smallest first')
opt.add_argument('-E', '--sort-reverse', action='store_true',
help='sort paths (by name/time/size) in reverse')
opt.add_argument('-X', '--group-dirs-first', dest='group_dirs',
action='store_const', const=1,
help='group directories first (including when sorted)')
opt.add_argument('-Y', '--group-dirs-last', dest='group_dirs',
action='store_const', const=0,
help='group directories last (including when sorted)')
opt.add_argument('-Z', '--no-group-dirs', dest='group_dirs',
action='store_const', const=-1,
help='negate the options to group directories')
opt.add_argument('--suffix', default=SUFFIX,
help='specify suffix for temp editor file, default="%(default)s"')
opt.add_argument('-V', '--version', action='store_true',
help=f'show {PROG} version')
opt.add_argument('args', nargs='*',
help='file|dir, or "-" for stdin')
# Merge in default args from user config file. Then parse the
# command line.
if CNFFILE.exists():
with CNFFILE.open() as fp:
lines = [re.sub(r'#.*$', '', line).strip() for line in fp]
cnflines = ' '.join(lines).strip()
else:
cnflines = ''
args = opt.parse_args(shlex.split(cnflines) + sys.argv[1:])
if args.version:
from importlib.metadata import version
try:
ver = version(PROG)
except Exception:
ver = 'unknown'
print(ver)
return 0
if args.trash:
if not args.trash_program:
opt.error('must specify trash program with --trash-program option')
args.trash_program = args.trash_program.split()
# Check if we are in a git repo
if args.git != 0:
out, giterr = run(('git', 'ls-files'))
if giterr and args.git:
print(f'Git invocation error: {giterr}', file=sys.stderr)
if out:
gitfiles.update(out.splitlines())
if args.git and not gitfiles:
opt.error('must be within a git repo to use -g/--git option')
# Set input list to a combination of arguments and stdin
filelist = args.args
if sys.stdin.isatty():
if not filelist:
filelist.append('.')
elif '-' not in filelist:
filelist.insert(0, '-')
# Iterate over all (unique) inputs to get a list of files/dirs
for name in OrderedDict.fromkeys(filelist):
if name == '-':
for line in sys.stdin:
name = line.rstrip('\n\r')
if name != '.':
Fpath.add(line.rstrip('\n\r'), False)
else:
Fpath.add(name, not args.dirnames)
# Sanity check that we have something to edit
if not Fpath.paths:
desc = 'files' if args.files else \
'directories' if args.dirs else 'files or directories'
print(f'No {desc}.')
return 0
if args.sort == 1:
Fpath.paths.sort(key=Fpath.sort_name, reverse=args.sort_reverse)
elif args.sort == 2:
Fpath.paths.sort(key=Fpath.sort_time, reverse=args.sort_reverse)
elif args.sort == 3:
Fpath.paths.sort(key=Fpath.sort_size, reverse=args.sort_reverse)
if args.group_dirs is not None and args.group_dirs >= 0:
ldirs: list[Fpath] = []
lfiles: list[Fpath] = []
for path in Fpath.paths:
(ldirs if path.is_dir else lfiles).append(path)
Fpath.paths = ldirs + lfiles if args.group_dirs else lfiles + ldirs
paths = Fpath.get_path_changes()
if not paths:
return 0
err: str | None
# Pass 1: Rename all moved files & dirs to temps, delete all removed
# files.
for p in paths:
if p.newpath:
if p.newpath != p.path:
if err := p.rename_temp():
log('rename',
f'"{p.diagrepr}" to "{p.newpath}{p.appdash}"', err)
elif not p.is_dir:
err = remove(p.path, p.is_git, args.trash)
log('remove', f'"{p.diagrepr}"', err)
# Pass 2: Delete all removed dirs, if empty or recursive delete.
for p in paths:
if p.is_dir and not p.newpath:
if remove(p.path, p.is_git, args.trash, args.recurse) is None:
# Have removed, so flag as finished for final dirs pass below
p.is_dir = False
log('remove', f'"{p.diagrepr}"{p.note}')
# Pass 3. Rename all temp files and dirs to final target, and make
# copies.
for p in paths:
if (err := p.restore_temp()) is not None:
log('rename', f'"{p.diagrepr}" to "{p.newpath}{p.appdash}"', err)
for c in p.copies:
err = p.copy(c)
log('copy', f'"{p.diagrepr}" to "{c}{p.appdash}"{p.note}', err)
# Remove all the temporary dirs we created
Fpath.remove_temps()
# Pass 4. Delete all remaining dirs
for p in paths:
if p.is_dir and not p.newpath:
err = remove(p.path, p.is_git, args.trash, args.recurse)
log('remove', f'"{p.diagrepr}"{p.note}', err)
# Return status code 0 = all good, 1 = some bad, 2 = all bad.
return (1 if counts[0] > 0 else 2) if counts[1] > 0 else 0
if __name__ == '__main__':
sys.exit(main())