def cmd_integrate(opts, args, overDict): """ A platform-dependent method to make an alias in OS for fast run : "bitdust". Than you can Run: cd bitdust python bitdust.py integrate > /usr/local/bin/bitdust chmod Ubuntu: This will create an executable file /usr/local/bin/bitdust with such content: #!/bin/sh cd [path to `bitdust` folder] python bitdust.py "$@" If this is sterted without root permissions, it should create a file ~/bin/bitdust. """ # def print_text(msg, nl='\n'): # """ # """ # sys.stdout.write(msg+nl) from system import bpio if bpio.Windows(): print_text('this feature is not yet available in OS Windows.') return 0 curpath = bpio.getExecutableDir() # cmdpath = '/usr/local/bin/bitdust' src = "#!/bin/sh\n" src += "cd %s\n" % curpath src += 'python bitdust.py "$@"\n' print_text(src) return 0
def cmd_integrate(opts, args, overDict): """ This is a helper to make a "system-wide" command called for fast access BitDust. Run: python bitdust.py alias > /usr/local/bin/bitdust chmod +x /usr/local/bin/bitdust This will create an executable file /usr/local/bin/bitdust with such content: #!/bin/sh python [path to `bitdust` folder]/bitdust.py "$@" """ def print_text(msg, nl='\n'): sys.stdout.write(msg + nl) sys.stdout.flush() from system import bpio if bpio.Windows(): print_text('this feature is not yet available in OS Windows.') return 0 curpath = bpio.getExecutableDir() # cmdpath = '/usr/local/bin/bitdust' src = "#!/bin/sh\n" src += '# This is a short shell script to fast access BitDust software.\n' src += '# NOTICE: BitDust software do not need root permissions to run, expected a normal user permissions.\n\n' # src += "cd %s\n" % curpath src += 'python %s/bitdust.py "$@"\n\n' % curpath print_text(src) return 0
def default_nodes(): """ A set of identity servers currently maintained, see file networks.json in the root folder. """ from system import bpio from system import local_fs from lib import serialization from lib import strng from main import settings # from logs import lg networks_json = serialization.BytesToDict( local_fs.ReadBinaryFile( os.path.join(bpio.getExecutableDir(), 'networks.json')), keys_to_text=True, values_to_text=True, ) my_network = local_fs.ReadTextFile(settings.NetworkFileName()).strip() if not my_network: my_network = 'main' if my_network not in networks_json: my_network = 'main' network_info = networks_json[my_network] identity_servers = {} for identity_server in network_info['identity-servers']: identity_servers[strng.to_bin(identity_server['host'])] = ( identity_server['http_port'], identity_server['tcp_port'], ) # lg.info('Active network is [%s] identity_servers=%s' % (my_network, identity_servers, )) return identity_servers
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
def default_nodes(): """ List of DHT nodes currently maintained : (host, UDP port number) """ from system import bpio from system import local_fs from lib import serialization from main import settings from logs import lg networks_json = serialization.BytesToDict( local_fs.ReadBinaryFile( os.path.join(bpio.getExecutableDir(), 'networks.json'))) my_network = local_fs.ReadTextFile(settings.NetworkFileName()).strip() if not my_network: my_network = 'main' if my_network not in networks_json: my_network = 'main' network_info = networks_json[my_network] dht_seeds = [] for dht_seed in network_info['dht-seeds']: dht_seeds.append(( dht_seed['host'], dht_seed['udp_port'], )) lg.info('Active network is [%s] dht_seeds=%s' % ( my_network, dht_seeds, )) return dht_seeds
def ReadRepoLocation(): """ This method reutrn a tuple of two strings: "name of the current repo" and "repository location". """ if bpio.Linux() or bpio.Mac(): repo_file = os.path.join(bpio.getExecutableDir(), 'repo') if os.path.isfile(repo_file): src = bpio.ReadTextFile(repo_file) if src: try: return src.split('\n')[0].strip(), src.split( '\n')[1].strip() except: lg.exc() return 'sources', 'https://bitdust.io/download/' src = strng.to_bin(bpio.ReadTextFile(settings.RepoFile())).strip() if not src: return settings.DefaultRepo(), settings.DefaultRepoURL( settings.DefaultRepo()) l = src.split('\n') if len(l) < 2: return settings.DefaultRepo(), settings.DefaultRepoURL( settings.DefaultRepo()) return l[0], l[1]
def run(cmdargs, base_dir=None, git_bin=None, env=None, callback=None): """ """ if _Debug: lg.out(_DebugLevel, 'git_proc.run') base_dir = base_dir or bpio.getExecutableDir() if bpio.Windows(): cmd = ['git', ] + cmdargs if git_bin: git_exe = git_bin else: git_exe = bpio.portablePath(os.path.join(base_dir, '..', 'git', 'bin', 'git.exe')) if not os.path.isfile(git_exe): if _Debug: lg.out(_DebugLevel, ' not found git.exe, try to run from shell') try: response, error, retcode = execute_in_shell(cmd, base_dir=base_dir) except: response = '' error = '' retcode = 1 if callback: callback(response, error, retcode) return if _Debug: lg.out(_DebugLevel, ' found git in %s' % git_exe) cmd = [git_exe, ] + cmdargs else: cmd = [git_bin or 'git', ] + cmdargs execute(cmd, callback=callback, base_dir=base_dir, env=env)
def cmd_integrate(opts, args, overDict): """ A platform-dependent method to make a "system" command called "bitdust". Than you can Run: sudo python bitdust.py integrate Linux: This will create an executable file /usr/local/bin/bitdust with such content: #!/bin/sh cd [path to `bitdust` folder] python bitdust.py "$@" If this is sterted without root permissions, it should create a file ~/bin/bitdust. """ def print_text(msg, nl='\n'): """ """ sys.stdout.write(msg+nl) from system import bpio if bpio.Windows(): print_text('this feature is not yet available in OS Windows.') return 0 curpath = bpio.getExecutableDir() cmdpath = '/usr/local/bin/bitdust' src = "#!/bin/sh\n" src += "cd %s\n" % curpath src += 'python bitdust.py "$@"\n' print_text('creating a command script : %s ... ' % cmdpath, nl='') result = False try: f = open(cmdpath, 'w') f.write(src) f.close() os.chmod(cmdpath, 0755) result = True print_text('SUCCESS') except: print_text('FAILED') if not result: cmdpath = os.path.join(os.path.expanduser('~'), 'bin', 'bitdust') print_text('try to create a command script in user home folder : %s ... ' % cmdpath, nl='') try: if not os.path.isdir(os.path.join(os.path.expanduser('~'), 'bin')): os.mkdir(os.path.join(os.path.expanduser('~'), 'bin')) f = open(cmdpath, 'w') f.write(src) f.close() os.chmod(cmdpath, 0755) result = True print_text('SUCCESS') except: print_text('FAILED') return 0 if result: print_text('now use "bitdust" command to access the BitDust software.\n') return 0
def sync(callback_func=None, update_method='rebase'): """ Runs commands and process stdout and stderr to recogneze the result: `git fetch --all -v` `git rebase origin/master -v` or `git reset --hard origin/master` """ src_dir_path = bpio.getExecutableDir() expected_src_dir = os.path.join(deploy.default_base_dir_portable(), 'src') if bpio.portablePath(src_dir_path) != bpio.portablePath(expected_src_dir): if _Debug: lg.out(_DebugLevel, 'git_proc.sync SKIP, non standard sources location: %r' % src_dir_path) return def _reset_done(response, error, retcode, result): if callback_func is None: return callback_func(result) def _rebase_done(response, error, retcode, result): if callback_func is None: return if retcode != 0: result = 'sync-error' else: if response.count(b'Changes from') or response.count(b'Fast-forwarded'): result = 'code-fetched' else: result = 'up-to-date' callback_func(result) def _fetch_done(response, error, retcode): if retcode != 0: if callback_func: callback_func('sync-error') return result = 'sync-error' if response.count(b'Unpacking') or \ (response.count(b'master') and response.count(b'->')) or \ response.count(b'Updating') or \ response.count(b'Receiving') or \ response.count(b'Counting'): result = 'new-code' if update_method == 'reset': run(['reset', '--hard', 'origin/master', ], callback=lambda resp, err, ret: _reset_done(resp, err, ret, result)) elif update_method == 'rebase': run(['rebase', 'origin/master', '-v'], callback=lambda resp, err, ret: _rebase_done(resp, err, ret, result)) else: raise Exception('invalid update method: %s' % update_method) run(['fetch', '--all', '-v'], callback=_fetch_done)
def init(): """ """ if _Debug: lg.out(_DebugLevel - 6, 'driver.init') available_services_dir = os.path.join(bpio.getExecutableDir(), 'services') loaded = set() for filename in os.listdir(available_services_dir): if not filename.endswith('.py') and not filename.endswith( '.pyo') and not filename.endswith('.pyc'): continue if not filename.startswith('service_'): continue name = str(filename[:filename.rfind('.')]) if name in loaded: continue if name in disabled_services(): if _Debug: lg.out(_DebugLevel - 4, '%s is hard disabled' % name) continue try: py_mod = importlib.import_module('services.' + name) except: if _Debug: lg.out( _DebugLevel - 4, '%s exception during module import' % name) lg.exc() continue try: services()[name] = py_mod.create_service() except: if _Debug: lg.out( _DebugLevel - 4, '%s exception while creating service instance' % name) lg.exc() continue loaded.add(name) if not services()[name].enabled(): if _Debug: lg.out(_DebugLevel - 4, '%s is switched off' % name) continue enabled_services().add(name) if _Debug: lg.out(_DebugLevel - 4, '%s initialized' % name) build_order() config.conf().addCallback('services/', on_service_enabled_disabled)
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 init(): lg.out(4, 'os_windows_update.init') # update_shedule_file(settings.getUpdatesSheduleData()) if not bpio.isFrozen() or not bpio.Windows(): lg.out(6, 'os_windows_update.init finishing') return # if not os.path.isfile(settings.VersionFile()): # bpio.WriteFile(settings.VersionFile(), '') SetLocalDir(bpio.getExecutableDir()) if settings.getUpdatesMode() != settings.getUpdatesModeValues()[2]: lg.out(6, 'os_windows_update.init starting the loop') reactor.callLater(0, loop, True) else: lg.out(6, 'os_windows_update.init skip, update mode is: %s' % settings.getUpdatesMode())
def refresh_indexes(db_instance, rewrite=True, reindex=True): """ """ if _Debug: lg.out(_DebugLevel, 'message_db.refresh_indexes in %s' % db_instance.path) ok = True for ind, ind_class_or_filename in message_index.definitions(): if isinstance(ind_class_or_filename, str): chat_history_dir = db_instance.path target_index_filepath = os.path.join(chat_history_dir, '_indexes', ind_class_or_filename) if not os.path.exists(os.path.dirname(target_index_filepath)): bpio._dirs_make(os.path.dirname(target_index_filepath)) bpio.WriteTextFile( target_index_filepath, bpio.ReadTextFile(os.path.join(bpio.getExecutableDir(), 'chat', 'indexes', ind_class_or_filename)), ) ind_obj = 'path:%s' % os.path.basename(target_index_filepath) else: ind_obj = ind_class_or_filename(db_instance.path, ind) if ind not in db_instance.indexes_names: try: db_instance.add_index(ind_obj, create=True) if _Debug: lg.out(_DebugLevel, ' added index %s' % ind) except: lg.exc('failed adding index "%r"' % ind) else: if rewrite: try: db_instance.edit_index(ind_obj, reindex=reindex) if _Debug: lg.out(_DebugLevel, ' updated index %s' % ind) except: lg.exc('failed rewriting index "%r"' % ind) ok = False break return ok
def init(): global _WSGIListener global _WSGIPort result = Deferred() if _Debug: lg.out(_DebugLevel, 'control.init') request_update() if _WSGIListener: if _Debug: lg.out(_DebugLevel, ' SKIP listener already exist') result.callback(0) return result try: import django # ver = django.get_version() # if not ver.startswith('1.7'): # if _Debug: # lg.out(_DebugLevel, ' Django version must be 1.7, skip!') # result.callback(0) # return result except: lg.exc() result.callback(0) return result if _Debug: lg.out(_DebugLevel + 6, ' \n' + pprint.pformat(sys.path)) if _Debug: lg.out( _DebugLevel, ' setting environment DJANGO_SETTINGS_MODULE=web.asite.settings' ) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.asite.settings") from django.core.wsgi import get_wsgi_application from django.conf import settings as django_settings from django.core import management # from django.contrib.auth.management.commands import changepassword if _Debug: lg.out(_DebugLevel, ' configuring WSGI bridge from Twisted to Django') wsgi_handler = get_wsgi_application() my_wsgi_handler = MyFakedWSGIHandler(wsgi_handler) pool = threadpool.ThreadPool() pool.start() reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) resource = wsgi.WSGIResource(reactor, pool, my_wsgi_handler) root = DjangoRootResource(resource) root_static_dir = os.path.join(bpio.getExecutableDir(), "web") for sub in os.listdir(root_static_dir): static_path = os.path.join(root_static_dir, sub, 'static') if not os.path.isdir(static_path): continue node = static.File(static_path) root.putChild(sub, node) if _Debug: lg.out(_DebugLevel, ' added static dir: %s->%s' % (sub, static_path)) if sub == 'asite': admin_path = os.path.join(root_static_dir, sub, 'admin', 'static') node.putChild('admin', static.File(admin_path)) if _Debug: lg.out( _DebugLevel, ' added ADMIN static dir: admin->%s' % admin_path) site = server.Site(root) _WSGIPort = 8080 # TODO: read port num from settings if _Debug: lg.out(_DebugLevel, ' %s' % my_wsgi_handler) lg.out(_DebugLevel, ' %s' % resource) lg.out(_DebugLevel, ' %s' % site) verbosity = 0 if lg.is_debug(18): verbosity = 3 if lg.is_debug(12): verbosity = 2 if lg.is_debug(8): verbosity = 1 # lg.out(4, ' running django "flush" command') # management.call_command('flush', interactive=False, verbosity=verbosity) # lg.out(4, ' running django "createsuperuser" command') # management.call_command('createsuperuser', # interactive=False, verbosity=verbosity, # username="******", email="admin@localhost") # command = changepassword.Command() # command._get_pass = lambda *args: 'admin' # command.execute("admin") if _Debug: lg.out(_DebugLevel, ' running django "syncdb" command') management.call_command('syncdb', stdout=open( os.path.join(settings.LogsDir(), 'django-syncdb.log'), 'w'), interactive=False, verbosity=verbosity) _ShortPoolPort = 8081 # TODO: read port num from settings # shortpool.init(get_update_items, set_updated, _ShortPoolPort) if _Debug: lg.out(_DebugLevel, ' starting listener: %s' % site) result = start_listener(site) result.addCallback(lambda portnum: post_init(portnum)) return result
def main(): lg.set_debug_level(18) lg.life_begins() from crypt import key key.InitMyKey() from contacts import identitycache identitycache.init() from system import tmpfile tmpfile.init() from services import driver required_services = [ 'service_http_connections', 'service_http_transport', 'service_gateway', 'service_network', ] available_services_dir = os.path.join(bpio.getExecutableDir(), 'services') for filename in os.listdir(available_services_dir): if not filename.endswith('.py') and not filename.endswith( '.pyo') and not filename.endswith('.pyc'): continue if not filename.startswith('service_'): continue name = str(filename[:filename.rfind('.')]) if name in required_services: continue driver.disabled_services().add(name) driver.init() driver.enabled_services().clear() driver.enabled_services().add('service_http_transport') driver.enabled_services().add('service_http_connections') driver.enabled_services().add('service_gateway') driver.enabled_services().add('service_network') driver.start() # options = { 'idurl': my_id.getIDURL(),} # options['host'] = nameurl.GetName(my_id.getIDURL())+'@'+'somehost.org' # options['dht_port'] = int(settings.getDHTPort()) # options['udp_port'] = int(settings.getUDPPort()) # udp.listen(int(settings.getUDPPort())) # dht_service.init(settings.getDHTPort()) # dht_service.connect() # udp_node.A('go-online', options) reactor.addSystemEventTrigger('before', 'shutdown', gateway.shutdown) gateway.init() gateway.start() def _ok_to_send(transport, oldstate, newstate): if newstate != 'LISTENING': return # [filename] [peer idurl] if len(sys.argv) >= 3: pass # bpio.WriteFile(sys.argv[1]+'.signed', p.Serialize()) # def _try_reconnect(): # sess = udp_session.get_by_peer_id(sys.argv[2]) # reconnect = False # if not sess: # reconnect = True # print 'sessions', udp_session.sessions_by_peer_id().keys() # print map(lambda s: s.peer_id, udp_session.sessions().values()) # else: # if sess.state != 'CONNECTED': # print 'state: ', sess.state # reconnect = True # if reconnect: # print 'reconnect', sess # udp_session.add_pending_outbox_file( # sys.argv[1] + '.signed', sys.argv[2], 'descr', Deferred(), False) # udp_node.A('connect', sys.argv[2]) # reactor.callLater(0.5, _try_reconnect) # # def _try_connect(): # if udp_node.A().state == 'LISTEN': # print 'connect' # gateway.stop_packets_timeout_loop() # udp_session.add_pending_outbox_file( # sys.argv[1] + '.signed', sys.argv[2], 'descr', Deferred(), False) # udp_node.A('connect', sys.argv[2]) # reactor.callLater(5, _try_reconnect) # else: # reactor.callLater(1, _try_connect) # # _try_connect() # # def _send(c): # from transport.udp import udp_stream # for idurl in sys.argv[2:]: # print '_send', udp_stream.streams().keys() # p = signed.Packet(commands.Data(), # my_id.getIDURL(), # my_id.getIDURL(), # 'packet%d' % c, # bpio.ReadBinaryFile(sys.argv[1]), # idurl) # gateway.outbox(p) # if c > 1: # reactor.callLater(0.01, _send, c - 1) # reactor.callLater(0, _send, 15) gateway.add_transport_state_changed_callback(_ok_to_send) reactor.run()
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': import shutdowner shutdowner.A('stop', 'exit') tray_icon.SetControlFunc(_tray_control_func) #---OS Windows init--- if bpio.Windows(): try: from win32event import CreateMutex 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 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() + '-' + 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.WriteFile(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, "bpmain.run UI=[%s]" % UI) if lg.is_debug(20): lg.out(0, '\n' + bpio.osinfofull()) lg.out(4, 'bpmain.run import automats') #---START!--- from automats import automat automat.LifeBegins(lg.when_life_begins()) automat.OpenLogFile(settings.AutomatsLog()) import initializer I = initializer.A() lg.out(4, 'bpmain.run send event "run" to initializer()') reactor.callWhenRunning(I.automat, 'run', UI) return I
def init(): global _WSGIListener global _WSGIPort result = Deferred() lg.out(4, 'control.init') request_update() if _WSGIListener: lg.out(4, ' SKIP listener already exist') result.callback(0) return result try: import django ver = django.get_version() if not ver.startswith('1.7'): lg.out(4, ' Django version must be 1.7, skip!') result.callback(0) return result except: lg.exc() result.callback(0) return result lg.out(10, ' \n' + pprint.pformat(sys.path)) lg.out(4, ' setting environment DJANGO_SETTINGS_MODULE=web.asite.settings') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.asite.settings") from django.core.wsgi import get_wsgi_application from django.conf import settings as django_settings from django.core import management from django.contrib.auth.management.commands import changepassword lg.out(4, ' configuring WSGI bridge from Twisted to Django') wsgi_handler = get_wsgi_application() my_wsgi_handler = MyFakedWSGIHandler(wsgi_handler) pool = threadpool.ThreadPool() pool.start() reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) resource = wsgi.WSGIResource(reactor, pool, my_wsgi_handler) root = DjangoRootResource(resource) root_static_dir = os.path.join(bpio.getExecutableDir(), "web") for sub in os.listdir(root_static_dir): static_path = os.path.join(root_static_dir, sub, 'static') if not os.path.isdir(static_path): continue node = static.File(static_path) root.putChild(sub, node) lg.out(4, ' added static dir: %s->%s' % (sub, static_path)) if sub == 'asite': admin_path = os.path.join(root_static_dir, sub, 'admin', 'static') node.putChild('admin', static.File(admin_path)) lg.out(4, ' added ADMIN static dir: admin->%s' % admin_path) site = server.Site(root) _WSGIPort = 8080 lg.out(4, ' %s' % my_wsgi_handler) lg.out(4, ' %s' % resource) lg.out(4, ' %s' % site) verbosity = 0 if lg.is_debug(18): verbosity = 3 if lg.is_debug(12): verbosity = 2 if lg.is_debug(8): verbosity = 1 # lg.out(4, ' running django "flush" command') # management.call_command('flush', interactive=False, verbosity=verbosity) # lg.out(4, ' running django "createsuperuser" command') # management.call_command('createsuperuser', # interactive=False, verbosity=verbosity, # username="******", email="admin@localhost") # command = changepassword.Command() # command._get_pass = lambda *args: 'admin' # command.execute("admin") lg.out(4, ' running django "syncdb" command') management.call_command('syncdb', stdout=sys.stdout, interactive=False, verbosity=verbosity) lg.out(4, ' starting listener: %s' % site) result = start_listener(site) result.addCallback(lambda portnum: post_init(portnum)) return result
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
def DoRestart(param='', detach=False, std_out='/dev/null', std_err='/dev/null'): """ A smart and portable way to restart a whole program. """ if bpio.Windows(): if bpio.isFrozen(): # lg.out(2, "misc.DoRestart under Windows (Frozen), param=%s" % param) # lg.out(2, "misc.DoRestart sys.executable=" + sys.executable) # lg.out(2, "misc.DoRestart sys.argv=" + str(sys.argv)) starter_filepath = os.path.join(bpio.getExecutableDir(), settings.WindowsStarterFileName()) if not os.path.isfile(starter_filepath): # lg.out(2, "misc.DoRestart ERROR %s not found" % starter_filepath) main_filepath = os.path.join( bpio.getExecutableDir(), settings.WindowsMainScriptFileName()) cmdargs = [ os.path.basename(main_filepath), ] if param != '': cmdargs.append(param) # lg.out(2, "misc.DoRestart cmdargs="+str(cmdargs)) return os.spawnve(os.P_DETACH, main_filepath, cmdargs, os.environ) # @UndefinedVariable cmdargs = [ os.path.basename(starter_filepath), ] if param != '': cmdargs.append(param) # lg.out(2, "misc.DoRestart cmdargs="+str(cmdargs)) return os.spawnve(os.P_DETACH, starter_filepath, cmdargs, os.environ) # @UndefinedVariable pypath = sys.executable cmdargs = [ sys.executable, ] cmdargs.append(sys.argv[0]) cmdargs += sys.argv[1:] if param != '' and not sys.argv.count(param): cmdargs.append(param) if cmdargs.count('restart'): cmdargs.remove('restart') if cmdargs.count('detach'): cmdargs.remove('detach') if cmdargs.count('daemon'): cmdargs.remove('daemon') if detach: from system import child_process cmdargs = [strng.to_text(a) for a in cmdargs] return child_process.detach(cmdargs) return os.execvpe(pypath, cmdargs, os.environ) pypyth = sys.executable cmdargs = [ sys.executable, ] if sys.argv[0] == '/usr/share/bitdust/bitdust.py': cmdargs.append('/usr/bin/bitdust') else: cmdargs.append(sys.argv[0]) if param: cmdargs.append(param) if cmdargs.count('restart'): cmdargs.remove('restart') if cmdargs.count('detach'): cmdargs.remove('detach') if cmdargs.count('daemon'): cmdargs.remove('daemon') pid = os.fork() if pid != 0: return None if detach: cmdargs[1] = os.path.abspath(cmdargs[1]) cmdargs.append('1>%s' % std_out) cmdargs.append('2>%s' % std_err) cmd = '/usr/bin/nohup ' + (' '.join(cmdargs)) + ' &' BITDUST_COVERAGE_PROCESS_START = os.environ.get( 'COVERAGE_PROCESS_START') if BITDUST_COVERAGE_PROCESS_START: cmd = 'COVERAGE_PROCESS_START="%s" %s' % ( BITDUST_COVERAGE_PROCESS_START, cmd, ) BITDUST_LOG_USE_COLORS = os.environ.get('BITDUST_LOG_USE_COLORS') if BITDUST_LOG_USE_COLORS: cmd = 'BITDUST_LOG_USE_COLORS="%s" %s' % ( BITDUST_LOG_USE_COLORS, cmd, ) return os.system(cmd) return os.execvpe(pypyth, cmdargs, os.environ)