Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import os
  2. import os.path
  3. import shutil #for copying file
  4. import zlib #for adler32 function
  5. import hashlib #for md5 hash
  6. import adler32 #for adler32 checksum
  7.  
  8. #function to split a file into chunks of size 100KB
  9. def splittochunk(place, line):
  10. fil = "~/Task2/"+place+"/"+line
  11. if (place==="source"):
  12. outfil = "schunk"
  13. else:
  14. outfil = "dchunk"
  15.  
  16. f = open(fil,'r')
  17. chunksize = 102400
  18.  
  19. for i in range(1,os.stat(fil).st_size/chunksize+1):
  20. o = open(outfil+str(i),'w')
  21. segment = f.readlines(chunksize)
  22. for c in range(0,len(segment)):
  23. o.write(segment[c])
  24. o.close()
  25.  
  26. #function to generate md5hash
  27. def md5hash(place, no):
  28. if (place=="source"):
  29. fil = "schunk"+str(no)
  30. else:
  31. fil = "dchunk"+str(no)
  32.  
  33. h = hashlib.md5()
  34. h.update(fil.read())
  35. return h.hexdigest()
  36.  
  37. #function to generate adler32 checksum
  38. def adler32(place, no):
  39. if (place=="source"):
  40. fil = "schunk"+str(no)
  41. else:
  42. fil = "dchunk"+str(no)
  43.  
  44. adl=adler32.new() # create new adler32 object
  45. adl.update(fil.read()) # update adler32 object with contents of file
  46. adl.rotate(byteout,bytein) # rotate byteout out and bytein into adler32
  47. return adl.hexdigest()
  48.  
  49. path = "~/Task2/source/"
  50. dirs = os.listdir( path ) #gives list of files in given path
  51.  
  52.  
  53. for line in dirs:
  54. file_path="~/Task2/destination/"+line
  55. if not os.path.exists(file_path) :
  56. copyfile("~/Task2/source/"+line,"~/Task2/destination/") # if such a file does nont exists in dest, copy it.
  57. else:
  58. splittochunk("source",line) #split source file into chunks
  59. splittochunk("destination",line) #split destination file into chunks
  60. for j in range(1,os.stat(fil).st_size/chunksize+1):
  61. if (adler32("source", j)!=adler32("destination", j)): #compare rolling checksum of chunks
  62. #replace that j chunk
  63. f = open("schunk"+str(j), "r")
  64. g = open("dchunk"+str(j), "w")
  65. g.write(f.read())
  66. f.close()
  67. g.close()
  68. h = open("~/Task2/destination/"+line, "w")
  69. for j in range(1,os.stat(fil).st_size/chunksize+1): #to merge altered dchunks
  70. tempfile="dchunk"+str(j)
  71. h.write(tempfile.read())
  72. os.remove("dchunk"+str(j), *, dir_fd=None)
  73. os.remove("schunk"+str(j), *, dir_fd=None)
  74.  
  75.  
  76. path = "~/Task2/source/"
  77. dirs = os.listdir( path ) #gives list of files in given path
  78. for line in dirs:
  79. file_path="~/Task2/destination/"+line
  80. if os.path.exists(file_path) :
  81. splittochunk("source",line) #split source file into chunks
  82. splittochunk("destination",line) #split destination file into chunks
  83. for j in range(1,os.stat(fil).st_size/chunksize+1):
  84. if (md5hash("source", j)!=md5hash("destination", j)): #compare md5hashes of chunks
  85. print "Sync Failed"
  86. else:
  87. print "Sync Success"
  88. else:
  89. print "Sync Failed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement