From ad8a9d03993ade25f84a6ce0a1be6d7303fedc65 Mon Sep 17 00:00:00 2001 From: EUcancER Date: Wed, 7 Dec 2011 13:02:54 +0100 Subject: //assets/bin: add truth2json,check-truth truth2json converts the truth-db into json, fails hard when the database is malformed check-truth takes a line to add to the truth from stdin and calls truth2json to check the sanity of the database-to-be --- assets/bin/check-truth | 11 +++++++++++ assets/bin/truth2json | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100755 assets/bin/check-truth create mode 100755 assets/bin/truth2json (limited to 'assets') diff --git a/assets/bin/check-truth b/assets/bin/check-truth new file mode 100755 index 00000000..88246a54 --- /dev/null +++ b/assets/bin/check-truth @@ -0,0 +1,11 @@ +#!/bin/sh +set -euf +DB=${1-"/krebs/db/truth"} +HERE=$(dirname $(readlink -f $0)) +read LINE +if (cat $DB;echo $LINE) | $HERE/truth2json - >/dev/null ;then + echo "success" +else + echo "you fail" +fi + diff --git a/assets/bin/truth2json b/assets/bin/truth2json new file mode 100755 index 00000000..c1aac73c --- /dev/null +++ b/assets/bin/truth2json @@ -0,0 +1,37 @@ +#!/usr/bin/python + +import sys + +try: + db=sys.argv[1] +except: + db="/krebs/db/truth" + +ret = {} + + +if db is "-": + sys.stderr.write("Using stdin\n") + f = sys.stdin +else: + sys.stderr.write("Using journal '%s'\n" % db) + f = open(db) + +for line in f: + lsplit = line.split() + date = ' '.join(lsplit[0:2]) + cmd = lsplit[2] + target = lsplit[3] + if cmd == "create": + assert target not in ret, "Target '%s' already created!" + ret[target] = {} + elif cmd == "set": + key = lsplit[4] + value = ' '.join(lsplit[5:]) + assert target in ret, "target '%s' not set yet!" % target + ret[target][key] = value + else: + raise AssertionError,"unknown command '%s'!" + +import json +print json.dumps(ret,sort_keys=True,indent=4) -- cgit v1.2.3