forked from jevinskie/jevutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-by-magic.py
executable file
·92 lines (81 loc) · 2.21 KB
/
find-by-magic.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
#!/usr/bin/env python3
import argparse
import re
import sys
from pathlib import Path
import magic
m = magic.Magic(raw=True)
mmime = magic.Magic(raw=True, mime=True)
def PathDir(string) -> Path:
path = Path(string)
if not path.is_dir():
msg = f"{path} is not a directory"
raise argparse.ArgumentTypeError(msg)
return path
def is_match(args, pth) -> bool:
if args.regexes is not None or args.ftypes is not None:
ftype = m.from_file(pth)
if args.mtypes is not None:
mtype = mmime.from_file(pth)
if args.regexes is not None:
for pat in args.regexes:
if re.search(pat, ftype) is not None:
return True
if args.ftypes is not None:
if ftype in args.ftypes:
return True
if args.mtypes is not None:
if mtype in args.mtypes:
return True
return False
def main(args) -> int:
for pdir in args.dirs:
for pth in pdir.glob("**/*"):
if not pth.is_file():
continue
if args.exts is not None and pth.suffix not in args.exts:
continue
if is_match(args, pth):
print(pth)
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Find files using libmagic")
parser.add_argument("dirs", metavar="DIR", type=PathDir, nargs="+", help="Directory to search")
parser.add_argument(
"-t",
"--type",
metavar="TYPE",
dest="ftypes",
type=str,
action="append",
help="File type to find",
)
parser.add_argument(
"-m",
"--mime",
metavar="MIME_TYPE",
dest="mtypes",
type=str,
action="append",
help="MIME type to find",
)
parser.add_argument(
"-r",
"--regex",
metavar="REGEX",
dest="regexes",
type=lambda x: re.compile(x, flags=re.I),
action="append",
help="File type to find",
)
parser.add_argument(
"-e",
"--ext",
metavar="EXTENSION",
dest="exts",
type=str,
action="append",
help="Required extension",
)
args = parser.parse_args()
sys.exit(main(args))