# HG changeset patch # User Laman # Date 2017-05-08 13:16:05 # Node ID 13d0327a4abb0d8d2dba67d2d4d5bcdc0630f8a7 # Parent 8b0dc65400f376ac4c28b957f92b218809ee5650 rudimentary progress info diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -1,7 +1,6 @@ from hashtree import HashTree import collections import socket -import sys import logging as log import config as conf diff --git a/src/hashtree.py b/src/hashtree.py --- a/src/hashtree.py +++ b/src/hashtree.py @@ -1,6 +1,8 @@ import hashlib import os +from util import progress + class HashTree: HASH_LEN=16 # bytes @@ -20,10 +22,13 @@ class HashTree: size=stat.st_size # !! symlinks leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks res=cls(leafCount) + print("hashing file:") for i in range(leafCount): data=f.read(HashTree.BLOCK_SIZE) res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:]) + + progress(i, leafCount) res.buildTree() return res @@ -68,8 +73,10 @@ class HashTree: ## Fast construction of the tree over the leaves. O(n). def buildTree(self): + print("building tree:") for i in range(self.leafStart-1,-1,-1): self.store[i]=hashlib.sha256(self.store[i*2+1]+self.store[i*2+2]).digest()[HashTree.HASH_LEN:] + progress(i, -1, self.leafStart - 1) if __name__=="__main__": diff --git a/src/morevna.py b/src/morevna.py --- a/src/morevna.py +++ b/src/morevna.py @@ -44,10 +44,10 @@ def serve(args): parser=ArgumentParser() subparsers=parser.add_subparsers() -pRebuild=subparsers.add_parser("build") -pRebuild.add_argument("treefile",help="stored hash tree location") -pRebuild.add_argument("datafile") -pRebuild.set_defaults(func=buildTree) +pBuild=subparsers.add_parser("build") +pBuild.add_argument("treefile", help="stored hash tree location") +pBuild.add_argument("datafile") +pBuild.set_defaults(func=buildTree) pUpdate=subparsers.add_parser("update") pUpdate.add_argument("-p","--port",type=int) diff --git a/src/util.py b/src/util.py new file mode 100644 --- /dev/null +++ b/src/util.py @@ -0,0 +1,13 @@ +def progress(i, n, i0=0): + def _progress(i,n,i0): + return 100*(i+1-i0)//(n-i0) + + if n_progress(i-1,n,i0): + print(r"{0}%".format(percentage),end="") + if percentage==100: print()