Esempio n. 1
0
File: app.py Progetto: madjar/yoppi
 def scan(self, min_ip, max_ip, verbose=1):
     range = IPRange(min_ip, max_ip)
     found = 0
     for ip in range:
         address = str(ip)
         try:
             ftp = ftplib.FTP(timeout=self.timeout)
             ftp.connect(address)
             ftp.close()
         # Server offline
         except IOError:
             try:
                 server = FtpServer.objects.get(address=address)
                 if verbose != 0:
                     name = server.display_name()
                     if not server.online and verbose >= 2:
                         stdout.write(
                                 ugettext("%s is still offline\n") % name)
                     elif server.online and verbose >= 1:
                         stdout.write(
                                 ugettext("%s is now offline\n") % name)
                 server.online = False
                 server.save()
             except FtpServer.DoesNotExist:
                 if verbose >= 3:
                     stdout.write(ugettext("%s didn't respond\n") % address)
         # Server online
         else:
             found += 1
             try:
                 server = FtpServer.objects.get(address=address)
                 if verbose != 0:
                     name = server.display_name()
                     if server.online and verbose >= 2:
                         stdout.write(
                                 ugettext("%s is still online\n") % name)
                     elif not server.online and verbose >= 1:
                         stdout.write(
                                 ugettext("%s is now online\n") % name)
                 server.online = True
                 server.last_online = timezone.now()
                 server.save()
             except FtpServer.DoesNotExist:
                 if verbose >= 1:
                     stdout.write(
                             ugettext("discovered new server at %s\n") %
                                     address)
                 server = FtpServer(
                     address=address, name=self._defaultServerName(address),
                     online=True, last_online=timezone.now())
                 server.save()
     return found
Esempio n. 2
0
 def _scan_address(self, address, ftp_object=None):
     if isinstance(address, IP):
         address = str(address)
     elif not isinstance(address, str):
         raise TypeError("_scan_address expected IP or str, got %s" % type(address))
     if ftp_online(address, self.timeout):
         try:
             if not ftp_object:
                 ftp_object = FtpServer.objects.get(address=address)
             if logger.isEnabledFor(logging.WARN):
                 name = ftp_object.display_name()
                 if ftp_object.online:
                     logger.info(ugettext(u"%s is still online"), name)
                 else:
                     logger.warn(ugettext(u"%s is now online"), name)
             ftp_object.online = True
             ftp_object.last_online = timezone.now()
             ftp_object.save()
         except FtpServer.DoesNotExist:
             logger.warn(ugettext(u"discovered new server at %s\n"), address)
             server = FtpServer(address=address, online=True, last_online=timezone.now())
             server.save()
         return True
     else:
         try:
             if not ftp_object:
                 ftp_object = FtpServer.objects.get(address=address)
             if logger.isEnabledFor(logging.WARN):
                 name = ftp_object.display_name()
                 if not ftp_object.online:
                     logger.info(ugettext(u"%s is still offline"), name)
                 else:
                     logger.warn(ugettext(u"%s is now offline"), name)
             ftp_object.online = False
             ftp_object.save()
         except FtpServer.DoesNotExist:
             logger.debug(ugettext(u"%s didn't respond"), address)
         return False
Esempio n. 3
0
 def test_time_format(self):
     self.assertEqual(FtpServer._format_duration(5), u"5 seconds")
     self.assertEqual(FtpServer._format_duration(-5), u"just now")
     self.assertEqual(FtpServer._format_duration(0), u"just now")
     self.assertEqual(FtpServer._format_duration(
             2 * 60 + 8),
             u"2 minutes")
     self.assertEqual(FtpServer._format_duration(
             (23 * 60 + 18) * 60 + 54),
             u"23 hours")
     self.assertEqual(FtpServer._format_duration(
             ((4 * 24 + 3) * 60 + 18) * 60 + 54),
             u"4 days")
     self.assertEqual(FtpServer._format_duration(
             ((1024 * 24 + 3) * 60 + 18) * 60 + 54),
             u"146 weeks")
Esempio n. 4
0
File: app.py Progetto: madjar/yoppi
def ServerIndexingLock(address, name=None):
    # Try to create a FtpServer
    try:
        server = FtpServer(
                address=address, name=name,
                online=True, last_online=timezone.now(),
                indexing=timezone.now())
        server.save(force_insert=True)
    # It already exists -- try to update it
    except IntegrityError:
        # Try to lock it
        if (FtpServer.objects.filter(indexing=None, address=address)
                .update(indexing=timezone.now()) == 0):
            raise ServerAlreadyIndexing(address)
        else:
            server = FtpServer.objects.get(address=address)
            server.online = True
            server.last_online = timezone.now()
            server.indexing = timezone.now()
            server.save()

    try:
        yield server
    finally:
        server.indexing = None
        server.save()