-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfsapp.py
281 lines (215 loc) · 8.66 KB
/
ipfsapp.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import codecs
import glob
import pickle as pkl
import ipfshttpclient
import json
import numpy as np
import os
import paramiko
import re
import requests
import time
class Action:
def __init__(self, sourcePaths):
self.__sourcePaths = sourcePaths
self.__timer = Timer()
def execute(self, fileRemover, runs=1):
performanceMeasures = np.zeros((len(self.__sourcePaths), runs))
fileIDs = ["" for i in range(len(self.__sourcePaths))]
for i in range(runs):
fileRemover.execute()
for j, path in enumerate(self.__sourcePaths):
self.__timer.start()
self.logic(fileIDs, j, path)
performanceMeasures[j][i] = self.__timer.stop()
return performanceMeasures, fileIDs
def logic(self, fileIDs, j, path):
raise NotImplementedError("Must override method")
class HTTPDownload(Action):
def __init__(self, sourceDirectory, targetDirectory, removeFiles=True):
super().__init__(self.__loadSourcePaths(sourceDirectory))
self.__downloadDirectory = targetDirectory
self.__fileRemover = FileRemoverOS(self.__downloadDirectory, ".txt", removeFiles)
def __loadSourcePaths(self, sourceDirectory):
with open(sourceDirectory, "r") as file_in:
data = json.loads(file_in.read())
key = list(data.keys())[0]
return data[key]
def logic(self, fileIDs, j, url):
fileIDs[j] = self.__downloadDirectory + "file" + str(j+1) + ".txt"
response = requests.get(url, allow_redirects=True)
with open(fileIDs[j], "wb") as file_out:
file_out.write(response.content)
def download(self, runs=1):
return super().execute(self.__fileRemover, runs)
class Serializer(Action):
def __init__(self, sourceDirectory, targetDirectory, targetFormat, removeFiles=True):
super().__init__(sourceDirectory)
self.__storageDirectory = targetDirectory
self.__targetFormat = targetFormat
self.__fileRemover = FileRemoverOS(self.__storageDirectory, self.__targetFormat, removeFiles)
def __logicJSON(self, fileIDs, j, path):
fileIDs[j] = self.__storageDirectory + "file" + str(j+1) + self.__targetFormat
with open(path, "r", encoding="latin1") as file_in, open(fileIDs[j], "w") as file_out:
data = file_in.read()
json.dump(data, file_out)
def __logicPICKLE(self, fileIDs, j, path):
fileIDs[j] = self.__storageDirectory + "file" + str(j+1) + self.__targetFormat
with open(path, "r", encoding="latin1") as file_in, open(fileIDs[j], "wb") as file_out:
data = file_in.read()
pkl.dump(data, file_out, protocol=pkl.HIGHEST_PROTOCOL)
def logic(self, fileIDs, j, path):
if (self.__targetFormat == ".json"):
self.__logicJSON(fileIDs, j, path)
elif (self.__targetFormat == ".pkl"):
self.__logicPICKLE(fileIDs, j, path)
else:
raise ValueError("Format not supported. Choose between .json and .pkl")
def serialize(self, runs=1):
return super().execute(self.__fileRemover, runs)
class Deserializer(Action):
def __init__(self, sourceDirectory, targetDirectory, targetFormat, removeFiles=True):
super().__init__(sourceDirectory)
self.__storageDirectory = targetDirectory
self.__targetFormat = targetFormat
self.__fileRemover = FileRemoverOS(self.__storageDirectory, ".txt", removeFiles)
def __logicJSON(self, fileIDs, j, path):
fileIDs[j] = self.__storageDirectory + "file" + str(j+1) + "FromJSON" + ".txt"
with open(path, "r") as file_in, open(fileIDs[j], "wb") as file_out:
data = json.load(file_in)
file_out.write(data.encode("latin1"))
def __logicPICKLE(self, fileIDs, j, path):
fileIDs[j] = self.__storageDirectory + "file" + str(j+1) + "FromPICKLE" + ".txt"
with open(path, "rb") as file_in, open(fileIDs[j], "wb") as file_out:
data = pkl.load(file_in)
file_out.write(data.encode("latin1"))
def logic(self, fileIDs, j, path):
if (self.__targetFormat == ".json"):
self.__logicJSON(fileIDs, j, path)
elif (self.__targetFormat == ".pkl"):
self.__logicPICKLE(fileIDs, j, path)
else:
raise ValueError("Format not supported. Choose between .json and .pkl")
def deserialize(self, runs=1):
return super().execute(self.__fileRemover, runs)
class IPFSUpload(Action):
def __init__(self, sourceDirectory, removeFiles=True):
super().__init__(sourceDirectory)
self.__fileRemover = FileRemoverIPFS(removeFiles)
def logic(self, fileIDs, j, path):
with ipfshttpclient.connect() as client:
result = client.add(path, wrap_with_directory=True)
directoryHash = result[1]['Hash']
fileName = result[0]['Name']
fileIDs[j] = [directoryHash, fileName]
def upload(self, runs=1):
return super().execute(self.__fileRemover, runs)
class IPFSDownloadDist(Action):
def __init__(self, sourceCIDs, targetDirectory, targetFormat, removeFiles=True):
super().__init__(sourceCIDs)
self.__storageDirectory = targetDirectory
self.__fileRemoverIPFS = FileRemoverIPFS(removeFiles)
self.__fileRemoverOS = FileRemoverOS(self.__storageDirectory, targetFormat, removeFiles)
def logic(self, fileIDs, j, path):
with ipfshttpclient.connect() as client:
client.get(path[0] + "/" + path[1], self.__storageDirectory)
fileIDs[j] = self.__storageDirectory + path[1]
def download(self, runs=1):
self.__fileRemoverOS.execute()
return super().execute(self.__fileRemoverIPFS, runs)
class IPFSDownloadLocal(Action):
def __init__(self, sourceCIDs, targetDirectory, targetFormat, removeFiles=True):
super().__init__(sourceCIDs)
self.__storageDirectory = targetDirectory
self.__fileRemoverOS = FileRemoverOS(self.__storageDirectory, targetFormat, removeFiles)
def logic(self, fileIDs, j, path):
with ipfshttpclient.connect() as client:
client.get(path[0] + "/" + path[1], self.__storageDirectory)
fileIDs[j] = self.__storageDirectory + path[1]
def download(self, runs=1):
return super().execute(self.__fileRemoverOS, runs)
class IPFSClient:
def __init__(self):
self.__client = ipfshttpclient.connect()
self.__confirmInstance()
self.__nodeIP = ""
def __confirmInstance(self):
print("\nIPFS daemon running on version: " + self.__client.version()["Version"])
self.__nodeIP = self.__client.id()["Addresses"][2]
print("Node IP: " + self.__nodeIP)
def getClient(self):
return self.__client
class IPFSDistributor:
def __init__(self, sourceDirectory, targetIDs):
self.__configfilePath = sourceDirectory
self.__targetIDs = targetIDs
self.__client = paramiko.SSHClient()
self.__distributedCIDs = ["" for i in range(len(self.__targetIDs))]
def distribute(self):
ip, user, key = self.__loadIP()
self.__client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.__client.connect(ip, 22, user, key, timeout=10)
for i in range(len(self.__targetIDs)):
stdin, stdout, stderr = self.__client.exec_command("ipfs get" + " " + self.__targetIDs[i][0] + "/" + self.__targetIDs[i][1])
stdin, stdout, stderr = self.__client.exec_command("ipfs add -w" + " " + self.__targetIDs[i][1])
storageInfos = codecs.decode(stdout.read()).split( )
self.__distributedCIDs[i] = storageInfos[-1] + "/" + storageInfos[2]
self.__client.close()
return self.__distributedCIDs
def __loadIP(self):
with open(self.__configfilePath, "r") as file_in:
data = json.loads(file_in.read())
return data["ip"], data["user"], data["key"]
# Auxiliary classes
# -----------------------------------------------------------
class FileRemoverOS:
def __init__(self, targetDirectory, targetFormat, removeFiles):
self.__targetDirectory = targetDirectory
self.__targetFormat = targetFormat
self.__removeFlag = removeFiles
def execute(self):
if (self.__removeFlag):
for file in glob.glob(self.__targetDirectory + "*" + self.__targetFormat):
os.remove(file)
else:
pass
class FileRemoverIPFS:
def __init__(self, removeFiles):
self.__removeFlag = removeFiles
def execute(self):
if (self.__removeFlag):
with ipfshttpclient.connect() as client:
pinnedContent = client.pin.ls(type='recursive')['Keys']
for i in list(pinnedContent.keys()):
client.pin.rm(i)
client.repo.gc()
else:
pass
class Timer:
def __init__(self):
self.__start_time = None
def start(self):
if self.__start_time is not None:
raise TimerError(f"Timer is running! Use stop() method first.")
self.__start_time = time.perf_counter()
def stop(self):
if self.__start_time is None:
raise TimerError(f"Timer is not running. Use start() method first.")
deltaTime = time.perf_counter() - self.__start_time
self.__start_time = None
return deltaTime
class TimerError(Exception):
""" Custom exception used for Timer class """
class DataStore:
def __init__(self, fileName):
self.__file = fileName
self.__initFile(fileName)
def __initFile(self, fileName):
with open(fileName, "w") as file:
json.dump({}, file)
def store(self, newData):
with open(self.__file, "r+") as file:
data = json.load(file)
data.update(newData)
file.seek(0)
json.dump(data, file)