Changeset - 3ba409cd6541
[Not reviewed]
default
0 1 0
Laman - 9 years ago 2016-08-10 21:20:13

rebuild command to get the original file
1 file changed with 27 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/work.py
Show inline comments
 
@@ -51,13 +51,38 @@ class Pipeline:
 
		return hashlib.sha256(chunk).digest()[:HASH_LEN]
 

	
 
	def copyChunk(self,chunk,i):
 
		path=pathlib.Path(TARGET_DIR).joinpath(Pipeline.getPath(("{0:0"+str(len(str(FILE_COUNT)))+"}").format(i)))
 
		path=self.getChunkPath(i)
 
		if not path.parent.exists(): path.parent.mkdir(parents=True) # !! no fail? existing file named as a dir
 
		log.debug("writing file %s", path)
 
		f=path.open(mode="wb")
 
		f.write(chunk)
 
		f.close()
 

	
 
	def mergeChunks(self):
 
		log.info("starting merge")
 
		try: targetFile=open(self.fileName,mode="xb")
 
		except FileExistsError:
 
			log.warning("file %s already exists, aborting",self.fileName)
 
			return False # !! be more specific
 

	
 
		i=0
 
		while True:
 
			try:
 
				sourcePath=self.getChunkPath(i)
 
				sourceFile=sourcePath.open(mode="rb")
 
				targetFile.write(sourceFile.read())
 
				sourceFile.close()
 
				log.info("file %s merged",sourcePath)
 
			except FileNotFoundError: break
 
			i+=1
 
		targetFile.close()
 
		log.info("merge done")
 
		return True
 

	
 
	def getChunkPath(self,i):
 
		paddedName=("{0:0"+str(len(str(FILE_COUNT)))+"}").format(i)
 
		return pathlib.Path(TARGET_DIR).joinpath(Pipeline.getPath(paddedName))
 

	
 
	@staticmethod
 
	def getPath(name):
 
		dirs=[name]
 
@@ -177,5 +202,6 @@ pipeline=Pipeline(fileName)
 
if action=="setup": pipeline.setup()
 
elif action=="update": pipeline.update()
 
#elif action=="transfer": transferChanges(sys.argv[3])
 
elif action=="rebuild": pipeline.mergeChunks()
 
else: print("bad action")
 

	
0 comments (0 inline, 0 general)