Changeset - 3070f9bc6ebe
[Not reviewed]
default
0 2 0
Mads Kiilerich (mads) - 6 years ago 2020-08-24 13:07:08
mads@kiilerich.com
Grafted from: 03aff38b214a
cli: refactor db_create to avoid app initialization in multiple places

Make sure kallithea.config.application.make_app only is invoked at a high level
... and avoid references to kallithea.CONFIG.global_conf right before
kallithea.CONFIG is set to a plain dict again.

Click commands that have both needs_config_file=True and
config_file_initialize_app=True will now be called twice - first time before
setting kallithea.CONFIG but with the config dict as parameter.

That seems kind of intuitive and will simplify the code and allow cleanup of
config handling.

Mute pytype warning:

File "kallithea/bin/kallithea_cli_db.py", line 73, in db_create: No attribute 'global_conf' on Dict[nothing, nothing] [attribute-error]
File "kallithea/bin/kallithea_cli_db.py", line 73, in db_create: No attribute 'local_conf' on Dict[nothing, nothing] [attribute-error]
2 files changed with 40 insertions and 39 deletions:
0 comments (0 inline, 0 general)
kallithea/bin/kallithea_cli_base.py
Show inline comments
 
@@ -77,6 +77,8 @@ def register_command(needs_config_file=F
 
                logging.config.fileConfig(cp,
 
                    {'__file__': path_to_ini_file, 'here': os.path.dirname(path_to_ini_file)})
 
                if config_file_initialize_app:
 
                    if needs_config_file:  # special case for db creation: also call annotated function (with config parameter) *before* app initialization
 
                        annotated(*args, config=kallithea.CONFIG, **kwargs)
 
                    kallithea.config.application.make_app(kallithea.CONFIG.global_conf, **kallithea.CONFIG.local_conf)
 
                return annotated(*args, **kwargs)
 
            return cli_command(runtime_wrapper)
kallithea/bin/kallithea_cli_db.py
Show inline comments
 
@@ -19,7 +19,7 @@ from kallithea.lib.db_manage import DbMa
 
from kallithea.model.meta import Session
 

	
 

	
 
@cli_base.register_command(needs_config_file=True)
 
@cli_base.register_command(needs_config_file=True, config_file_initialize_app=True)
 
@click.option('--reuse/--no-reuse', default=False,
 
        help='Reuse and clean existing database instead of dropping and creating (default: no reuse)')
 
@click.option('--user', help='Username of administrator account.')
 
@@ -30,7 +30,7 @@ from kallithea.model.meta import Session
 
@click.option('--force-no', is_flag=True, help='Answer no to every question.')
 
@click.option('--public-access/--no-public-access', default=True,
 
        help='Enable/disable public access on this installation (default: enable)')
 
def db_create(user, password, email, repos, force_yes, force_no, public_access, reuse):
 
def db_create(user, password, email, repos, force_yes, force_no, public_access, reuse, config=None):
 
    """Initialize the database.
 

	
 
    Create all required tables in the database specified in the configuration
 
@@ -39,44 +39,43 @@ def db_create(user, password, email, rep
 

	
 
    You can pass the answers to all questions as options to this command.
 
    """
 
    dbconf = kallithea.CONFIG['sqlalchemy.url']
 
    if config is not None:  # first called with config, before app initialization
 
        dbconf = config['sqlalchemy.url']
 

	
 
    # force_ask should be True (yes), False (no), or None (ask)
 
    if force_yes:
 
        force_ask = True
 
    elif force_no:
 
        force_ask = False
 
    else:
 
        force_ask = None
 
        # force_ask should be True (yes), False (no), or None (ask)
 
        if force_yes:
 
            force_ask = True
 
        elif force_no:
 
            force_ask = False
 
        else:
 
            force_ask = None
 

	
 
    cli_args = dict(
 
            username=user,
 
            password=password,
 
            email=email,
 
            repos_location=repos,
 
            force_ask=force_ask,
 
            public_access=public_access,
 
    )
 
    dbmanage = DbManage(dbconf=dbconf, root=kallithea.CONFIG['here'],
 
                        tests=False, cli_args=cli_args)
 
    dbmanage.create_tables(reuse_database=reuse)
 
    repo_root_path = dbmanage.prompt_repo_root_path(None)
 
    dbmanage.create_settings(repo_root_path)
 
    dbmanage.create_default_user()
 
    dbmanage.admin_prompt()
 
    dbmanage.create_permissions()
 
    dbmanage.populate_default_permissions()
 
    Session().commit()
 
        cli_args = dict(
 
                username=user,
 
                password=password,
 
                email=email,
 
                repos_location=repos,
 
                force_ask=force_ask,
 
                public_access=public_access,
 
        )
 
        dbmanage = DbManage(dbconf=dbconf, root=config['here'],
 
                            tests=False, cli_args=cli_args)
 
        dbmanage.create_tables(reuse_database=reuse)
 
        repo_root_path = dbmanage.prompt_repo_root_path(None)
 
        dbmanage.create_settings(repo_root_path)
 
        dbmanage.create_default_user()
 
        dbmanage.admin_prompt()
 
        dbmanage.create_permissions()
 
        dbmanage.populate_default_permissions()
 
        Session().commit()
 

	
 
    # initial repository scan
 
    kallithea.config.application.make_app(
 
            kallithea.CONFIG.global_conf, **kallithea.CONFIG.local_conf)
 
    added, _ = kallithea.lib.utils.repo2db_mapper(kallithea.model.scm.ScmModel().repo_scan())
 
    if added:
 
        click.echo('Initial repository scan: added following repositories:')
 
        click.echo('\t%s' % '\n\t'.join(added))
 
    else:
 
        click.echo('Initial repository scan: no repositories found.')
 
    else:  # then called again after app initialization
 
        added, _ = kallithea.lib.utils.repo2db_mapper(kallithea.model.scm.ScmModel().repo_scan())
 
        if added:
 
            click.echo('Initial repository scan: added following repositories:')
 
            click.echo('\t%s' % '\n\t'.join(added))
 
        else:
 
            click.echo('Initial repository scan: no repositories found.')
 

	
 
    click.echo('Database set up successfully.')
 
    click.echo("Don't forget to build the front-end using 'kallithea-cli front-end-build'.")
 
        click.echo('Database set up successfully.')
 
        click.echo("Don't forget to build the front-end using 'kallithea-cli front-end-build'.")
0 comments (0 inline, 0 general)