import sublime, sublime_plugin
import urllib2
import shutil
import os
import threading
import json
import sys
import formatter, htmllib
import subprocess
import zipfile
from urllib import quote
UPDATE_URL = "http://www.padpeek.com/sublime/update.txt" #url to check for latest st2 version
class LinksParser(htmllib.HTMLParser): #helper class for scraping the site for download links
def __init__(self, formatter):
htmllib.HTMLParser.__init__(self, formatter)
self.links = []
def start_a(self, attrs):
if len(attrs) > 0 :
for attr in attrs :
if attr[0] == "href":
# if attr[1].find("rackcdn") != -1:
self.links.append(attr[1])
def get_links(self):
return self.links
class BackgroundDownloader(threading.Thread):
def __init__(self, url, install_path, download_link):
self.url = url
self.result = None
self.download_link = download_link
self.install_path = install_path
threading.Thread.__init__(self)
def startInstaller(self):
file_name = self.download_link[self.download_link.find("sublimeExt"):]
fullfilename = sublime.packages_path() + "\\meetrice\\" + file_name
unzipdir = sublime.packages_path()
zip = zipfile.ZipFile(fullfilename,'r')
zip.extractall(unzipdir)
zip.close()
os.remove(fullfilename)
def run(self):
try:
file_name = self.url.split('/')[-1]
u = urllib2.urlopen(self.url)
print sublime.packages_path()
f = open(sublime.packages_path() + "\\meetrice\\" + file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
f.write(u.read())
f.close()
self.result = True
self.startInstaller()
return
except (urllib2.HTTPError) as (e):
err = '%s: HTTP error %s contacting URL' % (__name__, str(e.code))
except (urllib2.URLError) as (e):
err = '%s: URL error %s contacting URL' % (__name__, str(e.reason))
self.result = False
class SublimeUpdaterCommand(sublime_plugin.ApplicationCommand):
def getLatestVersion(self):
data = urllib2.urlopen(UPDATE_URL).read()
data = json.loads(data)
return data['latest_version']
def get_package_dir(self, package):
return os.path.join(sublime.packages_path(), package)
def get_metadata(self, package):
metadata_filename = os.path.join(self.get_package_dir(package),
'package-metadata.json')
if os.path.exists(metadata_filename):
with open(metadata_filename) as f:
try:
return json.load(f)
except (ValueError):
return {}
return {}
def run(self):
old_version = self.get_metadata('meetrice').get('version')
if int(self.getLatestVersion()) == int(old_version):
print ("currently on latest version")
else:
print ("new version available")
if sublime.platform() == "windows":
#download the latest installer
s = sublime.load_settings("Preferences.sublime-settings") #get the install path from preferences
install_path = s.get("install_path", "")
f = urllib2.urlopen("http://www.padpeek.com/sublime/")
format = formatter.NullFormatter()
parser = LinksParser(format)
html = f.read()
parser.feed(html) #get the list of latest installer urls
parser.close()
urls = parser.get_links()
download_link=urls[0]
download_link = quote(download_link, safe="%/:=&?~#+!$,;'@()*[]")
sublime.status_message('SublimeUpdater is downloading update')
thr = BackgroundDownloader(download_link, install_path, download_link) #start the download thread
threads = []
threads.append(thr)
thr.start()
elif sublime.platform() == "linux":
print "linux detected"
elif sublime.platform() == "osx":
print "mac detected"