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

584 lines, 319 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 ls ysh/*.{py,pgen2} builtin/{func,method}*.py builtin/*_ysh.py | filter-py
186}
187
188ysh-counts() {
189 local count=$1
190 shift
191
192 ysh-files | $count \
193 'YSH' 'Expression grammar, parser, evaluator, etc.' "$@"
194}
195
196data-lang-files() {
197 ls data_lang/*.asdl
198 ls data_lang/*.py | filter-py
199 ls data_lang/*.{c,h} | egrep -v '_test' # exclude j8_test_lib as well
200}
201
202data-lang-counts() {
203 local count=$1
204 shift
205
206 data-lang-files | $count \
207 'Data Languages' 'JSON, J8 Notation, ...' "$@"
208}
209
210tools-files() {
211 ls tools/*.py | filter-py
212}
213
214tools-counts() {
215 local count=$1
216 shift
217
218 tools-files | $count \
219 'Tools' '' "$@"
220}
221
222cpp-binding-files() {
223 ls cpp/*.{cc,h} | egrep -v '_test.cc'
224}
225
226mycpp-runtime-files() {
227 ls mycpp/*.{cc,h} | egrep -v '_test.cc|bump_leak_heap'
228}
229
230cpp-counts() {
231 local count=$1
232 shift
233
234 cpp-binding-files | $count \
235 'Hand-written C++ Code' \
236 'Includes OS bindings. Small C++ files like cpp/osh_arith_parse.{cc,h} correspond to larger Python files like osh/arith_parse.py.' \
237 "$@"
238
239 # Remove code that isn't "in production"
240 mycpp-runtime-files | $count \
241 'Garbage-Collected Runtime' \
242 'Uses a fork-friendly Mark-Sweep collector.' \
243 "$@"
244
245 ls mycpp/*_test.cc cpp/*_test.cc | $count \
246 'Unit tests in C++' \
247 'The goal is to make the spec tests pass, but unit tests are helpful too.' \
248 "$@"
249
250 ls NINJA*.sh */NINJA*.py build/ninja*.{sh,py} | $count \
251 'Incremental C++ Build' '' "$@"
252}
253
254gen-cpp-counts() {
255 local count=$1
256 shift
257
258 # NOTE: this excludes .re2c.h file
259 ls _gen/*/*.{cc,h} | $count \
260 'Generated C++ Code' \
261 'mycpp generates the big file _gen/bin/oils-for-unix.mycpp.cc. Other programs like Zephyr ASDL and re2c generate other files.' \
262 "$@"
263}
264
265mycpp-counts() {
266 local count=$1
267 shift
268
269 ls mycpp/*.py | grep -v 'NINJA_subgraph.py' | filter-py | $count \
270 'mycpp Translator' \
271 "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()." \
272 "$@"
273
274 ls mycpp/examples/*.py | $count \
275 'mycpp Test Data' \
276 'Small Python examples that translate to C++, compile, and run.' \
277 "$@"
278}
279
280code-generator-counts() {
281 local count=$1
282 shift
283
284 ls asdl/*.py | filter-py | grep -v -E 'arith_|tdop|_demo' | $count \
285 'Zephyr ASDL' \
286 'A DSL for algebraic data types, borrowed from Python. Oils is the most strongly typed Bourne shell implementation!' \
287 "$@"
288
289 ls pgen2/*.py | filter-py | $count \
290 'pgen2 Parser Generator' \
291 'An LL(1) parser generator used to parse YSH expressions. Also borrowed from CPython.' \
292 "$@"
293
294 ls */*_gen.py | $count \
295 'Other Code Generators' \
296 '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++.' \
297 "$@"
298
299 ls yaks/*.py | filter-py | $count \
300 'Yaks' \
301 'Experimental replacement for mycpp' \
302 "$@"
303}
304
305spec-gold-counts() {
306 local count=$1
307 shift
308
309 ls spec/*.test.sh | $count \
310 'Spec Tests' \
311 '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.' \
312 "$@"
313
314 ls test/gold/*.sh | $count \
315 'Gold Tests' \
316 'Another suite that tests shells "from the outside". Instead of making explicit assertions, we verify that OSH behaves like bash.' \
317 "$@"
318}
319
320#
321# Top Level Summaries
322#
323
324_for-translation() {
325 local count=$1
326 shift
327
328 mycpp-counts $count "$@"
329
330 code-generator-counts $count "$@"
331
332 cpp-counts $count "$@"
333
334 osh-counts $count "$@"
335
336 ysh-counts $count "$@"
337
338 data-lang-counts $count "$@"
339
340 tools-counts $count "$@"
341
342 spec-gold-counts $count "$@"
343
344 gen-cpp-counts $count "$@"
345}
346
347_overview() {
348 local count=$1
349 shift
350
351 osh-counts $count "$@"
352
353 ysh-counts $count "$@"
354
355 data-lang-counts $count "$@"
356
357 tools-counts $count "$@"
358
359 ls stdlib/*.ysh | $count \
360 "YSH stdlib" '' "$@"
361
362 ls pylib/*.py | filter-py | $count \
363 "Code Borrowed from Python's stdlib" '' "$@"
364
365 spec-gold-counts $count "$@"
366
367 test/unit.sh files-to-count | $count \
368 'Python Unit Tests' '' "$@"
369
370 ls test/*.{sh,py,R} | filter-py | grep -v jsontemplate.py | $count \
371 'Other Shell Tests' '' "$@"
372
373 ls */TEST.sh | $count \
374 'Test Automation' '' "$@"
375
376 mycpp-counts $count "$@"
377
378 code-generator-counts $count "$@"
379
380 cpp-counts $count "$@"
381
382 # Leaving off gen-cpp-counts since that requires a C++ build
383
384 ls build/*.{mk,sh,py,c} Makefile configure install \
385 | filter-py | egrep -v 'NINJA|TEST' | $count \
386 'Build Automation' '' "$@"
387
388 ls devtools/release*.sh | $count \
389 'Release Automation' '' "$@"
390
391 ls soil/*.{sh,py} | $count \
392 'Soil: Multi-cloud CI with containers' '' "$@"
393
394 ls benchmarks/*.{sh,py,R} | $count \
395 'Benchmarks' '' "$@"
396
397 ls metrics/*.{sh,R} | $count \
398 'Metrics' '' "$@"
399
400 ls _devbuild/gen/*.py | $count \
401 'Generated Python Code' \
402 'For the Python App Bundle.' \
403 "$@"
404
405 ls {doctools,lazylex}/*.py doctools/*.{h,cc} | filter-py | $count \
406 'Doc Tools' '' "$@"
407
408 ls web/*.js web/*/*.{js,py} | $count \
409 'Web' '' "$@"
410}
411
412for-translation() {
413 _for-translation category-text
414}
415
416overview() {
417 _overview category-text
418}
419
420print-files() {
421 xargs -n 1 -- echo
422}
423
424overview-list() {
425 _overview print-files
426}
427
428#
429# HTML Versions
430#
431
432html-head() {
433 PYTHONPATH=. doctools/html_head.py "$@"
434}
435
436metrics-html-head() {
437 local title="$1"
438
439 local base_url='../../../web'
440
441 html-head --title "$title" "$base_url/base.css" "$base_url/table/table-sort.css" "$base_url/line-counts.css"
442}
443
444counts-html() {
445 local name=$1
446 local title=$2
447
448 local tmp_dir=_tmp/metrics/line-counts/$name
449
450 rm -r -f -v $tmp_dir >& 2
451 mkdir -v -p $tmp_dir >& 2
452
453 tsv-row category category_HREF total_lines num_files > $tmp_dir/INDEX.tsv
454
455 echo $'column_name\ttype
456category\tstring
457category_HREF\tstring
458total_lines\tinteger
459num_files\tinteger' >$tmp_dir/INDEX.schema.tsv
460
461 # Generate the HTML
462 "_$name" category-html $tmp_dir
463
464 metrics-html-head "$title"
465 echo ' <body class="width40">'
466
467 echo "<h1>$title</h1>"
468
469 tsv2html $tmp_dir/INDEX.tsv
470
471 echo '<hr/>'
472
473 echo '<h2>Related Documents</h2>
474 <p>The <a href="https://www.oilshell.org/release/latest/doc/README.html">README for oilshell/oil</a>
475 has another overview of the repository.
476 </p>'
477
478 # All the parts
479 cat $tmp_dir/*.html
480
481 echo ' </body>'
482 echo '</html>'
483}
484
485for-translation-html() {
486 local title='Overview: Translating Oils to C++'
487 counts-html for-translation "$title"
488}
489
490overview-html() {
491 local title='Overview of Oils Code'
492 counts-html overview "$title"
493}
494
495write-reports() {
496 local out_dir=${1:-_tmp/metrics/line-counts}
497
498 mkdir -v -p $out_dir
499
500 for-translation-html > $out_dir/for-translation.html
501
502 overview-html > $out_dir/overview.html
503
504 ls -l $out_dir
505}
506
507#
508# Misc
509#
510
511# count instructions, for fun
512instructions() {
513 # http://pepijndevos.nl/2016/08/24/x86-instruction-distribution.html
514
515 local bin=_build/oil/ovm-opt.stripped
516 objdump -d $bin | cut -f3 | grep -oE "^[a-z]+" | hist
517}
518
519hist() {
520 sort | uniq -c | sort -n
521}
522
523stdlib-imports() {
524 oil-osh-files | xargs grep --no-filename '^import' | hist
525}
526
527imports() {
528 oil-osh-files | xargs grep --no-filename -w import | hist
529}
530
531imports-not-at-top() {
532 oil-osh-files | xargs grep -n -w import | awk -F : ' $2 > 100'
533}
534
535# For the compiler, see what's at the top level.
536top-level() {
537 grep '^[a-zA-Z]' {core,osh}/*.py \
538 | grep -v '_test.py' \
539 | egrep -v ':import|from|class|def' # note: colon is from grep output
540}
541
542_python-symbols() {
543 local main=$1
544 local name=$2
545 local out_dir=$3
546
547 mkdir -p $out_dir
548 local out=${out_dir}/${name}-symbols.txt
549
550 # To debug what version we're running eci
551 /usr/bin/env python2 -V
552 echo
553
554 # Run this from the repository root.
555 PYTHONPATH='.:vendor/' CALLGRAPH=1 $main | tee $out
556
557 wc -l $out
558 echo
559 echo "Wrote $out"
560}
561
562oil-python-symbols() {
563 local out_dir=${1:-_tmp/opy-test}
564 _python-symbols bin/oil.py oil $out_dir
565}
566
567old-style-classes() {
568 oil-python-symbols | grep -v '<'
569}
570
571# Some of these are "abstract classes" like ChildStateChange
572NotImplementedError() {
573 grep NotImplementedError */*.py
574}
575
576py-ext() {
577 # for the py-source build
578 # 35 imports
579 osh-files | xargs -- egrep 'import (fanos|libc|line_input|posix_|yajl)'
580}
581
582if test $(basename $0) = 'source-code.sh'; then
583 "$@"
584fi