mylar/mylar/versioncheck.py

236 lines
7.9 KiB
Python
Raw Normal View History

# This file is part of Mylar.
#
# Mylar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mylar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mylar. If not, see <http://www.gnu.org/licenses/>.
import platform, subprocess, re, os, urllib2, tarfile
import mylar
from mylar import logger, version
import lib.simplejson as simplejson
user = "evilhero"
branch = "development"
def runGit(args):
if mylar.GIT_PATH:
2015-05-22 08:32:51 +00:00
git_locations = ['"' +mylar.GIT_PATH +'"']
else:
git_locations = ['git']
2015-05-22 08:32:51 +00:00
if platform.system().lower() == 'darwin':
git_locations.append('/usr/local/git/bin/git')
2015-05-22 08:32:51 +00:00
output = err = None
2015-05-22 08:32:51 +00:00
for cur_git in git_locations:
2015-05-22 08:32:51 +00:00
cmd = cur_git +' ' +args
try:
logger.debug('Trying to execute: "' + cmd + '" with shell in ' + mylar.PROG_DIR)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=mylar.PROG_DIR)
output, err = p.communicate()
logger.debug('Git output: ' + output)
except OSError:
logger.debug('Command ' + cmd + ' didn\'t work, couldn\'t find git')
continue
2015-05-22 08:32:51 +00:00
if 'not found' in output or "not recognized as an internal or external command" in output:
logger.debug('Unable to find git with command ' + cmd)
output = None
elif 'fatal:' in output or err:
logger.error('Git returned bad info. Are you sure this is a git installation?')
output = None
elif output:
break
2015-05-22 08:32:51 +00:00
return (output, err)
2015-05-22 08:32:51 +00:00
def getVersion():
if version.MYLAR_VERSION.startswith('win32build'):
2015-05-22 08:32:51 +00:00
mylar.INSTALL_TYPE = 'win'
2015-05-22 08:32:51 +00:00
# Don't have a way to update exe yet, but don't want to set VERSION to None
return 'Windows Install'
2015-05-22 08:32:51 +00:00
elif os.path.isdir(os.path.join(mylar.PROG_DIR, '.git')):
2015-05-22 08:32:51 +00:00
mylar.INSTALL_TYPE = 'git'
output, err = runGit('rev-parse HEAD')
2015-05-22 08:32:51 +00:00
if not output:
logger.error('Couldn\'t find latest installed version.')
return None
2015-05-22 08:32:51 +00:00
2013-01-13 18:08:21 +00:00
#branch_history, err = runGit("log --oneline --pretty=format:'%h - %ar - %s' -n 5")
#bh = []
#print ("branch_history: " + branch_history)
#bh.append(branch_history.split('\n'))
#print ("bh1: " + bh[0])
cur_commit_hash = output.strip()
2015-05-22 08:32:51 +00:00
if not re.match('^[a-z0-9]+$', cur_commit_hash):
IMP: Ability to now specify search provider order (regardless of torrents or nzb) within the config.ini, IMP: (#667) Changed the db module to try to accomodate db locking errors and lowering the amount of actual write transactions that were committed along with a new scheduler system, IMP: Changed sabnzbd directory to post-processing, and included subdirs for sabnzbd & nzbget ComicRN scripts, IMP: NZBGet Post-Processing ComicRN.py script (updated for use with nzbget v11.0+)added & updated in post-processing/nzbget directory (thnx ministoat), FIX: If Issue Location was None, and status was Downloaded would cause error in GUI and break series, IMP: (#689) Minimum # of seeders added (will work with KAT), IMP: (#680) Added Boxcar 2 IO Notifications, IMP: Added PushBullet Notifications, IMP: Cleaned up some notification messages so it's not so cluttered, IMP: Added Clickable series link in History tab, IMP: Added Post-Processed as a status to History tab to show manually post-processed items, IMP: Removed log level dropdown from Logs page & added 'ThreadName' as a column, IMP: Added Force Check Availability & View Future Pull-list to Upcoming sub-tabs, IMP: Added '--safe' option to startup options which will redirect directly to Manage Comics screen incase things are broken, FIX: Added proper month conversions for manual post-processing when doing comparitive issue analysis for matches, FIX: (#613) Allow for negative issue numbers in post-processing when renaming and issue padding is enabled, FIX: File Permissions on post-processing would stop post-processing if couldn't change, now will just log the error and continue, IMP: Added Scheduler (from sickbeard) to allow for threadnaming and better scheduling, IMP: Filenames in the format of ' () ' will now get scanned in, IMP: During manual post-processing will now stop looking for matches upon a successful match, IMP: A Refresh/Weeklypull series check will now just scan in issue data, instead of series info,etc, IMP: Removed some legacy GCD code that is no longer in use, IMP: Exception/traceback handling will now be logged, FIX: Unable to grab torrents from KAT due to content-encoding detection failing, IMP: Added universal date-time conversion to allow for non-english based dates to be properly compared when checking search results against publication dates, FIX: Annuals will now get proper notification (prior was leaving out the word 'annual' from notification/logs), IMP: Improved future pull-list detection and increased retension (now ~5 months), IMP: Will now mark new issues as Wanted on a Refresh Series if autowant upcoming is enabled (was reverting to a status of None previously), IMP: Cannot change status to Downloaded if current status is Skipped or Wanted, FIX: (#704) UnSkipped will now work (X in options column on comic details page), IMP: future_check will check upcoming future issues (future pull-list) that have no series data yet (ie. #1's) and auto-add them to watchlist when the data is available and auto-want accordingly, IMP: (#706) Downloading issues to local machine (via comicdetails screen) with special characters in filename now will work, IMP: improved comparison checks during weekly pull list and improved speed abit since only refreshing issue data now instead of entire series, Other Referenced issues: (#670)(#690) and some others....
2014-05-25 18:32:11 +00:00
logger.error('Output does not look like a hash, not using it')
return None
2015-05-22 08:32:51 +00:00
return cur_commit_hash
2015-05-22 08:32:51 +00:00
else:
2015-05-22 08:32:51 +00:00
mylar.INSTALL_TYPE = 'source'
2015-05-22 08:32:51 +00:00
version_file = os.path.join(mylar.PROG_DIR, 'version.txt')
2015-05-22 08:32:51 +00:00
if not os.path.isfile(version_file):
return None
2015-05-22 08:32:51 +00:00
fp = open(version_file, 'r')
current_version = fp.read().strip(' \n\r')
fp.close()
2015-05-22 08:32:51 +00:00
if current_version:
return current_version
else:
return None
2015-05-22 08:32:51 +00:00
def checkGithub():
# Get the latest commit available from github
url = 'https://api.github.com/repos/%s/mylar/commits/%s' % (user, branch)
logger.info ('Retrieving latest version information from github')
try:
result = urllib2.urlopen(url).read()
git = simplejson.JSONDecoder().decode(result)
mylar.LATEST_VERSION = git['sha']
except:
logger.warn('Could not get the latest commit from github')
mylar.COMMITS_BEHIND = 0
return mylar.CURRENT_VERSION
2015-05-22 08:32:51 +00:00
# See how many commits behind we are
if mylar.CURRENT_VERSION:
logger.info('Comparing currently installed version with latest github version')
url = 'https://api.github.com/repos/%s/mylar/compare/%s...%s' % (user, mylar.CURRENT_VERSION, mylar.LATEST_VERSION)
2015-05-22 08:32:51 +00:00
try:
result = urllib2.urlopen(url).read()
git = simplejson.JSONDecoder().decode(result)
mylar.COMMITS_BEHIND = git['total_commits']
except:
logger.warn('Could not get commits behind from github')
mylar.COMMITS_BEHIND = 0
return mylar.CURRENT_VERSION
2015-05-22 08:32:51 +00:00
if mylar.COMMITS_BEHIND >= 1:
logger.info('New version is available. You are %s commits behind' % mylar.COMMITS_BEHIND)
elif mylar.COMMITS_BEHIND == 0:
logger.info('Mylar is up to date')
elif mylar.COMMITS_BEHIND == -1:
logger.info('You are running an unknown version of Mylar. Run the updater to identify your version')
2015-05-22 08:32:51 +00:00
else:
logger.info('You are running an unknown version of Mylar. Run the updater to identify your version')
2015-05-22 08:32:51 +00:00
return mylar.LATEST_VERSION
2015-05-22 08:32:51 +00:00
def update():
2015-05-22 08:32:51 +00:00
if mylar.INSTALL_TYPE == 'win':
2015-05-22 08:32:51 +00:00
logger.info('Windows .exe updating not supported yet.')
pass
2015-05-22 08:32:51 +00:00
elif mylar.INSTALL_TYPE == 'git':
2015-05-22 08:32:51 +00:00
output, err = runGit('pull origin ' + version.MYLAR_VERSION)
2015-05-22 08:32:51 +00:00
if not output:
logger.error('Couldn\'t download latest version')
2015-05-22 08:32:51 +00:00
for line in output.split('\n'):
2015-05-22 08:32:51 +00:00
if 'Already up-to-date.' in line:
logger.info('No update available, not updating')
logger.info('Output: ' + str(output))
elif line.endswith('Aborting.'):
2015-05-22 08:32:51 +00:00
logger.error('Unable to update from git: ' +line)
logger.info('Output: ' + str(output))
2015-05-22 08:32:51 +00:00
else:
2015-05-22 08:32:51 +00:00
tar_download_url = 'https://github.com/%s/mylar/tarball/%s' % (user, branch)
update_dir = os.path.join(mylar.PROG_DIR, 'update')
version_path = os.path.join(mylar.PROG_DIR, 'version.txt')
2015-05-22 08:32:51 +00:00
try:
2015-05-22 08:32:51 +00:00
logger.info('Downloading update from: ' +tar_download_url)
data = urllib2.urlopen(tar_download_url)
IMP: Cleaned up interface for StoryArcs / Story Arc Details, IMP: Cleaned up interface for Reading list Management, IMP: Added better reading list management - new status (added, downloaded, read), IMP: Added sync option for use with another device for reading list transfer (ie. tablet) Android only, IMP: Autopopulate new weekly pull releases to reading list, IMP: 'Watch' option in weekly pull list now fully functional. Will watch CV for series' that do not have any series data yet as they are new starting series. Will auto-add once available, IMP: Auto-watch check is run after every refresh/recreate of the weeklypull list, IMP: Improved the Add a Series option where it will now look for issues that are 'new' or 'wanted' during add sequence, IMP: Main page interface now has coloured have/total bars to denote series completion, IMP: New scheduler / threading locks in place in an attempt to avoid database locks, FIX: Removed some erroneous locking that was going on when importing a directory was being run, IMP: Stat counter now present when post-processing multiple issues in sequence, FIX: for issue number error when post-processing and issue number was a non-alphanumeric, FIX: for metatagging: when original file was .cbz, would try to convert and fail, FIX: for issues that were negative and were preceeded by a # in the filename (filechecker), FIX: for publisher having non-alphanumeric character in name when attempting to determine publisher, FIX: if annuals enabled, would incorrectly show as being 'already in library' when viewing search results if results constained annuals, FIX:(#944) for incorrect nzbname being used when post-processing was being performed from an nzb client (experimental mainly), IMP: Turned off logging for ComicVine API counter, FIX: Added retry attempts when connecting to ComicVine in order to avoid errors when adding a series, IMP:(#963) Added ability to add snatched to filter when viewing Wanted issues on Wanted tab, FIX: When importing and then selecting a series to import via the select screen, will now flip back to the importresults and add the selected series in the background, IMP:(#952) Main page is now sorted in ascending order by Continuing/Ended status (and subbed by whether is Active/Paused).Custom sorting is still available, FIX: Dupecheck will now automatically assume existing 0-byte files are to be overwritten when performing post-processing, FIX: If publication date for series contained a '?' (usually with brand new series) will force to 'Present' to allow for pull-list comparisons to take place, FIX: Mylar will now disallow search results which have 'covers only' or 'variant' in the filename, IMP: Better nzbname generation/retrieval (will check inside nzb for possible names) to be used when post-processing, IMP: DB Update will now perform update to all active comics in descending order by Latest Date (instead of random order), FIX: Enforce the 5hr limit rule when running DB update (will only update series that haven't been updated in >5 hours), FIX: Annuals will now have/retain the proper status upon doing DB Update, FIX: Have totals will now be updated when doing a recheck files (sometimes wouldn't get updated depending on various states of status'), FIX:(#966) Added urllib2.URLError exeception trap when attempting to check Git for updates, IMP: Removed the individual sqlite calls for weeklypull, and brought them into line with using the db module (which will minimize concurrent access, which seemed to be causing db locks), IMP: Cleaned up some code and shuffled some functions so they are in more appropriate locations
2015-03-27 17:27:59 +00:00
except (IOError, urllib2.URLError):
2015-05-22 08:32:51 +00:00
logger.error("Unable to retrieve new version from " +tar_download_url +", can't update")
return
2015-05-22 08:32:51 +00:00
#try sanitizing the name here...
download_name = data.geturl().split('/')[-1].split('?')[0]
tar_download_path = os.path.join(mylar.PROG_DIR, download_name)
2015-05-22 08:32:51 +00:00
# Save tar to disk
f = open(tar_download_path, 'wb')
f.write(data.read())
f.close()
2015-05-22 08:32:51 +00:00
# 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()
2015-05-22 08:32:51 +00:00
# Delete the tar.gz
logger.info('Deleting file' + tar_download_path)
os.remove(tar_download_path)
2015-05-22 08:32:51 +00:00
# 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:
2015-05-22 08:32:51 +00:00
logger.error(u"Invalid update data, update failed: " +str(update_dir_contents))
return
content_dir = os.path.join(update_dir, update_dir_contents[0])
2015-05-22 08:32:51 +00:00
# walk temp folder and move files to main folder
for dirname, dirnames, filenames in os.walk(content_dir):
2015-05-22 08:32:51 +00:00
dirname = dirname[len(content_dir) +1:]
for curfile in filenames:
old_path = os.path.join(content_dir, dirname, curfile)
new_path = os.path.join(mylar.PROG_DIR, dirname, curfile)
2015-05-22 08:32:51 +00:00
if os.path.isfile(new_path):
os.remove(new_path)
os.renames(old_path, new_path)
2015-05-22 08:32:51 +00:00
# Update version.txt
try:
ver_file = open(version_path, 'w')
ver_file.write(mylar.LATEST_VERSION)
ver_file.close()
except IOError, e:
2015-05-22 08:32:51 +00:00
logger.error(u"Unable to write current version to version.txt, update not complete: " +ex(e))
return