Fix patch on android

Create own unzip method since zipfile is no longer reliable.
This commit is contained in:
angelblue05 2018-09-09 22:53:20 -05:00
parent 8dad5bdd74
commit 419afcce3c
4 changed files with 80 additions and 32 deletions

View file

@ -6,17 +6,14 @@ import json
import logging import logging
import Queue import Queue
import threading import threading
import urllib
import shutil
import os import os
import zipfile
import xbmc import xbmc
import xbmcvfs import xbmcvfs
from libraries import requests from libraries import requests
from helper.utils import should_stop, delete_folder from helper.utils import should_stop, delete_folder
from helper import settings, stop, event, window, kodi_version from helper import settings, stop, event, window, kodi_version, unzip
from emby import Emby from emby import Emby
from emby.core import api from emby.core import api
from emby.core.exceptions import HTTPException from emby.core.exceptions import HTTPException
@ -300,34 +297,24 @@ def get_objects(src, filename):
''' Download objects dependency to temp cache folder. ''' Download objects dependency to temp cache folder.
''' '''
temp = xbmc.translatePath('special://temp/emby').decode('utf-8') temp = xbmc.translatePath('special://temp/emby').decode('utf-8')
final = os.path.join(temp, "objects") restart = not xbmcvfs.exists(os.path.join(temp, "objects") + '/')
restart = not xbmcvfs.exists(final + '/') # add slash for verification
delete_folder()
LOG.info(src)
path = os.path.join(temp, filename) path = os.path.join(temp, filename)
try:
response = requests.get(src, stream=True, verify=False)
response.raise_for_status()
except Exception as error:
raise
else:
dl = xbmcvfs.File(path, 'w')
dl.write(response.content)
dl.close()
del response
with zipfile.ZipFile(path) as zf: if not xbmcvfs.exists(path):
zf.extractall(temp) delete_folder()
dirs, files = xbmcvfs.listdir('zip://%s' % urllib.quote_plus(path)) LOG.info("From %s to %s", src, path)
extracted = os.path.join(temp, dirs[0]) try:
response = requests.get(src, stream=True, verify=False)
response.raise_for_status()
except Exception as error:
raise
else:
dl = xbmcvfs.File(path, 'w')
dl.write(response.content)
dl.close()
del response
try: unzip(path, temp, "objects")
shutil.copytree(src=os.path.join(extracted, "objects"), dst=final)
delete_folder(extracted)
return restart return restart
except Exception as error:
raise

View file

@ -153,7 +153,8 @@ class Service(xbmc.Monitor):
return return
if get_objects(zipfile, label + '.zip'): # no patch applied previously if get_objects(zipfile, label + '.zip'): # no patch applied previously
dialog("ok", heading="{emby}", line1=_(33135)) LOG.info("No previous patch found.")
dialog("ok", heading="{emby}", line1=_(33135))
xbmc.executebuiltin('RestartApp') xbmc.executebuiltin('RestartApp')
else: else:
dialog("notification", heading="{emby}", message=_(33156), icon="{emby}") dialog("notification", heading="{emby}", message=_(33156), icon="{emby}")

View file

@ -14,6 +14,7 @@ from utils import JSONRPC
from utils import indent from utils import indent
from utils import write_xml from utils import write_xml
from utils import compare_version from utils import compare_version
from utils import unzip
from wrapper import progress from wrapper import progress
from wrapper import catch from wrapper import catch

View file

@ -7,6 +7,7 @@ import logging
import os import os
import re import re
import unicodedata import unicodedata
import urllib
from uuid import uuid4 from uuid import uuid4
import xbmc import xbmc
@ -277,7 +278,6 @@ def delete_recursive(path, dirs):
''' Delete files and dirs recursively. ''' Delete files and dirs recursively.
''' '''
for directory in dirs: for directory in dirs:
dirs2, files = xbmcvfs.listdir(os.path.join(path, directory.decode('utf-8'))) dirs2, files = xbmcvfs.listdir(os.path.join(path, directory.decode('utf-8')))
for file in files: for file in files:
@ -286,6 +286,65 @@ def delete_recursive(path, dirs):
delete_recursive(os.path.join(path, directory.decode('utf-8')), dirs2) delete_recursive(os.path.join(path, directory.decode('utf-8')), dirs2)
xbmcvfs.rmdir(os.path.join(path, directory.decode('utf-8'))) xbmcvfs.rmdir(os.path.join(path, directory.decode('utf-8')))
def unzip(path, dest, folder=None):
''' Unzip file. zipfile module seems to fail on android.
'''
path = urllib.quote_plus(path)
root = "zip://" + path + '/'
if folder:
xbmcvfs.mkdir(os.path.join(dest, folder))
dest = os.path.join(dest, folder)
root = get_zip_directory(root, folder)
dirs, files = xbmcvfs.listdir(root)
if dirs:
unzip_recursive(root, dirs, dest)
for file in files:
unzip_file(os.path.join(root, file.decode('utf-8')), os.path.join(dest, file.decode('utf-8')))
LOG.info("Unzipped %s", path)
def unzip_recursive(path, dirs, dest):
for directory in dirs:
dirs_dir = os.path.join(path, directory.decode('utf-8'))
dest_dir = os.path.join(dest, directory.decode('utf-8'))
xbmcvfs.mkdir(dest_dir)
LOG.info("unzip: %s to %s", dirs_dir, dest_dir)
dirs2, files = xbmcvfs.listdir(dirs_dir)
if dirs2:
unzip_recursive(dirs_dir, dirs2, dest_dir)
for file in files:
unzip_file(os.path.join(dirs_dir, file.decode('utf-8')), os.path.join(dest_dir, file.decode('utf-8')))
def unzip_file(path, dest):
''' Unzip specific file. Path should start with zip://
'''
xbmcvfs.copy(path, dest)
LOG.info("unzip: %s to %s", path, dest)
def get_zip_directory(path, folder):
dirs, files = xbmcvfs.listdir(path)
if folder in dirs:
return os.path.join(path, folder)
for directory in dirs:
result = get_zip_directory(os.path.join(path, directory.decode('utf-8')), folder)
if result:
return result
def normalize_string(text): def normalize_string(text):
''' For theme media, do not modify unless ''' For theme media, do not modify unless