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

590 lines, 323 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/*.ysh | $count \
366 "YSH stdlib" '' "$@"
367
368 ls pylib/*.py | filter-py | $count \
369 "Code Borrowed from Python's stdlib" '' "$@"
370
371 spec-gold-counts $count "$@"
372
373 test/unit.sh files-to-count | $count \
374 'Python Unit Tests' '' "$@"
375
376 ls test/*.{sh,py,R} | filter-py | grep -v jsontemplate.py | $count \
377 'Other Shell Tests' '' "$@"
378
379 ls */TEST.sh | $count \
380 'Test Automation' '' "$@"
381
382 mycpp-counts $count "$@"
383
384 code-generator-counts $count "$@"
385
386 cpp-counts $count "$@"
387
388 # Leaving off gen-cpp-counts since that requires a C++ build
389
390 ls build/*.{mk,sh,py,c} Makefile configure install \
391 | filter-py | egrep -v 'NINJA|TEST' | $count \
392 'Build Automation' '' "$@"
393
394 ls devtools/release*.sh | $count \
395 'Release Automation' '' "$@"
396
397 ls soil/*.{sh,py} | $count \
398 'Soil: Multi-cloud CI with containers' '' "$@"
399
400 ls benchmarks/*.{sh,py,R} | $count \
401 'Benchmarks' '' "$@"
402
403 ls metrics/*.{sh,R} | $count \
404 'Metrics' '' "$@"
405
406 ls _devbuild/gen/*.py | $count \
407 'Generated Python Code' \
408 'For the Python App Bundle.' \
409 "$@"
410
411 ls {doctools,lazylex}/*.py doctools/*.{h,cc} | filter-py | $count \
412 'Doc Tools' '' "$@"
413
414 ls web/*.js web/*/*.{js,py} | $count \
415 'Web' '' "$@"
416}
417
418for-translation() {
419 _for-translation category-text
420}
421
422overview() {
423 _overview category-text
424}
425
426print-files() {
427 xargs -n 1 -- echo
428}
429
430overview-list() {
431 _overview print-files
432}
433
434#
435# HTML Versions
436#
437
438html-head() {
439 PYTHONPATH=. doctools/html_head.py "$@"
440}
441
442metrics-html-head() {
443 local title="$1"
444
445 local base_url='../../../web'
446
447 html-head --title "$title" "$base_url/base.css" "$base_url/table/table-sort.css" "$base_url/line-counts.css"
448}
449
450counts-html() {
451 local name=$1
452 local title=$2
453
454 local tmp_dir=_tmp/metrics/line-counts/$name
455
456 rm -r -f -v $tmp_dir >& 2
457 mkdir -v -p $tmp_dir >& 2
458
459 tsv-row category category_HREF total_lines num_files > $tmp_dir/INDEX.tsv
460
461 echo $'column_name\ttype
462category\tstring
463category_HREF\tstring
464total_lines\tinteger
465num_files\tinteger' >$tmp_dir/INDEX.schema.tsv
466
467 # Generate the HTML
468 "_$name" category-html $tmp_dir
469
470 metrics-html-head "$title"
471 echo ' <body class="width40">'
472
473 echo "<h1>$title</h1>"
474
475 tsv2html $tmp_dir/INDEX.tsv
476
477 echo '<hr/>'
478
479 echo '<h2>Related Documents</h2>
480 <p>The <a href="https://www.oilshell.org/release/latest/doc/README.html">README for oilshell/oil</a>
481 has another overview of the repository.
482 </p>'
483
484 # All the parts
485 cat $tmp_dir/*.html
486
487 echo ' </body>'
488 echo '</html>'
489}
490
491for-translation-html() {
492 local title='Overview: Translating Oils to C++'
493 counts-html for-translation "$title"
494}
495
496overview-html() {
497 local title='Overview of Oils Code'
498 counts-html overview "$title"
499}
500
501write-reports() {
502 local out_dir=${1:-_tmp/metrics/line-counts}
503
504 mkdir -v -p $out_dir
505
506 for-translation-html > $out_dir/for-translation.html
507
508 overview-html > $out_dir/overview.html
509
510 ls -l $out_dir
511}
512
513#
514# Misc
515#
516
517# count instructions, for fun
518instructions() {
519 # http://pepijndevos.nl/2016/08/24/x86-instruction-distribution.html
520
521 local bin=_build/oil/ovm-opt.stripped
522 objdump -d $bin | cut -f3 | grep -oE "^[a-z]+" | hist
523}
524
525hist() {
526 sort | uniq -c | sort -n
527}
528
529stdlib-imports() {
530 oil-osh-files | xargs grep --no-filename '^import' | hist
531}
532
533imports() {
534 oil-osh-files | xargs grep --no-filename -w import | hist
535}
536
537imports-not-at-top() {
538 oil-osh-files | xargs grep -n -w import | awk -F : ' $2 > 100'
539}
540
541# For the compiler, see what's at the top level.
542top-level() {
543 grep '^[a-zA-Z]' {core,osh}/*.py \
544 | grep -v '_test.py' \
545 | egrep -v ':import|from|class|def' # note: colon is from grep output
546}
547
548_python-symbols() {
549 local main=$1
550 local name=$2
551 local out_dir=$3
552
553 mkdir -p $out_dir
554 local out=${out_dir}/${name}-symbols.txt
555
556 # To debug what version we're running eci
557 /usr/bin/env python2 -V
558 echo
559
560 # Run this from the repository root.
561 PYTHONPATH='.:vendor/' CALLGRAPH=1 $main | tee $out
562
563 wc -l $out
564 echo
565 echo "Wrote $out"
566}
567
568oil-python-symbols() {
569 local out_dir=${1:-_tmp/opy-test}
570 _python-symbols bin/oil.py oil $out_dir
571}
572
573old-style-classes() {
574 oil-python-symbols | grep -v '<'
575}
576
577# Some of these are "abstract classes" like ChildStateChange
578NotImplementedError() {
579 grep NotImplementedError */*.py
580}
581
582py-ext() {
583 # for the py-source build
584 # 35 imports
585 osh-files | xargs -- egrep 'import (fanos|libc|line_input|posix_|yajl)'
586}
587
588if test $(basename $0) = 'source-code.sh'; then
589 "$@"
590fi