Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import hashlib
  5. from app_factory import app
  6. from blueprints import register_all_blueprints
  7. #!/usr/bin/env python
  8. import os
  9. import hashlib
  10.  
  11. # !/usr/bin/env python
  12.  
  13. import os
  14. import hashlib
  15.  
  16.  
  17. class MarkleTree:
  18. def __init__(self, root):
  19. self._linelength = 30
  20. self._root = root
  21. self._mt = {}
  22. self._hashlist = {}
  23. self._tophash = ''
  24. self.__MT__()
  25.  
  26. def Line(self):
  27. print self._linelength * '-'
  28.  
  29. def PrintHashList(self):
  30. self.Line()
  31. for item, itemhash in self._hashlist.iteritems():
  32. print "%s %s" % (itemhash, item)
  33. self.Line()
  34. return
  35.  
  36. def PrintMT(self, hash):
  37. value = self._mt[hash]
  38. item = value[0]
  39. child = value[1]
  40. print "%s %s" % (hash, item)
  41. if not child:
  42. return
  43. for itemhash, item in child.iteritems():
  44. print " -> %s %s" % (itemhash, item)
  45. for itemhash, item in child.iteritems():
  46. self.PrintMT(itemhash)
  47.  
  48. def MT(self):
  49. for node, hash in self._hashlist.iteritems():
  50. items = self.GetItems(node)
  51. value = []
  52. value.append(node)
  53. list = {}
  54. for item in items:
  55. if node == self._root:
  56. list[self._hashlist[item]] = item
  57. else:
  58. list[self._hashlist[os.path.join(node, item)]] = os.path.join(node, item)
  59. value.append(list)
  60. self._mt[hash] = value
  61. self._tophash = self._hashlist[self._root]
  62.  
  63. def __MT__(self):
  64. self.HashList(self._root)
  65. # self.PrintHashList()
  66. self.MT()
  67. print "Merkle Tree for %s: " % self._root
  68. self.PrintMT(self._tophash)
  69. self.Line()
  70.  
  71. def md5sum(self, data):
  72. m = hashlib.md5()
  73. fn = os.path.join(self._root, data)
  74. if os.path.isfile(fn):
  75. try:
  76. f = file(fn, 'rb')
  77. except:
  78. return 'ERROR: unable to open %s' % fn
  79. while True:
  80. d = f.read(8096)
  81. if not d:
  82. break
  83. m.update(d)
  84. f.close()
  85. else:
  86. m.update(data)
  87. return m.hexdigest()
  88.  
  89. def GetItems(self, directory):
  90. value = []
  91. if directory != self._root:
  92. directory = os.path.join(self._root, directory)
  93. if os.path.isdir(directory):
  94. items = os.listdir(directory)
  95. for item in items:
  96. value.append(item)
  97. # value.append(os.path.join(".", item))
  98. value.sort()
  99. return value
  100.  
  101. def HashList(self, rootdir):
  102. self.HashListChild(rootdir)
  103. items = self.GetItems(rootdir)
  104. if not items:
  105. self._hashlist[rootdir] = ''
  106. return
  107. s = ''
  108. for subitem in items:
  109. s = s + self._hashlist[subitem]
  110. self._hashlist[rootdir] = self.md5sum(s)
  111.  
  112. def HashListChild(self, rootdir):
  113. items = self.GetItems(rootdir)
  114. if not items:
  115. self._hashlist[rootdir] = ''
  116. return
  117. for item in items:
  118. itemname = os.path.join(rootdir, item)
  119. if os.path.isdir(itemname):
  120. self.HashListChild(item)
  121. subitems = self.GetItems(item)
  122. s = ''
  123. for subitem in subitems:
  124. s = s + self._hashlist[os.path.join(item, subitem)]
  125. if rootdir == self._root:
  126. self._hashlist[item] = self.md5sum(s)
  127. else:
  128. self._hashlist[itemname] = self.md5sum(s)
  129. else:
  130. if rootdir == self._root:
  131. self._hashlist[item] = self.md5sum(item)
  132. else:
  133. self._hashlist[itemname] = self.md5sum(itemname)
  134.  
  135.  
  136. def MTDiff(mt_a, a_tophash, mt_b, b_tophash):
  137. if a_tophash == b_tophash:
  138. print "Top hash is equal for %s and %s" % (mt_a._root, mt_b._root)
  139. else:
  140. a_value = mt_a._mt[a_tophash]
  141. a_child = a_value[1] # retrive the child list for merkle tree a
  142. b_value = mt_b._mt[b_tophash]
  143. b_child = b_value[1] # retrive the child list for merkle tree b
  144.  
  145. for itemhash, item in a_child.iteritems():
  146. try:
  147. if b_child[itemhash] == item:
  148. print "Info: SAME : %s" % item
  149. except:
  150. print "Info: DIFFERENT : %s" % item
  151. temp_value = mt_a._mt[itemhash]
  152. if len(temp_value[1]) > 0: # check if this is a directory
  153. diffhash = list(set(b_child.keys()) - set(a_child.keys()))
  154. MTDiff(mt_a, itemhash, mt_b, diffhash[0])
  155.  
  156. register_all_blueprints(app)
  157.  
  158. if __name__ == "__main__":
  159. mt_a = MarkleTree('old_merkle_tree')
  160. print mt_a._mt
  161. mt_b = MarkleTree('new_merkle_tree')
  162. MTDiff(mt_a, mt_a._tophash, mt_b, mt_b._tophash)
  163. app.run(debug=True, threaded=True, port=4001)
  164.  
  165. #mt_b = MarkleTree('testB')
  166. #MTDiff(mt_a, mt_a._tophash, mt_b, mt_b._tophash)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement