def test_word_match_option(self): # -w/--word-regexp # Not a word-match options = grin.Options(word_regexp=True, regex="tes", re_flags=[], before_context=0, after_context=0) regex_on_word_boundaries = grin.GrepText(grin.utils.get_regex(options)) self.assertEqual( regex_on_word_boundaries.do_grep(BytesIO(word_boundaries)), [], ) # Word-match options = grin.Options(word_regexp=True, regex="test", re_flags=[], before_context=0, after_context=0) regex_on_word_boundaries = grin.GrepText(grin.utils.get_regex(options)) self.assertEqual( regex_on_word_boundaries.do_grep(BytesIO(word_boundaries)), [(1, 0, "This is a test.\n", [(10, 14)])], )
def test_ascii(self): # -a/--ascii # No match when in ascii mode options = grin.Options(regex=r"\d", re_flags=[re.A], before_context=0, after_context=0) regex_unicode = grin.GrepText(grin.utils.get_regex(options)) self.assertEqual( regex_unicode.do_grep(BytesIO(unicode_digits)), [], ) # [(1, 0, 'an Arabic-Indic digit ٢ on the\n', [(22, 23)])] # Unicode (default) options = grin.Options(regex=r"\d", re_flags=[], before_context=0, after_context=0) regex_unicode = grin.GrepText(grin.utils.get_regex(options)) self.assertEqual( regex_unicode.do_grep(BytesIO(unicode_digits)), [(1, 0, "an Arabic-Indic digit ٢ on the\n", [(22, 23)])], )
def search_for_expression(output, filepaths, validfiles, expression, grepbefore, grepafter): """Carry out search for expression (using grep context) on validfiles returning matching files as output""" options = grin.Options() options['before_context'] = int(grepbefore) options['after_context'] = int(grepafter) options['use_color'] = False options['show_filename'] = False options['show_match'] = True options['show_emacs'] = False options['show_line_numbers'] = True anchorcount = 1 searchregexp = re.compile(expression) grindef = grin.GrepText(searchregexp, options) for file in validfiles: filepath = validfiles.get(file)[0] report = grindef.grep_a_file(filepath) if report: output += '<a name="filename' + str( anchorcount) + '"></a><h2>' + filepath + '</h2>' filepaths.append(filepath) reporttext = report.split("\n") for text in reporttext: if text: output += "line " + text + "<br>" anchorcount += 1 return output
def test_symmetric_2_line_context(): gt_context_2 = grin.GrepText(re.compile(b"foo"), options=grin.Options(before_context=2, after_context=2)) assert gt_context_2.do_grep(BytesIO(all_foo)) == [ (0, 0, b"foo\n", [(0, 3)]), (1, 0, b"foo\n", [(0, 3)]), (2, 0, b"foo\n", [(0, 3)]), (3, 0, b"foo\n", [(0, 3)]), (4, 0, b"foo\n", [(0, 3)]), ] assert gt_context_2.do_grep(BytesIO(first_foo)) == [ (0, 0, b"foo\n", [(0, 3)]), (1, 1, b"bar\n", None), (2, 1, b"bar\n", None), ] assert gt_context_2.do_grep(BytesIO(last_foo)) == [ (2, -1, b"bar\n", None), (3, -1, b"bar\n", None), (4, 0, b"foo\n", [(0, 3)]), ] assert gt_context_2.do_grep(BytesIO(second_foo)) == [ (0, -1, b"bar\n", None), (1, 0, b"foo\n", [(0, 3)]), (2, 1, b"bar\n", None), (3, 1, b"bar\n", None), ] assert gt_context_2.do_grep(BytesIO(second_last_foo)) == [ (1, -1, b"bar\n", None), (2, -1, b"bar\n", None), (3, 0, b"foo\n", [(0, 3)]), (4, 1, b"bar\n", None), ] assert gt_context_2.do_grep(BytesIO(middle_foo)) == [ (0, -1, b"bar\n", None), (1, -1, b"bar\n", None), (2, 0, b"foo\n", [(0, 3)]), (3, 1, b"bar\n", None), (4, 1, b"bar\n", None), ] assert gt_context_2.do_grep(BytesIO(small_gap)) == [ (0, -1, b"bar\n", None), (1, -1, b"bar\n", None), (2, 0, b"foo\n", [(0, 3)]), (3, 1, b"bar\n", None), (4, 0, b"foo\n", [(0, 3)]), (5, 1, b"bar\n", None), (6, 1, b"bar\n", None), ] assert gt_context_2.do_grep(BytesIO(no_eol)) == [(0, 0, b"foo", [(0, 3)])] assert gt_context_2.do_grep(BytesIO(middle_of_line)) == [ (0, -1, b"bar\n", None), (1, -1, b"bar\n", None), (2, 0, b"barfoobar\n", [(3, 6)]), (3, 1, b"bar\n", None), (4, 1, b"bar\n", None), ]
def test_fixed_string_option(self): # -F/--fixed-string works with unescaped regex metachars options = grin.Options(fixed_string=True, regex="foo(", re_flags=[], before_context=0, after_context=0) regex_with_metachars = grin.GrepText(grin.utils.get_regex(options)) self.assertEqual( regex_with_metachars.do_grep(BytesIO(regex_metachar_foo)), [(2, 0, "def foo(...):\n", [(4, 8)])], )
def test_one_line_after_context_no_lines_before(self): # 1 line of after-context, no lines before. gt_after_context_1 = grin.GrepText(re.compile("foo"), options=grin.Options( before_context=0, after_context=1)) self.assertEqual( gt_after_context_1.do_grep(BytesIO(all_foo)), [ (0, 0, "foo\n", [(0, 3)]), (1, 0, "foo\n", [(0, 3)]), (2, 0, "foo\n", [(0, 3)]), (3, 0, "foo\n", [(0, 3)]), (4, 0, "foo\n", [(0, 3)]), ], ) self.assertEqual( gt_after_context_1.do_grep(BytesIO(first_foo)), [(0, 0, "foo\n", [(0, 3)]), (1, 1, "bar\n", None)], ) self.assertEqual(gt_after_context_1.do_grep(BytesIO(last_foo)), [(4, 0, "foo\n", [(0, 3)])]) self.assertEqual( gt_after_context_1.do_grep(BytesIO(second_foo)), [(1, 0, "foo\n", [(0, 3)]), (2, 1, "bar\n", None)], ) self.assertEqual( gt_after_context_1.do_grep(BytesIO(second_last_foo)), [(3, 0, "foo\n", [(0, 3)]), (4, 1, "bar\n", None)], ) self.assertEqual( gt_after_context_1.do_grep(BytesIO(middle_foo)), [(2, 0, "foo\n", [(0, 3)]), (3, 1, "bar\n", None)], ) self.assertEqual( gt_after_context_1.do_grep(BytesIO(small_gap)), [ (2, 0, "foo\n", [(0, 3)]), (3, 1, "bar\n", None), (4, 0, "foo\n", [(0, 3)]), (5, 1, "bar\n", None), ], ) self.assertEqual(gt_after_context_1.do_grep(BytesIO(no_eol)), [(0, 0, "foo", [(0, 3)])]) self.assertEqual( gt_after_context_1.do_grep(BytesIO(middle_of_line)), [(2, 0, "barfoobar\n", [(3, 6)]), (3, 1, "bar\n", None)], )
def test_1_line_of_before_context_no_lines_before(): gt_after_context_1 = grin.GrepText(re.compile(b"foo"), options=grin.Options(before_context=0, after_context=1)) assert gt_after_context_1.do_grep(BytesIO(all_foo)) == [ (0, 0, b"foo\n", [(0, 3)]), (1, 0, b"foo\n", [(0, 3)]), (2, 0, b"foo\n", [(0, 3)]), (3, 0, b"foo\n", [(0, 3)]), (4, 0, b"foo\n", [(0, 3)]), ] assert gt_after_context_1.do_grep(BytesIO(first_foo)) == [ (0, 0, b"foo\n", [(0, 3)]), (1, 1, b"bar\n", None), ] assert gt_after_context_1.do_grep(BytesIO(last_foo)) == [(4, 0, b"foo\n", [(0, 3)])] assert gt_after_context_1.do_grep(BytesIO(second_foo)) == [ (1, 0, b"foo\n", [(0, 3)]), (2, 1, b"bar\n", None), ] assert gt_after_context_1.do_grep(BytesIO(second_last_foo)) == [ (3, 0, b"foo\n", [(0, 3)]), (4, 1, b"bar\n", None), ] assert gt_after_context_1.do_grep(BytesIO(middle_foo)) == [ (2, 0, b"foo\n", [(0, 3)]), (3, 1, b"bar\n", None), ] assert gt_after_context_1.do_grep(BytesIO(small_gap)) == [ (2, 0, b"foo\n", [(0, 3)]), (3, 1, b"bar\n", None), (4, 0, b"foo\n", [(0, 3)]), (5, 1, b"bar\n", None), ] assert gt_after_context_1.do_grep(BytesIO(no_eol)) == [(0, 0, b"foo", [(0, 3)])] assert gt_after_context_1.do_grep(BytesIO(middle_of_line)) == [ (2, 0, b"barfoobar\n", [(3, 6)]), (3, 1, b"bar\n", None), ]
def test_symmetric_two_line_context(self): # Symmetric 2-line context. gt_context_2 = grin.GrepText(re.compile("foo"), options=grin.Options(before_context=2, after_context=2)) self.assertEqual( gt_context_2.do_grep(BytesIO(all_foo)), [ (0, 0, "foo\n", [(0, 3)]), (1, 0, "foo\n", [(0, 3)]), (2, 0, "foo\n", [(0, 3)]), (3, 0, "foo\n", [(0, 3)]), (4, 0, "foo\n", [(0, 3)]), ], ) self.assertEqual( gt_context_2.do_grep(BytesIO(first_foo)), [(0, 0, "foo\n", [(0, 3)]), (1, 1, "bar\n", None), (2, 1, "bar\n", None)], ) self.assertEqual( gt_context_2.do_grep(BytesIO(last_foo)), [(2, -1, "bar\n", None), (3, -1, "bar\n", None), (4, 0, "foo\n", [(0, 3)])], ) self.assertEqual( gt_context_2.do_grep(BytesIO(second_foo)), [(0, -1, "bar\n", None), (1, 0, "foo\n", [(0, 3)]), (2, 1, "bar\n", None), (3, 1, "bar\n", None)], ) self.assertEqual( gt_context_2.do_grep(BytesIO(second_last_foo)), [(1, -1, "bar\n", None), (2, -1, "bar\n", None), (3, 0, "foo\n", [(0, 3)]), (4, 1, "bar\n", None)], ) self.assertEqual( gt_context_2.do_grep(BytesIO(middle_foo)), [ (0, -1, "bar\n", None), (1, -1, "bar\n", None), (2, 0, "foo\n", [(0, 3)]), (3, 1, "bar\n", None), (4, 1, "bar\n", None), ], ) self.assertEqual( gt_context_2.do_grep(BytesIO(small_gap)), [ (0, -1, "bar\n", None), (1, -1, "bar\n", None), (2, 0, "foo\n", [(0, 3)]), (3, 1, "bar\n", None), (4, 0, "foo\n", [(0, 3)]), (5, 1, "bar\n", None), (6, 1, "bar\n", None), ], ) self.assertEqual(gt_context_2.do_grep(BytesIO(no_eol)), [(0, 0, "foo", [(0, 3)])]) self.assertEqual( gt_context_2.do_grep(BytesIO(middle_of_line)), [ (0, -1, "bar\n", None), (1, -1, "bar\n", None), (2, 0, "barfoobar\n", [(3, 6)]), (3, 1, "bar\n", None), (4, 1, "bar\n", None), ], )
def grep(): if request is None or request.form is None: return render_template('list.html', error='no search expression specified') # Validate the form inputs if request.form['expression'] is None or len( request.form['expression']) == 0: return render_template('list.html', error='no search expression specified') expression = request.form['expression'] expression = expression.strip() grepbefore = request.form['grepbefore'] grepafter = request.form['grepafter'] validfiles = session.get('validfiles') print "Grep (before) " + str(grepbefore) print "Grep (after) " + str(grepafter) print "Expression (regex) " + expression print "Filelist " + str(validfiles) options = grin.Options() options['before_context'] = int(grepbefore) options['after_context'] = int(grepbefore) options['use_color'] = False options['show_filename'] = False options['show_match'] = True options['show_emacs'] = False options['show_line_numbers'] = True print "Grin Options: " + str(options) output = "" filepaths = [] # TODO: Fix regular epxressions searchregexp = re.compile(expression) grindef = grin.GrepText(searchregexp, options) anchorcount = 1 for file in validfiles: filepath = validfiles.get(file)[0] report = grindef.grep_a_file(filepath) if report: # Generate the anchor tag output += '<a name="filename' + str( anchorcount) + '"></a><h2>' + filepath + '</h2>' filepaths.append(filepath) reporttext = report.split("\n") for text in reporttext: if text: output += "line " + text + "<br>" anchorcount += 1 # Return error msg if no results found if not output: return render_template('list.html', error='No results found for search expression') # Decode the output encodedoutput = output.decode('utf-8') # Highlight the matches expression = expression.decode('utf-8') highlight = '<span class="highlightmatch">' + expression + '</span>' highlightedoutput = encodedoutput.replace(expression, highlight) return render_template('results.html', output=highlightedoutput, filepaths=filepaths, expression=expression)