mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 12:35:13 +00:00
refactor(tracks): fold segment resume into one segment store module
This commit is contained in:
@@ -13,20 +13,13 @@ import pytest
|
||||
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY, DownloadCancelled
|
||||
from unshackle.core.temp import (
|
||||
RESUME_STASH,
|
||||
STASH_MAX_AGE,
|
||||
restore_segments,
|
||||
resume_stash,
|
||||
stash_segments,
|
||||
sweep_task_dirs,
|
||||
task_temp_dir,
|
||||
)
|
||||
from unshackle.core.tracks import Video, resume
|
||||
from unshackle.core.temp import RESUME_STASH, STASH_MAX_AGE, resume_stash, sweep_task_dirs, task_temp_dir
|
||||
from unshackle.core.tracks import Video
|
||||
from unshackle.core.tracks.segment_store import DownloadMode, SegmentStore
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
DIGEST = "f" * 64
|
||||
SEGMENTS = [("https://example.test/0.mp4", None)]
|
||||
SEG_NAME = "Video_t1.mp4_segments"
|
||||
|
||||
|
||||
@@ -68,10 +61,17 @@ def run_download(monkeypatch: pytest.MonkeyPatch, parser) -> None:
|
||||
)
|
||||
|
||||
|
||||
def store(save_dir: Path) -> SegmentStore:
|
||||
return SegmentStore(make_track(), save_dir)
|
||||
|
||||
|
||||
def open_store(save_dir: Path) -> bool:
|
||||
return store(save_dir).open(make_track().url, SEGMENTS, mode=DownloadMode())
|
||||
|
||||
|
||||
def seed(ctx):
|
||||
ctx.save_dir.mkdir(parents=True, exist_ok=True)
|
||||
open_store(ctx.save_dir)
|
||||
(ctx.save_dir / "0.mp4").write_bytes(b"complete segment")
|
||||
resume.write_sidecar(ctx.save_dir, DIGEST)
|
||||
|
||||
|
||||
def failing_parser(track, ctx):
|
||||
@@ -95,13 +95,13 @@ def test_failed_track_resumes_in_next_run(temp_root, monkeypatch):
|
||||
|
||||
stashed = temp_root / RESUME_STASH / SEG_NAME
|
||||
assert (stashed / "0.mp4").read_bytes() == b"complete segment"
|
||||
assert resume.sidecar_path(stashed).exists()
|
||||
assert store(stashed).sidecar.exists()
|
||||
|
||||
seen = {}
|
||||
|
||||
def second_parser(track, ctx):
|
||||
seen["bytes"] = (ctx.save_dir / "0.mp4").read_bytes()
|
||||
seen["reusable"] = resume.reusable(ctx.save_dir, DIGEST)
|
||||
seen["reusable"] = open_store(ctx.save_dir)
|
||||
ctx.save_path.write_bytes(b"output")
|
||||
track.path = ctx.save_path
|
||||
|
||||
@@ -133,7 +133,8 @@ def test_partials_are_not_stashed(temp_root, monkeypatch):
|
||||
assert not list((temp_root / RESUME_STASH).rglob("*.!dev"))
|
||||
|
||||
|
||||
def test_existing_stash_target_is_left_alone(temp_root):
|
||||
def test_existing_stash_target_is_left_alone(temp_root, monkeypatch):
|
||||
monkeypatch.setattr(config, "continue_downloads", True)
|
||||
with task_temp_dir() as task_dir:
|
||||
target = temp_root / RESUME_STASH / SEG_NAME
|
||||
target.mkdir(parents=True)
|
||||
@@ -142,17 +143,18 @@ def test_existing_stash_target_is_left_alone(temp_root):
|
||||
save_dir.mkdir()
|
||||
(save_dir / "new.mp4").write_bytes(b"new")
|
||||
|
||||
stash_segments(save_dir)
|
||||
store(save_dir).abandon()
|
||||
|
||||
assert (target / "old.mp4").read_bytes() == b"old"
|
||||
assert not (target / "new.mp4").exists()
|
||||
assert save_dir.exists()
|
||||
|
||||
|
||||
def test_restore_is_a_noop_without_stash_or_with_present_dir(temp_root):
|
||||
def test_restore_is_a_noop_without_stash_or_with_present_dir(temp_root, monkeypatch):
|
||||
monkeypatch.setattr(config, "continue_downloads", True)
|
||||
with task_temp_dir() as task_dir:
|
||||
save_dir = task_dir / SEG_NAME
|
||||
restore_segments(save_dir)
|
||||
store(save_dir).restore()
|
||||
assert not save_dir.exists()
|
||||
|
||||
target = temp_root / RESUME_STASH / SEG_NAME
|
||||
@@ -160,7 +162,7 @@ def test_restore_is_a_noop_without_stash_or_with_present_dir(temp_root):
|
||||
(target / "a.mp4").write_bytes(b"a")
|
||||
save_dir.mkdir()
|
||||
|
||||
restore_segments(save_dir)
|
||||
store(save_dir).restore()
|
||||
|
||||
assert (target / "a.mp4").exists()
|
||||
assert not (save_dir / "a.mp4").exists()
|
||||
@@ -184,7 +186,7 @@ def test_locked_segment_never_replaces_download_error(temp_root, monkeypatch):
|
||||
rmtree = shutil.rmtree
|
||||
|
||||
def locked(path, *args, **kwargs):
|
||||
if Path(path).name == SEG_NAME:
|
||||
if Path(path).name == SEG_NAME and not kwargs.get("ignore_errors"):
|
||||
raise PermissionError(32, "being used by another process", str(path))
|
||||
rmtree(path, *args, **kwargs)
|
||||
|
||||
@@ -242,7 +244,7 @@ def test_cancelled_track_is_stashed(temp_root, monkeypatch):
|
||||
|
||||
stashed = temp_root / RESUME_STASH / SEG_NAME
|
||||
assert (stashed / "0.mp4").read_bytes() == b"complete segment"
|
||||
assert resume.sidecar_path(stashed).exists()
|
||||
assert store(stashed).sidecar.exists()
|
||||
|
||||
|
||||
def test_presized_data_file_is_dropped_with_its_control_file(temp_root, monkeypatch):
|
||||
@@ -264,15 +266,15 @@ def test_presized_data_file_is_dropped_with_its_control_file(temp_root, monkeypa
|
||||
assert not (stashed / "1.mp4.!dev").exists()
|
||||
|
||||
|
||||
def test_stash_refreshes_mtime(temp_root):
|
||||
def test_stash_refreshes_mtime(temp_root, monkeypatch):
|
||||
monkeypatch.setattr(config, "continue_downloads", True)
|
||||
with task_temp_dir() as task_dir:
|
||||
save_dir = task_dir / SEG_NAME
|
||||
save_dir.mkdir()
|
||||
resume.write_sidecar(save_dir, DIGEST)
|
||||
open_store(save_dir)
|
||||
stamp = time.time() - STASH_MAX_AGE - 60
|
||||
os.utime(save_dir, (stamp, stamp))
|
||||
|
||||
stash_segments(save_dir)
|
||||
store(save_dir).abandon()
|
||||
|
||||
target = temp_root / RESUME_STASH / SEG_NAME
|
||||
assert time.time() - target.stat().st_mtime < 60
|
||||
|
||||
Reference in New Issue
Block a user