ajout de gestion de bdd pour...
|
1 |
#!/bin/bash |
2 | ||
3 |
fgaddon_url=https://sourceforge.net/p/flightgear/fgaddon/HEAD/tree/trunk/Aircraft |
|
4 |
fgaddon_svn=https://svn.code.sf.net/p/flightgear/fgaddon/trunk/Aircraft |
|
5 |
fgaddon_path=$HOME/.fgfs/flightgear-addons/Aircraft |
|
6 | ||
7 |
attribute_regex='s/^.*id="l[0-9]+" class="code_block">.*<span class="nt"><__attribute__><.span>(.+)<span class="nt"><.__attribute__><.span>.*$/\1/p' |
|
8 | ||
9 |
aircrafts=$(mktemp --dry-run /dev/shm/Aircraft-XXXXXXXXX) |
|
10 |
aircraft=$(mktemp --dry-run /dev/shm/aircraft-XXXXXXX) |
|
11 |
setxml=$(mktemp --dry-run /dev/shm/setxml-XXXXXXXX) |
|
12 |
in_ram_database=$(mktemp --dry-run /dev/shm/XXXXXXX) |
|
13 |
database=${DB:-$0.db} |
|
14 | ||
15 |
attributes=(description long_description author flight_model) |
|
16 | ||
17 |
function get_attribute () { |
|
18 |
eval "unset $1" |
|
19 |
result=$(sed -rn "${attribute_regex//__attribute__/${1//_/-}}" $setxml) |
|
20 |
eval "$1=\$\"$result\"" |
|
21 |
} |
|
22 | ||
23 |
function sqlite_request () { |
|
24 |
sqlite3 "$in_ram_database" <<< "$1" |
|
25 |
} |
|
26 | ||
27 |
function trap_exit () { |
|
many improvements
|
28 |
echo "updating installation status" |
29 |
for ac in $(sqlite_request 'select printf("%i:%s/%s", aircrafts.id, aircrafts.name, setxml.file) from aircrafts inner join setxml where aircrafts.id = setxml.variantof;'); do |
|
30 |
ac_path=${ac#*:} |
|
31 |
test ! -e $fgaddon_path/$ac_path |
|
32 |
installed=$? |
|
33 |
sqlite_request "update setxml set installed = $installed where file = '${ac_path#*/}' and variantof = ${ac%:*}" |
|
34 |
done |
|
ajout de gestion de bdd pour...
|
35 |
if test -r "$database" && md5sum $in_ram_database | sed "s,$in_ram_database,$database," | md5sum --status -c -; then |
36 |
rm -f $in_ram_database |
|
many improvements
|
37 |
echo "no changes in $database" |
ajout de gestion de bdd pour...
|
38 |
elif test -w "$database"; then |
39 |
mv -f $in_ram_database "$database" |
|
many improvements
|
40 |
echo "database $database updated" |
ajout de gestion de bdd pour...
|
41 |
elif ! test -e "$database"; then |
42 |
mv $in_ram_database "$database" |
|
many improvements
|
43 |
echo "database $database created" |
ajout de gestion de bdd pour...
|
44 |
else |
45 |
rm -f $in_ram_database |
|
many improvements
|
46 |
echo "nothing can be done with $database !" |
ajout de gestion de bdd pour...
|
47 |
fi |
48 |
rm -f $aircrafts $aircraft $setxml |
|
49 |
} |
|
50 | ||
51 |
update_database () { |
|
52 |
let progress++ |
|
53 |
echo "[ ${progress}${total:+/$total} ] $ac" |
|
54 | ||
many improvements
|
55 |
dbupdate=$(sqlite_request "select revision from aircrafts where name is '$ac'") |
ajout de gestion de bdd pour...
|
56 |
if test -z "$dbupdate"; then |
many improvements
|
57 |
sqlite_request "insert into aircrafts (name, revision, date, author) values ('$ac', $revision, $revdate, '$revauthor')" |
58 |
elif test $dbupdate -lt $revision; then |
|
59 |
sqlite_request "update aircrafts set revision = $revision, author = '$revauthor' where name is '$ac'" |
|
ajout de gestion de bdd pour...
|
60 |
fi |
61 |
id=$(sqlite_request "select id from aircrafts where name is '$ac'") |
|
62 | ||
63 |
wget -qO- $fgaddon_url/$ac > $aircraft |
|
64 |
for sx in $(sed -rn 's/<a class="icon" href="(.+-set.xml)" title=".+"><i class="fa fa-file-o">.+$/\1/p' $aircraft); do |
|
65 |
echo " -> ${sx/-set.xml}" |
|
66 |
wget -qO- $fgaddon_url/$ac/$sx > $setxml |
|
67 |
for attribute in ${attributes[@]}; do |
|
68 |
get_attribute $attribute |
|
69 |
done |
|
70 | ||
71 |
if test -n "$description" -a -z "$flight_model"; then |
|
72 |
grep -qi 'jsbsim' <<< "$description" && flight_model='jsb' |
|
73 |
grep -qi 'yasim' <<< "$description" && flight_model='yasim' |
|
74 |
fi |
|
75 | ||
76 |
known=$(sqlite_request "select variantof from setxml where file is '$sx'") |
|
77 |
if test -n "$known"; then |
|
78 |
for attribute in ${attributes[@]}; do |
|
79 |
dbvalue=$(sqlite_request "select $attribute from setxml where file is '$sx'") |
|
80 |
eval "wgvalue=\$$attribute" |
|
81 |
if test "$dbvalue" != "$wgvalue"; then |
|
82 |
sqlite_request "update setxml set $attribute = '$wgvalue' where file is '$sx'" |
|
83 |
fi |
|
84 |
done |
|
85 |
else |
|
many improvements
|
86 |
test ! -e $fgaddon_path/$ac/$sx |
87 |
installed=$? |
|
ajout de gestion de bdd pour...
|
88 |
sqlite_request "insert into setxml values ('$sx', $id, '$description', '$long_description', '$author', '$type', '$flight_model', $installed)" |
89 |
fi |
|
90 |
done |
|
91 |
} |
|
92 | ||
many improvements
|
93 |
xmlgetnext () { |
94 |
local IFS='>' |
|
95 |
read -d '<' TAG VALUE |
|
96 |
} |
|
97 | ||
ajout de gestion de bdd pour...
|
98 |
trap trap_exit EXIT |
99 | ||
100 |
test -e $database && cp $database $in_ram_database |
|
101 | ||
many improvements
|
102 |
sqlite_request "create table if not exists aircrafts (id integer primary key, name text, revision integer, date integer, author text)" |
ajout de gestion de bdd pour...
|
103 |
sqlite_request "create table if not exists setxml (file text, variantof integer, description text, long_description text, author text, type text, flight_model text, installed integer)" |
104 | ||
105 |
test -z "$1" && echo "mode RSS" |
|
106 | ||
107 |
case ${1:-rss} in |
|
108 |
'full') |
|
109 |
echo "downloading the FGADDON list" |
|
many improvements
|
110 |
wget -qO- $fgaddon_url | sed -n '/<table>/,/<\/table>/p' > $aircrafts |
ajout de gestion de bdd pour...
|
111 | |
112 |
total=$(grep -c '<a class="icon" href=".*class="fa fa-folder".*i> ' $aircrafts) |
|
113 | ||
many improvements
|
114 |
while xmlgetnext; do |
115 |
case "$TAG" in |
|
116 |
'tr') |
|
117 |
unset ac revision revauthor |
|
118 |
;; |
|
119 |
'a class="icon" href='*) |
|
120 |
eval $(echo "${TAG#* }") |
|
121 |
ac=$href |
|
122 |
;; |
|
123 |
'span title='*) |
|
124 |
eval $(echo "${TAG#* }") |
|
125 |
revdate=$(date +%s -d "$title") |
|
126 |
;; |
|
127 |
'a href="/p/flightgear/fgaddon/'*) |
|
128 |
eval $(echo "${TAG#* }") |
|
129 |
revision=$(tr -cd '[:digit:]' <<< $href) |
|
130 |
;; |
|
131 |
'span class="icon emboss x16" style="text-align:center;" title="User"'*) |
|
132 |
user=1 |
|
133 |
;; |
|
134 |
'/span') |
|
135 |
test -n "$user" && revauthor=$(tr -cd '[:alnum:]' <<< $VALUE) && unset user |
|
136 |
;; |
|
137 |
'/tr') |
|
138 |
test -n "$revision" -a -n "$revauthor" -a -n "$ac" -a -n "$revdate" && update_database |
|
139 |
;; |
|
140 |
esac |
|
141 |
done < $aircrafts |
|
ajout de gestion de bdd pour...
|
142 |
;; |
143 |
'rss') |
|
144 |
get_updates_from_rss () { |
|
many improvements
|
145 |
while xmlgetnext; do |
ajout de gestion de bdd pour...
|
146 |
case "$TAG" in |
147 |
'item') |
|
small fixes
|
148 |
let entry++ |
many improvements
|
149 |
unset link revdate revauthor revision title;; |
ajout de gestion de bdd pour...
|
150 |
'link') |
151 |
link="$VALUE";; |
|
152 |
'title') |
|
153 |
title="$VALUE";; |
|
many improvements
|
154 |
'dc:creator'*) |
155 |
revauthor="$VALUE";; |
|
ajout de gestion de bdd pour...
|
156 |
'guid isPermaLink="false"') |
many improvements
|
157 |
revision=$(sed -r 's,^.+/([0-9]+)/$,\1,' <<< $VALUE) |
158 |
if test $entry -eq 1 -a $revision -eq ${latest_revision:-0}; then |
|
small fixes
|
159 |
echo "no new updates" >&2 |
160 |
break |
|
161 |
fi |
|
162 |
;; |
|
many improvements
|
163 |
'pubDate') |
164 |
revdate=$(date +%s -d "$VALUE") |
|
165 |
;; |
|
ajout de gestion de bdd pour...
|
166 |
'/item') |
many improvements
|
167 |
echo "$revision ($(date +"%d %B" -d@$revdate), $revauthor): $title" >&2 |
168 |
wget -qO- "$link" | awk -v data="revision=$revision revdate=$revdate revauthor=$revauthor" ' |
|
169 |
/>\/trunk\/Aircraft\/.+</ { |
|
170 |
ac[gensub("^.+>/trunk/Aircraft/([^/]+).+<.*$", "ac=\\1", "1", $0)]++ |
|
171 |
} |
|
172 |
END { |
|
173 |
for (i in ac) print data " " i |
|
174 |
}' |
|
ajout de gestion de bdd pour...
|
175 |
;; |
176 |
'/channel') |
|
many improvements
|
177 |
if test ${latest_revision:-0} -lt $revision; then |
178 |
echo "WARNING: $(( ($revision - ${latest_revision:-0}) )) revisions between the oldest RSS entry and last DB entry" >&2 |
|
179 |
echo "a \`$0 full' may be useful" >&2 |
|
ajout de gestion de bdd pour...
|
180 |
fi |
181 |
;; |
|
182 |
esac |
|
many improvements
|
183 |
done < $aircrafts | awk ' |
184 |
{ |
|
185 |
if (a[$4] == "") a[$4]=$0 |
|
186 |
} |
|
187 |
END { |
|
188 |
for (i in a) { |
|
189 |
total++ |
|
190 |
command = command a[i] " update_database;" |
|
191 |
} |
|
192 |
printf("total=%i;%s", total, command) |
|
193 |
}' |
|
ajout de gestion de bdd pour...
|
194 |
} |
many improvements
|
195 |
latest_revision=$(sqlite_request "select max(revision) from aircrafts") |
196 |
wget -qO- https://sourceforge.net/p/flightgear/fgaddon/feed > $aircrafts |
|
197 |
eval "$(get_updates_from_rss)" |
|
ajout de gestion de bdd pour...
|
198 |
;; |
199 |
*) |
|
200 |
echo "usage: [DB=path/to/database] $0 [rss|full]" |
|
201 |
echo |
|
202 |
echo "rss: updates only last repo changes" |
|
203 |
echo "full: updates from all repo" |
|
many improvements
|
204 |
exit 1 |
ajout de gestion de bdd pour...
|
205 |
;; |
206 |
esac |