def extract_archive(path, to_directory='.', merge_single=True): path = os.path.abspath(path) logger.debug("Extracting %s to %s", path, to_directory) if path.endswith('.zip'): opener, mode = zipfile.ZipFile, 'r' elif path.endswith('.tar.gz') or path.endswith('.tgz'): opener, mode = tarfile.open, 'r:gz' elif path.endswith('.gz'): decompress_gz(path, to_directory) return elif path.endswith('.tar.bz2') or path.endswith('.tbz'): opener, mode = tarfile.open, 'r:bz2' else: raise RuntimeError( "Could not extract `%s` as no appropriate extractor is found" % path) destination = os.path.join(to_directory, ".lutris_extracted") handler = opener(path, mode) handler.extractall(destination) handler.close() if merge_single: extracted = os.listdir(destination) if len(extracted) == 1: destination = os.path.join(destination, extracted[0]) for f in os.listdir(destination): source = os.path.join(destination, f) destination = os.path.join(to_directory, f) merge_folders(source, destination) logger.debug("Finished extracting %s", path)
def extract_archive(path, to_directory='.', merge_single=True, extractor=None): path = os.path.abspath(path) mode = None logger.debug("Extracting %s to %s", path, to_directory) if path.endswith('.tar.gz') or path.endswith('.tgz') or extractor == 'tgz': opener, mode = tarfile.open, 'r:gz' elif path.endswith('.tar.xz') or path.endswith( '.txz') or extractor == 'txz': opener, mode = tarfile.open, 'r:xz' elif path.endswith('.tar') or extractor == 'tar': opener, mode = tarfile.open, 'r:' elif path.endswith('.gz') or extractor == 'gzip': decompress_gz(path, to_directory) return elif (path.endswith('.tar.bz2') or path.endswith('.tbz') or extractor == 'bz2'): opener, mode = tarfile.open, 'r:bz2' elif is_7zip_supported(path, extractor): opener = '7zip' else: raise RuntimeError( "Could not extract `%s` as no appropriate extractor is found" % path) temp_name = ".extract-" + str(uuid.uuid4())[:8] temp_path = temp_dir = os.path.join(to_directory, temp_name) _do_extract(path, temp_path, opener, mode, extractor) if merge_single: extracted = os.listdir(temp_path) if len(extracted) == 1: temp_path = os.path.join(temp_path, extracted[0]) if os.path.isfile(temp_path): destination_path = os.path.join(to_directory, extracted[0]) if os.path.isfile(destination_path): logger.warning("Overwrite existing file %s", destination_path) os.remove(destination_path) shutil.move(temp_path, to_directory) os.removedirs(temp_dir) else: for archive_file in os.listdir(temp_path): source_path = os.path.join(temp_path, archive_file) destination_path = os.path.join(to_directory, archive_file) logger.debug("Moving extracted files from %s to %s", source_path, destination_path) if os.path.exists(destination_path): logger.warning("Overwrite existing path %s", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): system.merge_folders(source_path, destination_path) else: shutil.move(source_path, destination_path) system.remove_folder(temp_dir) logger.debug("Finished extracting %s", path) return path, to_directory
def extract_archive(path, to_directory=".", merge_single=True, extractor=None): path = os.path.abspath(path) logger.debug("Extracting %s to %s", path, to_directory) if extractor is None: extractor = guess_extractor(path) opener, mode = get_archive_opener(extractor, path) temp_name = ".extract-" + random_id() temp_path = temp_dir = os.path.join(to_directory, temp_name) try: _do_extract(path, temp_path, opener, mode, extractor) except (OSError, zlib.error, tarfile.ReadError, EOFError) as ex: logger.error("Extraction failed: %s", ex) raise ExtractFailure(str(ex)) if merge_single: extracted = os.listdir(temp_path) if len(extracted) == 1: temp_path = os.path.join(temp_path, extracted[0]) if os.path.isfile(temp_path): destination_path = os.path.join(to_directory, extracted[0]) if os.path.isfile(destination_path): logger.warning("Overwrite existing file %s", destination_path) os.remove(destination_path) if os.path.isdir(destination_path): os.rename(destination_path, destination_path + random_id()) shutil.move(temp_path, to_directory) os.removedirs(temp_dir) else: for archive_file in os.listdir(temp_path): source_path = os.path.join(temp_path, archive_file) destination_path = os.path.join(to_directory, archive_file) # logger.debug("Moving extracted files from %s to %s", source_path, destination_path) if system.path_exists(destination_path): logger.warning("Overwrite existing path %s", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): try: system.merge_folders(source_path, destination_path) except OSError as ex: logger.error( "Failed to merge to destination %s: %s", destination_path, ex, ) raise ExtractFailure(str(ex)) else: shutil.move(source_path, destination_path) system.remove_folder(temp_dir) logger.debug("Finished extracting %s to %s", path, to_directory) return path, to_directory
def extract_archive(path, to_directory='.', merge_single=True, extractor=None): path = os.path.abspath(path) mode = None logger.debug("Extracting %s to %s", path, to_directory) if path.endswith('.tar.gz') or path.endswith('.tgz') or extractor == 'tgz': opener, mode = tarfile.open, 'r:gz' elif path.endswith('.tar.xz') or path.endswith('.txz') or extractor == 'txz': opener, mode = tarfile.open, 'r:xz' elif path.endswith('.gz') or extractor == 'gzip': decompress_gz(path, to_directory) return elif(path.endswith('.tar.bz2') or path.endswith('.tbz') or extractor == 'bz2'): opener, mode = tarfile.open, 'r:bz2' elif(is_7zip_supported(path, extractor)): opener = '7zip' else: raise RuntimeError( "Could not extract `%s` as no appropriate extractor is found" % path ) temp_name = ".extract-" + str(uuid.uuid4())[:8] temp_path = temp_dir = os.path.join(to_directory, temp_name) _do_extract(path, temp_path, opener, mode, extractor) if merge_single: extracted = os.listdir(temp_path) if len(extracted) == 1: temp_path = os.path.join(temp_path, extracted[0]) if os.path.isfile(temp_path): destination_path = os.path.join(to_directory, extracted[0]) if os.path.isfile(destination_path): logger.warning("Overwrite existing file %s", destination_path) os.remove(destination_path) shutil.move(temp_path, to_directory) os.removedirs(temp_dir) else: for f in os.listdir(temp_path): logger.debug("Moving element %s of archive", f) source_path = os.path.join(temp_path, f) destination_path = os.path.join(to_directory, f) if os.path.exists(destination_path): logger.warning("Overwrite existing path %s", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): system.merge_folders(source_path, destination_path) else: shutil.move(source_path, destination_path) shutil.rmtree(temp_dir) logger.debug("Finished extracting %s", path) return (path, to_directory)
def on_runner_installed(*args): config_path = system.create_folder('~/.vice') lib_dir = os.path.join(settings.RUNNER_DIR, 'vice/lib/vice') if not os.path.exists(lib_dir): lib_dir = os.path.join(settings.RUNNER_DIR, 'vice/lib64/vice') if not os.path.exists(lib_dir): logger.error('Missing lib folder in the Vice runner') else: system.merge_folders(lib_dir, config_path) if callback: callback()
def save_to_cache(source, destination): """Copy a file or folder to the cache""" if os.path.dirname(source) == destination: logger.info("File is already cached in %s, skipping", destination) return if os.path.isdir(source): # Copy folder recursively merge_folders(source, destination) else: shutil.copy(source, destination) logger.debug("Copied %s to cache %s", source, destination)
def extract_archive(path, to_directory='.', merge_single=True, extractor=None): path = os.path.abspath(path) logger.debug("Extracting %s to %s", path, to_directory) if path.endswith('.zip') or extractor == 'zip': opener, mode = zipfile.ZipFile, 'r' elif(path.endswith('.tar.gz') or path.endswith('.tgz') or extractor == 'tgz'): opener, mode = tarfile.open, 'r:gz' elif path.endswith('.gz') or extractor == 'gzip': decompress_gz(path, to_directory) return elif(path.endswith('.tar.bz2') or path.endswith('.tbz') or extractor == 'bz2'): opener, mode = tarfile.open, 'r:bz2' else: raise RuntimeError( "Could not extract `%s` as no appropriate extractor is found" % path) temp_path = temp_dir = os.path.join(to_directory, "temp_lutris_extract") handler = opener(path, mode) handler.extractall(temp_path) handler.close() if merge_single: extracted = os.listdir(temp_path) if len(extracted) == 1: temp_path = os.path.join(temp_path, extracted[0]) if os.path.isfile(temp_path): destination_path = os.path.join(to_directory, extracted[0]) if os.path.isfile(destination_path): logger.warning("Overwrite existing file %s", destination_path) os.remove(destination_path) shutil.move(temp_path, to_directory) os.removedirs(temp_dir) else: for f in os.listdir(temp_path): logger.debug("Moving element %s of archive", f) source_path = os.path.join(temp_path, f) destination_path = os.path.join(to_directory, f) if os.path.exists(destination_path): logger.warning("Overwrite existing path %s", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): merge_folders(source_path, destination_path) else: shutil.move(source_path, destination_path) shutil.rmtree(temp_dir) logger.debug("Finished extracting %s", path) return (path, to_directory)
def save_to_cache(source, destination): """Copy a file or folder to the cache""" if not source: raise ValueError("Missing source") if os.path.dirname(source) == destination: logger.info("Skipping caching of %s, already cached in %s", source, destination) return if os.path.isdir(source): # Copy folder recursively merge_folders(source, destination) else: shutil.copy(source, destination) logger.debug("Cached %s to %s", source, destination)
def extract_archive(path, to_directory='.', merge_single=True, extractor=None): path = os.path.abspath(path) logger.debug("Extracting %s to %s", path, to_directory) if path.endswith('.zip') or extractor == 'zip': opener, mode = zipfile.ZipFile, 'r' elif (path.endswith('.tar.gz') or path.endswith('.tgz') or extractor == 'tgz'): opener, mode = tarfile.open, 'r:gz' elif path.endswith('.gz') or extractor == 'gzip': decompress_gz(path, to_directory) return elif (path.endswith('.tar.bz2') or path.endswith('.tbz') or extractor == 'bz2'): opener, mode = tarfile.open, 'r:bz2' else: raise RuntimeError( "Could not extract `%s` as no appropriate extractor is found" % path) destination = temp_dir = os.path.join(to_directory, ".lutris_extracted") handler = opener(path, mode) handler.extractall(destination) handler.close() if merge_single: extracted = os.listdir(destination) if len(extracted) == 1: destination = os.path.join(destination, extracted[0]) if os.path.isfile(destination): shutil.move(destination, to_directory) os.removedirs(temp_dir) else: for f in os.listdir(destination): logger.debug("Moving element %s of archive", f) source_path = os.path.join(destination, f) destination_path = os.path.join(to_directory, f) if os.path.exists(destination_path): logger.warning("%s already exists", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): merge_folders(source_path, destination_path) else: shutil.move(source_path, destination_path) shutil.rmtree(destination) logger.debug("Finished extracting %s", path)
def merge(self, params): src, dst = self._get_move_paths(params) logger.debug("Merging %s into %s" % (src, dst)) if not os.path.exists(src): raise ScriptingError("Source does not exist: %s" % src, params) if not os.path.exists(dst): os.makedirs(dst) if os.path.isfile(src): # If single file, copy it and change reference in game file so it # can be used as executable. shutil.copy(src, dst) if params['src'] in self.game_files.keys(): self.game_files[params['src']] = os.path.join( dst, os.path.basename(src) ) return merge_folders(src, dst)
def merge(self, params): self._check_required_params(["src", "dst"], params, "merge") src, dst = self._get_move_paths(params) logger.debug("Merging %s into %s" % (src, dst)) if not os.path.exists(src): raise ScriptingError("Source does not exist: %s" % src, params) if not os.path.exists(dst): os.makedirs(dst) if os.path.isfile(src): # If single file, copy it and change reference in game file so it # can be used as executable. Skip copying if the source is the same # as destination. if os.path.dirname(src) != dst: shutil.copy(src, dst) if params["src"] in self.game_files.keys(): self.game_files[params["src"]] = os.path.join(dst, os.path.basename(src)) return system.merge_folders(src, dst)
def merge(self, params): src, dst = self._get_move_paths(params) logger.debug("Merging %s into %s" % (src, dst)) if not os.path.exists(src): raise ScriptingError("Source does not exist: %s" % src, params) if not os.path.exists(dst): os.makedirs(dst) if os.path.isfile(src): # If single file, copy it and change reference in game file so it # can be used as executable. Skip copying if the source is the same # as destination. if os.path.dirname(src) != dst: shutil.copy(src, dst) if params['src'] in self.game_files.keys(): self.game_files[params['src']] = os.path.join( dst, os.path.basename(src)) return system.merge_folders(src, dst)
def extract_archive(path, to_directory=".", merge_single=True, extractor=None): path = os.path.abspath(path) mode = None logger.debug("Extracting %s to %s", path, to_directory) if path.endswith(".tar.gz") or path.endswith(".tgz") or extractor == "tgz": opener, mode = tarfile.open, "r:gz" elif path.endswith(".tar.xz") or path.endswith( ".txz") or extractor == "txz": opener, mode = tarfile.open, "r:xz" elif path.endswith(".tar") or extractor == "tar": opener, mode = tarfile.open, "r:" elif path.endswith(".gz") or extractor == "gzip": decompress_gz(path, to_directory) return elif path.endswith(".tar.bz2") or path.endswith( ".tbz") or extractor == "bz2": opener, mode = tarfile.open, "r:bz2" elif is_7zip_supported(path, extractor): opener = "7zip" else: raise RuntimeError( "Could not extract `%s` as no appropriate extractor is found" % path) temp_name = ".extract-" + str(uuid.uuid4())[:8] temp_path = temp_dir = os.path.join(to_directory, temp_name) try: _do_extract(path, temp_path, opener, mode, extractor) except (OSError, zlib.error) as ex: logger.exception("Extraction failed: %s", ex) raise ExtractFailure(str(ex)) if merge_single: extracted = os.listdir(temp_path) if len(extracted) == 1: temp_path = os.path.join(temp_path, extracted[0]) if os.path.isfile(temp_path): destination_path = os.path.join(to_directory, extracted[0]) if os.path.isfile(destination_path): logger.warning("Overwrite existing file %s", destination_path) os.remove(destination_path) shutil.move(temp_path, to_directory) os.removedirs(temp_dir) else: for archive_file in os.listdir(temp_path): source_path = os.path.join(temp_path, archive_file) destination_path = os.path.join(to_directory, archive_file) # logger.debug("Moving extracted files from %s to %s", source_path, destination_path) if system.path_exists(destination_path): logger.warning("Overwrite existing path %s", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): try: system.merge_folders(source_path, destination_path) except OSError as ex: logger.error("Failed to merge to destination %s: %s", destination_path, ex) raise ExtractFailure(str(ex)) else: shutil.move(source_path, destination_path) system.remove_folder(temp_dir) logger.debug("Finished extracting %s to %s", path, to_directory) return path, to_directory
def extract_archive(path, to_directory=".", merge_single=True, extractor=None): # noqa: C901 # pylint: disable=too-many-branches,too-many-statements # TODO: split into multiple methods to reduce complexity (30) path = os.path.abspath(path) mode = None logger.debug("Extracting %s to %s", path, to_directory) if extractor is None: if path.endswith(".tar.gz") or path.endswith(".tgz"): extractor = "tgz" elif path.endswith(".tar.xz") or path.endswith(".txz"): extractor = "txz" elif path.endswith(".tar"): extractor = "tar" elif path.endswith(".tar.bz2") or path.endswith(".tbz"): extractor = "bz2" elif path.endswith(".gz"): extractor = "gzip" elif path.endswith(".exe"): extractor = "exe" elif is_7zip_supported(path, None): extractor = None else: raise RuntimeError("Could not extract `%s` - no appropriate extractor found" % path) if extractor == "tgz": opener, mode = tarfile.open, "r:gz" elif extractor == "txz": opener, mode = tarfile.open, "r:xz" elif extractor == "tar": opener, mode = tarfile.open, "r:" elif extractor == "bz2": opener, mode = tarfile.open, "r:bz2" elif extractor == "gzip": decompress_gz(path, to_directory) return elif extractor == "gog": opener = "innoextract" elif extractor == "exe": opener = "exe" elif extractor is None or is_7zip_supported(path, extractor): opener = "7zip" else: raise RuntimeError("Could not extract `%s` - unknown format specified" % path) temp_name = ".extract-" + random_id() temp_path = temp_dir = os.path.join(to_directory, temp_name) try: _do_extract(path, temp_path, opener, mode, extractor) except (OSError, zlib.error, tarfile.ReadError, EOFError) as ex: logger.error("Extraction failed: %s", ex) raise ExtractFailure(str(ex)) if merge_single: extracted = os.listdir(temp_path) if len(extracted) == 1: temp_path = os.path.join(temp_path, extracted[0]) if os.path.isfile(temp_path): destination_path = os.path.join(to_directory, extracted[0]) if os.path.isfile(destination_path): logger.warning("Overwrite existing file %s", destination_path) os.remove(destination_path) if os.path.isdir(destination_path): os.rename(destination_path, destination_path + random_id()) shutil.move(temp_path, to_directory) os.removedirs(temp_dir) else: for archive_file in os.listdir(temp_path): source_path = os.path.join(temp_path, archive_file) destination_path = os.path.join(to_directory, archive_file) # logger.debug("Moving extracted files from %s to %s", source_path, destination_path) if system.path_exists(destination_path): logger.warning("Overwrite existing path %s", destination_path) if os.path.isfile(destination_path): os.remove(destination_path) shutil.move(source_path, destination_path) elif os.path.isdir(destination_path): try: system.merge_folders(source_path, destination_path) except OSError as ex: logger.error( "Failed to merge to destination %s: %s", destination_path, ex, ) raise ExtractFailure(str(ex)) else: shutil.move(source_path, destination_path) system.remove_folder(temp_dir) logger.debug("Finished extracting %s to %s", path, to_directory) return path, to_directory