cli.py 2.87 KB
Newer Older
Lukáš Lalinský's avatar
Lukáš Lalinský committed
1
import os
2
import importlib
3
import click
Lukáš Lalinský's avatar
Lukáš Lalinský committed
4
from typing import Optional
5
from acoustid.script import Script
Lukáš Lalinský's avatar
Lukáš Lalinský committed
6
from acoustid.wsgi_utils import run_web_app, run_api_app
7
from acoustid.cron import run_cron
8
from acoustid.scripts.import_submissions import run_import
9 10 11 12


@click.group()
def cli():
13
    # type: () -> None
14 15 16 17 18
    pass


@cli.group()
def run():
19
    # type: () -> None
20 21 22 23 24
    pass


@run.command('web')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
Lukáš Lalinský's avatar
Lukáš Lalinský committed
25 26 27 28
@click.option('-w', '--workers', type=int)
@click.option('-t', '--threads', type=int)
def run_web_cmd(config, workers=None, threads=None):
    # type: (str, Optional[int], Optional[int]) -> None
29
    """Run production uWSGI with the website."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
30
    os.environ['ACOUSTID_CONFIG'] = config
31 32 33
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
Lukáš Lalinský's avatar
Lukáš Lalinský committed
34
    run_web_app(script.config, workers=workers, threads=threads)
35 36 37 38


@run.command('api')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
Lukáš Lalinský's avatar
Lukáš Lalinský committed
39 40 41 42
@click.option('-w', '--workers', type=int)
@click.option('-t', '--threads', type=int)
def run_api_cmd(config, workers=None, threads=None):
    # type: (str, Optional[int], Optional[int]) -> None
43
    """Run production uWSGI with the API."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
44
    os.environ['ACOUSTID_CONFIG'] = config
45 46 47
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
Lukáš Lalinský's avatar
Lukáš Lalinský committed
48
    run_api_app(script.config, workers=workers, threads=threads)
49 50 51 52 53


@run.command('cron')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_cron_cmd(config):
54
    # type: (str) -> None
55 56 57 58
    """Run cron."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
59 60 61 62 63 64
    run_cron(script)


@run.command('import')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_import_cmd(config):
65
    # type: (str) -> None
66 67 68 69 70
    """Run import."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_import(script)
71 72


73 74 75 76
@run.command('script')
@click.argument('name')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_script_cmd(name, config):
Lukáš Lalinský's avatar
Lukáš Lalinský committed
77 78
    # type: (str, str) -> None
    """Run a built-in script."""
79 80 81 82 83 84 85 86 87
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    mod = importlib.import_module('acoustid.scripts.{}'.format(name))
    func_name = 'run_{}'.format(name)
    func = getattr(mod, func_name)
    func(script, None, None)


Lukáš Lalinský's avatar
Lukáš Lalinský committed
88 89 90
@cli.command('shell')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def shell_cmd(config):
91
    # type: (str) -> None
Lukáš Lalinský's avatar
Lukáš Lalinský committed
92 93 94 95 96 97
    """Run shell."""
    import IPython
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    with script.context() as ctx:
Lukáš Lalinský's avatar
Lukáš Lalinský committed
98
        _ = ctx
Lukáš Lalinský's avatar
Lukáš Lalinský committed
99 100 101
        IPython.embed()


102
def main():
103
    # type: () -> None
104
    cli()