Skip to content

Commit

Permalink
add grouped magpie-helper cli + all specific cli helpers prefixed wit…
Browse files Browse the repository at this point in the history
…h magpie_ when installed
  • Loading branch information
fmigneault committed Jun 18, 2020
1 parent 338629d commit 552fd6d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 17 deletions.
46 changes: 46 additions & 0 deletions magpie/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import argparse
import importlib
import os
import sys

from magpie.__meta__ import __version__


def magpie_helper_cli():
"""
Groups all sub-helper CLI listed in :py:mod:`magpie.helpers` as a common ``magpie_helper``.
Dispatches the provided arguments to the appropriate sub-helper CLI as requested. Each sub-helper CLI must implement
functions ``make_parser`` and ``main`` to generate the arguments and dispatch them to the corresponding caller.
"""
parser = argparse.ArgumentParser(description="Execute Magpie helper operations.")
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__),
help="prints the version of the library and exits")
subparsers = parser.add_subparsers(title="Helper", dest="helper", description="Name of the helper to execute.")
helpers_dir = os.path.dirname(__file__)
helper_mods = os.listdir(helpers_dir)
helpers = dict()
for module_item in sorted(helper_mods):
helper_path = os.path.join(helpers_dir, module_item)
if os.path.isfile(helper_path) and "__init__" not in module_item and module_item.endswith(".py"):
helper_name = module_item.replace(".py", "")
helper_module = importlib.import_module(helper_name, "magpie.helpers")
parser_maker = getattr(helper_module, "make_parser", None)
helper_caller = getattr(helper_module, "main", None)
if parser_maker and helper_caller:
# add help disabled otherwise conflicts with this main helper's help
helper_parser = parser_maker()
subparsers.add_parser(helper_name, parents=[helper_parser], add_help=False)
helpers[helper_name] = {"caller": helper_caller, "parser": helper_parser}
args = sys.argv[1:] # save as was parse args does, but we must provide them to subparser
ns = parser.parse_args() # if 'helper' is unknown, auto prints the help message with exit(2)
helper_name = vars(ns).pop("helper")
helper_args = args[1:]
helper_caller = helpers[helper_name]["caller"]
helper_parser = helpers[helper_name]["parser"]
result = helper_caller(args=helper_args, parser=helper_parser, namespace=ns)
return 0 if result is None else result


if __name__ == "__main__":
magpie_helper_cli()
18 changes: 15 additions & 3 deletions magpie/helpers/create_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import logging
import random
import string
from typing import TYPE_CHECKING

import requests

if TYPE_CHECKING:
from typing import Any, AnyStr, Optional, Sequence

LOGGER = logging.getLogger(__name__)
COLUMN_SIZE = 60

Expand Down Expand Up @@ -75,13 +79,21 @@ def delete_users(user_names, magpie_url, magpie_admin_user_name, magpie_admin_pa
return users


def main():
parser = argparse.ArgumentParser(description="Create users on Magpie")
def make_parser():
# type: () -> argparse.ArgumentParser
parser = argparse.ArgumentParser(description="Create users on a running Magpie instance")
parser.add_argument("url", help="url used to access the magpie service")
parser.add_argument("user_name", help="admin username for magpie login")
parser.add_argument("password", help="admin password for magpie login")
parser.add_argument("emails", nargs="*", help="list of emails for users to be created")
args = parser.parse_args()
return parser


def main(args=None, parser=None, namespace=None):
# type: (Optional[Sequence[AnyStr]], Optional[argparse.ArgumentParser], Optional[argparse.Namespace]) -> Any
if not parser:
parser = make_parser()
args = parser.parse_args(args=args, namespace=namespace)

LOGGER.setLevel(logging.DEBUG)
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S")
Expand Down
25 changes: 22 additions & 3 deletions magpie/helpers/register_default_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import logging
import time
from typing import TYPE_CHECKING
Expand All @@ -15,6 +16,8 @@
if TYPE_CHECKING:
# pylint: disable=W0611,unused-import
from magpie.typedefs import AnySettingsContainer, Str, Optional # noqa: F401
from typing import Any, AnyStr, Optional, Sequence # noqa: F401

LOGGER = get_logger(__name__)


Expand Down Expand Up @@ -119,8 +122,8 @@ def init_users_group(db_session, settings=None):
print_log("MAGPIE_USERS_GROUP already initialized", level=logging.DEBUG)


def register_default_users(db_session=None, settings=None):
# type: (Optional[Session], Optional[AnySettingsContainer]) -> None
def register_default_users(db_session=None, settings=None, ini_file_path=None):
# type: (Optional[Session], Optional[AnySettingsContainer], Optional[AnyStr]) -> None
"""
Registers in db every undefined default users and groups matching following variables :
Expand All @@ -130,7 +133,8 @@ def register_default_users(db_session=None, settings=None):
- ``MAGPIE_ADMIN_USER``
"""
if not isinstance(db_session, Session):
ini_file_path = get_constant("MAGPIE_INI_FILE_PATH", settings_container=settings)
if not ini_file_path:
ini_file_path = get_constant("MAGPIE_INI_FILE_PATH", settings_container=settings)
db_session = db.get_db_session_from_config_ini(ini_file_path)
if not db.is_database_ready(db_session):
time.sleep(2)
Expand All @@ -143,5 +147,20 @@ def register_default_users(db_session=None, settings=None):
db_session.close()


def make_parser():
# type: () -> argparse.ArgumentParser
parser = argparse.ArgumentParser(description="Registers default users in Magpie")
parser.add_argument("ini_file_path", help="Path of the configuration INI file to use to retrieve required settings")
return parser


def main(args=None, parser=None, namespace=None):
# type: (Optional[Sequence[AnyStr]], Optional[argparse.ArgumentParser], Optional[argparse.Namespace]) -> Any
if not parser:
parser = make_parser()
args = parser.parse_args(args=args, namespace=namespace)
return register_default_users(ini_file_path=args.ini_file_path)


if __name__ == "__main__":
register_default_users()
25 changes: 19 additions & 6 deletions magpie/helpers/run_db_migration.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import argparse
from typing import TYPE_CHECKING

from magpie.constants import MAGPIE_INI_FILE_PATH
from magpie.db import run_database_migration

if TYPE_CHECKING:
# pylint: disable=W0611,unused-import
from typing import Any, AnyStr, Optional, Sequence # noqa: F401

def run_database_migration_cli():
parser = argparse.ArgumentParser(description="Run Magpie database migration")

def make_parser():
# type: () -> argparse.ArgumentParser
parser = argparse.ArgumentParser(description="Run Magpie database migration.")
parser.add_argument("-c", "--config-file", metavar="config_file", dest="config_file", type=str,
default=MAGPIE_INI_FILE_PATH,
help="configuration file to employ for database connection settings "
help="Configuration file to employ for database connection settings "
"(default: MAGPIE_INI_FILE_PATH='%(default)s)'")
args = parser.parse_args()
run_database_migration(settings={"magpie.ini_file_path": args.config_file})
return parser


def main(args=None, parser=None, namespace=None):
# type: (Optional[Sequence[AnyStr]], Optional[argparse.ArgumentParser], Optional[argparse.Namespace]) -> Any
if not parser:
parser = make_parser()
args = parser.parse_args(args=args, namespace=namespace)
return run_database_migration(settings={"magpie.ini_file_path": args.config_file})


if __name__ == "__main__":
run_database_migration_cli()
main()
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ def _extra_requirements(base_requirements, other_requirements):
"main = magpie.app:main"
],
"console_scripts": [
"create_users = magpie.helpers.create_users:main",
"register_default_users = magpie.helpers.register_default_users:register_default_users",
"register_providers = magpie.helpers.register_providers:main",
"run_database_migration = magpie.helpers.run_database_migration:run_database_migration",
"sync_resources = magpie.helpers.sync_resources:main",
"magpie_helper = magpie.helpers:magpie_helper_cli" # redirect to others below
"magpie_create_users = magpie.helpers.create_users:main",
"magpie_register_default_users = magpie.helpers.register_default_users:main",
"magpie_register_providers = magpie.helpers.register_providers:main",
"magpie_run_database_migration = magpie.helpers.run_database_migration:main",
"magpie_sync_resources = magpie.helpers.sync_resources:main",
],
}
)
Expand Down

0 comments on commit 552fd6d

Please sign in to comment.