예제 #1
0
def init():

    # create the database
    db.configure_mappers()
    db.create_all()

    # create the super user
    u = User(name=settings.SUPER_USER,
             email=settings.SUPER_USER_EMAIL,
             password=settings.SUPER_USER_PASSWORD,
             admin=True,
             super_user=True)

    # optionally add super user apikey
    if getattr(settings, 'SUPER_USER_APIKEY', None):
        u.apikey = settings.SUPER_USER_APIKEY
    db_session.add(u)

    # load sql extensions + functions
    for sql in load_sql():
        db_session.execute(sql)

    # load built-in sous-chefs
    for sc in load_sous_chefs():
        sc = sous_chef_schema.validate(sc)
        s = SousChef(**sc)
        db_session.add(s)

    # commit
    db_session.commit()
예제 #2
0
def run(opts, **kwargs):
    # create the database
    if opts and not opts.log_type == 'json' and not opts.dev:
        print LOGO
    with app.app_context():

        # import defaults
        from newslynx.defaults import _CONFIG_REQUIRES, _DEFAULT_DEFAULTS
        from newslynx.settings import CONFIG_FILE as config_file
        from newslynx.settings import DEFAULT_TAGS as tags_file
        from newslynx.settings import DEFAULT_RECIPES as recipes_file
        from newslynx.core import settings

        try:
            log.info('(Re)Creating database "{}"'.format(
                settings.SQLALCHEMY_DATABASE_URI))
            db.configure_mappers()
            db.create_all()

            log.info('(Re)Loading SQL Extensions')

            # load sql extensions + functions
            for sql in load_sql():
                db.session.execute(sql)
            # install app defaults.
            if (not opts or not opts.bare) and not kwargs.get('empty', False):
                if not kwargs.get('empty', False):
                    log.info('(Re)Initializing App Defaults')
                modules = [('default_tags', tags_file),
                           ('default_recipes', recipes_file)]
                conf_str = open(config_file).read()
                for k, m in modules:
                    m = os.path.expanduser(m)
                    parts = m.split('/')
                    default_dir = "/".join(parts[:-1])

                    path = parts[-1]
                    name = parts[-1].split('.')[0]
                    try:
                        os.makedirs(default_dir)
                    except OSError:
                        pass

                    log.info('Storing default {} in: {}'.format(
                        name, config_file))

                    with open(m, 'wb') as f1:
                        with open(os.path.join(_DEFAULT_DEFAULTS, path),
                                  'rb') as f2:
                            f1.write(f2.read())

                    cx = re.compile(re_conf.format(k))
                    newval = "{}: {}".format(k, m)
                    m = cx.search(conf_str)
                    if m:
                        conf_str = cx.sub(newval, conf_str)
                    else:
                        conf_str += "\n" + newval
                if not kwargs.get('empty', False):
                    log.info('Storing new configurations to: {}'.format(
                        config_file))
                with open(config_file, 'wb') as f:
                    f.write(conf_str)

            if not kwargs.get('empty', False):
                log.info('(Re)Initializing Super User Org {}'.format(
                    settings.SUPER_USER_ORG))
                default.org()

        except Exception as e:
            db.session.rollback()
            db.session.close()
            log.error(format_exc())
            sys.exit(1)

        else:
            if not kwargs.get('empty', False):
                log.info('Success!')
                log.info(
                    'You can now start the API by running: $ newslynx debug')
예제 #3
0
def run(opts, **kwargs):
    # create the database
    if opts and not opts.log_type == 'json' and not opts.dev:
        print LOGO
    with app.app_context():

        # import defaults
        from newslynx.defaults import _CONFIG_REQUIRES, _DEFAULT_DEFAULTS
        from newslynx.settings import CONFIG_FILE as config_file
        from newslynx.settings import DEFAULT_TAGS as tags_file
        from newslynx.settings import DEFAULT_RECIPES as recipes_file
        from newslynx.core import settings

        try:
            log.info('(Re)Creating database "{}"'.format(
                settings.SQLALCHEMY_DATABASE_URI))
            db.configure_mappers()
            db.create_all()

            log.info('(Re)Loading SQL Extensions')

            # load sql extensions + functions
            for sql in load_sql():
                db.session.execute(sql)
            # install app defaults.
            if (not opts or not opts.bare) and not kwargs.get('empty', False):
                if not kwargs.get('empty', False):
                    log.info('(Re)Initializing App Defaults')
                modules = [
                    ('default_tags', tags_file),
                    ('default_recipes', recipes_file)
                ]
                conf_str = open(config_file).read()
                for k, m in modules:
                    m = os.path.expanduser(m)
                    parts = m.split('/')
                    default_dir = "/".join(parts[:-1])

                    path = parts[-1]
                    name = parts[-1].split('.')[0]
                    try:
                        os.makedirs(default_dir)
                    except OSError:
                        pass

                    log.info(
                        'Storing default {} in: {}'.format(name, config_file))

                    with open(m, 'wb') as f1:
                        with open(os.path.join(_DEFAULT_DEFAULTS, path), 'rb') as f2:
                            f1.write(f2.read())

                    cx = re.compile(re_conf.format(k))
                    newval = "{}: {}".format(k, m)
                    m = cx.search(conf_str)
                    if m:
                        conf_str = cx.sub(newval, conf_str)
                    else:
                        conf_str += "\n" + newval
                if not kwargs.get('empty', False):
                    log.info(
                        'Storing new configurations to: {}'.format(config_file))
                with open(config_file, 'wb') as f:
                    f.write(conf_str)

            if not kwargs.get('empty', False):
                log.info(
                    '(Re)Initializing Super User Org {}'.format(settings.SUPER_USER_ORG))
                default.org()

        except Exception as e:
            db.session.rollback()
            db.session.close()
            log.error(format_exc())
            sys.exit(1)

        else:
            if not kwargs.get('empty', False):
                log.info('Success!')
                log.info(
                    'You can now start the API by running: $ newslynx debug')
예제 #4
0
파일: init.py 프로젝트: eads/newslynx-core
def run(opts, **kwargs):
    # create the database
    try:
        with app.app_context():
            echo('Creating database "{}"'.format(settings.SQLALCHEMY_DATABASE_URI), 
                no_color=opts.no_color)
            db.configure_mappers()
            db.create_all()
            
            # create the super user
            u = User.query.filter_by(email=settings.SUPER_USER_EMAIL).first()
            if not u:
                echo('Creating super user "{}"'.format(settings.SUPER_USER_EMAIL),
                    no_color=opts.no_color)
                u = User(name=settings.SUPER_USER,
                         email=settings.SUPER_USER_EMAIL,
                         password=settings.SUPER_USER_PASSWORD,
                         admin=True,
                         super_user=True)

                # optionally add super user apikey
                if getattr(settings, 'SUPER_USER_APIKEY', None):
                    u.apikey = settings.SUPER_USER_APIKEY
            else:
                echo('Updating super user "{}"'.format(settings.SUPER_USER_EMAIL), 
                    no_color=opts.no_color)
                u.name=settings.SUPER_USER,
                u.email=settings.SUPER_USER_EMAIL,
                u.password=settings.SUPER_USER_PASSWORD,
                u.admin=True
                super_user=True
            db.session.add(u)

            echo('(Re)Loading SQL Extensions', no_color=opts.no_color)
            # load sql extensions + functions
            for sql in load_sql():
                db.session.execute(sql)

            # load built-in sous-chefs
            for sc in load_sous_chefs():
                sc = sous_chef_schema.validate(sc)

                sc_obj = db.session.query(SousChef).filter_by(slug=sc['slug']).first()
                if not sc_obj:
                    echo('Importing Sous Chef "{}"'.format(sc['slug']),
                        no_color=opts.no_color)
                    sc_obj = SousChef(**sc)
                
                else:
                    echo('Updating Sous Chef "{}"'.format(sc['slug']),
                        no_color=opts.no_color)
                    sc = sous_chef_schema.update(sc_obj.to_dict(), sc)
                    # udpate
                    for name, value in sc.items():
                        setattr(sc_obj, name, value)
                db.session.add(sc_obj)

            # commit
            db.session.commit()
            db.session.close()

    except Exception as e:
        db.session.rollback()
        db.session.close()
        raise e