#!/usr/bin/env python

"""
tool that reads jsons files, normalizes (sorts keys, indents) then prints the md5
"""
import argparse
from glob import glob
import json
from md5 import md5

def check_fp(f):
	docs = map(json.loads, f)
	combined = json.dumps(docs, sort_keys=True, indent=2)
	return md5(combined).hexdigest()

def check_fn(filename):
	with open(filename) as f:
		return check_fp(f)

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description='Compute md5 of normalized jsons file')
	parser.add_argument("-p", "--pattern", type=str, help="glob pattern of jsons files", default="*jsons")
	args = parser.parse_args()
	files = sorted(glob(args.pattern))
	for fn in files:
		print("%s\t%s" % (fn, check_fn(fn)))
@@ -175,16 +175,39 @@
 port md5
+%0Afrom sys import stdout
 %0A%0Adef ch
@@ -407,16 +407,153 @@
 _fp(f)%0A%0A
+def check_filenames(pattern, out=stdout):%0A%09files = sorted(glob(pattern))%0A%09for fn in files:%0A%09%09out.write(%22%25s%5Ct%25s%5Cn%22 %25 (fn, check_fn(fn)))%0A%0A
 if __nam
@@ -790,35 +790,31 @@
 args()%0A%09
-files = sorted(glob
+check_filenames
 (args.pa
@@ -823,62 +823,6 @@
 ern)
-)
 %0A%09
-for fn in files:%0A%09%09print(%22%25s%5Ct%25s%22 %25 (fn, check_fn(fn)))

refactor jsonsmd5