Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
hpc-compendium
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ZIH
hpcsupport
hpc-compendium
Commits
dac73c7e
Commit
dac73c7e
authored
3 years ago
by
Jan Frenzel
Browse files
Options
Downloads
Patches
Plain Diff
Added check-templated-code-snippets.py and
check-templated-code-snippets.sh.
parent
d63acbb2
No related branches found
No related tags found
2 merge requests
!530
Automated merge from preview to main
,
!481
Added check-templated-code-snippets.py and check-templated-code-snippets.sh.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
doc.zih.tu-dresden.de/util/check-templated-code-snippets.py
+39
-0
39 additions, 0 deletions
doc.zih.tu-dresden.de/util/check-templated-code-snippets.py
doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh
+56
-0
56 additions, 0 deletions
doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh
with
95 additions
and
0 deletions
doc.zih.tu-dresden.de/util/check-templated-code-snippets.py
0 → 100755
+
39
−
0
View file @
dac73c7e
#!/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
(
"
<[^<>
'
]*>
"
)
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
(
"
\\
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
)
This diff is collapsed.
Click to expand it.
doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh
0 → 100755
+
56
−
0
View file @
dac73c7e
#!/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
`
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment