Package changes to support the aircraft DB.

This commit is contained in:
Oliver Jowett 2015-02-25 13:16:54 +00:00
parent 8209267301
commit 60eab2284f
11 changed files with 172 additions and 23 deletions

109
tools/update-aircraft-database.sh Executable file
View file

@ -0,0 +1,109 @@
#!/bin/bash
# This script checks for a new version of BasicAircraftLookup from
# the VRS website and, if one is available, downloads it and updates
# the dump1090 webmap json files.
set -e
# defaults that can be overridden:
VRS_URL=${VRS_URL:-http://www.virtualradarserver.co.uk/Files/BasicAircraftLookup.sqb.gz}
CACHEDIR=${CACHEDIR:-/var/cache/dump1090-mutability}
JSONDIR=${JSONDIR:-$CACHEDIR/db}
SQBDIR=${SQBDIR:-$CACHEDIR/sqb}
LOGFILE=${LOGFILE:-/var/log/dump1090-mutability.log}
UPDATESCRIPT=${UPDATESCRIPT:-/usr/share/dump1090-mutability/vrs-basicaircraft-to-json.py}
if [ -f /etc/default/dump1090-mutability ]
then
. /etc/default/dump1090-mutability
fi
ETAGFILE=$SQBDIR/BasicAircraftLookup.sqb.etag
SQBFILE=$SQBDIR/BasicAircraftLookup.sqb
CHECKMODIFIED=true
LOGTOFILE=false
while [ "$#" -gt 0 ]
do
case "$1" in
-f|--force) CHECKMODIFIED=false ;;
-l|--log-to-file) LOGTOFILE=true ;;
*) echo "unrecognized option: $1" >&2; exit 1 ;;
esac
shift
done
if $LOGTOFILE; then exec >>$LOGFILE 2>&1; fi
log() {
date "+%c $*" >&2
}
mkdir -p $CACHEDIR $JSONDIR $SQBDIR
rm -f $ETAGFILE.new $SQBFILE.new
log "Checking VRS server for an updated database.."
# get ETag
curl --silent --fail --include --head $VRS_URL | grep ETag >$ETAGFILE.new
# check for existing file
RETRIEVE=true
ARGS=""
if $CHECKMODIFIED && [ -f $SQBFILE ]
then
if [ -s $ETAGFILE -a -s $ETAGFILE.new ]
then
if cmp -s $ETAGFILE $ETAGFILE.new
then
log "Database not modified."
RETRIEVE=false
else
log "Database modified, will retrieve a new copy."
fi
else
# do an if-modified-since
log "Database possibly modified, will try to retrieve a new copy."
ARGS="-z $SQBFILE"
fi
fi
if $RETRIEVE
then
log "Retrieving database.."
curl --silent --fail --remote-time --retry 2 $ARGS -o $SQBFILE.new $VRS_URL
mv $ETAGFILE.new $ETAGFILE
if [ -f $SQBFILE.new ]
then
log "Decompressing database.."
zcat $SQBFILE.new >$SQBFILE
touch -r $SQBFILE.new $SQBFILE
rm $SQBFILE.new
else
log "Database not modified."
fi
fi
UPDATE=true
if $CHECKMODIFIED
then
if test -f $JSONDIR/last_update; then
if ! test $SQBFILE -nt $JSONDIR/last_update; then UPDATE=false; fi
fi
fi
if $UPDATE
then
log "Updating JSON files from database.."
mkdir -p $JSONDIR/new
$UPDATESCRIPT $SQBFILE $JSONDIR/new
rm -f $JSONDIR/*.json
mv $JSONDIR/new/*.json $JSONDIR/
touch -r $SQBFILE $JSONDIR/last_update
rmdir $JSONDIR/new
log "Done."
else
log "No update to JSON files needed."
fi

View file

@ -8,7 +8,7 @@
import sqlite3, json
from contextlib import closing
def extract(dbfile, todir, blocklimit):
def extract(dbfile, todir, blocklimit, debug):
ac_count = 0
block_count = 0
@ -35,7 +35,7 @@ def extract(dbfile, todir, blocklimit):
blockdata = blocks[bkey]
if len(blockdata) > blocklimit:
print 'Splitting block', bkey, 'with', len(blockdata), 'entries..',
if debug: print 'Splitting block', bkey, 'with', len(blockdata), 'entries..',
# split all children out
children = {}
@ -62,7 +62,7 @@ def extract(dbfile, todir, blocklimit):
retained += 1
del children[0]
print len(children), 'children created,', len(blockdata), 'entries retained in parent'
if debug: print len(children), 'children created,', len(blockdata), 'entries retained in parent'
blockdata['children'] = sorted([x[0] for x in children])
blocks[bkey] = blockdata
for c_bkey, c_entries in children:
@ -70,7 +70,7 @@ def extract(dbfile, todir, blocklimit):
queue.append(c_bkey)
path = todir + '/' + bkey + '.json'
print 'Writing', len(blockdata), 'entries to', path
if debug: print 'Writing', len(blockdata), 'entries to', path
block_count += 1
with closing(open(path, 'w')) as f:
json.dump(obj=blockdata, fp=f, check_circular=False, separators=(',',':'), sort_keys=True)
@ -83,5 +83,5 @@ if __name__ == '__main__':
print 'Syntax: %s <path to BasicAircraftLookup.sqb> <path to DB dir>' % sys.argv[0]
sys.exit(1)
else:
extract(sys.argv[1], sys.argv[2], 1000)
extract(sys.argv[1], sys.argv[2], 1000, False)
sys.exit(0)