@@ -83,29 +83,38 @@ def _search_through_file(self, path, term, incl_comments=False):
8383 compiled = re .compile (pattern )
8484
8585 try :
86- # try native grep
87- result = sarge .run (["grep" , "-q" , "-E" , pattern , path ])
88- return result .returncode == 0
89- except ValueError as exc :
90- if "Command not found" in str (exc ):
91- try :
92- # try python only approach
93- with io .open (path , mode = "r" , encoding = "utf8" , errors = "replace" ) as f :
94- for line in f :
95- if term in line and (incl_comments or compiled .match (line )):
96- return True
97- return False
98- except Exception :
99- self ._logger .exception (
100- "Something unexpectedly went wrong while trying to "
101- "search for {} in {} via native python" .format (term , path )
86+ try :
87+ # try native grep
88+ result = sarge .capture_stderr (["grep" , "-q" , "-E" , pattern , path ])
89+ if result .stderr .text :
90+ self ._logger .warning (
91+ "Error raised by native grep, falling back to python "
92+ "implementation: {}" .format (result .stderr .text .strip ())
10293 )
103- else :
104- self ._logger .exception (
105- "Something unexpectedly went wrong while trying to "
106- "search for {} in {} via grep" .format (term , path )
107- )
94+ return self ._search_through_file_python (
95+ path , term , compiled , incl_comments = incl_comments
96+ )
97+ return result .returncode == 0
98+ except ValueError as exc :
99+ if "Command not found" in str (exc ):
100+ return self ._search_through_file_python (
101+ path , term , compiled , incl_comments = incl_comments
102+ )
103+ else :
104+ raise
105+ except Exception :
106+ self ._logger .exception (
107+ "Something unexpectedly went wrong while trying to "
108+ "search for {} in {} via grep" .format (term , path )
109+ )
110+
111+ return False
108112
113+ def _search_through_file_python (self , path , term , compiled , incl_comments = False ):
114+ with io .open (path , mode = "r" , encoding = "utf8" , errors = "replace" ) as f :
115+ for line in f :
116+ if term in line and (incl_comments or compiled .match (line )):
117+ return True
109118 return False
110119
111120 def _notify (self , notification_type , storage , path ):
0 commit comments