_twistw.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- test-case-name: twisted.test.test_twistd -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. import os
  5. import sys
  6. from twisted import copyright
  7. from twisted.application import app, internet, service
  8. from twisted.python import log
  9. class ServerOptions(app.ServerOptions):
  10. synopsis = "Usage: twistd [options]"
  11. optFlags = [
  12. ["nodaemon", "n", "(for backwards compatibility)."],
  13. ]
  14. def opt_version(self):
  15. """
  16. Print version information and exit.
  17. """
  18. print(
  19. f"twistd (the Twisted Windows runner) {copyright.version}",
  20. file=self.stdout,
  21. )
  22. print(copyright.copyright, file=self.stdout)
  23. sys.exit()
  24. class WindowsApplicationRunner(app.ApplicationRunner):
  25. """
  26. An ApplicationRunner which avoids unix-specific things. No
  27. forking, no PID files, no privileges.
  28. """
  29. def preApplication(self):
  30. """
  31. Do pre-application-creation setup.
  32. """
  33. self.oldstdout = sys.stdout
  34. self.oldstderr = sys.stderr
  35. os.chdir(self.config["rundir"])
  36. def postApplication(self):
  37. """
  38. Start the application and run the reactor.
  39. """
  40. service.IService(self.application).privilegedStartService()
  41. app.startApplication(self.application, not self.config["no_save"])
  42. app.startApplication(internet.TimerService(0.1, lambda: None), 0)
  43. self.startReactor(None, self.oldstdout, self.oldstderr)
  44. log.msg("Server Shut Down.")