Exemplo n.º 1
0
    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')
Exemplo n.º 2
0
    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")
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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