Ejemplo n.º 1
0
 def doInitInterfaces(self, *args, **kwargs):
     if bpio.Android():
         lg.close_intercepted_log_file()
         lg.open_intercepted_log_file(
             os.path.join(settings.LogsDir(), 'android.log'))
     if _Debug:
         lg.out(_DebugLevel, 'initializer.doInitInterfaces')
     if settings.enableFTPServer():
         try:
             from interface import ftp_server
             ftp_server.init()
         except:
             lg.exc()
     if settings.enableJsonRPCServer():
         try:
             from interface import api_jsonrpc_server
             api_jsonrpc_server.init()
         except:
             lg.exc()
     if settings.enableRESTHTTPServer():
         try:
             from interface import api_rest_http_server
             api_rest_http_server.init(
                 port=settings.getRESTHTTPServerPort())
         except:
             lg.exc()
     if settings.enableWebSocketServer():
         try:
             from interface import api_web_socket
             api_web_socket.init(port=settings.getWebSocketServerPort())
         except:
             lg.exc()
     reactor.callLater(0, self.automat,
                       'init-interfaces-done')  # @UndefinedVariable
Ejemplo n.º 2
0
def init():
    global _WSGIListener
    global _WSGIPort
    result = Deferred()
    if _Debug:
        lg.out(_DebugLevel, 'control.init')
    request_update()
    if _WSGIListener:
        if _Debug:
            lg.out(_DebugLevel, '    SKIP listener already exist')
        result.callback(0)
        return result

    try:
        import django


#         ver = django.get_version()
#         if not ver.startswith('1.7'):
#             if _Debug:
#                 lg.out(_DebugLevel, '    Django version must be 1.7, skip!')
#             result.callback(0)
#             return result
    except:
        lg.exc()
        result.callback(0)
        return result

    if _Debug:
        lg.out(_DebugLevel + 6, '    \n' + pprint.pformat(sys.path))

    if _Debug:
        lg.out(
            _DebugLevel,
            '    setting environment DJANGO_SETTINGS_MODULE=web.asite.settings'
        )
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.asite.settings")

    from django.core.wsgi import get_wsgi_application
    from django.conf import settings as django_settings
    from django.core import management
    # from django.contrib.auth.management.commands import changepassword

    if _Debug:
        lg.out(_DebugLevel,
               '    configuring WSGI bridge from Twisted to Django')
    wsgi_handler = get_wsgi_application()
    my_wsgi_handler = MyFakedWSGIHandler(wsgi_handler)
    pool = threadpool.ThreadPool()
    pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    resource = wsgi.WSGIResource(reactor, pool, my_wsgi_handler)
    root = DjangoRootResource(resource)
    root_static_dir = os.path.join(bpio.getExecutableDir(), "web")
    for sub in os.listdir(root_static_dir):
        static_path = os.path.join(root_static_dir, sub, 'static')
        if not os.path.isdir(static_path):
            continue
        node = static.File(static_path)
        root.putChild(sub, node)
        if _Debug:
            lg.out(_DebugLevel,
                   '        added static dir: %s->%s' % (sub, static_path))
        if sub == 'asite':
            admin_path = os.path.join(root_static_dir, sub, 'admin', 'static')
            node.putChild('admin', static.File(admin_path))
            if _Debug:
                lg.out(
                    _DebugLevel,
                    '        added ADMIN static dir: admin->%s' % admin_path)
    site = server.Site(root)
    _WSGIPort = 8080  # TODO: read port num from settings
    if _Debug:
        lg.out(_DebugLevel, '        %s' % my_wsgi_handler)
        lg.out(_DebugLevel, '        %s' % resource)
        lg.out(_DebugLevel, '        %s' % site)

    verbosity = 0
    if lg.is_debug(18):
        verbosity = 3
    if lg.is_debug(12):
        verbosity = 2
    if lg.is_debug(8):
        verbosity = 1

    # lg.out(4, '    running django "flush" command')
    # management.call_command('flush', interactive=False, verbosity=verbosity)

    # lg.out(4, '    running django "createsuperuser" command')
    # management.call_command('createsuperuser',
    #     interactive=False, verbosity=verbosity,
    #     username="******", email="admin@localhost")
    # command = changepassword.Command()
    # command._get_pass = lambda *args: 'admin'
    # command.execute("admin")

    if _Debug:
        lg.out(_DebugLevel, '    running django "syncdb" command')
    management.call_command('syncdb',
                            stdout=open(
                                os.path.join(settings.LogsDir(),
                                             'django-syncdb.log'), 'w'),
                            interactive=False,
                            verbosity=verbosity)

    _ShortPoolPort = 8081  # TODO: read port num from settings
    # shortpool.init(get_update_items, set_updated, _ShortPoolPort)

    if _Debug:
        lg.out(_DebugLevel, '    starting listener: %s' % site)
    result = start_listener(site)
    result.addCallback(lambda portnum: post_init(portnum))

    return result
Ejemplo n.º 3
0
    def doScanAndQueue(self, arg):
        global _ShutdownFlag
        if _Debug:
            lg.out(
                _DebugLevel,
                'data_sender.doScanAndQueue _ShutdownFlag=%r' % _ShutdownFlag)
        if _Debug:
            log = open(os.path.join(settings.LogsDir(), 'data_sender.log'),
                       'w')
            log.write('doScanAndQueue %s\n' % time.asctime())
        if _ShutdownFlag:
            if _Debug:
                log.write('doScanAndQueue _ShutdownFlag is True\n')
            self.automat('scan-done')
            if _Debug:
                log.flush()
                log.close()
            return
        for customer_idurl in contactsdb.known_customers():
            if '' not in contactsdb.suppliers(customer_idurl):
                from storage import backup_matrix
                for backupID in misc.sorted_backup_ids(
                        backup_matrix.local_files().keys(), True):
                    packetsBySupplier = backup_matrix.ScanBlocksToSend(
                        backupID)
                    if _Debug:
                        log.write('%s\n' % packetsBySupplier)
                    for supplierNum in packetsBySupplier.keys():
                        supplier_idurl = contactsdb.supplier(
                            supplierNum, customer_idurl=customer_idurl)
                        if not supplier_idurl:
                            lg.warn('?supplierNum? %s for %s' %
                                    (supplierNum, backupID))
                            continue
                        for packetID in packetsBySupplier[supplierNum]:
                            backupID_, _, supplierNum_, _ = packetid.BidBnSnDp(
                                packetID)
                            if backupID_ != backupID:
                                lg.warn('?backupID? %s for %s' %
                                        (packetID, backupID))
                                continue
                            if supplierNum_ != supplierNum:
                                lg.warn('?supplierNum? %s for %s' %
                                        (packetID, backupID))
                                continue
                            if io_throttle.HasPacketInSendQueue(
                                    supplier_idurl, packetID):
                                if _Debug:
                                    log.write(
                                        '%s already in sending queue for %s\n'
                                        % (packetID, supplier_idurl))
                                continue
                            if not io_throttle.OkToSend(supplier_idurl):
                                if _Debug:
                                    log.write('ok to send %s ? - NO!\n' %
                                              supplier_idurl)
                                continue
                            customerGlobalID, pathID = packetid.SplitPacketID(
                                packetID)
                            # tranByID = gate.transfers_out_by_idurl().get(supplier_idurl, [])
                            # if len(tranByID) > 3:
                            #     log.write('transfers by %s: %d\n' % (supplier_idurl, len(tranByID)))
                            #     continue
                            customerGlobalID, pathID = packetid.SplitPacketID(
                                packetID)
                            filename = os.path.join(
                                settings.getLocalBackupsDir(),
                                customerGlobalID,
                                pathID,
                            )
                            if not os.path.isfile(filename):
                                if _Debug:
                                    log.write('%s is not a file\n' % filename)
                                continue
                            if io_throttle.QueueSendFile(
                                    filename,
                                    packetID,
                                    supplier_idurl,
                                    my_id.getLocalID(),
                                    self._packetAcked,
                                    self._packetFailed,
                            ):
                                if _Debug:
                                    log.write(
                                        'io_throttle.QueueSendFile %s\n' %
                                        packetID)
                            else:
                                if _Debug:
                                    log.write(
                                        'io_throttle.QueueSendFile FAILED %s\n'
                                        % packetID)
                            # lg.out(6, '  %s for %s' % (packetID, backupID))
                            # DEBUG
                            # break

        self.automat('scan-done')
        if _Debug:
            log.flush()
            log.close()