def test_pack_unpack(tmp_path: Path): files = {"abc.pth": str(tmp_path / "foo.pth")} with (tmp_path / "foo.pth").open("w"): pass with (tmp_path / "bar.yaml").open("w") as f: # I dared to stack "/" to test yaml.safe_dump({"a": str(tmp_path / "//foo.pth")}, f) with (tmp_path / "a").open("w"): pass (tmp_path / "b").mkdir(parents=True, exist_ok=True) with (tmp_path / "b" / "a").open("w"): pass pack( files=files, yaml_files={"def.yaml": str(tmp_path / "bar.yaml")}, option=[tmp_path / "a", tmp_path / "b" / "a"], outpath=str(tmp_path / "out.tgz"), ) retval = unpack(str(tmp_path / "out.tgz"), str(tmp_path)) assert retval == { "abc": str(tmp_path / "packed" / "abc.pth"), "def": str(tmp_path / "packed" / "def.yaml"), "option": [ str(tmp_path / "packed" / "option" / "a"), str(tmp_path / "packed" / "option" / "a.1"), ], "meta": str(tmp_path / "packed" / "meta.yaml"), }
def test_pack_unpack(tmp_path: Path, type): files = {"abc": str(tmp_path / "foo.pth")} with (tmp_path / "foo.pth").open("w"): pass with (tmp_path / "bar.yaml").open("w") as f: # I dared to stack "/" to test yaml.safe_dump({"a": str(tmp_path / "//foo.pth")}, f) with (tmp_path / "a").open("w"): pass (tmp_path / "b").mkdir(parents=True, exist_ok=True) with (tmp_path / "b" / "a").open("w"): pass pack( files=files, yaml_files={"def": str(tmp_path / "bar.yaml")}, option=[tmp_path / "a", tmp_path / "b" / "a"], outpath=str(tmp_path / f"out.{type}"), ) retval = unpack(str(tmp_path / f"out.{type}"), str(tmp_path)) # Retry unpack. If cache file exists, generate dict from it retval2 = unpack(str(tmp_path / f"out.{type}"), str(tmp_path)) assert retval == { "abc": str(tmp_path / tmp_path / "foo.pth"), "def": str(tmp_path / tmp_path / "bar.yaml"), } assert retval2 == { "abc": str(tmp_path / tmp_path / "foo.pth"), "def": str(tmp_path / tmp_path / "bar.yaml"), }
def test_pack_unpack_recursive(tmp_path: Path, type): p = tmp_path / "a" / "b" p.mkdir(parents=True) with (p / "foo.pth").open("w"): pass pack( files={}, yaml_files={}, option=[p], outpath=str(tmp_path / f"out.{type}"), ) unpack(str(tmp_path / f"out.{type}"), str(tmp_path)) assert (tmp_path / p / "foo.pth").exists()
def main(cmd=None): parser = get_parser() args = parser.parse_args(cmd) if not hasattr(args, "contents"): parser.print_help() parser.exit(2) yaml_files = { y: getattr(args, y) for y in args.contents.yaml_files if getattr(args, y) is not None } files = { y: getattr(args, y) for y in args.contents.files if getattr(args, y) is not None } pack( yaml_files=yaml_files, files=files, option=args.option, outpath=args.outpath, )
def test_pack_not_exist_file(): with pytest.raises(FileNotFoundError): pack(files={"a": "aaa"}, yaml_files={}, outpath="out")