forked from sed-szeged/FobSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodification.py
More file actions
81 lines (66 loc) · 2 KB
/
modification.py
File metadata and controls
81 lines (66 loc) · 2 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import random
import json
import os
import shutil
import time
import multiprocessing
keychain = multiprocessing.Queue()
def get_key():
while not keychain.empty():
keychain.get()
def return_key():
keychain.put('KEY')
def initiate_files(gossip_activated):
for filename in os.listdir('temporary'):
file_path = os.path.join('temporary', filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
write_file('temporary/confirmation_log.json', {})
write_file('temporary/miner_wallets_log.json', {})
if gossip_activated:
write_file('temporary/longest_chain.json', {'chain': {}, 'from': 'Miner_1'})
def read_file(file_path):
while keychain.empty():
time.sleep(0.1)
get_key()
while True:
try:
with open(file_path, 'r') as f:
file = json.load(f)
return_key()
return file
except Exception as e:
pass
def write_file(file_path, contents):
while keychain.empty():
time.sleep(0.1)
get_key()
while True:
try:
with open(file_path, 'w') as f:
json.dump(contents, f, indent=4)
return_key()
break
except Exception as e:
pass
def rewrite_file(file_path, new_version):
while keychain.empty():
time.sleep(0.1)
get_key()
while True:
try:
os.remove(file_path)
except Exception as e:
try:
with open(file_path, "w") as f:
json.dump(new_version, f, indent=4)
return_key()
break
except Exception as e:
pass
return_key()