From ec688b4723a041044226358bcd4dd6e2da39da49 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Thu, 23 Feb 2017 17:01:02 +0100 Subject: Initial commit: cframework. Longbow and Libparc Change-Id: I90378dbd30da6033b20fb1f829b3b822cf366c59 Signed-off-by: Luca Muscariello --- .../site-packages/longbow/VocabularyReport.py | 162 +++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100755 longbow/src/python/site-packages/longbow/VocabularyReport.py (limited to 'longbow/src/python/site-packages/longbow/VocabularyReport.py') diff --git a/longbow/src/python/site-packages/longbow/VocabularyReport.py b/longbow/src/python/site-packages/longbow/VocabularyReport.py new file mode 100755 index 00000000..5609db11 --- /dev/null +++ b/longbow/src/python/site-packages/longbow/VocabularyReport.py @@ -0,0 +1,162 @@ +#! /usr/bin/env python +# Copyright (c) 2017 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# + +import sys +import itertools + +import LongBow + +def computeVocabularyScore(tokenCount): + return 100.0 + + +def csvFunctionResult(file, function): + score = computeVocabularyScore(file.token_count) + string = "vocabulary,%s,%s,%d,%d,%.2f" % (file.filename, function.name, function.start_line, function.token_count, score) + + LongBow.scorePrinter([90, 80], score, string) + return function.token_count + + +def csvFileVocabulary(file): + score = computeVocabularyScore(file.token_count) + string = "vocabulary,%s,,,%.2f,%.2f" % (file.filename, file.average_token, score) + LongBow.scorePrinter([90, 80], score, string) + return + + +def csvFunction(fileInformationList): + for fileInformation in fileInformationList: + complexities = map(lambda function: csvFunctionResult(fileInformation, function), fileInformation) + return + + +def csvSummary(fileInformationList): + map(lambda file: csvFileVocabulary(file), fileInformationList) + return + + +def textFunctionResult(file, function, maxFileNameLength, maxFunctionNameLength): + score = computeVocabularyScore(function.token_count) + format = "%-" + str(maxFileNameLength) + "s %-" + str(maxFunctionNameLength) + "s %3d %3d %6.2f" + string = format % (file.filename, function.name, function.start_line, function.token_count, score) + + LongBow.scorePrinter([90, 80], score, string) + return function.cyclomatic_complexity + + +def textFileVocabulary(file, maxFileNameLength, printFormat=""): + score = computeVocabularyScore(file.average_CCN) + if printFormat == "": + printFormat = "%-" + str(maxFileNameLength) + "s %6.2f %6.2f" + string = printFormat % (file.filename, file.average_token, score) + LongBow.scorePrinter([90, 80], score, string) + return + + +def computeMaxFileNameLength(fileInformationList): + result = 0 + for fileInformation in fileInformationList: + if len(fileInformation.filename) > result: + result = len(fileInformation.filename) + return result + + +def computeMaxFunctionNameLength(fileInformationList): + result = 0 + for fileInformation in fileInformationList: + if len(fileInformation.filename) > result: + result = len(fileInformation.filename) + return result + + +def textFunction(fileInformationList): + maxFileNameLength = max(map(lambda fileInformation: len(fileInformation.filename), fileInformationList)) + maxFunctionNameLength = max(map(lambda fileInformation: max(map(lambda function: len(function.name), fileInformation)), fileInformationList)) + + for fileInformation in fileInformationList: + complexities = map(lambda function: textFunctionResult(fileInformation, function, maxFileNameLength, maxFunctionNameLength), fileInformation) + return + + +def textSummary(fileInformationList, prefix=""): + if len(fileInformationList) < 1: + print "%sNo Files To Grade" % prefix + return + maxFileNameLength = max(map(lambda fileInformation: len(fileInformation.filename), fileInformationList)) + printFormat = prefix + "%-" + str(maxFileNameLength) + "s %10s %6s" + print printFormat % ("File Path", "Ave Token", "Score") + printFormat = prefix + "%-" + str(maxFileNameLength) + "s %10.2f %6.2f" + map(lambda file: textFileVocabulary(file, maxFileNameLength, printFormat), fileInformationList) + return + + +def computeAverage(fileInformationList): + vocabulary = map(lambda fileInformation : fileInformation.average_token, fileInformationList) + sum = reduce(lambda sum, x: sum + x, vocabulary) + return float(sum) / float(len(vocabulary)) + + +def gradeAndPrint(fileList, hfcca, problemsOnly=False, prefix=""): + options, arguments = hfcca.createHfccaCommandLineParser().parse_args(args=["foo"]) + result = hfcca.analyze(fileList, options) + + # Convert from that iterator to a simple list... + fileInformationList = map(lambda x : x, result) + if problemsOnly: + fileInformationList = filter(lambda item: computeVocabularyScore(item.average_CCN) < 100, fileInformationList) + + textSummary(fileInformationList, prefix) + +def commandLineMain(args, hfcca): + targets = [] + + if args.stdin: + for line in sys.stdin: + t = line.strip() + if (len(t) > 0): + targets.append(t) + else: + targets = args.files + + if (len(targets) == 0): + print >> sys.stderr, "Error: target list cannot be empty" + + # If nothing was specified, print the summary as a default + if args.summary == False and args.function == False and args.average == False: + args.summary = True + + options, arguments = hfcca.createHfccaCommandLineParser().parse_args(args=["VocabularyReport"]) + result = hfcca.analyze(targets, options) + + # Convert from that iterator to a simple list... + fileInformationList = map(lambda x : x, result) + + if args.function: + if args.output == "text": + textFunction(fileInformationList) + else: + csvFunction(fileInformationList) + + if args.summary: + if args.output == "text": + textSummary(fileInformationList) + else: + csvSummary(fileInformationList) + + if args.average: + print "%.2f" % computeAverage(fileInformationList) -- cgit 1.2.3-korg