def maybe_extract(self): if self.verify_files: tf = tarfile.open(self.selected_build.tar_name, 'r') utils.log("Starting extraction from tar file " + self.selected_build.tar_name) # Create the .update directory if necessary. if not os.path.exists(constants.UPDATE_DIR): utils.log("Creating {} directory".format(constants.UPDATE_DIR)) os.mkdir(constants.UPDATE_DIR) # Extract the update files from the tar file to the .update directory. tar_members = (m for m in tf.getmembers() if os.path.basename(m.name) in constants.UPDATE_FILES) for member in tar_members: ti = tf.extractfile(member) outfile = os.path.join(constants.UPDATE_DIR, os.path.basename(member.name)) try: with progress.FileProgress("Extracting", ti, outfile, ti.size, self.background) as extractor: extractor.start() utils.log("Extracted " + outfile) except script_exceptions.Canceled: utils.remove_update_files() sys.exit(0) except script_exceptions.WriteError as e: utils.write_error(outfile, str(e)) sys.exit(1) tf.close() else: # Just move the tar file to the update directory. os.rename(self.selected_build.tar_name, os.path.join(constants.UPDATE_DIR, self.selected_build.tar_name))
def download(self): import requests2 as requests tar_name = self.selected_build.tar_name filename = self.selected_build.filename utils.log("Download URL = " + self.selected_build.url) try: resp = requests.get(self.selected_build.url, stream=True) utils.log("Opened URL " + self.selected_build.url) bz2_size = int(resp.headers['Content-Length']) utils.log("Size of file = " + utils.size_fmt(bz2_size)) if (os.path.isfile(filename) and os.path.getsize(filename) == bz2_size): # Skip the download if the file exists with the correct size. utils.log("Skipping download") pass else: # Do the download utils.log("Starting download of " + self.selected_build.url) with progress.FileProgress("Downloading", resp.raw, filename, bz2_size, self.background) as downloader: downloader.start() utils.log("Completed download of " + self.selected_build.url) except script_exceptions.Canceled: sys.exit(0) except requests.RequestException as e: utils.url_error(self.selected_build.url, str(e)) sys.exit(1) except script_exceptions.WriteError as e: utils.write_error(os.path.join(__dir__, filename), str(e)) sys.exit(1) # Do the decompression if necessary. if self.selected_build.compressed and not os.path.isfile(tar_name): try: bf = open(filename, 'rb') utils.log("Starting decompression of " + filename) with progress.DecompressProgress("Decompressing", bf, tar_name, bz2_size, self.background) as decompressor: decompressor.start() utils.log("Completed decompression of " + filename) except script_exceptions.Canceled: sys.exit(0) except script_exceptions.WriteError as e: utils.write_error(os.path.join(__dir__, tar_name), str(e)) sys.exit(1) except script_exceptions.DecompressError as e: utils.decompress_error(os.path.join(__dir__, filename), str(e)) sys.exit(1)
def maybe_copy_to_archive(self): if self.archive and not xbmcvfs.exists(self.archive_tar_path): log.log("Archiving tar file to {}".format(self.archive_tar_path)) tar = open(self.temp_tar_path) size = os.path.getsize(self.temp_tar_path) try: with progress.FileProgress(L10n(32017), tar, self.archive_tar_path, size, self.background) as extractor: extractor.start() except script_exceptions.Canceled: log.log("Archive copy canceled") xbmcvfs.delete(self.archive_tar_path) except script_exceptions.WriteError as e: utils.write_error(self.archive_tar_path, str(e)) xbmcvfs.delete(self.archive_tar_path)
def maybe_verify(self): if not self.verify_files: return log.log("Verifying update file") with closing(tarfile.open(self.update_tar_path, 'r')) as tf: tar_names = tf.getnames() for update_image in libreelec.UPDATE_IMAGES: path_in_tar = next( name for name in tar_names if name.endswith(os.path.join('target', update_image))) ti = tf.extractfile(path_in_tar) temp_image_path = os.path.join(TEMP_PATH, update_image) try: with progress.FileProgress(L10n(32018), ti, temp_image_path, ti.size, self.background) as extractor: extractor.start() log.log("Extracted " + temp_image_path) except script_exceptions.Canceled: return except script_exceptions.WriteError as e: utils.write_error(temp_image_path, str(e)) return md5sum = tf.extractfile(path_in_tar + '.md5').read().split()[0] log.log("{}.md5 file = {}".format(update_image, md5sum)) if not progress.md5sum_verified(md5sum, temp_image_path, self.background): log.log("{} md5 mismatch!".format(update_image)) utils.ok( L10n(32019).format(update_image), self.selected_build.filename, L10n(32020).format(update_image), L10n(32021)) utils.remove_update_files() return else: log.log("{} md5 is correct".format(update_image)) funcs.remove_file(temp_image_path)
def maybe_copy_to_archive(self): if __addon__.getSetting('archive') == "true" and self.selected_build.archive is None: archive_dir = os.path.join(self.archive_root, self.source) archive_file = os.path.join(archive_dir, self.selected_build.tar_name) utils.log("Archiving tar file to {}".format(archive_file)) tarpath = os.path.join(__dir__, self.selected_build.tar_name) tar = open(tarpath) size = os.path.getsize(tarpath) try: with progress.FileProgress("Copying to archive", tar, archive_file, size, self.background) as extractor: extractor.start() except script_exceptions.Canceled: utils.log("Archive copy canceled") xbmcvfs.delete(archive_file) except script_exceptions.WriteError as e: utils.write_error(archive_file, str(e)) xbmcvfs.delete(archive_file)
def maybe_verify(self): if not self.verify_files: return log.log("Verifying update file") with closing(tarfile.open(self.update_tar_path, 'r')) as tf: tar_names = tf.getnames() for update_image in openelec.UPDATE_IMAGES: path_in_tar = next(name for name in tar_names if name.endswith(os.path.join('target', update_image))) ti = tf.extractfile(path_in_tar) temp_image_path = os.path.join(TEMP_PATH, update_image) try: with progress.FileProgress(L10n(32018), ti, temp_image_path, ti.size, self.background) as extractor: extractor.start() log.log("Extracted " + temp_image_path) except script_exceptions.Canceled: return except script_exceptions.WriteError as e: utils.write_error(temp_image_path, str(e)) return md5sum = tf.extractfile(path_in_tar + '.md5').read().split()[0] log.log("{}.md5 file = {}".format(update_image, md5sum)) if not progress.md5sum_verified(md5sum, temp_image_path, self.background): log.log("{} md5 mismatch!".format(update_image)) utils.ok(L10n(32019).format(update_image), self.selected_build.filename, L10n(32020).format(update_image), L10n(32021)) utils.remove_update_files() return else: log.log("{} md5 is correct".format(update_image)) funcs.remove_file(temp_image_path)
def maybe_download(self): try: remote_file = self.selected_build.remote_file() except requests.RequestException as e: utils.url_error(self.selected_build.url, str(e)) sys.exit(1) filename = self.selected_build.filename tar_name = self.selected_build.tar_name size = self.selected_build.size self.download_path = os.path.join(TEMP_PATH, filename) self.temp_tar_path = os.path.join(TEMP_PATH, tar_name) self.update_tar_path = os.path.join(libreelec.UPDATE_DIR, tar_name) if self.archive: self.archive_tar_path = os.path.join(self.archive_dir, tar_name) if not self.copy_from_archive(): if (os.path.isfile(self.download_path) and os.path.getsize(self.download_path) == size): # Skip the download if the file exists with the correct size. log.log("Skipping download") else: try: log.log("Starting download of {} to {}".format( self.selected_build.url, self.download_path)) with progress.FileProgress(L10n(32014), remote_file, self.download_path, size, self.background) as downloader: downloader.start() log.log("Completed download") except script_exceptions.Canceled: sys.exit(0) except requests.RequestException as e: utils.url_error(self.selected_build.url, str(e)) sys.exit(1) except script_exceptions.WriteError as e: utils.write_error(self.download_path, str(e)) sys.exit(1) if self.selected_build.compressed: try: bf = open(self.download_path, 'rb') log.log("Starting decompression of " + self.download_path) with progress.DecompressProgress( L10n(32015), bf, self.temp_tar_path, size, self.background) as decompressor: decompressor.start() log.log("Completed decompression") except script_exceptions.Canceled: sys.exit(0) except script_exceptions.WriteError as e: utils.write_error(self.temp_tar_path, str(e)) sys.exit(1) except script_exceptions.DecompressError as e: utils.decompress_error(self.download_path, str(e)) sys.exit(1) finally: funcs.remove_file(self.download_path) self.maybe_copy_to_archive() log.log("Moving tar file to " + self.update_tar_path) os.renames(self.temp_tar_path, self.update_tar_path) addon.set_setting('update_pending', 'true')
def maybe_download(self): try: remote_file = self.selected_build.remote_file() except requests.RequestException as e: utils.url_error(self.selected_build.url, str(e)) sys.exit(1) filename = self.selected_build.filename tar_name = self.selected_build.tar_name size = self.selected_build.size self.download_path = os.path.join(TEMP_PATH, filename) self.temp_tar_path = os.path.join(TEMP_PATH, tar_name) self.update_tar_path = os.path.join(openelec.UPDATE_DIR, tar_name) if self.archive: self.archive_tar_path = os.path.join(self.archive_dir, tar_name) if not self.copy_from_archive(): if (os.path.isfile(self.download_path) and os.path.getsize(self.download_path) == size): # Skip the download if the file exists with the correct size. log.log("Skipping download") else: try: log.log("Starting download of {} to {}".format(self.selected_build.url, self.download_path)) with progress.FileProgress(L10n(32014), remote_file, self.download_path, size, self.background) as downloader: downloader.start() log.log("Completed download") except script_exceptions.Canceled: sys.exit(0) except requests.RequestException as e: utils.url_error(self.selected_build.url, str(e)) sys.exit(1) except script_exceptions.WriteError as e: utils.write_error(self.download_path, str(e)) sys.exit(1) if self.selected_build.compressed: try: bf = open(self.download_path, 'rb') log.log("Starting decompression of " + self.download_path) with progress.DecompressProgress(L10n(32015), bf, self.temp_tar_path, size, self.background) as decompressor: decompressor.start() log.log("Completed decompression") except script_exceptions.Canceled: sys.exit(0) except script_exceptions.WriteError as e: utils.write_error(self.temp_tar_path, str(e)) sys.exit(1) except script_exceptions.DecompressError as e: utils.decompress_error(self.download_path, str(e)) sys.exit(1) finally: funcs.remove_file(self.download_path) self.maybe_copy_to_archive() log.log("Moving tar file to " + self.update_tar_path) os.renames(self.temp_tar_path, self.update_tar_path) addon.set_setting('update_pending', 'true')