diff --git a/doc.zih.tu-dresden.de/util/check-templated-code-snippets.py b/doc.zih.tu-dresden.de/util/check-templated-code-snippets.py new file mode 100755 index 0000000000000000000000000000000000000000..14470ed787f86c9267460aef53e590c8afd93336 --- /dev/null +++ b/doc.zih.tu-dresden.de/util/check-templated-code-snippets.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- +import re +import sys + +def escapeSomeSigns(someString): + return someString.replace("$", "\\$").replace("(", "\\(").replace(")", "\\)").replace("*", "\\*") + +fileName = sys.argv[1] +print("FILE: " + fileName) +lines = [] +NORMAL_MODE = 0 +CODE_MODE = 1 +readMode = NORMAL_MODE +pattern = re.compile(r"<[^<>']*>") +with open(fileName) as f: + lineNumber = 1 + for line in f: + if "```" in line: + # toggle read mode if we find a line with ```, so that we know that we are in a code block or not + readMode = CODE_MODE if readMode == NORMAL_MODE else NORMAL_MODE + strippedLine = line.strip() + # We want tuples with lineNumber, the line itself, whether it is a code line, whether it contains a template (e. g. <FILENAME>) and the line again with all templats replaced by '\\S' + lines.append((lineNumber, strippedLine, readMode, pattern.search(strippedLine) != None, pattern.sub(r"\\S*", escapeSomeSigns(strippedLine)))) + lineNumber += 1 +# those tuples with the CODE_MODE as field 2 represent code lines +codeLines = list(filter(lambda line: line[2] == CODE_MODE, lines)) +# we take line number, the line and a regular expression from the those code lines which contain a template, call them templatedLines +templatedLines = list(map(lambda line: (line[0], line[1], re.compile(line[4])), filter(lambda line: line[3], codeLines))) +allPatternsFound = True +for templateLine in templatedLines: + # find code lines which have a higher line number than the templateLine, contain no template themselves and match the pattern of the templateLine + matchingCodeLines = list(filter(lambda line: (line[0] > templateLine[0]) and (not line[3]) and (templateLine[2].match(line[1]) != None), codeLines)) + if len(matchingCodeLines) == 0: + allPatternsFound = False + print(" Example for \"" + templateLine[1] + "\" (Line " + str(templateLine[0]) + ") missing") + +if not allPatternsFound: + sys.exit(1) diff --git a/doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh b/doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh new file mode 100755 index 0000000000000000000000000000000000000000..2eecc039b7a3611eeef9b4b264bc0185c6fcad39 --- /dev/null +++ b/doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +set -eo pipefail + +scriptpath=${BASH_SOURCE[0]} +basedir=`dirname "$scriptpath"` +pythonscript="$basedir/check-templated-code-snippets.py" +basedir=`dirname "$basedir"` + +function usage() { + cat <<-EOF +usage: $0 [file | -a] +Search for code snippets that use templates but do not give examples. +If file is given, outputs all lines where no example could be found. +If parameter -a (or --all) is given instead of the file, checks all markdown files. +Otherwise, checks whether any changed file contains code snippets with templates without examples. +EOF +} + +branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}" + +# Options +if [ $# -eq 1 ]; then + case $1 in + help | -help | --help) + usage + exit + ;; + -a | --all) + echo "Search in all markdown files." + files=$(git ls-tree --full-tree -r --name-only HEAD $basedir/ | grep .md) + ;; + *) + files="$1" + ;; + esac +elif [ $# -eq 0 ]; then + echo "Search in git-changed files." + files=`git diff --name-only "$(git merge-base HEAD "$branch")" | grep .md || true` +else + usage +fi + +all_ok='' +for f in $files; do +if ! $pythonscript $f; then +all_ok='no' +fi +done + +if [ -z "$all_ok" ]; then +echo "Success!" +else +echo "Fail!" +exit 1 +fi diff --git a/doc.zih.tu-dresden.de/util/pre-commit b/doc.zih.tu-dresden.de/util/pre-commit index 1cc901e00efbece94209bfa6c4bbbc54aad682e9..cd91996b696ba94f095cec86b0be9b45fdc560a0 100755 --- a/doc.zih.tu-dresden.de/util/pre-commit +++ b/doc.zih.tu-dresden.de/util/pre-commit @@ -82,6 +82,13 @@ then exit_ok=no fi +echo "Looking for files with templates but without examples..." +docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh +if [ $? -ne 0 ] +then + exit_ok=no +fi + if [ $exit_ok == yes ] then exit 0