-
Notifications
You must be signed in to change notification settings - Fork 0
/
redebug.py
95 lines (82 loc) · 2.91 KB
/
redebug.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
#!/usr/bin/env python
#
# redebug.py
#
# Jiyong Jang, 2012
#
import sys
import os
import re
import time
import common
import patchloader
import sourceloader
import reporter
try:
import argparse
import magic
except ImportError as err:
print(err)
sys.exit(-1)
def parse_args():
'''
Parse command line arguments
'''
parser = argparse.ArgumentParser()
# optional arguments
parser.add_argument('-n', '--ngram',\
action='store', dest='ngram_size', type=int, default=4, metavar='NUM',\
help='use n-gram of NUM lines (default: %(default)s)')
parser.add_argument('-c', '--context',\
action='store', dest='context_line', type=int, default=10, metavar='NUM',\
help='print NUM lines of context (default: %(default)s)')
parser.add_argument('-v', '--verbose',\
action='store_true', dest='verbose_mode', default=False,\
help='enable verbose mode (default: %(default)s)')
# positional arguments
parser.add_argument('patch_path', action='store', help='path to patch files (in unified diff format)')
parser.add_argument('source_path', action='store', help='path to source files')
try:
args = parser.parse_args()
common.ngram_size = args.ngram_size
common.context_line = args.context_line
common.verbose_mode = args.verbose_mode
return args.patch_path, args.source_path
except IOError as msg:
parser.error(msg)
if __name__ == '__main__':
# parse arguments
start_time = time.time()
patch_path, source_path = parse_args()
common.verbose_print('[-] ngram_size : %d' % common.ngram_size)
common.verbose_print('[-] context_line : %d' % common.context_line)
common.verbose_print('[-] verbose_mode : %s' % common.verbose_mode)
common.verbose_print('[-] patch_path : %s' % patch_path)
common.verbose_print('[-] source_path : %s' % source_path)
# initialize a magic cookie pointer
try:
common.magic_cookie = magic.open(magic.MAGIC_MIME)
common.magic_cookie.load()
except AttributeError:
common.magic_cookie = magic.Magic(mime=True, uncompress=True)
common.verbose_print('[-] initialized magic cookie\n')
# traverse patch files
patch = patchloader.PatchLoader()
npatch = patch.traverse(patch_path)
if npatch == 0:
print('[!] no patch to be queried')
sys.exit(1)
# traverse source files
source = sourceloader.SourceLoader()
nmatch = source.traverse(source_path, patch)
if nmatch == 0:
print('[!] no match to be checked')
sys.exit(1)
# generate a report
report = reporter.Reporter(patch, source)
exact_nmatch = report.output()
if exact_nmatch == 0:
print('[!] no exact match found')
sys.exit(1)
elapsed_time = time.time() - start_time
print('[+] %d matches given %d patches ... %.1fs' % (exact_nmatch, npatch, elapsed_time))