# HG changeset patch # User Laman # Date 2016-08-10 21:20:13 # Node ID 3ba409cd6541a61be60b9dad961996a0d7ce5ae5 # Parent 95bfc9d721330c8715195f907cc8d249efd9079d rebuild command to get the original file diff --git a/src/work.py b/src/work.py --- a/src/work.py +++ b/src/work.py @@ -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")