def assert_chroot_perms(copyfn): with temporary_dir() as src: one = os.path.join(src, "one") touch(one) two = os.path.join(src, "two") touch(two) chmod_plus_x(two) with temporary_dir() as dst: chroot = Chroot(dst) copyfn(chroot, one, "one") copyfn(chroot, two, "two") assert extract_perms(one) == extract_perms( os.path.join(chroot.path(), "one")) assert extract_perms(two) == extract_perms( os.path.join(chroot.path(), "two")) zip_path = os.path.join(src, "chroot.zip") chroot.zip(zip_path) with temporary_dir() as extract_dir: with contextlib.closing(PermPreservingZipFile(zip_path)) as zf: zf.extractall(extract_dir) assert extract_perms(one) == extract_perms( os.path.join(extract_dir, "one")) assert extract_perms(two) == extract_perms( os.path.join(extract_dir, "two"))
def test_chroot_zip_symlink(): # type: () -> None with temporary_dir() as tmp: chroot = Chroot(os.path.join(tmp, "chroot")) chroot.write(b"data", "directory/subdirectory/file") chroot.write(b"data", "directory/subdirectory/file.foo") chroot.symlink( os.path.join(chroot.path(), "directory/subdirectory/file"), "directory/subdirectory/symlinked", ) cwd = os.getcwd() try: os.chdir(os.path.join(chroot.path(), "directory/subdirectory")) chroot.symlink( "file", "directory/subdirectory/rel-symlinked", ) finally: os.chdir(cwd) chroot.symlink(os.path.join(chroot.path(), "directory"), "symlinked") zip_dst = os.path.join(tmp, "chroot.zip") chroot.zip(zip_dst, exclude_file=lambda path: path.endswith(".foo")) with open_zip(zip_dst) as zip: assert [ "directory/", "directory/subdirectory/", "directory/subdirectory/file", "directory/subdirectory/rel-symlinked", "directory/subdirectory/symlinked", "symlinked/", "symlinked/subdirectory/", "symlinked/subdirectory/file", "symlinked/subdirectory/rel-symlinked", "symlinked/subdirectory/symlinked", ] == sorted(zip.namelist()) assert b"" == zip.read("directory/") assert b"" == zip.read("directory/subdirectory/") assert b"data" == zip.read("directory/subdirectory/file") assert b"data" == zip.read("directory/subdirectory/rel-symlinked") assert b"data" == zip.read("directory/subdirectory/symlinked") assert b"" == zip.read("symlinked/") assert b"" == zip.read("symlinked/subdirectory/") assert b"data" == zip.read("symlinked/subdirectory/file") assert b"data" == zip.read("symlinked/subdirectory/rel-symlinked") assert b"data" == zip.read("symlinked/subdirectory/symlinked")
def assert_chroot_perms(copyfn): with temporary_dir() as src: one = os.path.join(src, 'one') touch(one) two = os.path.join(src, 'two') touch(two) chmod_plus_x(two) with temporary_dir() as dst: chroot = Chroot(dst) copyfn(chroot, one, 'one') copyfn(chroot, two, 'two') assert extract_perms(one) == extract_perms(os.path.join(chroot.path(), 'one')) assert extract_perms(two) == extract_perms(os.path.join(chroot.path(), 'two')) zip_path = os.path.join(src, 'chroot.zip') chroot.zip(zip_path) with temporary_dir() as extract_dir: with contextlib.closing(PermPreservingZipFile(zip_path)) as zf: zf.extractall(extract_dir) assert extract_perms(one) == extract_perms(os.path.join(extract_dir, 'one')) assert extract_perms(two) == extract_perms(os.path.join(extract_dir, 'two'))