cppcheck/tools/donate-cpu-server.py

66 lines
1.7 KiB
Python
Raw Normal View History

# Server for 'donate-cpu.py'
import os
import socket
import re
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)
while True:
# wait for a connection
print 'waiting for a connection'
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()
print('get:' + packages[packageIndex])
connection.sendall(packages[packageIndex])
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:
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-23 22:13:53 +02:00
print('write:'+url)
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))
f = open(resultPath + '/' + res.group(1), 'wt')
f.write(data[pos+1:])
f.close()
2018-08-23 22:13:53 +02:00
else:
print('invalid cmd')
finally:
connection.close()