Esempio n. 1
0
def main():
    """
    The entrypoint to the Piccolo CLI.
    """
    # In case it's run from an entrypoint:
    sys.path.insert(0, os.getcwd())

    ###########################################################################
    # Run in diagnose mode if requested.

    diagnose = get_diagnose_flag()
    if diagnose:
        print("Diagnosis...")
        if Finder(diagnose=True).get_app_registry():
            print("Everything OK")
        return

    ###########################################################################

    cli = CLI(description="Piccolo CLI")

    ###########################################################################
    # Register the base apps.

    for _app_config in [
            app_config,
            asgi_config,
            meta_config,
            migrations_config,
            playground_config,
            project_config,
            shell_config,
            sql_shell_config,
            user_config,
    ]:
        for command in _app_config.commands:
            cli.register(command, group_name=_app_config.app_name)

    ###########################################################################
    # Get user defined apps.

    try:
        APP_REGISTRY: AppRegistry = Finder().get_app_registry()
    except (ImportError, AttributeError):
        print("Can't import the APP_REGISTRY from piccolo_conf - some "
              "commands may be missing. If this is a new project don't worry. "
              f"To see a full traceback use `piccolo {DIAGNOSE_FLAG}`")
    else:
        for app_name, _app_config in APP_REGISTRY.app_configs.items():
            for command in _app_config.commands:
                if cli.command_exists(group_name=app_name,
                                      command_name=command.__name__):
                    # Skipping - already registered.
                    continue
                cli.register(command, group_name=app_name)

    ###########################################################################

    cli.run()
Esempio n. 2
0
def main():
    """
    The entrypoint to the Piccolo CLI.
    """
    # In case it's run from an entrypoint:
    sys.path.insert(0, os.getcwd())

    ###########################################################################
    # Run in diagnose mode if requested.

    diagnose = get_diagnose_flag()
    if diagnose:
        print("Diagnosis...")
        if Finder(diagnose=True).get_app_registry():
            print("Everything OK")
        return

    ###########################################################################

    cli = CLI(description="Piccolo CLI")

    ###########################################################################
    # Register the base apps.

    for _app_config in [
            app_config,
            asgi_config,
            fixtures_config,
            meta_config,
            migrations_config,
            playground_config,
            project_config,
            schema_config,
            shell_config,
            sql_shell_config,
            tester_config,
            user_config,
    ]:
        for command in _app_config.commands:
            cli.register(
                command.callable,
                group_name=_app_config.app_name,
                aliases=command.aliases,
            )

    ###########################################################################
    # Get user defined apps.

    try:
        APP_REGISTRY: AppRegistry = Finder().get_app_registry()
    except (ImportError, AttributeError):
        print("Can't import the APP_REGISTRY from piccolo_conf - some "
              "commands may be missing. If this is a new project don't worry. "
              f"To see a full traceback use `piccolo {DIAGNOSE_FLAG}`")
    else:
        for app_name, _app_config in APP_REGISTRY.app_configs.items():
            for command in _app_config.commands:
                if cli.command_exists(group_name=app_name,
                                      command_name=command.callable.__name__):
                    # Skipping - already registered.
                    continue
                cli.register(
                    command.callable,
                    group_name=app_name,
                    aliases=command.aliases,
                )

        if "migrations" not in sys.argv:
            # Show a warning if any migrations haven't been run.
            # Don't run it if it looks like the user is running a migration
            # command, as this information is redundant.

            try:
                havent_ran_count = run_sync(
                    CheckMigrationManager(app_name="all").havent_ran_count())
                if havent_ran_count:
                    message = (f"{havent_ran_count} migration hasn't"
                               if havent_ran_count == 1 else
                               f"{havent_ran_count} migrations haven't")

                    colored_warning(
                        message=("=> {} been run - the app "
                                 "might not behave as expected.\n"
                                 "To check which use:\n"
                                 "    piccolo migrations check\n"
                                 "To run all migrations:\n"
                                 "    piccolo migrations forwards all\n"
                                 ).format(message),
                        level=Level.high,
                    )
            except Exception:
                pass

    ###########################################################################

    cli.run()