forked from sslinux/magepython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.py
More file actions
47 lines (34 loc) · 1.02 KB
/
find.py
File metadata and controls
47 lines (34 loc) · 1.02 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
import argparse
import pathlib
import datetime
import grp
import stat
import fnmatch
parser = argparse.ArgumentParser(prog='find')
parser.add_argument('path')
parser.add_argument('-name',dest='name',type=str,default='*')
parser.add_argument('-executable',dest='executable',action='store_true')
args = parser.parse_args()
def _walk(path: pathlib.Path,):
for item in path.iterdir():
if item.is_dir():
yield from _walk(item)
yield item
def walk(path):
yield from _walk(pathlib.Path(path))
def is_name_match(item:pathlib.Path,pattern:str) ->bool:
return fnmatch.fnmatch(str(item),pattern)
def is_executable(item:pathlib.Path) -> bool:
mode = item.lstat().st_mode
return stat.S_IEXEC & mode > 0
def filter(item:pathlib.Path) -> bool:
ret = is_name_match(item,args.name)
if args.executable:
ret = ret and is_executable(item)
return ret
def main():
for item in walk(args.path):
if filter(item):
print(item)
if __name__ == "__main__":
main()