__init__.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. __package__ = 'archivebox.cli'
  2. __command__ = 'archivebox'
  3. import os
  4. import sys
  5. from importlib import import_module
  6. import rich_click as click
  7. from rich import print
  8. from archivebox.config.version import VERSION
  9. if '--debug' in sys.argv:
  10. os.environ['DEBUG'] = 'True'
  11. sys.argv.remove('--debug')
  12. class ArchiveBoxGroup(click.Group):
  13. """lazy loading click group for archivebox commands"""
  14. meta_commands = {
  15. 'help': 'archivebox.cli.archivebox_help.main',
  16. 'version': 'archivebox.cli.archivebox_version.main',
  17. 'mcp': 'archivebox.cli.archivebox_mcp.main',
  18. }
  19. setup_commands = {
  20. 'init': 'archivebox.cli.archivebox_init.main',
  21. 'install': 'archivebox.cli.archivebox_install.main',
  22. }
  23. archive_commands = {
  24. 'add': 'archivebox.cli.archivebox_add.main',
  25. 'remove': 'archivebox.cli.archivebox_remove.main',
  26. 'update': 'archivebox.cli.archivebox_update.main',
  27. 'search': 'archivebox.cli.archivebox_search.main',
  28. 'status': 'archivebox.cli.archivebox_status.main',
  29. 'config': 'archivebox.cli.archivebox_config.main',
  30. 'schedule': 'archivebox.cli.archivebox_schedule.main',
  31. 'server': 'archivebox.cli.archivebox_server.main',
  32. 'shell': 'archivebox.cli.archivebox_shell.main',
  33. 'manage': 'archivebox.cli.archivebox_manage.main',
  34. # Worker/orchestrator commands
  35. 'orchestrator': 'archivebox.cli.archivebox_orchestrator.main',
  36. 'worker': 'archivebox.cli.archivebox_worker.main',
  37. # Task commands (called by workers as subprocesses)
  38. 'crawl': 'archivebox.cli.archivebox_crawl.main',
  39. 'snapshot': 'archivebox.cli.archivebox_snapshot.main',
  40. 'extract': 'archivebox.cli.archivebox_extract.main',
  41. }
  42. all_subcommands = {
  43. **meta_commands,
  44. **setup_commands,
  45. **archive_commands,
  46. }
  47. renamed_commands = {
  48. 'setup': 'install',
  49. 'list': 'search',
  50. 'import': 'add',
  51. 'archive': 'add',
  52. 'export': 'search',
  53. }
  54. @classmethod
  55. def get_canonical_name(cls, cmd_name):
  56. return cls.renamed_commands.get(cmd_name, cmd_name)
  57. def get_command(self, ctx, cmd_name):
  58. # handle renamed commands
  59. if cmd_name in self.renamed_commands:
  60. new_name = self.renamed_commands[cmd_name]
  61. print(f' [violet]Hint:[/violet] `archivebox {cmd_name}` has been renamed to `archivebox {new_name}`')
  62. cmd_name = new_name
  63. ctx.invoked_subcommand = cmd_name
  64. # handle lazy loading of commands
  65. if cmd_name in self.all_subcommands:
  66. return self._lazy_load(cmd_name)
  67. # fall-back to using click's default command lookup
  68. return super().get_command(ctx, cmd_name)
  69. @classmethod
  70. def _lazy_load(cls, cmd_name):
  71. import_path = cls.all_subcommands[cmd_name]
  72. modname, funcname = import_path.rsplit('.', 1)
  73. # print(f'LAZY LOADING {import_path}')
  74. mod = import_module(modname)
  75. func = getattr(mod, funcname)
  76. if not hasattr(func, '__doc__'):
  77. raise ValueError(f'lazy loading of {import_path} failed - no docstring found on method')
  78. # if not isinstance(cmd, click.BaseCommand):
  79. # raise ValueError(f'lazy loading of {import_path} failed - not a click command')
  80. return func
  81. @click.group(cls=ArchiveBoxGroup, invoke_without_command=True)
  82. @click.option('--help', '-h', is_flag=True, help='Show help')
  83. @click.version_option(VERSION, '-v', '--version', package_name='archivebox', message='%(version)s')
  84. @click.pass_context
  85. def cli(ctx, help=False):
  86. """ArchiveBox: The self-hosted internet archive"""
  87. subcommand = ArchiveBoxGroup.get_canonical_name(ctx.invoked_subcommand)
  88. # if --help is passed or no subcommand is given, show custom help message
  89. if help or ctx.invoked_subcommand is None:
  90. ctx.invoke(ctx.command.get_command(ctx, 'help'))
  91. # if the subcommand is in the archive_commands dict and is not 'manage',
  92. # then we need to set up the django environment and check that we're in a valid data folder
  93. if subcommand in ArchiveBoxGroup.archive_commands:
  94. # print('SETUP DJANGO AND CHECK DATA FOLDER')
  95. try:
  96. from archivebox.config.django import setup_django
  97. from archivebox.misc.checks import check_data_folder
  98. setup_django()
  99. check_data_folder()
  100. except Exception as e:
  101. print(f'[red][X] Error setting up Django or checking data folder: {e}[/red]', file=sys.stderr)
  102. if subcommand not in ('manage', 'shell'): # not all management commands need django to be setup beforehand
  103. raise
  104. def main(args=None, prog_name=None, stdin=None):
  105. # show `docker run archivebox xyz` in help messages if running in docker
  106. IN_DOCKER = os.environ.get('IN_DOCKER', False) in ('1', 'true', 'True', 'TRUE', 'yes')
  107. IS_TTY = sys.stdin.isatty()
  108. prog_name = prog_name or (f'docker compose run{"" if IS_TTY else " -T"} archivebox' if IN_DOCKER else 'archivebox')
  109. # stdin param allows passing input data from caller (used by __main__.py)
  110. # currently not used by click-based CLI, but kept for backwards compatibility
  111. try:
  112. cli(args=args, prog_name=prog_name)
  113. except KeyboardInterrupt:
  114. print('\n\n[red][X] Got CTRL+C. Exiting...[/red]')
  115. if __name__ == '__main__':
  116. main()