refactor(tracks): fold segment resume into one segment store module

This commit is contained in:
imSp4rky
2026-11-28 19:21:05 -07:00
parent 5a2680d63d
commit 37357673fe
15 changed files with 633 additions and 368 deletions
+7 -29
View File
@@ -40,8 +40,7 @@ from unshackle.core.drm.verify import (
)
from unshackle.core.events import events
from unshackle.core.session import MAX_BACKOFF, AnySession, RnetSession, unrecorded, utf8_text
from unshackle.core.temp import restore_segments, stash_segments
from unshackle.core.tracks import resume
from unshackle.core.tracks.segment_store import SegmentStore
from unshackle.core.utilities import language_name, log_event, try_ensure_utf8
from unshackle.core.utils import run_proxy
from unshackle.core.utils.subprocess import run_tool
@@ -774,31 +773,13 @@ class Track:
else:
save_dir = save_path.parent
keep_segments = config.continue_downloads and self.descriptor != self.Descriptor.URL
def cleanup() -> None:
save_path.unlink(missing_ok=True)
if save_dir.name.endswith("_segments"):
if keep_segments:
if save_dir.exists():
for partial in save_dir.rglob("*.!dev"):
partial.with_name(partial.name.removesuffix(".!dev")).unlink(missing_ok=True)
partial.unlink(missing_ok=True)
else:
if save_dir.exists():
shutil.rmtree(save_dir)
resume.clear_sidecar(save_dir)
store = SegmentStore(self, save_dir)
def abandon() -> None:
"""Clean up a failed or cancelled download and stash its segments when resume is on.
The downloader does not wait for its workers on failure, so on Windows one can still hold a
.!dev file open (WinError 32). A cleanup error must not replace the download's own error.
"""
"""Remove the output of a failed or cancelled download; a cleanup error must not replace its error."""
with contextlib.suppress(OSError):
cleanup()
if keep_segments:
stash_segments(save_dir)
save_path.unlink(missing_ok=True)
store.abandon()
if not DOWNLOAD_LICENCE_ONLY.is_set():
if config.directories.temp.is_file():
@@ -806,11 +787,8 @@ class Track:
config.directories.temp.mkdir(parents=True, exist_ok=True)
# completed segments are reusable once the parser proves the segmentation
# unchanged (resume sidecar); partial .!dev files never survive a run boundary
if keep_segments:
restore_segments(save_dir)
cleanup()
save_path.unlink(missing_ok=True)
store.restore()
try:
manifest_parsers: dict[Track.Descriptor, type[DASH] | type[HLS] | type[ISM]] = {