-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathels.py
More file actions
executable file
·730 lines (603 loc) · 17.3 KB
/
els.py
File metadata and controls
executable file
·730 lines (603 loc) · 17.3 KB
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#!/usr/bin/env python3
# pyright: strict
from typing import (
TypedDict,
Literal,
Dict,
List,
Callable,
Any,
cast,
Optional as Opt,
Iterable as Iter
)
import re
import sys
import os
import pwd
import grp
import stat
import datetime
import argparse
import subprocess
from functools import reduce
class StatRes:
st_mode: int
st_mtime: float
st_uid: int
st_gid: int
st_size: int
FileType = Literal["file", "directory"]
ContentType = Literal[
"directory",
"not_readable",
"empty",
"binary_executable",
"binary_other",
"text",
"other",
"unknown"
]
class FileRowInfo(TypedDict):
fname: str
ftype: FileType
stat_res: StatRes
contenttype: ContentType
timeepoch: str
class FileRow(TypedDict):
info: FileRowInfo
render: Dict[str, str]
FileRows = List[FileRow]
Align = Literal["left", "right"]
ColsType = Literal[
"acls", "owner", "filetype", "size",
"timeiso", "srcname", "targetname", "preview"
]
ColsTypes = List[ColsType]
class FileDef(TypedDict):
align: Align
name: ColsType
onlyfull: bool
func: Callable[[Any], str]
ColPaddings = Dict[str, int]
FileDefs = Dict[ColsType, FileDef]
COLORS = {
'red': '\033[31m',
'magenta': '\033[35m',
'light_magenta': '\033[95m',
'green': '\033[32m',
'light_green': '\033[92m',
'light_cyan': '\033[96m',
'light_yellow': '\033[93m',
'light_gray': '\033[37m',
'dark_gray': '\033[30m',
'light_red': '\033[91m',
'light_blue': '\033[94m',
'blue': '\033[34m',
'end': '\033[0m'
}
COLOR_VALS = {
'srcname_directory': 'light_red',
'srcname_file': 'light_green',
'targetname': 'light_cyan',
'time': 'blue',
'size_filecount': 'magenta',
'size_bytes': 'green',
'acls': 'dark_gray',
'owner': 'dark_gray',
'filetype': 'dark_gray',
'preview': 'dark_gray',
'default': 'light_magenta'
}
PREVIEW_READ_LEN = 256
PREVIEW_TRUNC_LEN = 48
DEFAULT_START_PATH = './'
def sortfile(row: FileRow):
fname = row['info']['fname']
lowfname = fname.strip().lower()
key = 0 if (row['info']['ftype'] == 'directory') else 1
out = (key, lowfname)
return out
def getcolordefs(row: FileRow, field: str):
if field == 'targetname':
return 'targetname'
if field == 'srcname':
if row['info']['ftype'] == 'directory':
return 'srcname_directory'
return 'srcname_file'
if field == 'timeiso':
return 'time'
if field == 'size':
if row['info']['ftype'] == 'directory':
return 'size_filecount'
return 'size_bytes'
if field == 'acls':
return 'acls'
if field == 'owner':
return 'owner'
if field == 'size':
return 'size'
if field == 'filetype':
return 'filetype'
if field == 'preview':
return 'preview'
return 'default'
def addpadding(field: str, val: str, colpaddings: ColPaddings, align: Align):
if len(val) == 0:
return ' '
alignchar = '>' if (align == 'right') else '<'
padlen = colpaddings[field]
padstr = ''.join(['{:', alignchar, str(padlen), 's}'])
ret = padstr.format(val)
return ret
def makepretty(row: FileRow, field: ColsType, colpaddings: ColPaddings, fdefs: FileDefs):
align = fdefs[field]['align']
clr = getcolordefs(row, field)
clrval = COLOR_VALS[clr]
textval = row['render'][field]
paddedval = addpadding(field, textval, colpaddings, align)
colorval = addcolor(paddedval, clrval)
return colorval
def addcolor(text: str, color: str):
return text.join([COLORS[color], COLORS['end']])
def getcolslisting(full: bool=False):
out: ColsTypes = []
if full:
out.append('acls')
out.append('owner')
out.append('filetype')
out.append('size')
out.append('timeiso')
out.append('srcname')
out.append('targetname')
if full:
out.append('preview')
return out
def structurecols(row: FileRow, colpaddings: ColPaddings, fdefs: FileDefs, full: bool=False):
colslisting = getcolslisting(full=full)
return map(lambda name: makepretty(row, name, colpaddings, fdefs), colslisting)
def rendercols(row: FileRow, colpaddings: ColPaddings, fdefs: FileDefs, full: bool=False):
margin = ' '
structcols = structurecols(row, colpaddings, fdefs, full=full)
return ''.join([margin, margin.join(structcols)])
def getcolpaddings(rows: FileRows):
longest: Dict[str, int] = {}
for row in rows:
for colname, colval in row['render'].items():
if colname not in longest:
longest[colname] = 0
collen = len(colval)
if collen > longest[colname]:
longest[colname] = collen
return longest
def getrowdefs():
ret: FileDefs = {
'acls': {
'name': 'acls',
'func': col_acls,
'onlyfull': True,
'align': 'left'
},
'owner': {
'name': 'owner',
'func': col_owner,
'onlyfull': True,
'align': 'left'
},
'filetype': {
'name': 'filetype',
'func': col_filetype,
'onlyfull': True,
'align': 'left'
},
'size': {
'name': 'size',
'func': col_size,
'onlyfull': False,
'align': 'right'
},
'timeiso': {
'name': 'timeiso',
'func': col_timeiso,
'onlyfull': False,
'align': 'left'
},
'srcname': {
'name': 'srcname',
'func': col_srcname,
'onlyfull': False,
'align': 'left'
},
'targetname': {
'name': 'targetname',
'func': col_targetname,
'onlyfull': False,
'align': 'left'
},
'preview': {
'name': 'preview',
'func': col_preview,
'onlyfull': True,
'align': 'left'
}
}
return ret
def renderrows(rows: FileRows, full: bool=False):
colpaddings = getcolpaddings(rows)
fdefs = getrowdefs()
rendered = map(lambda r: rendercols(r, colpaddings, fdefs, full=full), rows)
out = '\n'.join(rendered)
return out
def get_acls_me(fname: str, stat_res: StatRes):
t_no = 0
me_can_read = os.access(fname, os.R_OK)
me_can_write = os.access(fname, os.W_OK)
me_can_exec = os.access(fname, os.X_OK)
me_pdefs = [
(me_can_read, 4),
(me_can_write, 2),
(me_can_exec, 1)
]
me_vals = map(lambda x: x[1] if x[0] else t_no, me_pdefs)
me_acls_num = reduce(lambda x, y: x | y, me_vals, 0)
me_acls_mode = str(me_acls_num)
return me_acls_mode
def get_acls_all(fname: str, stat_res: StatRes):
return str(oct(stat.S_IMODE(stat_res.st_mode)))[-3:]
def col_acls(rowinfo: FileRowInfo):
fname = rowinfo['fname']
stat_res = rowinfo['stat_res']
all_acls_mode = get_acls_all(fname, stat_res)
me_acls_mode = get_acls_me(fname, stat_res)
return ' '.join([all_acls_mode, me_acls_mode])
def col_owner(rowinfo: FileRowInfo):
stat_res = rowinfo['stat_res']
owner = pwd.getpwuid(stat_res.st_uid).pw_name
group = grp.getgrgid(stat_res.st_gid).gr_name
ret = ':'.join([owner, group])
return ret
def getfilesize(rowinfo: FileRowInfo):
stat_res = rowinfo['stat_res']
ret = '{:,}'.format(stat_res.st_size)
return ret
def col_size(rowinfo: FileRowInfo):
if rowinfo['ftype'] == 'directory':
return getsubfilecount(rowinfo)
return getfilesize(rowinfo)
def col_timeiso(rowinfo: FileRowInfo):
stat_res = rowinfo['stat_res']
dt = datetime.datetime.fromtimestamp(stat_res.st_mtime)
return dt.strftime('%Y-%m-%d %H:%M:%S')
def col_srcname(rowinfo: FileRowInfo):
fname = rowinfo['fname']
isdir = rowinfo['ftype'] == 'directory'
return ''.join([fname, '/']) if isdir else fname
def col_targetname(rowinfo: FileRowInfo):
fname = rowinfo['fname']
islink = os.path.islink(fname)
if islink:
real = os.path.relpath(os.path.realpath(fname))
isdir = rowinfo['ftype'] == 'directory'
return ''.join([real, '/']) if isdir else real
return ' '
def getsubfilecount(rowinfo: FileRowInfo):
fname = rowinfo['fname']
islink = os.path.islink(fname)
real = os.path.relpath(os.path.realpath(fname)) if islink else fname
if not os.path.isdir(real):
return '-'
if not os.access(real, os.R_OK):
return '-'
return str(len(os.listdir(real)))
def col_filetype(rowinfo: FileRowInfo):
contenttype = rowinfo['contenttype']
if contenttype == 'directory':
return 'd'
if contenttype == 'binary_executable':
return 'e'
if contenttype == 'binary_other':
return 'b'
if contenttype == 'text':
return 't'
return 'u'
def col_preview(rowinfo: FileRowInfo):
fname = rowinfo['fname']
contenttype = rowinfo['contenttype']
if contenttype == 'directory':
return preview_directory(fname)
if contenttype == 'binary_other':
return preview_binary(fname)
if contenttype == 'text':
return preview_text(fname)
return ' '
def preview_directory(fname: str):
def _func(subfname: str):
checkfname = os.path.join(fname, subfname)
real = (
os.path.relpath(os.path.realpath(checkfname)) if
os.path.islink(checkfname) else
checkfname
)
path = (
''.join([checkfname, '/']) if
os.path.isdir(real) else
checkfname
)
stripped = path[(len(fname) + 1):]
return stripped
try:
all_files = os.listdir(fname)
except PermissionError:
return "-"
all_len = len(all_files)
sub_files = list(map(_func, all_files[:32]))
sub_len = len(sub_files)
txt = ' '.join(sub_files)
truncated = txt[:38]
lastindex = truncated.rfind(' ')
cleaned = (
truncated if
(lastindex < 1) else
truncated[:lastindex]
)
return ' '.join([cleaned, '...']) if sub_len < all_len else cleaned
def preview_binary(fname: str):
def _inrange(a: int):
return (
(a >= 0x0021 and a <= 0x007E) or
(a >= 0x0009 and a <= 0x000D)
)
data: bytes
with open(fname, 'rb') as fh:
data = fh.read(PREVIEW_READ_LEN)
if len(data) == 0:
return ' '
newdata = ''.join(map(chr, filter(_inrange, data)))
stripped = newdata.strip()
cleaned = re.sub(r"(?u)[\s]+", ' ', stripped)
return cleaned[:PREVIEW_TRUNC_LEN]
def preview_text(fname: str):
data: str
with open(fname, 'rt', errors='replace') as fh:
data = fh.read(PREVIEW_READ_LEN)
if len(data) == 0:
return ' '
##
## Match
##
## * First 32 characters of ascii
## * The DEL character
## * Anything that is a whitespace
##
pat = r'(?u)[\u0000-\u0020\u0127\s]+'
cleaned = re.sub(pat, ' ', data).strip()
return cleaned[:PREVIEW_TRUNC_LEN]
def getfileinfo(fname: str):
"""
See: http://stackoverflow.com/a/898759
"""
def _proc():
return (
subprocess.Popen(
[
'file',
'--mime',
'--dereference',
fname
],
stdout=subprocess.PIPE
)
)
rawdata: Opt[bytes]
with _proc() as subproc:
rawdata = None if subproc.stdout is None else subproc.stdout.read()
if rawdata is None:
return cast(ContentType, "unknown")
data = rawdata.decode('utf-8', errors='replace')
##
## First check if text
##
if (re.search('(?u)[^a-zA-Z]text[^a-zA-Z]', data) is not None):
return cast(ContentType, 'text')
##
## Executable binary file such as ELF
##
if (
re.search(
'(?u)[^a-zA-Z]x-(executable|sharedlib)[^a-zA-Z]',
data
)
is not None
):
return cast(ContentType, 'binary_executable')
##
## Some other binary file such as an image
##
return cast(ContentType, 'binary_other')
def shouldbuild(defrec: FileDef, full: bool=False):
if defrec['onlyfull'] and (not full):
return False
return True
def buildrow(fname: str, fdefs: FileDefs, full: bool=False):
rowinfo = getrowinfo(fname)
def _func(rec: FileDef):
return (
rec['name'],
(
rec['func'](rowinfo) if
shouldbuild(rec, full=full) else
' '
)
)
ret: FileRow = {
'info': rowinfo,
'render': dict(map(_func, fdefs.values()))
}
return ret
def info_ftype(fname: str, stat_res: StatRes):
real = (
os.path.relpath(os.path.realpath(fname)) if
os.path.islink(fname) else
fname
)
if os.path.isdir(real):
return 'directory'
return 'file'
def info_timeepoch(fname: str, stat_res: StatRes):
return str(stat_res.st_mtime)
def info_contenttype(fname: str, stat_res: StatRes):
if os.path.isdir(fname):
return 'directory'
if not os.access(fname, os.R_OK):
return 'not_readable'
if stat_res.st_size == 0:
return 'empty'
fileinfo = getfileinfo(fname)
##
## Binary Executable such as ELF
##
if fileinfo == 'binary_executable':
return 'binary_executable'
##
## Some other binary such as image
##
if fileinfo == 'binary_other':
return 'binary_other'
##
## If is text
##
if fileinfo == 'text':
return 'text'
##
## Something else
##
return 'other'
def getrowinfo(fname: str):
stat_res = cast(StatRes, os.lstat(fname))
ret: FileRowInfo = {
'fname': fname,
'stat_res': stat_res,
'ftype': info_ftype(fname, stat_res),
'contenttype': info_contenttype(fname, stat_res),
'timeepoch': info_timeepoch(fname, stat_res)
}
return ret
def processrows(files: Iter[str], full: bool = False):
fdefs = getrowdefs()
return map(lambda fname: buildrow(fname, fdefs, full=full), files)
def concatoutput(data: str):
return ''.join(['\n', data, '\n', '\n'])
def display(rows_str: str):
pagedisplay(concatoutput(rows_str))
return True
def get_dir_listing(start: Opt[str] = None, filtres: Opt[str] = None):
start1 = "./" if start is None else start
realstart = os.path.relpath(start1) if start1 != "./" else start1
if (not os.path.exists(start1)) or (not os.path.isdir(start1)):
return None
files = os.listdir(start)
fpaths = (
iter(files) if
(filtres is None) else
filter(lambda f: filtres in f, files)
)
paths = map(
lambda f: os.path.join(
(realstart[2:] if (realstart[:2] == './') else realstart),
f
),
fpaths
)
return paths
def getfiles(start: Opt[str] = None, full: bool = False, filtres: Opt[str] = None):
paths = get_dir_listing(start=start, filtres=filtres)
if paths is None:
return None
processed = processrows(paths, full=full)
return sorted(processed, key=sortfile, reverse=False)
def pagedisplay(output: str):
"""
See:
https://chase-seibert.github.io/blog
/2012/10/31/python-fork-exec-vim-raw-input.html
"""
proc = lambda: (
subprocess.Popen(
[
'less',
'--RAW-CONTROL-CHARS',
'--quit-at-eof',
'--quit-if-one-screen',
'--no-init'
],
stdin=subprocess.PIPE,
stdout=sys.stdout
)
)
encoded = output.encode('utf-8')
with proc() as pager:
try:
pager.communicate(encoded)
if pager.stdin is not None:
pager.stdin.close()
pager.wait()
except KeyboardInterrupt:
## TODO: Any way to stop ctrl-c from
## screwing up keyboard entry?
pager.terminate()
pager.wait()
def run(start: Opt[str]=None, full: bool=False, filtres: Opt[str]=None):
files = getfiles(start=start, full=full, filtres=filtres)
if files is None:
rendererror()
return False
rows = renderrows(files, full=full)
display(rows)
return True
def rendererror():
sys.stderr.write("Path could not be found, or path is not a directory.\n")
def getargs():
arger = argparse.ArgumentParser(description='Replacement for ls')
arger.add_argument(
'start',
metavar='STARTPATH',
type=str,
nargs='?',
help='Starting Path',
default=DEFAULT_START_PATH,
action='store'
)
arger.add_argument(
'-g',
'--filter',
metavar='FILTERSTR',
type=str,
help='Filter Results',
dest='filtres',
default=None,
action='store'
)
arger.add_argument(
'-f',
'--full',
help='Full Output',
dest='full',
default=False,
action='store_true'
)
args = arger.parse_args()
return args
def main():
args = getargs()
ret = (
run(
start=args.start,
full=args.full,
filtres=args.filtres
)
)
if ret is False:
return 1
return 0
if __name__ == '__main__':
sys.exit(main())