예제 #1
0
def find_main_process(pid_file_path=None):
    """
    """
    appList = find_process([
        'bitdustnode.exe',
        'BitDustNode.exe',
        'bpmain.py',
        'bitdust.py',
        'regexp:^/usr/bin/python.*bitdust.*$',
    ])
    if not appList:
        return []
    try:
        if not pid_file_path:
            from main import settings
            pid_file_path = os.path.join(settings.MetaDataDir(), 'processid')
        processid = int(_read_data(pid_file_path))
    except:
        processid = None
    if not processid:
        return appList
    if processid not in appList:
        return []
    return [
        processid,
    ]
예제 #2
0
def eraseLocalIdentity(do_backup=True):
    if do_backup:
        if os.path.isfile(settings.LocalIdentityFilename()):
            current_identity_xmlsrc = local_fs.ReadBinaryFile(settings.LocalIdentityFilename())
            if current_identity_xmlsrc:
                fd, fname = tempfile.mkstemp(prefix='localidentity_', dir=settings.MetaDataDir())
                os.write(fd, current_identity_xmlsrc)
                os.close(fd)
                lg.info('created backup copy of my local identity in the file : %r' % fname)
    filename = bpio.portablePath(settings.LocalIdentityFilename())
    if not os.path.exists(filename):
        if _Debug:
            lg.out(_DebugLevel, "my_id.eraseLocalIdentity SKIP file %s not exist" % filename)
        return True
    if not os.path.isfile(filename):
        if _Debug:
            lg.out(_DebugLevel, "my_id.eraseLocalIdentity ERROR path %s is not a file" % filename)
        return False
    try:
        os.remove(filename)
    except:
        lg.exc()
        return False
    events.send('local-identity-erased', data=dict())
    if _Debug:
        lg.out(_DebugLevel, "my_id.eraseLocalIdentity file %s was deleted" % filename)
    return True
예제 #3
0
def find_main_process(pid_file_path=None,
                      extra_lookups=[],
                      check_processid_file=True):
    """
    """
    appList = find_process([
        'bitdustnode.exe',
        'BitDustNode.exe',
        'BitDustConsole.exe',
        # 'bitdust.py',
        'regexp:^.*python.*bitdust.py$',
    ] + extra_lookups)
    if not appList:
        return []
    if not check_processid_file:
        return appList
    try:
        if not pid_file_path:
            from main import settings
            pid_file_path = os.path.join(settings.MetaDataDir(), 'processid')
        processid = int(ReadTextFile(pid_file_path))
    except:
        processid = None
    if not processid:
        return appList
    if processid not in appList:
        return []
    return [
        processid,
    ]
예제 #4
0
def find_network_config_file():
    networks_json_path = os.path.join(settings.MetaDataDir(), 'networkconfig')
    if not os.path.isfile(networks_json_path):
        # use hard-coded default_network.json file from the repository root
        networks_json_path = os.path.join(bpio.getExecutableDir(),
                                          'default_network.json')
    return networks_json_path
예제 #5
0
def ForgetMyKey(keyfilename=None, erase_file=False, do_backup=False):
    """
    Remove Private Key from memory.
    """
    global _MyKeyObject
    if _MyKeyObject:
        _MyKeyObject.forget()
    _MyKeyObject = None
    if erase_file:
        if keyfilename is None:
            keyfilename = settings.KeyFileName()
        if do_backup:
            if os.path.isfile(keyfilename):
                current_pk_src = local_fs.ReadBinaryFile(keyfilename)
                if current_pk_src:
                    fd, fname = tempfile.mkstemp(prefix='mykeyfile_',
                                                 dir=settings.MetaDataDir())
                    os.write(fd, current_pk_src)
                    os.close(fd)
                    lg.info(
                        'created backup copy of my private key in the file : %r'
                        % fname)
        if os.path.isfile(keyfilename):
            os.remove(keyfilename)
            lg.info('local private key erased, deleted file : %r' %
                    keyfilename)
예제 #6
0
def find_main_process(pid_file_path=None,
                      extra_lookups=[],
                      check_processid_file=True):
    """
    """
    if Android():
        return []
    q = [
        'bitdustnode.exe',
        'BitDustNode.exe',
        'BitDustConsole.exe',
        # 'bitdust.py',
    ]
    if os.environ.get('BITDUST_IN_DOCKER') == '1':
        q.extend([
            'regexp:^.*python.*bitdust.py.*?$',
        ])
    else:
        q.extend([
            'regexp:^.*(?<!\/root\/\.bitdust\/venv\/bin\/)python.*bitdust.py.*?$',
            'regexp:^.*(?<!\/root\/\.bitdust\/venv\/bin\/)Python.*bitdust.py.*?$',
        ])
    q += extra_lookups
    appList = find_process(q)
    if not appList:
        return []
    if not check_processid_file:
        return appList
    try:
        if not pid_file_path:
            from main import settings
            pid_file_path = os.path.join(settings.MetaDataDir(), 'processid')
        processid = int(ReadTextFile(pid_file_path))
    except:
        processid = None
    if not processid:
        return appList
    if processid not in appList:
        return []
    return [
        processid,
    ]
예제 #7
0
파일: bpmain.py 프로젝트: riyazudheen/devel
def init(UI='', options=None, args=None, overDict=None, executablePath=None):
    """
    In the method ``main()`` program firstly checks the command line arguments
    and then calls this method to start the whole process.

    This initialize some low level modules and finally create an
    instance of ``initializer()`` state machine and send it an event
    "run".
    """
    global AppDataDir

    from logs import lg
    lg.out(4, 'bpmain.run UI="%s"' % UI)

    from system import bpio

    #---settings---
    from main import settings
    if overDict:
        settings.override_dict(overDict)
    settings.init(AppDataDir)
    if not options or options.debug is None:
        lg.set_debug_level(settings.getDebugLevel())
    from main import config
    config.conf().addCallback('logs/debug-level',
                              lambda p, value, o, r: lg.set_debug_level(value))

    #---USE_TRAY_ICON---
    if os.path.isfile(settings.LocalIdentityFilename()) and os.path.isfile(settings.KeyFileName()):
        try:
            from system.tray_icon import USE_TRAY_ICON
            if bpio.Mac() or not bpio.isGUIpossible():
                lg.out(4, '    GUI is not possible')
                USE_TRAY_ICON = False
            if USE_TRAY_ICON:
                from twisted.internet import wxreactor
                wxreactor.install()
                lg.out(4, '    wxreactor installed')
        except:
            USE_TRAY_ICON = False
            lg.exc()
    else:
        lg.out(4, '    local identity or key file is not ready')
        USE_TRAY_ICON = False
    lg.out(4, '    USE_TRAY_ICON=' + str(USE_TRAY_ICON))
    if USE_TRAY_ICON:
        from system import tray_icon
        icons_path = bpio.portablePath(os.path.join(bpio.getExecutableDir(), 'icons'))
        lg.out(4, 'bpmain.run call tray_icon.init(%s)' % icons_path)
        tray_icon.init(icons_path)

        def _tray_control_func(cmd):
            if cmd == 'exit':
                from . import shutdowner
                shutdowner.A('stop', 'exit')
        tray_icon.SetControlFunc(_tray_control_func)

    #---OS Windows init---
    if bpio.Windows():
        try:
            from win32event import CreateMutex  # @UnresolvedImport
            mutex = CreateMutex(None, False, "BitDust")
            lg.out(4, 'bpmain.run created a Mutex: %s' % str(mutex))
        except:
            lg.exc()

    #---twisted reactor---
    lg.out(4, 'bpmain.run want to import twisted.internet.reactor')
    try:
        from twisted.internet import reactor  # @UnresolvedImport
    except:
        lg.exc()
        sys.exit('Error initializing reactor in bpmain.py\n')

    #---logfile----
    if lg.logs_enabled() and lg.log_file():
        lg.out(2, 'bpmain.run want to switch log files')
        if bpio.Windows() and bpio.isFrozen():
            lg.stdout_stop_redirecting()
        lg.close_log_file()
        lg.open_log_file(settings.MainLogFilename())
        # lg.open_log_file(settings.MainLogFilename() + '-' + time.strftime('%y%m%d%H%M%S') + '.log')
        if bpio.Windows() and bpio.isFrozen():
            lg.stdout_start_redirecting()

    #---memdebug---
#    if settings.uconfig('logs.memdebug-enable') == 'True':
#        try:
#            from logs import memdebug
#            memdebug_port = int(settings.uconfig('logs.memdebug-port'))
#            memdebug.start(memdebug_port)
#            reactor.addSystemEventTrigger('before', 'shutdown', memdebug.stop)
#            lg.out(2, 'bpmain.run memdebug web server started on port %d' % memdebug_port)
#        except:
#            lg.exc()

    #---process ID---
    try:
        pid = os.getpid()
        pid_file_path = os.path.join(settings.MetaDataDir(), 'processid')
        bpio.WriteTextFile(pid_file_path, str(pid))
        lg.out(2, 'bpmain.run wrote process id [%s] in the file %s' % (str(pid), pid_file_path))
    except:
        lg.exc()

#    #---reactor.callLater patch---
#    if lg.is_debug(12):
#        patchReactorCallLater(reactor)
#        monitorDelayedCalls(reactor)

#    #---plugins---
#    from plugins import plug
#    plug.init()
#    reactor.addSystemEventTrigger('before', 'shutdown', plug.shutdown)

    lg.out(2, "    python executable is: %s" % sys.executable)
    lg.out(2, "    python version is:\n%s" % sys.version)
    lg.out(2, "    python sys.path is:\n                %s" % ('\n                '.join(sys.path)))

    lg.out(2, "bpmain.run UI=[%s]" % UI)

    if lg.is_debug(20):
        lg.out(0, '\n' + bpio.osinfofull())

    lg.out(4, 'import automats')

    #---START!---
    from automats import automat
    automat.LifeBegins(lg.when_life_begins())
    automat.OpenLogFile(settings.AutomatsLog())

    from main import events
    events.init()

    from main import initializer
    IA = initializer.A()
    lg.out(4, 'sending event "run" to initializer()')
    reactor.callWhenRunning(IA.automat, 'run', UI)  # @UndefinedVariable
    return IA