def main(): parser = OptionParser(version=b3.getB3versionString()) parser.add_option("-c", "--config", dest="config", default=None, help="B3 config file. Example: -c b3.xml") parser.add_option( "-r", "--restart", action="store_true", dest="restart", default=False, help="Auto-restart B3 on crash" ) parser.add_option( "-s", "--setup", action="store_true", dest="setup", default=False, help="Setup main b3.xml config file" ) (options, args) = parser.parse_args() if not options.config and len(args) == 1: options.config = args[0] if options.setup: run_setup() if options.restart: if options.config: run_autorestart(["--config", options.config] + args) else: run_autorestart([]) else: if main_is_frozen(): # which happens when running from the py2exe build try: run(config=options.config) except SystemExit, msg: # make sure we are not writting to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg except:
def run(options): """ Run B3 in console. :param options: command line options """ analysis = None # main config analysis result printexit = False # whether the exit message has been printed alreadty or not try: if options.config: config = b3.getAbsolutePath(options.config, True) if not os.path.isfile(config): printexit = True console_exit('ERROR: configuration file not found (%s).\n' 'Please visit %s to create one.' % (config, B3_CONFIG_GENERATOR)) else: config = None for p in ('b3.%s', 'conf/b3.%s', 'b3/conf/b3.%s', os.path.join(HOMEDIR, 'b3.%s'), os.path.join(HOMEDIR, 'conf', 'b3.%s'), os.path.join(HOMEDIR, 'b3', 'conf', 'b3.%s'), '@b3/conf/b3.%s'): for e in ('ini', 'cfg', 'xml'): path = b3.getAbsolutePath(p % e, True) if os.path.isfile(path): print "Using configuration file: %s" % path config = path sleep(3) break if not config: printexit = True console_exit('ERROR: could not find any valid configuration file.\n' 'Please visit %s to create one.' % B3_CONFIG_GENERATOR) # LOADING MAIN CONFIGURATION main_config = b3.config.MainConfig(b3.config.load(config)) analysis = main_config.analyze() if analysis: raise b3.config.ConfigFileNotValid('invalid configuration file specified') # START B3 b3.start(main_config, options) except b3.config.ConfigFileNotValid: if analysis: print 'CRITICAL: invalid configuration file specified:\n' for problem in analysis: print" >>> %s\n" % problem else: print 'CRITICAL: invalid configuration file specified!' raise SystemExit(1) except SystemExit, msg: if not printexit and main_is_frozen(): if sys.stdout != sys.__stdout__: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg raw_input("press any key to continue...") raise
def run_console(options): """ Run B3 in console mode. :param options: command line options """ analysis = None # main config analysis result printexit = False # whether the exit message has been printed alreadty or not try: if options.config: config = b3.getAbsolutePath(options.config, True) if not os.path.isfile(config): printexit = True console_exit('ERROR: configuration file not found (%s).\n' 'Please visit %s to create one.' % (config, B3_CONFIG_GENERATOR)) else: config = None for p in ('b3.%s', 'conf/b3.%s', 'b3/conf/b3.%s', os.path.join(HOMEDIR, 'b3.%s'), os.path.join(HOMEDIR, 'conf', 'b3.%s'), os.path.join(HOMEDIR, 'b3', 'conf', 'b3.%s'), '@b3/conf/b3.%s'): for e in ('ini', 'cfg', 'xml'): path = b3.getAbsolutePath(p % e, True) if os.path.isfile(path): print "Using configuration file: %s" % path config = path sleep(3) break if not config: printexit = True console_exit('ERROR: could not find any valid configuration file.\n' 'Please visit %s to create one.' % B3_CONFIG_GENERATOR) # LOADING MAIN CONFIGURATION main_config = b3.config.MainConfig(b3.config.load(config)) analysis = main_config.analyze() if analysis: raise b3.config.ConfigFileNotValid('invalid configuration file specified') # START B3 b3.start(main_config, options) except b3.config.ConfigFileNotValid: if analysis: print 'CRITICAL: invalid configuration file specified:\n' for problem in analysis: print" >>> %s\n" % problem else: print 'CRITICAL: invalid configuration file specified!' raise SystemExit(1) except SystemExit, msg: if not printexit and main_is_frozen(): if sys.stdout != sys.__stdout__: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg raw_input("press any key to continue...") raise
def getB3versionString(): """ Return the B3 version as a string. """ sversion = re.sub(r'\^[0-9a-z]', '', version) if main_is_frozen(): vinfo = getB3versionInfo() sversion = '%s [%s%s]' % (sversion, vinfo[1], vinfo[2]) return sversion
def getB3Path(decode=False): """ Return the path to the main B3 directory. :param decode: if True will decode the path string using the default file system encoding before returning it """ if main_is_frozen(): path = os.path.dirname(sys.executable) else: path = modulePath if not decode: return os.path.normpath(os.path.expanduser(path)) return decode_(os.path.normpath(os.path.expanduser(path)))
def main(): parser = OptionParser(version=b3.getB3versionString()) parser.add_option('-c', '--config', dest='config', default=None, help='B3 config file. Example: -c b3.xml') parser.add_option('-r', '--restart', action='store_true', dest='restart', default=False, help='Auto-restart B3 on crash') parser.add_option('-s', '--setup', action='store_true', dest='setup', default=False, help='Setup main b3.xml config file') parser.add_option('-n', '--nosetup', action="store_true", dest='nosetup', default=False, help='Do not enter setup mode when config is missing') (options, args) = parser.parse_args() if not options.config and len(args) == 1: options.config = args[0] if options.setup: run_setup(config=options.config) if options.restart: if options.config: run_autorestart(['--config', options.config] + args) else: run_autorestart([]) else: try: run(config=options.config, nosetup=options.nosetup) except SystemExit, msg: # This needs some work, is ugly a.t.m. but works... kinda if main_is_frozen(): if sys.stdout != sys.__stdout__: # make sure we are not writing to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg raw_input("Press the [ENTER] key") raise except:
def main(): parser = OptionParser(version=b3.getB3versionString()) parser.add_option('-c', '--config', dest='config', default=None, help='B3 config file. Example: -c b3.xml') parser.add_option('-r', '--restart', action='store_true', dest='restart', default=False, help='Auto-restart B3 on crash') parser.add_option('-s', '--setup', action='store_true', dest='setup', default=False, help='Setup main b3.xml config file') parser.add_option('-u', '--update', action='store_true', dest='update', default=False, help='Update B3 database to latest version') parser.add_option('-n', '--nosetup', action="store_true", dest='nosetup', default=False, help='Do not enter setup mode when config is missing') (options, args) = parser.parse_args() if not options.config and len(args) == 1: options.config = args[0] if options.setup: run_setup(config=options.config) if options.update: run_update(config=options.config) if options.restart: if options.config: run_autorestart(['--config', options.config] + args) else: run_autorestart([]) else: try: run(config=options.config, nosetup=options.nosetup) except SystemExit, msg: # This needs some work, is ugly a.t.m. but works... kinda if main_is_frozen(): if sys.stdout != sys.__stdout__: # make sure we are not writing to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg raw_input("Press the [ENTER] key") raise except:
def getB3Path(): if main_is_frozen(): # which happens when running from the py2exe build return os.path.dirname(sys.executable) return modulePath
def run_autorestart(args=None): """ Run B3 in auto-restart mode. """ restart_num = 0 if main_is_frozen(): # if we are running the frozen application we do not # need to run any script, just the executable itself script = '' else: # if we are running from sources, then sys.executable is set to `python` script = os.path.join(modulePath[:-3], 'b3_run.py') if not os.path.isfile(script): # must be running from the wheel, so there is no b3_run script = os.path.join(modulePath[:-3], 'b3', 'run.py') if os.path.isfile(script + 'c'): script += 'c' if args: script = '%s %s %s --autorestart' % (sys.executable, script, ' '.join(args)) else: script = '%s %s --autorestart' % (sys.executable, script) while True: try: try: import subprocess32 as subprocess except ImportError: import subprocess status = subprocess.call(script, shell=True) sys.stdout.write('Exited with status: %s ... ' % status) sys.stdout.flush() sleep(2) if status == 221: restart_num += 1 sys.stdout.write('restart requested (%s)\n' % restart_num) sys.stdout.flush() elif status == 222: sys.stdout.write('shutdown requested!\n') sys.stdout.flush() break elif status == 220 or status == 223: sys.stdout.write('B3 error (check log file)\n') sys.stdout.flush() break elif status == 224: sys.stdout.write('B3 error (check console)\n') sys.stdout.flush() break elif status == 256: sys.stdout.write('python error, (check log file)\n') sys.stdout.flush() break elif status == 0: sys.stdout.write('normal shutdown\n') sys.stdout.flush() break elif status == 1: sys.stdout.write('general error (check console)\n') sys.stdout.flush() break else: restart_num += 1 sys.stdout.write( 'unknown exit code (%s), restarting (%s)...\n' % (status, restart_num)) sys.stdout.flush() sleep(4) except KeyboardInterrupt: print('Quit') break
def getB3versionString(): sversion = re.sub(r'\^[0-9a-z]', '', version) if main_is_frozen(): sversion = "%s [Win32 standalone]" % sversion return sversion
if options.restart: if options.config: run_autorestart(['--config', options.config] + args) else: run_autorestart([]) else: try: run(config=options.config) except SystemExit, msg: print msg if sys.stdout != sys.__stdout__: # make sure we are not writting to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg except: traceback.print_exc() if sys.stdout != sys.__stdout__: # make sure we are not writting to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ traceback.print_exc() if main_is_frozen(): # which happens when running from the py2exe build # we wait for keyboad keypress to give a chance to the # user to see the error message raw_input("Press the [ENTER] key") if __name__ == '__main__': main()
run(config=options.config, nosetup=options.nosetup) except SystemExit, msg: # This needs some work, is ugly a.t.m. but works... kinda if main_is_frozen(): if sys.stdout != sys.__stdout__: # make sure we are not writing to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ print msg raw_input("Press the [ENTER] key") raise except: if sys.stdout != sys.__stdout__: # make sure we are not writing to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ traceback.print_exc() if main_is_frozen(): # which happens when running from the py2exe build # we wait for keyboad keypress to give a chance to the # user to see the error message if sys.stdout != sys.__stdout__: # make sure we are not writing to the log: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ raw_input("Press the [ENTER] key") if __name__ == '__main__': main()
def getB3Path(self): if main_is_frozen(): # which happens when running from the py2exe build return os.path.dirname(sys.executable) return self.resource_directory('b3')
import os import sys from b3.functions import main_is_frozen __all__ = ['resource_directory'] def resource_directory(module): """ Use this if pkg_resources is NOT installed """ return os.path.dirname(sys.modules[module].__file__) if not main_is_frozen(): try: import pkg_resources except ImportError: pkg_resources = None pass else: # package tools is installed def resource_directory_from_pkg_resources(module): """ Use this if pkg_resources is installed """ return pkg_resources.resource_filename(module, '') resource_directory = resource_directory_from_pkg_resources
def run_autorestart(args=None): _restarts = 0 if main_is_frozen(): script = '' else: script = os.path.join(modulePath[:-3], 'b3_run.py') if not os.path.isfile(script): # must be running from the egg script = os.path.join(modulePath[:-3], 'b3', 'run.py') if os.path.isfile(script + 'c'): script += 'c' if args: script = '%s %s %s' % (sys.executable, script, ' '.join(args)) else: script = '%s %s' % (sys.executable, script) while True: try: print 'Running in auto-restart mode...' if _restarts > 0: print 'Bot restarted %s times.' % _restarts time.sleep(1) try: import subprocess status = subprocess.call(script, shell=True) except ImportError: #for Python versions < 2.5 #status = os.system(script) print 'Restart mode not fully supported!\nUse B3 without the -r (--restart) option or update your python installation!' break print 'Exited with status %s' % status if status == 221: # restart print 'Restart requested...' elif status == 222: # stop print 'Shutdown requested.' break elif status == 220: # stop print 'B3 Error, check log file.' break elif status == 223: # stop print 'B3 Error Restart, check log file.' break elif status == 224: # stop print 'B3 Error, check console.' break elif status == 256: # stop print 'Python error, stopping, check log file.' break elif status == 0: # stop print 'Normal shutdown, stopping.' break elif status == 1: # stop print 'Error, stopping, check console.' break else: print 'Unknown shutdown status (%s), restarting...' % status _restarts += 1 time.sleep(4) except KeyboardInterrupt: print 'Quit' break
def run_autorestart(args=None): """ Run B3 in auto-restart mode. """ restart_num = 0 if main_is_frozen(): # if we are running the frozen application we do not # need to run any script, just the executable itself script = '' else: # if we are running from sources, then sys.executable is set to `python` script = os.path.join(modulePath[:-3], 'b3_run.py') if not os.path.isfile(script): # must be running from the wheel, so there is no b3_run script = os.path.join(modulePath[:-3], 'b3', 'run.py') if os.path.isfile(script + 'c'): script += 'c' if args: script = '%s %s %s --autorestart' % (sys.executable, script, ' '.join(args)) else: script = '%s %s --autorestart' % (sys.executable, script) while True: try: try: import subprocess32 as subprocess except ImportError: import subprocess status = subprocess.call(script, shell=True) sys.stdout.write('Exited with status: %s ... ' % status) sys.stdout.flush() sleep(2) if status == 221: restart_num += 1 sys.stdout.write('restart requested (%s)\n' % restart_num) sys.stdout.flush() elif status == 222: sys.stdout.write('shutdown requested!\n') sys.stdout.flush() break elif status == 220 or status == 223: sys.stdout.write('B3 error (check log file)\n') sys.stdout.flush() break elif status == 224: sys.stdout.write('B3 error (check console)\n') sys.stdout.flush() break elif status == 256: sys.stdout.write('python error, (check log file)\n') sys.stdout.flush() break elif status == 0: sys.stdout.write('normal shutdown\n') sys.stdout.flush() break elif status == 1: sys.stdout.write('general error (check console)\n') sys.stdout.flush() break else: restart_num += 1 sys.stdout.write('unknown exit code (%s), restarting (%s)...\n' % (status, restart_num)) sys.stdout.flush() sleep(4) except KeyboardInterrupt: print 'Quit' break
def run_autorestart(args=None): _restarts = 0 if main_is_frozen(): script = '' else: script = os.path.join(modulePath[:-3], 'b3_run.py') if os.path.isfile(script + 'c'): script += 'c' if args: script = '%s %s %s' % (sys.executable, script, ' '.join(args)) else: script = '%s %s' % (sys.executable, script) while True: try: print 'Running in auto-restart mode...' if _restarts > 0: print 'Bot restarted %s times.' %_restarts time.sleep(1) try: import subprocess status = subprocess.call(script, shell=True) except ImportError: #for Python versions < 2.5 #status = os.system(script) print 'Restart mode not fully supported!\nUse B3 without the -r (--restart) option or update your python installation!' break print 'Exited with status %s' % status if status == 221: # restart print 'Restart requested...' elif status == 222: # stop print 'Shutdown requested.' break elif status == 220: # stop print 'B3 Error, check log file.' break elif status == 223: # stop print 'B3 Error Restart, check log file.' break elif status == 224: # stop print 'B3 Error, check console.' break elif status == 256: # stop print 'Python error, stopping, check log file.' break elif status == 0: # stop print 'Normal shutdown, stopping.' break elif status == 1: # stop print 'Error, stopping, check console.' break else: print 'Unknown shutdown status (%s), restarting...' % status _restarts += 1 time.sleep(4) except KeyboardInterrupt: print 'Quit' break