Process the server command.
01366 : """Process the server command.""" database = self.GetDatabase() # Get the port number specified by a command option, if any. # Otherwise use a default value. port_number = self.GetCommandOption("port", default=0) try: port_number = int(port_number) except ValueError: raise qm.cmdline.CommandError, qm.error("bad port number") # Get the local address specified by a command option, if any. # If not was specified, use the loopback address. The loopback # address is used by default for security reasons; it restricts # access to the QMTest server to users on the local machine. address = self.GetCommandOption("address", default="127.0.0.1") # If a log file was requested, open it now. log_file_path = self.GetCommandOption("log-file") if log_file_path == "-": # A hyphen path name means standard output. log_file = sys.stdout elif log_file_path is None: # No log file. log_file = None else: # Otherwise, it's a file name. Open it for append. log_file = open(log_file_path, "a+") # If a PID file was requested, create it now. pid_file_path = self.GetCommandOption("pid-file") if pid_file_path is not None: # If a PID file was requested, but no explicit path was # given, use a default value. if not pid_file_path: pid_file_path = qm.common.rc.Get("pid-file", "/var/run/qmtest.pid", "qmtest") try: pid_file = open(pid_file_path, "w") except IOError, e: raise qm.cmdline.CommandError, str(e) else: pid_file = None # Figure out which targets to use. targets = self.GetTargets() # Compute the context in which the tests will be run. context = self.MakeContext() # Set up the server. server = qm.test.web.web.QMTestServer(database, port_number, address, log_file, targets, context, self.__GetExpectedOutcomes()) port_number = server.GetServerAddress() # Construct the URL to the main page on the server. if address == "": url_address = qm.platform.get_host_name() else: url_address = address url = "http://%s:%d/test/dir" % (url_address, port_number) if not self.HasCommandOption("no-browser"): # Now that the server is bound to its address, start the # web browser. qm.platform.open_in_browser(url) message = qm.message("server url", url=url) sys.stderr.write(message + "\n") # Become a daemon, if appropriate. if self.GetCommandOption("daemon") is not None: # Fork twice. if os.fork() != 0: os._exit(0) if os.fork() != 0: os._exit(0) # This process is now the grandchild of the original # process. # Write out the PID file. The correct PID is not known until # after the transformation to a daemon has taken place. try: if pid_file: pid_file.write(str(os.getpid())) pid_file.close() # Accept requests. try: server.Run() except qm.platform.SignalException, se: if se.GetSignalNumber() == signal.SIGTERM: # If we receive SIGTERM, shut down. pass else: # Other signals propagate outwards. raise finally: if pid_file: os.remove(pid_file_path) return 0 def __WriteCommandHelp(self, command):