local: Fix inconsistent uri handling in local scanner

We now only operate on local track uris, instead of a funny mix of local and
file uris. To achieve this we instead maintain a uri->path mapping to use for
the actual scanning.
This commit is contained in:
Thomas Adamcik 2013-11-27 23:16:42 +01:00
parent 603b57ef3c
commit 4161c2bf27
2 changed files with 36 additions and 21 deletions

View File

@ -44,27 +44,26 @@ class ScanCommand(commands.Command):
local_updater = updaters.values()[0](config)
# TODO: cleanup to consistently use local urls, not a random mix of
# local and file uris depending on how the data was loaded.
uris_library = set()
uris_update = set()
uris_remove = set()
uri_path_mapping = {}
uris_in_library = set()
uris_to_update = set()
uris_to_remove = set()
tracks = local_updater.load()
logger.info('Checking %d tracks from library.', len(tracks))
for track in tracks:
track_path = translator.local_to_path(track.uri, media_dir)
uri_path_mapping[track.uri] = track_path
try:
uri = translator.local_to_file_uri(track.uri, media_dir)
stat = os.stat(path.uri_to_path(uri))
if int(stat.st_mtime) > track.last_modified:
uris_update.add(uri)
uris_library.add(uri)
if int(os.stat(track_path).st_mtime) > track.last_modified:
uris_to_update.add(track.uri)
uris_in_library.add(track.uri)
except OSError:
logger.debug('Missing file %s', track.uri)
uris_remove.add(track.uri)
uris_to_remove.add(track.uri)
logger.info('Removing %d missing tracks.', len(uris_remove))
for uri in uris_remove:
logger.info('Removing %d missing tracks.', len(uris_to_remove))
for uri in uris_to_remove:
local_updater.remove(uri)
logger.info('Checking %s for unknown tracks.', media_dir)
@ -74,20 +73,21 @@ class ScanCommand(commands.Command):
logger.debug('Skipped %s: File extension excluded.', uri)
continue
uri = path.path_to_uri(os.path.join(media_dir, relpath))
if uri not in uris_library:
uris_update.add(uri)
uri = translator.path_to_local(relpath)
if uri not in uris_in_library:
uris_to_update.add(uri)
uri_path_mapping[uri] = os.path.join(media_dir, relpath)
logger.info('Found %d unknown tracks.', len(uris_update))
logger.info('Found %d unknown tracks.', len(uris_to_update))
logger.info('Scanning...')
scanner = scan.Scanner(scan_timeout)
progress = Progress(len(uris_update))
progress = Progress(len(uris_to_update))
for uri in sorted(uris_update):
for uri in sorted(uris_to_update):
try:
data = scanner.scan(uri)
track = scan.audio_data_to_track(data)
data = scanner.scan(path.path_to_uri(uri_path_mapping[uri]))
track = scan.audio_data_to_track(data).copy(uri=uri)
local_updater.add(track)
logger.debug('Added %s', track.uri)
except exceptions.ScannerError as error:

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
import logging
import os
import urlparse
import urllib
from mopidy.utils.encoding import locale_decode
from mopidy.utils.path import path_to_uri, uri_to_path
@ -10,6 +11,7 @@ from mopidy.utils.path import path_to_uri, uri_to_path
logger = logging.getLogger('mopidy.backends.local')
# TODO: remove once tag cache is gone
def local_to_file_uri(uri, media_dir):
# TODO: check that type is correct.
file_path = uri_to_path(uri).split(b':', 1)[1]
@ -17,6 +19,19 @@ def local_to_file_uri(uri, media_dir):
return path_to_uri(file_path)
def local_to_path(uri, media_dir):
if not uri.startswith('local:track:'):
raise Exception
file_path = uri_to_path(uri).split(b':', 1)[1]
return os.path.join(media_dir, file_path)
def path_to_local(relpath):
if isinstance(relpath, unicode):
relpath = relpath.encode('utf-8')
return b'local:track:%s' % urllib.quote(relpath)
def parse_m3u(file_path, media_dir):
r"""
Convert M3U file list of uris