Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from twisted.internet import reactor, protocol, error, defer
- from twisted.internet.task import LoopingCall
- # IMPORT FACTORS BEFORE RUNNING THIS SCRIPT
- # CREATE A USER (anyuser) and give that user SUDO right by going into your sudo file
- # and pasting the following lines at the bottom (replacing user with the user you created)
- # user ALL=(ALL) NOPASSWD:ALL
- # then when you run this script su to that user and make sure you are running as that user
- import os
- import pwd
- import logging
- # set up logging
- logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s',
- filename='/tmp/twisted_xvfb.log',
- level=logging.DEBUG)
- class TimedProcessProtocol(protocol.ProcessProtocol):
- def __init__(self, timeout):
- self.timeout = timeout
- def connectionMade(self):
- logging.debug("connection made timeout = %d", self.timeout)
- @defer.inlineCallbacks
- def killIfAlive():
- logging.debug("timeout reached - killing process")
- try:
- yield self.transport.signalProcess('KILL')
- except error.ProcessExitedAlready:
- logging.debug("process already exited")
- pass
- d = reactor.callLater(self.timeout, killIfAlive)
- def outReceived(self, data):
- logging.debug("output: %s", data)
- def errReceived(self, data):
- logging.debug("errReceived %s", data)
- def errConnectionLost(self):
- logging.debug("errConnectionLost")
- def inConnectionLost(self):
- logging.debug("inConnectionLost")
- def processExited(self, reason):
- logging.debug("process exited, status %s", reason.value.exitCode)
- class FirefoxProcess:
- # This Class is responsible for executing, communicating with
- # and finally killing a firefox processs
- def __init__(self):
- pass
- def run(self):
- logging.debug("FirefoxProcess run called")
- username = pwd.getpwuid( os.getuid() )[0]
- logging.debug("current user: %s" % username)
- command = ["/usr/bin/xvfb-run", "--server-args=\"-e /tmp/xvfb.log --auto-servernum\"", "xlogo"]
- subprocess = reactor.spawnProcess(TimedProcessProtocol(20), command[0], command)
- logging.debug(type(subprocess))
- logging.debug("spawned a process: pid: %d", subprocess.pid)
- lc = LoopingCall(self.check_if_file)
- self.check = lc
- lc.start(0.5)
- def check_if_file(self):
- logging.debug("check_if_file called")
- if os.path.exists("/path/to/important/file"):
- logging.debug("firesharkReady file to exist")
- self.check.stop()
- ff = FirefoxProcess()
- ff.run()
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement