|
import ZeroconfService
|
|
from BaseHTTPServer import HTTPServer
|
|
import logging
|
|
import os
|
|
import time
|
|
import WebServer
|
|
import MyUtils
|
|
import threading
|
|
|
|
LOG_FILENAME = "/var/log/autocloneserver.log"
|
|
rootPath = "/var/lib/autocloneserver"
|
|
downloadsPath = rootPath + "/www/"
|
|
portWebServer = 8000
|
|
classroomName = MyUtils.getClassroomName()
|
|
|
|
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
|
|
|
|
def checkmd5File(pathFileName):
|
|
md5File = pathFileName+'.md5'
|
|
if not os.path.isfile(md5File):
|
|
md5Hash = MyUtils.getMD5Hash(pathFileName+'.iso')
|
|
f = open(md5File, 'wt')
|
|
f.write(md5Hash)
|
|
f.close()
|
|
|
|
# publish files (Avahi)
|
|
def publishFiles():
|
|
published = []
|
|
|
|
while True:
|
|
listFiles = os.listdir(downloadsPath)
|
|
for i in listFiles:
|
|
if os.path.isfile(downloadsPath + i) and i.endswith('.iso'):
|
|
fileName = MyUtils.getFileName(os.path.basename(i))
|
|
pathFileName = downloadsPath + fileName
|
|
|
|
checkmd5File(pathFileName)
|
|
|
|
service = ZeroconfService.ZeroconfService(name = str(portWebServer)+"@"+i, port=3000, stype = "_autoclone_"+classroomName+"._tcp") #, domain="replicant", host="localhost", text="Esto es una prueba")
|
|
service.publish()
|
|
|
|
published.append(service)
|
|
|
|
time.sleep(300) #refresh in seconds
|
|
for i in published:
|
|
i.unpublish()
|
|
|
|
# Start web server
|
|
def startWebServer():
|
|
server = HTTPServer(('', portWebServer), WebServer.MyHandler)
|
|
server.serve_forever()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if not os.path.exists(rootPath):
|
|
os.mkdir(rootPath)
|
|
|
|
if not os.path.exists(downloadsPath):
|
|
os.mkdir(downloadsPath)
|
|
|
|
t = threading.Thread(target=publishFiles)
|
|
t.start()
|
|
|
|
t2 = threading.Thread(target=startWebServer)
|
|
t2.start()
|
|
|
|
|
|
#server.socket.close()
|
|
#service.unpublish()
|