def run_egret(regexStr, baseSubstring, testList): inputStrs = egret_ext.run(regexStr, baseSubstring, False, False) status = inputStrs[0] if status[0:5] == "ERROR": return ([], [], status, []) elif status == "SUCCESS": warnings = None else: warnings = status.rstrip().split("\n") inputStrs = inputStrs[1:] matches = [] nonMatches = [] regex = re.compile(regexStr) inputStrs = sorted(list(set(inputStrs) | set(testList))) for inputStr in inputStrs: search = regex.fullmatch(inputStr) if search: matches.append(inputStr) else: nonMatches.append(inputStr) return (matches, nonMatches, None, warnings)
def run_egret(regexStr, baseSubstring, testList): try: regex = re.compile(regexStr) except re.error as e: status = "ERROR (compiler error): Regular expression did not compile: " + str(e) return ([], [], status, []) inputStrs = egret_ext.run(regexStr, baseSubstring, False, True, False, False) idx = 0 line = inputStrs[idx] if line[0:5] == "ERROR": return ([], [], line, []) while line != "BEGIN": idx += 1; line = inputStrs[idx] if idx == 0: alerts = [] inputStrs = inputStrs[1:] else: alerts = inputStrs[:idx] inputStrs = inputStrs[idx+1:] warnings = "" for a in alerts: warnings += a matches = [] nonMatches = [] inputStrs = sorted(list(set(inputStrs) | set(testList))) for inputStr in inputStrs: search = regex.fullmatch(inputStr) if search: matches.append(inputStr) else: nonMatches.append(inputStr) return (matches, nonMatches, None, warnings)
except: descStr = "" inFile.close() elif opts.regex != None: regexStr = opts.regex else: regexStr = input("Enter a Regular Expression: ") # compile the regular expression try: hasError = False regex = re.compile(regexStr) # execute regex-test # start_time = time.process_time() alerts = egret_ext.run(regexStr, "evil", True, False, opts.debugMode, opts.statMode) # elapsed_time = time.process_time() - start_time except re.error as e: status = "ERROR (compiler error): Regular expression did not compile: " + str( e) + "\n" alerts = [status] #if opts.statMode: # fmt = "{0:30}| {1}" # print(fmt.format("Time", elapsed_time)) # write the output header header = "Regex: " + regexStr + "\n\n" if descStr != "": header += ("Description: " + descStr + "\n\n")
descStr = inFile.readline().rstrip() except: descStr = "" inFile.close() elif opts.regex != None: regexStr = opts.regex else: regexStr = input("Enter a Regular Expression: ") # compile the regular expression try: regex = re.compile(regexStr) # execute regex-test #start_time = time.process_time() inputStrs = egret_ext.run(regexStr, opts.baseSubstring, False, False, opts.debugMode, opts.statMode) status = inputStrs[0] hasError = (status[0:5] == "ERROR") except re.error as e: status = "ERROR (compiler error): Regular expression did not compile: " + str( e) hasError = True if hasError: alerts = [status] else: idx = 0 line = inputStrs[idx] while line != "BEGIN": idx += 1
def run_acre(regexStr): try: regex = re.compile(regexStr) except re.error as e: errorMsg = "ERROR (compiler error): Regular expression did not compile: " + str(e) return (None, errorMsg) alerts = egret_ext.run(regexStr, "evil", True, True, False, False) first_line = alerts[0] if first_line[0:5] == "ERROR": return (None, first_line) status = "" for s in alerts: lines = s.split("<br>") prevLine = "" for line in lines: suppressLine = False example = False anchorExample = False if re.match("\.\.\.String with", line) != None: example = True anchorExample = True if re.match("\.\.\.Example", line) != None: example = True if example: suppressLine = True first, second = line.split(':', 1) exampleStr = second[1:] success = re.fullmatch(regexStr, exampleStr) != None if anchorExample: if prevLine != "": if prevSuccess and success: # both anchor examples successful status += prevLine status += '<br>' status += line status += '<br>' prevLine = "" else: # prevLine == "" prevLine = line prevSuccess = success else: # example but not anchor example prevLine = "" if success: suppressLine = False else: # not example prevLine = "" if re.match("\.\.\.Suggested fix", line) != None: first, second = line.split(':', 1) fixStr = second[1:] try: r = re.compile(fixStr) except re.error as e: suppressLine = True if not suppressLine: status += line status += '<br>' # get rid of line breaks at end (eliminates extra space at the end) while (status[-4:] == "<br>"): status = status[0:-4] return (status, None)