2018-08-23 21:31:02 +02:00
|
|
|
|
|
|
|
# Server for 'donate-cpu.py'
|
|
|
|
|
2018-08-25 08:49:40 +02:00
|
|
|
import glob
|
2018-08-23 21:31:02 +02:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import re
|
2018-08-24 13:04:25 +02:00
|
|
|
import datetime
|
2018-08-25 08:49:40 +02:00
|
|
|
import time
|
2018-08-25 18:38:51 +02:00
|
|
|
from threading import Thread
|
2018-08-27 18:21:16 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-08-24 13:04:25 +02:00
|
|
|
|
|
|
|
def strDateTime():
|
|
|
|
d = datetime.date.strftime(datetime.datetime.now().date(), '%Y-%m-%d')
|
|
|
|
t = datetime.time.strftime(datetime.datetime.now().time(), '%H:%M')
|
|
|
|
return d + ' ' + t
|
|
|
|
|
2018-08-26 16:47:20 +02:00
|
|
|
def fmt(a,b,c,d,e):
|
2018-08-25 08:49:40 +02:00
|
|
|
ret = a + ' '
|
|
|
|
while len(ret)<10:
|
|
|
|
ret += ' '
|
|
|
|
if len(ret) == 10:
|
|
|
|
ret += b[:10] + ' '
|
|
|
|
while len(ret)<21:
|
|
|
|
ret += ' '
|
|
|
|
ret += b[-5:] + ' '
|
|
|
|
while len(ret) < 32-len(c):
|
|
|
|
ret += ' '
|
|
|
|
ret += c + ' '
|
|
|
|
while len(ret) < 37-len(d):
|
|
|
|
ret += ' '
|
|
|
|
ret += d
|
2018-08-26 16:47:20 +02:00
|
|
|
ret += ' ' + e
|
2018-08-25 10:59:49 +02:00
|
|
|
if a != 'Package':
|
|
|
|
pos = ret.find(' ')
|
|
|
|
ret = '<a href="' + a + '">' + a + '</a>' + ret[pos:]
|
2018-08-25 08:49:40 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-08-25 09:06:15 +02:00
|
|
|
def latestReport(latestResults):
|
2018-08-26 16:47:20 +02:00
|
|
|
html = '<html><head><title>Latest daca@home results</title></head><body>\n'
|
2018-08-25 08:49:40 +02:00
|
|
|
html += '<h1>Latest daca@home results</h1>'
|
2018-08-26 16:57:09 +02:00
|
|
|
html += '<pre>\n<b>' + fmt('Package','Date Time ','1.84','Head','Diff') + '</b>\n'
|
2018-08-25 08:49:40 +02:00
|
|
|
|
|
|
|
# Write report for latest results
|
2018-08-25 09:06:15 +02:00
|
|
|
for filename in latestResults:
|
2018-08-25 08:49:40 +02:00
|
|
|
package = filename[filename.rfind('/')+1:]
|
|
|
|
|
|
|
|
datestr = ''
|
2018-08-26 16:47:20 +02:00
|
|
|
count = ['0','0']
|
|
|
|
lost = 0
|
|
|
|
added = 0
|
2018-08-25 08:49:40 +02:00
|
|
|
for line in open(filename,'rt'):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('2018-'):
|
|
|
|
datestr = line
|
2018-08-26 16:47:20 +02:00
|
|
|
#elif line.startswith('cppcheck:'):
|
|
|
|
# cppcheck = line[9:]
|
|
|
|
elif line.startswith('count: '):
|
|
|
|
count = line.split(' ')[1:]
|
|
|
|
elif line.startswith('head '):
|
|
|
|
added += 1
|
2018-08-26 16:57:09 +02:00
|
|
|
elif line.startswith('1.84 '):
|
|
|
|
lost += 1
|
2018-08-26 16:47:20 +02:00
|
|
|
diff = ''
|
|
|
|
if lost > 0:
|
|
|
|
diff += '-' + str(lost)
|
|
|
|
if added > 0:
|
2018-08-26 16:57:09 +02:00
|
|
|
diff += '+' + str(added)
|
|
|
|
html += fmt(package, datestr, count[1], count[0], diff) + '\n'
|
2018-08-25 08:49:40 +02:00
|
|
|
|
|
|
|
html += '</pre></body></html>\n'
|
|
|
|
return html
|
|
|
|
|
2018-08-25 18:38:51 +02:00
|
|
|
|
2018-08-31 14:28:01 +02:00
|
|
|
def crashReport():
|
|
|
|
html = '<html><head><title>Crash report</title></head><body>\n'
|
|
|
|
html += '<h1>Crash report</h1>\n'
|
|
|
|
html += '<pre>\n'
|
|
|
|
html += '<b>Package 1.84 Head</b>\n'
|
|
|
|
for filename in sorted(glob.glob(os.path.expanduser('~/daca@home/donated-results/*'))):
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
continue
|
|
|
|
for line in open(filename, 'rt'):
|
|
|
|
if not line.startswith('count:'):
|
|
|
|
continue
|
|
|
|
if line.find('Crash') < 0:
|
|
|
|
break
|
|
|
|
packageName = filename[filename.rfind('/')+1:]
|
|
|
|
counts = line.strip().split(' ')
|
|
|
|
out = packageName + ' '
|
|
|
|
while len(out) < 40:
|
|
|
|
out += ' '
|
|
|
|
if counts[2] == 'Crash!':
|
|
|
|
out += 'Crash '
|
|
|
|
else:
|
|
|
|
out += ' '
|
|
|
|
if counts[1] == 'Crash!':
|
|
|
|
out += 'Crash'
|
|
|
|
out = '<a href=/"' + packageName + '">' + packageName + '</a>' + out[out.find(' '):]
|
|
|
|
html += out + '\n'
|
|
|
|
break
|
|
|
|
html += '</pre>\n'
|
|
|
|
html += '</body></html>\n'
|
|
|
|
return html
|
|
|
|
|
|
|
|
|
2018-08-27 18:21:16 +02:00
|
|
|
def diffReport():
|
|
|
|
html = '<html><head><title>Diff report</title></head><body>\n'
|
|
|
|
html += '<h1>Diff report</h1>\n'
|
|
|
|
html += '<pre>\n'
|
|
|
|
|
|
|
|
out = {}
|
|
|
|
|
|
|
|
# grep '^1.84 .*\]$' donated-results/* | sed 's/.*\[\(.*\)\]/\1/' | sort | uniq -c
|
|
|
|
p = subprocess.Popen(['./getdiff.sh', '1.84'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
comm = p.communicate()
|
|
|
|
stdout = comm[0]
|
|
|
|
for line in stdout.split('\n'):
|
|
|
|
a = line.strip().split()
|
|
|
|
if len(a) == 2:
|
|
|
|
count = a[0]
|
|
|
|
messageId = a[1]
|
|
|
|
out[messageId] = [count, '0']
|
|
|
|
|
|
|
|
# grep '^head .*\]$' donated-results/* | sed 's/.*\[\(.*\)\]/\1/' | sort | uniq -c
|
|
|
|
p = subprocess.Popen(['./getdiff.sh', 'head'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
comm = p.communicate()
|
|
|
|
stdout = comm[0]
|
|
|
|
for line in stdout.split('\n'):
|
|
|
|
a = line.strip().split()
|
|
|
|
if len(a) == 2:
|
|
|
|
count = a[0]
|
|
|
|
messageId = a[1]
|
|
|
|
if messageId in out:
|
|
|
|
out[messageId][1] = count
|
|
|
|
else:
|
|
|
|
out[messageId] = ['0',count]
|
|
|
|
|
|
|
|
html += '<b>MessageID 1.84 Head</b>\n'
|
|
|
|
sum0 = 0
|
|
|
|
sum1 = 0
|
|
|
|
for messageId in sorted(out.keys()):
|
|
|
|
line = messageId + ' '
|
|
|
|
counts = out[messageId]
|
|
|
|
sum0 += int(counts[0])
|
|
|
|
sum1 += int(counts[1])
|
|
|
|
if counts[0] > 0:
|
|
|
|
c = counts[0]
|
|
|
|
while len(line) < 40 - len(c):
|
|
|
|
line += ' '
|
|
|
|
line += c + ' '
|
|
|
|
if counts[1] > 0:
|
|
|
|
c = counts[1]
|
|
|
|
while len(line) < 48 - len(c):
|
|
|
|
line += ' '
|
|
|
|
line += c
|
|
|
|
line = '<a href="diff-' + messageId + '">' + messageId + '</a>' + line[line.find(' '):]
|
|
|
|
html += line + '\n'
|
|
|
|
|
|
|
|
# Sum
|
|
|
|
html += '================================================\n'
|
|
|
|
line = ''
|
|
|
|
while len(line) < 40 - len(str(sum0)):
|
|
|
|
line += ' '
|
|
|
|
line += str(sum0) + ' '
|
|
|
|
while len(line) < 48 - len(str(sum1)):
|
|
|
|
line += ' '
|
|
|
|
line += str(sum1)
|
|
|
|
html += line + '\n'
|
|
|
|
|
|
|
|
return html
|
|
|
|
|
2018-08-27 18:44:17 +02:00
|
|
|
def diffMessageIdReport(resultPath, messageId):
|
2018-08-27 18:21:16 +02:00
|
|
|
text = messageId + '\n'
|
|
|
|
e = '[' + messageId + ']\n'
|
2018-08-27 18:44:17 +02:00
|
|
|
for filename in sorted(glob.glob(resultPath + '/*')):
|
2018-08-27 18:21:16 +02:00
|
|
|
url = None
|
|
|
|
diff = False
|
|
|
|
for line in open(filename,'rt'):
|
|
|
|
if line.startswith('ftp://'):
|
|
|
|
url = line
|
|
|
|
elif line == 'diff:\n':
|
|
|
|
diff = True
|
|
|
|
elif not diff:
|
|
|
|
continue
|
|
|
|
elif line.endswith(e):
|
|
|
|
if url:
|
|
|
|
text += url
|
|
|
|
url = None
|
|
|
|
text += line
|
|
|
|
return text
|
|
|
|
|
2018-08-25 10:25:05 +02:00
|
|
|
def sendAll(connection, data):
|
|
|
|
while data:
|
2018-08-25 18:38:51 +02:00
|
|
|
num = connection.send(data)
|
|
|
|
if num < len(data):
|
|
|
|
data = data[num:]
|
2018-08-25 10:25:05 +02:00
|
|
|
else:
|
|
|
|
data = None
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-25 18:38:51 +02:00
|
|
|
|
|
|
|
def httpGetResponse(connection, data, contentType):
|
2018-08-26 11:17:18 +02:00
|
|
|
resp = 'HTTP/1.1 200 OK\r\n'
|
|
|
|
resp += 'Connection: close\r\n'
|
|
|
|
resp += 'Content-length: ' + str(len(data)) + '\r\n'
|
|
|
|
resp += 'Content-type: ' + contentType + '\r\n\r\n'
|
2018-08-25 10:59:49 +02:00
|
|
|
resp += data
|
|
|
|
sendAll(connection, resp)
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-25 18:38:51 +02:00
|
|
|
class HttpClientThread(Thread):
|
2018-08-27 18:21:16 +02:00
|
|
|
def __init__(self, connection, cmd, resultPath, latestResults):
|
2018-08-25 18:38:51 +02:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.connection = connection
|
|
|
|
self.cmd = cmd[:cmd.find('\n')]
|
2018-08-27 18:21:16 +02:00
|
|
|
self.resultPath = resultPath
|
2018-08-25 18:38:51 +02:00
|
|
|
self.latestResults = latestResults
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-25 18:38:51 +02:00
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
cmd = self.cmd
|
|
|
|
print('[' + strDateTime() + '] ' + cmd)
|
2018-08-27 18:21:16 +02:00
|
|
|
res = re.match(r'GET /([a-zA-Z0-9_\-\.]+) HTTP', cmd)
|
|
|
|
if res is None:
|
|
|
|
self.connection.close()
|
|
|
|
return
|
|
|
|
url = res.group(1)
|
|
|
|
if url == 'latest.html':
|
2018-08-25 18:38:51 +02:00
|
|
|
html = latestReport(self.latestResults)
|
|
|
|
httpGetResponse(self.connection, html, 'text/html')
|
2018-08-31 14:28:01 +02:00
|
|
|
elif url == 'crash':
|
|
|
|
html = crashReport()
|
|
|
|
httpGetResponse(self.connection, html, 'text/html')
|
2018-08-27 18:21:16 +02:00
|
|
|
elif url == 'diff':
|
|
|
|
html = diffReport()
|
|
|
|
httpGetResponse(self.connection, html, 'text/html')
|
|
|
|
elif url.startswith('diff-'):
|
|
|
|
messageId = url[5:]
|
2018-08-27 18:44:17 +02:00
|
|
|
text = diffMessageIdReport(self.resultPath, messageId)
|
2018-08-27 18:21:16 +02:00
|
|
|
httpGetResponse(self.connection, text, 'text/plain')
|
2018-08-25 18:38:51 +02:00
|
|
|
else:
|
2018-08-27 18:21:16 +02:00
|
|
|
filename = resultPath + '/' + url
|
2018-08-25 18:38:51 +02:00
|
|
|
if not os.path.isfile(filename):
|
2018-08-25 20:58:31 +02:00
|
|
|
print('HTTP/1.1 404 Not Found')
|
2018-08-27 18:21:16 +02:00
|
|
|
self.connection.send('HTTP/1.1 404 Not Found\r\n\r\n')
|
2018-08-25 18:38:51 +02:00
|
|
|
else:
|
|
|
|
f = open(filename,'rt')
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
httpGetResponse(self.connection, data, 'text/plain')
|
2018-08-26 11:17:18 +02:00
|
|
|
finally:
|
2018-08-25 20:58:31 +02:00
|
|
|
time.sleep(1)
|
2018-08-25 18:38:51 +02:00
|
|
|
self.connection.close()
|
|
|
|
|
2018-08-27 18:21:16 +02:00
|
|
|
def server(server_address_port, packages, packageIndex, resultPath):
|
2018-08-25 18:38:51 +02:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2018-08-27 18:21:16 +02:00
|
|
|
server_address = ('', server_address_port)
|
2018-08-25 18:38:51 +02:00
|
|
|
sock.bind(server_address)
|
|
|
|
|
|
|
|
sock.listen(1)
|
|
|
|
|
|
|
|
latestResults = []
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# wait for a connection
|
|
|
|
print('[' + strDateTime() + '] waiting for a connection')
|
|
|
|
connection, client_address = sock.accept()
|
2018-08-25 20:00:04 +02:00
|
|
|
try:
|
|
|
|
cmd = connection.recv(128)
|
2018-08-25 20:43:20 +02:00
|
|
|
except socket.error:
|
2018-08-27 18:21:16 +02:00
|
|
|
connection.close()
|
2018-08-25 20:00:04 +02:00
|
|
|
continue
|
2018-08-25 18:38:51 +02:00
|
|
|
if cmd.find('\n') < 1:
|
|
|
|
continue
|
|
|
|
firstLine = cmd[:cmd.find('\n')]
|
|
|
|
if re.match('[a-zA-Z0-9./ ]+',firstLine) is None:
|
|
|
|
connection.close()
|
|
|
|
continue;
|
|
|
|
if cmd.startswith('GET /'):
|
2018-08-27 18:21:16 +02:00
|
|
|
newThread = HttpClientThread(connection, cmd, resultPath, latestResults)
|
2018-08-25 18:38:51 +02:00
|
|
|
newThread.start()
|
|
|
|
elif cmd=='get\n':
|
2018-08-23 22:13:53 +02:00
|
|
|
packages[packageIndex] = packages[packageIndex].strip()
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] get:' + packages[packageIndex])
|
2018-08-25 18:38:51 +02:00
|
|
|
connection.send(packages[packageIndex])
|
2018-08-23 21:31:02 +02:00
|
|
|
packageIndex += 1
|
|
|
|
if packageIndex >= len(packages):
|
|
|
|
packageIndex = 0
|
2018-08-25 20:58:31 +02:00
|
|
|
f = open('package-index.txt', 'wt')
|
|
|
|
f.write(str(packageIndex) + '\n')
|
|
|
|
f.close()
|
2018-08-25 18:38:51 +02:00
|
|
|
connection.close()
|
|
|
|
elif cmd.startswith('write\nftp://'):
|
|
|
|
# read data
|
2018-08-23 21:31:02 +02:00
|
|
|
data = cmd[6:]
|
2018-08-25 18:38:51 +02:00
|
|
|
try:
|
|
|
|
t = 0
|
|
|
|
while (len(data) < 1024 * 1024) and (not data.endswith('\nDONE')) and (t < 10):
|
|
|
|
d = connection.recv(1024)
|
|
|
|
if d:
|
|
|
|
t = 0
|
|
|
|
data += d
|
|
|
|
else:
|
|
|
|
time.sleep(0.2)
|
|
|
|
t += 0.2
|
|
|
|
connection.close()
|
|
|
|
except socket.error as e:
|
|
|
|
pass
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
pos = data.find('\n')
|
2018-08-25 18:38:51 +02:00
|
|
|
if pos < 10:
|
|
|
|
continue
|
|
|
|
url = data[:pos]
|
2018-08-26 16:47:20 +02:00
|
|
|
print('[' + strDateTime() + '] write:' + url)
|
2018-08-25 18:38:51 +02:00
|
|
|
|
|
|
|
# save data
|
|
|
|
res = re.match(r'ftp://.*pool/main/[^/]+/([^/]+)/[^/]*tar.gz',url)
|
|
|
|
if res and url in packages:
|
|
|
|
print('results added for package ' + res.group(1))
|
|
|
|
filename = resultPath + '/' + res.group(1)
|
|
|
|
f = open(filename, 'wt')
|
2018-08-26 16:47:20 +02:00
|
|
|
f.write(strDateTime() + '\n' + data)
|
2018-08-25 10:59:49 +02:00
|
|
|
f.close()
|
2018-08-25 18:38:51 +02:00
|
|
|
# track latest added results..
|
|
|
|
if len(latestResults) >= 20:
|
|
|
|
latestResults = latestResults[1:]
|
|
|
|
latestResults.append(filename)
|
2018-08-23 22:13:53 +02:00
|
|
|
else:
|
2018-08-25 18:38:51 +02:00
|
|
|
print('[' + strDateTime() + '] invalid command: ' + firstLine)
|
|
|
|
connection.close()
|
2018-08-27 18:21:16 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
workPath = os.path.expanduser('~/daca@home')
|
|
|
|
os.chdir(workPath)
|
|
|
|
resultPath = 'donated-results'
|
|
|
|
|
|
|
|
f = open('packages.txt', 'rt')
|
|
|
|
packages = f.readlines()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
print('packages: ' + str(len(packages)))
|
|
|
|
|
|
|
|
if len(packages) == 0:
|
|
|
|
print('fatal: there are no packages')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
packageIndex = 0
|
|
|
|
if os.path.isfile('package-index.txt'):
|
|
|
|
f = open('package-index.txt', 'rt')
|
|
|
|
packageIndex = int(f.read())
|
|
|
|
if packageIndex < 0 or packageIndex >= len(packages):
|
|
|
|
packageIndex = 0
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
server_address_port = 8000
|
|
|
|
if '--test' in sys.argv[1:]:
|
|
|
|
server_address_port = 8001
|
|
|
|
|
|
|
|
server(server_address_port, packages, packageIndex, resultPath)
|
|
|
|
|
|
|
|
|