1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Calculate and filter deps of Python apps.
|
4 | #
|
5 | # Usage:
|
6 | # build/dynamic-deps.sh <function name>
|
7 |
|
8 | set -o nounset
|
9 | set -o pipefail
|
10 | set -o errexit
|
11 |
|
12 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
13 |
|
14 | source mycpp/common.sh # $MYPY_REPO
|
15 |
|
16 | readonly PY_PATH='.:vendor/'
|
17 |
|
18 | # Temporary
|
19 | readonly DIR=_build/NINJA
|
20 |
|
21 | # In git
|
22 | readonly FILTER_DIR='prebuilt/dynamic-deps'
|
23 |
|
24 | make-egrep() {
|
25 | # match chars until # or space, and line must be non-empty
|
26 | gawk '
|
27 | match($0, /([^# ]*)/, m) {
|
28 | contents = m[0]
|
29 | if (contents) { # skip empty lines
|
30 | print(contents)
|
31 | }
|
32 | }
|
33 | '
|
34 | }
|
35 |
|
36 | write-filters() {
|
37 | ### Write filename filters in the egrep -f format
|
38 |
|
39 | # For ./NINJA-config.sh to use.
|
40 | # This style lets us add comments.
|
41 |
|
42 | # For asdl.asdl_main and other tools
|
43 | make-egrep >$FILTER_DIR/filter-py-tool.txt <<'EOF'
|
44 | __init__.py
|
45 | typing.py # vendor/typing.py isn't imported normally
|
46 | EOF
|
47 |
|
48 | # Don't typecheck these files.
|
49 |
|
50 | make-egrep >$FILTER_DIR/filter-typecheck.txt <<'EOF'
|
51 | __init__.py
|
52 | typing.py
|
53 |
|
54 | # OrderedDict is polymorphic
|
55 | pylib/collections_.py
|
56 |
|
57 | # lots of polymorphic stuff etc.
|
58 | mycpp/mylib.py
|
59 |
|
60 | # TODO: move or remove these
|
61 | tools/deps.py
|
62 | tools/readlink.py
|
63 | EOF
|
64 |
|
65 | # On top of the typecheck filter, exclude these from translation. They are
|
66 | # not inputs to mycpp.
|
67 |
|
68 | make-egrep >$FILTER_DIR/filter-translate.txt <<'EOF'
|
69 | # generated code shouldn't be translated
|
70 | _devbuild/
|
71 | _gen/
|
72 |
|
73 | # definitions that are used by */*_gen.py
|
74 | .*_def\.py
|
75 | .*_spec\.py
|
76 |
|
77 | asdl/py.* # pybase.py ported by hand to C++
|
78 |
|
79 | core/py.* # pyos.py, pyutil.py ported by hand to C++
|
80 | core/optview\.py # core/optview_gen.py
|
81 |
|
82 | data_lang/py.* # pyj8.py
|
83 |
|
84 | frontend/py.*\.py # py_readline.py ported by hand to C++
|
85 | frontend/consts.py # frontend/consts_gen.py
|
86 | frontend/match.py # frontend/lexer_gen.py
|
87 |
|
88 | mycpp/mops.py # Implemented in gc_mops.{h,cC}
|
89 |
|
90 | pgen2/grammar.py # These files are re-done in C++
|
91 | pgen2/pnode.py
|
92 | pgen2/token.py
|
93 |
|
94 | # should be py_path_stat.py, because it's ported by hand to C++
|
95 | pylib/path_stat.py
|
96 |
|
97 | # should be py_bool_stat.py, because it's ported by hand to C++
|
98 | osh/bool_stat.py
|
99 |
|
100 | tea/
|
101 | EOF
|
102 |
|
103 | wc -l $FILTER_DIR/filter-*
|
104 | }
|
105 |
|
106 | repo-filter() {
|
107 | ### Select files from the dynamic_deps.py output
|
108 |
|
109 | # select what's in the repo; eliminating stdlib stuff
|
110 | # eliminate _cache for mycpp running under Python-3.10
|
111 | fgrep -v "$REPO_ROOT/_cache" | fgrep "$REPO_ROOT" | awk '{ print $2 }'
|
112 | }
|
113 |
|
114 | exclude-filter() {
|
115 | ### Exclude repo-relative paths
|
116 |
|
117 | local filter_name=$1
|
118 |
|
119 | egrep -v -f $FILTER_DIR/filter-$filter_name.txt
|
120 | }
|
121 |
|
122 | mysort() {
|
123 | LC_ALL=C sort
|
124 | }
|
125 |
|
126 | #
|
127 | # Programs
|
128 | #
|
129 |
|
130 | py-tool() {
|
131 | local py_module=$1
|
132 |
|
133 | local dir=$DIR/$py_module
|
134 | mkdir -p $dir
|
135 |
|
136 | PYTHONPATH=$PY_PATH /usr/bin/env python2 \
|
137 | build/dynamic_deps.py py-manifest $py_module \
|
138 | > $dir/all-pairs.txt
|
139 |
|
140 | cat $dir/all-pairs.txt | repo-filter | exclude-filter py-tool | mysort \
|
141 | > $dir/deps.txt
|
142 |
|
143 | echo "DEPS $dir/deps.txt"
|
144 | }
|
145 |
|
146 | # Code generators
|
147 | list-gen() {
|
148 | ls */*_gen.py
|
149 | }
|
150 |
|
151 | # mycpp and pea deps are committed to git instead of in _build/NINJA/ because
|
152 | # users might not have Python 3.10
|
153 |
|
154 | write-pea() {
|
155 | # PYTHONPATH=$PY_PATH
|
156 | local module='pea.pea_main'
|
157 | local dir=prebuilt/ninja/$module
|
158 | mkdir -p $dir
|
159 |
|
160 | source build/dev-shell.sh # python3
|
161 |
|
162 | # Can't use vendor/typing.py
|
163 | PYTHONPATH=. python3 \
|
164 | build/dynamic_deps.py py-manifest $module \
|
165 | > $dir/all-pairs.txt
|
166 |
|
167 | cat $dir/all-pairs.txt | repo-filter | mysort | tee $dir/deps.txt
|
168 |
|
169 | echo
|
170 | echo $dir/*
|
171 | }
|
172 |
|
173 | write-mycpp() {
|
174 | local module='mycpp.mycpp_main'
|
175 | local dir=prebuilt/ninja/$module
|
176 | mkdir -p $dir
|
177 |
|
178 | ( source $MYCPP_VENV/bin/activate
|
179 | PYTHONPATH=$REPO_ROOT:$REPO_ROOT/mycpp:$MYPY_REPO maybe-our-python3 \
|
180 | build/dynamic_deps.py py-manifest $module > $dir/all-pairs.txt
|
181 | )
|
182 |
|
183 | cat $dir/all-pairs.txt \
|
184 | | grep -v oilshell/oil_DEPS \
|
185 | | repo-filter \
|
186 | | exclude-filter py-tool \
|
187 | | mysort \
|
188 | | tee $dir/deps.txt
|
189 |
|
190 | echo
|
191 | echo $dir/*
|
192 | }
|
193 |
|
194 | mycpp-example-parse() {
|
195 | ### Manifests for mycpp/examples/parse are committed to git
|
196 |
|
197 | local dir=$DIR/parse
|
198 | mkdir -p $dir
|
199 |
|
200 | PYTHONPATH=$PY_PATH /usr/bin/env python2 \
|
201 | build/dynamic_deps.py py-manifest mycpp.examples.parse \
|
202 | > $dir/all-pairs.txt
|
203 |
|
204 | local ty=mycpp/examples/parse.typecheck.txt
|
205 | local tr=mycpp/examples/parse.translate.txt
|
206 |
|
207 | cat $dir/all-pairs.txt | repo-filter | exclude-filter typecheck | mysort > $ty
|
208 |
|
209 | cat $ty | exclude-filter translate > $tr
|
210 |
|
211 | wc -l $ty $tr
|
212 |
|
213 | #head $ty $tr
|
214 | }
|
215 |
|
216 | pea-hack() {
|
217 | # Leave out help_.py for Soil
|
218 | grep -v '_devbuild/gen/help_meta.py' $DIR/bin.oils_for_unix/typecheck.txt \
|
219 | > pea/oils-typecheck.txt
|
220 | }
|
221 |
|
222 | # Sourced by NINJA-config.sh
|
223 | if test $(basename $0) = 'dynamic-deps.sh'; then
|
224 | "$@"
|
225 | fi
|