fix updater bug
This commit is contained in:
		
							parent
							
								
									d93286e97c
								
							
						
					
					
						commit
						62d3e2119d
					
				@ -10,11 +10,14 @@ import io
 | 
				
			|||||||
from AnkiHub.updates import Ui_DialogUpdates
 | 
					from AnkiHub.updates import Ui_DialogUpdates
 | 
				
			||||||
from AnkiHub.markdown2 import markdown
 | 
					from AnkiHub.markdown2 import markdown
 | 
				
			||||||
import aqt
 | 
					import aqt
 | 
				
			||||||
 | 
					from aqt import mw
 | 
				
			||||||
from anki.hooks import addHook
 | 
					from anki.hooks import addHook
 | 
				
			||||||
from anki.utils import isMac, isWin
 | 
					from anki.utils import isMac, isWin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					'''
 | 
				
			||||||
# taken from Anki's aqt/profiles.py
 | 
					# taken from Anki's aqt/profiles.py
 | 
				
			||||||
def defaultBase():
 | 
					def defaultBase():
 | 
				
			||||||
 | 
					    print(mw.pm.addonFolder())
 | 
				
			||||||
    if isWin:
 | 
					    if isWin:
 | 
				
			||||||
        loc = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DocumentsLocation)
 | 
					        loc = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DocumentsLocation)
 | 
				
			||||||
        return os.path.join(loc, "Anki")
 | 
					        return os.path.join(loc, "Anki")
 | 
				
			||||||
@ -31,9 +34,10 @@ def defaultBase():
 | 
				
			|||||||
                return os.path.expanduser("~/Documents/Anki")
 | 
					                return os.path.expanduser("~/Documents/Anki")
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                return os.path.join(loc, "Anki")
 | 
					                return os.path.join(loc, "Anki")
 | 
				
			||||||
 | 
					'''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
headers = {"User-Agent": "AnkiHub"}
 | 
					headers = {"User-Agent": "AnkiHub"}
 | 
				
			||||||
dataPath = os.path.join(defaultBase(),'.ankihub.json')
 | 
					dataPath = '.ankihub.json' #os.path.join(defaultBase(),'.ankihub.json')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class DialogUpdates(QtGui.QDialog, Ui_DialogUpdates):
 | 
					class DialogUpdates(QtGui.QDialog, Ui_DialogUpdates):
 | 
				
			||||||
@ -43,7 +47,11 @@ class DialogUpdates(QtGui.QDialog, Ui_DialogUpdates):
 | 
				
			|||||||
        totalSize = sum(map(lambda x:x['size'],data['assets']))
 | 
					        totalSize = sum(map(lambda x:x['size'],data['assets']))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def answer(doUpdate,answ):
 | 
					        def answer(doUpdate,answ):
 | 
				
			||||||
            callback(doUpdate,answ,self.appendHtml,self.close,install)
 | 
					            self.update.setEnabled(False)
 | 
				
			||||||
 | 
					            self.dont.setEnabled(False)
 | 
				
			||||||
 | 
					            self.always.setEnabled(False)
 | 
				
			||||||
 | 
					            self.never.setEnabled(False)
 | 
				
			||||||
 | 
					            callback(doUpdate,answ,self.appendHtml,self.finish,install)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.html = u''
 | 
					        self.html = u''
 | 
				
			||||||
        self.appendHtml(markdown(data['body']))
 | 
					        self.appendHtml(markdown(data['body']))
 | 
				
			||||||
@ -58,10 +66,10 @@ class DialogUpdates(QtGui.QDialog, Ui_DialogUpdates):
 | 
				
			|||||||
            self.connect(self.never,QtCore.SIGNAL('clicked()'),
 | 
					            self.connect(self.never,QtCore.SIGNAL('clicked()'),
 | 
				
			||||||
                     lambda:answer(False,'never'))
 | 
					                     lambda:answer(False,'never'))
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self.update.setEnabled(False)
 | 
					            #self.update.setEnabled(False)
 | 
				
			||||||
            self.dont.setEnabled(False)
 | 
					            #self.dont.setEnabled(False)
 | 
				
			||||||
            self.always.setEnabled(False)
 | 
					            #self.always.setEnabled(False)
 | 
				
			||||||
            self.never.setEnabled(False)
 | 
					            #self.never.setEnabled(False)
 | 
				
			||||||
            answer(True,automaticAnswer)
 | 
					            answer(True,automaticAnswer)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        fromVersion = ''
 | 
					        fromVersion = ''
 | 
				
			||||||
@ -78,10 +86,13 @@ class DialogUpdates(QtGui.QDialog, Ui_DialogUpdates):
 | 
				
			|||||||
        self.html += html
 | 
					        self.html += html
 | 
				
			||||||
        self.textBrowser.setHtml(u'<html><body>{0}{1}</body></html>'.format(self.html,temp))
 | 
					        self.textBrowser.setHtml(u'<html><body>{0}{1}</body></html>'.format(self.html,temp))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def finish(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def installZipFile(data, fname):
 | 
					def installZipFile(data, fname):
 | 
				
			||||||
    base = os.path.join(defaultBase(),'addons')
 | 
					    base = mw.pm.addonFolder()#os.path.join(defaultBase(),'addons')
 | 
				
			||||||
    if fname.endswith(".py"):
 | 
					    if fname.endswith(".py"):
 | 
				
			||||||
        path = os.path.join(base, fname)
 | 
					        path = os.path.join(base, fname)
 | 
				
			||||||
        open(path, "wb").write(data)
 | 
					        open(path, "wb").write(data)
 | 
				
			||||||
@ -140,6 +151,7 @@ def updateSingle(repositories,path,data):
 | 
				
			|||||||
                    if install:
 | 
					                    if install:
 | 
				
			||||||
                        filesBefore = aqt.mw.addonManager.files()
 | 
					                        filesBefore = aqt.mw.addonManager.files()
 | 
				
			||||||
                        #directoriesBefore = aqt.mw.addonManager.directories()
 | 
					                        #directoriesBefore = aqt.mw.addonManager.directories()
 | 
				
			||||||
 | 
					                    appendHtml('Installing ...<br/>')
 | 
				
			||||||
                    if not installZipFile(d,fname):
 | 
					                    if not installZipFile(d,fname):
 | 
				
			||||||
                        appendHtml('Corrupt file<br/>')
 | 
					                        appendHtml('Corrupt file<br/>')
 | 
				
			||||||
                    else:
 | 
					                    else:
 | 
				
			||||||
@ -151,7 +163,6 @@ def updateSingle(repositories,path,data):
 | 
				
			|||||||
                            appendHtml('Executing new scripts...<br/>')
 | 
					                            appendHtml('Executing new scripts...<br/>')
 | 
				
			||||||
                            newFiles = set(aqt.mw.addonManager.files()) - set(filesBefore)
 | 
					                            newFiles = set(aqt.mw.addonManager.files()) - set(filesBefore)
 | 
				
			||||||
                            #newDirectories = set(aqt.mw.addonManager.directories()) - set(directoriesBefore)
 | 
					                            #newDirectories = set(aqt.mw.addonManager.directories()) - set(directoriesBefore)
 | 
				
			||||||
                            onReady() # close the AnkiHub update window
 | 
					 | 
				
			||||||
                            for file in newFiles:
 | 
					                            for file in newFiles:
 | 
				
			||||||
                                try:
 | 
					                                try:
 | 
				
			||||||
                                    __import__(file.replace(".py", ""))
 | 
					                                    __import__(file.replace(".py", ""))
 | 
				
			||||||
@ -163,7 +174,10 @@ def updateSingle(repositories,path,data):
 | 
				
			|||||||
                            #    except:
 | 
					                            #    except:
 | 
				
			||||||
                            #        traceback.print_exc()
 | 
					                            #        traceback.print_exc()
 | 
				
			||||||
                            aqt.mw.addonManager.rebuildAddonsMenu()
 | 
					                            aqt.mw.addonManager.rebuildAddonsMenu()
 | 
				
			||||||
 | 
					                            appendHtml('Done.<br/>')
 | 
				
			||||||
 | 
					                            onReady() # close the AnkiHub update window       
 | 
				
			||||||
                        else:
 | 
					                        else:
 | 
				
			||||||
 | 
					                            appendHtml('Done.<br/>Please restart Anki.<br/>')
 | 
				
			||||||
                            onReady() # close the AnkiHub update window
 | 
					                            onReady() # close the AnkiHub update window
 | 
				
			||||||
                        
 | 
					                        
 | 
				
			||||||
                installData()
 | 
					                installData()
 | 
				
			||||||
@ -196,6 +210,7 @@ def update(add=[],install=False):
 | 
				
			|||||||
                'update': 'ask'
 | 
					                'update': 'ask'
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ret = False
 | 
				
			||||||
    for path,repository in repositories.items():
 | 
					    for path,repository in repositories.items():
 | 
				
			||||||
        username,repositoryName = path.split('/')
 | 
					        username,repositoryName = path.split('/')
 | 
				
			||||||
        if repository['update'] != 'never':
 | 
					        if repository['update'] != 'never':
 | 
				
			||||||
@ -261,8 +276,11 @@ def update(add=[],install=False):
 | 
				
			|||||||
                    else:
 | 
					                    else:
 | 
				
			||||||
                        dialog = DialogUpdates(None,data,repository,updateSingle(repositories,path,data))
 | 
					                        dialog = DialogUpdates(None,data,repository,updateSingle(repositories,path,data))
 | 
				
			||||||
                    dialog.exec_()
 | 
					                    dialog.exec_()
 | 
				
			||||||
 | 
					                    ret = True
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    with open(dataPath,'w') as file:
 | 
					    with open(dataPath,'w') as file:
 | 
				
			||||||
        json.dump(repositories,file,indent=2)
 | 
					        json.dump(repositories,file,indent=2)
 | 
				
			||||||
 | 
					    return ret
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#update()
 | 
					#update()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -463,8 +463,10 @@ class OptionsDialog(QDialog):
 | 
				
			|||||||
def check_updates():
 | 
					def check_updates():
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        from .libs import ankihub
 | 
					        from .libs import ankihub
 | 
				
			||||||
        ankihub.update(['sth2018/FastWordQuery'])
 | 
					        if not ankihub.update(['sth2018/FastWordQuery']):
 | 
				
			||||||
 | 
					            showInfo(_('LATEST_VERSION'))
 | 
				
			||||||
    except:
 | 
					    except:
 | 
				
			||||||
 | 
					        showInfo(_('CHECK_FAILURE'))
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user