Esempio n. 1
0
    def readlinesAndWait(self):
        from twisted.internet import reactor
        self.isRunning = True
        Topen.activePool.append(self)

        self.finished.acquire()
        from Hellanzb.Log import debug
        import thread
        debug('spawnProcess THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ')')

        # The reactor could have fallen asleep on us in -Lp mode! Why? I'm not sure, but
        # it dies after the first par2 process (Topen call) =[
        reactor.wakeUp()

        # Have the main, twisted thread, run the process. If we trigger spawnProcess from
        # a separate thread (which PostProcessor/Topens are always used from) instead, bad
        # things can occur (on Linux 2.6.x we can end up with defunct processes -- Trac
        # ticket #33). Running any twisted call from a non twisted thread is asking for
        # trouble. We also MUST usePTY, otherwise the processes receive signals (in
        # particular, SIGINT, rendering our first CTRL-C ignoring code useless, as it ends
        # up killing our sub processes)
        reactor.callFromThread(reactor.spawnProcess, self, self.cmd[0], self.cmd, os.environ,
                               usePTY = (not isWindows() and not isSolaris()) and 1 or 0)

        self.finished.wait()
        self.finished.release()

        # Here is where PostProcessor will typically die. After a process has been killed
        checkShutdown()

        # prepare the outbuffer (LAME)
        output = [line + '\n' for line in self.outBuf.getvalue().split('\n')]
        
        return output, self.returnCode
Esempio n. 2
0
 def authenticateUser(self, request):
     username, password = request.getUser(), request.getPassword()
     
     m = md5()
     m.update(password)
     
     authenticated = username == self.user and self.passwordDigest == m.digest()
     if not authenticated:
         debug('Failed HTTP Basic auth, user: ' + self.user)
         
     return authenticated
Esempio n. 3
0
    def authenticateUser(self, request):
        username, password = request.getUser(), request.getPassword()

        m = md5()
        m.update(password)

        authenticated = username == self.user and self.passwordDigest == m.digest(
        )
        if not authenticated:
            debug('Failed HTTP Basic auth, user: ' + self.user)

        return authenticated
Esempio n. 4
0
 def kill(self):
     from Hellanzb.Log import debug, error
     if isWindows():
         warn('Left running process: %s' % self.prettyCmd)
         return
     if self.isRunning:
         try:
             os.kill(self.getPid(), signal.SIGKILL)
         except OSError, ose:
             error('Unexpected problem while kill -9ing pid: ' + str(self.getPid()) + \
                   ' process: ' + self.prettyCmd, ose)
         except Exception, e:
             debug('could not kill process: ' + self.prettyCmd + ': ' + str(e))
Esempio n. 5
0
 def kill(self):
     from Hellanzb.Log import debug, error
     if isWindows():
         warn('Left running process: %s' % self.prettyCmd)
         return
     if self.isRunning:
         try:
             os.kill(self.getPid(), signal.SIGKILL)
         except OSError, ose:
             error('Unexpected problem while kill -9ing pid: ' + str(self.getPid()) + \
                   ' process: ' + self.prettyCmd, ose)
         except Exception, e:
             debug('could not kill process: ' + self.prettyCmd + ': ' +
                   str(e))
Esempio n. 6
0
def checkShutdown(message='Shutting down..'):
    """ Raise a SystemExit exception if the SHUTDOWN flag has been set """
    try:
        if Hellanzb.SHUTDOWN:
            debug(message)
            raise SystemExit(Hellanzb.SHUTDOWN_CODE)
        return False

    except (AttributeError, NameError):
        # typical during app shutdown
        raise SystemExit(Hellanzb.SHUTDOWN_CODE)

    except Exception, e:
        print 'Error in Util.checkShutdown' + str(e)
        raise SystemExit(Hellanzb.SHUTDOWN_CODE)
Esempio n. 7
0
    def processEnded(self, reason):
        self.returnCode = reason.value.exitCode

        from Hellanzb.Log import debug
        import thread
        debug('processEnded THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ') ' + 'aquiring lock')
        self.finished.acquire()
        debug('processEnded THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ')' + ' (pid: ' + str(self.getPid()) + ')')
        self.finished.notify()
        self.finished.release()

        self.isRunning = False
        Topen.activePool.remove(self)
Esempio n. 8
0
def checkShutdown(message = 'Shutting down..'):
    """ Raise a SystemExit exception if the SHUTDOWN flag has been set """
    try:
        if Hellanzb.SHUTDOWN:
            debug(message)
            raise SystemExit(Hellanzb.SHUTDOWN_CODE)
        return False
    
    except (AttributeError, NameError):
        # typical during app shutdown
        raise SystemExit(Hellanzb.SHUTDOWN_CODE)
    
    except Exception, e:
        print 'Error in Util.checkShutdown' + str(e)
        raise SystemExit(Hellanzb.SHUTDOWN_CODE)
Esempio n. 9
0
    def processEnded(self, reason):
        self.returnCode = reason.value.exitCode

        from Hellanzb.Log import debug
        import thread
        debug('processEnded THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ') ' + 'aquiring lock')
        self.finished.acquire()
        debug('processEnded THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ')' + ' (pid: ' + str(self.getPid()) + ')')
        self.finished.notify()
        self.finished.release()

        self.isRunning = False
        Topen.activePool.remove(self)
Esempio n. 10
0
def stdinEchoOff():
    """ ECHO OFF standard input """
    if not termios or Hellanzb.DAEMONIZE or Hellanzb.DISABLE_SCROLLER:
        return

    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return

    try:
        new = termios.tcgetattr(fd)
    except Exception, e:
        debug('stdinEchoOn error', e)
        return
Esempio n. 11
0
def stdinEchoOff():
    """ ECHO OFF standard input """
    if not termios or Hellanzb.DAEMONIZE or Hellanzb.DISABLE_SCROLLER:
        return
    
    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return

    try:
        new = termios.tcgetattr(fd)
    except Exception, e:
        debug('stdinEchoOn error', e)
        return
Esempio n. 12
0
def stdinEchoOn():
    """ ECHO ON standard input """
    if not termios or getattr(Hellanzb, 'DAEMONIZE', False) \
            or getattr(Hellanzb, 'DISABLE_SCROLLER', False):
        return

    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return

    try:
        new = termios.tcgetattr(fd)
    except Exception, e:
        debug('stdinEchoOn error', e)
        return
Esempio n. 13
0
def stdinEchoOn():
    """ ECHO ON standard input """
    if not termios or getattr(Hellanzb, 'DAEMONIZE', False) \
            or getattr(Hellanzb, 'DISABLE_SCROLLER', False):
        return
    
    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return

    try:
        new = termios.tcgetattr(fd)
    except Exception, e:
        debug('stdinEchoOn error', e)
        return
Esempio n. 14
0
    def readlinesAndWait(self):
        from twisted.internet import reactor
        self.isRunning = True
        Topen.activePool.append(self)

        self.finished.acquire()
        from Hellanzb.Log import debug
        import thread
        debug('spawnProcess THREAD ID: ' + str(thread.get_ident()) + ' (' + \
                  self.prettyCmd + ')')

        # The reactor could have fallen asleep on us in -Lp mode! Why? I'm not sure, but
        # it dies after the first par2 process (Topen call) =[
        reactor.wakeUp()

        # Have the main, twisted thread, run the process. If we trigger spawnProcess from
        # a separate thread (which PostProcessor/Topens are always used from) instead, bad
        # things can occur (on Linux 2.6.x we can end up with defunct processes -- Trac
        # ticket #33). Running any twisted call from a non twisted thread is asking for
        # trouble. We also MUST usePTY, otherwise the processes receive signals (in
        # particular, SIGINT, rendering our first CTRL-C ignoring code useless, as it ends
        # up killing our sub processes)
        reactor.callFromThread(
            reactor.spawnProcess,
            self,
            self.cmd[0],
            self.cmd,
            os.environ,
            usePTY=(not isWindows() and not isSolaris()) and 1 or 0)

        self.finished.wait()
        self.finished.release()

        # Here is where PostProcessor will typically die. After a process has been killed
        checkShutdown()

        # prepare the outbuffer (LAME)
        output = [line + '\n' for line in self.outBuf.getvalue().split('\n')]

        return output, self.returnCode
Esempio n. 15
0
    def updateLog(self):
        """ Log ticker """
        if Hellanzb.DAEMONIZE or Hellanzb.DISABLE_SCROLLER:
            return

        ACODE = Hellanzb.ACODE
        if self.currentLog != None:
            # Kill previous lines,
            if Hellanzb.DISABLE_ANSI:
                currentLog = '\n'
            else:
                currentLog = Hellanzb.ACODE.moveUp(self.maxCount)
        else:
            # unless we have just began logging. and in that case, explicitly log the
            # first message
            currentLog = ''

        # Log information we want to prefix the scroll (so it stays on the screen)
        if len(self.scrollHeaders) > 0:
            scrollHeader = ''
            for message in self.scrollHeaders:
                message = NEWLINE_RE.sub(ACODE.KILL_LINE + '\n', message)
                scrollHeader = '%s%s%s\n' % (scrollHeader, message,
                                             ACODE.KILL_LINE)

            currentLog = '%s%s' % (currentLog, scrollHeader)

        # listing sorted via heapq
        heap = self.segments[:]
        sortedSegments = []
        colorCount = self.connectionCounts.copy()
        try:
            while True:
                p, segment, color = heapq.heappop(heap)
                colorCount[color] -= 1
                sortedSegments.append((segment, color))
        except IndexError:
            pass

        lastSegment = None
        i = 0
        for segment, color in sortedSegments:
            i += 1
            if self.maxCount > 9:
                prettyId = str(i).zfill(2)
            else:
                prettyId = str(i)

            # Determine when we've just found the real file name, then use that as the
            # show name
            try:
                if segment.nzbFile.showFilenameIsTemp == True and segment.nzbFile.filename != None:
                    segment.nzbFile.showFilename = segment.nzbFile.filename
                    segment.nzbFile.showFilenameIsTemp = False
            except AttributeError, ae:
                from Hellanzb.Log import debug
                debug('ATTRIBUTE ERROR: ' + str(ae) + ' num: ' + str(segment.number) + \
                      ' duh: ' + str(segment.articleData))
                pass

            connectionPrefix = color + '[' + ACODE.RESET + '%s' + \
                                color + ']' + ACODE.RESET
            prefix = connectionPrefix % prettyId
            if lastSegment != None and lastSegment.nzbFile == segment.nzbFile:
                # 57 line width -- approximately 80 - 5 (prefix) - 18 (max suffix)
                currentLog = '%s%s %s%s' % (currentLog, prefix,
                                            rtruncate(
                                                segment.nzbFile.showFilename,
                                                length=57), ACODE.KILL_LINE)
            else:
                currentLog = '%s%s %s - %s%2d%%%s%s @ %s%s%.1fKB/s%s' % \
                    (currentLog, prefix, rtruncate(segment.nzbFile.showFilename,
                                                        length = 57), ACODE.F_DGREEN,
                     segment.nzbFile.downloadPercentage, ACODE.RESET, ACODE.F_DBLUE,
                     ACODE.RESET, ACODE.F_DRED, segment.nzbFile.getCurrentRate(),
                     ACODE.KILL_LINE)

            currentLog = '%s\n' % currentLog

            lastSegment = segment
Esempio n. 16
0
    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return

    try:
        new = termios.tcgetattr(fd)
    except Exception, e:
        debug('stdinEchoOn error', e)
        return

    new[3] = new[3] & ~termios.ECHO  # 3 == 'lflags'
    try:
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        debug('stdinEchoOff - OFF')
    except Exception, e:
        debug('stdinEchoOff error', e)


def stdinEchoOn():
    """ ECHO ON standard input """
    if not termios or getattr(Hellanzb, 'DAEMONIZE', False) \
            or getattr(Hellanzb, 'DISABLE_SCROLLER', False):
        return

    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return
Esempio n. 17
0
    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return

    try:
        new = termios.tcgetattr(fd)
    except Exception, e:
        debug('stdinEchoOn error', e)
        return

    new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
    try:
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        debug('stdinEchoOff - OFF')
    except Exception, e:
        debug('stdinEchoOff error', e)
    
def stdinEchoOn():
    """ ECHO ON standard input """
    if not termios or getattr(Hellanzb, 'DAEMONIZE', False) \
            or getattr(Hellanzb, 'DISABLE_SCROLLER', False):
        return
    
    from Hellanzb.Log import debug
    try:
        fd = sys.stdin.fileno()
    except:
        return
Esempio n. 18
0
    def updateLog(self):
        """ Log ticker """
        if Hellanzb.DAEMONIZE or Hellanzb.DISABLE_SCROLLER:
            return
        
        ACODE = Hellanzb.ACODE
        if self.currentLog != None:
            # Kill previous lines,
            if Hellanzb.DISABLE_ANSI:
                currentLog = '\n'
            else:
                currentLog = Hellanzb.ACODE.moveUp(self.maxCount)
        else:
            # unless we have just began logging. and in that case, explicitly log the
            # first message
            currentLog = ''

        # Log information we want to prefix the scroll (so it stays on the screen)
        if len(self.scrollHeaders) > 0:
            scrollHeader = ''
            for message in self.scrollHeaders:
                message = NEWLINE_RE.sub(ACODE.KILL_LINE + '\n', message)
                scrollHeader = '%s%s%s\n' % (scrollHeader, message, ACODE.KILL_LINE)
                
            currentLog = '%s%s' % (currentLog, scrollHeader)

        # listing sorted via heapq
        heap = self.segments[:]
        sortedSegments = []
        colorCount = self.connectionCounts.copy()
        try:
            while True:
                p, segment, color = heapq.heappop(heap)
                colorCount[color] -= 1
                sortedSegments.append((segment, color))
        except IndexError:
            pass

        lastSegment = None
        i = 0
        for segment, color in sortedSegments:
            i += 1
            if self.maxCount > 9:
                prettyId = str(i).zfill(2)
            else:
                prettyId = str(i)
            
            # Determine when we've just found the real file name, then use that as the
            # show name
            try:
                if segment.nzbFile.showFilenameIsTemp == True and segment.nzbFile.filename != None:
                    segment.nzbFile.showFilename = segment.nzbFile.filename
                    segment.nzbFile.showFilenameIsTemp = False
            except AttributeError, ae:
                from Hellanzb.Log import debug
                debug('ATTRIBUTE ERROR: ' + str(ae) + ' num: ' + str(segment.number) + \
                      ' duh: ' + str(segment.articleData))
                pass

            connectionPrefix = color + '[' + ACODE.RESET + '%s' + \
                                color + ']' + ACODE.RESET
            prefix = connectionPrefix % prettyId
            if lastSegment != None and lastSegment.nzbFile == segment.nzbFile:
                # 57 line width -- approximately 80 - 5 (prefix) - 18 (max suffix)
                currentLog = '%s%s %s%s' % (currentLog, prefix,
                                                 rtruncate(segment.nzbFile.showFilename,
                                                           length = 57), ACODE.KILL_LINE)
            else:
                currentLog = '%s%s %s - %s%2d%%%s%s @ %s%s%.1fKB/s%s' % \
                    (currentLog, prefix, rtruncate(segment.nzbFile.showFilename,
                                                        length = 57), ACODE.F_DGREEN,
                     segment.nzbFile.downloadPercentage, ACODE.RESET, ACODE.F_DBLUE,
                     ACODE.RESET, ACODE.F_DRED, segment.nzbFile.getCurrentRate(),
                     ACODE.KILL_LINE)

            currentLog = '%s\n' % currentLog

            lastSegment = segment