Ejemplo n.º 1
0
    def test_build_non_importable(self, serve_instance):
        def gen_deployment():
            @serve.deployment
            def f():
                pass

            return f

        with pytest.raises(RuntimeError, match="must be importable"):
            build_app(gen_deployment().bind()).to_dict()
Ejemplo n.º 2
0
def maybe_build(
    node: DeploymentNode, use_build: bool
) -> Union[Application, DeploymentNode]:
    if use_build:
        return Application.from_dict(build_app(node).to_dict())
    else:
        return node
Ejemplo n.º 3
0
def build(import_path: str, app_dir: str, output_path: Optional[str]):
    sys.path.insert(0, app_dir)

    node: Union[ClassNode, FunctionNode] = import_attr(import_path)
    if not isinstance(node, (ClassNode, FunctionNode)):
        raise TypeError(
            f"Expected '{import_path}' to be ClassNode or "
            f"FunctionNode, but got {type(node)}."
        )

    app = build_app(node)

    config = ServeApplicationSchema(
        deployments=[deployment_to_schema(d) for d in app.deployments.values()]
    ).dict()
    config["import_path"] = import_path

    if output_path is not None:
        if not output_path.endswith(".yaml"):
            raise ValueError("FILE_PATH must end with '.yaml'.")

        with open(output_path, "w") as f:
            yaml.safe_dump(config, stream=f, default_flow_style=False, sort_keys=False)
    else:
        print(yaml.safe_dump(config, default_flow_style=False, sort_keys=False), end="")
Ejemplo n.º 4
0
def build(app_dir: str, output_path: Optional[str], import_path: str):
    sys.path.insert(0, app_dir)

    node: Union[ClassNode, FunctionNode] = import_attr(import_path)
    if not isinstance(node, (ClassNode, FunctionNode)):
        raise TypeError(f"Expected '{import_path}' to be ClassNode or "
                        f"FunctionNode, but got {type(node)}.")

    app = build_app(node)

    if output_path is not None:
        if not output_path.endswith(".yaml"):
            raise ValueError("FILE_PATH must end with '.yaml'.")

        with open(output_path, "w") as f:
            app.to_yaml(f)
    else:
        print(app.to_yaml(), end="")
Ejemplo n.º 5
0
def build(import_path: str, app_dir: str, output_path: Optional[str]):
    sys.path.insert(0, app_dir)

    node: Union[ClassNode, FunctionNode] = import_attr(import_path)
    if not isinstance(node, (ClassNode, FunctionNode)):
        raise TypeError(f"Expected '{import_path}' to be ClassNode or "
                        f"FunctionNode, but got {type(node)}.")

    app = build_app(node)

    config = ServeApplicationSchema(deployments=[
        deployment_to_schema(d) for d in app.deployments.values()
    ]).dict()
    config["import_path"] = import_path

    config_str = ("# This file was generated using the `serve build` command "
                  f"on Ray v{ray.__version__}.\n\n")
    config_str += yaml.dump(config,
                            Dumper=ServeBuildDumper,
                            default_flow_style=False,
                            sort_keys=False)

    with open(output_path, "w") if output_path else sys.stdout as f:
        f.write(config_str)
Ejemplo n.º 6
0
def maybe_build(node: ClassNode,
                use_build: bool) -> Union[Application, ClassNode]:
    if use_build:
        return build_app(node)
    else:
        return node
Ejemplo n.º 7
0
 def test_build_non_json_serializable_kwargs(self, serve_instance):
     with pytest.raises(
         TypeError, match="must be JSON-serializable to build.*init_kwargs"
     ):
         build_app(self.A.bind(kwarg=np.zeros(100))).to_dict()