<s> from extensions . extension import Extension , ExtensionError <EOL> class InstallationError ( Exception ) : <EOL> def __init__ ( self , title , message , is_html = False ) : <EOL> self . title = title <EOL> self . message = message <EOL> self . is_html = is_html <EOL> def doInstallExtension ( db , user , extension , version ) : <EOL> is_universal = user is None <EOL> extension_id = extension . getExtensionID ( db , create = True ) <EOL> manifest = extension . getManifest ( version ) <EOL> current_installs = Extension . getInstalls ( db , user ) <EOL> for current_extension_id , _ , _ , current_is_universal in current_installs : <EOL> if is_universal != current_is_universal : <EOL> continue <EOL> try : <EOL> current_extension = Extension . fromId ( db , current_extension_id ) <EOL> except ExtensionError as error : <EOL> if extension . getName ( ) == error . extension . getName ( ) : <EOL> doUninstallExtension ( db , user , error . extension ) <EOL> continue <EOL> if extension_id == current_extension_id : <EOL> raise InstallationError ( <EOL> title = "<STR_LIT>" , <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( current_extension . getTitle ( db ) , <EOL> "<STR_LIT>" if is_universal else "<STR_LIT>" ) ) , <EOL> is_html = True ) <EOL> if extension . getName ( ) == current_extension . getName ( ) : <EOL> raise InstallationError ( <EOL> title = "<STR_LIT>" , <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( current_extension . getTitle ( db ) , <EOL> "<STR_LIT>" if is_universal else "<STR_LIT>" , <EOL> extension . getTitle ( db ) ) ) , <EOL> is_html = True ) <EOL> cursor = db . cursor ( ) <EOL> if is_universal : <EOL> user_id = None <EOL> else : <EOL> user_id = user . id <EOL> if version is not None : <EOL> sha1 = extension . prepareVersionSnapshot ( version ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( extension_id , version , sha1 ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> ( version_id , ) = row <EOL> else : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( extension_id , version , sha1 ) ) <EOL> ( version_id , ) = cursor . fetchone ( ) <EOL> for role in manifest . roles : <EOL> role . install ( db , version_id ) <EOL> else : <EOL> version_id = None <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( user_id , extension_id , version_id ) ) <EOL> def doUninstallExtension ( db , user , extension ) : <EOL> extension_id = extension . getExtensionID ( db ) <EOL> if extension_id is None : <EOL> return <EOL> cursor = db . cursor ( ) <EOL> if user is not None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( user . id , extension_id ) ) <EOL> else : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( extension_id , ) ) <EOL> def getExtension ( author_name , extension_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return Extension ( author_name , extension_name ) <EOL> except ExtensionError as error : <EOL> if error . extension is None : <EOL> raise error <EOL> return error . extension <EOL> def installExtension ( db , user , author_name , extension_name , version ) : <EOL> doInstallExtension ( db , user , Extension ( author_name , extension_name ) , version ) <EOL> db . commit ( ) <EOL> def uninstallExtension ( db , user , author_name , extension_name , version ) : <EOL> doUninstallExtension ( db , user , getExtension ( author_name , extension_name ) ) <EOL> db . commit ( ) <EOL> def reinstallExtension ( db , user , author_name , extension_name , version ) : <EOL> doUninstallExtension ( db , user , getExtension ( author_name , extension_name ) ) <EOL> doInstallExtension ( db , user , Extension ( author_name , extension_name ) , version ) <EOL> db . commit ( ) </s>
