TiledViz
Loading...
Searching...
No Matches
transfer.py
1# -*- coding: utf-8 -*-
2
3import os, sys
4import hashlib
5import time
6import stat
7import re
8
9Blocksize=65536
10
11def hashfile(blockfile):
12 for block in blockfile:
13 hashlib.sha256().update(block)
14 return hashlib.sha256().hexdigest()
15
16def readblock(filed):
17 with filed:
18 block = filed.read(Blocksize)
19 while len(block) > 0:
20 yield block
21 block = filed.read(Blocksize)
22
23def send_file_server(client,TileSet,pathin,filename,pathout):
24 filein=os.path.join(pathin,filename)
25 filesize = os.path.getsize(filein)
26 filed=open(filein, 'rb')
27 filesha256=hashfile(readblock(filed))
28 filed.close()
29 command='putfile TS='+TileSet+" "+pathout+" "+filename+" "+str(filesize)+" "+str(filesha256)
30 print("Command putfile : %s" % (command))
31 client.send_server(command)
32 print("Out of send command %s" % (str(client.get_OK())))
33 client.send_file(pathin,filename)
34 print("Out of send file %s" % (str(client.get_OK())))
35 # detect executable file
36 if oct((os.stat(filein).st_mode & stat.S_IRWXU+stat.S_IRWXG+stat.S_IRWXO) > stat.S_IRWXU) == oct(1):
37 command='launch TS='+TileSet+" "+pathout+" chmod u+x "+filename
38 client.send_server(command)
39 print("Chmod %s : %s" % (pathout, str(client.get_OK())))
40
41
42def get_file_client(client,TileSet,pathserv,filename,pathout):
43 command='askfile TS='+TileSet+" "+pathserv+" "+filename
44 print("Command askfile : %s" % (command))
45 client.send_server(command)
46 print("Out of ask command and file exists %s" % (str(client.get_OK())))
47
48 while True:
49 fileinfos=client.recv()
50 if not fileinfos or fileinfos == 0:
51 print("Error with fileinfos %s" % (str(fileinfos)))
52 return -1
53 else:
54 print("Retrieve fileinfos : %s" % (str(fileinfos)))
55 break
56
57 if (re.search(r'fileinfos',fileinfos)):
58 p=re.compile(r'fileinfos ([^ ]*) ([0-9]*) ([a-z0-9]*)')
59 CommandFilename=p.sub(r'\1',fileinfos)
60 CommandSize=int(p.sub(r'\2',fileinfos))
61 CommandSha256=p.sub(r'\3',fileinfos)
62
63
64 print("File transfer with filename %s with size %d and sha256 %s " % (CommandFilename,CommandSize,CommandSha256))
65
66 client.get_file(pathout,filename,CommandSize,CommandSha256)
67 print("Out of get file %s" % (str(client.get_OK())))
68 return(int(CommandSize))
69 else:
70 return -2
71
72