-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_log.py
More file actions
63 lines (52 loc) · 2.13 KB
/
Copy pathcommit_log.py
File metadata and controls
63 lines (52 loc) · 2.13 KB
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
from datetime import datetime
from threading import Lock
import os, tqdm
class CommitLog:
def __init__(self, file='commit-log.txt'):
self.file = file
self.lock = Lock()
def truncate(self):
with self.lock:
with open(self.file, 'w') as f:
f.truncate()
def log(self, command, sep=" "):
with self.lock:
with open(self.file, 'a') as f:
now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
message = now + "," + command
f.write(f"{message}\n")
def read_log(self):
with self.lock:
output = []
with open(self.file, 'r') as f:
for line in f:
_, command = line.strip().split(",")
output += [command]
return output
def write_log_from_sock(self, sock):
with self.lock:
file_name = self.file
file_size = os.path.getsize(file_name)
BUFFER_SIZE = 4096
sock.send("commitlog".encode())
progress = tqdm.tqdm(range(file_size), f"Receiving {file_name}", unit="B", unit_scale=True, unit_divisor=1024)
with open(self.file, 'ab') as f:
while True:
bytes_read = sock.recv(BUFFER_SIZE)
if not bytes_read:
break
f.write(bytes_read)
progress.update(len(bytes_read))
def send_log_to_sock(self, sock):
with self.lock:
file_name = self.file
file_size = os.path.getsize(file_name)
BUFFER_SIZE = 4096
progress = tqdm.tqdm(range(file_size), f"Sending {file_name}", unit="B", unit_scale=True, unit_divisor=1024)
with open(file_name, "rb") as f:
while True:
bytes_read = f.read(BUFFER_SIZE)
if not bytes_read:
break
sock.sendall(bytes_read)
progress.update(len(bytes_read))