From 742deab392f972b5c7d1a7cd2b6e0e7528ad631e Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Sat, 16 Aug 2025 01:23:58 +0200 Subject: [PATCH] Fix for default cache directory --- Dockerfile | 4 ++-- music_assistant/__main__.py | 28 +++++++++++++++++++--------- music_assistant/mass.py | 13 ------------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index 507127b4..f2024328 100644 --- a/Dockerfile +++ b/Dockerfile @@ -68,9 +68,9 @@ LABEL \ io.hass.platform="${TARGETPLATFORM}" \ io.hass.type="addon" -VOLUME [ "/data" ] +VOLUME [ "/data", "/cache" ] EXPOSE 8095 WORKDIR $VIRTUAL_ENV -ENTRYPOINT ["mass", "--config", "/data"] +ENTRYPOINT ["mass", "--data-dir", "/data", "--cache-dir", "/cache"] diff --git a/music_assistant/__main__.py b/music_assistant/__main__.py index 50feb951..f48b5a2a 100644 --- a/music_assistant/__main__.py +++ b/music_assistant/__main__.py @@ -6,6 +6,7 @@ import argparse import asyncio import logging import os +import shutil import subprocess import sys import threading @@ -35,31 +36,33 @@ def get_arguments() -> argparse.Namespace: """Arguments handling.""" parser = argparse.ArgumentParser(description="MusicAssistant") + # determine default data directory if os.path.isdir(old_data_dir := os.path.join(os.path.expanduser("~"), ".musicassistant")): - data_dir = old_data_dir - cache_dir = os.path.join(data_dir, ".cache") + # prefer (existing) legacy directory + default_data_dir = old_data_dir else: - data_dir = os.path.join( + default_data_dir = os.path.join( os.getenv("XDG_DATA_HOME", os.path.join(os.path.expanduser("~"), ".local", "share")), "music-assistant", ) - cache_dir = os.path.join( - os.getenv("XDG_CACHE_HOME", os.path.join(os.path.expanduser("~"), ".cache")), - "music-assistant", - ) + + default_cache_dir = os.path.join( + os.getenv("XDG_CACHE_HOME", os.path.join(os.path.expanduser("~"), ".cache")), + "music-assistant", + ) parser.add_argument( "--data-dir", "-c", "--config", metavar="path_to_data_dir", - default=data_dir, + default=default_data_dir, help="Directory that contains MusicAssistant persistent data", ) parser.add_argument( "--cache-dir", metavar="path_to_cache_dir", - default=cache_dir, + default=default_cache_dir, help="Directory that contains MusicAssistant cache data", ) parser.add_argument( @@ -194,7 +197,14 @@ def main() -> None: data_dir = args.data_dir cache_dir = args.cache_dir + # move legacy cache directory + old_cache_dir = os.path.join(data_dir, ".cache") + if os.path.isdir(old_cache_dir) and old_cache_dir != cache_dir: + with suppress(OSError): + shutil.move(old_cache_dir, cache_dir) + os.makedirs(data_dir, exist_ok=True) + os.makedirs(cache_dir, exist_ok=True) # TEMP: override options though hass config file hass_options_file = os.path.join(data_dir, "options.json") diff --git a/music_assistant/mass.py b/music_assistant/mass.py index 049d222a..c93caa40 100644 --- a/music_assistant/mass.py +++ b/music_assistant/mass.py @@ -880,16 +880,3 @@ class MusicAssistant: await mkdirs(self.storage_path) if not await isdir(self.cache_path): await mkdirs(self.cache_path) - # cleanup old cache files from their old locations - # TODO: Remove this code after MA version 2.5+ - old_cache_db = os.path.join(self.storage_path, "cache.db") - if await isfile(old_cache_db): - await rmfile(old_cache_db) - for filename in await listdir(self.storage_path): - if filename.startswith(("spotify", "collage")): - old_loc = os.path.join(self.storage_path, filename) - new_loc = os.path.join(self.cache_path, filename) - if await isfile(new_loc): - await rmfile(old_loc) - else: - await rename(old_loc, new_loc) -- 2.34.1