Jeff DiCorpo | 4306d9a | 2021-01-26 02:34:17 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-FileCopyrightText: 2020 Efabless Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | # SPDX-License-Identifier: Apache-2.0 |
| 17 | |
| 18 | import argparse |
| 19 | import re |
| 20 | import os |
| 21 | |
| 22 | parser = argparse.ArgumentParser( |
| 23 | description="Summarizes a Calibre summary report into a csv file.") |
| 24 | |
| 25 | parser.add_argument("--targetPath", "-t", required=True, |
| 26 | help="Summaries Path. All .summary files inside that path and its subdirectories will be parsed") |
| 27 | |
| 28 | parser.add_argument("--waivableList", "-w", required=False, |
| 29 | help="A file that contains white space separated list of waivable violations") |
| 30 | |
| 31 | parser.add_argument("--outputDirectory", "-o", required=False, |
| 32 | help="Output Directory") |
| 33 | |
| 34 | args = parser.parse_args() |
| 35 | summaryFolder = os.path.abspath(args.targetPath) |
| 36 | outputDirectory = args.outputDirectory |
| 37 | if outputDirectory is None: |
| 38 | outputDirectory = summaryFolder |
| 39 | |
| 40 | if not os.path.exists(outputDirectory): |
| 41 | os.makedirs(outputDirectory) |
| 42 | |
| 43 | waiveList=[] |
| 44 | if args.waivableList is not None: |
| 45 | waivableListFileOpener = open(args.waivableList,"r") |
| 46 | waiveList = waivableListFileOpener.read().split() |
| 47 | waivableListFileOpener.close() |
| 48 | |
| 49 | header = "RULE,Waivable,rule_letter,category,COUNT 1, COUNT 2\n" |
| 50 | |
| 51 | |
| 52 | mainOutputFile = outputDirectory+'/'+os.path.basename(summaryFolder)+'_merged.csv' |
| 53 | |
| 54 | mainOutputFileOpener = open(mainOutputFile, "w+") |
| 55 | mainOutputFileOpener.write(header) |
| 56 | mainOutputFileOpener.close() |
| 57 | |
| 58 | |
| 59 | nonwaivableOutputFile = outputDirectory+'/'+os.path.basename(summaryFolder)+'_non_waivable.csv' |
| 60 | |
| 61 | nonwaivableFileOpener = open(nonwaivableOutputFile, "w+") |
| 62 | nonwaivableFileOpener.write(header) |
| 63 | nonwaivableFileOpener.close() |
| 64 | |
| 65 | |
| 66 | def getListOfFiles(dirName): |
| 67 | # create a list of file and sub directories |
| 68 | # names in the given directory |
| 69 | listOfFile = os.listdir(dirName) |
| 70 | allFiles = list() |
| 71 | # Iterate over all the entries |
| 72 | for entry in listOfFile: |
| 73 | # Create full path |
| 74 | fullPath = os.path.join(dirName, entry) |
| 75 | # If entry is a directory then get the list of files in this directory |
| 76 | if os.path.isdir(fullPath): |
| 77 | allFiles = allFiles + getListOfFiles(fullPath) |
| 78 | else: |
| 79 | allFiles.append(fullPath) |
| 80 | return allFiles |
| 81 | |
| 82 | |
| 83 | def extractSummary(summaryFile): |
| 84 | summaryFileOpener = open(summaryFile,"r") |
| 85 | summaryContent = summaryFileOpener.read().split("\n") |
| 86 | summaryFileOpener.close() |
| 87 | rule_category=os.path.basename(os.path.dirname(summaryFile)) |
| 88 | outputFile = outputDirectory+'/'+rule_category+'.'+os.path.basename(summaryFile)+'.csv' |
| 89 | |
| 90 | splitter = "----------------------------------------------------------------------------------" |
| 91 | ruleCheckString = "RULECHECK RESULTS STATISTICS" |
| 92 | |
| 93 | beginFlag = False |
| 94 | |
| 95 | outputFileOpener = open(outputFile, "w+") |
| 96 | outputFileOpener.write(header) |
| 97 | outputFileOpener.close() |
| 98 | |
| 99 | for line in summaryContent: |
| 100 | if line.find(ruleCheckString) != -1: |
| 101 | beginFlag = True |
| 102 | |
| 103 | if beginFlag: |
| 104 | if line == splitter: |
| 105 | break |
| 106 | |
| 107 | rule = re.findall(r'RULECHECK (\S+)\s*.*\s*TOTAL Result Count = (\d+)\s*\((\d+)\)', line) |
| 108 | |
| 109 | if len(rule) == 1: |
| 110 | ruleName = rule[0][0] |
| 111 | rk=ruleName[0] |
| 112 | waivable= 'no' |
| 113 | if ruleName in waiveList: |
| 114 | waivable='yes' |
| 115 | elif int(rule[0][1]) + int(rule[0][2]) != 0: |
| 116 | nonwaivableFileOpener = open(nonwaivableOutputFile, "a+") |
| 117 | nonwaivableFileOpener.write(str(",".join((ruleName, waivable,rk,rule_category,rule[0][1],rule[0][2])))+'\n') |
| 118 | nonwaivableFileOpener.close() |
| 119 | |
| 120 | outputFileOpener = open(outputFile, "a+") |
| 121 | outputFileOpener.write(str(",".join((ruleName, waivable,rk,rule_category,rule[0][1],rule[0][2])))+'\n') |
| 122 | outputFileOpener.close() |
| 123 | |
| 124 | mainOutputFileOpener = open(mainOutputFile, "a+") |
| 125 | mainOutputFileOpener.write(str(",".join((ruleName, waivable,rk,rule_category,rule[0][1],rule[0][2])))+'\n') |
| 126 | mainOutputFileOpener.close() |
| 127 | |
| 128 | files = getListOfFiles(summaryFolder) |
| 129 | for f in files: |
| 130 | extension = os.path.splitext(f)[1] |
| 131 | if extension == '.summary': |
| 132 | extractSummary(f) |