summaryrefslogtreecommitdiff
path: root/tools/confirm.py
blob: 94e066a41a1ef5cf07cefe816a14d2a549751d3c (plain)
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
#!/usr/bin/python

# This script confirms what ledger tells you.

import sys
import os
import re

def clean(num):
    num = re.sub("(\s+|\$|,)","", num)
    m = re.search("([-0-9.]+)", num)
    if m:
        return float(m.group(1))
    else:
        return float(num)

running_total = 0.0
index = 1
last_line = ""
errors = 0

args = sys.argv[1]
for line in os.popen(re.sub('\$cmd', 'reg', args)):
    match = re.match("\\s*([-$,0-9.]+)\\s+([-$,0-9.]+)", line[54:])
    if not match:
        continue
    value = clean(match.group(1))
    total = clean(match.group(2))

    running_total += value
    diff = abs(running_total - total)
    if re.search(' -[VGB] ', args) and diff < 0.015:
        diff = 0.0
    if diff > 0.001:
        print "DISCREPANCY: %.3f (%.3f - %.3f) at line %d:" % \
              (running_total - total, running_total, total, index)
        print line,
        running_total = total
        errors += 1

    index += 1
    last_line = line

balance_total = 0.0

for line in os.popen(re.sub('\$cmd', 'bal', args)):
    if line[0] != '-':
        balance_total = clean(line[:20])

diff = abs(balance_total - running_total)
if re.search(' -[VGB] ', args) and diff < 0.015:
    diff = 0.0
if diff > 0.001:
    print
    print "DISCREPANCY: %.3f (%.3f - %.3f) between register and balance" % \
              (balance_total - running_total, balance_total, running_total)
    print last_line,
    errors += 1

sys.exit(errors)