def test_auto_delete(self): tmpdir = tempfile.mkdtemp() fname = tmpdir + "test.zip" shutil.copy(test_structure_zip, fname) reusables.extract(fname, path=tmpdir, delete_on_success=True) assert not os.path.exists(fname) shutil.rmtree(tmpdir)
def test_extract_zip(self): assert os.path.exists(test_structure_zip) reusables.extract(test_structure_zip, path=test_root, delete_on_success=False) assert os.path.exists(test_structure) assert os.path.isdir(test_structure) shutil.rmtree(test_structure)
def test_extract_empty(self): empt = tempfile.mktemp() open(empt, "a").close() try: reusables.extract(empt) assert False, "Should have failed" except OSError: assert True os.unlink(empt)
def test_extract_empty(self): empt = tempfile.mktemp() open(empt, "a").close() try: reusables.extract(empt) assert False, "Should have failed" except OSError: assert True os.unlink(empt)
def test_extract_rar(self): if reusables.win_based: import rarfile rarfile.UNRAR_TOOL = os.path.abspath(os.path.join(test_root, "UnRAR.exe")) assert os.path.exists(test_structure_rar) reusables.extract(test_structure_rar, path=test_root, delete_on_success=False, enable_rar=True) assert os.path.exists(test_structure) assert os.path.isdir(test_structure) shutil.rmtree(test_structure)
def test_extract_bad(self): self._extract_structure() path = os.path.join(test_structure, "Files", "file_1") assert os.path.exists(path) try: reusables.extract(path, path=test_root, delete_on_success=False) except TypeError: pass else: raise AssertionError("Extracted a bad file?")
def test_extract_bad(self): self._extract_structure() path = os.path.join(test_structure, "Files", "file_1") assert os.path.exists(path) try: reusables.extract(path, path=test_root, delete_on_success=False) except TypeError: pass else: raise AssertionError("Extracted a bad file?")
def test_extract_rar(self): if reusables.win_based: import rarfile rarfile.UNRAR_TOOL = os.path.abspath( os.path.join(test_root, "UnRAR.exe")) assert os.path.exists(test_structure_rar) reusables.extract(test_structure_rar, path=test_root, delete_on_success=False, enable_rar=True) assert os.path.exists(test_structure) assert os.path.isdir(test_structure) shutil.rmtree(test_structure)
def latest_ffmpeg(signal, stop_signal, **_): stop = False def stop_me(): nonlocal stop stop = True stop_signal.connect(stop_me) ffmpeg_folder = Path(user_data_dir("FFmpeg", appauthor=False, roaming=True)) ffmpeg_folder.mkdir(exist_ok=True) url = "https://api.github.com/repos/BtbN/FFmpeg-Builds/releases/latest" try: data = requests.get(url, timeout=15).json() except Exception: message(t("Could not connect to github to check for newer versions.")) raise if stop: message(t("Download Cancelled")) return gpl_ffmpeg = [asset for asset in data["assets"] if asset["name"].endswith("win64-gpl.zip")] if not gpl_ffmpeg: message( t("Could not find any matching FFmpeg ending with 'win64-gpl.zip' with") + f" {t('latest release from')} <a href='https://github.com/BtbN/FFmpeg-Builds/releases/'>" "https://github.com/BtbN/FFmpeg-Builds/releases/</a> " ) raise req = requests.get(gpl_ffmpeg[0]["browser_download_url"], stream=True) filename = ffmpeg_folder / "ffmpeg-full.zip" with open(filename, "wb") as f: for i, block in enumerate(req.iter_content(chunk_size=1024)): if i % 1000 == 0.0: # logger.debug(f"Downloaded {i // 1000}MB") signal.emit(int(((i * 1024) / gpl_ffmpeg[0]["size"]) * 90)) f.write(block) if stop: f.close() Path(filename).unlink() message(t("Download Cancelled")) return if filename.stat().st_size < 1000: message(t("FFmpeg was not properly downloaded as the file size is too small")) try: Path(filename).unlink() except OSError: pass raise try: reusables.extract(filename, path=ffmpeg_folder) except Exception: message(f"{t('Could not extract FFmpeg files from')} {filename}!") raise if stop: Path(filename).unlink() message(t("Download Cancelled")) return signal.emit(95) try: shutil.rmtree(str(ffmpeg_folder / "bin"), ignore_errors=True) shutil.rmtree(str(ffmpeg_folder / "doc"), ignore_errors=True) Path(filename).unlink() except OSError: pass signal.emit(96) sub_dir = next(Path(ffmpeg_folder).glob("ffmpeg-*")) for item in os.listdir(sub_dir): try: shutil.move(str(sub_dir / item), str(ffmpeg_folder)) except Exception as err: message(f"{t('Error while moving files in')} {ffmpeg_folder}: {err}") raise signal.emit(98) shutil.rmtree(sub_dir, ignore_errors=True) signal.emit(100)
def test_extract_zip(self): assert os.path.exists(test_structure_zip) reusables.extract(test_structure_zip, path=test_root, delete_on_success=False) assert os.path.exists(test_structure) assert os.path.isdir(test_structure) shutil.rmtree(test_structure)
def latest_ffmpeg(done_alert=False): ffmpeg_folder = Path(user_data_dir("FFmpeg", appauthor=False, roaming=True)) ffmpeg_folder.mkdir(exist_ok=True) url = "https://api.github.com/repos/BtbN/FFmpeg-Builds/releases/latest" try: data = requests.get(url, timeout=15).json() except Exception: message("Could not connect to github to check for newer versions.") return gpl_ffmpeg = [asset for asset in data["assets"] if asset["name"].endswith("win64-gpl.zip")] if not gpl_ffmpeg: message( "Could not find any matching FFmpeg ending with 'win64-gpl.zip' with " "latest release from <a href='https://github.com/BtbN/FFmpeg-Builds/releases/'>" "https://github.com/BtbN/FFmpeg-Builds/releases/</a> " ) return req = requests.get(gpl_ffmpeg[0]["browser_download_url"], stream=True) filename = ffmpeg_folder / "ffmpeg-full.zip" with open(filename, "wb") as f: for i, block in enumerate(req.iter_content(chunk_size=1024)): if i % 1000 == 0.0: logger.debug(f"Downloaded {i // 1000}MB") f.write(block) if filename.stat().st_size < 1000: message("FFmpeg was not properly downloaded as the file size is too small") try: Path(filename).unlink() except OSError: pass return try: reusables.extract(filename, path=ffmpeg_folder) except Exception: message(f"Could not extract FFmpeg files from {filename}!") return try: shutil.rmtree(str(ffmpeg_folder / "bin"), ignore_errors=True) shutil.rmtree(str(ffmpeg_folder / "doc"), ignore_errors=True) Path(filename).unlink() except OSError: pass sub_dir = next(Path(ffmpeg_folder).glob("ffmpeg-*")) for item in os.listdir(sub_dir): try: shutil.move(str(sub_dir / item), str(ffmpeg_folder)) except Exception as err: message(f"Error while moving files in {ffmpeg_folder}: {err}") shutil.rmtree(sub_dir, ignore_errors=True) if done_alert: message(f"FFmpeg has been downloaded to {ffmpeg_folder}")