OILS / metrics / source-code.sh View on Github | oilshell.org

593 lines, 324 significant
1#!/usr/bin/env bash
2#
3# Count lines of code in various ways.
4#
5# Usage:
6# metrics/source-code.sh <function name>
7
8set -o nounset
9set -o pipefail
10set -o errexit
11
12REPO_ROOT=$(cd $(dirname $0)/.. && pwd) # tsv-lib.sh uses this
13readonly REPO_ROOT
14
15source test/common.sh
16source test/tsv-lib.sh
17
18filter-py() {
19 grep -E -v '__init__.py$|_gen.py|_test.py|_tests.py|NINJA_subgraph.py$'
20}
21
22readonly -a OSH_ASDL=( {frontend,core,display}/*.asdl )
23
24oils-files() {
25 # what's in the runtime
26 osh-files
27 ysh-files
28 data-lang-files
29 tools-files
30}
31
32# OSH and common
33osh-files() {
34 # Exclude:
35 # - line_input.c because I didn't write it. It still should be minimized.
36 # - code generators
37 # - test library
38 #
39 # note: could move display/ to a separate part
40 ls bin/oils_for_unix.py {osh,core,display,frontend}/*.py builtin/*_osh.py \
41 pyext/*.c */*.pyi \
42 "${OSH_ASDL[@]}" \
43 | filter-py | grep -E -v 'posixmodule.c$|line_input.c$|_gen.py$|test_lib.py$|os.pyi$'
44}
45
46# cloc doesn't understand ASDL files.
47# Use a wc-like format, filtering out blank lines and comments.
48asdl-cloc() {
49 python -c '
50import sys
51
52total = 0
53for path in sys.argv[1:]:
54 num_lines = 0
55 with open(path) as f:
56 for line in f:
57 line = line.strip()
58 if not line or line.startswith("#"):
59 continue
60 num_lines += 1
61
62 print "%5d %s" % (num_lines, path)
63 total += num_lines
64
65print "%5d %s" % (total, "total")
66' "$@"
67}
68
69cloc-report() {
70 echo '(non-blank non-comment lines)'
71 echo
72
73 echo 'OSH'
74 echo
75 osh-files | xargs cloc --quiet "$@"
76 echo
77 echo
78
79 echo 'YSH'
80 echo
81 ysh-files | xargs cloc --quiet "$@"
82 echo
83 echo
84
85 echo 'Data Languages'
86 echo
87 data-lang-files | xargs cloc --quiet "$@"
88 echo
89 echo
90
91 echo 'Tools'
92 echo
93 tools-files | xargs cloc --quiet "$@"
94 echo
95 echo
96
97 echo 'ASDL SCHEMAS (non-blank non-comment lines)'
98 asdl-cloc "${OSH_ASDL[@]}" data_lang/*.asdl
99 echo
100 echo
101
102 # NOTE: --csv option could be parsed into HTML.
103 # Or just sum with asdl-cloc!
104
105 echo 'Hand-Written C++ code (non-blank non-comment lines)'
106 echo
107 { cpp-binding-files; mycpp-runtime-files; } | xargs cloc --quiet "$@"
108}
109
110preprocessed() {
111 ./NINJA-config.sh
112
113 # Clang has slightly fewer lines, but it's not on the CI machine
114 #local -a files=(_build/preprocessed/{cxx,clang}-{dbg,opt}.txt)
115
116 local -a files=(_build/preprocessed/cxx-{dbg,opt}.txt)
117
118 ninja "${files[@]}"
119
120 # Publish with release and show and CI
121
122 local dir=_tmp/metrics/preprocessed
123 mkdir -p $dir
124 cp -v "${files[@]}" $dir
125
126 head -n 100 $dir/*.txt
127}
128
129#
130# Two variants of the $count function: text and html
131#
132
133category-text() {
134 local header=$1
135 local comment=$2
136
137 echo "$header"
138 # omit comment
139
140 # stdin is the files
141 xargs wc -l | sort --numeric
142 echo
143}
144
145# This is overly clever ...
146shopt -s lastpipe
147SECTION_ID=0 # mutable global
148
149category-html() {
150 # TODO: Don't use wc -l, and just count and sum the lines yourself
151
152 xargs wc -l | metrics/line_counts.py $((++SECTION_ID)) "$@"
153}
154
155#
156# Functions That Count
157#
158
159# Note this style is OVERLY ABSTRACT, but it's hard to do better in shell. We
160# want to parameterize over text and HTML. In Oils I think we would use this:
161#
162# proc p1 {
163# category 'OSH (and common libraries)' {
164# comment = 'This is the input'
165# osh-files | read --lines :files
166# }
167# }
168#
169# This produces a series of dicts that looks like
170# { name: 'OSH ...', comment: "This ...", files: %(one two three) }
171#
172# Then we iterate over the categories and produce text or HTML.
173
174osh-counts() {
175 local count=$1
176 shift
177
178 osh-files | $count \
179 'OSH (and common libraries)' \
180 'This is the input to the translators, written in statically-typed Python. Note that bash is at least 140K lines of code, and OSH implements a large part of bash and more.' \
181 "$@"
182}
183
184ysh-files() {
185 # Count meta_oils.py as YSH, not OSH, even though it contains the shell
186 # 'builtin command type' builtins. We will generalize that a bit
187 ls ysh/*.{py,pgen2} \
188 builtin/{func,method}*.py \
189 builtin/*_ysh.py \
190 builtin/*_oils.py \
191 | filter-py
192}
193
194ysh-counts() {
195 local count=$1
196 shift
197
198 ysh-files | $count \
199 'YSH' 'Expression grammar, parser, evaluator, etc.' "$@"
200}
201
202data-lang-files() {
203 ls data_lang/*.asdl
204 ls data_lang/*.py | filter-py
205 ls data_lang/*.{c,h} | egrep -v '_test' # exclude j8_test_lib as well
206}
207
208data-lang-counts() {
209 local count=$1
210 shift
211
212 data-lang-files | $count \
213 'Data Languages' 'JSON, J8 Notation, ...' "$@"
214}
215
216tools-files() {
217 ls tools/*.py | filter-py
218}
219
220tools-counts() {
221 local count=$1
222 shift
223
224 tools-files | $count \
225 'Tools' '' "$@"
226}
227
228cpp-binding-files() {
229 ls cpp/*.{cc,h} | egrep -v '_test.cc'
230}
231
232mycpp-runtime-files() {
233 ls mycpp/*.{cc,h} | egrep -v '_test.cc|bump_leak_heap'
234}
235
236cpp-counts() {
237 local count=$1
238 shift
239
240 cpp-binding-files | $count \
241 'Hand-written C++ Code' \
242 'Includes OS bindings. Small C++ files like cpp/osh_arith_parse.{cc,h} correspond to larger Python files like osh/arith_parse.py.' \
243 "$@"
244
245 # Remove code that isn't "in production"
246 mycpp-runtime-files | $count \
247 'Garbage-Collected Runtime' \
248 'Uses a fork-friendly Mark-Sweep collector.' \
249 "$@"
250
251 ls mycpp/*_test.cc cpp/*_test.cc | $count \
252 'Unit tests in C++' \
253 'The goal is to make the spec tests pass, but unit tests are helpful too.' \
254 "$@"
255
256 ls NINJA*.sh */NINJA*.py build/ninja*.{sh,py} | $count \
257 'Incremental C++ Build' '' "$@"
258}
259
260gen-cpp-counts() {
261 local count=$1
262 shift
263
264 # NOTE: this excludes .re2c.h file
265 ls _gen/*/*.{cc,h} | $count \
266 'Generated C++ Code' \
267 'mycpp generates the big file _gen/bin/oils-for-unix.mycpp.cc. Other programs like Zephyr ASDL and re2c generate other files.' \
268 "$@"
269}
270
271mycpp-counts() {
272 local count=$1
273 shift
274
275 ls mycpp/*.py | grep -v 'NINJA_subgraph.py' | filter-py | $count \
276 'mycpp Translator' \
277 "This prototype uses the MyPy frontend to translate statically-typed Python to C++. The generated code calls a small runtime which implements things like List[T], Dict[K, V], and Python's len()." \
278 "$@"
279
280 ls mycpp/examples/*.py | $count \
281 'mycpp Test Data' \
282 'Small Python examples that translate to C++, compile, and run.' \
283 "$@"
284}
285
286code-generator-counts() {
287 local count=$1
288 shift
289
290 ls asdl/*.py | filter-py | grep -v -E 'arith_|tdop|_demo' | $count \
291 'Zephyr ASDL' \
292 'A DSL for algebraic data types, borrowed from Python. Oils is the most strongly typed Bourne shell implementation!' \
293 "$@"
294
295 ls pgen2/*.py | filter-py | $count \
296 'pgen2 Parser Generator' \
297 'An LL(1) parser generator used to parse YSH expressions. Also borrowed from CPython.' \
298 "$@"
299
300 ls */*_gen.py | $count \
301 'Other Code Generators' \
302 'In order to make Oils statically typed, we had to abandon Python reflection and use C++ source code generation instead. The lexer, flag definitions, and constants can be easily compiled to C++.' \
303 "$@"
304
305 ls yaks/*.py | filter-py | $count \
306 'Yaks' \
307 'Experimental replacement for mycpp' \
308 "$@"
309}
310
311spec-gold-counts() {
312 local count=$1
313 shift
314
315 ls spec/*.test.sh | $count \
316 'Spec Tests' \
317 'A comprehensive test suite that compares OSH against other shells. If OSH passes these tests in BOTH Python and C++, it means that the translation works.' \
318 "$@"
319
320 ls test/gold/*.sh | $count \
321 'Gold Tests' \
322 'Another suite that tests shells "from the outside". Instead of making explicit assertions, we verify that OSH behaves like bash.' \
323 "$@"
324}
325
326#
327# Top Level Summaries
328#
329
330_for-translation() {
331 local count=$1
332 shift
333
334 mycpp-counts $count "$@"
335
336 code-generator-counts $count "$@"
337
338 cpp-counts $count "$@"
339
340 osh-counts $count "$@"
341
342 ysh-counts $count "$@"
343
344 data-lang-counts $count "$@"
345
346 tools-counts $count "$@"
347
348 spec-gold-counts $count "$@"
349
350 gen-cpp-counts $count "$@"
351}
352
353_overview() {
354 local count=$1
355 shift
356
357 osh-counts $count "$@"
358
359 ysh-counts $count "$@"
360
361 data-lang-counts $count "$@"
362
363 tools-counts $count "$@"
364
365 ls stdlib/osh/*.sh | $count \
366 "OSH stdlib" '' "$@"
367
368 ls stdlib/ysh/*.ysh | $count \
369 "YSH stdlib" '' "$@"
370
371 ls pylib/*.py | filter-py | $count \
372 "Code Borrowed from Python's stdlib" '' "$@"
373
374 spec-gold-counts $count "$@"
375
376 test/unit.sh files-to-count | $count \
377 'Python Unit Tests' '' "$@"
378
379 ls test/*.{sh,py,R} | filter-py | grep -v jsontemplate.py | $count \
380 'Other Shell Tests' '' "$@"
381
382 ls */TEST.sh | $count \
383 'Test Automation' '' "$@"
384
385 mycpp-counts $count "$@"
386
387 code-generator-counts $count "$@"
388
389 cpp-counts $count "$@"
390
391 # Leaving off gen-cpp-counts since that requires a C++ build
392
393 ls build/*.{mk,sh,py,c} Makefile configure install \
394 | filter-py | egrep -v 'NINJA|TEST' | $count \
395 'Build Automation' '' "$@"
396
397 ls devtools/release*.sh | $count \
398 'Release Automation' '' "$@"
399
400 ls soil/*.{sh,py} | $count \
401 'Soil: Multi-cloud CI with containers' '' "$@"
402
403 ls benchmarks/*.{sh,py,R} | $count \
404 'Benchmarks' '' "$@"
405
406 ls metrics/*.{sh,R} | $count \
407 'Metrics' '' "$@"
408
409 ls _devbuild/gen/*.py | $count \
410 'Generated Python Code' \
411 'For the Python App Bundle.' \
412 "$@"
413
414 ls {doctools,lazylex}/*.py doctools/*.{h,cc} | filter-py | $count \
415 'Doc Tools' '' "$@"
416
417 ls web/*.js web/*/*.{js,py} | $count \
418 'Web' '' "$@"
419}
420
421for-translation() {
422 _for-translation category-text
423}
424
425overview() {
426 _overview category-text
427}
428
429print-files() {
430 xargs -n 1 -- echo
431}
432
433overview-list() {
434 _overview print-files
435}
436
437#
438# HTML Versions
439#
440
441html-head() {
442 PYTHONPATH=. doctools/html_head.py "$@"
443}
444
445metrics-html-head() {
446 local title="$1"
447
448 local base_url='../../../web'
449
450 html-head --title "$title" "$base_url/base.css" "$base_url/table/table-sort.css" "$base_url/line-counts.css"
451}
452
453counts-html() {
454 local name=$1
455 local title=$2
456
457 local tmp_dir=_tmp/metrics/line-counts/$name
458
459 rm -r -f -v $tmp_dir >& 2
460 mkdir -v -p $tmp_dir >& 2
461
462 tsv-row category category_HREF total_lines num_files > $tmp_dir/INDEX.tsv
463
464 echo $'column_name\ttype
465category\tstring
466category_HREF\tstring
467total_lines\tinteger
468num_files\tinteger' >$tmp_dir/INDEX.schema.tsv
469
470 # Generate the HTML
471 "_$name" category-html $tmp_dir
472
473 metrics-html-head "$title"
474 echo ' <body class="width40">'
475
476 echo "<h1>$title</h1>"
477
478 tsv2html $tmp_dir/INDEX.tsv
479
480 echo '<hr/>'
481
482 echo '<h2>Related Documents</h2>
483 <p>The <a href="https://www.oilshell.org/release/latest/doc/README.html">README for oilshell/oil</a>
484 has another overview of the repository.
485 </p>'
486
487 # All the parts
488 cat $tmp_dir/*.html
489
490 echo ' </body>'
491 echo '</html>'
492}
493
494for-translation-html() {
495 local title='Overview: Translating Oils to C++'
496 counts-html for-translation "$title"
497}
498
499overview-html() {
500 local title='Overview of Oils Code'
501 counts-html overview "$title"
502}
503
504write-reports() {
505 local out_dir=${1:-_tmp/metrics/line-counts}
506
507 mkdir -v -p $out_dir
508
509 for-translation-html > $out_dir/for-translation.html
510
511 overview-html > $out_dir/overview.html
512
513 ls -l $out_dir
514}
515
516#
517# Misc
518#
519
520# count instructions, for fun
521instructions() {
522 # http://pepijndevos.nl/2016/08/24/x86-instruction-distribution.html
523
524 local bin=_build/oil/ovm-opt.stripped
525 objdump -d $bin | cut -f3 | grep -oE "^[a-z]+" | hist
526}
527
528hist() {
529 sort | uniq -c | sort -n
530}
531
532stdlib-imports() {
533 oil-osh-files | xargs grep --no-filename '^import' | hist
534}
535
536imports() {
537 oil-osh-files | xargs grep --no-filename -w import | hist
538}
539
540imports-not-at-top() {
541 oil-osh-files | xargs grep -n -w import | awk -F : ' $2 > 100'
542}
543
544# For the compiler, see what's at the top level.
545top-level() {
546 grep '^[a-zA-Z]' {core,osh}/*.py \
547 | grep -v '_test.py' \
548 | egrep -v ':import|from|class|def' # note: colon is from grep output
549}
550
551_python-symbols() {
552 local main=$1
553 local name=$2
554 local out_dir=$3
555
556 mkdir -p $out_dir
557 local out=${out_dir}/${name}-symbols.txt
558
559 # To debug what version we're running eci
560 /usr/bin/env python2 -V
561 echo
562
563 # Run this from the repository root.
564 PYTHONPATH='.:vendor/' CALLGRAPH=1 $main | tee $out
565
566 wc -l $out
567 echo
568 echo "Wrote $out"
569}
570
571oil-python-symbols() {
572 local out_dir=${1:-_tmp/opy-test}
573 _python-symbols bin/oil.py oil $out_dir
574}
575
576old-style-classes() {
577 oil-python-symbols | grep -v '<'
578}
579
580# Some of these are "abstract classes" like ChildStateChange
581NotImplementedError() {
582 grep NotImplementedError */*.py
583}
584
585py-ext() {
586 # for the py-source build
587 # 35 imports
588 osh-files | xargs -- egrep 'import (fanos|libc|line_input|posix_|yajl)'
589}
590
591if test $(basename $0) = 'source-code.sh'; then
592 "$@"
593fi