-
Notifications
You must be signed in to change notification settings - Fork 2
/
import.sh
executable file
·270 lines (241 loc) · 5.73 KB
/
import.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
#! /bin/sh
# Create SQL script import.sql to import facts from ipcc-fact-checking
# into a MySQL database. The SQL script shall run separately to perform
# the actual import.
#
# Usage:
# import.sh
#
# Config: size of database fields in characters
commitMaxSize=7
sourceMaxSize=30
documentMaxSize=35
datasetMaxSize=35
nameMaxSize=30
valueMaxSize=1100
echo "Change to parent directory of the script"
cd $(dirname $0)
echo "Current directory: $(pwd)"
# Reference:
# Update a submodule to the latest commit
# http://stackoverflow.com/a/8191413
echo "Remove untracked files from ipcc-fact-checking submodule"
cd ipcc-fact-checking
git clean -df
git checkout master
echo "Update ipcc-fact-checking to latest commit"
git pull origin master
cd ..
# Reference:
# "Get the short git version hash"
# http://stackoverflow.com/a/5694416
cd ipcc-fact-checking
commit=$(git rev-parse --short HEAD)
echo "Read short hash of last commit: $commit"
cd ..
echo "Create SQL script import.sql"
cat << EOF > import.sql
-- create database ipcc_facts
CREATE DATABASE IF NOT EXISTS ipcc_facts
DEFAULT CHARACTER SET utf8
;
USE ipcc_facts
#-- drop table (uncomment to apply new definition)
#DROP TABLE facts;
-- create table facts
CREATE TABLE IF NOT EXISTS facts (
commit CHAR($commitMaxSize) NOT NULL
COMMENT 'hash of latest commit in ipcc-fact-checking',
source VARCHAR($sourceMaxSize) NOT NULL
COMMENT 'first-level folder name in ipcc-fact-checking',
document VARCHAR($documentMaxSize) NOT NULL
COMMENT 'second-level folder name in ipcc-fact-checking',
dataset VARCHAR($datasetMaxSize) NOT NULL
COMMENT 'third-level folder name in ipcc-fact-checking',
line SMALLINT UNSIGNED NOT NULL
COMMENT 'line number in data.csv, or 0 for meta.txt',
name VARCHAR($nameMaxSize) NOT NULL
COMMENT 'column header in data.csv, property name in meta.txt',
value VARCHAR($valueMaxSize) NOT NULL
COMMENT 'field value in data.csv, property value in meta.txt',
PRIMARY KEY USING HASH (
commit, source, document, dataset, line, name
)
)
;
-- insert facts
EOF
logError()
{
echo "ERROR: $1"
}
checkSize()
{
if test "${#2}" -gt "$3"
then
logError "$1 '$2' has ${#2} characters > maximum expected: $3"
fi
}
identify()
{
echo "File: $1"
source=$(dirname "$1")
dataset=$(basename "$source")
source=$(dirname "$source")
document=$(basename "$source")
source=$(dirname "$source")
source=$(basename "$source")
# cat << EOF
#commit: $commit
#source: $source
#document: $document
#dataset: $dataset
#EOF
checkSize 'Commit' "$commit" $commitMaxSize
checkSize 'Source' "$source" $sourceMaxSize
checkSize 'Document' "$document" $documentMaxSize
checkSize 'Dataset' "$dataset" $datasetMaxSize
}
insertFact='INSERT INTO facts VALUES'
addFact()
{
checkSize 'Fact Name' "$name" $nameMaxSize
checkSize 'Fact Value' "$value" $valueMaxSize
echo \
"$insertFact (" \
"'$commit'," \
"'$source'," \
"'$document'," \
"'$dataset'," \
"$line," \
"'$name'," \
"\"$value\"" \
');' \
>> import.sql
}
parseMetaLine()
{
line=0
name=${1%%": "*}
value=${1#*": "}
addFact
}
parseMetaFile()
{
# Reference:
# Shell script read missing last line
# http://stackoverflow.com/a/12919766
# Read file line by line, including last line without EOF
while read -r metaLine || test -n "$metaLine"
do
case $metaLine in
*": "*) parseMetaLine "$metaLine";;
# stop on first empty line
"") break
esac
done < "$1"
}
# Reference:
# Split() is Not Always The Best Way to Split a String
# http://www.regexguru.com/2009/04/split-is-not-always-the-best-way-to-split-a-string/
discardEndOfDataLine()
{
# skip the rest of the line
headerFields=''
}
parseDataField()
{
case "$headerFields" in
# quoted field
\"*)
# discard initial quote character
headerFields=${headerFields#\"}
# field ends with ",
endOfField='\",'
;;
# unquoted field
*)
# field ends with ,
endOfField=','
esac
case "$headerFields" in
*$endOfField*)
name=${headerFields%%$endOfField*}
headerFields=${headerFields#$name$endOfField}
;;
*)
logError "End of field '$endOfField' not found in '$headerFields'"
discardEndOfDataLine
return
esac
case "$dataFields" in
# quoted field
\"*)
dataFields=${dataFields#\"}
endOfField='\",'
;;
# unquoted field
*)
endOfField=','
esac
case "$dataFields" in
*$endOfField*)
value=${dataFields%%$endOfField*}
dataFields=${dataFields#$value$endOfField}
;;
*)
logError "End of field '$endOfField' not found in '$dataFields'"
discardEndOfDataLine
return
esac
# skip empty values
if test -n "$value"
then
addFact
fi
}
parseDataLine()
{
dataFields="$1"
headerFields="$2"
until test -z "$headerFields"
do
parseDataField
done
}
parseDataFile()
{
line=0
# Read file line by line, including last line without EOF
while read -r dataLine || test -n "$dataLine"
do
line=$(($line + 1))
if test "$line" -eq 1
then
headers="$dataLine"
fi
case $dataLine in
# line with at list one value
# parse the data row and headers,
# with a final ',' added to simplify parsing
*[!,]*) parseDataLine "$dataLine," "$headers,";;
# only commas: no value
*) continue
esac
done < "$1"
}
echo "Gather facts from meta.txt files"
for meta in ipcc-fact-checking/*/*/*/meta.txt
do
identify "$meta"
parseMetaFile "$meta"
done
echo "Gather facts from data.csv files"
for data in ipcc-fact-checking/*/*/*/data.csv
do
identify "$data"
parseDataFile "$data"
done
echo ';' >> import.sql
echo "Facts from commit '$commit' are ready for import."
echo "You can now run import.sql to perform the actual import."