2019-04-27 21:26:24 +00:00
|
|
|
__package__ = 'archivebox.index'
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2019-04-24 08:07:46 +00:00
|
|
|
from io import StringIO
|
|
|
|
from typing import List, Tuple, Iterator
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
from .schema import Link
|
2019-04-17 07:49:18 +00:00
|
|
|
from ..util import enforce_types
|
2019-04-19 01:09:54 +00:00
|
|
|
from ..config import setup_django, OUTPUT_DIR
|
2019-04-17 07:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
### Main Links Index
|
|
|
|
|
|
|
|
@enforce_types
|
2019-04-19 01:09:54 +00:00
|
|
|
def parse_sql_main_index(out_dir: str=OUTPUT_DIR) -> Iterator[Link]:
|
|
|
|
setup_django(out_dir, check_db=True)
|
2019-05-01 03:44:51 +00:00
|
|
|
from core.models import Snapshot
|
2019-04-17 07:49:18 +00:00
|
|
|
|
|
|
|
return (
|
2019-05-01 03:44:51 +00:00
|
|
|
Link.from_json(page.as_json(*Snapshot.keys))
|
|
|
|
for page in Snapshot.objects.all()
|
2019-04-17 07:49:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@enforce_types
|
2019-04-19 01:09:54 +00:00
|
|
|
def write_sql_main_index(links: List[Link], out_dir: str=OUTPUT_DIR) -> None:
|
|
|
|
setup_django(out_dir, check_db=True)
|
2019-05-01 03:44:51 +00:00
|
|
|
from core.models import Snapshot
|
|
|
|
from django.db import transaction
|
2019-04-17 07:49:18 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
all_urls = {link.url: link for link in links}
|
2019-05-01 03:44:51 +00:00
|
|
|
all_ts = {link.timestamp: link for link in links}
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
for snapshot in Snapshot.objects.all():
|
|
|
|
if snapshot.timestamp in all_ts:
|
|
|
|
info = {k: v for k, v in all_urls.pop(snapshot.url)._asdict().items() if k in Snapshot.keys}
|
|
|
|
snapshot.delete()
|
|
|
|
Snapshot.objects.create(**info)
|
2020-06-25 21:48:27 +00:00
|
|
|
elif snapshot.url in all_urls:
|
2019-05-01 03:44:51 +00:00
|
|
|
info = {k: v for k, v in all_urls.pop(snapshot.url)._asdict().items() if k in Snapshot.keys}
|
|
|
|
snapshot.delete()
|
|
|
|
Snapshot.objects.create(**info)
|
|
|
|
else:
|
|
|
|
snapshot.delete()
|
|
|
|
|
|
|
|
for url, link in all_urls.items():
|
|
|
|
info = {k: v for k, v in link._asdict().items() if k in Snapshot.keys}
|
|
|
|
Snapshot.objects.update_or_create(url=url, defaults=info)
|
2019-04-27 21:26:24 +00:00
|
|
|
|
2019-04-24 08:07:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def list_migrations(out_dir: str=OUTPUT_DIR) -> List[Tuple[bool, str]]:
|
|
|
|
setup_django(out_dir, check_db=False)
|
|
|
|
from django.core.management import call_command
|
|
|
|
out = StringIO()
|
|
|
|
call_command("showmigrations", list=True, stdout=out)
|
|
|
|
out.seek(0)
|
|
|
|
migrations = []
|
|
|
|
for line in out.readlines():
|
|
|
|
if line.strip() and ']' in line:
|
|
|
|
status_str, name_str = line.strip().split(']', 1)
|
|
|
|
is_applied = 'X' in status_str
|
|
|
|
migration_name = name_str.strip()
|
|
|
|
migrations.append((is_applied, migration_name))
|
|
|
|
|
|
|
|
return migrations
|
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def apply_migrations(out_dir: str=OUTPUT_DIR) -> List[str]:
|
|
|
|
setup_django(out_dir, check_db=False)
|
|
|
|
from django.core.management import call_command
|
|
|
|
null, out = StringIO(), StringIO()
|
|
|
|
call_command("makemigrations", interactive=False, stdout=null)
|
|
|
|
call_command("migrate", interactive=False, stdout=out)
|
|
|
|
out.seek(0)
|
|
|
|
|
|
|
|
return [line.strip() for line in out.readlines() if line.strip()]
|
2019-04-24 15:37:30 +00:00
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def get_admins(out_dir: str=OUTPUT_DIR) -> List[str]:
|
|
|
|
setup_django(out_dir, check_db=False)
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
return User.objects.filter(is_superuser=True)
|