def __init__(self, description, mainClassOrJar, args=None, workingDir=qtasteRootDirectory, classPath=None, vmArgs="", jmxPort=None, checkAfter=None, priority=None, useJacoco=False, useJavaGUI=False, active=True): """ Initialize JavaProcess object @param description control script action description, also used as window title @param mainClassOrJar java main class or jar file to execute @param args arguments to pass to the application or None if no argument @param workingDir working directory to start process in, defaults to QTaste root directory @param classPath class path or None to use current class path @param vmArgs arguments to be passed to the java VM or None to use no additional java VM arguments @param jmxPort JMX port or None to disable JMX @param checkAfter number of seconds after which to check if process still exist or None to not check @param priority specifies to run the process with the given priority: "low", "belownormal", "normal", "abovenormal", "high" or "realtime" or none for default priority @param useJacoco enable the coverage analysis using jacoco tool @param useJavaGUI enable the javagui service to enable remote javagui accessibility """ ControlAction.__init__(self, description, active) self.callerScript = traceback.format_stack()[0].split("\"")[1] self.mainClassOrJar = mainClassOrJar self.args = args if args is None: self.mainWithArgs = mainClassOrJar else: self.mainWithArgs = mainClassOrJar + ' ' + args if _OS.getType() != _OS.Type.WINDOWS: self.workingDir = workingDir else: self.workingDir = workingDir.replace("/", _os.sep) if classPath: if _OS.getType() != _OS.Type.WINDOWS: self.classPath = classPath.replace(";",":") else: self.classPath = classPath.replace(":",";") self.classPath = self.classPath.replace("/", _os.sep) else: self.classPath = None self.vmArgs = vmArgs # if useJacoco: # jacocoHome = _os.getenv("JACOCO_HOME") # if not jacocoHome: # print "WARNING: JACOCO_HOME variable not defined - Jacoco coverage disabled!\n" # else: # self.vmArgs += " -javaagent:" + jacocoHome + _os.sep + "lib" + _os.sep + "jacocoagent.jar=append=true,destfile=" + "reports" + _os.sep + description + ".jacoco" self.useJacoco = useJacoco # if useJavaGUI: # self.vmArgs += " -javaagent:" + qtasteRootDirectory + "plugins" + _os.sep + "SUT" + _os.sep + "qtaste-javagui-deploy.jar" self.useJavaGUI = useJavaGUI if jmxPort: self.jmxPort = "%d" % jmxPort else: self.jmxPort = None if checkAfter: self.checkAfter = "%d" % checkAfter else: self.checkAfter = None self.priority = priority
def stop(self): print "Stopping " + self.description + "..."; shellScriptArguments = self.executable shellScriptArguments = shellScriptArguments + " " + self.args if _OS.getType() != _OS.Type.WINDOWS: ControlAction.executeShellScript("stop_process", '"' + shellScriptArguments + '"') else: ControlAction.executeShellScript("stop_process", shellScriptArguments)
def stop(self): print "Stopping " + self.description + "..."; shellScriptArguments = self.serviceName if _OS.getType() != _OS.Type.WINDOWS: print "Not yet implemented!" #ControlAction.executeShellScript("stop_service_process", '"' + shellScriptArguments + '"') else: ControlAction.executeShellScript("stop_service_process", shellScriptArguments)
def __init__(self, startCommand=None, stopCommand=None, shell=_IF(_OS.getType() == _OS.Type.WINDOWS, "cmd", "bash"), active=True): """ Initializes ShellCommand object. @param startCommand shell command to execute on start, or None @param stopCommand shell command to execute on stop, or None @param shell the shell to use (default is "bash" on Linux, "cmd" on Windows) """ Command.__init__(self, "Command execution using " + shell, startCommand, stopCommand, active) self.shell = shell
def start(self): print "Starting " + self.description + "..." isJar = self.mainClassOrJar.endswith(".jar") vmArgs = self.vmArgs if self.useJacoco: vmArgs += " " + self.getJacocoVar() if self.useJavaGUI: vmArgs += " " + self.getJavaGUIVar() if self.useJavaGUIFX: vmArgs += " " + self.getJavaGUIFXVar() if _OS.getType() != _OS.Type.WINDOWS: shellScriptArguments = [] if isJar: shellScriptArguments.append("-jar") shellScriptArguments.append(self.mainWithArgs) shellScriptArguments.append("-dir") shellScriptArguments.append(self.workingDir) if self.classPath: shellScriptArguments.append("-cp") shellScriptArguments.append(self.classPath) shellScriptArguments.append("-title") shellScriptArguments.append(self.description) if len(vmArgs) > 0: shellScriptArguments.append("-vmArgs") shellScriptArguments.append(vmArgs) if self.jmxPort: shellScriptArguments.append("-jmxPort") shellScriptArguments.append(self.jmxPort) if self.checkAfter: shellScriptArguments.append("-checkAfter") shellScriptArguments.append(self.checkAfter) if self.priority: shellScriptArguments.append("-priority") shellScriptArguments.append(self.priority) shellScriptArguments.append("-restart") shellScriptArguments.append("true") else: shellScriptArguments = _IF(isJar, '-jar ', '') + '"' + self.mainWithArgs + '" -dir ' + self.workingDir + ' -title "' + self.description + '"' if self.classPath: updateQTasteRoot = qtasteRootDirectory.replace(":",";") self.classPath = self.classPath.replace(updateQTasteRoot, qtasteRootDirectory) shellScriptArguments += ' -cp "' + self.classPath + '"' if len(vmArgs) > 0: shellScriptArguments += ' -vmArgs "' + vmArgs + '"' if self.jmxPort: shellScriptArguments += ' -jmxPort ' + str(self.jmxPort) if self.checkAfter: shellScriptArguments += ' -checkAfter ' + str(self.checkAfter) if self.priority: shellScriptArguments += ' -priority ' + self.priority shellScriptArguments += ' -restart true' ControlAction.executeShellScript("start_java_process", shellScriptArguments) print
def escapeArgument(argument): """ Escape special characters in argument @param argument argument to escape @return argument with special characters escaped """ if (_OS.getType() == _OS.Type.WINDOWS): # under Windows, escape '"' characters in command return argument.replace('"', r'\"') else: return argument
def start(self): print "Starting " + self.description + "..." isJar = self.mainClassOrJar.endswith(".jar") vmArgs = self.vmArgs if self.useJacoco: vmArgs += " " + self.getJacocoVar() if self.useJavaGUI: vmArgs += " " + self.getJavaGUIVar() if self.useJavaGUIFX: vmArgs += " " + self.getJavaGUIFXVar() if _OS.getType() != _OS.Type.WINDOWS: shellScriptArguments = [] if isJar: shellScriptArguments.append("-jar") shellScriptArguments.append(self.mainWithArgs) shellScriptArguments.append("-dir") shellScriptArguments.append(self.workingDir) if self.classPath: shellScriptArguments.append("-cp") shellScriptArguments.append(self.classPath) shellScriptArguments.append("-title") shellScriptArguments.append(self.description) if len(vmArgs) > 0: shellScriptArguments.append("-vmArgs") shellScriptArguments.append(vmArgs) if self.jmxPort: shellScriptArguments.append("-jmxPort") shellScriptArguments.append(self.jmxPort) if self.checkAfter: shellScriptArguments.append("-checkAfter") shellScriptArguments.append(self.checkAfter) if self.priority: shellScriptArguments.append("-priority") shellScriptArguments.append(self.priority) else: shellScriptArguments = _IF(isJar, '-jar ', '') + '"' + self.mainWithArgs + '" -dir ' + self.workingDir + ' -title "' + self.description + '"' if self.classPath: updateQTasteRoot = qtasteRootDirectory.replace(":",";") self.classPath = self.classPath.replace(updateQTasteRoot, qtasteRootDirectory) shellScriptArguments += ' -cp "' + self.classPath + '"' if len(vmArgs) > 0: shellScriptArguments += ' -vmArgs "' + vmArgs + '"' if self.jmxPort: shellScriptArguments += ' -jmxPort ' + str(self.jmxPort) if self.checkAfter: shellScriptArguments += ' -checkAfter ' + str(self.checkAfter) if self.priority: shellScriptArguments += ' -priority ' + self.priority ControlAction.executeShellScript("start_java_process", shellScriptArguments) print
def escapeArgument(argument): """ Escape special characters in argument @param argument argument to escape @return argument with special characters escaped """ if _OS.getType() == _OS.Type.WINDOWS: # under Windows, escape '"' characters in command return argument.replace('"', r'\"') else: return argument
def start(self): print "Starting " + self.description + "..." if _OS.getType() != _OS.Type.WINDOWS: print "Not yet implemented!" #shellScriptArguments = [] #shellScriptArguments.append(self.serviceName) #shellScriptArguments.append("-title") #shellScriptArguments.append(self.description) else: shellScriptArguments = '"' + self.serviceName + '" -title "' + self.description + '"' ControlAction.executeShellScript("start_service_process", shellScriptArguments) print
def __init__(self, findString, replaceString, files, active=True): """ Initialize ReplaceInFiles object. @param findString regular expression string to find @param replaceString string by which to replace findString, may contain matches references in the form \1 @param files file name or list of files names """ ControlAction.__init__(self, "Replace in file(s)", active) self.callerScript = traceback.format_stack()[0].split("\"")[1] self.findString = findString self.replaceString = replaceString self.files = _IF(type(files) == str, files, " ".join(files)) sed = _IF(_OS.getType() == _OS.Type.WINDOWS, qtasteRootDirectory + r"tools\GnuWin32\bin\sed", "sed") self.sedCommand = sed + " -r -i s/" + findString.replace("/", r"\/") + "/" + replaceString.replace("/", r"\/") + "/g " + self.files
def __init__(self, host, login, waitingTime=60, active=True): """ Initialize RebootRLogin object. @param host remote host @param login remote user login @param waitingTime time to wait, after reboot """ ControlAction.__init__(self, "Remote reboot using rlogin", active) self.callerScript = traceback.format_stack()[0].split('"')[1] self.host = host self.login = login self.waitingTime = waitingTime self.localuser = _IF(_OS.getType() == _OS.Type.WINDOWS, _os.getenv("username"), _os.getenv("user")) self.rlogin = _RLogin(host, self.localuser, login, "", False, False)
def __init__(self, findString, replaceString, files, active=True): """ Initialize ReplaceInFiles object. @param findString regular expression string to find @param replaceString string by which to replace findString, may contain matches references in the form \1 @param files file name or list of files names """ ControlAction.__init__(self, "Replace in file(s)", active) self.callerScript = traceback.format_stack()[0].split('"')[1] self.findString = findString self.replaceString = replaceString self.files = _IF(type(files) == str, files, " ".join(files)) sed = _IF(_OS.getType() == _OS.Type.WINDOWS, qtasteRootDirectory + r"tools\GnuWin32\bin\sed", "sed") self.sedCommand = sed + " -r -i s/" + findString.replace("/", r"\/") + "/" + replaceString.replace("/", r"\/") + "/g " + self.files
def __init__(self, host, login, waitingTime=60, active=True): """ Initialize RebootRLogin object. @param host remote host @param login remote user login @param waitingTime time to wait, after reboot """ ControlAction.__init__(self, "Remote reboot using rlogin", active) self.callerScript = traceback.format_stack()[0].split("\"")[1] self.host = host self.login = login self.waitingTime = waitingTime self.localuser = _IF(_OS.getType() == _OS.Type.WINDOWS, _os.getenv("username"), _os.getenv("user")) self.rlogin = _RLogin(host, self.localuser, login, "", False, False)
def start(self): print "Starting " + self.description + "..." if _OS.getType() != _OS.Type.WINDOWS: shellScriptArguments = [] shellScriptArguments.append(self.executable) shellScriptArguments.append(self.args) shellScriptArguments.append("-dir") shellScriptArguments.append(self.workingDir) shellScriptArguments.append("-title") shellScriptArguments.append(self.description) if self.checkAfter: shellScriptArguments.append("-checkAfter") shellScriptArguments.append(self.checkAfter) else: shellScriptArguments = '"' + self.executable + '" "' + self.args + '" -dir ' + self.workingDir + ' -title "' + self.description + '"' if self.checkAfter: shellScriptArguments += ' -checkAfter ' + str(self.checkAfter) ControlAction.executeShellScript("start_process", shellScriptArguments) print
def start(self): print "Starting " + self.description + "..."; if _OS.getType() != _OS.Type.WINDOWS: shellScriptArguments = [] shellScriptArguments.append(self.executable) shellScriptArguments.append(self.args) shellScriptArguments.append("-dir") shellScriptArguments.append(self.workingDir) shellScriptArguments.append("-title") shellScriptArguments.append(self.description) if self.checkAfter: shellScriptArguments.append("-checkAfter") shellScriptArguments.append(self.checkAfter) else: shellScriptArguments = '"' + self.executable + '" "' + self.args + '" -dir ' + self.workingDir + ' -title "' + self.description + '"'; if self.checkAfter: shellScriptArguments += ' -checkAfter ' + str(self.checkAfter); ControlAction.executeShellScript("start_process", shellScriptArguments); print
def execute(self, command): if command: print 'Remotely executing "%s" on %s using ssh' % (command, self.host) ssh = _IF(_OS.getType() == _OS.Type.WINDOWS, qtasteRootDirectory + "tools/tools4ever/T4eSsh", "ssh") ControlAction.executeCommand([ssh, self.host, "-l", self.login, ControlAction.escapeArgument(command)]) print
def execute(self, command): if command: print 'Executing "%s" using %s' % (command, self.shell) ControlAction.executeCommand([self.shell, _IF(_OS.getType() == _OS.Type.WINDOWS, "/c", "-c"), command]) print
class ControlAction(object): """ Control script action """ def __init__(self, description, active=True): """ Initialize ControlAction object. @param description string describing the control action """ global controlScriptID self.callerScript = traceback.format_stack()[0].split('"')[1] self.description = description self.caID = controlScriptID self.active = active controlScriptID += 1 def start(self): """ Method called on start, to be overridden by subclasses """ pass def stop(self): """ Method called on stop, to be overridden by subclasses """ pass def dumpDataType(self, prefix, writer): """ Method called on start. It dumps the data type. to be overridden by subclasses """ writer.write(prefix + ".description=string\n") writer.write(prefix + ".type=string\n") writer.write(prefix + ".controlActionID=integer\n") writer.write(prefix + ".callerScript=string\n") writer.write(prefix + ".active=boolean\n") def dump(self, writer): """ Method called on start. It dumps the control action parameter in the writer, to be overridden by subclasses """ writer.write(str(self.caID) + '.description="' + self.description + '"\n') writer.write(str(self.caID) + ".type=" + self.__class__.__name__+ "\n") writer.write(str(self.caID) + ".controlActionID=" + str(self.caID) + "\n") writer.write(str(self.caID) + ".callerScript=" + self.callerScript + "\n") if self.active: writer.write(str(self.caID) + ".active=true\n") else: writer.write(str(self.caID) + ".active=false\n") def executeCommand(command): """ Execute a command and exit with error code if command returned an error @param command command (string or strings list) """ # don't use os.exec() because it don't return until all launched process are terminated error = _Exec().exec(command) if error: _sys.exit(error) executeCommand = staticmethod(executeCommand) def executeShellScript(name, arguments=None): """ Execute a shell script with arguments and exit with error code if shell script returned an error @param name shell script name without extension @param args arguments string passed to the shell script """ shellScriptFileName = ControlAction.shellScriptsDirectory + name + ControlAction.shellScriptExtension if type(arguments) is list: shellCommand =[shellScriptFileName] for argument in arguments: shellCommand.append(argument) ControlAction.executeCommand(shellCommand) else: shellCommand = shellScriptFileName if arguments: shellCommand += " " + arguments ControlAction.executeCommand(shellCommand) executeShellScript = staticmethod(executeShellScript) def escapeArgument(argument): """ Escape special characters in argument @param argument argument to escape @return argument with special characters escaped """ if _OS.getType() == _OS.Type.WINDOWS: # under Windows, escape '"' characters in command return argument.replace('"', r'\"') else: return argument escapeArgument = staticmethod(escapeArgument) # shell scripts directory shellScriptsDirectory = qtasteRootDirectory + "tools/" # shell script extension shellScriptExtension = _IF(_OS.getType() == _OS.Type.WINDOWS, ".cmd", ".sh")