def _create_zip(archive_file: Path, *sources: Path) -> None: with zipfile.ZipFile(archive_file, "w") as archive: for src in sources: with sh.cd(src.parent): items = [src.relative_to(src.parent), *sh.walk(src.name)] for item in items: archive.write(item)
def _create_unsafe_zip(archive_file: Path, src: Path, parent_path: Path) -> None: with zipfile.ZipFile(archive_file, "w") as archive: with sh.cd(src.parent): items = [src.relative_to(src.parent)] + list(sh.walk(src.name)) for item in items: member = zipfile.ZipInfo.from_file(str(item)) member.filename = str(parent_path / member.filename) data = item.read_text() if item.is_file() else "" archive.writestr(member, data=data)
def _create_unsafe_tar(archive_file: Path, src: Path, parent_path: Path) -> None: ext = "".join(archive_file.suffixes) compression = TAR_COMPRESSIONS[ext] mode = f"w:{compression}" with tarfile.open(archive_file, mode, format=tarfile.PAX_FORMAT) as archive: with sh.cd(src.parent): items = [src.relative_to(src.parent)] + list(sh.walk(src.name)) for item in items: member = archive.gettarinfo(str(item)) member.name = str(parent_path / member.name) archive.addfile(member)
def test_chmod__recursively_sets_mode( tmp_path: Path, items: t.List[t.Union[Dir, File]], mode: t.Union[int, str], expected_file_mode: int, expected_dir_mode: int, ): test_dir = Dir(tmp_path / "test_dir", *items) test_dir.mkdir() sh.chmod(test_dir.path, mode, recursive=True) for path in (test_dir.path, *sh.walk(test_dir.path)): expected_mode = expected_dir_mode if path.is_dir( ) else expected_file_mode path_mode = stat.filemode(path.stat().st_mode) assert ( path_mode == expected_mode ), f"Expected mode of {path} to be {expected_mode!r}, not {path_mode!r}"
Path("2"), Path("c"), }, ), param( [ Dir( "a", Dir("aa1", Dir("aaa1", File("aaa1.txt")), Dir("aaa2", File("aaa2.txt"))), Dir("aa2"), ), Dir("b"), Dir("c"), ], [Path("a"), sh.ls("b"), sh.walk("c")], ".", { "a": "1", "b": "2", "c": "3" }, { Path("1"), Path("1/aa1"), Path("1/aa1/aaa1"), Path("1/aa1/aaa1/aaa1.txt"), Path("1/aa1/aaa2"), Path("1/aa1/aaa2/aaa2.txt"), Path("1/aa2"), Path("2"),