-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle-web-file.sh
executable file
·147 lines (127 loc) · 3.56 KB
/
handle-web-file.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
#!/usr/bin/env bash
# handle-web-file.sh [LINK_TEXT] URL
#
# Downloads an archive and tries to guess how to build the coq
# development from it
set -ex
descr="$1"
url="$2"
if [ -z "$url" ]; then
url="$descr"
descr=""
fi
if [ -z "$url" ]; then
>&2 printf "USAGE: %s [LINK_TEXT] URL\n" "$0"
exit 1
fi
rawfname="$(basename "$url")"
extension="${rawfname##*.}"
base="${rawfname%%.*}"
fname="downloaded-$rawfname"
qfname="$(printf "%q" "$fname")"
qextension="$(printf "%q" "$extension")"
wget "$url" -O "$fname"
success="no"
function trial_extract_raw() {
if [ "$success" == "no" ]; then
rm -rf bug-output
mkdir -p bug-output
cd bug-output
bash -c "$1" && { success="yes"; cd ..; } || { cd ..; rm -rf bug-output; }
fi
}
function trial_extract() {
trial_extract_raw "$(printf "%q " "$@")"
}
function succeed() {
success="yes"
}
function run_unique_file_pattern() {
count="$(find . -name "$1" -executable -type f | wc -l)"
rv=1
if [ "$count" -eq 1 ]; then
rv=0
f="$(find . -name "$1" -executable -type f)"
dirf="$(dirname "$f")"
f="$(basename "$f")"
pushd "$dirf"
./"$f" || { rv=$?; }
popd
fi
return $rv
}
function run_first_unique_file_patterns() {
success="no"
for name in "$@"; do
test "$success" == "yes" || { run_unique_file_pattern "$name" && succeed || true; }
done
test "$success" == "yes"
}
function try_autogen() {
run_first_unique_file_patterns autogen autogen.sh
}
function try_configure() {
run_first_unique_file_patterns configure configure.sh
}
function try_make() {
fs="$(find . \( -name GNUmakefile -o -name makefile -o -name Makefile \) -type f)"
count="$(find . \( -name GNUmakefile -o -name makefile -o -name Makefile \) -type f | wc -l)"
if [ "$count" -eq 1 ]; then
dirf="$(dirname "$fs")"
f="$(basename "$fs")"
pushd "$dirf"
make || true
popd
return 0
fi
return 1
}
function try_autogen_configure_make() {
try_make || { try_configure && try_make; } || { try_autogen && try_configure && try_make; }
}
function try_coq_makefile() {
fs="$(find . \( -name "_CoqProject" -o -name "CoqProject" \) -type f)"
count="$(find . \( -name "_CoqProject" -o -name "CoqProject" \) -type f | wc -l)"
if [ "$count" -eq 1 ]; then
dirf="$(dirname "$fs")"
f="$(basename "$fs")"
pushd "$dirf"
coq_makefile -f "$f" -o Makefile
make || true
popd
return 0
fi
return 1
}
function try_coqc() {
fs="$(find . -name "*.v" -type f)"
count="$(find . -name "*.v" -type f | wc -l)"
if [ "$count" -eq 1 ]; then
dirf="$(dirname "$fs")"
f="$(basename "$fs")"
pushd "$dirf"
coqc -q "$f" || true
popd
return 0
elif [ "$count" -gt 1 ]; then
find . -name "*.v" -type f | xargs coq_makefile -o Makefile
make || true
return 0
else
return 1
fi
}
trial_extract tar -xvf "../$fname"
trial_extract unzip "../$fname"
trial_extract_raw "file ../$qfname | grep -q text && cp ../$qfname downloaded_bug.v"
trial_extract_raw "test x$qextension == xtxt && cp ../$qfname downloaded_bug.v"
for kind in --bzip2 --xz --lzip --lzma --lzop --gunzip --uncompress --zstd; do
trial_extract tar $kind -xvf "../$fname"
done
if [ "$success" == "no" ]; then
>&2 printf "ERROR: Could not recognize %s as archive or .v file\n" "$fname"
>&2 file "$fname"
exit 1
fi
cd bug-output
try_autogen_configure_make || try_coq_makefile || try_coqc