Files
@ 095908159393
Branch filter:
Location: Morevna/src/morevna.py
095908159393
2.8 KiB
text/x-python
handled connection error, skipped needless tree rebuild
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | import sys
import os.path
import logging as log
from argparse import ArgumentParser
from util import spawnDaemon
import config as conf
import stats
from hashtree import HashTree
from client import Client, Connection as ClientConnection
from server import Miniserver
def _checkFile(f):
if not os.path.isfile(f):
print("invalid file specified:",f,file=sys.stderr)
sys.exit(1)
def buildTree(args):
_checkFile(args.datafile)
if os.path.isfile(args.treefile):
treeMod=os.stat(args.treefile).st_mtime
dataMod=os.stat(args.datafile).st_mtime
if dataMod<treeMod and not args.force:
print("tree file is up to date")
return
tree=HashTree.fromFile(args.datafile)
tree.save(args.treefile)
def push(args):
_checkFile(args.datafile)
if args.tree:
_checkFile(args.tree)
if args.host: conf.hosts.insert(0,args.host)
if args.port: conf.port=args.port
c=Client(args.datafile,args.tree)
with ClientConnection() as con:
c.setConnection(con)
blocksToTransfer=c.negotiate()
c.sendData(blocksToTransfer)
print()
print(stats.report())
def pull(args):
_checkFile(args.datafile)
if args.tree:
_checkFile(args.tree)
if args.host: conf.hosts.insert(0,args.host)
if args.port: conf.port=args.port
c=Client(args.datafile,args.tree)
with ClientConnection() as con:
c.setConnection(con)
blocksToTransfer=c.negotiate()
c.pullData(blocksToTransfer)
print()
print(stats.report())
def serve(args):
_checkFile(args.datafile)
if args.tree:
_checkFile(args.tree)
if args.host: conf.hosts.insert(0,args.host)
if args.port: conf.port=args.port
s=Miniserver(args.datafile,args.tree)
try:
spawnDaemon(s.serve)
except Exception as e:
log.exception("exception: %s",e)
parser=ArgumentParser()
subparsers=parser.add_subparsers()
pBuild=subparsers.add_parser("build")
pBuild.add_argument("-f","--force",action="store_true",help="force tree rebuild")
pBuild.add_argument("treefile", help="stored hash tree location")
pBuild.add_argument("datafile")
pBuild.set_defaults(func=buildTree)
pUpdate=subparsers.add_parser("push")
pUpdate.add_argument("-p","--port",type=int)
pUpdate.add_argument("--host")
pUpdate.add_argument("-t","--tree",help="stored hash tree location")
pUpdate.add_argument("datafile")
pUpdate.set_defaults(func=push)
pUpdate=subparsers.add_parser("pull")
pUpdate.add_argument("-p","--port",type=int)
pUpdate.add_argument("--host",default="127.0.0.1")
pUpdate.add_argument("-t","--tree",help="stored hash tree location")
pUpdate.add_argument("datafile")
pUpdate.set_defaults(func=pull)
pServe=subparsers.add_parser("serve")
pServe.add_argument("-p","--port",type=int)
pServe.add_argument("--host")
pServe.add_argument("-t","--tree",help="stored hash tree location")
pServe.add_argument("datafile")
pServe.set_defaults(func=serve)
args=parser.parse_args()
args.func(args)
|