cue sheet + temp dir fix ups

- cue not processing correctly if using temp dir
-  allow temp dir to be specified from new config option
- attempt to stop multiple temp dirs created for the same albumpath
This commit is contained in:
Ade
2016-12-11 17:11:42 +13:00
parent 0ecfe499fc
commit 4d1107ee54
3 changed files with 87 additions and 41 deletions

View File

@@ -144,6 +144,7 @@ _CONFIG_DEFINITIONS = {
'KAT_RATIO': (str, 'Kat', ''),
'KEEP_NFO': (int, 'General', 0),
'KEEP_TORRENT_FILES': (int, 'General', 0),
'KEEP_TORRENT_FILES_DIR': (path, 'General', ''),
'LASTFM_USERNAME': (str, 'General', ''),
'LAUNCH_BROWSER': (int, 'General', 1),
'LIBRARYSCAN': (int, 'General', 1),

View File

@@ -20,6 +20,8 @@ import datetime
import shutil
import time
import sys
import tempfile
import glob
import fnmatch
import re
@@ -623,24 +625,41 @@ def get_downloaded_track_list(albumpath):
def preserve_torrent_directory(albumpath):
"""
Copy torrent directory to headphones-modified to keep files for seeding.
Copy torrent directory to temp headphones_ directory to keep files for seeding.
"""
from headphones import logger
new_folder = os.path.join(albumpath,
'headphones-modified'.encode(headphones.SYS_ENCODING, 'replace'))
logger.info(
"Copying files to 'headphones-modified' subfolder to preserve downloaded files for seeding")
try:
shutil.copytree(albumpath, new_folder)
return new_folder
except Exception as e:
logger.warn("Cannot copy/move files to temp folder: " +
new_folder.decode(headphones.SYS_ENCODING, 'replace') +
". Not continuing. Error: " + str(e))
# Create temp dir
if headphones.CONFIG.KEEP_TORRENT_FILES_DIR:
tempdir = headphones.CONFIG.KEEP_TORRENT_FILES_DIR
else:
tempdir = tempfile.gettempdir()
prefix = "headphones_" + os.path.basename(os.path.normpath(albumpath)) + "_"
new_folder = tempfile.mkdtemp(prefix=prefix, dir=tempdir)
# Copy to temp dir
subdir = os.path.join(new_folder, "headphones")
logger.info("Copying files to " + subdir.decode(headphones.SYS_ENCODING, 'replace')
+ " subfolder to preserve downloaded files for seeding")
# Attempt to stop multiple temp dirs being created for the same albumpath
tempdir = os.path.join(tempdir, prefix)
if len (glob.glob(tempdir + '*/')) >= 3:
logger.error("Looks like a temp subfolder has previously been created for this albumpath, not continuing "
+ tempdir.decode(headphones.SYS_ENCODING, 'replace'))
return None
try:
shutil.copytree(albumpath, subdir)
# Update the album path with the new location
return subdir
except Exception as e:
logger.warn("Cannot copy/move files to temp folder: "
+ new_folder.decode(headphones.SYS_ENCODING, 'replace') + ". Not continuing. Error: " + str(e))
shutil.rmtree(new_folder)
return None
def cue_split(albumpath):
def cue_split(albumpath,keep_original_folder=False):
"""
Attempts to check and split audio files by a cue for the given directory.
"""
@@ -662,6 +681,15 @@ def cue_split(albumpath):
# Split cue
if cue_count and cue_count >= count and cue_dirs:
# Copy to temp directory
if keep_original_folder:
temppath = preserve_torrent_directory(albumpath)
if temppath:
cue_dirs = [cue_dir.replace(albumpath, temppath) for cue_dir in cue_dirs]
albumpath = temppath
else:
return None
from headphones import logger, cuesplit
logger.info("Attempting to split audio files by cue")
@@ -672,12 +700,12 @@ def cue_split(albumpath):
except Exception as e:
os.chdir(cwd)
logger.warn("Cue not split: " + str(e))
return False
return None
os.chdir(cwd)
return True
return albumpath
return False
return None
def extract_logline(s):

View File

@@ -17,7 +17,6 @@ import shutil
import uuid
import threading
import itertools
import tempfile
import os
import re
@@ -207,13 +206,23 @@ def verify(albumid, albumpath, Kind=None, forced=False, keep_original_folder=Fal
'replace') + " isn't complete yet. Will try again on the next run")
return
# Split cue
# Check to see if we're preserving the torrent dir
if (headphones.CONFIG.KEEP_TORRENT_FILES and Kind == "torrent") or headphones.CONFIG.KEEP_ORIGINAL_FOLDER:
keep_original_folder = True
# Split cue before metadata check
if headphones.CONFIG.CUE_SPLIT and downloaded_cuecount and downloaded_cuecount >= len(
downloaded_track_list):
if headphones.CONFIG.KEEP_TORRENT_FILES and Kind == "torrent":
if keep_original_folder:
keep_original_folder = False
albumpath = helpers.preserve_torrent_directory(albumpath)
if albumpath and helpers.cue_split(albumpath):
downloaded_track_list = helpers.get_downloaded_track_list(albumpath)
if not albumpath:
return
Kind = "cue_split"
albumpath = helpers.cue_split(albumpath)
if not albumpath:
return
downloaded_track_list = helpers.get_downloaded_track_list(albumpath)
# test #1: metadata - usually works
logger.debug('Verifying metadata...')
@@ -316,19 +325,16 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list,
keep_original_folder=False):
logger.info('Starting post-processing for: %s - %s' % (release['ArtistName'], release['AlbumTitle']))
new_folder = None
# Check to see if we're preserving the torrent dir
if (headphones.CONFIG.KEEP_TORRENT_FILES and Kind == "torrent" and 'headphones-modified' not in albumpath) or headphones.CONFIG.KEEP_ORIGINAL_FOLDER or keep_original_folder:
new_folder = tempfile.mkdtemp(prefix="headphones_")
subdir = os.path.join(new_folder, "headphones")
logger.info("Copying files to " + subdir.decode(headphones.SYS_ENCODING, 'replace') + " subfolder to preserve downloaded files for seeding")
try:
shutil.copytree(albumpath, subdir)
# Update the album path with the new location
albumpath = subdir
except Exception as e:
logger.warn("Cannot copy/move files to temp folder: " + new_folder.decode(headphones.SYS_ENCODING, 'replace') + ". Not continuing. Error: " + str(e))
shutil.rmtree(new_folder)
# Preserve the torrent dir
if keep_original_folder:
albumpath = helpers.preserve_torrent_directory(albumpath)
if not albumpath:
return
else:
new_folder = os.path.split(albumpath)[0]
elif Kind == "cue_split":
new_folder = os.path.split(albumpath)[0]
# Need to update the downloaded track list with the new location.
# Could probably just throw in the "headphones-modified" folder,
@@ -1257,12 +1263,23 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None, keep_orig
except Exception:
name = album = None
# Check if there's a cue to split
if headphones.CONFIG.CUE_SPLIT and not name and not album and helpers.cue_split(folder):
try:
name, album, year = helpers.extract_metadata(folder)
except Exception:
name = album = None
# Not found from meta data, check if there's a cue to split and try meta data again
kind = None
if headphones.CONFIG.CUE_SPLIT and not name and not album:
cue_folder = helpers.cue_split(folder,keep_original_folder=keep_original_folder)
if cue_folder:
try:
name, album, year = helpers.extract_metadata(cue_folder)
except Exception:
name = album = None
if name:
folder = cue_folder
if keep_original_folder:
keep_original_folder = False
kind = "cue_split"
elif folder != cue_folder:
cue_folder = os.path.split(cue_folder)[0]
shutil.rmtree(cue_folder)
if name and album:
release = myDB.action(
@@ -1272,7 +1289,7 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None, keep_orig
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, keep_original_folder=keep_original_folder)
verify(release['AlbumID'], folder, Kind=kind, keep_original_folder=keep_original_folder)
continue
else:
logger.info('Querying MusicBrainz for the release group id for: %s - %s', name,
@@ -1284,7 +1301,7 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None, keep_orig
rgid = None
if rgid:
verify(rgid, folder, keep_original_folder=keep_original_folder)
verify(rgid, folder, Kind=kind, keep_original_folder=keep_original_folder)
continue
else:
logger.info('No match found on MusicBrainz for: %s - %s', name, album)