2019-04-03 04:27:37 +00:00
|
|
|
__package__ = 'archivebox.cli'
|
2020-07-02 07:53:39 +00:00
|
|
|
__command__ = 'archivebox'
|
2019-04-03 04:27:37 +00:00
|
|
|
|
|
|
|
import os
|
2020-07-02 08:00:51 +00:00
|
|
|
import sys
|
2020-07-02 07:53:39 +00:00
|
|
|
import argparse
|
|
|
|
|
2020-10-30 08:50:08 +00:00
|
|
|
from typing import Optional, Dict, List, IO, Union
|
2020-09-30 19:43:14 +00:00
|
|
|
from pathlib import Path
|
2020-07-02 07:53:39 +00:00
|
|
|
|
2021-04-01 07:30:53 +00:00
|
|
|
from ..config import OUTPUT_DIR, check_data_folder, check_migrations
|
2019-04-19 01:09:54 +00:00
|
|
|
|
2019-04-03 04:27:37 +00:00
|
|
|
from importlib import import_module
|
|
|
|
|
2020-09-30 19:43:14 +00:00
|
|
|
CLI_DIR = Path(__file__).resolve().parent
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
# these common commands will appear sorted before any others for ease-of-use
|
2021-04-06 01:15:32 +00:00
|
|
|
meta_cmds = ('help', 'version') # dont require valid data folder at all
|
|
|
|
main_cmds = ('init', 'config', 'setup') # dont require existing db present
|
|
|
|
archive_cmds = ('add', 'remove', 'update', 'list', 'status') # require existing db present
|
|
|
|
fake_db = ("oneshot",) # use fake in-memory db
|
2020-12-08 23:05:37 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
display_first = (*meta_cmds, *main_cmds, *archive_cmds)
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
# every imported command module must have these properties in order to be valid
|
|
|
|
required_attrs = ('__package__', '__command__', 'main')
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
# basic checks to make sure imported files are valid subcommands
|
|
|
|
is_cli_module = lambda fname: fname.startswith('archivebox_') and fname.endswith('.py')
|
|
|
|
is_valid_cli_module = lambda module, subcommand: (
|
|
|
|
all(hasattr(module, attr) for attr in required_attrs)
|
|
|
|
and module.__command__.split(' ')[-1] == subcommand
|
|
|
|
)
|
2019-04-11 10:59:14 +00:00
|
|
|
|
2020-07-02 07:53:39 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
def list_subcommands() -> Dict[str, str]:
|
|
|
|
"""find and import all valid archivebox_<subcommand>.py files in CLI_DIR"""
|
2019-04-11 10:59:14 +00:00
|
|
|
|
|
|
|
COMMANDS = []
|
2019-04-03 04:27:37 +00:00
|
|
|
for filename in os.listdir(CLI_DIR):
|
2019-04-19 01:09:54 +00:00
|
|
|
if is_cli_module(filename):
|
2019-04-03 04:27:37 +00:00
|
|
|
subcommand = filename.replace('archivebox_', '').replace('.py', '')
|
|
|
|
module = import_module('.archivebox_{}'.format(subcommand), __package__)
|
2019-04-19 01:09:54 +00:00
|
|
|
assert is_valid_cli_module(module, subcommand)
|
2019-05-01 03:10:48 +00:00
|
|
|
COMMANDS.append((subcommand, module.main.__doc__))
|
2019-04-19 01:09:54 +00:00
|
|
|
globals()[subcommand] = module.main
|
|
|
|
|
|
|
|
display_order = lambda cmd: (
|
|
|
|
display_first.index(cmd[0])
|
|
|
|
if cmd[0] in display_first else
|
|
|
|
100 + len(cmd[0])
|
|
|
|
)
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
return dict(sorted(COMMANDS, key=display_order))
|
2019-04-03 04:27:37 +00:00
|
|
|
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
def run_subcommand(subcommand: str,
|
|
|
|
subcommand_args: List[str]=None,
|
|
|
|
stdin: Optional[IO]=None,
|
2020-10-30 08:50:08 +00:00
|
|
|
pwd: Union[Path, str, None]=None) -> None:
|
2019-05-01 03:10:48 +00:00
|
|
|
"""Run a given ArchiveBox subcommand with the given list of args"""
|
2020-12-08 23:42:01 +00:00
|
|
|
|
2021-04-06 01:13:49 +00:00
|
|
|
subcommand_args = subcommand_args or []
|
|
|
|
|
2020-12-08 23:42:01 +00:00
|
|
|
if subcommand not in meta_cmds:
|
|
|
|
from ..config import setup_django
|
2021-03-01 03:53:23 +00:00
|
|
|
|
|
|
|
cmd_requires_db = subcommand in archive_cmds
|
|
|
|
init_pending = '--init' in subcommand_args or '--quick-init' in subcommand_args
|
|
|
|
|
2021-04-01 07:30:53 +00:00
|
|
|
if cmd_requires_db:
|
|
|
|
check_data_folder(pwd)
|
|
|
|
|
2021-03-01 03:53:23 +00:00
|
|
|
setup_django(in_memory_db=subcommand in fake_db, check_db=cmd_requires_db and not init_pending)
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2021-04-01 07:30:53 +00:00
|
|
|
if cmd_requires_db:
|
|
|
|
check_migrations()
|
|
|
|
|
2019-04-03 04:27:37 +00:00
|
|
|
module = import_module('.archivebox_{}'.format(subcommand), __package__)
|
2019-04-27 21:26:24 +00:00
|
|
|
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
|
2019-04-19 01:09:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
SUBCOMMANDS = list_subcommands()
|
|
|
|
|
2020-07-02 17:31:05 +00:00
|
|
|
class NotProvided:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def main(args: Optional[List[str]]=NotProvided, stdin: Optional[IO]=NotProvided, pwd: Optional[str]=None) -> None:
|
|
|
|
args = sys.argv[1:] if args is NotProvided else args
|
|
|
|
stdin = sys.stdin if stdin is NotProvided else stdin
|
2020-07-02 07:53:39 +00:00
|
|
|
|
|
|
|
subcommands = list_subcommands()
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog=__command__,
|
|
|
|
description='ArchiveBox: The self-hosted internet archive',
|
|
|
|
add_help=False,
|
|
|
|
)
|
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument(
|
|
|
|
'--help', '-h',
|
|
|
|
action='store_true',
|
|
|
|
help=subcommands['help'],
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
'--version',
|
|
|
|
action='store_true',
|
|
|
|
help=subcommands['version'],
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"subcommand",
|
|
|
|
type=str,
|
|
|
|
help= "The name of the subcommand to run",
|
|
|
|
nargs='?',
|
|
|
|
choices=subcommands.keys(),
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"subcommand_args",
|
|
|
|
help="Arguments for the subcommand",
|
|
|
|
nargs=argparse.REMAINDER,
|
|
|
|
)
|
|
|
|
command = parser.parse_args(args or ())
|
|
|
|
|
2020-08-18 19:58:05 +00:00
|
|
|
if command.version:
|
2020-07-02 07:53:39 +00:00
|
|
|
command.subcommand = 'version'
|
2020-08-18 19:58:05 +00:00
|
|
|
elif command.help or command.subcommand is None:
|
|
|
|
command.subcommand = 'help'
|
|
|
|
|
2020-07-13 15:26:30 +00:00
|
|
|
if command.subcommand not in ('help', 'version', 'status'):
|
2020-07-22 16:02:13 +00:00
|
|
|
from ..logging_util import log_cli_command
|
2020-07-13 15:26:30 +00:00
|
|
|
|
|
|
|
log_cli_command(
|
|
|
|
subcommand=command.subcommand,
|
|
|
|
subcommand_args=command.subcommand_args,
|
|
|
|
stdin=stdin,
|
|
|
|
pwd=pwd or OUTPUT_DIR
|
|
|
|
)
|
2020-07-02 07:53:39 +00:00
|
|
|
|
|
|
|
run_subcommand(
|
|
|
|
subcommand=command.subcommand,
|
|
|
|
subcommand_args=command.subcommand_args,
|
|
|
|
stdin=stdin,
|
|
|
|
pwd=pwd or OUTPUT_DIR,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
__all__ = (
|
|
|
|
'SUBCOMMANDS',
|
|
|
|
'list_subcommands',
|
|
|
|
'run_subcommand',
|
|
|
|
*SUBCOMMANDS.keys(),
|
|
|
|
)
|
2020-10-26 13:02:04 +00:00
|
|
|
|
|
|
|
|