From 5961a1f5c8663c44d0e398e93eccf447d1693de5 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 20 May 2013 22:56:09 +0200 Subject: [PATCH] scanner: Write tag cache to tmp file. Tag cache is now output to a tmp file residing in the same folder as the real one. Once generated the tmpfile is moved over the original file for an atomic updated. --- mopidy/scanner.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mopidy/scanner.py b/mopidy/scanner.py index 7fd7b541..0cc51d2a 100644 --- a/mopidy/scanner.py +++ b/mopidy/scanner.py @@ -5,6 +5,7 @@ import datetime import logging import os import sys +import tempfile import gobject gobject.threads_init() @@ -104,16 +105,21 @@ def main(): except KeyboardInterrupt: scanner.stop() - logging.info('Done scanning; writing tag cache...') + logging.info('Done scanning; writing tag cache to temporary file.') + + directory, basename = os.path.split(config['local']['tag_cache_file']) + tmp = tempfile.NamedTemporaryFile( + prefix=basename + '.', dir=directory, delete=False) for row in mpd_translator.tracks_to_tag_cache_format( tracks.values(), config['local']['media_dir']): if len(row) == 1: - print ('%s' % row).encode('utf-8') + tmp.write(('%s\n' % row).encode('utf-8')) else: - print ('%s: %s' % row).encode('utf-8') + tmp.write(('%s: %s\n' % row).encode('utf-8')) - logging.info('Done writing tag cache') + os.rename(tmp.name, config['local']['tag_cache_file']) + logging.info('Done writing; overwriting active tag cache.') def parse_args(): @@ -132,6 +138,7 @@ def parse_args(): return parser.parse_args(args=mopidy_args) +# TODO: move into scanner. def translator(data): albumartist_kwargs = {} album_kwargs = {}