diff --git a/archivebox/cli/archivebox_add.py b/archivebox/cli/archivebox_add.py index 04c3fecb..26ea1e2d 100644 --- a/archivebox/cli/archivebox_add.py +++ b/archivebox/cli/archivebox_add.py @@ -7,6 +7,7 @@ __description__ = 'Add a new URL or list of URLs to your archive' import sys import argparse +from ..legacy.config import stderr, check_dependencies, check_data_folder from ..legacy.util import ( handle_stdin_import, handle_file_import, @@ -14,7 +15,7 @@ from ..legacy.util import ( from ..legacy.main import update_archive_data -def main(args=None): +def main(args=None, stdin=None): args = sys.argv[1:] if args is None else args parser = argparse.ArgumentParser( @@ -53,13 +54,16 @@ def main(args=None): ) command = parser.parse_args(args) + check_dependencies() + check_data_folder() + ### Handle ingesting urls piped in through stdin # (.e.g if user does cat example_urls.txt | archivebox add) import_path = None - if not sys.stdin.isatty(): - stdin_raw_text = sys.stdin.read() + if stdin or not sys.stdin.isatty(): + stdin_raw_text = stdin or sys.stdin.read() if stdin_raw_text and command.url: - print( + stderr( '[X] You should pass either a path as an argument, ' 'or pass a list of links via stdin, but not both.\n' ) diff --git a/archivebox/cli/archivebox_init.py b/archivebox/cli/archivebox_init.py index 942387ad..f5757f8c 100755 --- a/archivebox/cli/archivebox_init.py +++ b/archivebox/cli/archivebox_init.py @@ -9,59 +9,7 @@ import sys import argparse from ..legacy.util import reject_stdin -from ..legacy.index import write_links_index -from ..legacy.config import ( - OUTPUT_DIR, - SOURCES_DIR, - ARCHIVE_DIR, - DATABASE_DIR, - ANSI, - stderr, -) - - -def init(output_dir: str=OUTPUT_DIR): - if not os.path.exists(output_dir): - os.makedirs(output_dir) - - harmless_files = {'.DS_Store', '.venv', 'venv', 'virtualenv', '.virtualenv'} - is_empty = not len(set(os.listdir(output_dir)) - harmless_files) - existing_index = os.path.exists(os.path.join(output_dir, 'index.json')) - - if not is_empty: - if existing_index: - stderr('{green}[√] You already have an archive index in this folder.{reset}'.format(**ANSI)) - stderr(' To add new links, you can run:') - stderr(" archivebox add 'https://example.com'") - stderr() - stderr(' For more usage and examples, run:') - stderr(' archivebox help') - # TODO: import old archivebox version's archive data folder - - raise SystemExit(1) - else: - stderr( - ("{red}[X] This folder already has files in it. You must run init inside a completely empty directory.{reset}" - "\n\n" - " {lightred}Hint:{reset} To import a data folder created by an older version of ArchiveBox, \n" - " just cd into the folder and run the archivebox command to pick up where you left off.\n\n" - " (Always make sure your data folder is backed up first before updating ArchiveBox)" - ).format(output_dir, **ANSI) - ) - raise SystemExit(1) - - - stderr('{green}[+] Initializing new archive directory: {}{reset}'.format(output_dir, **ANSI)) - os.makedirs(SOURCES_DIR) - stderr(f' > {SOURCES_DIR}') - os.makedirs(ARCHIVE_DIR) - stderr(f' > {ARCHIVE_DIR}') - os.makedirs(DATABASE_DIR) - stderr(f' > {DATABASE_DIR}') - - write_links_index([], out_dir=OUTPUT_DIR, finished=True) - - stderr('{green}[√] Done.{reset}'.format(**ANSI)) +from ..legacy.main import init def main(args=None): diff --git a/archivebox/legacy/__init__.py b/archivebox/legacy/__init__.py index e69de29b..2bbcd2fc 100644 --- a/archivebox/legacy/__init__.py +++ b/archivebox/legacy/__init__.py @@ -0,0 +1 @@ +__package__ = 'archivebox.legacy'