ff_merge_nodes_json 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python
  2. #
  3. # Maximilian Wilhelm <max@rfc2324.org>
  4. # -- Tue 20 Jun 2017 06:40:18 PM CEST
  5. #
  6. import argparse
  7. import json
  8. import os
  9. import sys
  10. import time
  11. parser = argparse.ArgumentParser (description = 'Merge nodes.json files')
  12. parser.add_argument ('files', help = 'Path for nodes.json file(s)', nargs = '+')
  13. parser.add_argument ('--pretty-print', help = 'Pretty-print JSON output', action = 'store_true')
  14. args = parser.parse_args ()
  15. all_nodes = {}
  16. uberdict = {}
  17. # Read all nodes lists into all_nodes dict, thereby dropping any duplicate nodes.
  18. for file_path in args.files:
  19. try:
  20. with open (file_path, 'rb') as fh:
  21. nodes = json.load (fh)
  22. except IOError as (errno, strerror):
  23. print "Error while reading file '%s': %s" % (file_path, strerror)
  24. sys.exit (1)
  25. for node in nodes['nodes']:
  26. node_id = node['nodeinfo']['node_id']
  27. # If node_id has already been seen make sure to use the newer entry
  28. if node_id in all_nodes:
  29. try:
  30. node_lastseen = time.strptime (node['lastseen'], "%Y-%m-%dT%H:%M:%S%z")
  31. existing_node_lastseen = time.strptime (existing_node_lastseen['lastseen'], "%Y-%m-%dT%H:%M:%S%z")
  32. # If the node information already stored in all_nodes is more
  33. # recent than the node we just found, don't overwrite it.
  34. if existing_node_lastseen > node_lastseen:
  35. continue
  36. except Exception:
  37. # If parsing a timestamp fails just carry on
  38. continue
  39. all_nodes[node['nodeinfo']['node_id']] = node
  40. for key in nodes.keys ():
  41. if key != 'nodes':
  42. uberdict[key] = nodes[key]
  43. uberdict['nodes'] = all_nodes.values ()
  44. # Print merged nodes.json's to stdout
  45. if args.pretty_print:
  46. print (json.dumps (uberdict, sort_keys = True, indent = 4, separators = (',', ': ')))
  47. else:
  48. print (json.dumps (uberdict))