-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.py
109 lines (78 loc) · 2.85 KB
/
Node.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
from socket import *
import os
from radon.complexity import SCORE
from radon.cli.harvest import CCHarvester
from radon.cli import Config
import requests
from time import gmtime, strftime
import os.path
import sys
import shutil
from re import match
totalCC = 0
class Node():
masterUrl = 'http://localhost:1111/'
totalCC = 0
ccConfig = Config(
exclude='',
ignore='venv',
order=SCORE,
no_assert=True,
show_closures=False,
min='A',
max='F',
)
def __init__(self):
self.blobUrl = requests.get(self.masterUrl).json()
print(self.blobUrl)
def getHeader(self):
with open('token.txt', 'r') as tmp_file:
token = tmp_file.read() # get the token from a text file in current directory
payload = {'access_token': token}
headers = {'Accept': 'application/vnd.github.v3.raw'}
#print(token)
return (payload, headers)
def checkPy(self, filename):
return True if match('.*\.py', filename) is not None else False
def calcCC(self, blobUrl):
print(blobUrl)
url = blobUrl.split('|')[0]
filename = blobUrl.split('|')[1]
headers = self.getHeader()
flag = self.checkPy(filename)
if flag == True:
resp = requests.get(url, params=headers[0], headers=headers[1])
filePath = filename + '.py'
with open(filePath, 'w') as tmpFile:
tmpFile.write(resp.text)
tmpFile.close()
getFile = open(filePath, 'r')
results = CCHarvester(filePath, self.ccConfig).gobble(getFile)
getFile.close()
os.remove(filePath)
fileCC = 0
for x in results:
print (x.complexity)
fileCC += int(x.complexity)
print("Complexity of file: " + str(fileCC))
return fileCC
else:
return 0
def receiveWork(self):
print("Blob: " + self.blobUrl)
fileCC= self.calcCC(self.blobUrl)
self.totalCC += fileCC
self.blobUrl = requests.get(self.masterUrl).json()
while self.blobUrl != "finished":
fileCC = self.calcCC(self.blobUrl)
self.totalCC += fileCC
self.blobUrl = requests.get(self.masterUrl).json()
print("Finished...")
print("Total CC: " + str(self.totalCC))
requests.put(self.masterUrl, data={'cc': self.totalCC})
def main():
print("Worker is ready to receive...")
node = Node()
node.receiveWork()
if __name__ == "__main__":
main()