Bugfixes in helpers.py for MediaFile errors. Pre-detect potential file errors

Conflicts:
	headphones/postprocessor.py
This commit is contained in:
Bas Stottelaar
2014-04-14 17:49:05 +02:00
parent 5be36b517f
commit d490091a9a
2 changed files with 33 additions and 12 deletions
+1 -1
View File
@@ -360,7 +360,7 @@ def extract_metadata(f):
# Try to read the file info # Try to read the file info
try: try:
media_file = MediaFile(os.path.join(root, file)) media_file = MediaFile(os.path.join(root, file))
except FileTypeError, UnreadableFileError: except (FileTypeError, UnreadableFileError):
# Probably not a media file # Probably not a media file
continue continue
+32 -11
View File
@@ -22,7 +22,7 @@ import threading
import headphones import headphones
from beets import autotag from beets import autotag
from beets.mediafile import MediaFile from beets.mediafile import MediaFile, FileTypeError, UnreadableFileError
from headphones import notifiers from headphones import notifiers
from headphones import db, albumart, librarysync, lyrics from headphones import db, albumart, librarysync, lyrics
@@ -346,6 +346,27 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list,
downloaded_track_list.append(os.path.join(r, files)) downloaded_track_list.append(os.path.join(r, files))
elif files.lower().endswith('.cue'): elif files.lower().endswith('.cue'):
downloaded_cuecount += 1 downloaded_cuecount += 1
# Check if files are valid media files and are writeable, before the steps
# below are executed. This simplifies errors and prevents unfinished steps.
for downloaded_track in downloaded_track_list:
try:
media_file = MediaFile(downloaded_track)
except (FileTypeError, UnreadableFileError):
logger.error("Track file is not a valid media file: %s. Not continuing.", downloaded_track.decode(headphones.SYS_ENCODING, 'replace'))
return
# Not sure if line(s) below are needed, since it is possible to not
# touch any files.
if headphones.EMBED_ALBUM_ART or headphones.CLEANUP_FILES or \
headphones.ADD_ALBUM_ART or headphones.CORRECT_METADATA or \
headphones.EMBED_LYRICS or headphones.RENAME_FILES or \
headphones.MOVE_FILES:
if not os.access(downloaded_track, os.W_OK):
logger.error("Track file is not writeable, which is equired for some post processing steps: %s", downloaded_track.decode(headphones.SYS_ENCODING, 'replace'))
return
#start encoding #start encoding
if headphones.MUSIC_ENCODER: if headphones.MUSIC_ENCODER:
downloaded_track_list=music_encoder.encode(albumpath) downloaded_track_list=music_encoder.encode(albumpath)
@@ -634,7 +655,7 @@ def moveFiles(albumpath, release, tracks):
try: try:
shutil.rmtree(lossless_destination_path) shutil.rmtree(lossless_destination_path)
except Exception, e: except Exception, e:
logger.error("Error deleting existing folder: %s. Creating duplicate folder. Error: %s" % (lossless_destination_path.decode(headphones.SYS_ENCODING, 'replace'), str(e))) logger.error("Error deleting existing folder: %s. Creating duplicate folder. Error: %s" % (lossless_destination_path.decode(headphones.SYS_ENCODING, 'replace'), e))
create_duplicate_folder = True create_duplicate_folder = True
if not headphones.REPLACE_EXISTING_FOLDERS or create_duplicate_folder: if not headphones.REPLACE_EXISTING_FOLDERS or create_duplicate_folder:
@@ -667,7 +688,7 @@ def moveFiles(albumpath, release, tracks):
try: try:
shutil.rmtree(lossy_destination_path) shutil.rmtree(lossy_destination_path)
except Exception, e: except Exception, e:
logger.error("Error deleting existing folder: %s. Creating duplicate folder. Error: %s" % (lossy_destination_path.decode(headphones.SYS_ENCODING, 'replace'), str(e))) logger.error("Error deleting existing folder: %s. Creating duplicate folder. Error: %s" % (lossy_destination_path.decode(headphones.SYS_ENCODING, 'replace'), e))
create_duplicate_folder = True create_duplicate_folder = True
if not headphones.REPLACE_EXISTING_FOLDERS or create_duplicate_folder: if not headphones.REPLACE_EXISTING_FOLDERS or create_duplicate_folder:
@@ -748,13 +769,13 @@ def moveFiles(albumpath, release, tracks):
try: try:
os.chmod(os.path.normpath(temp_f).encode(headphones.SYS_ENCODING, 'replace'), int(headphones.FOLDER_PERMISSIONS, 8)) os.chmod(os.path.normpath(temp_f).encode(headphones.SYS_ENCODING, 'replace'), int(headphones.FOLDER_PERMISSIONS, 8))
except Exception, e: except Exception, e:
logger.error("Error trying to change permissions on folder: %s. %s", temp_f.decode(headphones.SYS_ENCODING, 'replace'), e) logger.error("Error trying to change permissions on folder: %s. %s", temp_f, e)
# If we failed to move all the files out of the directory, this will fail too # If we failed to move all the files out of the directory, this will fail too
try: try:
shutil.rmtree(albumpath) shutil.rmtree(albumpath)
except Exception, e: except Exception, e:
logger.error('Could not remove directory: %s. %s', albumpath.decode(headphones.SYS_ENCODING, 'replace'), e) logger.error('Could not remove directory: %s. %s', albumpath, e)
destination_paths = [] destination_paths = []
@@ -1108,16 +1129,18 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None):
continue continue
# Attempt 4: Hail mary. Just assume the folder name is the album name if it doesn't have a separator in it # Attempt 4: Hail mary. Just assume the folder name is the album name if it doesn't have a separator in it
if '-' not in folder: logger.debug('Attempt to extract album name by assuming it is the folder name')
release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE AlbumTitle LIKE ?', [folder]).fetchone()
if '-' not in folder_basename:
release = myDB.action('SELECT AlbumID, ArtistName, AlbumTitle from albums WHERE AlbumTitle LIKE ?', [folder_basename]).fetchone()
if release: if release:
logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album', release['ArtistName'], release['AlbumTitle']) logger.info('Found a match in the database: %s - %s. Verifying to make sure it is the correct album', release['ArtistName'], release['AlbumTitle'])
verify(release['AlbumID'], folder) verify(release['AlbumID'], folder)
continue continue
else: else:
logger.info('Querying MusicBrainz for the release group id for: %s', folder) logger.info('Querying MusicBrainz for the release group id for: %s', folder_basename)
try: try:
rgid = mb.findAlbumID(album=helpers.latinToAscii(folder)) rgid = mb.findAlbumID(album=helpers.latinToAscii(folder_basename))
except: except:
logger.error('Can not get release information for this album') logger.error('Can not get release information for this album')
rgid = None rgid = None
@@ -1127,5 +1150,3 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None):
continue continue
else: else:
logger.info('No match found on MusicBrainz for: %s - %s', name, album) logger.info('No match found on MusicBrainz for: %s - %s', name, album)