# Path: resources/lib/model/update.py
# class Update:
#     def __init__(self, current_version=None, update_version=None, asset_url=None, asset_name=None, changelog=None,
#                  file_path=None):
#         self.current_version = current_version,
#         self.update_version = update_version,
#         self.asset_url = asset_url,
#         self.asset_name = asset_name,
#         self.changelog = changelog
#         self.file_path = file_path
# 
# Path: resources/lib/views/updateinfo.py
# class UpdateInfo(pyxbmct.AddonDialogWindow):
#     def __init__(self, controller, update, title=''):
#         super(UpdateInfo, self).__init__(title)
#         self.controller = controller
#         self.update = update
#         background = None
#         if self.controller.get_active_skin() == 'skin.osmc':
#             media_path = '/usr/share/kodi/addons/skin.osmc/media'
#             if os.path.exists(media_path):
#                 background = os.path.join(media_path, 'dialogs/DialogBackground_old.png')
# 
#         if background is not None:
#             self.background.setImage(background)
#             self.removeControl(self.title_background)
#             self.removeControl(self.window_close_button)
#             self.removeControl(self.title_bar)
# 
#         # init controls
#         self.version = None
#         self.changelog = None
#         self.button_update = None
#         self.button_cancel = None
# 
#         self.setGeometry(1280, 720, 12, 6, padding=60)
#         self.set_info_controls(update)
#         self.set_active_controls()
#         self.set_navigation()
#         self.connect(pyxbmct.ACTION_NAV_BACK, self.close)
# 
#     def set_info_controls(self, update):
#         update_headline = 'Luna %s available' % update.update_version
#         title_label = pyxbmct.Label(update_headline, alignment=pyxbmct.ALIGN_LEFT, font='XLarge', textColor=COLOR_HEADING)
#         self.placeControl(title_label, 0, 0, 2, 3)
# 
#         changelog_label = pyxbmct.Label('Changelog', alignment=pyxbmct.ALIGN_LEFT, font='Med', textColor=COLOR_DETAILS)
#         self.placeControl(changelog_label, 2, 0)
# 
#         self.changelog = pyxbmct.TextBox(font='Med')
#         self.placeControl(self.changelog, 4, 0, 6, 6)
#         self.changelog.setText(update.changelog)
#         self.changelog.autoScroll(delay=5000, time=2000, repeat=10000)
# 
#     def set_active_controls(self):
#         self.button_update = pyxbmct.Button('Update', focusTexture='', noFocusTexture='', focusedColor=COLOR_FO,
#                                             textColor=COLOR_NF, font='Med', alignment=pyxbmct.ALIGN_LEFT)
#         self.placeControl(self.button_update, 11, 0)
#         self.connect(self.button_update, self.do_update)
# 
#         self.button_cancel = pyxbmct.Button('Cancel', focusTexture='', noFocusTexture='',
#                                             focusedColor=COLOR_FO, textColor=COLOR_NF, font='Med',
#                                             alignment=pyxbmct.ALIGN_LEFT)
#         self.placeControl(self.button_cancel, 11, 1, columnspan=2)
#         self.connect(self.button_cancel, self.cancel)
# 
#     def set_navigation(self):
#         self.button_update.controlRight(self.button_cancel)
#         self.button_update.controlLeft(self.button_cancel)
#         self.button_cancel.controlRight(self.button_update)
#         self.button_cancel.controlLeft(self.button_update)
# 
#         self.setFocus(self.button_update)
# 
#     def do_update(self):
#         self.controller.do_update(self.update)
#         self.close()
# 
#     def cancel(self):
#         self.close()
# 
#     def setAnimation(self, control):
#         control.setAnimations(
#             [
#                 ('WindowOpen', 'effect=fade start=0 end=100 time=500',),
#                 ('WindowClose', 'effect=fade start=100 end=0 time=500',)
#             ]
#         )


import json
import os
import re
import urllib2
import zipfile
import xbmc
import xbmcaddon
import xbmcgui
from resources.lib.model.update import Update
from resources.lib.views.updateinfo import UpdateInfo
        file_path = update.file_path
        with open(file_path, 'wb') as asset:
            asset.write(urllib2.urlopen(update.asset_url).read())
            asset.close()
        zip_file = zipfile.ZipFile(file_path)
        zip_file.extractall(self.core.internal_path, self._get_members(zip_file))

        xbmcgui.Dialog().ok(
            self.core.string('name'),
            'Luna has been updated to version %s and will now relaunch.' % update.update_version
        )

        xbmc.executebuiltin('RunPlugin(\'script.luna\')')

    def _get_members(self, zip_file):
        parts = []
        for name in zip_file.namelist():
            if not name.endswith('/'):
                parts.append(name.split('/')[:-1])
        prefix = os.path.commonprefix(parts) or ''
        if prefix:
            prefix = '/'.join(prefix) + '/'
        offset = len(prefix)
        for zipinfo in zip_file.infolist():
            name = zipinfo.filename
            if len(name) > offset:
                zipinfo.filename = name[offset:]
                yield zipinfo

    def parse_release_information(self, release):

# Path: resources/lib/model/update.py
# class Update:
#     def __init__(self, current_version=None, update_version=None, asset_url=None, asset_name=None, changelog=None,
#                  file_path=None):
#         self.current_version = current_version,
#         self.update_version = update_version,
#         self.asset_url = asset_url,
#         self.asset_name = asset_name,
#         self.changelog = changelog
#         self.file_path = file_path
# 
# Path: resources/lib/views/updateinfo.py
# class UpdateInfo(pyxbmct.AddonDialogWindow):
#     def __init__(self, controller, update, title=''):
#         super(UpdateInfo, self).__init__(title)
#         self.controller = controller
#         self.update = update
#         background = None
#         if self.controller.get_active_skin() == 'skin.osmc':
#             media_path = '/usr/share/kodi/addons/skin.osmc/media'
#             if os.path.exists(media_path):
#                 background = os.path.join(media_path, 'dialogs/DialogBackground_old.png')
# 
#         if background is not None:
#             self.background.setImage(background)
#             self.removeControl(self.title_background)
#             self.removeControl(self.window_close_button)
#             self.removeControl(self.title_bar)
# 
#         # init controls
#         self.version = None
#         self.changelog = None
#         self.button_update = None
#         self.button_cancel = None
# 
#         self.setGeometry(1280, 720, 12, 6, padding=60)
#         self.set_info_controls(update)
#         self.set_active_controls()
#         self.set_navigation()
#         self.connect(pyxbmct.ACTION_NAV_BACK, self.close)
# 
#     def set_info_controls(self, update):
#         update_headline = 'Luna %s available' % update.update_version
#         title_label = pyxbmct.Label(update_headline, alignment=pyxbmct.ALIGN_LEFT, font='XLarge', textColor=COLOR_HEADING)
#         self.placeControl(title_label, 0, 0, 2, 3)
# 
#         changelog_label = pyxbmct.Label('Changelog', alignment=pyxbmct.ALIGN_LEFT, font='Med', textColor=COLOR_DETAILS)
#         self.placeControl(changelog_label, 2, 0)
# 
#         self.changelog = pyxbmct.TextBox(font='Med')
#         self.placeControl(self.changelog, 4, 0, 6, 6)
#         self.changelog.setText(update.changelog)
#         self.changelog.autoScroll(delay=5000, time=2000, repeat=10000)
# 
#     def set_active_controls(self):
#         self.button_update = pyxbmct.Button('Update', focusTexture='', noFocusTexture='', focusedColor=COLOR_FO,
#                                             textColor=COLOR_NF, font='Med', alignment=pyxbmct.ALIGN_LEFT)
#         self.placeControl(self.button_update, 11, 0)
#         self.connect(self.button_update, self.do_update)
# 
#         self.button_cancel = pyxbmct.Button('Cancel', focusTexture='', noFocusTexture='',
#                                             focusedColor=COLOR_FO, textColor=COLOR_NF, font='Med',
#                                             alignment=pyxbmct.ALIGN_LEFT)
#         self.placeControl(self.button_cancel, 11, 1, columnspan=2)
#         self.connect(self.button_cancel, self.cancel)
# 
#     def set_navigation(self):
#         self.button_update.controlRight(self.button_cancel)
#         self.button_update.controlLeft(self.button_cancel)
#         self.button_cancel.controlRight(self.button_update)
#         self.button_cancel.controlLeft(self.button_update)
# 
#         self.setFocus(self.button_update)
# 
#     def do_update(self):
#         self.controller.do_update(self.update)
#         self.close()
# 
#     def cancel(self):
#         self.close()
# 
#     def setAnimation(self, control):
#         control.setAnimations(
#             [
#                 ('WindowOpen', 'effect=fade start=0 end=100 time=500',),
#                 ('WindowClose', 'effect=fade start=100 end=0 time=500',)
#             ]
#         )


# Path: resources/lib/core/updateservice.py
import json
import os
import re
import urllib2
import zipfile
import xbmc
import xbmcaddon
import xbmcgui
from resources.lib.model.update import Update
from resources.lib.views.updateinfo import UpdateInfo

        file_path = update.file_path
        with open(file_path, 'wb') as asset:
            asset.write(urllib2.urlopen(update.asset_url).read())
            asset.close()
        zip_file = zipfile.ZipFile(file_path)
        zip_file.extractall(self.core.internal_path, self._get_members(zip_file))

        xbmcgui.Dialog().ok(
            self.core.string('name'),
            'Luna has been updated to version %s and will now relaunch.' % update.update_version
        )

        xbmc.executebuiltin('RunPlugin(\'script.luna\')')

    def _get_members(self, zip_file):
        parts = []
        for name in zip_file.namelist():
            if not name.endswith('/'):
                parts.append(name.split('/')[:-1])
        prefix = os.path.commonprefix(parts) or ''
        if prefix:
            prefix = '/'.join(prefix) + '/'
        offset = len(prefix)
        for zipinfo in zip_file.infolist():
            name = zipinfo.filename
            if len(name) > offset:
                zipinfo.filename = name[offset:]
                yield zipinfo

    def parse_release_information(self, release):

        update = Update()