async def create_binary(addresses: BuildFileAddresses, console: Console, workspace: Workspace, options: Binary.Options, options_bootstrapper: OptionsBootstrapper, build_root: BuildRoot) -> Binary: with Binary.line_oriented(options, console) as print_stdout: global_options = options_bootstrapper.bootstrap_options.for_global_scope( ) pants_distdir = Path(global_options.pants_distdir) if not is_child_of(pants_distdir, build_root.pathlib_path): console.print_stderr( f"When set to an absolute path, `--pants-distdir` must be relative to the build root." "You set it to {pants_distdir}. Instead, use a relative path or an absolute path relative to the build root." ) return Binary(exit_code=1) relative_distdir = pants_distdir.relative_to( build_root.pathlib_path) if pants_distdir.is_absolute( ) else pants_distdir print_stdout(f"Generating binaries in `./{relative_distdir}`") binaries = await MultiGet( Get[CreatedBinary](Address, address.to_address()) for address in addresses) merged_digest = await Get[Digest](DirectoriesToMerge( tuple(binary.digest for binary in binaries))) result = workspace.materialize_directory( DirectoryToMaterialize(merged_digest, path_prefix=str(relative_distdir))) for path in result.output_paths: print_stdout(f"Wrote {path}") return Binary(exit_code=0)
def validate_distdir(distdir: Path, buildroot: Path) -> DistDir: if not is_child_of(distdir, buildroot): raise InvalidDistDir( f"When set to an absolute path, `--pants-distdir` must be relative to the build root." f"You set it to {distdir}. Instead, use a relative path or an absolute path relative " f"to the build root.") relpath = distdir.relative_to( buildroot) if distdir.is_absolute() else distdir return DistDir(relpath)
def test_is_child_of() -> None: mock_build_root = Path("/mock/build/root") assert is_child_of(Path("/mock/build/root/dist/dir"), mock_build_root) assert is_child_of(Path("dist/dir"), mock_build_root) assert is_child_of(Path("./dist/dir"), mock_build_root) assert is_child_of(Path("../root/dist/dir"), mock_build_root) assert is_child_of(Path(""), mock_build_root) assert is_child_of(Path("./"), mock_build_root) assert not is_child_of(Path("/other/random/directory/root/dist/dir"), mock_build_root) assert not is_child_of(Path("../not_root/dist/dir"), mock_build_root)