Example #1
0
 def determine_source_root(path: str) -> str:
     source_root = source_roots_object.safe_find_by_path(path)
     if source_root is not None:
         return cast(str, source_root.path)
     if source_root_config.options.unmatched == "fail":
         raise NoSourceRootError(
             f"Could not find a source root for `{path}`.")
     # Otherwise, create a source root by using the parent directory.
     return PurePath(path).parent.as_posix()
Example #2
0
async def list_backends(
    backend_options: BackendsOptions,
    global_options: GlobalOptions,
    console: Console,
) -> Backends:
    discovered_register_pys = await Get[Snapshot](PathGlobs(
        ["**/*/register.py"]))
    register_pys_content = await Get[FilesContent](
        Digest, discovered_register_pys.digest)

    source_root_stripped_sources = await Get[SourceRootStrippedSources](
        StripSnapshotRequest(snapshot=discovered_register_pys))
    file_to_stripped_file_mapping = source_root_stripped_sources.get_file_to_stripped_file_mapping(
    )
    stripped_paths = []
    for fc in register_pys_content:
        stripped_path = file_to_stripped_file_mapping.get(fc.path)
        if stripped_path is None:
            # This should never happen, but it's worth checking proactively.
            raise NoSourceRootError(fc.path)
        stripped_paths.append(stripped_path)

    backend_infos = tuple(
        BackendInfo.create(fc, stripped_path, global_options)
        for fc, stripped_path in zip(register_pys_content, stripped_paths))
    v1_backends = []
    v2_backends = []
    for backend in backend_infos:
        if backend.is_v1:
            v1_backends.append(backend)
        if backend.is_v2:
            v2_backends.append(backend)

    with backend_options.line_oriented(console) as print_stdout:
        if global_options.options.v1:
            print_stdout(
                format_section(v1_backends,
                               console,
                               version_number=1,
                               option_name="backend_packages"))
        if global_options.options.v2:
            print_stdout(
                format_section(v2_backends,
                               console,
                               version_number=2,
                               option_name="backend_packages2"))
    return Backends(exit_code=0)
Example #3
0
    def create(cls, file_content: FileContent, source_roots: SourceRoots,
               global_options: GlobalOptions) -> "BackendInfo":
        source_root = source_roots.safe_find_by_path(file_content.path)
        if source_root is None:
            raise NoSourceRootError(
                f"Could not find a source root for `{file_content.path}`.")
        stripped_path = file_content.path[len(source_root.path) + 1:]
        module_name = os.path.dirname(stripped_path).replace(os.sep, ".")

        v1_entry_points = ("register_goals", "global_subsystems",
                           "build_file_aliases")
        # NB: We intentionally do not check for `targets2` because even V1 is expected to have
        # Target API bindings.
        v2_entry_points = ("rules", "build_file_aliases2")

        def any_entry_points_registered(entry_points: Sequence[str]) -> bool:
            return any(f"def {entry_point}()" in file_content.content.decode()
                       for entry_point in entry_points)

        activated_v1_backends = {
            "pants.build_graph",
            "pants.core_tasks",
            *global_options.options.backend_packages,
        }
        activated_v2_backends = {
            "pants.rules.core", *global_options.options.backend_packages2
        }

        return cls(
            name=module_name,
            description=hackily_get_module_docstring(
                file_content.content.decode()),
            is_v1=any_entry_points_registered(v1_entry_points),
            is_v2=any_entry_points_registered(v2_entry_points),
            is_v1_activated=module_name in activated_v1_backends,
            is_v2_activated=module_name in activated_v2_backends,
        )
Example #4
0
def source_root_or_raise(source_roots: SourceRoots, path: str) -> str:
    """Find the source root for the given path, or raise if none is found."""
    source_root = source_roots.find_by_path(path)
    if not source_root:
        raise NoSourceRootError(f"Found no source root for {path}")
    return source_root.path
Example #5
0
def source_root_or_raise(source_roots: SourceRoots, path: str) -> str:
    source_root = source_roots.find_by_path(path)
    if not source_root:
        raise NoSourceRootError(f"Found no source root for {path}")
    return source_root.path