From a8fcacc6f86dac3f475e3cd4cf809cc113527303 Mon Sep 17 00:00:00 2001 From: Remy Date: Thu, 14 Jul 2011 18:53:19 -0700 Subject: [PATCH] Added self-update for tar/zip installs, added black hole option --- headphones/__init__.py | 9 ++- headphones/searcher.py | 105 ++++++++++++++----------- headphones/templates.py | 19 ++++- headphones/version.py | 1 + headphones/versioncheck.py | 153 ++++++++++++++++++++++++++++++------- headphones/webserve.py | 13 +++- 6 files changed, 221 insertions(+), 79 deletions(-) create mode 100644 headphones/version.py diff --git a/headphones/__init__.py b/headphones/__init__.py index 75c65bb5..882fed07 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -43,6 +43,7 @@ HTTP_ROOT = None LAUNCH_BROWSER = False GIT_PATH = None +INSTALL_TYPE = None CURRENT_VERSION = None LATEST_VERSION = None COMMITS_BEHIND = None @@ -58,6 +59,8 @@ RENAME_FILES = False CLEANUP_FILES = False ADD_ALBUM_ART = False DOWNLOAD_DIR = None +BLACKHOLE = None +BLACKHOLE_DIR = None USENET_RETENTION = None NZB_SEARCH_INTERVAL = 360 @@ -135,7 +138,7 @@ def initialize(): HTTP_PORT, HTTP_HOST, HTTP_USERNAME, HTTP_PASSWORD, HTTP_ROOT, LAUNCH_BROWSER, GIT_PATH, \ CURRENT_VERSION, \ MUSIC_DIR, PREFER_LOSSLESS, FLAC_TO_MP3, MOVE_FILES, RENAME_FILES, FOLDER_FORMAT, \ - FILE_FORMAT, CLEANUP_FILES, ADD_ALBUM_ART, DOWNLOAD_DIR, USENET_RETENTION, \ + FILE_FORMAT, CLEANUP_FILES, ADD_ALBUM_ART, DOWNLOAD_DIR, BLACKHOLE, BLACKHOLE_DIR, USENET_RETENTION, \ NZB_SEARCH_INTERVAL, LIBRARYSCAN_INTERVAL, \ SAB_HOST, SAB_USERNAME, SAB_PASSWORD, SAB_APIKEY, SAB_CATEGORY, \ NZBMATRIX, NZBMATRIX_USERNAME, NZBMATRIX_APIKEY, \ @@ -178,6 +181,8 @@ def initialize(): CLEANUP_FILES = bool(check_setting_int(CFG, 'General', 'cleanup_files', 0)) ADD_ALBUM_ART = bool(check_setting_int(CFG, 'General', 'add_album_art', 0)) DOWNLOAD_DIR = check_setting_str(CFG, 'General', 'download_dir', '') + BLACKHOLE = bool(check_setting_int(CFG, 'General', 'blackhole', 0)) + BLACKHOLE_DIR = check_setting_str(CFG, 'General', 'blackhole_dir', '') USENET_RETENTION = check_setting_int(CFG, 'General', 'usenet_retention', '') NZB_SEARCH_INTERVAL = check_setting_int(CFG, 'General', 'nzb_search_interval', 360) @@ -305,6 +310,8 @@ def config_write(): new_config['General']['cleanup_files'] = int(CLEANUP_FILES) new_config['General']['add_album_art'] = int(ADD_ALBUM_ART) new_config['General']['download_dir'] = DOWNLOAD_DIR + new_config['General']['blackhole'] = int(BLACKHOLE) + new_config['General']['blackhole_dir'] = BLACKHOLE_DIR new_config['General']['usenet_retention'] = USENET_RETENTION new_config['General']['nzb_search_interval'] = NZB_SEARCH_INTERVAL diff --git a/headphones/searcher.py b/headphones/searcher.py index a8b653d1..78543132 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -2,7 +2,7 @@ import urllib import string import lib.feedparser as feedparser import sqlite3 -import re +import os, re import headphones from headphones import logger @@ -64,13 +64,12 @@ def searchNZB(albumid=None): size = int(item.links[1]['length']) if size < maxsize: resultlist.append((title, size, url)) - logger.info(u"Found " + title +" : " + url + " (Size: " + size + ")") + logger.info('Found %s. Size: %i' % (title, size)) else: - logger.info(title + u" is larger than the maxsize for this category, skipping. (Size: " + size+")") - + logger.info('%s is larger than the maxsize for this category, skipping. (Size: %i bytes)' % (title, size)) - except: - logger.info(u"No results found") + except Exception, e: + logger.info(u"No results found. %s" % e) if headphones.NEWZNAB: @@ -100,12 +99,12 @@ def searchNZB(albumid=None): size = int(item.links[1]['length']) if size < maxsize: resultlist.append((title, size, url)) - logger.info(u"Found " + title +" : " + url + " (Size: " + size + ")") + logger.info('Found %s. Size: %i' % (title, size)) else: - logger.info(title + u" is larger than the maxsize for this category, skipping. (Size: " + size+")") + logger.info('%s is larger than the maxsize for this category, skipping. (Size: %i bytes)' % (title, size)) - except: - logger.info(u"No results found") + except Exception, e: + logger.info(u"No results found. %s" % e) if headphones.NZBSORG: @@ -137,50 +136,68 @@ def searchNZB(albumid=None): size = int(item.links[1]['length']) if size < maxsize: resultlist.append((title, size, url)) - logger.info(u"Found " + title +" : " + url + " (Size: " + size + ")") + logger.info('Found %s. Size: %i' % (title, size)) else: - logger.info(title + u" is larger than the maxsize for this category, skipping. (Size: " + size +")") - + logger.info('%s is larger than the maxsize for this category, skipping. (Size: %i bytes)' % (title, size)) - except: - logger.info(u"No results found") + except Exception, e: + logger.info(u"No results found. %s" % e) if len(resultlist): bestqual = sorted(resultlist, key=lambda title: title[1], reverse=True)[0] - logger.info(u"Downloading: " + bestqual[0]) + logger.info(u"Found best result: %s (%s) - %s bytes" % (bestqual[0], bestqual[2], bestqual[1])) downloadurl = bestqual[2] - linkparams = {} - - linkparams["mode"] = "addurl" - - if headphones.SAB_APIKEY: - linkparams["apikey"] = headphones.SAB_APIKEY - if headphones.SAB_USERNAME: - linkparams["ma_username"] = headphones.SAB_USERNAME - if headphones.SAB_PASSWORD: - linkparams["ma_password"] = headphones.SAB_PASSWORD - if headphones.SAB_CATEGORY: - linkparams["cat"] = headphones.SAB_CATEGORY - - linkparams["name"] = downloadurl + if headphones.SAB_HOST and not headphones.BLACKHOLE: + linkparams = {} - saburl = 'http://' + headphones.SAB_HOST + '/sabnzbd/api?' + urllib.urlencode(linkparams) - logger.info(u"Sending link to SABNZBD: " + saburl) - - try: - urllib.urlopen(saburl) + linkparams["mode"] = "addurl" - except: - logger.error(u"Unable to send link. Are you sure the host address is correct?") + if headphones.SAB_APIKEY: + linkparams["apikey"] = headphones.SAB_APIKEY + if headphones.SAB_USERNAME: + linkparams["ma_username"] = headphones.SAB_USERNAME + if headphones.SAB_PASSWORD: + linkparams["ma_password"] = headphones.SAB_PASSWORD + if headphones.SAB_CATEGORY: + linkparams["cat"] = headphones.SAB_CATEGORY + + linkparams["name"] = downloadurl + linkparams["nzbname"] = ('%s - %s [%s]' % (albums[0], albums[1], year)) + + saburl = 'http://' + headphones.SAB_HOST + '/sabnzbd/api?' + urllib.urlencode(linkparams) + logger.info(u"Sending link to SABNZBD: " + saburl) - c.execute('UPDATE albums SET status = "Snatched" WHERE AlbumID="%s"' % albums[2]) - c.execute('INSERT INTO snatched VALUES( ?, ?, ?, ?, CURRENT_DATE, ?)', (albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched")) - conn.commit() - - else: - pass + try: + urllib.urlopen(saburl) + + except: + logger.error(u"Unable to send link. Are you sure the host address is correct?") + break + + c.execute('UPDATE albums SET status = "Snatched" WHERE AlbumID="%s"' % albums[2]) + c.execute('INSERT INTO snatched VALUES( ?, ?, ?, ?, CURRENT_DATE, ?)', (albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched")) + conn.commit() + c.close() + + elif headphones.BLACKHOLE: + + nzb_name = ('%s - %s [%s].nzb' % (albums[0], albums[1], year)) + download_path = os.path.join(headphones.BLACKHOLE_DIR, nzb_name) + + try: + urllib.urlretrieve(downloadurl, download_path) + except Exception, e: + logger.error('Couldn\'t retrieve NZB: %s' % e) + break + + c.execute('UPDATE albums SET status = "Snatched" WHERE AlbumID="%s"' % albums[2]) + c.execute('INSERT INTO snatched VALUES( ?, ?, ?, ?, CURRENT_DATE, ?)', (albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched")) + conn.commit() + c.close() + + + - c.close() \ No newline at end of file diff --git a/headphones/templates.py b/headphones/templates.py index ecc49446..fd8409d2 100644 --- a/headphones/templates.py +++ b/headphones/templates.py @@ -140,6 +140,23 @@ configform = form = ''' i.e. Downloads/music or /Users/name/Downloads/music + + + +
+ +

Use Black Hole:

+ + + +
+ +

Black Hole Directory:


+ + Folder your Download program watches for NZBs + + + @@ -249,7 +266,7 @@ configform = form = '''
-

Path to Music folder:
+

Path to Music folder:

i.e. /Users/name/Music/iTunes or /Volumes/share/music

diff --git a/headphones/version.py b/headphones/version.py new file mode 100644 index 00000000..0dda129e --- /dev/null +++ b/headphones/version.py @@ -0,0 +1 @@ +HEADPHONES_VERSION = "master" \ No newline at end of file diff --git a/headphones/versioncheck.py b/headphones/versioncheck.py index b651227a..bebb782b 100644 --- a/headphones/versioncheck.py +++ b/headphones/versioncheck.py @@ -1,7 +1,10 @@ -import platform, subprocess, re +import platform, subprocess, re, os + +import urllib2 +import tarfile import headphones -from headphones import logger +from headphones import logger, version from lib.pygithub import github @@ -45,20 +48,47 @@ def runGit(args): def getVersion(): - output, err = runGit('rev-parse HEAD') - - if not output: - logger.error('Couldn\'t find latest installed version.') - return None + if version.HEADPHONES_VERSION.startswith('build '): - cur_commit_hash = output.strip() - - if not re.match('^[a-z0-9]+$', cur_commit_hash): - logger.error('Output doesn\'t look like a hash, not using it') - return None + headphones.INSTALL_TYPE = 'win' - return cur_commit_hash - + # Don't have a way to update exe yet, but don't want to set VERSION to None + return 'Windows Install' + + elif os.path.isdir(os.path.join(headphones.PROG_DIR, '.git')): + + headphones.INSTALL_TYPE = 'git' + output, err = runGit('rev-parse HEAD') + + if not output: + logger.error('Couldn\'t find latest installed version.') + return None + + cur_commit_hash = output.strip() + + if not re.match('^[a-z0-9]+$', cur_commit_hash): + logger.error('Output doesn\'t look like a hash, not using it') + return None + + return cur_commit_hash + + else: + + headphones.INSTALL_TYPE = 'source' + + version_file = os.path.join(headphones.PROG_DIR, 'version.txt') + + if not os.path.isfile(version_file): + return None + + fp = open(version_file, 'r') + current_version = fp.read().strip(' \n\r') + fp.close() + + if current_version: + return current_version + else: + return None def checkGithub(): @@ -88,20 +118,85 @@ def checkGithub(): def update(): - - output, err = runGit('pull origin master') - - if not output: - logger.error('Couldn\'t download latest version') - - for line in output.split('\n'): - - if 'Already up-to-date.' in line: - logger.info('No update available, not updating') - logger.info('Output: ' + str(output)) - elif line.endswith('Aborting.'): - logger.error('Unable to update from git: '+line) - logger.info('Output: ' + str(output)) - \ No newline at end of file + if headphones.INSTALL_TYPE == 'win': + + logger.info('Windows .exe updating not supported yet.') + pass + + + elif headphones.INSTALL_TYPE == 'git': + + output, err = runGit('pull origin ' + version.HEADPHONES_VERSION) + + if not output: + logger.error('Couldn\'t download latest version') + + for line in output.split('\n'): + + if 'Already up-to-date.' in line: + logger.info('No update available, not updating') + logger.info('Output: ' + str(output)) + elif line.endswith('Aborting.'): + logger.error('Unable to update from git: '+line) + logger.info('Output: ' + str(output)) + + else: + + tar_download_url = 'http://github.com/rembo10/headphones/tarball'+version.HEADPHONES_VERSION + update_dir = os.path.join(headphones.PROG_DIR, 'update') + version_path = os.path.join(headphones.PROG_DIR, 'version.txt') + + try: + logger.info('Downloading update from: '+tar_download_url) + data = urllib2.urlopen(tar_download_url) + except (IOError, URLError): + logger.error("Unable to retrieve new version from "+tar_download_url+", can't update") + return + + download_name = data.geturl().split('/')[-1] + + tar_download_path = os.path.join(headphones.PROG_DIR, download_name) + + # Save tar to disk + f = open(tar_download_path, 'wb') + f.write(data.read()) + f.close() + + # Extract the tar to update folder + logger.info('Extracing file' + tar_download_path) + tar = tarfile.open(tar_download_path) + tar.extractall(update_dir) + tar.close() + + # Delete the tar.gz + logger.info('Deleting file' + tar_download_path) + os.remove(tar_download_path) + + # Find update dir name + update_dir_contents = [x for x in os.listdir(update_dir) if os.path.isdir(os.path.join(update_dir, x))] + if len(update_dir_contents) != 1: + logger.error(u"Invalid update data, update failed: "+str(update_dir_contents)) + return + content_dir = os.path.join(update_dir, update_dir_contents[0]) + + # walk temp folder and move files to main folder + for dirname, dirnames, filenames in os.walk(content_dir): + dirname = dirname[len(content_dir)+1:] + for curfile in filenames: + old_path = os.path.join(content_dir, dirname, curfile) + new_path = os.path.join(headphones.PROG_DIR, dirname, curfile) + + if os.path.isfile(new_path): + os.remove(new_path) + os.renames(old_path, new_path) + + # Update version.txt + try: + ver_file = open(version_path, 'w') + ver_file.write(headphones.LATEST_VERSION) + ver_file.close() + except IOError, e: + logger.error(u"Unable to write version file, update not complete: "+ex(e)) + return \ No newline at end of file diff --git a/headphones/webserve.py b/headphones/webserve.py index 19a21ced..c7c5ff3f 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -26,9 +26,10 @@ class WebInterface(object): def home(self): page = [templates._header] - if headphones.LATEST_VERSION and headphones.CURRENT_VERSION: - if headphones.CURRENT_VERSION != headphones.LATEST_VERSION: - page.append('''
A + if not headphones.LATEST_VERSION: + page.append('''''') + elif headphones.CURRENT_VERSION != headphones.LATEST_VERSION and headphones.INSTALL_TYPE != 'win': + page.append('''
A newer version is available. You're %s commits behind. Click here to update
''' % (headphones.CURRENT_VERSION, headphones.LATEST_VERSION, headphones.COMMITS_BEHIND)) page.append(templates._logobar) @@ -556,6 +557,8 @@ class WebInterface(object): headphones.SAB_PASSWORD, headphones.SAB_CATEGORY, headphones.DOWNLOAD_DIR, + checked(headphones.BLACKHOLE), + headphones.BLACKHOLE_DIR, headphones.USENET_RETENTION, checked(headphones.NZBMATRIX), headphones.NZBMATRIX_USERNAME, @@ -581,7 +584,7 @@ class WebInterface(object): def configUpdate(self, http_host='0.0.0.0', http_username=None, http_port=8181, http_password=None, launch_browser=0, - sab_host=None, sab_username=None, sab_apikey=None, sab_password=None, sab_category=None, download_dir=None, + sab_host=None, sab_username=None, sab_apikey=None, sab_password=None, sab_category=None, download_dir=None, blackhole=0, blackhole_dir=None, usenet_retention=None, nzbmatrix=0, nzbmatrix_username=None, nzbmatrix_apikey=None, newznab=0, newznab_host=None, newznab_apikey=None, nzbsorg=0, nzbsorg_uid=None, nzbsorg_hash=None, prefer_lossless=0, flac_to_mp3=0, move_files=0, music_dir=None, rename_files=0, cleanup_files=0, add_album_art=0): @@ -596,6 +599,8 @@ class WebInterface(object): headphones.SAB_APIKEY = sab_apikey headphones.SAB_CATEGORY = sab_category headphones.DOWNLOAD_DIR = download_dir + headphones.BLACKHOLE = blackhole + headphones.BLACKHOLE_DIR = blackhole_dir headphones.USENET_RETENTION = usenet_retention headphones.NZBMATRIX = nzbmatrix headphones.NZBMATRIX_USERNAME = nzbmatrix_username