This repository was archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathr2wiki.py
98 lines (81 loc) · 3.94 KB
/
r2wiki.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
"""
Directions on how to setup is in the wiki itself.
"""
import os
import re
import sys
import pydoc
from argparse import ArgumentParser
from subprocess import Popen, PIPE
from pygments import highlight
from pygments.lexers import MarkdownLexer
from pygments.formatters import TerminalFormatter
black_list = ['<p hidden>', '<!--', '<img src']
src_dir = os.path.dirname(os.path.realpath(__file__))
def check_if_up_to_date():
status = Popen('git -C %s remote show origin' % src_dir,
stdout=PIPE, shell=True).stdout.readlines()[-1]
if 'out of date' in status:
print '[-] r2wiki out of date. Run $wiki -u or \n' \
'Update with git -C %s pull origin master' % src_dir
elif 'fatal: unable to access' in status:
print '[-] Could not check for r2wiki update'
def arg_parse():
parse = ArgumentParser()
parse.add_argument('what_to_search_for', metavar='what_to_search_for',
help='What to search for. Supports regex . Triple escape escapes. \\\ ')
parse.add_argument('-a', action='store_true', dest='match_any',
help='Will match any of the words.')
parse.add_argument('-l', action='store_true', dest='show_link',
help='Show the corresponding r2wiki link')
parse.add_argument('-u', action='store_true', dest='update',
help='Update r2wiki with latest content')
a = parse.parse_args()
return a
args = arg_parse()
check_if_up_to_date()
if args.update:
Popen('git -C %s pull origin master' % src_dir, shell=True, stdout=PIPE).wait()
print '\n[+] r2wiki updated'
exit(0)
if args.match_any:
pattern = re.compile('|'.join(args.what_to_search_for.split()), flags=re.IGNORECASE)
else:
pattern = re.compile(''.join('(?=.*{})'.format(arg) for arg in args.what_to_search_for.split()),
flags=re.IGNORECASE)
try:
found = ''
for path, subdirs, files in os.walk(src_dir):
for name in files:
md_path = os.path.join(path, name)
if not '.git' in md_path and md_path.endswith('.md'):
with open(md_path, 'r') as f:
for lines in f.readlines():
if re.search(pattern, lines):
if not any(filter in lines for filter in black_list):
match = ((re.sub('\*\*', '', lines)).lstrip() + '\n') # .replace('`', "'")
if match.startswith('- ['):
split = match.split(']')
found += max(split, key=len).replace('[', '') + \
' [.](https://radare2.securisec.com%s' % split[-1].strip('(')
else:
if match.startswith('>'):
match = ''.join(list(match)[1:])
if match.startswith('#'):
match = ''.join(list(match)[1:])
if match.startswith(' _'):
match = re.sub('_', '', ''.join(list(match)[1:]))
match = re.sub('{\.is-warning}|'
'{\.is-info}', '', match)
link = '[.](https://radare2.securisec.com/%s)' % md_path.strip(src_dir).strip('.md')
if args.show_link:
found += '%s\n%s' % (link, match)
else:
found += match
pydoc.pipepager(highlight(found, MarkdownLexer(), TerminalFormatter()), cmd='less -r')
except IndexError:
print 'Usage: %s search_param' % sys.argv[0]
except NameError:
print 'pip install Pygments'
except ImportError:
print 'Pygments dependency not met. pip install Pygments'