def test_read_only_mode(self): with self.assertRaises(InvalidFileMode): BufferedReader('/tmp/foo', mode='a') with self.assertRaises(InvalidFileMode): BufferedReader('/tmp/foo', mode='ab') with self.assertRaises(InvalidFileMode): BufferedReader('/tmp/foo', mode='w') with self.assertRaises(InvalidFileMode): BufferedReader('/tmp/foo', mode='wb')
def test_read_only_mode(self): with self.assertRaises(InvalidFileMode): BufferedReader("/tmp/foo", mode="a") with self.assertRaises(InvalidFileMode): BufferedReader("/tmp/foo", mode="ab") with self.assertRaises(InvalidFileMode): BufferedReader("/tmp/foo", mode="w") with self.assertRaises(InvalidFileMode): BufferedReader("/tmp/foo", mode="wb")
def match(self, dirname, filename, fstat): if not stat.S_ISREG(fstat[stat.ST_MODE]): return None with BufferedReader(os.path.join(dirname, filename), mode='rb') as br: for chunk in br: if self.re.search(chunk): return os.path.join(dirname, filename) return None
def match(self, dirname, filename, fstat): if not stat.S_ISREG(fstat[stat.ST_MODE]): return None dfilename = os.path.join(dirname, filename) with BufferedReader(dfilename, mode='rb') as bread: for chunk in bread: if self.regex.search(chunk): return dfilename return None
def find_value(text): stripped_text = text.strip() try: with BufferedReader(file_name) as breader: for chunk in breader: if stripped_text in chunk: return True return False except (IOError, OSError): return False
def match(self, dirname, filename, fstat): if not stat.S_ISREG(fstat[stat.ST_MODE]): return None dfilename = os.path.join(dirname, filename) # This could technically be done with ``open`` but if the file is large # it can cause resource usage issues, so buffer the output read in # memory with BufferedReader(dfilename, mode='rb') as bread: try: for chunk in bread: if self.regex.search(chunk): return dfilename except UnicodeDecodeError: next return None
def contains_glob(path, glob): ''' Return True if the given glob matches a string in the named file CLI Example:: salt '*' /etc/foobar '*cheese*' ''' if not os.path.exists(path): return False try: with BufferedReader(path) as br: for chunk in br: if fnmatch.fnmatch(chunk, glob): return True return False except (IOError, OSError): return False
def contains(path, text): ''' Return True if the file at ``path`` contains ``text`` CLI Example:: salt '*' file.contains /etc/crontab 'mymaintenance.sh' .. versionadded:: 0.9.5 ''' if not os.path.exists(path): return False try: with BufferedReader(path) as br: for chunk in br: if text.strip() == chunk.strip(): return True return False except (IOError, OSError): return False
def contains_regex(path, regex, lchar=''): ''' Return True if the given regular expression matches anything in the text of a given file CLI Example:: salt '*' /etc/crontab '^maint' ''' if not os.path.exists(path): return False try: with BufferedReader(path) as br: for chunk in br: if lchar: chunk = chunk.lstrip(lchar) if re.search(regex, chunk, re.MULTILINE): return True return False except (IOError, OSError): return False