def updater_run_install_popup_handler(scene):
    global ran_autocheck_install_popup
    ran_autocheck_install_popup = True

    # in case of error importing updater
    if updater.invalidupdater == True:
        return

    try:
        bpy.app.handlers.scene_update_post.remove(
            updater_run_install_popup_handler)
    except:
        pass

    if "ignore" in updater.json and updater.json["ignore"] == True:
        return  # don't do popup if ignore pressed
    # elif type(updater.update_version) != type((0,0,0)):
    # 	# likely was from master or another branch, shouldn't trigger popup
    # 	updater.json_reset_restore()
    # 	return
    elif "version_text" in updater.json and "version" in updater.json[
            "version_text"]:
        version = updater.json["version_text"]["version"]
        ver_tuple = updater.version_tuple_from_text(version)

        if ver_tuple < updater.current_version:
            # user probably manually installed to get the up to date addon
            # in here. Clear out the update flag using this function
            if updater.verbose:
                print("{} updater: appears user updated, clearing flag".format( \
                    updater.addon))
            updater.json_reset_restore()
            return
    atr = addon_updater_install_popup.bl_idname.split(".")
    getattr(getattr(bpy.ops, atr[0]), atr[1])('INVOKE_DEFAULT')
def check_for_update_background():
    # in case of error importing updater
    if updater.invalidupdater == True:
        return

    global ran_background_check
    if ran_background_check == True:
        # Global var ensures check only happens once
        return
    elif updater.update_ready != None or updater.async_checking == True:
        # Check already happened
        # Used here to just avoid constant applying settings below
        return

    # apply the UI settings
    addon_prefs = bpy.context.preferences.addons.get(__package__, None)
    if not addon_prefs:
        return
    settings = addon_prefs.preferences
    updater.set_check_interval(enable=settings.auto_check_update,
                               months=settings.updater_intrval_months,
                               days=settings.updater_intrval_days,
                               hours=settings.updater_intrval_hours,
                               minutes=settings.updater_intrval_minutes
                               )  # optional, if auto_check_update

    # input is an optional callback function
    # this function should take a bool input, if true: update ready
    # if false, no update ready
    if updater.verbose:
        print("{} updater: Running background check for update".format( \
            updater.addon))
    updater.check_for_update_async(background_update_callback)
    ran_background_check = True
 def execute(self, context):
     # in case of error importing updater
     if updater.invalidupdater == True:
         return {'CANCELLED'}
     updater.ignore_update()
     self.report({"INFO"}, "Open addon preferences for updater options")
     return {'FINISHED'}
    def execute(self, context):

        # in case of error importing updater
        if updater.invalidupdater == True:
            return {'CANCELLED'}

        if updater.async_checking == True and updater.error == None:
            # Check already happened
            # Used here to just avoid constant applying settings below
            # Ignoring if error, to prevent being stuck on the error screen
            return {'CANCELLED'}

        # apply the UI settings
        settings = context.preferences.addons[__package__].preferences
        updater.set_check_interval(enable=settings.auto_check_update,
                                   months=settings.updater_intrval_months,
                                   days=settings.updater_intrval_days,
                                   hours=settings.updater_intrval_hours,
                                   minutes=settings.updater_intrval_minutes
                                   )  # optional, if auto_check_update

        # input is an optional callback function
        # this function should take a bool input, if true: update ready
        # if false, no update ready
        updater.check_for_update_now()

        return {'FINISHED'}
    def execute(self, context):

        # in case of error importing updater
        if updater.invalidupdater == True:
            return {'CANCELLED'}

        if updater.manual_only == True:
            bpy.ops.wm.url_open(url=updater.website)
        elif updater.update_ready == True:
            res = updater.run_update(force=False,
                                     callback=post_update_callback,
                                     clean=self.clean_install)
            # should return 0, if not something happened
            if updater.verbose:
                if res == 0:
                    print("Updater returned successful")
                else:
                    print("Updater returned " + str(res) + ", error occurred")
        elif updater.update_ready == None:
            (update_ready, version, link) = updater.check_for_update(now=True)

            # re-launch this dialog
            atr = addon_updater_install_popup.bl_idname.split(".")
            getattr(getattr(bpy.ops, atr[0]), atr[1])('INVOKE_DEFAULT')
        else:
            if updater.verbose: print("Doing nothing, not ready for update")
        return {'FINISHED'}
    def draw(self, context):
        layout = self.layout

        if updater.invalidupdater == True:
            layout.label("Updater error")
            return

        saved = updater.json
        if self.error != "":
            col = layout.column()
            col.scale_y = 0.7
            col.label("Error occurred, did not install", icon="ERROR")
            col.label(updater.error_msg, icon="BLANK1")
            rw = col.row()
            rw.scale_y = 2
            rw.operator("wm.url_open",
                        text="Click for manual download.",
                        icon="BLANK1").url = updater.website
        # manual download button here
        elif updater.auto_reload_post_update == False:
            # tell user to restart blender
            if "just_restored" in saved and saved["just_restored"] == True:
                col = layout.column()
                col.scale_y = 0.7
                col.label("Addon restored", icon="RECOVER_LAST")
                col.label("Restart blender to reload.", icon="BLANK1")
                updater.json_reset_restore()
            else:
                col = layout.column()
                col.scale_y = 0.7
                col.label("Addon successfully installed", icon="FILE_TICK")
                col.label("Restart blender to reload.", icon="BLANK1")

        else:
            # reload addon, but still recommend they restart blender
            if "just_restored" in saved and saved["just_restored"] == True:
                col = layout.column()
                col.scale_y = 0.7
                col.label("Addon restored", icon="RECOVER_LAST")
                col.label("Consider restarting blender to fully reload.",
                          icon="BLANK1")
                updater.json_reset_restore()
            else:
                col = layout.column()
                col.scale_y = 0.7
                col.label("Addon successfully installed", icon="FILE_TICK")
                col.label("Consider restarting blender to fully reload.",
                          icon="BLANK1")
def unregister():
    bpy.utils.unregister_class(addon_updater_install_popup)
    bpy.utils.unregister_class(addon_updater_check_now)
    bpy.utils.unregister_class(addon_updater_update_now)
    bpy.utils.unregister_class(addon_updater_update_target)
    bpy.utils.unregister_class(addon_updater_install_manually)
    bpy.utils.unregister_class(addon_updater_updated_successful)
    bpy.utils.unregister_class(addon_updater_restore_backup)
    bpy.utils.unregister_class(addon_updater_ignore)
    bpy.utils.unregister_class(addon_updater_end_background)

    # clear global vars since they may persist if not restarting blender
    updater.clear_state()  # clear internal vars, avoids reloading oddities

    global ran_autocheck_install_popup
    ran_autocheck_install_popup = False

    global ran_update_sucess_popup
    ran_update_sucess_popup = False

    global ran_background_check
    ran_background_check = False
    def execute(self, context):

        # in case of error importing updater
        if updater.invalidupdater == True:
            return {'CANCELLED'}

        if updater.manual_only == True:
            bpy.ops.wm.url_open(url=updater.website)
        if updater.update_ready == True:
            # if it fails, offer to open the website instead
            try:
                res = updater.run_update(force=False,
                                         callback=post_update_callback,
                                         clean=self.clean_install)

                # should return 0, if not something happened
                if updater.verbose:
                    if res == 0:
                        print("Updater returned successful")
                    else:
                        print("Updater returned " + str(res) +
                              ", error occurred")
            except:
                atr = addon_updater_install_manually.bl_idname.split(".")
                getattr(getattr(bpy.ops, atr[0]), atr[1])('INVOKE_DEFAULT')
        elif updater.update_ready == None:
            (update_ready, version, link) = updater.check_for_update(now=True)
            # re-launch this dialog
            atr = addon_updater_install_popup.bl_idname.split(".")
            getattr(getattr(bpy.ops, atr[0]), atr[1])('INVOKE_DEFAULT')

        elif updater.update_ready == False:
            self.report({'INFO'}, "Nothing to update")
        else:
            self.report({'ERROR'},
                        "Encountered problem while trying to update")

        return {'FINISHED'}
def check_for_update_nonthreaded(self, context):
    # in case of error importing updater
    if updater.invalidupdater == True:
        return

    # only check if it's ready, ie after the time interval specified
    # should be the async wrapper call here

    settings = context.preferences.addons[__package__].preferences
    updater.set_check_interval(enable=settings.auto_check_update,
                               months=settings.updater_intrval_months,
                               days=settings.updater_intrval_days,
                               hours=settings.updater_intrval_hours,
                               minutes=settings.updater_intrval_minutes
                               )  # optional, if auto_check_update

    (update_ready, version, link) = updater.check_for_update(now=False)
    if update_ready == True:
        atr = addon_updater_install_popup.bl_idname.split(".")
        getattr(getattr(bpy.ops, atr[0]), atr[1])('INVOKE_DEFAULT')
    else:
        if updater.verbose: print("No update ready")
        self.report({'INFO'}, "No update ready")
def showReloadPopup():
    # in case of error importing updater
    if updater.invalidupdater == True:
        return

    saved_state = updater.json
    global ran_update_sucess_popup

    a = saved_state != None
    b = "just_updated" in saved_state
    c = saved_state["just_updated"]

    if a and b and c:
        updater.json_reset_postupdate()  # so this only runs once

        # no handlers in this case
        if updater.auto_reload_post_update == False: return

        if updater_run_success_popup_handler not in \
                bpy.app.handlers.scene_update_post \
                and ran_update_sucess_popup == False:
            bpy.app.handlers.scene_update_post.append(
                updater_run_success_popup_handler)
            ran_update_sucess_popup = True
    def execute(self, context):

        # in case of error importing updater
        if updater.invalidupdater == True:
            return {'CANCELLED'}

        res = updater.run_update(force=False,
                                 revert_tag=self.target,
                                 callback=post_update_callback,
                                 clean=self.clean_install)

        # should return 0, if not something happened
        if updater.verbose:
            if res == 0:
                print("Updater returned successful")
            else:
                print("Updater returned " + str(res) + ", error occurred")
            return {'CANCELLED'}

        return {'FINISHED'}
 def execute(self, context):
     # in case of error importing updater
     if updater.invalidupdater == True:
         return {'CANCELLED'}
     updater.stop_async_check_update()
     return {'FINISHED'}
 def execute(self, context):
     # in case of error importing updater
     if updater.invalidupdater == True:
         return {'CANCELLED'}
     updater.restore_backup()
     return {'FINISHED'}