Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import os.path
- import shutil #for copying file
- import zlib #for adler32 function
- import hashlib #for md5 hash
- import adler32 #for adler32 checksum
- #function to split a file into chunks of size 100KB
- def splittochunk(place, line):
- fil = "~/Task2/"+place+"/"+line
- if (place==="source"):
- outfil = "schunk"
- else:
- outfil = "dchunk"
- f = open(fil,'r')
- chunksize = 102400
- for i in range(1,os.stat(fil).st_size/chunksize+1):
- o = open(outfil+str(i),'w')
- segment = f.readlines(chunksize)
- for c in range(0,len(segment)):
- o.write(segment[c])
- o.close()
- #function to generate md5hash
- def md5hash(place, no):
- if (place=="source"):
- fil = "schunk"+str(no)
- else:
- fil = "dchunk"+str(no)
- h = hashlib.md5()
- h.update(fil.read())
- return h.hexdigest()
- #function to generate adler32 checksum
- def adler32(place, no):
- if (place=="source"):
- fil = "schunk"+str(no)
- else:
- fil = "dchunk"+str(no)
- adl=adler32.new() # create new adler32 object
- adl.update(fil.read()) # update adler32 object with contents of file
- adl.rotate(byteout,bytein) # rotate byteout out and bytein into adler32
- return adl.hexdigest()
- path = "~/Task2/source/"
- dirs = os.listdir( path ) #gives list of files in given path
- for line in dirs:
- file_path="~/Task2/destination/"+line
- if not os.path.exists(file_path) :
- copyfile("~/Task2/source/"+line,"~/Task2/destination/") # if such a file does nont exists in dest, copy it.
- else:
- splittochunk("source",line) #split source file into chunks
- splittochunk("destination",line) #split destination file into chunks
- for j in range(1,os.stat(fil).st_size/chunksize+1):
- if (adler32("source", j)!=adler32("destination", j)): #compare rolling checksum of chunks
- #replace that j chunk
- f = open("schunk"+str(j), "r")
- g = open("dchunk"+str(j), "w")
- g.write(f.read())
- f.close()
- g.close()
- h = open("~/Task2/destination/"+line, "w")
- for j in range(1,os.stat(fil).st_size/chunksize+1): #to merge altered dchunks
- tempfile="dchunk"+str(j)
- h.write(tempfile.read())
- os.remove("dchunk"+str(j), *, dir_fd=None)
- os.remove("schunk"+str(j), *, dir_fd=None)
- path = "~/Task2/source/"
- dirs = os.listdir( path ) #gives list of files in given path
- for line in dirs:
- file_path="~/Task2/destination/"+line
- if os.path.exists(file_path) :
- splittochunk("source",line) #split source file into chunks
- splittochunk("destination",line) #split destination file into chunks
- for j in range(1,os.stat(fil).st_size/chunksize+1):
- if (md5hash("source", j)!=md5hash("destination", j)): #compare md5hashes of chunks
- print "Sync Failed"
- else:
- print "Sync Success"
- else:
- print "Sync Failed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement