def doUpdate(LocalDir, REMOTE_PATH, Title, localFileName): try: from urllib2 import urlopen f = urlopen(REMOTE_PATH) # Open our local file for writing with open(localFileName, "wb") as local_file: local_file.write(f.read()) updateFile = zipfile.ZipFile(localFileName) removeFilesNotInRepo(updateFile, LocalDir) for index, n in enumerate(updateFile.namelist()): if n[-1] != "/": dest = os.path.join(LocalDir, "/".join(n.split("/")[1:])) destdir = os.path.dirname(dest) if not os.path.isdir(destdir): os.makedirs(destdir) data = updateFile.read(n) if os.path.exists(dest): os.remove(dest) f = open(dest, 'wb') f.write(data) f.close() updateFile.close() os.remove(localFileName) xbmc.executebuiltin("XBMC.UpdateLocalAddons()") return True except: log("DevUpdate not possible due download error") return False
def doUpdate(LocalDir, REMOTE_PATH, Title, localFileName, auth): try: response = requests.get(REMOTE_PATH, auth=auth) # verify=False, # Open our local file for writing # with open(localFileName,"wb") as local_file: # local_file.write(f.read()) if response.status_code == 200: open(localFileName, "wb").write(response.content) else: return False updateFile = zipfile.ZipFile(localFileName) removeFilesNotInRepo(updateFile, LocalDir) for index, n in enumerate(updateFile.namelist()): if n[-1] != "/": dest = os.path.join(LocalDir, "/".join(n.split("/")[1:])) destdir = os.path.dirname(dest) if not os.path.isdir(destdir): os.makedirs(destdir) data = updateFile.read(n) if os.path.exists(dest): os.remove(dest) f = open(dest, 'wb') f.write(data) f.close() updateFile.close() os.remove(localFileName) xbmc.executebuiltin("XBMC.UpdateLocalAddons()") return True except: xbmc.log("do Update not possible due download error") return False
def extract_zip_dp(zip_path, extract_path, zip_pwd=None, save_list=[]): dp = xbmcgui.DialogProgress() dp.create('EXTRACT ZIP', 'Unpacking data !', 'Please wait ...') dp.update(0) zip_path = os.path.abspath(zip_path) extract_path = os.path.abspath(extract_path) zip = zipfile.ZipFile(zip_path, mode='r', compression=zipfile.ZIP_STORED, allowZip64=True) count = int(0) list_len = len(zip.infolist()) for item in zip.infolist(): count += 1 try: percent = min(count * 100 / list_len, 100) dp.update(percent) except: pass if not any((x in item.filename for x in save_list)): try: zip.extract(item, path=extract_path, pwd=zip_pwd) except: pass if dp.iscanceled(): break zip.close() dp.close()
def zipfolder(foldername, target_dir): zipobj = zipfile.ZipFile(foldername + '.zip', 'w', zipfile.ZIP_DEFLATED) rootlen = len(target_dir) + 1 for base, dirs, files in os.walk(target_dir): for file in files: fn = os.path.join(base, file) zipobj.write(fn, fn[rootlen:]) zipobj.close()
def check_zipfile(zip_file_path): if os.path.exists(zip_file_path): try: return zipfile.ZipFile(zip_file_path, mode='r', compression=zipfile.ZIP_STORED, allowZip64=True) except zipfile.BadZipfile: return 'No zip file !' else: return 'No zip file !'
def zip_dir_dp(dir_path, zip_save_path, new_archive_name, base_dir=True): dp = xbmcgui.DialogProgress() dp.create('CREATE ZIP', 'Create zip archive !', 'Please wait ...') dp.update(0) dir_path = os.path.abspath(dir_path) zip_save_path = os.path.abspath(zip_save_path) zip_full_save_path = os.path.join(zip_save_path, new_archive_name + '.zip') zip = zipfile.ZipFile(zip_full_save_path, mode='w', compression=zipfile.ZIP_STORED, allowZip64=True) count = int(0) files_list = [] files_list = list( os.walk(dir_path, topdown=False, onerror=None, followlinks=True)) list_len = sum(len(files) for path, dirs, files in files_list) root_len = len(dir_path) if base_dir == True: root_len = root_len - len(os.path.basename(dir_path)) for root, dirs, files in files_list: for file in files: path = os.path.join(root, file) archive_name = os.path.join(os.path.abspath(root)[root_len:], file) try: zip.write(path, archive_name, zipfile.ZIP_DEFLATED) except: pass count += 1 try: percent = min(count * 100 / list_len, 100) dp.update(percent) except: pass if dp.iscanceled(): break zip.close() dp.close()
def update(LocalDir, REMOTE_PATH, Title, localFileName): logger.info(Title + " from: " + REMOTE_PATH) cDownload().download(REMOTE_PATH, localFileName, False, Title) updateFile = zipfile.ZipFile(os.path.join(profilePath, localFileName)) removeFilesNotInRepo(updateFile, LocalDir) for index, n in enumerate(updateFile.namelist()): if n[-1] != "/": dest = os.path.join(LocalDir, "/".join(n.split("/")[1:])) destdir = os.path.dirname(dest) if not os.path.isdir(destdir): os.makedirs(destdir) data = updateFile.read(n) if os.path.exists(dest): os.remove(dest) f = open(dest, 'wb') f.write(data) f.close() updateFile.close() xbmc.executebuiltin("XBMC.UpdateLocalAddons()") logger.info("Update Successful")