2013-04-24 12:50:14 +02:00
|
|
|
#!/usr/bin/python
|
2014-09-16 21:40:07 +02:00
|
|
|
##
|
|
|
|
## @author Edouard DUPIN
|
|
|
|
##
|
|
|
|
## @copyright 2012, Edouard DUPIN, all right reserved
|
|
|
|
##
|
|
|
|
## @license APACHE v2.0 (see license file)
|
|
|
|
##
|
|
|
|
|
2013-04-24 12:50:14 +02:00
|
|
|
import sys
|
|
|
|
import lutinDebug as debug
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import Queue
|
|
|
|
import os
|
2013-04-24 21:30:46 +02:00
|
|
|
import subprocess
|
2013-07-05 21:41:19 +02:00
|
|
|
import lutinTools
|
2013-07-09 15:00:07 +02:00
|
|
|
import lutinEnv
|
2014-10-17 21:46:31 +02:00
|
|
|
import shlex
|
2013-04-24 12:50:14 +02:00
|
|
|
|
2013-04-25 22:11:05 +02:00
|
|
|
queueLock = threading.Lock()
|
|
|
|
workQueue = Queue.Queue()
|
|
|
|
currentThreadWorking = 0
|
|
|
|
threads = []
|
2014-10-17 21:46:31 +02:00
|
|
|
# To know the first error arrive in the pool ==> to display all the time the same error file when multiple compilation
|
|
|
|
currentIdExecution = 0
|
|
|
|
errorExecution = {
|
|
|
|
"id":-1,
|
|
|
|
"cmd":"",
|
|
|
|
"return":0,
|
|
|
|
"err":"",
|
|
|
|
"out":"",
|
|
|
|
}
|
2013-04-25 22:11:05 +02:00
|
|
|
|
|
|
|
exitFlag = False # resuest stop of the thread
|
2013-12-23 22:22:24 +01:00
|
|
|
isinit = False # the thread are initialized
|
2013-04-25 22:11:05 +02:00
|
|
|
errorOccured = False # a thread have an error
|
|
|
|
processorAvaillable = 1 # number of CPU core availlable
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def store_command(cmdLine, file):
|
2013-07-05 21:41:19 +02:00
|
|
|
# write cmd line only after to prevent errors ...
|
2014-06-15 20:02:37 +02:00
|
|
|
if file != "" \
|
|
|
|
and file != None:
|
2013-07-05 21:41:19 +02:00
|
|
|
# Create directory:
|
2013-12-23 22:22:24 +01:00
|
|
|
lutinTools.create_directory_of_file(file)
|
2013-07-05 21:41:19 +02:00
|
|
|
# Store the command Line:
|
|
|
|
file2 = open(file, "w")
|
|
|
|
file2.write(cmdLine)
|
|
|
|
file2.flush()
|
|
|
|
file2.close()
|
|
|
|
|
2013-04-25 22:11:05 +02:00
|
|
|
|
2014-10-17 21:46:31 +02:00
|
|
|
def run_command(cmdLine, storeCmdLine="", buildId=-1, file=""):
|
|
|
|
global errorOccured
|
|
|
|
global exitFlag
|
|
|
|
global currentIdExecution
|
|
|
|
# prepare command line:
|
|
|
|
args = shlex.split(cmdLine)
|
|
|
|
#debug.verbose("cmd = " + str(args))
|
2013-04-24 21:30:46 +02:00
|
|
|
try:
|
2014-10-17 21:46:31 +02:00
|
|
|
# create the subprocess
|
|
|
|
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
debug.error("subprocess.CalledProcessError : TODO ...")
|
|
|
|
# launch the subprocess:
|
|
|
|
output, err = p.communicate()
|
|
|
|
# Check error :
|
|
|
|
if p.returncode == 0:
|
|
|
|
debug.debug(lutinEnv.print_pretty(cmdLine))
|
|
|
|
queueLock.acquire()
|
|
|
|
# TODO : Print the output all the time .... ==> to show warnings ...
|
|
|
|
if buildId >= 0 and (output != "" or err != ""):
|
|
|
|
debug.warning("output in subprocess compiling: '" + file + "'")
|
|
|
|
if output != "":
|
|
|
|
debug.print_compilator(output)
|
|
|
|
if err != "":
|
|
|
|
debug.print_compilator(err)
|
|
|
|
queueLock.release()
|
|
|
|
else:
|
2013-04-25 22:11:05 +02:00
|
|
|
errorOccured = True
|
|
|
|
exitFlag = True
|
2014-10-17 21:46:31 +02:00
|
|
|
# if No ID : Not in a multiprocess mode ==> just stop here
|
|
|
|
if buildId < 0:
|
|
|
|
debug.debug(lutinEnv.print_pretty(cmdLine), force=True)
|
|
|
|
debug.print_compilator(output)
|
|
|
|
debug.print_compilator(err)
|
|
|
|
if p.returncode == 2:
|
|
|
|
debug.error("can not compile file ... [keyboard interrrupt]")
|
|
|
|
else:
|
|
|
|
debug.error("can not compile file ... ret : " + str(p.returncode))
|
2013-04-25 22:11:05 +02:00
|
|
|
else:
|
2014-10-17 21:46:31 +02:00
|
|
|
# in multiprocess interface
|
|
|
|
queueLock.acquire()
|
|
|
|
# if an other write an error before, check if the current process is started before ==> then is the first error
|
|
|
|
if errorExecution["id"] >= buildId:
|
|
|
|
# nothing to do ...
|
|
|
|
queueLock.release()
|
|
|
|
return;
|
|
|
|
errorExecution["id"] = buildId
|
|
|
|
errorExecution["cmd"] = cmdLine
|
|
|
|
errorExecution["return"] = p.returncode
|
|
|
|
errorExecution["err"] = err,
|
|
|
|
errorExecution["out"] = output,
|
|
|
|
queueLock.release()
|
|
|
|
# not write the command file...
|
2013-04-25 22:11:05 +02:00
|
|
|
return
|
|
|
|
# write cmd line only after to prevent errors ...
|
2013-12-23 22:22:24 +01:00
|
|
|
store_command(cmdLine, storeCmdLine)
|
2013-04-24 12:50:14 +02:00
|
|
|
|
2013-04-25 22:11:05 +02:00
|
|
|
|
2013-04-24 12:50:14 +02:00
|
|
|
|
|
|
|
class myThread(threading.Thread):
|
|
|
|
def __init__(self, threadID, lock, queue):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.threadID = threadID
|
|
|
|
self.name = "Thread " + str(threadID)
|
|
|
|
self.queue = queue
|
|
|
|
self.lock = lock
|
|
|
|
def run(self):
|
2013-04-24 21:30:46 +02:00
|
|
|
debug.verbose("Starting " + self.name)
|
2013-04-24 12:50:14 +02:00
|
|
|
global exitFlag
|
2013-04-24 21:30:46 +02:00
|
|
|
global currentThreadWorking
|
|
|
|
workingSet = False
|
2014-10-17 21:46:31 +02:00
|
|
|
while exitFlag == False:
|
2013-04-24 12:50:14 +02:00
|
|
|
self.lock.acquire()
|
|
|
|
if not self.queue.empty():
|
2013-04-24 21:30:46 +02:00
|
|
|
if workingSet==False:
|
|
|
|
currentThreadWorking += 1
|
|
|
|
workingSet = True
|
2013-04-24 12:50:14 +02:00
|
|
|
data = self.queue.get()
|
|
|
|
self.lock.release()
|
2013-04-24 21:30:46 +02:00
|
|
|
debug.verbose(self.name + " processing '" + data[0] + "'")
|
2013-04-24 12:50:14 +02:00
|
|
|
if data[0]=="cmdLine":
|
|
|
|
comment = data[2]
|
|
|
|
cmdLine = data[1]
|
2013-04-24 21:30:46 +02:00
|
|
|
cmdStoreFile = data[3]
|
2014-10-17 21:46:31 +02:00
|
|
|
debug.print_element( "[" + str(data[4]) + "][" + str(self.threadID) + "] " + comment[0], comment[1], comment[2], comment[3])
|
|
|
|
run_command(cmdLine, cmdStoreFile, buildId=data[4], file=comment[3])
|
2013-04-24 12:50:14 +02:00
|
|
|
else:
|
|
|
|
debug.warning("unknow request command : " + data[0])
|
|
|
|
else:
|
2013-04-24 21:30:46 +02:00
|
|
|
if workingSet==True:
|
|
|
|
currentThreadWorking -= 1
|
|
|
|
workingSet=False
|
2013-04-24 12:50:14 +02:00
|
|
|
# no element to parse, just wait ...
|
|
|
|
self.lock.release()
|
|
|
|
time.sleep(0.2)
|
|
|
|
# kill requested ...
|
2013-04-24 21:30:46 +02:00
|
|
|
debug.verbose("Exiting " + self.name)
|
2013-04-24 12:50:14 +02:00
|
|
|
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def error_occured():
|
2013-04-24 12:50:14 +02:00
|
|
|
global exitFlag
|
|
|
|
exitFlag = True
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def set_core_number(numberOfcore):
|
2013-04-24 21:30:46 +02:00
|
|
|
global processorAvaillable
|
2013-04-24 12:50:14 +02:00
|
|
|
processorAvaillable = numberOfcore
|
2013-04-24 21:30:46 +02:00
|
|
|
debug.debug(" set number of core for multi process compilation : " + str(processorAvaillable))
|
2013-04-24 12:50:14 +02:00
|
|
|
# nothing else to do
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def init():
|
2013-04-24 12:50:14 +02:00
|
|
|
global exitFlag
|
2013-12-23 22:22:24 +01:00
|
|
|
global isinit
|
|
|
|
if isinit==False:
|
|
|
|
isinit=True
|
2013-04-24 12:50:14 +02:00
|
|
|
global threads
|
|
|
|
global queueLock
|
|
|
|
global workQueue
|
|
|
|
# Create all the new threads
|
|
|
|
threadID = 0
|
|
|
|
while threadID < processorAvaillable:
|
|
|
|
thread = myThread(threadID, queueLock, workQueue)
|
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
|
|
|
threadID += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def un_init():
|
2013-04-24 12:50:14 +02:00
|
|
|
global exitFlag
|
|
|
|
# Notify threads it's time to exit
|
|
|
|
exitFlag = True
|
|
|
|
if processorAvaillable > 1:
|
|
|
|
# Wait for all threads to complete
|
|
|
|
for tmp in threads:
|
2013-04-24 21:30:46 +02:00
|
|
|
debug.verbose("join thread ...")
|
2013-04-24 12:50:14 +02:00
|
|
|
tmp.join()
|
2013-04-24 21:30:46 +02:00
|
|
|
debug.verbose("Exiting ALL Threads")
|
2013-04-24 12:50:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def run_in_pool(cmdLine, comment, storeCmdLine=""):
|
2014-10-17 21:46:31 +02:00
|
|
|
global currentIdExecution
|
2013-04-24 12:50:14 +02:00
|
|
|
if processorAvaillable <= 1:
|
2013-12-23 22:22:24 +01:00
|
|
|
debug.print_element(comment[0], comment[1], comment[2], comment[3])
|
2014-10-17 21:46:31 +02:00
|
|
|
run_command(cmdLine, storeCmdLine, file=comment[3])
|
2013-04-24 12:50:14 +02:00
|
|
|
return
|
|
|
|
# multithreaded mode
|
2013-12-23 22:22:24 +01:00
|
|
|
init()
|
2013-04-24 12:50:14 +02:00
|
|
|
# Fill the queue
|
|
|
|
queueLock.acquire()
|
|
|
|
debug.verbose("add : in pool cmdLine")
|
2014-10-17 21:46:31 +02:00
|
|
|
workQueue.put(["cmdLine", cmdLine, comment, storeCmdLine, currentIdExecution])
|
|
|
|
currentIdExecution +=1;
|
2013-04-24 12:50:14 +02:00
|
|
|
queueLock.release()
|
|
|
|
|
|
|
|
|
2013-12-23 22:22:24 +01:00
|
|
|
def pool_synchrosize():
|
2013-04-25 22:11:05 +02:00
|
|
|
global errorOccured
|
2014-10-17 21:46:31 +02:00
|
|
|
global errorExecution
|
2013-04-24 12:50:14 +02:00
|
|
|
if processorAvaillable <= 1:
|
|
|
|
#in this case : nothing to synchronise
|
|
|
|
return
|
|
|
|
|
|
|
|
debug.verbose("wait queue process ended\n")
|
|
|
|
# Wait for queue to empty
|
2013-04-25 22:11:05 +02:00
|
|
|
while not workQueue.empty() \
|
|
|
|
and False==errorOccured:
|
2013-04-24 12:50:14 +02:00
|
|
|
time.sleep(0.2)
|
|
|
|
pass
|
2013-04-24 21:30:46 +02:00
|
|
|
# Wait all thread have ended their current process
|
2013-04-25 22:11:05 +02:00
|
|
|
while currentThreadWorking != 0 \
|
|
|
|
and False==errorOccured:
|
2013-04-24 21:30:46 +02:00
|
|
|
time.sleep(0.2)
|
|
|
|
pass
|
2013-04-25 22:11:05 +02:00
|
|
|
if False==errorOccured:
|
|
|
|
debug.verbose("queue is empty")
|
|
|
|
else:
|
2013-12-23 22:22:24 +01:00
|
|
|
un_init()
|
2014-10-17 21:46:31 +02:00
|
|
|
debug.debug("Thread return with error ... ==> stop all the pool")
|
|
|
|
if errorExecution["id"] == -1:
|
|
|
|
debug.error("Pool error occured ... (No return information on Pool)")
|
|
|
|
return
|
|
|
|
debug.error("Error in an pool element : [" + str(errorExecution["id"]) + "]", crash=False)
|
|
|
|
debug.debug(lutinEnv.print_pretty(errorExecution["cmd"]), force=True)
|
|
|
|
debug.print_compilator(str(errorExecution["out"][0]))
|
|
|
|
debug.print_compilator(str(errorExecution["err"][0]))
|
|
|
|
if errorExecution["return"] == 2:
|
|
|
|
debug.error("can not compile file ... [keyboard interrrupt]")
|
|
|
|
else:
|
|
|
|
debug.error("can not compile file ... return value : " + str(errorExecution["return"]))
|
2013-04-24 12:50:14 +02:00
|
|
|
|