• sublime 自动升级 脚本


    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"
  • 相关阅读:
    [Err] 1064
    maven项目警告: Using platform encoding (UTF-8 actually) to copy filtered resources
    maven中引入oracle驱动报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0
    springMVC 中url后缀使用html,不能返回json数据,否则会报406错误
    网站并发量的计算方法
    win7旗舰版显示不了文件扩展名
    Java获取mysql数据库元数据
    spring中 context:property-placeholder 导入多个独立的 .properties配置文件
    Could not autowire field: private java.lang.Integer com.taotao.sso.service.impl.UserServiceImpl.SSO_
    linux Nginx服务开机自启
  • 原文地址:https://www.cnblogs.com/meetrice/p/2881486.html
Copyright © 2020-2023  润新知