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-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-25 08:49:40 +02:00
|
|
|
def fmt(a,b,c,d):
|
|
|
|
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-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-25 08:49:40 +02:00
|
|
|
html = '<html><body>\n'
|
|
|
|
html += '<h1>Latest daca@home results</h1>'
|
2018-08-25 10:59:49 +02:00
|
|
|
html += '<pre>\n<b>' + fmt('Package','Date Time ','head','1.84') + '</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 = ''
|
|
|
|
cppcheck = 'cppcheck/cppcheck'
|
|
|
|
count = {'cppcheck/cppcheck':0, '1.84/cppcheck':0}
|
|
|
|
for line in open(filename,'rt'):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('2018-'):
|
|
|
|
datestr = line
|
|
|
|
elif line.startswith('cppcheck:'):
|
|
|
|
cppcheck = line[9:]
|
|
|
|
elif re.match(r'.*:[0-9]+:[0-9]+: [a-z]+: .*\]$', line):
|
|
|
|
count[cppcheck] += 1
|
|
|
|
|
|
|
|
html += fmt(package, datestr, str(count['cppcheck/cppcheck']), str(count['1.84/cppcheck'])) + '\n'
|
|
|
|
|
|
|
|
html += '</pre></body></html>\n'
|
|
|
|
return html
|
|
|
|
|
2018-08-25 10:25:05 +02:00
|
|
|
def sendAll(connection, data):
|
|
|
|
while data:
|
|
|
|
bytes = connection.send(data)
|
|
|
|
if bytes < len(data):
|
|
|
|
data = data[bytes:]
|
|
|
|
else:
|
|
|
|
data = None
|
|
|
|
time.sleep(0.5)
|
2018-08-23 21:31:02 +02:00
|
|
|
|
2018-08-25 10:59:49 +02:00
|
|
|
def httpGetResponse(data, contentType):
|
|
|
|
resp = 'HTTP/1.1 200 OK\n'
|
|
|
|
resp += 'Connection: close\n'
|
|
|
|
resp += 'Content-length: ' + str(len(data)) + '\n'
|
|
|
|
resp += 'Content-type: ' + contentType + '\n\n'
|
|
|
|
resp += data
|
|
|
|
sendAll(connection, resp)
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
resultPath = os.path.expanduser('~/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
|
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
server_address = ('', 8000)
|
|
|
|
sock.bind(server_address)
|
|
|
|
|
|
|
|
sock.listen(1)
|
|
|
|
|
2018-08-25 09:06:15 +02:00
|
|
|
latestResults = []
|
|
|
|
|
2018-08-23 21:31:02 +02:00
|
|
|
while True:
|
|
|
|
# wait for a connection
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] waiting for a connection')
|
2018-08-23 21:31:02 +02:00
|
|
|
connection, client_address = sock.accept()
|
|
|
|
|
|
|
|
try:
|
|
|
|
cmd = connection.recv(16)
|
|
|
|
if 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-23 22:13:53 +02:00
|
|
|
connection.sendall(packages[packageIndex])
|
2018-08-23 21:31:02 +02:00
|
|
|
packageIndex += 1
|
|
|
|
if packageIndex >= len(packages):
|
|
|
|
packageIndex = 0
|
|
|
|
elif cmd.startswith('write\n'):
|
|
|
|
data = cmd[6:]
|
2018-08-23 22:13:53 +02:00
|
|
|
while len(data) < 1024 * 1024:
|
2018-08-23 21:31:02 +02:00
|
|
|
d = connection.recv(1024)
|
|
|
|
if d:
|
|
|
|
data += d
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
pos = data.find('\n')
|
|
|
|
if data.startswith('ftp://') and pos > 10:
|
|
|
|
url = data[:pos]
|
2018-08-24 13:04:25 +02:00
|
|
|
print('[' + strDateTime() + '] write:'+url)
|
2018-08-23 21:31:02 +02:00
|
|
|
res = re.match(r'ftp://.*pool/main/[^/]+/([^/]+)/[^/]*tar.gz',url)
|
2018-08-23 22:13:53 +02:00
|
|
|
if res and url in packages:
|
2018-08-23 21:54:46 +02:00
|
|
|
print('results added for package ' + res.group(1))
|
2018-08-25 09:06:15 +02:00
|
|
|
filename = resultPath + '/' + res.group(1)
|
|
|
|
f = open(filename, 'wt')
|
2018-08-24 13:04:25 +02:00
|
|
|
f.write(strDateTime() + '\n' + data[pos+1:])
|
2018-08-23 21:31:02 +02:00
|
|
|
f.close()
|
2018-08-25 09:06:15 +02:00
|
|
|
if len(latestResults) >= 20:
|
|
|
|
latestResults = latestResults[1:]
|
|
|
|
latestResults.append(filename)
|
2018-08-25 08:49:40 +02:00
|
|
|
elif cmd=='GET /latest.html':
|
|
|
|
print('[' + strDateTime() + '] ' + cmd)
|
2018-08-25 09:06:15 +02:00
|
|
|
html = latestReport(latestResults)
|
2018-08-25 10:59:49 +02:00
|
|
|
httpGetResponse(html, 'text/html')
|
2018-08-25 08:49:40 +02:00
|
|
|
elif cmd.startswith('GET /'):
|
|
|
|
print('[' + strDateTime() + '] ' + cmd)
|
2018-08-25 10:59:49 +02:00
|
|
|
package = cmd[5:]
|
2018-08-25 11:08:41 +02:00
|
|
|
if package.find(' ') > 0:
|
|
|
|
package = package[:package.find(' ')]
|
2018-08-25 10:59:49 +02:00
|
|
|
filename = resultPath + '/' + package
|
|
|
|
print('filename:"' + filename + '"')
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
connection.send('HTTP/1.1 404 Not Found\n\n')
|
|
|
|
print(404)
|
|
|
|
else:
|
|
|
|
f = open(filename,'rt')
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
httpGetResponse(data, 'text/plain')
|
2018-08-23 22:13:53 +02:00
|
|
|
else:
|
2018-08-24 21:07:50 +02:00
|
|
|
print('[' + strDateTime() + '] invalid command: ' + cmd)
|
2018-08-23 21:31:02 +02:00
|
|
|
finally:
|
|
|
|
connection.close()
|
|
|
|
|