forked from jevinskie/jevutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-by-mentions.py
executable file
·103 lines (90 loc) · 2.46 KB
/
find-by-mentions.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
#!/usr/bin/env python3
import argparse
import re
import sys
from pathlib import Path
import magic
m = magic.Magic(raw=True)
def PathDir(string):
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):
buf = None
with open(pth, "rb") as f:
buf = f.read()
ftype = None
if args.regexes is not None or args.ftypes is not None:
ftype = m.from_buffer(buf)
if args.regexes is not None:
for pat in args.regexes:
if pat.search(ftype) is not None:
return True
if args.ftypes is not None:
if ftype in args.ftypes:
return True
if args.mentions is not None:
matches = 0
for pat in args.mentions:
matches += sum(1 for _ in pat.finditer(buf))
return matches
return False
def main(args):
matches = {}
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
m = is_match(args, pth)
if m:
matches[pth] = m
ml = matches.items()
ml = sorted(ml, key=lambda e: e[1])
for pth, m in ml:
print(f"path: {pth} m: {m}")
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(
"-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(
"-m",
"--mention",
metavar="MENTION_REGEX",
dest="mentions",
type=lambda x: re.compile(x.encode("utf-8"), flags=re.I),
action="append",
help="mention regexes 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))