1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Run yapf formatter; it's installed in ~/wedge/ by build/deps.sh
|
4 | #
|
5 | # Usage:
|
6 | # test/format.sh <function name>
|
7 |
|
8 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
9 |
|
10 | source build/dev-shell.sh # python3 in $PATH
|
11 | source devtools/task-five.sh
|
12 |
|
13 | # Hack to prevent interference. TODO: Make a separate wedge for yapf.
|
14 | unset PYTHONPATH
|
15 |
|
16 | . build/common.sh # $CLANG_DIR
|
17 |
|
18 | set -o nounset
|
19 | set -o pipefail
|
20 | set -o errexit
|
21 | shopt -s strict:all 2>/dev/null || true # dogfood for OSH
|
22 |
|
23 | #
|
24 | # Python
|
25 | #
|
26 |
|
27 | readonly YAPF_VENV='_tmp/yapf-venv'
|
28 |
|
29 | install-yapf() {
|
30 | local venv=$YAPF_VENV
|
31 |
|
32 | rm -r -f -v $venv
|
33 |
|
34 | python3 -m venv $venv
|
35 |
|
36 | . $venv/bin/activate
|
37 |
|
38 | # 0.40.1 is the 2023-06-20 release
|
39 | #
|
40 | # Pin the version so formatting is stable!
|
41 |
|
42 | python3 -m pip install 'yapf == 0.40.1'
|
43 |
|
44 | yapf-version
|
45 | }
|
46 |
|
47 | yapf-version() {
|
48 | . $YAPF_VENV/bin/activate
|
49 | python3 -m yapf --version
|
50 | }
|
51 |
|
52 | # For now, run yapf on specific files. TODO: could query git for the files
|
53 | # that are are different from master branch, and run it on those.
|
54 | yapf-files() {
|
55 | . $YAPF_VENV/bin/activate
|
56 | python3 -m yapf -i "$@"
|
57 | }
|
58 |
|
59 | yapf-known() {
|
60 | ### yapf some files that have been normalized
|
61 |
|
62 | time yapf-files \
|
63 | {asdl,benchmarks,builtin,core,data_lang,doctools,frontend,lazylex,mycpp,mycpp/examples,osh,spec/*,yaks,ysh}/*.py \
|
64 | */NINJA_subgraph.py
|
65 | }
|
66 |
|
67 | yapf-changed() {
|
68 | branch="${1:-master}"
|
69 |
|
70 | #git diff --name-only .."$branch" '*.py'
|
71 |
|
72 | git diff --name-only .."$branch" '*.py' \
|
73 | | xargs --no-run-if-empty -- $0 yapf-files
|
74 | }
|
75 |
|
76 | #
|
77 | # Doc strings - one off
|
78 | #
|
79 |
|
80 | install-docformatter() {
|
81 | python3 -m pip install docformatter
|
82 | }
|
83 |
|
84 | docstrings() {
|
85 | ### Format docstrings - NOT done automatically, because it can mangle them
|
86 |
|
87 | # Requires manual fix-up
|
88 |
|
89 | #time test/lint.sh py2-files-to-lint \
|
90 | # | xargs --verbose -- python3 -m docformatter --in-place
|
91 |
|
92 | python3 -m docformatter --in-place lazylex/*.py
|
93 | }
|
94 |
|
95 | #
|
96 | # C++
|
97 | #
|
98 |
|
99 | clang-format() {
|
100 | # See //.clang-format for the style config.
|
101 | $CLANG_DIR/bin/clang-format --style=file "$@"
|
102 | }
|
103 |
|
104 | readonly -a CPP_FILES=(
|
105 | {asdl,core}/*.cc
|
106 | benchmarks/*.c
|
107 | cpp/*.{c,cc,h}
|
108 | data_lang/*.{c,cc,h}
|
109 | mycpp/*.{cc,h}
|
110 | mycpp/demo/*.{cc,h}
|
111 | demo/*.c
|
112 | doctools/*.{h,cc}
|
113 | yaks/*.h
|
114 |
|
115 | # Could add pyext, but they have sort of a Python style
|
116 | # pyext/fanos.c
|
117 | )
|
118 |
|
119 | cpp-files() {
|
120 | shopt -s nullglob
|
121 | for file in "${CPP_FILES[@]}"; do
|
122 |
|
123 | echo $file
|
124 | done
|
125 | }
|
126 |
|
127 | all-cpp() {
|
128 | # see build/common.sh
|
129 | if test -n "$CLANG_IS_MISSING"; then
|
130 | log ''
|
131 | log " *** $0: Did not find $CLANG_DIR_1"
|
132 | log " *** Run deps/from-binary.sh to get it"
|
133 | log ''
|
134 | return 1
|
135 | fi
|
136 |
|
137 | cpp-files | egrep -v 'greatest.h' | xargs -- $0 clang-format -i
|
138 | git diff
|
139 | }
|
140 |
|
141 | test-asdl-format() {
|
142 | ### Test how clang-format would like our generated code
|
143 |
|
144 | local file=${1:-_gen/asdl/hnode.asdl.h}
|
145 |
|
146 | local tmp=_tmp/hnode
|
147 | clang-format $file > $tmp
|
148 | diff -u $file $tmp
|
149 | }
|
150 |
|
151 | task-five "$@"
|