forked from sslinux/magepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.py
More file actions
66 lines (53 loc) · 1.84 KB
/
ls.py
File metadata and controls
66 lines (53 loc) · 1.84 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
#!/usr/bin/env python
import argparse
import pathlib
import stat
import pwd
import grp
import datetime
parser = argparse.ArgumentParser(prog='ls',add_help=True)
parser.add_argument('-l',dest="long_format",help='long format',action='store_true')
parser.add_argument('-h',dest='human',help='human readable',action='store_true')
parser.add_argument('-a',dest='all',help='all include . and ..',action='store_true')
parser.add_argument('path',nargs='*',help='which path you want to list.',default='.')
args = parser.parse_args()
def scan(path:str):
yield from (x for x in pathlib.Path(path).iterdir() if args.all or not x.name.startswith('.'))
def time_format(mtime:int) -> str:
dt = datetime.datetime.fromtimestamp(mtime)
return '{:>2} {:>2} {:>2} {:>2}'.format(dt.month,dt.day,dt.hour,dt.minute)
def size_setup(size:int) -> str:
if not args.human:
return str(size)
units = ['','K','M','G','T','P','E']
idx = 0
while size > 1024:
size /= 1024
idx += 1
return '{} {}'.format(round(size,1),units[idx])
def format(item:pathlib.Path) -> str:
if not args.long_format:
return item.name
st = item.stat()
attr = {
'mode': stat.filemode(st.st_mode),
'nlink': st.st_nlink,
'user': pwd.getpwuid(st.st_uid).pw_name,
'group': grp.getgrgid(st.st_gid).gr_name,
'size': size_setup(st.st_size),
'mtime':time_format(st.st_mtime),
'name':item.name
}
return '{mode} {nlink} {user} {group} {size} {mtime} {name}]'.format(**attr)
def main():
if isinstance(args.path,list):
for path in args.path:
print('{}'.format(path))
for item in scan (path):
print(format(item))
print()
else:
for item in scan(args.path):
print(format(item))
if __name__ == "__main__":
main()