コード例 #1
0
ファイル: dhnmain.py プロジェクト: vesellov/datahaven
 def do_spawn(x=None):
     from lib.settings import WindowsStarterFileName
     starter_filepath = os.path.join(dhnio.getExecutableDir(), WindowsStarterFileName())
     dhnio.Dprint(0, "dhnmain.main dhnstarter.exe path: %s " % starter_filepath)
     if not os.path.isfile(starter_filepath):
         dhnio.Dprint(0, "dhnmain.main ERROR %s not found" % starter_filepath)
         dhnio.shutdown()
         return 1
     cmdargs = [os.path.basename(starter_filepath), 'uninstall']
     dhnio.Dprint(0, "dhnmain.main os.spawnve cmdargs="+str(cmdargs))
     ret = os.spawnve(os.P_DETACH, starter_filepath, cmdargs, os.environ)
     dhnio.shutdown()
     return ret
コード例 #2
0
ファイル: dhnmain.py プロジェクト: vesellov/datahaven
 def do_reactor_stop_and_spawn(x=None):
     reactor.stop()
     ret = do_spawn()
     dhnio.shutdown()
     return ret
コード例 #3
0
ファイル: dhnmain.py プロジェクト: vesellov/datahaven
def main():
    """
    THIS IS THE ENTRY POINT OF THE PROGRAM!
    """
    try:
        import lib.dhnio as dhnio
    except:
        dirpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        sys.path.insert(0, os.path.abspath('datahaven'))
        sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..')))
        sys.path.insert(0, os.path.abspath(os.path.join(dirpath, '..', '..')))
        try:
            import lib.dhnio as dhnio
        except:
            return 1

    # init IO module, update locale
    dhnio.init()

    # TODO
    # sys.excepthook = dhnio.ExceptionHook
    if not dhnio.isFrozen():
        from twisted.internet.defer import setDebugging
        setDebugging(True)

    # ask dhnio to count time for each line from that moment, not absolute time 
    dhnio.LifeBegins()

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

    if opts.no_logs:
        dhnio.DisableLogs()

    #---logpath---
    logpath = ''
    if dhnio.Windows():
        logpath = os.path.join(os.environ['APPDATA'], 'DataHaven.NET', 'logs', 'dhnmainstart.log')
    elif dhnio.Linux():
        old_path = os.path.join(os.path.expanduser('~'), 'datahavennet')
        if os.path.isdir(old_path):
            logpath = os.path.join(old_path, 'logs', 'dhnmainstart.log')
        else:
            logpath = os.path.join(os.path.expanduser('~'), '.datahaven', 'logs', 'dhnmainstart.log')

    if opts.output:
        logpath = opts.output

    if logpath != '':
        dhnio.OpenLogFile(logpath)
        dhnio.Dprint(2, 'dhnmain.main log file opened ' + logpath)
        if dhnio.Windows() and dhnio.isFrozen():
            dhnio.StdOutRedirectingStart()
            dhnio.Dprint(2, 'dhnmain.main redirecting started')

    if opts.debug or str(opts.debug) == '0':
        dhnio.SetDebug(opts.debug)

    if opts.quite and not opts.verbose:
        dhnio.DisableOutput()

    if opts.verbose:
        copyright()

    dhnio.Dprint(2, 'dhnmain.main started ' + time.asctime())

    overDict = override_options(opts, args)

    cmd = ''
    if len(args) > 0:
        cmd = args[0].lower()
        
    dhnio.Dprint(2, 'dhnmain.main args=%s' % str(args))

    #---start---
    if cmd == '' or cmd == 'start' or cmd == 'go':
        appList = dhnio.find_process([
            'dhnmain.exe',
            'dhnmain.py',
            'dhn.py',
            'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$',
            ])
        
#        pid = -1
#        try:
#            if dhnio.Windows():
#                dhn_data_path = os.path.join(os.environ.get('APPDATA', os.path.join(os.path.expanduser('~'), 'Application Data')), 'DataHaven.NET')
#                pid_path = os.path.join(dhn_data_path, 'metadata', 'processid')
#            else:
#                pid_path = os.path.join(os.path.expanduser('~'), '.datahaven', 'metadata', 'processid')
#            if os.path.isfile(pid_path):
#                pid = int(dhnio.ReadBinaryFile(pid_path).strip())
#        except:
#            dhnio.DprintException()
        # this is extra protection for Debian release
        # I am nut 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 - DHN is working
        # if file not exists we don't want to start if found some other jobs with same name 
        # PREPRO probably in future we can switch to this line:
        # if len(appList) > 0 and pid != -1 and pid in appList
        # because if we do not have pid - the process is not working
        # but old versions do not have pid file so we need to wait till 
        # all users be updated to this version - revision 7520+
#        if len(appList) > 0 and ( ( pid != -1 and pid in appList ) or ( pid == -1 ) ):

        if len(appList) > 0:
            dhnio.Dprint(0, 'DataHaven.NET already started, found another process: %s' % str(appList))
            dhnio.shutdown()
            return 0
        ret = run('', opts, args, overDict)
        dhnio.shutdown()
        return ret

    #---restart---
    elif cmd == 'restart':
        appList = dhnio.find_process([
            'dhnmain.exe',
            'dhnmain.py',
            'dhn.py',
            'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$',
            ])
        if len(appList) > 0:
            dhnio.Dprint(0, 'found main DataHaven.NET process: %s, sending "restart" command ... ' % str(appList), '')
            def done(x):
                dhnio.Dprint(0, 'DONE\n', '')
                from twisted.internet import reactor
                if reactor.running and not reactor._stopped:
                    reactor.stop()
            def failed(x):
                dhnio.Dprint(0, 'FAILED, killing previous process and do restart\n', '')
                try:
                    kill()
                except:
                    dhnio.DprintException()
                from twisted.internet import reactor
                import lib.misc as misc
                reactor.addSystemEventTrigger('after','shutdown', misc.DoRestart)
                reactor.stop()
            try:
                from twisted.internet import reactor
                from command_line import run_url_command
                d = run_url_command('?action=restart', False)
                d.addCallback(done)
                d.addErrback(failed)
                reactor.run()
                dhnio.shutdown()
                return 0
            except:
                dhnio.DprintException()
                dhnio.shutdown()
                return 1
        else:
            ret = run('', opts, args, overDict)
            dhnio.shutdown()
            return ret

    #---show---
    elif cmd == 'show' or cmd == 'open':
        appList_dhnview = dhnio.find_process([
            'dhnview.exe',
            'dhnview.py',
            ])
        appList = dhnio.find_process([
            'dhnmain.exe',
            'dhnmain.py',
            'dhn.py',
            'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$',
            ])
        if len(appList_dhnview) > 0:
            if len(appList) == 0:
                for pid in appList_dhnview:
                    dhnio.kill_process(pid)
            else:
                dhnio.Dprint(0, 'DataHaven.NET GUI already opened, found another process: %s' % str(appList))
                dhnio.shutdown()
                return 0
        if len(appList) == 0:
            ret = run('show', opts, args, overDict)
            dhnio.shutdown()
            return ret
        
        dhnio.Dprint(0, 'found main DataHaven.NET process: %s, start the GUI\n' % str(appList))
        ret = show()
        dhnio.shutdown()
        return ret

    #---stop---
    elif cmd == 'stop' or cmd == 'kill' or cmd == 'shutdown':
        appList = dhnio.find_process([
            'dhnmain.exe',
            'dhnmain.py',
            'dhn.py',
            'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$',
            ])
        if len(appList) > 0:
            dhnio.Dprint(0, 'found main DataHaven.NET process: %s, sending command "exit" ... ' % str(appList), '')
            try:
                from twisted.internet import reactor
                from command_line import run_url_command
                url = '?action=exit'
                run_url_command(url, False).addBoth(wait_then_kill)
                reactor.run()
                dhnio.shutdown()
                return 0
            except:
                dhnio.DprintException()
                ret = kill()
                dhnio.shutdown()
                return ret
        else:
            dhnio.Dprint(0, 'DataHaven.NET is not running at the moment')
            dhnio.shutdown()
            return 0

    #---uninstall---
    elif cmd == 'uninstall':
        def do_spawn(x=None):
            from lib.settings import WindowsStarterFileName
            starter_filepath = os.path.join(dhnio.getExecutableDir(), WindowsStarterFileName())
            dhnio.Dprint(0, "dhnmain.main dhnstarter.exe path: %s " % starter_filepath)
            if not os.path.isfile(starter_filepath):
                dhnio.Dprint(0, "dhnmain.main ERROR %s not found" % starter_filepath)
                dhnio.shutdown()
                return 1
            cmdargs = [os.path.basename(starter_filepath), 'uninstall']
            dhnio.Dprint(0, "dhnmain.main os.spawnve cmdargs="+str(cmdargs))
            ret = os.spawnve(os.P_DETACH, starter_filepath, cmdargs, os.environ)
            dhnio.shutdown()
            return ret
        def do_reactor_stop_and_spawn(x=None):
            reactor.stop()
            ret = do_spawn()
            dhnio.shutdown()
            return ret
        dhnio.Dprint(0, 'dhnmain.main UNINSTALL!')
        if not dhnio.Windows():
            dhnio.Dprint(0, 'This command can be used only under OS Windows.')
            dhnio.shutdown()
            return 0
        if not dhnio.isFrozen():
            dhnio.Dprint(0, 'You are running DataHaven.NET from sources, uninstall command is available only for binary version.')
            dhnio.shutdown()
            return 0
        appList = dhnio.find_process(['dhnmain.exe',])
        if len(appList) > 0:
            dhnio.Dprint(0, 'found main DataHaven.NET process...   ', '')
            try:
                from twisted.internet import reactor
                from command_line import run_url_command
                url = '?action=exit'
                run_url_command(url).addBoth(do_reactor_stop_and_spawn)
                reactor.run()
                dhnio.shutdown()
                return 0
            except:
                dhnio.DprintException()
        ret = do_spawn()
        dhnio.shutdown()
        return ret
        
    #---command_line---
    import command_line
    ret = command_line.run(opts, args, overDict, pars)
    if ret == 2:
        print usage()
    dhnio.shutdown()
    return ret