def step1(version_digest): dhnio.Dprint(4, 'dhnupdate.step1') global _UpdatingInProgress global _CurrentVersionDigest global _NewVersionNotifyFunc global _UpdatingByUser _CurrentVersionDigest = str(version_digest).strip() local_version = dhnio.ReadBinaryFile(settings.VersionFile()).strip() if local_version == _CurrentVersionDigest: dhnio.Dprint(6, 'dhnupdate.step1 no need to update') _UpdatingInProgress = False if _NewVersionNotifyFunc is not None: _NewVersionNotifyFunc(_CurrentVersionDigest) return appList = dhnio.find_process(['dhnview.', ]) if len(appList) > 0: if not _UpdatingByUser: dhnio.Dprint(6, 'dhnupdate.step1 dhnview is running, ask user to update.') _UpdatingInProgress = False if _NewVersionNotifyFunc is not None: _NewVersionNotifyFunc(_CurrentVersionDigest) return d = download_info() d.addCallback(step2, _CurrentVersionDigest) d.addErrback(fail)
def kill(): dhnio.Dprint(0, 'kill') total_count = 0 found = False while True: appList = dhnio.find_process([ 'dhnmain.exe', 'dhnmain.py', 'dhn.py', 'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$', 'dhnview.exe', 'dhnview.py', 'dhnbackup.exe', 'dhnbackup.py', 'dhntester.exe', 'dhntester.py', 'dhnstarter.exe', ]) if len(appList) > 0: found = True for pid in appList: dhnio.Dprint(0, 'trying to stop pid %d' % pid) dhnio.kill_process(pid) if len(appList) == 0: if found: dhnio.Dprint(0, 'DataHaven.NET stopped\n') else: dhnio.Dprint(0, 'DataHaven.NET was not started\n') return 0 total_count += 1 if total_count > 10: dhnio.Dprint(0, 'some DataHaven.NET process found, but can not stop it\n') return 1 time.sleep(1)
def _try(): dhnio.Dprint(0, '_try') appList = dhnio.find_process([ 'dhnmain.exe', 'dhnview.exe', 'dhnbackup.exe', 'dhntester.exe', 'dhnstarter.exe', ]) dhnio.Dprint(0, 'appList:' + str(appList)) if len(appList) == 0: dhnio.Dprint(0, 'finished') reactor.stop() do_uninstall() return 0 total_count += 1 dhnio.Dprint(0, '%d' % total_count) if total_count > 10: dhnio.Dprint(0, 'not responding') ret = kill() reactor.stop() if ret == 0: do_uninstall() return ret reactor.callLater(1, _try)
def init(gate_xmlrpc_url): global _Process dhnio.Dprint(4, 'tcp_interface.init') if len(dhnio.find_process('dhntcp.')) > 0: dhnio.Dprint(4, 'tcp_interface.init [dhntcp] already started') return _Process _Process = child_process.run('dhntcp', ['--rooturl', gate_xmlrpc_url]) return _Process
def wait_then_kill(x): """ For correct shutdown of the program need to send a URL request to the HTTP server: http://localhost:<random port>/?action=exit After receiving such request the program will call `p2p.dhninit.shutdown()` method and stops. But if the main process was blocked it needs to be killed with system "kill" procedure. This method will wait for 10 seconds and then call method `kill()`. """ from twisted.internet import reactor import lib.dhnio as dhnio total_count = 0 while True: appList = dhnio.find_process([ 'dhnmain.exe', 'dhnmain.py', 'dhn.py', 'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$', 'dhnview.exe', 'dhnview.py', 'dhnbackup.exe', 'dhnbackup.py', 'dhntester.exe', 'dhntester.py', 'dhnstarter.exe', ]) if len(appList) == 0: dhnio.Dprint(0, 'DONE') reactor.stop() return 0 total_count += 1 if total_count > 10: dhnio.Dprint(0, 'not responding, KILLING ...') ret = kill() reactor.stop() return ret time.sleep(1)
def cmd_uninstall(opts, args, overDict): if not dhnio.Windows(): print 'This command can be used only under OS Windows.' return 0 if not dhnio.isFrozen(): print 'You are running DataHaven.NET from sources, uninstall command is available only for binary version.' return 0 def do_uninstall(): dhnio.Dprint(0, 'command_line.do_uninstall') # batfilename = misc.MakeBatFileToUninstall() # misc.UpdateRegistryUninstall(True) # misc.RunBatFile(batfilename, 'c:/out2.txt') def kill(): dhnio.Dprint(0, 'kill') total_count = 0 found = False while True: appList = dhnio.find_process([ 'dhnmain.exe', 'dhnmain.py', 'dhn.py', 'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$', 'dhnview.exe', 'dhnview.py', 'dhnbackup.exe', 'dhnbackup.py', 'dhntester.exe', 'dhntester.py', 'dhnstarter.exe', ]) if len(appList) > 0: found = True for pid in appList: dhnio.Dprint(0, 'trying to stop pid %d' % pid) dhnio.kill_process(pid) if len(appList) == 0: if found: dhnio.Dprint(0, 'DataHaven.NET stopped\n') else: dhnio.Dprint(0, 'DataHaven.NET was not started\n') return 0 total_count += 1 if total_count > 10: dhnio.Dprint(0, 'some DataHaven.NET process found, but can not stop it\n') return 1 time.sleep(1) def wait_then_kill(x): dhnio.Dprint(0, 'wait_then_kill') total_count = 0 #while True: def _try(): dhnio.Dprint(0, '_try') appList = dhnio.find_process([ 'dhnmain.exe', 'dhnview.exe', 'dhnbackup.exe', 'dhntester.exe', 'dhnstarter.exe', ]) dhnio.Dprint(0, 'appList:' + str(appList)) if len(appList) == 0: dhnio.Dprint(0, 'finished') reactor.stop() do_uninstall() return 0 total_count += 1 dhnio.Dprint(0, '%d' % total_count) if total_count > 10: dhnio.Dprint(0, 'not responding') ret = kill() reactor.stop() if ret == 0: do_uninstall() return ret reactor.callLater(1, _try) _try() # time.sleep(1) appList = dhnio.find_process([ 'dhnmain.exe', 'dhnview.exe', 'dhnbackup.exe', 'dhntester.exe', 'dhnstarter.exe', ]) if len(appList) == 0: dhnio.Dprint(0, 'uninstalling DataHaven.NET ... ') do_uninstall() return 0 dhnio.Dprint(0, 'found DataHaven.NET processes ... ') try: url = webcontrol._PAGE_ROOT+'?action=exit' run_url_command(url).addCallback(wait_then_kill) #reactor.addSystemEventTrigger('before', 'shutdown', do_uninstall) reactor.run() return 0 except: dhnio.DprintException() ret = kill() if ret == 0: do_uninstall() return ret
def run(opts, args, overDict, pars): """ The entry point, this is called from `p2p.dhnmain` to process command line arguments. """ print 'Copyright 2006, DataHaven.NET LTD. of Anguilla. All rights reserved.' if overDict: settings.override_dict(overDict) dhnio.init() settings.init() if not opts or opts.debug is None: dhnio.SetDebug(0) appList = dhnio.find_process([ 'dhnmain.exe', 'dhnmain.py', 'dhn.py', 'regexp:^/usr/bin/python\ +/usr/bin/datahaven.*$', ]) running = len(appList) > 0 cmd = '' if len(args) > 0: cmd = args[0].lower() #---help--- if cmd in ['help', 'h']: 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 settings.uconfig().print_all() else: print help.help() print pars.format_option_help() return 0 #---backup--- elif cmd in ['backup', 'backups', 'bk']: if not running: print 'DataHaven.NET 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 'DataHaven.NET 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 'DataHaven.NET 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 'DataHaven.NET 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 'DataHaven.NET is not running at the moment\n' return 0 return cmd_customers(opts, args, overDict) #---register--- elif cmd == 'register': if running: print 'DataHaven.NET already started.\n' return 0 return cmd_register(opts, args, overDict) #---recover--- elif cmd == 'recover': if running: print 'DataHaven.NET 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 'DataHaven.NET is not running at the moment\n' return 0 return cmd_stats(opts, args, overDict) #---version--- elif cmd in [ 'version', 'v', 'ver' ]: revnum = dhnio.ReadTextFile(settings.RevisionNumberFile()).strip() repo, location = misc.ReadRepoLocation() print 'revision: ', revnum print 'repository:', repo return 0 #---states--- elif cmd in [ 'states', 'sta', 'automats', 'auto' ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_states(opts, args, overDict) #---reconnect--- elif cmd in [ 'reconnect', ]: if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_reconnect(opts, args, overDict) #---set--- elif cmd == 'set': if len(args) == 1 or args[1].lower() in [ 'help', '?' ]: 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 'DataHaven.NET is not running at the moment\n' return 0 return cmd_memory(opts, args, overDict) #---money--- elif cmd == 'money': if not running: print 'DataHaven.NET is not running at the moment\n' return 0 return cmd_money(opts, args, overDict) elif cmd == 'storage': if not running: print 'DataHaven.NET 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
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