Initialize a new ArchiveBox collection in the current directory
(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path=OUTPUT_DIR)
| 306 | |
| 307 | @enforce_types |
| 308 | def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path=OUTPUT_DIR) -> None: |
| 309 | """Initialize a new ArchiveBox collection in the current directory""" |
| 310 | |
| 311 | from core.models import Snapshot |
| 312 | |
| 313 | out_dir.mkdir(exist_ok=True) |
| 314 | is_empty = not len(set(os.listdir(out_dir)) - ALLOWED_IN_OUTPUT_DIR) |
| 315 | |
| 316 | if (out_dir / JSON_INDEX_FILENAME).exists(): |
| 317 | stderr("[!] This folder contains a JSON index. It is deprecated, and will no longer be kept up to date automatically.", color="lightyellow") |
| 318 | stderr(" You can run `archivebox list --json --with-headers > static_index.json` to manually generate it.", color="lightyellow") |
| 319 | |
| 320 | existing_index = (out_dir / SQL_INDEX_FILENAME).exists() |
| 321 | |
| 322 | if is_empty and not existing_index: |
| 323 | print('{green}[+] Initializing a new ArchiveBox v{} collection...{reset}'.format(VERSION, **ANSI)) |
| 324 | print('{green}----------------------------------------------------------------------{reset}'.format(**ANSI)) |
| 325 | elif existing_index: |
| 326 | # TODO: properly detect and print the existing version in current index as well |
| 327 | print('{green}[^] Verifying and updating existing ArchiveBox collection to v{}...{reset}'.format(VERSION, **ANSI)) |
| 328 | print('{green}----------------------------------------------------------------------{reset}'.format(**ANSI)) |
| 329 | else: |
| 330 | if force: |
| 331 | stderr('[!] This folder appears to already have files in it, but no index.sqlite3 is present.', color='lightyellow') |
| 332 | stderr(' Because --force was passed, ArchiveBox will initialize anyway (which may overwrite existing files).') |
| 333 | else: |
| 334 | stderr( |
| 335 | ("{red}[X] This folder appears to already have files in it, but no index.sqlite3 present.{reset}\n\n" |
| 336 | " You must run init in a completely empty directory, or an existing data folder.\n\n" |
| 337 | " {lightred}Hint:{reset} To import an existing data folder make sure to cd into the folder first, \n" |
| 338 | " then run and run 'archivebox init' to pick up where you left off.\n\n" |
| 339 | " (Always make sure your data folder is backed up first before updating ArchiveBox)" |
| 340 | ).format(out_dir, **ANSI) |
| 341 | ) |
| 342 | raise SystemExit(2) |
| 343 | |
| 344 | if existing_index: |
| 345 | print('\n{green}[*] Verifying archive folder structure...{reset}'.format(**ANSI)) |
| 346 | else: |
| 347 | print('\n{green}[+] Building archive folder structure...{reset}'.format(**ANSI)) |
| 348 | |
| 349 | print(f' + ./{ARCHIVE_DIR.relative_to(OUTPUT_DIR)}, ./{SOURCES_DIR.relative_to(OUTPUT_DIR)}, ./{LOGS_DIR.relative_to(OUTPUT_DIR)}...') |
| 350 | Path(SOURCES_DIR).mkdir(exist_ok=True) |
| 351 | Path(ARCHIVE_DIR).mkdir(exist_ok=True) |
| 352 | Path(LOGS_DIR).mkdir(exist_ok=True) |
| 353 | print(f' + ./{CONFIG_FILE.relative_to(OUTPUT_DIR)}...') |
| 354 | write_config_file({}, out_dir=out_dir) |
| 355 | |
| 356 | if (out_dir / SQL_INDEX_FILENAME).exists(): |
| 357 | print('\n{green}[*] Verifying main SQL index and running any migrations needed...{reset}'.format(**ANSI)) |
| 358 | else: |
| 359 | print('\n{green}[+] Building main SQL index and running initial migrations...{reset}'.format(**ANSI)) |
| 360 | |
| 361 | DATABASE_FILE = out_dir / SQL_INDEX_FILENAME |
| 362 | for migration_line in apply_migrations(out_dir): |
| 363 | print(f' {migration_line}') |
| 364 | |
| 365 | assert DATABASE_FILE.exists() |