Package pysys :: Package utils :: Module filegrep
[hide private]
[frames] | no frames]

Source Code for Module pysys.utils.filegrep

  1  #!/usr/bin/env python 
  2  # PySys System Test Framework, Copyright (C) 2006-2013  M.B.Grieve 
  3   
  4  # This library is free software; you can redistribute it and/or 
  5  # modify it under the terms of the GNU Lesser General Public 
  6  # License as published by the Free Software Foundation; either 
  7  # version 2.1 of the License, or (at your option) any later version. 
  8   
  9  # This library is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 12  # Lesser General Public License for more details. 
 13   
 14  # You should have received a copy of the GNU Lesser General Public 
 15  # License along with this library; if not, write to the Free Software 
 16  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 17   
 18  # Contact: moraygrieve@users.sourceforge.net 
 19   
 20  import os.path, sys, re, string, copy 
 21   
 22  from pysys import log 
 23  from pysys.constants import * 
 24  from pysys.exceptions import * 
 25  from pysys.utils.filediff import trimContents 
 26   
 27   
28 -def getmatches(file, regexpr):
29 """Look for matches on a regular expression in an input file, return a sequence of the matches. 30 31 @param file: The full path to the input file 32 @param regexpr: The regular expression used to search for matches 33 @return: A list of the match objects 34 @rtype: list 35 @raises FileNotFoundException: Raised if the input file does not exist 36 37 """ 38 matches = [] 39 rexp = re.compile(regexpr) 40 41 log.debug("Looking for expression \"%s\" in input file %s" %(regexpr, file)) 42 43 if not os.path.exists(file): 44 raise FileNotFoundException, "unable to find file %s" % (os.path.basename(file)) 45 else: 46 list = open(file, 'r').readlines() 47 for i in range(0, len(list)): 48 match = rexp.search(list[i]) 49 if match is not None: 50 log.debug(("Found match for line: %s" % list[i]).rstrip()) 51 matches.append(match) 52 return matches
53 54
55 -def filegrep(file, expr):
56 """Search for matches to a regular expression in an input file, returning true if a match occurs. 57 58 @param file: The full path to the input file 59 @param expr: The regular expression (uncompiled) to search for in the input file 60 @returns: success (True / False) 61 @rtype: integer 62 @raises FileNotFoundException: Raised if the input file does not exist 63 64 """ 65 if not os.path.exists(file): 66 raise FileNotFoundException, "unable to find file %s" % (os.path.basename(file)) 67 else: 68 contents = open(file, 'r').readlines() 69 logContents("Contents of %s;" % os.path.basename(file), contents) 70 regexpr = re.compile(expr) 71 for line in contents: 72 if regexpr.search(line) is not None: return True 73 return False
74 75
76 -def lastgrep(file, expr, ignore=[], include=[]):
77 """Search for matches to a regular expression in the last line of an input file, returning true if a match occurs. 78 79 @param file: The full path to the input file 80 @param expr: The regular expression (uncompiled) to search for in the last line of the input file 81 @returns: success (True / False) 82 @param ignore: A list of regular expressions which remove entries in the input file contents before making the grep 83 @param include: A list of regular expressions used to select lines from the input file contents to use in the grep 84 @rtype: integer 85 @raises FileNotFoundException: Raised if the input file does not exist 86 87 """ 88 if not os.path.exists(file): 89 raise FileNotFoundException, "unable to find file %s" % (os.path.basename(file)) 90 else: 91 contents = open(file, 'r').readlines() 92 contents = trimContents(contents, ignore, exclude=True) 93 contents = trimContents(contents, include, exclude=False) 94 95 logContents("Contents of %s after pre-processing;" % os.path.basename(file), contents) 96 if len(contents) > 0: 97 line = contents[len(contents)-1] 98 regexpr = re.compile(expr) 99 if regexpr.search(line) is not None: return True 100 return False
101 102
103 -def orderedgrep(file, exprList):
104 """Seach for ordered matches to a set of regular expressions in an input file, returning true if the matches occur in the correct order. 105 106 The ordered grep method will only return true if matches to the set of regular expression in the expression 107 list occur in the input file in the order they appear in the expression list. Matches to the regular expressions 108 do not have to be across sequential lines in the input file, only in the correct order. For example, for a file 109 with contents :: 110 111 A is for apple 112 B is for book 113 C is for cat 114 D is for dog 115 116 an expression list of ["^A.*$", "^C.*$", "^D.*$"] will return true, whilst an expression list of 117 ["^A.*$", "^C.$", "^B.$"] will return false. 118 119 @param file: The full path to the input file 120 @param exprList: A list of regular expressions (uncompiled) to search for in the input file 121 @returns: success (True / False) 122 @rtype: integer 123 @raises FileNotFoundException: Raised if the input file does not exist 124 125 """ 126 list = copy.deepcopy(exprList) 127 list.reverse(); 128 expr = list.pop(); 129 130 if not os.path.exists(file): 131 raise FileNotFoundException, "unable to find file %s" % (os.path.basename(file)) 132 else: 133 contents = open(file, 'r').readlines() 134 for i in range(len(contents)): 135 regexpr = re.compile(expr) 136 if regexpr.search(r"%s"%contents[i]) is not None: 137 try: 138 expr = list.pop(); 139 except: 140 return None 141 return expr
142 143
144 -def logContents(message, list):
145 """Log a list of strings, prepending the line number to each line in the log output. 146 147 @param list: The list of strings to log 148 """ 149 count = 0 150 log.debug(message) 151 for line in list: 152 count = count + 1 153 log.debug((" Line %-5d: %s" % (count, line)).rstrip())
154 155 156 157 # entry point for running the script as an executable 158 if __name__ == "__main__": 159 if len(sys.argv) < 3: 160 print "Usage: filegrep.py <file> <regexpr>" 161 sys.exit() 162 else: 163 try: 164 status = filegrep(sys.argv[1], sys.argv[2]) 165 except FileNotFoundException, value: 166 print "caught %s: %s" % (sys.exc_info()[0], value) 167 print "unable to perform grep... exiting" 168 else: 169 if status == True: 170 print "Matches found" 171 else: 172 print "No matches found" 173