Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 4 | # |
| 5 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 6 | # "License"); you may not use this file except in compliance with the |
| 7 | # License. You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.eclipse.org/legal/epl-v10.html |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import os |
| 19 | import subprocess |
| 20 | import re |
| 21 | |
| 22 | VALIDATE_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 23 | CLI_PATH = os.path.realpath(os.path.join(VALIDATE_DIR, "../cli.py")) |
| 24 | INPUT_DIR = os.path.join(VALIDATE_DIR, "input_files") |
| 25 | OUTPUT_DIR = "cli_validate" |
| 26 | |
| 27 | def validator(input_path, output_path): |
| 28 | print "Validator started." |
| 29 | print "Input path: %s" % input_path |
| 30 | print "Output path: %s" % output_path |
| 31 | subprocess.call("python %s < %s > %s" % (CLI_PATH, input_path, output_path), shell=True) |
| 32 | |
| 33 | |
| 34 | def sanitize(path): |
| 35 | filters = [r"^[^<>#()]+(> .*)", r"^[^<>#()]+(# .*)", r"^[^<>#()]+(\([^()]+\)# .*)", |
| 36 | r"^Need error", r"^SDNShell", r"^default controller:", |
| 37 | r"^PERMUTE", r"^SUBMODE", r"^Exiting"] |
| 38 | patterns = [re.compile(f) for f in filters] |
| 39 | tmp_path = "%s.tmp" % path |
| 40 | f1 = open(path) |
| 41 | f2 = open(tmp_path, "w") |
| 42 | |
| 43 | def matchRegexList(compiledRegexList, line): |
| 44 | for cr in compiledRegexList: |
| 45 | if cr.search(line): |
| 46 | return True |
| 47 | return False |
| 48 | |
| 49 | for line in f1: |
| 50 | if matchRegexList(patterns, line): |
| 51 | continue |
| 52 | f2.write(line) |
| 53 | f1.close() |
| 54 | f2.close() |
| 55 | os.remove(path) |
| 56 | os.rename(tmp_path, path) |
| 57 | |
| 58 | |
| 59 | def main(): |
| 60 | subprocess.call(["rm", "-rf", OUTPUT_DIR]) |
| 61 | subprocess.call(["mkdir", "-p", OUTPUT_DIR]) |
| 62 | for f in os.listdir(INPUT_DIR): |
| 63 | in_path = os.path.join(INPUT_DIR, f) |
| 64 | out_path = os.path.join(OUTPUT_DIR, "%s_output" % f) |
| 65 | validator(in_path, out_path) |
| 66 | sanitize(out_path) |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |
| 70 | |