Advertisement
Guest User

Twisted spawnProcess xvfb

a guest
Aug 20th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from twisted.internet import reactor, protocol, error, defer
  4. from twisted.internet.task import LoopingCall
  5.  
  6. # IMPORT FACTORS BEFORE RUNNING THIS SCRIPT
  7. # CREATE A USER (anyuser) and give that user SUDO right by going into your sudo file
  8. # and pasting the following lines at the bottom (replacing user with the user you created)
  9. # user ALL=(ALL) NOPASSWD:ALL
  10. # then when you run this script su to that user and make sure you are running as that user
  11.  
  12. import os
  13. import pwd
  14. import logging
  15.  
  16. # set up logging
  17. logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s',
  18. filename='/tmp/twisted_xvfb.log',
  19. level=logging.DEBUG)
  20.  
  21. class TimedProcessProtocol(protocol.ProcessProtocol):
  22.  
  23. def __init__(self, timeout):
  24. self.timeout = timeout
  25.  
  26. def connectionMade(self):
  27. logging.debug("connection made timeout = %d", self.timeout)
  28. @defer.inlineCallbacks
  29. def killIfAlive():
  30. logging.debug("timeout reached - killing process")
  31. try:
  32. yield self.transport.signalProcess('KILL')
  33. except error.ProcessExitedAlready:
  34. logging.debug("process already exited")
  35. pass
  36.  
  37. d = reactor.callLater(self.timeout, killIfAlive)
  38.  
  39. def outReceived(self, data):
  40. logging.debug("output: %s", data)
  41.  
  42. def errReceived(self, data):
  43. logging.debug("errReceived %s", data)
  44.  
  45. def errConnectionLost(self):
  46. logging.debug("errConnectionLost")
  47.  
  48. def inConnectionLost(self):
  49. logging.debug("inConnectionLost")
  50.  
  51. def processExited(self, reason):
  52. logging.debug("process exited, status %s", reason.value.exitCode)
  53.  
  54.  
  55. class FirefoxProcess:
  56. # This Class is responsible for executing, communicating with
  57. # and finally killing a firefox processs
  58.  
  59. def __init__(self):
  60. pass
  61.  
  62. def run(self):
  63.  
  64. logging.debug("FirefoxProcess run called")
  65. username = pwd.getpwuid( os.getuid() )[0]
  66. logging.debug("current user: %s" % username)
  67.  
  68. command = ["/usr/bin/xvfb-run", "--server-args=\"-e /tmp/xvfb.log --auto-servernum\"", "xlogo"]
  69. subprocess = reactor.spawnProcess(TimedProcessProtocol(20), command[0], command)
  70.  
  71. logging.debug(type(subprocess))
  72. logging.debug("spawned a process: pid: %d", subprocess.pid)
  73.  
  74. lc = LoopingCall(self.check_if_file)
  75. self.check = lc
  76. lc.start(0.5)
  77.  
  78. def check_if_file(self):
  79. logging.debug("check_if_file called")
  80. if os.path.exists("/path/to/important/file"):
  81. logging.debug("firesharkReady file to exist")
  82. self.check.stop()
  83.  
  84. ff = FirefoxProcess()
  85. ff.run()
  86. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement