Skip to content

Commit

Permalink
remove mtime tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 10, 2023
1 parent 6bfc48d commit bc7ef69
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
7 changes: 2 additions & 5 deletions migrations/20231009_01_qlEHa-bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 12 additions & 10 deletions rose/cache/update.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import re
from dataclasses import asdict, dataclass
Expand All @@ -9,6 +10,8 @@
from rose.foundation.conf import Config
from rose.tagger import AudioFile

logger = logging.getLogger(__name__)

SUPPORTED_EXTENSIONS = [
".mp3",
".m4a",
Expand All @@ -31,7 +34,7 @@
"unknown",
]

ID_REGEX = re.compile(r"\{id=([^}]+)\}$")
ID_REGEX = re.compile(r"\{id=([^\}]+)\}$")


@dataclass
Expand All @@ -48,7 +51,6 @@ class CachedRelease:
class CachedTrack:
id: str
source_path: Path
source_mtime: int
title: str
release_id: str
trackno: str
Expand All @@ -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"""
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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(),
Expand Down Expand Up @@ -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",
Expand All @@ -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 = ?,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions rose/cache/update_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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,))
Expand Down
2 changes: 1 addition & 1 deletion rose/virtualfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
fuse.fuse_python_api = (0, 2)


class VirtualFS(fuse.Fuse):
class VirtualFS(fuse.Fuse): # type: ignore
pass


Expand Down

0 comments on commit bc7ef69

Please sign in to comment.