Exemple #1
0
def main():
    lg.set_debug_level(24)

    # TEST
    # call with parameters like that:
    # python raidworker.py C:\temp\joomla2.sql ecc/7x7 myID_ABC 1234 c:\temp\somedir
    tasks = {}

    def _cb(cmd, taskdata, result):
        print 'DONE!', cmd, taskdata, result
        tasks.pop(taskdata[3])
        if len(tasks) == 0:
            reactor.stop()
        else:
            print len(tasks), 'more'

    def _add(blocknum):
        tasks[blocknum] = (sys.argv[1], sys.argv[2], sys.argv[3], blocknum, sys.argv[5])
        raid_worker.A('new-task',
                      ('make', (sys.argv[1], sys.argv[2], sys.argv[3], blocknum, sys.argv[5]),
                       _cb))
    from system import bpio
    bpio.init()
    lg.set_debug_level(20)
    from raid import raid_worker
    reactor.callWhenRunning(raid_worker.A, 'init')
    start_block_num = int(sys.argv[4])
    reactor.callLater(0.01, _add, start_block_num)
    reactor.callLater(0.02, _add, start_block_num + 1)
    reactor.callLater(0.03, _add, start_block_num + 2)
    reactor.callLater(0.04, _add, start_block_num + 3)
    reactor.callLater(0.05, _add, start_block_num + 4)
    reactor.run()
Exemple #2
0
def main():
    import pprint

    lg.set_debug_level(14)
    if sys.argv.count("list"):
        maps = lst()
        for itm in maps:
            pprint.pprint(itm)
    elif sys.argv.count("info"):
        locip, extip, maps = info()
        pprint.pprint(locip)
        pprint.pprint(extip)
        for itm in maps:
            pprint.pprint(itm)
    elif sys.argv.count("add"):
        print add(sys.argv[2], "TCP")
    elif sys.argv.count("del"):
        print dlt(sys.argv[2], "TCP")
    elif sys.argv.count("update"):
        bpio.init()
        settings.init()
        init()
        pprint.pprint(update(sys.argv[2]))
    elif sys.argv.count("clear"):
        print clear()
    else:
        print "usage:"
        print "run_upnpc.py info"
        print "run_upnpc.py list"
        print "run_upnpc.py add [port]"
        print "run_upnpc.py del [port]"
        print "run_upnpc.py update [port]"
        print "run_upnpc.py clear"
Exemple #3
0
def main():
    bpio.init()
    lg.set_debug_level(18)
    listnport = int(sys.argv[1])

    def received(dgrm, addr):
        send_command(listnport, CMD_ALIVE, 'ok', addr)

    def go(x, port):
        print 'go', x
        l = listen(port)
        l.protocol.add_callback(received)

    def restart(port):
        print 'restart'
        if listener(port):
            close(port).addCallback(go, port)
        else:
            go(None, port)

    def ping(fromport, toaddr):
        print 'ping'
        send_command(fromport, CMD_PING, 'ping', toaddr)
    if len(sys.argv) > 2:
        addr = sys.argv[2].split(':')
        addr = (addr[0], int(addr[1]))
        listen(listnport)
        task.LoopingCall(ping, listnport, addr).start(1, False)
    else:
        restart(listnport)
        # task.LoopingCall(restart, listnport).start(5)
    reactor.run()
def test1():
    lg.set_debug_level(20)
    bpio.init()
    settings.init()
    update_sheduler()
    #SetLocalDir('c:\\Program Files\\\xc4 \xd8 \xcd')
    # download_and_replace_starter()
    reactor.run()
def main():
    def _cb(cmd, taskdata, result):
        print cmd, taskdata, result
    bpio.init()
    lg.set_debug_level(20)
    reactor.callWhenRunning(A, 'init')
    reactor.callLater(0.5, A, 'new-task', ('make', _cb, ('sdfsdf', '45', '324', '45')))
    reactor.run()
def main():
    from twisted.internet import reactor
    lg.set_debug_level(24)
    bpio.init()
    settings.init()
    dht_service.init(settings.getDHTPort())
    dht_service.connect()
    udp.listen(settings.getUDPPort())
    A('start', settings.getUDPPort())
    reactor.run()
Exemple #7
0
def main():
    bpio.init()
    settings.init()
    lg.set_debug_level(20)
    reactor.addSystemEventTrigger('before', 'shutdown',
                                  A().automat, 'shutdown')
    reactor.callWhenRunning(A, 'init',
                            (settings.getIdServerWebPort(), settings.getIdServerTCPPort()))
    reactor.callLater(0, A, 'start')
    reactor.run()
    lg.out(2, 'reactor stopped, EXIT')
def main():
    bpio.init()
    settings.init()
    lg.set_debug_level(20)
    from twisted.internet import reactor
    if len(sys.argv) > 2:
        args = (sys.argv[1], sys.argv[2])
    else:
        args = (sys.argv[1])
    reactor.callWhenRunning(A, 'start', args)
    reactor.run()
Exemple #9
0
def update():
    """
    A good way to check all things - load and sign again.
    """
    from userid import my_id
    bpio.init()
    settings.init()
    src = bpio.ReadTextFile(settings.LocalIdentityFilename())
    my_id.setLocalIdentity(identity(xmlsrc=src))
    my_id.getLocalIdentity().sign()
    my_id.saveLocalIdentity()
    print my_id.getLocalIdentity().serialize()
Exemple #10
0
def main():
    from interface import api
    from main import settings
    from main import bpmain
    from system import bpio
    from services import driver
    lg.open_log_file('test_api.log')
    lg.set_debug_level(20)
    lg.life_begins()
    lg._NoOutput = True
    bpio.init()
    bpmain.init()
    reactor.callWhenRunning(run_tests)
    reactor.callLater(60, api.stop)
    bpmain.run_twisted_reactor()
    bpmain.shutdown()
Exemple #11
0
def main():
    if len(sys.argv) <= 1:
        print 'usage:'
        print '    natpunch.py server [min port] [max port]'
        print '    natpunch.py listen [local port] [servers list filename] [incoming connections filename]'
        print '    natpunch.py connect [local port] [remote IP] [servers list file] [min port] [max port]'
        return

    lg.set_debug_level(24)
    bpio.init()

    if sys.argv[1] == 'server':
        min_port = int(sys.argv[2])
        max_port = int(sys.argv[3])
        for port_num in range(min_port, max_port + 1):
            udp.listen(port_num)
            udp.proto(port_num).add_callback(lambda d, a: datagram_received(d, a, port_num))

    elif sys.argv[1] == 'listen':
        port_num = int(sys.argv[2])
        udp.listen(port_num)
        udp.proto(port_num).add_callback(lambda d, a: datagram_received(d, a, port_num))
        servers = []
        for line in open(sys.argv[3]).read().split('\n'):
            addr = line.strip().split(':')
            addr[1] = int(addr[1])
            servers.append(tuple(addr))
        listen(port_num, servers, sys.argv[4])

    elif sys.argv[1] == 'connect':
        port_num = int(sys.argv[2])
        remote_ip = sys.argv[3]
        udp.listen(port_num)
        udp.proto(port_num).add_callback(lambda d, a: datagram_received(d, a, port_num))
        servers = []
        for line in open(sys.argv[4]).read().split('\n'):
            addr = line.strip().split(':')
            addr[1] = int(addr[1])
            servers.append(tuple(addr))
        min_port = int(sys.argv[5])
        max_port = int(sys.argv[6])
        connect(port_num, remote_ip, servers, min_port, max_port)

    reactor.run()
Exemple #12
0
def main():
    bpio.init()
    settings.init()
    lg.set_debug_level(18)
    (options, args) = parseCommandLine()
    init(options.udpport, options.dhtdb)
    connect()
    if len(args) == 0:
        pass
    elif len(args) > 0:

        def _r(x):
            print x
            reactor.stop()

        cmd = args[0]
        if cmd == "get":
            get_value(args[1]).addBoth(_r)
        elif cmd == "set":
            set_value(args[1], args[2]).addBoth(_r)
        elif cmd == "find":
            find_node(key_to_hash(args[1])).addBoth(_r)
    reactor.run()
Exemple #13
0
def main():
    lg.life_begins()
    bpio.init()
    settings.init()
    misc.init()
    my_id.init()
    identitycache.init()
    from crypt import key

    key.InitMyKey()
    (options, args) = parseCommandLine()
    settings.override("transport.transport-tcp.transport-tcp-port", options.tcpport)
    settings.override("transport.transport-udp.transport-udp-port", options.udpport)
    settings.override("network.network-dht-port", options.dhtport)
    lg.set_debug_level(options.debug)
    tmpfile.init()
    if True:
        import lib.udp

        lib.udp.listen(options.udpport)
        import dht.dht_service

        dht.dht_service.init(options.dhtport)
    reactor.addSystemEventTrigger("before", "shutdown", shutdown)
    init()
    start()
    globals()["num_in"] = 0

    def _in(a, b, c, d):
        lg.out(2, "INBOX %d : %r" % (globals()["num_in"], a))
        globals()["num_in"] += 1
        return True

    callback.insert_inbox_callback(-1, _in)
    if len(args) > 0:
        globals()["num_out"] = 0

        def _s():
            p = signed.Packet(
                commands.Data(),
                my_id.getLocalID(),
                my_id.getLocalID(),
                my_id.getLocalID(),
                bpio.ReadBinaryFile(args[1]),
                args[0],
            )
            outbox(p, wide=True)
            lg.out(2, "OUTBOX %d : %r" % (globals()["num_out"], p))
            globals()["num_out"] += 1

        old_state_changed = transport("udp").state_changed

        def new_state_changed(oldstate, newstate, event, arg):
            old_state_changed(oldstate, newstate, event, arg)
            if newstate == "LISTENING":
                reactor.callLater(1, _s)

        transport("udp").state_changed = new_state_changed
        # t = task.LoopingCall(_s)
        # reactor.callLater(5, t.start, 60, True)
        # reactor.callLater(2, t.stop)

    reactor.run()
def run(opts, args, overDict, pars):
    """
    The entry point, this is called from ``p2p.bpmain`` to process command line arguments.
    """
    print 'Copyright 2014, BitDust. All rights reserved.'
    
    if overDict:
        settings.override_dict(overDict)
    bpio.init()
    settings.init()
    if not opts or opts.debug is None:
        lg.set_debug_level(0)

    appList = bpio.find_process([
        'bitdust.exe',
        'bpmain.py',
        'bitdust.py',
        'regexp:^/usr/bin/python.*bitdust.*$',
        ])
    running = len(appList) > 0
   
    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()
    
    #---help---
    if cmd in ['help', 'h']:
        from main import help
        if len(args) >= 2 and args[1].lower() == 'schedule':
            print help.schedule_format()
        elif len(args) >= 2 and args[1].lower() == 'settings':
            print config.conf().print_all()
        else:
            print help.help()
            print pars.format_option_help()
        return 0
    
    #---backup---
    elif cmd in ['backup', 'backups', 'bk']:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_backups(opts, args, overDict)

    #---restore---
    elif cmd in ['restore', 're']:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_restore(opts, args, overDict)

    #---schedule---
    elif cmd in ['schedule', 'shed', 'sched', 'sh']:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_schedule(opts, args, overDict)

    #---suppliers---
    elif cmd in [ 'suppliers', 'supplier', 'sup', 'supp', 'sp', ]:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_suppliers(opts, args, overDict)
    
    #---customers---
    elif cmd in [ 'customers', 'customer', 'cus', 'cust', 'cs', ]:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_customers(opts, args, overDict)

    #---register---
    elif cmd == 'register':
        if running:
            print 'BitDust already started.\n'
            return 0
        return cmd_register(opts, args, overDict)

    #---recover---
    elif cmd == 'recover':
        if running:
            print 'BitDust already started.\n'
            return 0
        return cmd_recover(opts, args, overDict)

    #---key---
    elif cmd == 'key':
        return cmd_key(opts, args, overDict)

    #---stats---
    elif cmd in [ 'stats', 'st' ]:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_stats(opts, args, overDict)

    #---version---
    elif cmd in [ 'version', 'v', 'ver' ]:
        ver = bpio.ReadTextFile(settings.VersionNumberFile()).strip()
        chksum = bpio.ReadTextFile(settings.CheckSumFile()).strip()
        repo, location = misc.ReadRepoLocation()
        print 'checksum:   ', chksum 
        print 'version:    ', ver
        print 'repository: ', repo
        print 'location:   ', location
        return 0

    #---states---
    elif cmd in [ 'states', 'sta', 'automats', 'auto' ]:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_states(opts, args, overDict)
    
    #---cache---
    elif cmd in [ 'cache' ]:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_cache(opts, args, overDict)

    #---reconnect---
    elif cmd in [ 'reconnect', ]:
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_reconnect(opts, args, overDict)
        
    #---set---
    elif cmd in [ 'set', 'setting', 'settings', 'conf', 'config', 'configs', 'option', 'options', ]:
        if len(args) == 1 or args[1].lower() in [ 'help', '?' ]:
            from main import help
            print help.settings_help()
            return 0
        if not running:
            cmd_set_directly(opts, args, overDict)
            return 0
        return cmd_set_request(opts, args, overDict)
    
    #---memory---
    elif cmd == 'memory':
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_memory(opts, args, overDict)
    
    #---money---
    elif cmd == 'money':
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_money(opts, args, overDict)

    #---storage---
    elif cmd == 'storage':
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_storage(opts, args, overDict)
    
    #---message---
    elif cmd == 'msg' or cmd == 'message' or cmd == 'messages':
        if not running:
            print 'BitDust is not running at the moment\n'
            return 0
        return cmd_message(opts, args, overDict)

    #---integrate---
    elif cmd == 'integrate':
        return cmd_integrate(opts, args, overDict)
    
#    elif cmd == 'uninstall':
#        return cmd_uninstall(opts, args, overDict)
    
    return 2
Exemple #15
0
def run(opts, args, pars=None, overDict=None): 
    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()

    from system import bpio
    bpio.init()

    #---start---
    if cmd == '' or cmd == 'start' or cmd == 'go' or cmd == 'run':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
            ])
        if len(appList) > 0:
            print_text('BitDust already started, found another process: %s' % str(appList))
            return 0
        return run_now(opts, args)

    #---detach---
    elif cmd == 'detach':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
            ])
        if len(appList) > 0:
            print_text('main BitDust process already started: %s' % str(appList))
            return 0
        from lib import misc
        print_text('run and detach main BitDust process')
        result = misc.DoRestart(detach=True)
        try:
            result = result.pid
        except:
            pass
        print_text(result)
        return 0

    #---restart---
    elif cmd == 'restart':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
            ])
        if len(appList) == 0:
            return run_now()
        print_text('found main BitDust process: %s, sending "restart" command ... ' % str(appList), '')
        def done(x):
            print_text('DONE\n', '')
            from twisted.internet import reactor
            if reactor.running and not reactor._stopped:
                reactor.stop()
        def failed(x):
            print_text('FAILED, killing previous process and do restart\n', '')
            try:
                kill()
            except:
                print_exception()
            from twisted.internet import reactor
            from lib import misc
            reactor.addSystemEventTrigger('after','shutdown', misc.DoRestart)
            reactor.stop()
        try:
            from twisted.internet import reactor
            call_xmlrpc_method('restart').addCallbacks(done, failed)
            reactor.run()
        except:
            print_exception()
            return 1
        return 0

    #---show---
    elif cmd == 'show' or cmd == 'open':
        appList_bpgui = bpio.find_process([
            'bpgui.exe',
            'bpgui.py',
            ])
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
            ])
        if len(appList_bpgui) > 0:
            if len(appList) == 0:
                for pid in appList_bpgui:
                    bpio.kill_process(pid)
            else:
                print_text('BitDust GUI already opened, found another process: %s' % str(appList))
                return 0
        if len(appList) == 0:
            from lib import misc
            print_text('run and detach main BitDust process')
            result = misc.DoRestart('show', detach=True)
            try:
                result = result.pid
            except:
                pass
            print_text(result)
            return 0
        print_text('found main BitDust process: %s, sending command "show" to start the GUI\n' % str(appList))
        call_xmlrpc_method('show')
        return 0

    #---stop---
    elif cmd == 'stop' or cmd == 'kill' or cmd == 'shutdown':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
            ])
        if len(appList) > 0:
            print_text('found main BitDust process: %s, sending command "exit"' % str(appList))
            try:
                from twisted.internet import reactor
                call_xmlrpc_method('stop').addBoth(wait_then_kill)
                reactor.run()
                return 0
            except:
                print_exception()
                ret = kill()
                return ret
        else:
            print_text('BitDust is not running at the moment')
            return 0

    #---help---
    elif cmd in ['help', 'h', 'hlp', '?']:
        from main import help
        if len(args) >= 2 and args[1].lower() == 'schedule':
            print_text(help.schedule_format())
        elif len(args) >= 2 and args[1].lower() == 'settings':
            # from main import settings
            # settings.uconfig().print_all()
            from main import config
            for k in config.conf().listAllEntries():
                print k, config.conf().getData(k)
        else:
            print_text(help.help())
            print_text(pars.format_option_help())
        return 0

    appList = bpio.find_process([
        'bitdust.exe',
        'bpmain.py',
        'bitdust.py',
        'regexp:^/usr/bin/python.*bitdust.*$',
        ])
    running = len(appList) > 0
    overDict = override_options(opts, args)

    #---set---
#    if cmd == 'set':
#        if len(args) == 1 or args[1].lower() in [ 'help', '?' ]:
#            from main import help
#            print_text(help.settings_help())
#            return 0
#        if not running:
#            cmd_set_directly(opts, args, overDict)
#            return 0
#        return cmd_set_request(opts, args, overDict)
    
    #---backup---
    if cmd in ['backup', 'backups', 'bk']:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_backups(opts, args, overDict)

    #---restore---
    elif cmd in ['restore', 're']:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_restore(opts, args, overDict)

    #---messages---
    elif cmd == 'msg' or cmd == 'message' or cmd == 'messages':
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_message(opts, args, overDict)
    
    #---friends---
    elif cmd == 'friend' or cmd == 'friends' or cmd == 'buddy':
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_friend(opts, args, overDict)

    #---integrate---
    elif cmd == 'integrate':
        return cmd_integrate(opts, args, overDict)

    #---schedule---
    elif cmd in ['schedule', 'shed', 'sched', 'sh']:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_schedule(opts, args, overDict)



    #---suppliers---
#    elif cmd in [ 'suppliers', 'supplier', 'sup', 'supp', 'sp', ]:
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_suppliers(opts, args, overDict)
    
    #---customers---
#    elif cmd in [ 'customers', 'customer', 'cus', 'cust', 'cs', ]:
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_customers(opts, args, overDict)

    #---register---
#    elif cmd == 'register':
#        if running:
#            print_text('BitDust already started.\n')
#            return 0
#        return cmd_register(opts, args, overDict)

    #---recover---
#    elif cmd == 'recover':
#        if running:
#            print_text('BitDust already started.\n')
#            return 0
#        return cmd_recover(opts, args, overDict)

    #---key---
#    elif cmd == 'key':
#        return cmd_key(opts, args, overDict)

    #---stats---
#    elif cmd in [ 'stats', 'st' ]:
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_stats(opts, args, overDict)

    #---version---
    elif cmd in [ 'version', 'v', 'ver' ]:
        from main import settings
        ver = bpio.ReadTextFile(settings.VersionNumberFile()).strip()
        chksum = bpio.ReadTextFile(settings.CheckSumFile()).strip()
        repo, location = misc.ReadRepoLocation()
        print_text('checksum:   %s' % chksum )
        print_text('version:    %s' % ver)
        print_text('repository: %s' % repo)
        print_text('location:   %s' % location)
        return 0

    #---states---
#    elif cmd in [ 'states', 'sta', 'automats', 'auto' ]:
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_states(opts, args, overDict)
    
    #---cache---
#    elif cmd in [ 'cache' ]:
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_cache(opts, args, overDict)

    #---reconnect---
#    elif cmd in [ 'reconnect', ]:
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_reconnect(opts, args, overDict)
        
    #---memory---
#    elif cmd == 'memory':
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_memory(opts, args, overDict)
    
    #---money---
#    elif cmd == 'money':
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_money(opts, args, overDict)
    
#    elif cmd == 'storage':
#        if not running:
#            print_text('BitDust is not running at the moment\n')
#            return 0
#        return cmd_storage(opts, args, overDict)
    
   
#    elif cmd == 'uninstall':
#        return cmd_uninstall(opts, args, overDict)
    
    return 2
Exemple #16
0
def main():
    from system import bpio
    from storage import backup_tar

    bpio.init()
    settings.init()

    lg.set_debug_level(24)
    lg.life_begins()

    automat.LifeBegins(lg.when_life_begins())
    automat.OpenLogFile(settings.AutomatsLog())

    key.InitMyKey()
    my_id.init()

    sourcePath = sys.argv[1]
    compress_mode = 'none'  # 'gz'
    backupID = sys.argv[2]
    raid_worker.A('init')

    if bpio.pathIsDir(sourcePath):
        backupPipe = backup_tar.backuptardir_thread(sourcePath,
                                                    compress=compress_mode)
    else:
        backupPipe = backup_tar.backuptarfile_thread(sourcePath,
                                                     compress=compress_mode)

    def _bk_done(bid, result):
        from crypt import signed
        customer, remotePath = packetid.SplitPacketID(bid)
        try:
            os.mkdir(
                os.path.join(settings.getLocalBackupsDir(), customer,
                             remotePath + '.out'))
        except:
            pass
        for filename in os.listdir(
                os.path.join(settings.getLocalBackupsDir(), customer,
                             remotePath)):
            filepath = os.path.join(settings.getLocalBackupsDir(), customer,
                                    remotePath, filename)
            payld = bpio.ReadBinaryFile(filepath)
            newpacket = signed.Packet('Data', my_id.getIDURL(),
                                      my_id.getIDURL(), filename, payld,
                                      'http://megafaq.ru/cvps1010.xml')
            newfilepath = os.path.join(settings.getLocalBackupsDir(), customer,
                                       remotePath + '.out', filename)
            bpio.WriteBinaryFile(newfilepath, newpacket.Serialize())

    def _bk_closed(*args, **kwargs):
        # job.automat('fail')
        # del job
        reactor.stop()  # @UndefinedVariable

    def _bk_start():
        job = backup(backupID, backupPipe, blockSize=16 * 1024 * 1024)
        job.finishCallback = _bk_done  # lambda bid, result: _bk_done(bid, result, job)
        job.addStateChangedCallback(_bk_closed, oldstate=None, newstate='DONE')
        reactor.callLater(1, job.automat, 'start')  # @UndefinedVariable

    reactor.callLater(0, _bk_start)  # @UndefinedVariable
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
def run(opts, args, pars=None, overDict=None, executablePath=None):
    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()

    from system import bpio
    bpio.init()

    #---start---
    if cmd == '' or cmd == 'start' or cmd == 'go' or cmd == 'run':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
        ])
        if len(appList) > 0:
            print_text('BitDust already started, found another process: %s' % str(appList))
            return 0
        return run_now(opts, args)

    #---detach---
    elif cmd == 'detach':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
        ])
        if len(appList) > 0:
            print_text('main BitDust process already started: %s' % str(appList))
            return 0
        from lib import misc
        print_text('run and detach main BitDust process')
        result = misc.DoRestart(detach=True)
        try:
            result = result.pid
        except:
            pass
        print_text(result)
        return 0

    #---restart---
    elif cmd == 'restart' or cmd == 'reboot':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
        ])
        if len(appList) == 0:
            return run_now(opts, args)
        ui = False
        if cmd == 'restart':
            ui = True
        print_text('found main BitDust process: %s, sending "restart" command' % str(appList))

        def done(x):
            print_text('DONE\n', '')
            from twisted.internet import reactor
            if reactor.running and not reactor._stopped:
                reactor.stop()

        def failed(x):
            print_text('soft restart FAILED, now killing previous process and do restart')
            try:
                kill()
            except:
                print_exception()
            from twisted.internet import reactor
            from lib import misc
            reactor.addSystemEventTrigger('after', 'shutdown', misc.DoRestart, param='show' if ui else '', detach=True)
            reactor.stop()
        try:
            from twisted.internet import reactor
            call_jsonrpc_method('restart', ui).addCallbacks(done, failed)
            reactor.run()
        except:
            print_exception()
            return 1
        return 0

    #---show---
    elif cmd == 'show' or cmd == 'open':
        appList_bpgui = bpio.find_process([
            'bpgui.exe',
            'bpgui.py',
        ])
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
        ])
        if len(appList_bpgui) > 0:
            if len(appList) == 0:
                for pid in appList_bpgui:
                    bpio.kill_process(pid)
            else:
                print_text('BitDust GUI already opened, found another process: %s' % str(appList))
                return 0
        if len(appList) == 0:
            from lib import misc
            print_text('run and detach main BitDust process')
            result = misc.DoRestart('show', detach=True)
            try:
                result = result.pid
            except:
                pass
            print_text(result)
            return 0
        print_text('found main BitDust process: %s, sending command "show" to start the GUI\n' % str(appList))
        call_jsonrpc_method('show')
        return 0

    #---stop---
    elif cmd == 'stop' or cmd == 'kill' or cmd == 'shutdown':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python.*bitdust.*$',
        ])
        if len(appList) > 0:
            print_text('found main BitDust process: %s, sending command "exit" ... ' % str(appList), '')
            try:
                from twisted.internet import reactor
                call_jsonrpc_method('stop').addBoth(wait_then_kill)
                reactor.run()
                return 0
            except:
                print_exception()
                ret = kill()
                return ret
        else:
            print_text('BitDust is not running at the moment')
            return 0

    #---help---
    elif cmd in ['help', 'h', 'hlp', '?']:
        from main import help
        if len(args) >= 2 and args[1].lower() == 'schedule':
            print_text(help.schedule_format())
        elif len(args) >= 2 and args[1].lower() == 'settings':
            # from main import settings
            # settings.uconfig().print_all()
            from main import config
            for k in config.conf().listAllEntries():
                print k, config.conf().getData(k)
        else:
            print_text(help.help())
            print_text(pars.format_option_help())
        return 0

    appList = bpio.find_process([
        'bitdust.exe',
        'bpmain.py',
        'bitdust.py',
        'regexp:^/usr/bin/python.*bitdust.*$',
    ])
    running = len(appList) > 0
    overDict = override_options(opts, args)

    #---identity---
    if cmd in ['identity', 'id', 'idurl', ]:
        return cmd_identity(opts, args, overDict, running)

    #---key---
    elif cmd == 'key':
        return cmd_key(opts, args, overDict, running, executablePath)

    #---ping---
    if cmd == 'ping' or cmd == 'call' or cmd == 'sendid':
        if len(args) < 1:
            return 2
        tpl = jsontemplate.Template(templ.TPL_RAW)
        return call_jsonrpc_method_template_and_stop('ping', tpl, args[1])

    #---set---
    elif cmd in ['set', 'get', 'conf', 'config', 'option', 'setting', ]:
        if len(args) == 1 or args[1].lower() in ['help', '?']:
            from main import help
            print_text(help.settings_help())
            return 0
        if not running:
            return cmd_set(opts, args, overDict)
        return cmd_set_request(opts, args, overDict)

    #---reconnect---
    if cmd in ['reconnect', 'rejoin', 'connect', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_reconnect(opts, args, overDict)

    #---api---
    elif cmd in ['api', 'call', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_api(opts, args, overDict, executablePath)

    #---messages---
    elif cmd in ['msg', 'message', 'messages', 'chat', 'talk', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_message(opts, args, overDict)

    #---suppliers---
    elif cmd in ['suppliers', 'supplier', 'sup', 'supp', 'sp']:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_suppliers(opts, args, overDict)

    #---customers---
    elif cmd in ['customers', 'customer', 'cus', 'cust', 'cu']:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_customers(opts, args, overDict)

    #---storage---
    elif cmd in ['storage', 'space']:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_storage(opts, args, overDict)

    #---automats---
    elif cmd in ['automats', 'aut', 'states', 'machines', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_automats(opts, args, overDict)

    #---services---
    elif cmd in ['services', 'service', 'svc', 'serv', 'srv', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_services(opts, args, overDict)

    #---friends---
    elif cmd == 'friend' or cmd == 'friends' or cmd == 'buddy' or cmd == 'correspondent' or cmd == 'contact' or cmd == 'peer':
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_friend(opts, args, overDict)

    #---backup---
    elif cmd in ['file', 'files', 'fi', 'fs', 'backup', 'backups', 'bk', 'up', 'upload', 'uploads', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_backup(opts, args, overDict, executablePath)

    #---restore---
    elif cmd in ['restore', 'rest', 'download', 'down', ]:
        if not running:
            print_text('BitDust is not running at the moment\n')
            return 0
        return cmd_restore(opts, args, overDict, executablePath)

    #---version---
    elif cmd in ['version', 'v', 'ver']:
        from main import settings
        from lib import misc
        ver = bpio.ReadTextFile(settings.VersionNumberFile()).strip()
        chksum = bpio.ReadTextFile(settings.CheckSumFile()).strip()
        repo, location = misc.ReadRepoLocation()
        print_text('checksum:   %s' % chksum)
        print_text('version:    %s' % ver)
        print_text('repository: %s' % repo)
        print_text('location:   %s' % location)
        return 0

    #---integrate---
    elif cmd == 'integrate' or cmd == 'alias' or cmd == 'shell':
        return cmd_integrate(opts, args, overDict)

    return 2
Exemple #18
0
def main(executable_path=None):
    """
    THIS IS THE ENTRY POINT OF THE PROGRAM!
    """
    global AppDataDir

    pars = parser()
    (opts, args) = pars.parse_args()
    overDict = override_options(opts, args)

    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()
    #---install---
    if cmd in [
            'deploy',
            'install',
            'venv',
            'virtualenv',
    ]:
        from system import deploy
        return deploy.run(args)

    try:
        from logs import lg
    except:
        dirpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..')))
        # sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..', '..')))
        from distutils.sysconfig import get_python_lib
        sys.path.append(os.path.join(get_python_lib(), 'bitdust'))
        try:
            from logs import lg
        except:
            print('ERROR! can not import working code.  Python Path:')
            print('\n'.join(sys.path))
            return 1

    # init IO module, update locale
    from system import bpio
    bpio.init()

    # sys.excepthook = lg.exception_hook

    if not bpio.isFrozen():
        try:
            from twisted.internet.defer import setDebugging
            setDebugging(True)
            # from twisted.python import log as twisted_log
            # twisted_log.startLogging(sys.stdout)
        except:
            lg.warn('python-twisted is not installed')

    if opts.appdir:
        appdata = opts.appdir
        AppDataDir = appdata

    else:
        curdir = os.getcwd(
        )  # os.path.dirname(os.path.abspath(sys.executable))
        appdatafile = os.path.join(curdir, 'appdata')
        defaultappdata = os.path.join(os.path.expanduser('~'), '.bitdust')
        appdata = defaultappdata
        if os.path.isfile(appdatafile):
            try:
                appdata = os.path.abspath(
                    open(appdatafile, 'rb').read().strip())
            except:
                appdata = defaultappdata
            if not os.path.isdir(appdata):
                appdata = defaultappdata
        AppDataDir = appdata

    # ask to count time for each log line from that moment, not absolute time
    lg.life_begins()
    # try to read debug level value at the early stage - no problem if fail here
    try:
        if cmd == '' or cmd == 'start' or cmd == 'go' or cmd == 'show' or cmd == 'open':
            lg.set_debug_level(
                int(
                    bpio.ReadTextFile(
                        os.path.abspath(
                            os.path.join(appdata, 'config', 'logs',
                                         'debug-level')))))
    except:
        pass

    if opts.no_logs:
        lg.disable_logs()

    #---logpath---
    logpath = os.path.join(appdata, 'logs', 'start.log')
    if opts.output:
        logpath = opts.output

    need_redirecting = False

    if bpio.Windows() and not bpio.isConsoled():
        need_redirecting = True

    if logpath != '':
        lg.open_log_file(logpath)
        lg.out(2, 'bpmain.main log file opened ' + logpath)
        if bpio.Windows() and bpio.isFrozen():
            need_redirecting = True

    if need_redirecting:
        lg.stdout_start_redirecting()
        lg.out(2, 'bpmain.main redirecting started')

    # TODO: temporary solution to record run-time errors
    try:
        if os.path.isfile(os.path.join(appdata, 'logs', 'exception.log')):
            os.remove(os.path.join(appdata, 'logs', 'exception.log'))
    except:
        pass

    if opts.debug or str(opts.debug) == '0':
        lg.set_debug_level(opts.debug)

    # if opts.quite and not opts.verbose:
    #     lg.disable_output()

    if opts.verbose:
        copyright_text()

    lg.out(2, 'bpmain.main started ' + time.asctime())
    lg.out(2, 'bpmain.main args=%s' % str(args))

    #---start---
    if cmd == '' or cmd == 'start' or cmd == 'go':
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        if appList:
            lg.out(
                0, 'BitDust already started, found another process: %s\n' %
                str(appList))
            bpio.shutdown()
            return 0

        UI = ''
        # if cmd == 'show' or cmd == 'open':
        # UI = 'show'
        try:
            ret = run(UI, opts, args, overDict, executable_path)
        except:
            lg.exc()
            ret = 1
        bpio.shutdown()
        return ret

    #---daemon---
    elif cmd == 'detach' or cmd == 'daemon':
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        if len(appList) > 0:
            lg.out(0,
                   'main BitDust process already started: %s\n' % str(appList))
            bpio.shutdown()
            return 0
        from lib import misc
        lg.out(
            0,
            'new BitDust process will be started in daemon mode, finishing current process\n'
        )
        bpio.shutdown()
        result = misc.DoRestart(detach=True)
        if result is not None:
            try:
                result = int(result)
            except:
                try:
                    result = result.pid
                except:
                    pass
        return 0

    #---restart---
    elif cmd == 'restart' or cmd == 'reboot':
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        ui = False
        if len(appList) > 0:
            lg.out(
                0,
                'found main BitDust process: %s, sending "restart" command ... '
                % str(appList), '')

            def done(x):
                lg.out(0, 'DONE\n', '')
                from twisted.internet import reactor
                if reactor.running and not reactor._stopped:
                    reactor.stop()

            def failed(x):
                ok = str(x).count('Connection was closed cleanly') > 0
                from twisted.internet import reactor
                if ok and reactor.running and not reactor._stopped:
                    lg.out(0, 'DONE\n', '')
                    reactor.stop()
                    return
                lg.out(
                    0,
                    'FAILED while killing previous process - do HARD restart\n',
                    '')
                try:
                    kill()
                except:
                    lg.exc()
                from lib import misc
                reactor.addSystemEventTrigger('after',
                                              'shutdown',
                                              misc.DoRestart,
                                              param='show' if ui else '',
                                              detach=True)
                reactor.stop()

            try:
                from twisted.internet import reactor
                # from interface.command_line import run_url_command
                # d = run_url_command('?action=restart', False)
                # from interface import cmd_line
                # d = cmd_line.call_xmlrpc_method('restart', ui)
                from interface import cmd_line_json
                d = cmd_line_json.call_jsonrpc_method('restart', ui)
                d.addCallback(done)
                d.addErrback(failed)
                reactor.run()
                bpio.shutdown()
                return 0
            except:
                lg.exc()
                bpio.shutdown()
                return 1
        else:
            ui = ''
            if cmd == 'restart':
                ui = 'show'
            try:
                ret = run(ui, opts, args, overDict, executable_path)
            except:
                lg.exc()
                ret = 1
            bpio.shutdown()
            return ret

    #---show---
    elif cmd == 'show' or cmd == 'open':
        if not bpio.isGUIpossible():
            lg.out(0, 'BitDust GUI is turned OFF\n')
            bpio.shutdown()
            return 0
        if bpio.Linux() and not bpio.X11_is_running():
            lg.out(0, 'this operating system not supported X11 interface\n')
            bpio.shutdown()
            return 0
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        if len(appList) == 0:
            try:
                ret = run('show', opts, args, overDict, executable_path)
            except:
                lg.exc()
                ret = 1
            bpio.shutdown()
            return ret
        lg.out(
            0,
            'found main BitDust process: %s, start the GUI\n' % str(appList))
        ret = show()
        bpio.shutdown()
        return ret

    #---stop---
    elif cmd == 'stop' or cmd == 'kill' or cmd == 'shutdown':
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        if len(appList) > 0:
            lg.out(
                0,
                'found main BitDust process: %s, sending command "exit" ... ' %
                str(appList), '')
            try:
                from twisted.internet import reactor

                # from interface.command_line import run_url_command
                # url = '?action=exit'
                # run_url_command(url, False).addBoth(wait_then_kill)
                # reactor.run()
                # bpio.shutdown()

                def _stopped(x):
                    lg.out(0, 'BitDust process finished correctly\n')
                    reactor.stop()
                    bpio.shutdown()

                # from interface import cmd_line
                # cmd_line.call_xmlrpc_method('stop').addBoth(_stopped)
                from interface import cmd_line_json
                cmd_line_json.call_jsonrpc_method('stop').addBoth(_stopped)
                reactor.run()
                return 0
            except:
                lg.exc()
                ret = kill()
                bpio.shutdown()
                return ret
        else:
            lg.out(0, 'BitDust is not running at the moment\n')
            bpio.shutdown()
            return 0

    #---command_line---
    from interface import cmd_line_json as cmdln
    ret = cmdln.run(opts, args, pars, overDict, executable_path)
    if ret == 2:
        print(usage_text())
    bpio.shutdown()
    return ret
Exemple #19
0
def main(executable_path=None):
    """
    THIS IS THE ENTRY POINT OF THE PROGRAM!
    """
    global AppDataDir

    import warnings
    warnings.filterwarnings("ignore", message="You do not have a working installation of the service_identity module")

    try:
        from logs import lg
    except:
        dirpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..')))
        # sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..', '..')))
        from distutils.sysconfig import get_python_lib
        sys.path.append(os.path.join(get_python_lib(), 'bitdust'))
        try:
            from logs import lg
        except:
            print 'ERROR! can not import working code.  Python Path:'
            print '\n'.join(sys.path)
            return 1

    # init IO module, update locale
    from system import bpio
    bpio.init()

    # sys.excepthook = lg.exception_hook

    if not bpio.isFrozen():
        from twisted.internet.defer import setDebugging
        setDebugging(True)

    pars = parser()
    (opts, args) = pars.parse_args()

    if opts.appdir:
        appdata = opts.appdir
        AppDataDir = appdata

    else:
        curdir = os.getcwd()  # os.path.dirname(os.path.abspath(sys.executable))
        appdatafile = os.path.join(curdir, 'appdata')
        defaultappdata = os.path.join(os.path.expanduser('~'), '.bitdust')
        appdata = defaultappdata
        if os.path.isfile(appdatafile):
            try:
                appdata = os.path.abspath(open(appdatafile, 'rb').read().strip())
            except:
                appdata = defaultappdata
            if not os.path.isdir(appdata):
                appdata = defaultappdata
        AppDataDir = appdata

    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()

    # ask to count time for each log line from that moment, not absolute time
    lg.life_begins()
    # try to read debug level value at the early stage - no problem if fail here
    try:
        if cmd == '' or cmd == 'start' or cmd == 'go' or cmd == 'show' or cmd == 'open':
            lg.set_debug_level(int(
                bpio._read_data(
                    os.path.abspath(
                        os.path.join(appdata, 'config', 'logs', 'debug-level')))))
    except:
        pass

    if opts.no_logs:
        lg.disable_logs()

    #---logpath---
    logpath = os.path.join(appdata, 'logs', 'start.log')
    if opts.output:
        logpath = opts.output

    need_redirecting = False

    if bpio.Windows() and not bpio.isConsoled():
        need_redirecting = True

    if logpath != '':
        lg.open_log_file(logpath)
        lg.out(2, 'bpmain.main log file opened ' + logpath)
        if bpio.Windows() and bpio.isFrozen():
            need_redirecting = True

    if need_redirecting:
        lg.stdout_start_redirecting()
        lg.out(2, 'bpmain.main redirecting started')

    try:
        if os.path.isfile(os.path.join(appdata, 'logs', 'exception.log')):
            os.remove(os.path.join(appdata, 'logs', 'exception.log'))
    except:
        pass

    if opts.debug or str(opts.debug) == '0':
        lg.set_debug_level(opts.debug)

    # if opts.quite and not opts.verbose:
    #     lg.disable_output()

    if opts.verbose:
        copyright()

    lg.out(2, 'bpmain.main started ' + time.asctime())

    overDict = override_options(opts, args)

    lg.out(2, 'bpmain.main args=%s' % str(args))

    #---start---
    if cmd == '' or cmd == 'start' or cmd == 'go':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python\ +/usr/bin/bitdust.*$',
        ])

        # this is extra protection for Debian release
        # I am not sure how process name can looks on different systems
        # check the process ID from previous start
        # it file exists and we found this PID in the currently running apps - BitDust is working
        # if file not exists we don't want to start if found some other jobs with same name
#        pid = -1
#        try:
#            if bpio.Windows():
#                _data_path = os.path.join(os.environ.get('APPDATA', os.path.join(os.path.expanduser('~'), 'Application Data')), 'BitDust')
#                pid_path = os.path.join(_data_path, 'metadata', 'processid')
#            else:
#                pid_path = os.path.join(appdata, 'metadata', 'processid')
#            if os.path.isfile(pid_path):
#                pid = int(bpio.ReadBinaryFile(pid_path).strip())
#        except:
#            lg.exc()

        if len(appList) > 0:
            lg.out(0, 'BitDust already started, found another process: %s\n' % str(appList))
            bpio.shutdown()
            return 0
        UI = ''
        # if cmd == 'show' or cmd == 'open':
        # UI = 'show'
        try:
            ret = run(UI, opts, args, overDict, executable_path)
        except:
            lg.exc()
            ret = 1
        bpio.shutdown()
        return ret

    #---detach---
    elif cmd == 'detach' or cmd == 'daemon' or cmd == 'background':
        # lg.set_debug_level(20)
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python\ +/usr/bin/bitdust.*$',
        ])
        if len(appList) > 0:
            lg.out(0, 'main BitDust process already started: %s\n' % str(appList))
            bpio.shutdown()
            return 0
        from lib import misc
        # from twisted.internet import reactor
        # def _detach():
        #     result = misc.DoRestart(detach=True)
        #     lg.out(0, 'run and detach main BitDust process: %s' % str(result))
        #     reactor.callLater(2, reactor.stop)
        # reactor.addSystemEventTrigger('after','shutdown', misc.DoRestart, detach=True)
        # reactor.callLater(0.01, _detach)
        # reactor.run()
        lg.out(0, 'main BitDust process started in daemon mode\n')
        bpio.shutdown()
        result = misc.DoRestart(detach=True)
        try:
            result = result.pid
        except:
            pass
        return 0

    #---restart---
    elif cmd == 'restart' or cmd == 'reboot':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python\ +/usr/bin/bitdust.*$',
        ])
        ui = False
        # if cmd == 'restart':
        # ui = True
        if len(appList) > 0:
            lg.out(0, 'found main BitDust process: %s, sending "restart" command ... ' % str(appList), '')

            def done(x):
                lg.out(0, 'DONE\n', '')
                from twisted.internet import reactor
                if reactor.running and not reactor._stopped:
                    reactor.stop()

            def failed(x):
                ok = str(x).count('Connection was closed cleanly') > 0
                from twisted.internet import reactor
                if ok and reactor.running and not reactor._stopped:
                    lg.out(0, 'DONE\n', '')
                    reactor.stop()
                    return
                lg.out(0, 'FAILED while killing previous process - do HARD restart\n', '')
                try:
                    kill()
                except:
                    lg.exc()
                from lib import misc
                reactor.addSystemEventTrigger('after', 'shutdown', misc.DoRestart, param='show' if ui else '', detach=True)
                reactor.stop()
            try:
                from twisted.internet import reactor
                # from interface.command_line import run_url_command
                # d = run_url_command('?action=restart', False)
                # from interface import cmd_line
                # d = cmd_line.call_xmlrpc_method('restart', ui)
                from interface import cmd_line_json
                d = cmd_line_json.call_jsonrpc_method('restart', ui)
                d.addCallback(done)
                d.addErrback(failed)
                reactor.run()
                bpio.shutdown()
                return 0
            except:
                lg.exc()
                bpio.shutdown()
                return 1
        else:
            ui = ''
            if cmd == 'restart':
                ui = 'show'
            try:
                ret = run(ui, opts, args, overDict, executable_path)
            except:
                lg.exc()
                ret = 1
            bpio.shutdown()
            return ret

    #---show---
    elif cmd == 'show' or cmd == 'open':
        from main import settings
        if not bpio.isGUIpossible() and not settings.NewWebGUI():
            lg.out(0, 'BitDust GUI is turned OFF\n')
            bpio.shutdown()
            return 0
        if bpio.Linux() and not bpio.X11_is_running():
            lg.out(0, 'this operating system not supported X11 interface\n')
            bpio.shutdown()
            return 0
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python\ +/usr/bin/bitdust.*$',
        ])
        if not settings.NewWebGUI():
            appList_bpgui = bpio.find_process([
                'bpgui.exe',
                'bpgui.py',
            ])
            if len(appList_bpgui) > 0:
                if len(appList) == 0:
                    for pid in appList_bpgui:
                        bpio.kill_process(pid)
                else:
                    lg.out(0, 'BitDust GUI already opened, found another process: %s\n' % str(appList))
                    bpio.shutdown()
                    return 0
        if len(appList) == 0:
            try:
                ret = run('show', opts, args, overDict, executable_path)
            except:
                lg.exc()
                ret = 1
            bpio.shutdown()
            return ret
        lg.out(0, 'found main BitDust process: %s, start the GUI\n' % str(appList))
        ret = show()
        bpio.shutdown()
        return ret

    #---stop---
    elif cmd == 'stop' or cmd == 'kill' or cmd == 'shutdown':
        appList = bpio.find_process([
            'bitdust.exe',
            'bpmain.py',
            'bitdust.py',
            'regexp:^/usr/bin/python\ +/usr/bin/bitdust.*$',
        ])
        if len(appList) > 0:
            lg.out(0, 'found main BitDust process: %s, sending command "exit" ... ' % str(appList), '')
            try:
                from twisted.internet import reactor
                # from interface.command_line import run_url_command
                # url = '?action=exit'
                # run_url_command(url, False).addBoth(wait_then_kill)
                # reactor.run()
                # bpio.shutdown()

                def _stopped(x):
                    lg.out(0, 'BitDust process finished correctly\n')
                    reactor.stop()
                    bpio.shutdown()
                # from interface import cmd_line
                # cmd_line.call_xmlrpc_method('stop').addBoth(_stopped)
                from interface import cmd_line_json
                cmd_line_json.call_jsonrpc_method('stop').addBoth(_stopped)
                reactor.run()
                return 0
            except:
                lg.exc()
                ret = kill()
                bpio.shutdown()
                return ret
        else:
            lg.out(0, 'BitDust is not running at the moment\n')
            bpio.shutdown()
            return 0

    #---uninstall---
    elif cmd == 'uninstall':
        def do_spawn(x=None):
            from main.settings import WindowsStarterFileName
            starter_filepath = os.path.join(bpio.getExecutableDir(), WindowsStarterFileName())
            lg.out(0, "bpmain.main bitstarter.exe path: %s " % starter_filepath)
            if not os.path.isfile(starter_filepath):
                lg.out(0, "ERROR: %s not found\n" % starter_filepath)
                bpio.shutdown()
                return 1
            cmdargs = [os.path.basename(starter_filepath), 'uninstall']
            lg.out(0, "bpmain.main os.spawnve cmdargs=" + str(cmdargs))
            ret = os.spawnve(os.P_DETACH, starter_filepath, cmdargs, os.environ)
            bpio.shutdown()
            return ret

        def do_reactor_stop_and_spawn(x=None):
            lg.out(0, 'BitDust process finished correctly\n')
            reactor.stop()
            ret = do_spawn()
            bpio.shutdown()
            return ret
        lg.out(0, 'bpmain.main UNINSTALL!')
        if not bpio.Windows():
            lg.out(0, 'This command can be used only under OS Windows.\n')
            bpio.shutdown()
            return 0
        if not bpio.isFrozen():
            lg.out(0, 'You are running BitDust from sources, uninstall command is available only for installable version.\n')
            bpio.shutdown()
            return 0
        appList = bpio.find_process(['bitdust.exe', ])
        if len(appList) > 0:
            lg.out(0, 'found main BitDust process...   ', '')
            try:
                # from twisted.internet import reactor
                # from interface.command_line import run_url_command
                # url = '?action=exit'
                # run_url_command(url).addBoth(do_reactor_stop_and_spawn)
                # reactor.run()
                # bpio.shutdown()
                # from interface import cmd_line
                # cmd_line.call_xmlrpc_method('stop').addBoth(do_reactor_stop_and_spawn)
                from interface import cmd_line_json
                cmd_line_json.call_jsonrpc_method('stop').addBoth(do_reactor_stop_and_spawn)
                reactor.run()
                return 0
            except:
                lg.exc()
        ret = do_spawn()
        bpio.shutdown()
        return ret

    #---command_line---
    # from interface import command_line as cmdln
    # from interface import cmd_line as cmdln
    from interface import cmd_line_json as cmdln
    ret = cmdln.run(opts, args, pars, overDict, executable_path)
    if ret == 2:
        print usage()
    bpio.shutdown()
    return ret
Exemple #20
0
        self.wsgi_resource = wsgi_resource

    def getChild(self, path, request):
        path0 = request.prepath.pop(0)
        request.postpath.insert(0, path0)
        # lg.out(4, 'control.DjangoRootResource.getChild %s' % path0)
        return self.wsgi_resource


class DebugMixin(object):

    def get_context_data(self, **kwargs):
        if 'debug' not in kwargs:
            try:
                kwargs['debug'] = str(pprint.pformat(self.context))
                pprint.pprint(self.context)
            except:
                lg.exc()
        return kwargs

#------------------------------------------------------------------------------

if __name__ == "__main__":
    bpio.init()
    settings.init()
    lg.set_debug_level(20)
    reactor.addSystemEventTrigger('before', 'shutdown', shutdown)
    reactor.callWhenRunning(init)
    reactor.run()
    lg.out(2, 'reactor stopped, EXIT')
Exemple #21
0
def main(executable_path=None, start_reactor=True):
    """
    THE ENTRY POINT
    """
    global AppDataDir

    pars = parser()
    (opts, args) = pars.parse_args()

    if opts.coverage:
        import coverage  # @UnresolvedImport
        cov = coverage.Coverage(config_file=opts.coverage_config)
        cov.start()

    overDict = override_options(opts, args)

    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()

    try:
        from system import deploy
    except:
        dirpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..')))
        from distutils.sysconfig import get_python_lib
        sys.path.append(os.path.join(get_python_lib(), 'bitdust'))
        try:
            from system import deploy
        except:
            print_text('ERROR! can not import working code.  Python Path:')
            print_text('\n'.join(sys.path))
            return 1

    #---install---
    if cmd in [
            'deploy',
            'install',
            'venv',
            'virtualenv',
    ]:
        from system import deploy
        return deploy.run(args)

    if opts.appdir:
        appdata = opts.appdir
        AppDataDir = appdata

    else:
        curdir = os.getcwd()
        appdatafile = os.path.join(curdir, 'appdata')
        defaultappdata = deploy.default_base_dir_portable()
        appdata = defaultappdata
        if os.path.isfile(appdatafile):
            try:
                appdata = os.path.abspath(
                    open(appdatafile, 'rb').read().strip())
            except:
                appdata = defaultappdata
            if not os.path.isdir(appdata):
                appdata = defaultappdata
        AppDataDir = appdata

    #---BitDust Home
    deploy.init_base_dir(base_dir=AppDataDir)

    from logs import lg

    #---init IO module
    from system import bpio
    bpio.init()

    appList = bpio.find_main_process(
        pid_file_path=os.path.join(appdata, 'metadata', 'processid'))

    if bpio.Android():
        lg.close_intercepted_log_file()
        lg.open_intercepted_log_file(
            '/storage/emulated/0/Android/data/org.bitdust_io.bitdust1/files/Documents/.bitdust/logs/android.log'
        )

    # sys.excepthook = lg.exception_hook

    #---init logging
    from twisted.internet.defer import setDebugging
    if _Debug:
        if bpio.isFrozen():
            setDebugging(False)
        else:
            setDebugging(True)
    else:
        setDebugging(False)

    from twisted.logger import globalLogPublisher, LogLevel
    tw_log_observer = TwistedUnhandledErrorsObserver(level=LogLevel.critical)
    globalLogPublisher.addObserver(tw_log_observer)

    #---life begins!
    # ask logger to count time for each log line from that moment, not absolute time
    lg.life_begins()

    # try to read debug level value at the early stage - no problem if fail here
    try:
        if cmd == '' or cmd == 'start' or cmd == 'go' or cmd == 'show' or cmd == 'open':
            lg.set_debug_level(
                int(
                    bpio.ReadTextFile(
                        os.path.abspath(
                            os.path.join(appdata, 'config', 'logs',
                                         'debug-level')))))
    except:
        pass

    if opts.no_logs:
        lg.disable_logs()

    if opts.debug or str(opts.debug) == '0':
        lg.set_debug_level(int(opts.debug))

    #---logpath---
    logpath = None
    if opts.output:
        logpath = opts.output
    else:
        try:
            os.makedirs(os.path.join(appdata, 'logs'), exist_ok=True)
        except:
            pass
        logpath = os.path.join(appdata, 'logs', 'stdout.log')

    need_redirecting = False

    if bpio.Windows() and not bpio.isConsoled():
        need_redirecting = True

    if logpath:
        if not appList:
            if cmd not in [
                    'detach',
                    'daemon',
                    'stop',
                    'kill',
                    'shutdown',
                    'restart',
                    'reboot',
                    'reconnect',
                    'show',
                    'open',
            ]:
                lg.open_log_file(logpath)
        if bpio.Windows() and bpio.isFrozen():
            need_redirecting = True

    if bpio.Android():
        need_redirecting = True

    if opts.quite and not opts.verbose:
        lg.disable_output()
    else:
        if need_redirecting:
            lg.stdout_start_redirecting()
            lg.stderr_start_redirecting()

    #---start---
    if cmd == '' or cmd == 'start' or cmd == 'go':
        if appList:
            print_text('BitDust already started, found another process: %s\n' %
                       str(appList),
                       nl='')
            bpio.shutdown()
            return 0

        UI = ''
        try:
            ret = run(UI, opts, args, overDict, executable_path, start_reactor)
        except:
            lg.exc()
            ret = 1
        bpio.shutdown()

        if opts.coverage:
            cov.stop()
            cov.save()
            if opts.coverage_report:
                cov.report(file=open(opts.coverage_report, 'w'))

        return ret

    #---daemon---
    elif cmd == 'detach' or cmd == 'daemon':
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        if len(appList) > 0:
            print_text('main BitDust process already started: %s\n' %
                       str(appList),
                       nl='')
            bpio.shutdown()
            if opts.coverage:
                cov.stop()
                cov.save()
                if opts.coverage_report:
                    cov.report(file=open(opts.coverage_report, 'w'))
            return 0
        from lib import misc
        print_text('new BitDust process will be started in daemon mode\n',
                   nl='')
        result = misc.DoRestart(
            detach=True,
            # std_out=os.path.join(appdata, 'logs', 'stdout.log'),
            # std_err=os.path.join(appdata, 'logs', 'stderr.log'),
        )
        if result is not None:
            try:
                result = int(result)
            except:
                try:
                    result = result.pid
                except:
                    pass
        bpio.shutdown()
        if opts.coverage:
            cov.stop()
            cov.save()
            if opts.coverage_report:
                cov.report(file=open(opts.coverage_report, 'w'))
        return 0

    #---restart---
    elif cmd == 'restart' or cmd == 'reboot':
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        ui = False
        if len(appList) > 0:
            print_text('found main BitDust process: %r ... ' % appList, nl='')

            def done(x):
                print_text('finished successfully\n', nl='')
                from twisted.internet import reactor  # @UnresolvedImport
                if reactor.running and not reactor._stopped:  # @UndefinedVariable
                    reactor.stop()  # @UndefinedVariable

            def failed(x):
                if isinstance(x, Failure):
                    print_text('finished with: %s\n' % x.getErrorMessage(),
                               nl='')
                else:
                    print_text('finished successfully\n', nl='')
                ok = str(x).count('Connection was closed cleanly') > 0
                from twisted.internet import reactor  # @UnresolvedImport
                if ok and reactor.running and not reactor._stopped:  # @UndefinedVariable
                    # print_text('DONE\n', '')
                    reactor.stop()  # @UndefinedVariable
                    return
                print_text('forcing previous process shutdown\n', nl='')
                try:
                    kill()
                except:
                    lg.exc()
                from lib import misc
                reactor.addSystemEventTrigger(  # @UndefinedVariable
                    'after',
                    'shutdown',
                    misc.DoRestart,
                    param='show' if ui else '',
                    detach=True,
                    # std_out=os.path.join(appdata, 'logs', 'stdout.log'),
                    # std_err=os.path.join(appdata, 'logs', 'stderr.log'),
                )
                reactor.stop()  # @UndefinedVariable

            try:
                from twisted.internet import reactor  # @UnresolvedImport
                # from interface.command_line import run_url_command
                # d = run_url_command('?action=restart', False)
                # from interface import cmd_line
                # d = cmd_line.call_xmlrpc_method('restart', ui)
                from interface import cmd_line_json
                d = cmd_line_json.call_websocket_method('process_restart',
                                                        websocket_timeout=5)
                d.addCallback(done)
                d.addErrback(failed)
                reactor.run()  # @UndefinedVariable
                bpio.shutdown()
                if opts.coverage:
                    cov.stop()
                    cov.save()
                    if opts.coverage_report:
                        cov.report(file=open(opts.coverage_report, 'w'))
                return 0
            except:
                lg.exc()
                bpio.shutdown()
                if opts.coverage:
                    cov.stop()
                    cov.save()
                    if opts.coverage_report:
                        cov.report(file=open(opts.coverage_report, 'w'))
                return 1
        else:
            ui = ''
            if cmd == 'restart':
                ui = 'show'
            try:
                ret = run(ui, opts, args, overDict, executable_path)
            except:
                lg.exc()
                ret = 1
            bpio.shutdown()
            if opts.coverage:
                cov.stop()
                cov.save()
                if opts.coverage_report:
                    cov.report(file=open(opts.coverage_report, 'w'))
            return ret

    #---show---
    elif cmd == 'show' or cmd == 'open':
        if not bpio.isGUIpossible():
            print_text('BitDust GUI is turned OFF\n', nl='')
            bpio.shutdown()
            return 0
        if bpio.Linux() and not bpio.X11_is_running():
            print_text('this operating system not supporting X11 interface\n',
                       nl='')
            bpio.shutdown()
            return 0
        appList = bpio.find_main_process(
            pid_file_path=os.path.join(appdata, 'metadata', 'processid'))
        if len(appList) == 0:
            try:
                ret = run('show', opts, args, overDict, executable_path)
            except:
                lg.exc()
                ret = 1
            bpio.shutdown()
            return ret
        # print_text('found main BitDust process: %s, start the GUI\n' % str(appList))
        # ret = show()
        bpio.shutdown()
        return ret

    #---stop---
    elif cmd == 'stop' or cmd == 'kill' or cmd == 'shutdown':
        if cmd == 'kill':
            ret = kill()
            bpio.shutdown()
            if opts.coverage:
                cov.stop()
                cov.save()
                if opts.coverage_report:
                    cov.report(file=open(opts.coverage_report, 'w'))
            return ret
        appList = bpio.find_main_process(pid_file_path=os.path.join(
            appdata, 'metadata', 'processid'), )
        if len(appList) > 0:
            if cmd == 'kill':
                print_text(
                    'found main BitDust process: %s, about to kill running process ... '
                    % appList,
                    nl='')
                ret = kill()
                bpio.shutdown()
                if opts.coverage:
                    cov.stop()
                    cov.save()
                    if opts.coverage_report:
                        cov.report(file=open(opts.coverage_report, 'w'))
                return ret
            try:
                from twisted.internet import reactor  # @UnresolvedImport
                from twisted.python.failure import Failure

                def _stopped(x):
                    if _Debug:
                        if isinstance(x, Failure):
                            print_text('finished with: %s\n' %
                                       x.getErrorMessage(),
                                       nl='')
                        else:
                            print_text('finished with: %s\n' % x, nl='')
                    else:
                        print_text('finished successfully\n', nl='')
                    reactor.stop()  # @UndefinedVariable
                    bpio.shutdown()

                print_text('found main BitDust process: %s ... ' % appList,
                           nl='')
                from interface import cmd_line_json
                cmd_line_json.call_websocket_method(
                    'process_stop', websocket_timeout=2).addBoth(_stopped)
                reactor.run()  # @UndefinedVariable
                if opts.coverage:
                    cov.stop()
                    cov.save()
                    if opts.coverage_report:
                        cov.report(file=open(opts.coverage_report, 'w'))
                return 0
            except:
                lg.exc()
                ret = kill()
                bpio.shutdown()
                if opts.coverage:
                    cov.stop()
                    cov.save()
                    if opts.coverage_report:
                        cov.report(file=open(opts.coverage_report, 'w'))
                return ret
        else:
            appListAllChilds = bpio.find_main_process(
                check_processid_file=False,
                extra_lookups=[],
            )
            if len(appListAllChilds) > 0:
                print_text(
                    'BitDust child processes found: %s, performing "kill process" action ...\n'
                    % appListAllChilds,
                    nl='')
                ret = kill()
                if opts.coverage:
                    cov.stop()
                    cov.save()
                    if opts.coverage_report:
                        cov.report(file=open(opts.coverage_report, 'w'))
                return ret

            print_text('BitDust is not running at the moment\n', nl='')
            bpio.shutdown()
            if opts.coverage:
                cov.stop()
                cov.save()
                if opts.coverage_report:
                    cov.report(file=open(opts.coverage_report, 'w'))
            return 0

    #---command_line---
    from interface import cmd_line_json as cmdln
    ret = cmdln.run(opts, args, pars, overDict, executable_path)
    if ret == 2:
        print_text(usage_text())
    bpio.shutdown()

    #---coverage report---
    if opts.coverage:
        cov.stop()
        cov.save()
        if opts.coverage_report:
            cov.report(file=open(opts.coverage_report, 'w'))

    return ret
        self.err += inp
        for line in inp.splitlines():
            if _Debug:
                lg.out(_DebugLevel, '%s' % line)

    def outReceived(self, inp):
        """
        """
        self.out += inp
        for line in inp.splitlines():
            if _Debug:
                lg.out(_DebugLevel, '%s' % line)

    def processEnded(self, reason):
        """
        """
        if _Debug:
            lg.out(_DebugLevel,
                   'geth process FINISHED : %s' % reason.value.exitCode)
        if self.callback:
            self.callback(self.out, reason.value.exitCode)


#------------------------------------------------------------------------------

if __name__ == "__main__":
    bpio.init()
    lg.set_debug_level(18)
    reactor.callWhenRunning(run_geth_node)
    reactor.run()