+ def invokeAspell(self):
+ try:
+ aspellSummary = ""
+ aspellIgnoreDictPath = ""
+ aspellIgnoreDictPathArg = ""
+ sourceType = self._currentSourceType()
+ if sourceType == self.SourceTypeTpkProject:
+ aspellIgnoreDictPath = os.path.join(os.getcwd(), "sample-project-src/aspell.ignore.txt")
+ elif sourceType == self.SourceTypeSampleProject:
+ aspellIgnoreDictPath = os.path.join(os.getcwd(), "aspell.ignore.txt")
+ if os.path.exists(aspellIgnoreDictPath):
+ aspellIgnoreDictPathArg = " --personal=" + aspellIgnoreDictPath + " "
+ for root, dirs, files in os.walk(os.getcwd()):
+ for fileName in files:
+ if fileName.endswith(".c") or fileName.endswith(".h"):
+
+ fullFilePath = os.path.join(root, fileName)
+ relativeFilePath = os.path.relpath(fullFilePath)
+ file = open(fullFilePath)
+ lines = file.readlines()
+ file.close()
+ aspell = subprocess.Popen("bash -c 'set -o pipefail; aspell pipe list --mode=ccpp --run-together --lang=en_US " + aspellIgnoreDictPathArg + "< " +
+ fullFilePath + " | grep \"\w\+ [0-9]\+ [0-9]\+:\" || true'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ stdoutOutput,stderrOutput = aspell.communicate()
+ if aspell.returncode != 0 or stderrOutput:
+ return False, "Error from aspell (error code:" + str(aspell.returncode) + "):\n" + stderrOutput
+ #remove empty lines
+ outputLines = filter(None, stdoutOutput.split("\n"))
+
+ if outputLines is not None and len(outputLines) > 0:
+ words = []
+ hints = []
+ for outputLine in outputLines:
+ tmpWord = re.search("[\w']+(?= )", outputLine).group(0)
+ if tmpWord not in words:
+ words.append(tmpWord)
+ hints.append(outputLine.split(":")[1])
+ longestOutputLine = len(max(words, key=len))
+ checkResult = []
+ for i,word in enumerate(words):
+ for lineNum, inputFileLine in enumerate(lines):
+ #standard regex \b doesn't include _ but aspell treats it as beginning of a word
+ reRes = re.search("(\\b{0}\\b)|(_+{0}\\b)|(_+{0}_+)|(\\b{0}_+)".format(word), inputFileLine)
+ if reRes is not None:
+ checkResult.append(str(lineNum + 1).rjust(5, " ") + ": " + word.ljust(longestOutputLine + 1, " ") + " -> " + re.sub("\\b"+word+"\\b", word.upper(),inputFileLine.strip()) +
+ "\n" + "(".rjust(5 + 2 + longestOutputLine + 1 + 4, " ") + hints[i] + ")")
+ if len(words) > len(checkResult):
+ return False, "There is some problem in regular expression (words set is bigger than found set)"
+
+ if len(checkResult) > 0:
+ #sort by line number - the output is like LINU_NUM:WORD
+ # 12: someunknownword
+ checkResult.sort(key=lambda line: int(line.split(":")[0].strip()))
+
+ aspellSummary += "\nASPELL CHECK FILE: " + relativeFilePath + "\n"
+ aspellSummary += "\n".join(checkResult)
+ return len(aspellSummary) == 0, aspellSummary
+ except subprocess.CalledProcessError as err:
+ return False, traceback.format_exc() + "\noutput:" + err.output