def main(): """ Main execution. """ p = argparse.ArgumentParser() p.add_argument('-c', '--config', dest='config', default=None, metavar='b3.ini', help='B3 config file. Example: -c b3.ini') p.add_argument('-r', '--restart', action='store_true', dest='restart', default=False, help='Auto-restart B3 on crash') p.add_argument('-s', '--setup', action='store_true', dest='setup', default=False, help='Setup main b3.ini config file') p.add_argument('-u', '--update', action='store_true', dest='update', default=False, help='Update B3 database to latest version') p.add_argument('-v', '--version', action='version', default=False, version=b3.getB3versionString(), help='Show B3 version and exit') p.add_argument('-a', '--autorestart', action='store_true', dest='autorestart', default=False, help=argparse.SUPPRESS) (options, args) = p.parse_known_args() if not options.config and len(args) == 1: options.config = args[0] if options.setup: # setup procedure is deprecated: show configuration file generator web tool url instead sys.stdout.write('\n') console_exit(" *** NOTICE: the console setup procedure is deprecated!\n" \ " *** Please visit %s to generate a new B3 configuration file.\n" % B3_CONFIG_GENERATOR) if options.update: ## UPDATE => CONSOLE run_update(config=options.config) if options.restart: ## AUTORESTART => CONSOLE if options.config: run_autorestart(['--config', options.config] + args) else: run_autorestart([]) else: run(options)
def main(): """ Main execution. """ p = argparse.ArgumentParser() p.add_argument('-c', '--config', dest='config', default=None, metavar='b3.ini', help='B3 config file. Example: -c b3.ini') p.add_argument('-r', '--restart', action='store_true', dest='restart', default=False, help='Auto-restart B3 on crash') p.add_argument('-s', '--setup', action='store_true', dest='setup', default=False, help='Setup main b3.ini config file') p.add_argument('-u', '--update', action='store_true', dest='update', default=False, help='Update B3 database to latest version') p.add_argument('-v', '--version', action='version', default=False, version=b3.getB3versionString(), help='Show B3 version and exit') p.add_argument('-a', '--autorestart', action='store_true', dest='autorestart', default=False, help=argparse.SUPPRESS) (options, args) = p.parse_known_args() if not options.config and len(args) == 1: options.config = args[0] if options.setup: # setup procedure is deprecated: show configuration file generator web tool url instead sys.stdout.write('\n') console_exit(" *** NOTICE: the console setup procedure is deprecated!\n" \ " *** Please visit %s to generate a new B3 configuration file.\n" % B3_CONFIG_GENERATOR) if options.update: ## UPDATE => CONSOLE run_update(config=options.config) if options.restart: ## AUTORESTART => CONSOLE if options.config: run_autorestart(['--config', options.config] + args) else: run_autorestart([]) else: run_console(options)
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 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(self): """ Run the DB update """ clearscreen() print(""" _\|/_ (o o) {:>32} +----oOO---OOo----------------------------------+ | | | UPDATING B3 DATABASE | | | +-----------------------------------------------+ """.format('B3 : %s' % b3.__version__)) input("press any key to start the update...") def _update_database(storage, update_version): """ Update a B3 database. :param storage: the initialized storage module :param update_version: the update version """ if B3version(b3.__version__) >= update_version: sql = b3.getAbsolutePath('@b3/sql/%s/b3-update-%s.sql' % (storage.protocol, update_version)) if os.path.isfile(sql): try: print('>>> updating database to version %s' % update_version) sleep(.5) storage.queryFromFile(sql) except Exception as err: print( 'WARNING: could not update database properly: %s' % err) sleep(3) dsn = self.config.get('b3', 'database') dsndict = splitDSN(dsn) database = getStorage(dsn, dsndict, StubParser()) _update_database(database, '1.3.0') _update_database(database, '1.6.0') _update_database(database, '1.7.0') _update_database(database, '1.8.1') _update_database(database, '1.9.0') _update_database(database, '1.10.0') console_exit('B3 database update completed!')
def __init__(self, config=None): """ Object constructor. :param config: The B3 configuration file path """ if config: # use the specified configuration file config = b3.getAbsolutePath(config, True) if not os.path.isfile(config): console_exit('ERROR: configuration file not found (%s).\n' 'Please visit %s to create one.' % (config, B3_CONFIG_GENERATOR)) else: # search a configuration file 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: console_exit('ERROR: could not find any valid configuration file.\n' 'Please visit %s to create one.' % B3_CONFIG_GENERATOR) try: self.config = b3.config.MainConfig(b3.config.load(config)) if self.config.analyze(): raise b3.config.ConfigFileNotValid except b3.config.ConfigFileNotValid: console_exit('ERROR: configuration file not valid (%s).\n' 'Please visit %s to generate a new one.' % (config, B3_CONFIG_GENERATOR))
class DBUpdate(object): """ Console database update procedure. """ def __init__(self, config=None): """ Object constructor. :param config: The B3 configuration file path """ if config: # use the specified configuration file config = b3.getAbsolutePath(config, True) if not os.path.isfile(config): console_exit('ERROR: configuration file not found (%s).\n' 'Please visit %s to create one.' % (config, B3_CONFIG_GENERATOR)) else: # search a configuration file 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: console_exit('ERROR: could not find any valid configuration file.\n' 'Please visit %s to create one.' % B3_CONFIG_GENERATOR) try: self.config = b3.config.MainConfig(b3.config.load(config)) if self.config.analyze(): raise b3.config.ConfigFileNotValid except b3.config.ConfigFileNotValid: console_exit('ERROR: configuration file not valid (%s).\n' 'Please visit %s to generate a new one.' % (config, B3_CONFIG_GENERATOR)) def run(self): """ Run the DB update """ clearscreen() print """ _\|/_ (o o) {:>32} +----oOO---OOo----------------------------------+ | | | UPDATING B3 DATABASE | | | +-----------------------------------------------+ """.format('B3 : %s' % b3.__version__) raw_input("press any key to start the update...") def _update_database(storage, update_version): """ Update a B3 database. :param storage: the initialized storage module :param update_version: the update version """ if B3version(b3.__version__) >= update_version: sql = b3.getAbsolutePath('@b3/sql/%s/b3-update-%s.sql' % (storage.protocol, update_version)) if os.path.isfile(sql): try: print '>>> updating database to version %s' % update_version sleep(.5) storage.queryFromFile(sql) except Exception, err: print 'WARNING: could not update database properly: %s' % err sleep(3) dsn = self.config.get('b3', 'database') dsndict = splitDSN(dsn) database = getStorage(dsn, dsndict, StubParser()) _update_database(database, '1.3.0') _update_database(database, '1.6.0') _update_database(database, '1.7.0') _update_database(database, '1.8.1') _update_database(database, '1.9.0') _update_database(database, '1.10.0') console_exit('B3 database update completed!')