From bc7ef699b0fc4fa4841b189ba10404fdb3dddd2b Mon Sep 17 00:00:00 2001 From: blissful Date: Tue, 10 Oct 2023 13:24:50 -0400 Subject: [PATCH] remove mtime tracking --- migrations/20231009_01_qlEHa-bootstrap.sql | 7 ++----- pyproject.toml | 3 +++ rose/cache/update.py | 22 ++++++++++++---------- rose/cache/update_test.py | 4 ++-- rose/virtualfs/__init__.py | 2 +- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/migrations/20231009_01_qlEHa-bootstrap.sql b/migrations/20231009_01_qlEHa-bootstrap.sql index a7d58ac..605b294 100644 --- a/migrations/20231009_01_qlEHa-bootstrap.sql +++ b/migrations/20231009_01_qlEHa-bootstrap.sql @@ -43,7 +43,6 @@ CREATE INDEX releases_labels_label ON releases_labels(label); CREATE TABLE tracks ( id TEXT PRIMARY KEY, source_path TEXT NOT NULL UNIQUE, - source_mtime TIMESTAMP NOT NULL, title TEXT NOT NULL, release_id TEXT NOT NULL REFERENCES releases(id), track_number TEXT NOT NULL, @@ -84,8 +83,7 @@ CREATE INDEX tracks_artists_artist ON tracks_artists(artist); CREATE TABLE collections ( id TEXT PRIMARY KEY, name TEXT NOT NULL, - source_path TEXT UNIQUE NOT NULL, - source_mtime TIMESTAMP NOT NULL + source_path TEXT UNIQUE NOT NULL ); CREATE INDEX collections_source_path ON collections(source_path); @@ -101,8 +99,7 @@ CREATE UNIQUE INDEX collections_releases_collection_position ON collections_rele CREATE TABLE playlists ( id TEXT PRIMARY KEY, name TEXT NOT NULL, - source_path TEXT UNIQUE NOT NULL, - source_mtime TIMESTAMP NOT NULL + source_path TEXT UNIQUE NOT NULL ); CREATE INDEX playlists_source_path ON playlists(source_path); diff --git a/pyproject.toml b/pyproject.toml index 68d4668..e2b2a8d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,6 +63,9 @@ explicit_package_bases = true module = "yoyo" ignore_missing_imports = true [[tool.mypy.overrides]] +module = "fuse" +ignore_missing_imports = true +[[tool.mypy.overrides]] module = "setuptools" ignore_missing_imports = true diff --git a/rose/cache/update.py b/rose/cache/update.py index f1ee47a..e6aa539 100644 --- a/rose/cache/update.py +++ b/rose/cache/update.py @@ -1,3 +1,4 @@ +import logging import os import re from dataclasses import asdict, dataclass @@ -9,6 +10,8 @@ from rose.foundation.conf import Config from rose.tagger import AudioFile +logger = logging.getLogger(__name__) + SUPPORTED_EXTENSIONS = [ ".mp3", ".m4a", @@ -31,7 +34,7 @@ "unknown", ] -ID_REGEX = re.compile(r"\{id=([^}]+)\}$") +ID_REGEX = re.compile(r"\{id=([^\}]+)\}$") @dataclass @@ -48,7 +51,6 @@ class CachedRelease: class CachedTrack: id: str source_path: Path - source_mtime: int title: str release_id: str trackno: str @@ -67,8 +69,10 @@ def update_cache_for_all_releases(c: Config) -> None: Process and update the cache for all releases. Delete any nonexistent releases. """ dirs = [Path(d.path).resolve() for d in os.scandir(c.music_source_dir) if d.is_dir()] + logger.info(f"Found {len(dirs)} releases to update") for i, d in enumerate(dirs): dirs[i] = update_cache_for_release(c, d) + logger.info("Deleting cached releases that are not on disk") with connect(c) as conn: conn.execute( f""" @@ -86,6 +90,7 @@ def update_cache_for_release(c: Config, release_dir: Path) -> Path: Returns the new release_dir if a rename occurred; otherwise, returns the same release_dir. """ + logger.info(f"Refreshing cached data for {release_dir.name}") with connect(c) as conn, transaction(conn) as conn: # The release will be updated based on the album tags of the first track. release: CachedRelease | None = None @@ -94,6 +99,7 @@ def update_cache_for_release(c: Config, release_dir: Path) -> Path: release_id = _parse_uuid_from_path(release_dir) if not release_id: release_id = str(uuid6.uuid7()) + logger.debug(f"Assigning id={release_id} to release {release_dir.name}") release_dir = _rename_with_uuid(release_dir, release_id) for f in os.scandir(release_dir): @@ -104,6 +110,7 @@ def update_cache_for_release(c: Config, release_dir: Path) -> Path: tags = AudioFile.from_file(Path(f.path)) # If this is the first track, upsert the release. if release is None: + logger.debug("Upserting release from first track's tags") release = CachedRelease( id=release_id, source_path=release_dir.resolve(), @@ -172,18 +179,16 @@ def update_cache_for_release(c: Config, release_dir: Path) -> Path: # Now process the track. Release is guaranteed to exist here. filepath = Path(f.path) - # Get the mtime before we may possibly rename the file. - source_mtime = int(f.stat().st_mtime) track_id = _parse_uuid_from_path(filepath) if not track_id: track_id = str(uuid6.uuid7()) + logger.debug(f"Assigning id={release_id} to track {filepath.name}") filepath = _rename_with_uuid(filepath, track_id) track = CachedTrack( id=track_id, source_path=filepath, - source_mtime=source_mtime, title=tags.title or "Unknown Title", release_id=release.id, trackno=tags.track_number or "1", @@ -193,12 +198,11 @@ def update_cache_for_release(c: Config, release_dir: Path) -> Path: conn.execute( """ INSERT INTO tracks - (id, source_path, source_mtime, title, release_id, track_number, disc_number, + (id, source_path, title, release_id, track_number, disc_number, duration_seconds) - VALUES (?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET source_path = ?, - source_mtime = ?, title = ?, release_id = ?, track_number = ?, @@ -208,14 +212,12 @@ def update_cache_for_release(c: Config, release_dir: Path) -> Path: ( track.id, str(track.source_path), - track.source_mtime, track.title, track.release_id, track.trackno, track.discno, track.duration_sec, str(track.source_path), - track.source_mtime, track.title, track.release_id, track.trackno, diff --git a/rose/cache/update_test.py b/rose/cache/update_test.py index 514af1e..b0edde7 100644 --- a/rose/cache/update_test.py +++ b/rose/cache/update_test.py @@ -65,7 +65,7 @@ def test_update_cache_for_release(config: Config) -> None: continue # Check that the track file was given a UUID. - m = ID_REGEX.search(f.name) + m = ID_REGEX.search(f.stem) assert m is not None track_id = m[1] @@ -126,7 +126,7 @@ def test_update_cache_with_existing_id(config: Config) -> None: continue # Check that the track file was given a UUID. - m = ID_REGEX.search(f.name) + m = ID_REGEX.search(f.stem) assert m is not None track_id = m[1] cursor = conn.execute("SELECT EXISTS(SELECT * FROM tracks WHERE id = ?)", (track_id,)) diff --git a/rose/virtualfs/__init__.py b/rose/virtualfs/__init__.py index e135fc4..4448852 100644 --- a/rose/virtualfs/__init__.py +++ b/rose/virtualfs/__init__.py @@ -7,7 +7,7 @@ fuse.fuse_python_api = (0, 2) -class VirtualFS(fuse.Fuse): +class VirtualFS(fuse.Fuse): # type: ignore pass