1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
#!/bin/bash
# Count lines of code in the project in an intelligent way.
#
# Notes/gotchas:
#
# 1. Use temporary files instead of variables to allow easier examination
# after the script is run, and also because newline handling in shell
# variables is rather hairy & error prone (e.g., command substitution
# strips trailing newlines but echo adds a trailing newline, hiding it).
#
# 2. To add/update language definitions, cloc only has “merge, but mine are
# lower priority” (--read-lang-def) and “use mine only, with no default
# definitions” (--force-lang-def). We need “merge my definitions with
# default, but with mine higher priority”, so we emulate it with
# --force-lang-def, providing all the language definitions we need, some
# of which are altered.
#
# 3. cloc also does not have a way to define file prefixes (say,
# Dockerfile.*). So we list all the Dockerfiles we have manually in the
# language definitions.
#
# [1]: https://github.com/AlDanial/cloc/issues/401
set -e -o pipefail
export LC_ALL=C
cd "$(dirname "$0")"/..
countem () {
msg=$1
infile=$2
outfile=${2}.out
catfile=${2}.cat
ignore_expected=${3:-0}
nolist=$4
echo
echo "*** $msg ***"
#cat "$infile"
rm -f /tmp/loc.ignore.out
cloc --force-lang-def=/dev/stdin \
--categorized="$catfile" \
--list-file="$infile" \
--ignored=/tmp/loc.ignore.out \
<<'EOF' | tee "$outfile"
Bash
filter remove_matches ^\s*#
filter remove_inline #.*$
extension bash
extension bats
extension bats.in
script_exe bash
3rd_gen_scale 3.81
end_of_line_continuation \\$
C
filter rm_comments_in_strings " /* */
filter rm_comments_in_strings " //
filter call_regexp_common C++
extension c
extension h
3rd_gen_scale 0.77
end_of_line_continuation \\$
conf-like
filter remove_matches ^\s*#
filter remove_inline #.*$
extension rpmlintrc
extension spec
filename .dockerignore
filename .gitattributes
filename .gitignore
3rd_gen_scale 1.00
end_of_line_continuation \\$
Dockerfile
filter remove_matches ^\s*#
filter remove_inline #.*$
extension df
filename Dockerfile
filename Dockerfile.argenv
filename Dockerfile.centos_7ch
filename Dockerfile.almalinux_9ch
filename Dockerfile.debian_12ch
filename Dockerfile.file-quirks
filename Dockerfile.libfabric
filename Dockerfile.metadata
filename Dockerfile.mpich
filename Dockerfile.nvidia
filename Dockerfile.ocimanifest
filename Dockerfile.openmpi
filename Dockerfile.quick
3rd_gen_scale 2.00
end_of_line_continuation \\$
JSON
filter remove_matches ^\s*$
extension json
3rd_gen_scale 2.50
m4
filter remove_matches ^dnl\s
filter remove_matches ^\s*#
filter remove_inline #.*$
extension ac
extension m4
3rd_gen_scale 1.00
Make
filter remove_matches ^\s*#
filter remove_inline #.*$
extension Gnumakefile
extension Makefile
extension am
extension gnumakefile
extension makefile
extension mk
filename Gnumakefile
filename Makefile
filename gnumakefile
filename makefile
script_exe make
3rd_gen_scale 2.50
end_of_line_continuation \\$
Markdown
filter remove_between_general <!-- -->
filter remove_between_regex \[(comment|\/\/)?\]\s*:?\s*(<\s*>|#)?\s*\(.*? .*?\)
extension md
3rd_gen_scale 1.00
patch
filter remove_matches ^#
filter remove_matches ^\-\-\-
filter remove_matches ^\+\+\+
filter remove_matches ^\s
filter remove_matches ^@@
extension diff
extension patch
3rd_gen_scale 1.00
plain text
filter remove_matches TEXT_HAS_NO_COMMENTS_BUT_A_FILTER_IS_REQUIRED
extension txt
filename NOTICE
filename PERUSEME
filename README
filename VERSION
filename approved-trailing-whitespace
3rd_gen_scale 1.00
POSIX sh
filter remove_matches ^\s*#
filter remove_inline #.*$
extension sh
script_exe sh
3rd_gen_scale 3.81
end_of_line_continuation \\$
Python
filter remove_matches ^\s*#
filter docstring_to_C
filter call_regexp_common C
filter remove_inline #.*$
extension py
extension py.in
script_exe python
script_exe python2.6
script_exe python2.7
script_exe python3
script_exe python3.3
script_exe python3.4
script_exe python3.5
3rd_gen_scale 4.20
end_of_line_continuation \\$
ReST
filter remove_between_regex ^\.\. ^[^\s\.]
extension rst
3rd_gen_scale 1.50
Ruby
filter remove_matches ^\s*#
filter remove_below_above ^=begin ^=end
filter remove_inline #.*$
extension rake
extension rb
filename Rakefile
filename rakefile
filename Vagrantfile
script_exe ruby
3rd_gen_scale 4.20
end_of_line_continuation \\$
VTK
filter remove_matches ^\s*#
extension vtk
3rd_gen_scale 1.00
end_of_line_continuation \\$
YAML
filter remove_matches ^\s*#
filter remove_inline #.*$
extension yaml
extension yml
extension yaml.in
extension yml.in
3rd_gen_scale 0.90
EOF
if [[ -z $nolist ]]; then
cat "$catfile"
fi
# Counting the number of ignored files is a problem because (1) before
# 1.84, the list of ignored files was empty [1], but (2) starting with
# 1.98, --quiet is assumed if stdin is not a TTY and there’s no way to get
# the previous behavior, so you can no longer look at the output. I
# couldn’t think of anything better than to assume the ≥1.98 behavior,
# which is probably OK since that came out in Sept. 2019.
#
# [1]: https://github.com/AlDanial/cloc/issues/401
ignore_actual=$(cat /tmp/loc.ignore.out | grep -c . || true)
if [[ $ignore_expected -ne $ignore_actual ]]; then
grep -F '(unknown)' "$catfile" || true
echo
echo "🚨🚨🚨 cloc ignoring wrong number of files; expected ${ignore_expected}, actual ${ignore_actual} 🚨🚨🚨"
cat /tmp/loc.ignore.out
exit 1
fi
}
if [[ -e ./Makefile.in ]]; then
echo '🚨🚨🚨 Makefile.in seems to exist 🚨🚨🚨'
echo 'did you "./autogen.sh --clean --rm-lark"?'
exit 1
fi
min_cloc=1.84
if ! command -v cloc > /dev/null \
|| [[ $( printf '%s\n%s\n' "$min_cloc" "$(cloc --version)" \
| sort -V | head -1 ) != "$min_cloc" ]]; then
echo "🚨🚨🚨 no cloc version ≥ $min_cloc 🚨🚨🚨"
echo 'did you try a better operating system?'
exit 1
fi
# program (Charliecloud itself)
find ./bin ./lib -type f -a \( \
-name '*.c' \
-o -name '*.h' \
-o -name '*.py' \
-o -name '*.py.in' \
-o -name '*.sh' \
-o -path './bin/ch-*' \) | grep -Fv ./bin/ch-test | sort > /tmp/loc.program
countem "PROGRAM" /tmp/loc.program
# test suite
find ./examples ./test -type f -a \( \
-name '*.bash' \
-o -name '*.bats' \
-o -name '*.bats.in' \
-o -name '*.c' \
-o -name '*.df' \
-o -name '*.json' \
-o -name '*.patch' \
-o -name '*.py' \
-o -name '*.py.in' \
-o -name '*.sh' \
-o -name '*.vtk' \
-o -name '*.yml' \
-o -name '*.yml.in' \
-o -name 'Build.*' \
-o -name 'Dockerfile.*' \
-o -name .dockerignore \
-o -name Build \
-o -name Dockerfile \
-o -name Makefile \
-o -name README \
-o -path ./examples/chtest/printns \
-o -path ./examples/spack/packages.yaml \
-o -path ./examples/spack/libfuse.patch \
-o -path ./test/fixtures/README \
-o -path ./test/gitlab.com/build-images \
-o -path ./test/gitlab.com/expand-matrices \
-o -path ./test/gitlab.com/reviews-email \
-o -path ./test/approved-trailing-whitespace \
-o -path ./test/common.bash \
-o -path ./test/doctest-auto \
-o -path ./test/old-storage \
-o -path ./test/sotest/files_inferrable.txt \
-o -path ./test/whiteout \) | sort > /tmp/loc.test
echo ./bin/ch-test >> /tmp/loc.test
countem "TEST SUITE & EXAMPLES" /tmp/loc.test
# documentation
find . -type f -a \( \
-path './doc/*.rst' \
-o -path './doc/releases/*.md' \
-o -path ./NOTICE \
-o -path ./README.rst \
-o -path ./doc/cdi-cray-libfabric.json \
-o -path ./doc/cdi-nvidia.json \
-o -path ./doc/conf.py \
-o -path ./doc/make-deps-overview \
-o -path ./doc/man/README \
-o -path ./doc/publish \) | sort > /tmp/loc.doc
countem "DOCUMENTATION" /tmp/loc.doc
# build system
find . -type f -a \( \
-name Makefile.am \
-o -path ./autogen.sh \
-o -path ./configure.ac \
-o -path ./misc/mkdir-p \
-o -path ./misc/version \
-o -path ./misc/m4/README \) | sort > /tmp/loc.build
countem "BUILD SYSTEM" /tmp/loc.build
# packaging code
find ./packaging \( -name Makefile.am \) -prune -o -type f \
-print | sort > /tmp/loc.packaging
countem "PACKAGING" /tmp/loc.packaging
# misc
find . -type f -a \( \
-path ./.gitattributes \
-o -path ./.gitignore \
-o -path ./VERSION \
-o -path ./misc/branches-tidy \
-o -path ./misc/gdb-backtrace \
-o -path ./misc/grep \
-o -path ./misc/loc \
-o -path ./misc/tar-upload \) | sort > /tmp/loc.misc
countem "MISCELLANEOUS" /tmp/loc.misc
# ignored - this includes binaries and files we distribute but didn’t write
find . -type f -a \( \
-name 'core' \
-o -name 'a.out' \
-o -name '*.ico' \
-o -name '.gitmodules' \
-o -name 'file_' \
-o -name '*.png' \
-o -name '*.tar.gz' \
-o -name 'file[A-Z0-9y]*' \
-o -name 's_dir?' \
-o -name 's_file?' \
-o -path './.vscode/*' \
-o -path './misc/m4/*.m4' \
-o -path './packaging/debian/*' \
-o -path ./LICENSE \
-o -path ./test/fixtures/empty-file \) | sort > /tmp/loc.ignored
echo
echo "*** IGNORED ***"
cat /tmp/loc.ignored
# everything
find . \( -path ./.git \
-o -path ./doc/html \
-o -name .DS_Store \
-o -name ._.DS_Store \
-o -name __pycache__ \) \
-prune -o -type f -print \
| sort > /tmp/loc.all
cat /tmp/loc.{all,ignored} | sort | uniq -u > /tmp/loc.total
countem "TOTAL" /tmp/loc.total 0 nolist
# test for files we forgot
cat /tmp/loc.{program,test,doc,build,packaging,misc,ignored,all} \
| sort | uniq -c | (grep -Ev '^\s*2' || true) > /tmp/loc.extra
if [[ -s /tmp/loc.extra ]]; then
echo
echo 'UNKNOWN FILES'
cat /tmp/loc.extra
echo
echo '🚨🚨🚨 unknown files found 🚨🚨🚨'
echo 'hint: did you forget to add new file(s) to misc/loc?'
exit 1
fi
# generate loc.rst
cmd='s/^SUM:.* ([0-9]+)$/\1/p'
cat <<EOF > doc/_loc.rst
.. Do not edit this file — it’s auto-generated.
We pride ourselves on keeping Charliecloud lightweight and simple. The lines
of code as of version $(cat VERSION) is:
.. list-table::
* - Program itself
- $(sed -En "${cmd}" /tmp/loc.program.out)
* - Test suite & examples
- $(sed -En "${cmd}" /tmp/loc.test.out)
* - Documentation
- $(sed -En "${cmd}" /tmp/loc.doc.out)
* - Build system
- $(sed -En "${cmd}" /tmp/loc.build.out)
* - Packaging
- $(sed -En "${cmd}" /tmp/loc.packaging.out)
* - Miscellaneous
- $(sed -En "${cmd}" /tmp/loc.misc.out)
* - Total
- $(sed -En "${cmd}" /tmp/loc.total.out)
These include code only, excluding blank lines and comments. They were counted
using \`cloc <https://github.com/AlDanial/cloc>\`_ version $(cloc --version).
We typically quote the "Program itself" number when describing the size of
Charliecloud. (Please do not quote the size in Priedhorsky and Randles 2017,
as that number is very out of date.)
EOF
echo
cat doc/_loc.rst
|