forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpython-defs.sh
executable file
·354 lines (291 loc) · 8.65 KB
/
cpython-defs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env bash
#
# Usage:
# build/cpython-defs.sh <function name>
#
# Example:
#
# # make clean tree of .c files
# devtools/release.sh quick-oil-tarball
# devtools/release.sh test-oil-tar # can Ctrl-C this
#
# build/cpython-defs.sh oil-py-names # extract names
# build/cpython-defs.sh filter-methods
#
# NOTE: 'build/ovm-compile.sh make-tar' is complex, so it's easier to just extract
# the tarball, even though it leads to a weird dependency.
set -o nounset
set -o pipefail
set -o errexit
REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
readonly REPO_ROOT
source build/common.sh # $PY27
source build/dev-shell.sh # R_LIBS_USER
readonly BASE_DIR=_tmp/cpython-defs
# Could be published in metrics?
readonly PY_NAMES=_tmp/oil-py-names.txt
# Print the .py files in the tarball in their original locations. For slimming
# down the build. Similar to build/metrics.sh linecounts-pydeps.
# Hm that doesn't seem to duplicate posixpath while this does?
oil-py-deps() {
cat _build/oil/opy-app-deps.txt | awk ' $1 ~ /\.py$/ { print $1 }'
}
oil-py-names() {
time oil-py-deps | xargs bin/opyc lex-names | sort | uniq > $PY_NAMES
wc -l $PY_NAMES
}
# NOTE: We can replace os with posix. Will save 700 lines of code, 25K + 25K.
# os.getenv() is a trivial wrapper around os.environ.get(). It gets
# initialized in posixmodule.c.
os-module-deps() {
#oil-py-deps | xargs egrep --no-filename -o '\bos\.[a-z]+' */*.py | sort | uniq -c |sort -n
oil-py-deps | xargs egrep -l '\bos\.'
}
# TODO:
# Write to a separate file like _build/pydefs/intobject.include
# #ifdef OVM_MAIN
# #include "intobject.include"
# #else
# ...
# #end
#
# Should those files be checked in an edited by hand? Or join them somehow
# with oil-symbols.txt?
# I think this is hard because of METHODS.
# Maybe you should have a config file that controls it. It takes a .include
# file and then whitelist/blacklist, and then generates a new one.
# could put it in build/pydefs-config.txt
#
# And then reprint the PyMethoDef without docstrings? It shouldn't be that
# hard to parse. You can almost do it with a regex, since commas don't appear
# in the string.
extract-methods() {
local path_prefix=$1 # to strip
shift
local edit_list=$BASE_DIR/method-edit-list.txt
# NOTE: PyMemberDef is also interesting, but we don't need it for the build.
gawk -v path_prefix_length=${#path_prefix} -v edit_list=$edit_list '
/static.*PyMethodDef/ {
if (printing != 0) {
printf("%s:%d Expected not to be printing\n", FILENAME, FNR) > "/dev/stderr";
exit 1;
}
# NOTE: We had to adjust stringobject.c and _weakref.c so that the name is
# on one line! Not a big deal.
if (match($0, /static.*PyMethodDef ([a-zA-Z0-9_]+)\[\]/, m)) {
def_name = m[1];
} else {
printf("%s:%d Could not parse declaration name\n",
FILENAME, FNR) > "/dev/stderr";
exit 1;
}
printing = 1;
line_begin = FNR;
rel_path = substr(FILENAME, path_prefix_length + 1);
if (!found[FILENAME]) {
# This special line seems to survive the preprocessor?
printf("\n");
printf("FILE %s\n", rel_path);
printf("\n");
printf("Filtering %s\n", FILENAME) > "/dev/stderr";
found[FILENAME] = 1 # count number of files that have matches
}
}
printing { print }
# Looking for closing brace (with leading space)
/^[:space:]*\}/ && printing {
# Print the edit list for #ifdef #endif.
line_end = FNR;
printf("%s %s %d %d\n", rel_path, def_name, line_begin, line_end) > edit_list;
printing = 0;
}
END {
for (name in found) {
num_found++;
}
printf("extract-methods.awk: Found definitions in %d out of %d files\n",
num_found, ARGC) > "/dev/stderr";
}
' "$@"
}
preprocess() {
# TODO: Use PREPROC_FLAGS from build/ovm-compile.sh.
# - What about stuff in pyconfig.h?
# - Hack to define WTERMSIG! We really need to include <sys/wait.h>, but
# that causes parse errors in cpython_defs.py. Really we should get rid of
# this whole hack!
# - WIFSTOPPED is another likely thing...
gcc -I $PY27 -E -D OVM_MAIN -D WTERMSIG -
}
readonly TARBALL_ROOT=$(echo _tmp/oil-tar-test/oil-*)
extract-all-methods() {
echo '#include "pyconfig.h"'
# 52 different instances. Sometimes multiple ones per file.
find "$TARBALL_ROOT" -type f -a -name '*.c' \
| xargs -- $0 extract-methods "$TARBALL_ROOT/"
}
cpython-defs() {
# Annoying: this depends on Oil for 'R' and 'C', then indirectly imports on
# 'typing' module.
PYTHONPATH='.:vendor' build/cpython_defs.py "$@"
}
filter-methods() {
local tmp=$BASE_DIR
mkdir -p $tmp
extract-all-methods > $tmp/extracted.txt
cat $tmp/extracted.txt | preprocess > $tmp/preprocessed.txt
local out_dir=build/oil-defs
mkdir -p $out_dir
#head -n 30 $tmp
cat $tmp/preprocessed.txt | cpython-defs filter $PY_NAMES $out_dir
echo
find $out_dir -name '*.def' | xargs wc -l | sort -n
echo
wc -l $tmp/*.txt
# syntax check
#cc _tmp/filtered.c
}
edit-file() {
local rel_path=$1
local def_name=$2
local line_begin=$3
local line_end=$4
local def_path="${rel_path}/${def_name}.def"
local tmp=_tmp/buf.txt
# DESTRUCTIVE
mv $rel_path $tmp
gawk -v def_path=$def_path -v line_begin=$line_begin -v line_end=$line_end '
NR == line_begin {
print("#ifdef OVM_MAIN")
printf("#include \"%s\"\n", def_path)
print("#else")
print # print the PyMethodDef line {
next
}
NR == line_end {
print # print the }
print("#endif");
next
}
# All other lines just get printed
{
print
}
' $tmp > $rel_path
echo "Wrote $rel_path"
}
edit-all() {
# Reversed so that edits to the same file work! We are always inserting
# lines.
#tac $BASE_DIR/method-edit-list.txt | xargs -n 4 -- $0 edit-file
# One-off editing
grep typeobject.c $BASE_DIR/method-edit-list.txt \
| tac | xargs -n 4 -- $0 edit-file
}
extract-types() {
local path_prefix=$1 # to strip
shift
local edit_list=$BASE_DIR/type-edit-list.txt
# NOTE: PyMemberDef is also interesting, but we don't need it for the build.
gawk -v path_prefix_length=${#path_prefix} -v edit_list=$edit_list '
function maybe_print_file_header() {
rel_path = substr(FILENAME, path_prefix_length + 1);
if (!found[FILENAME]) {
# This special line seems to survive the preprocessor?
printf("\n");
printf("FILE %s\n", rel_path);
printf("\n");
printf("Filtering %s\n", FILENAME) > "/dev/stderr";
found[FILENAME] = 1 # count number of files that have matches
}
}
/PyTypeObject.*=.*\{.*\}/ {
if (printing != 0) {
printf("%s:%d Expected not to be printing\n", FILENAME, FNR) > "/dev/stderr";
exit 1;
}
// Found it all on one line
print
num_one_line_types++;
next
}
/PyTypeObject.*=.*\{/ {
if (printing != 0) {
printf("%s:%d Expected not to be printing\n", FILENAME, FNR) > "/dev/stderr";
exit 1;
}
printing = 1;
line_begin = FNR;
maybe_print_file_header()
num_types++;
}
{
if (printing) {
print
}
}
/^[:space:]*\}/ {
if (printing) {
# Print the edit list for #ifdef #endif.
line_end = FNR;
printf("%s %s %d %d\n", rel_path, def_name, line_begin, line_end) > edit_list;
printing = 0;
}
}
END {
for (name in found) {
num_found++;
}
printf("extract-types.awk: Found %d definitions in %d files (of %d files)\n",
num_types, num_found, ARGC) > "/dev/stderr";
printf("extract-types.awk: Also found %d types on one line\n",
num_one_line_types) > "/dev/stderr";
}
' "$@"
}
extract-all-types() {
find "$TARBALL_ROOT" -type f -a -name '*.c' \
| xargs -- $0 extract-types "$TARBALL_ROOT/"
}
#
# Analysis
#
readonly METRICS_DIR=_tmp/metrics/cpython-defs
# Show current Oil definitions literally.
show-oil() {
find build/oil-defs -name '*.def' | xargs cat | less
}
# Show in a contenses format.
methods-audit() {
mkdir -p $METRICS_DIR
cat $BASE_DIR/preprocessed.txt | cpython-defs audit $PY_NAMES \
| tee _tmp/methods.txt
wc -l _tmp/methods.txt
}
methods-tsv() {
mkdir -p $METRICS_DIR
local out=$METRICS_DIR/methods.tsv
cat $BASE_DIR/preprocessed.txt | cpython-defs tsv $PY_NAMES | tee $out
}
_report() {
metrics/cpython-defs.R "$@"
}
report() {
_report metrics $METRICS_DIR
}
run-for-release() {
# Repeats what we did at the beginning of the release process, because _tmp/
# was deleted
oil-py-names
filter-methods
methods-tsv
report | tee $METRICS_DIR/overview.txt
}
unfiltered() {
cpython-defs filtered | sort > _tmp/left.txt
awk '{print $1}' $BASE_DIR/edit-list.txt \
| egrep -o '[^/]+$' \
| sort | uniq > _tmp/right.txt
diff -u _tmp/{left,right}.txt
}
"$@"