Beispiel #1
0
 def testIsLocked(self):
     lockf = self.mktemp()
     self.failIf(lockfile.isLocked(lockf))
     lock = lockfile.FilesystemLock(lockf)
     self.failUnless(lock.lock())
     self.failUnless(lockfile.isLocked(lockf))
     lock.unlock()
     self.failIf(lockfile.isLocked(lockf))
Beispiel #2
0
 def testIsLocked(self):
     lockf = self.mktemp()
     self.failIf(lockfile.isLocked(lockf))
     lock = lockfile.FilesystemLock(lockf)
     self.failUnless(lock.lock())
     self.failUnless(lockfile.isLocked(lockf))
     lock.unlock()
     self.failIf(lockfile.isLocked(lockf))
Beispiel #3
0
 def test_isLocked(self):
     """
     L{isLocked} returns C{True} if the named lock is currently locked,
     C{False} otherwise.
     """
     lockf = self.mktemp()
     self.assertFalse(lockfile.isLocked(lockf))
     lock = lockfile.FilesystemLock(lockf)
     self.assertTrue(lock.lock())
     self.assertTrue(lockfile.isLocked(lockf))
     lock.unlock()
     self.assertFalse(lockfile.isLocked(lockf))
Beispiel #4
0
 def test_isLocked(self):
     """
     L{isLocked} returns C{True} if the named lock is currently locked,
     C{False} otherwise.
     """
     lockf = self.mktemp()
     self.assertFalse(lockfile.isLocked(lockf))
     lock = lockfile.FilesystemLock(lockf)
     self.assertTrue(lock.lock())
     self.assertTrue(lockfile.isLocked(lockf))
     lock.unlock()
     self.assertFalse(lockfile.isLocked(lockf))
Beispiel #5
0
 def testPIDFile(self):
     filename = self.mktemp()
     f = Factory(self, filename)
     l = reactor.listenUNIX(filename, f, mode = 0600, wantPID=1)
     self.failUnless(lockfile.isLocked(filename + ".lock"))
     tcf = TestClientFactory(self, filename)
     c = reactor.connectUNIX(filename, tcf, checkPID=1)
     spinUntil(lambda :getattr(f.protocol, 'made', None) and
                       getattr(tcf.protocol, 'made', None))
     self._addPorts(l, c.transport, tcf.protocol.transport, f.protocol.transport)
     self.cleanPorts(*self.ports)
     self.failIf(lockfile.isLocked(filename + ".lock"))
Beispiel #6
0
    def testPIDFile(self):
        filename = self.mktemp()
        f = Factory(self, filename)
        l = reactor.listenUNIX(filename, f, mode = 0600, wantPID=1)
        self.failUnless(lockfile.isLocked(filename + ".lock"))
        tcf = TestClientFactory(self, filename)
        c = reactor.connectUNIX(filename, tcf, checkPID=1)

        spinUntil(lambda :getattr(f.protocol, 'made', None) and
                          getattr(tcf.protocol, 'made', None))

        self._addPorts(l, c.transport, tcf.protocol.transport, f.protocol.transport)
        self.cleanPorts(*self.ports)

        self.failIf(lockfile.isLocked(filename + ".lock"))
Beispiel #7
0
 def __init__(self, filename, connector, reactor=None, checkPID = 0):
     self.connector = connector
     self.realAddress = self.addr = filename
     if checkPID and not lockfile.isLocked(filename + ".lock"):
         self._finishInit(None, None, error.BadFileError(filename), reactor)
     self._finishInit(self.doConnect, self.createInternetSocket(),
                      None, reactor)
Beispiel #8
0
 def __init__(self, filename, connector, reactor=None, checkPID=0):
     self.connector = connector
     self.realAddress = self.addr = filename
     if checkPID and not lockfile.isLocked(filename + ".lock"):
         self._finishInit(None, None, error.BadFileError(filename), reactor)
     self._finishInit(self.doConnect, self.createInternetSocket(), None,
                      reactor)
Beispiel #9
0
 def __init__(self, filename, connector, reactor=None, checkPID=0):
     _SendmsgMixin.__init__(self)
     # Normalise the filename using UNIXAddress
     filename = address.UNIXAddress(filename).name
     self.connector = connector
     self.realAddress = self.addr = filename
     if checkPID and not lockfile.isLocked(filename + b".lock"):
         self._finishInit(None, None, error.BadFileError(filename), reactor)
     self._finishInit(self.doConnect, self.createInternetSocket(), None, reactor)
Beispiel #10
0
 def __init__(self, filename, connector, reactor=None, checkPID = 0):
     _SendmsgMixin.__init__(self)
     # Normalise the filename using UNIXAddress
     filename = address.UNIXAddress(filename).name
     self.connector = connector
     self.realAddress = self.addr = filename
     if checkPID and not lockfile.isLocked(filename + b".lock"):
         self._finishInit(None, None, error.BadFileError(filename), reactor)
     self._finishInit(self.doConnect, self.createInternetSocket(),
                      None, reactor)
Beispiel #11
0
 def testPIDFile(self):
     filename = self.mktemp()
     f = Factory(self, filename)
     l = reactor.listenUNIX(filename, f, mode = 0600, wantPID=1)
     self.failUnless(lockfile.isLocked(filename + ".lock"))
     tcf = TestClientFactory(self, filename)
     c = reactor.connectUNIX(filename, tcf, checkPID=1)
     d = defer.gatherResults([f.deferred, tcf.deferred])
     def _portStuff(ignored):
         self._addPorts(l, c.transport, tcf.protocol.transport,
                        f.protocol.transport)
         return self.cleanPorts(*self.ports)
     def _check(ignored):
         self.failIf(lockfile.isLocked(filename + ".lock"), 'locked')
     d.addCallback(_portStuff)
     d.addCallback(_check)
     return d
Beispiel #12
0
    def test_pidFile(self):
        """
        A lockfile is created and locked when L{IReactorUNIX.listenUNIX} is
        called and released when the Deferred returned by the L{IListeningPort}
        provider's C{stopListening} method is called back.
        """
        filename = self.mktemp()
        serverFactory = MyServerFactory()
        serverConnMade = defer.Deferred()
        serverFactory.protocolConnectionMade = serverConnMade
        unixPort = reactor.listenUNIX(filename, serverFactory, wantPID=True)
        self.assertTrue(lockfile.isLocked(filename + ".lock"))

        # XXX This part would test something about the checkPID parameter, but
        # it doesn't actually.  It should be rewritten to test the several
        # different possible behaviors.  -exarkun
        clientFactory = MyClientFactory()
        clientConnMade = defer.Deferred()
        clientFactory.protocolConnectionMade = clientConnMade
        reactor.connectUNIX(filename, clientFactory, checkPID=1)

        d = defer.gatherResults([serverConnMade, clientConnMade])

        def _portStuff(args):
            serverProtocol, clientProto = args

            # Incidental assertion which may or may not be redundant with some
            # other test.  This probably deserves its own test method.
            self.assertEqual(
                clientFactory.peerAddresses, [address.UNIXAddress(filename)]
            )

            clientProto.transport.loseConnection()
            serverProtocol.transport.loseConnection()
            return unixPort.stopListening()

        d.addCallback(_portStuff)

        def _check(ignored):
            self.assertFalse(lockfile.isLocked(filename + ".lock"), "locked")

        d.addCallback(_check)
        return d
    def test_pidFile(self):
        """
        A lockfile is created and locked when L{IReactorUNIX.listenUNIX} is
        called and released when the Deferred returned by the L{IListeningPort}
        provider's C{stopListening} method is called back.
        """
        filename = self.mktemp()
        serverFactory = MyServerFactory()
        serverConnMade = defer.Deferred()
        serverFactory.protocolConnectionMade = serverConnMade
        unixPort = reactor.listenUNIX(filename, serverFactory, wantPID=True)
        self.assertTrue(lockfile.isLocked(filename + ".lock"))

        # XXX This part would test something about the checkPID parameter, but
        # it doesn't actually.  It should be rewritten to test the several
        # different possible behaviors.  -exarkun
        clientFactory = MyClientFactory()
        clientConnMade = defer.Deferred()
        clientFactory.protocolConnectionMade = clientConnMade
        reactor.connectUNIX(filename, clientFactory, checkPID=1)

        d = defer.gatherResults([serverConnMade, clientConnMade])

        def _portStuff((serverProtocol, clientProto)):

            # Incidental assertion which may or may not be redundant with some
            # other test.  This probably deserves its own test method.
            self.assertEqual(clientFactory.peerAddresses, [address.UNIXAddress(filename)])

            clientProto.transport.loseConnection()
            serverProtocol.transport.loseConnection()
            return unixPort.stopListening()

        d.addCallback(_portStuff)

        def _check(ignored):
            self.failIf(lockfile.isLocked(filename + ".lock"), "locked")

        d.addCallback(_check)
        return d
Beispiel #14
0
class CtxFactory(ssl.ClientContextFactory):
    def getContext(self):
        self.method = SSL.TLSv1_METHOD
        ctx = ssl.ClientContextFactory.getContext(self)
        ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
                       verifyCallback)
        #print dir(ctx)
        #raise SystemExit
        return ctx


from os import path

if __name__ == '__main__':
    lock = FilesystemLock("treestatusbot.lock")
    if isLocked("treestatusbot.lock"):
        raise SystemExit("There's already a bot running. If this is not the "
                         "case, please remove treestatusbot.lock manually")
    else:
        lock.lock()

    def unlock():
        lock.unlock()

    f = GaiaBotFactory()
    reactor.connectSSL(SERVER, int(PORT), f, CtxFactory())
    print "Connecting to", SERVER, PORT
    # run bot
    reactor.addSystemEventTrigger('before', 'shutdown', unlock)
    reactor.run()
Beispiel #15
0
 def _check(ignored):
     self.assertFalse(lockfile.isLocked(filename + ".lock"), 'locked')
Beispiel #16
0
 def _check(ignored):
     self.failIf(lockfile.isLocked(filename + ".lock"), 'locked')
Beispiel #17
0
    def getContext(self):
        self.method = SSL.TLSv1_METHOD
        ctx = ssl.ClientContextFactory.getContext(self)
        ctx.set_verify(
            SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
            verifyCallback
        )
        #print dir(ctx)
        #raise SystemExit
        return ctx


from os import path

if __name__ == '__main__':
    lock = FilesystemLock("treestatusbot.lock")
    if isLocked("treestatusbot.lock"):
        raise SystemExit("There's already a bot running. If this is not the "
                         "case, please remove treestatusbot.lock manually")
    else:
        lock.lock()
    def unlock():
        lock.unlock()

    f = GaiaBotFactory()
    reactor.connectSSL(SERVER, int(PORT), f, CtxFactory())
    print "Connecting to", SERVER, PORT
    # run bot
    reactor.addSystemEventTrigger('before', 'shutdown', unlock)
    reactor.run()
Beispiel #18
0
 def _check(ignored):
     self.assertFalse(lockfile.isLocked(filename + ".lock"), 'locked')
Beispiel #19
0
 def test_holds_file_system_lock(self):
     lock = self.make_lock()
     self.assertFalse(lockfile.isLocked(lock.path))
     with lock:
         self.assertTrue(lockfile.isLocked(lock.path))
     self.assertFalse(lockfile.isLocked(lock.path))
Beispiel #20
0
 def _check(ignored):
     self.failIf(lockfile.isLocked(filename + ".lock"), 'locked')