archivebox.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. # archivebox [command]
  3. __package__ = 'archivebox.cli'
  4. __command__ = 'archivebox'
  5. __description__ = 'ArchiveBox: The self-hosted internet archive.'
  6. import os
  7. import sys
  8. import argparse
  9. from . import list_subcommands, run_subcommand
  10. from ..legacy.config import OUTPUT_DIR
  11. def parse_args(args=None):
  12. args = sys.argv[1:] if args is None else args
  13. subcommands = list_subcommands()
  14. parser = argparse.ArgumentParser(
  15. prog=__command__,
  16. description=__description__,
  17. add_help=False,
  18. )
  19. group = parser.add_mutually_exclusive_group()
  20. group.add_argument(
  21. '--help', '-h',
  22. action='store_true',
  23. help=subcommands['help'],
  24. )
  25. group.add_argument(
  26. '--version',
  27. action='store_true',
  28. help=subcommands['version'],
  29. )
  30. group.add_argument(
  31. "subcommand",
  32. type=str,
  33. help= "The name of the subcommand to run",
  34. nargs='?',
  35. choices=subcommands.keys(),
  36. default=None,
  37. )
  38. parser.add_argument(
  39. "args",
  40. help="Arguments for the subcommand",
  41. nargs=argparse.REMAINDER,
  42. )
  43. command = parser.parse_args(args)
  44. if command.help:
  45. command.subcommand = 'help'
  46. if command.version:
  47. command.subcommand = 'version'
  48. # print('--------------------------------------------')
  49. # print('Command: ', sys.argv[0])
  50. # print('Subcommand: ', command.subcommand)
  51. # print('Args to pass:', args[1:])
  52. # print('--------------------------------------------')
  53. return command.subcommand, command.args
  54. def print_import_tutorial():
  55. print('Welcome to ArchiveBox!')
  56. print()
  57. print('To import an existing archive (from a previous version of ArchiveBox):')
  58. print(' 1. cd into your data dir OUTPUT_DIR (usually ArchiveBox/output) and run:')
  59. print(' 2. archivebox init')
  60. print()
  61. print('To start a new archive:')
  62. print(' 1. Create an emptry directory, then cd into it and run:')
  63. print(' 2. archivebox init')
  64. print()
  65. print('For more information, see the migration docs here:')
  66. print(' https://github.com/pirate/ArchiveBox/wiki/Migration')
  67. def main(args=None):
  68. subcommand, subcommand_args = parse_args(args)
  69. existing_index = os.path.exists(os.path.join(OUTPUT_DIR, 'index.json'))
  70. if subcommand is None:
  71. if existing_index:
  72. run_subcommand('help', subcommand_args)
  73. else:
  74. print_import_tutorial()
  75. raise SystemExit(0)
  76. run_subcommand(subcommand, subcommand_args)
  77. if __name__ == '__main__':
  78. main()