Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fal file #281

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/fal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies = [
"pyjwt[crypto]>=2.8.0,<3",
"uvicorn>=0.29.0,<1",
"cookiecutter",
"tomli"
]

[project.optional-dependencies]
Expand Down
88 changes: 78 additions & 10 deletions projects/fal/src/fal/cli/deploy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import argparse
from collections import namedtuple
from pathlib import Path
from typing import Optional, Union

from fal.files import find_pyproject_toml, parse_pyproject_toml

from .parser import FalClientParser, RefAction

Expand Down Expand Up @@ -60,11 +63,39 @@ def _get_user() -> User:
raise FalServerlessError(f"Could not parse the user data: {e}")


def _deploy(args):
def _deploy_from_toml(app_name, args):
toml_path = find_pyproject_toml()

if toml_path is None:
raise ValueError("No pyproject.toml file found.")

fal_data = parse_pyproject_toml(toml_path)
apps = fal_data.get("apps", {})

try:
app_data = apps[app_name]
except KeyError:
raise ValueError(f"App {app_name} not found in pyproject.toml")

try:
app_ref = app_data["ref"]
except KeyError:
raise ValueError(f"App {app_name} does not have a ref key in pyproject.toml")

app_auth = app_data.get("auth", "private")

file_path, func_name = RefAction.split_ref(app_ref)

_deploy_from_reference((file_path, func_name), app_name, app_auth, args)


def _deploy_from_reference(
app_ref: tuple[Optional[Union[Path, str]], ...], app_name: str, auth: str, args
):
from fal.api import FalServerlessError, FalServerlessHost
from fal.utils import load_function_from

file_path, func_name = args.app_ref
file_path, func_name = app_ref
if file_path is None:
# Try to find a python file in the current directory
options = list(Path(".").glob("*.py"))
Expand All @@ -77,17 +108,17 @@ def _deploy(args):
)

[file_path] = options
file_path = str(file_path)
file_path = str(file_path) # type: ignore

user = _get_user()
host = FalServerlessHost(args.host)
isolated_function, app_name, app_auth = load_function_from(
isolated_function, guessed_app_name, app_auth = load_function_from(
host,
file_path,
func_name,
file_path, # type: ignore
func_name, # type: ignore
)
app_name = args.app_name or app_name
app_auth = args.auth or app_auth or "private"
app_name = app_name or guessed_app_name # type: ignore
app_auth = auth or app_auth or "private"
app_id = host.register(
func=isolated_function.func,
options=isolated_function.options,
Expand All @@ -114,6 +145,26 @@ def _deploy(args):
)


def _is_app_name(app_ref):
is_single_file = app_ref[1] is None
is_python_file = app_ref[0].endswith(".py")

return is_single_file and not is_python_file


def _deploy(args):
if _is_app_name(args.app_ref):
app_name = args.app_ref[0]

# we do not allow --app-name and --auth to be used with app name
if args.app_name or args.auth:
raise ValueError("Cannot use --app-name or --auth with app name reference.")

_deploy_from_toml(app_name, args)
else:
_deploy_from_reference(args.app_ref, args.app_name, args.auth, args)


def add_parser(main_subparsers, parents):
from fal.sdk import ALIAS_AUTH_MODES

Expand All @@ -122,36 +173,53 @@ def valid_auth_option(option):
raise argparse.ArgumentTypeError(f"{option} is not a auth option")
return option

deploy_help = "Deploy a fal application."
deploy_help = (
"Deploy a fal application. "
"If no app reference is provided, the command will look for a "
"pyproject.toml file with a [tool.fal.apps] section and deploy the "
"application specified with the provided app name."
)

epilog = (
"Examples:\n"
" fal deploy\n"
" fal deploy path/to/myfile.py\n"
" fal deploy path/to/myfile.py::MyApp\n"
" fal deploy path/to/myfile.py::MyApp --app-name myapp --auth public\n"
" fal deploy my-app\n"
)

parser = main_subparsers.add_parser(
"deploy",
parents=[*parents, FalClientParser(add_help=False)],
description=deploy_help,
help=deploy_help,
epilog=epilog,
)

parser.add_argument(
"app_ref",
nargs="?",
action=RefAction,
help=(
"Application reference. " "For example: `myfile.py::MyApp`, `myfile.py`."
"Application reference. Either a file path or a file path and a "
"function name separated by '::'. If no reference is provided, the "
"command will look for a pyproject.toml file with a [tool.fal.apps] "
"section and deploy the application specified with the provided app name.\n"
"File path example: path/to/myfile.py::MyApp\n"
"App name example: my-app\n"
),
)

parser.add_argument(
"--app-name",
help="Application name to deploy with.",
)

parser.add_argument(
"--auth",
type=valid_auth_option,
help="Application authentication mode (private, public).",
)

parser.set_defaults(func=_deploy)
18 changes: 11 additions & 7 deletions projects/fal/src/fal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ def __init__(self, *args, **kwargs):
kwargs.setdefault("default", (None, None))
super().__init__(*args, **kwargs)

def __call__(self, parser, args, values, option_string=None): # noqa: ARG002
if isinstance(values, tuple):
file_path, obj_path = values
elif values.find("::") > 1:
file_path, obj_path = values.split("::", 1)
else:
file_path, obj_path = values, None
@classmethod
def split_ref(cls, value):
if isinstance(value, tuple):
return value

if value.find("::") > 1:
return value.split("::", 1)

return value, None

def __call__(self, parser, args, values, option_string=None): # noqa: ARG002
file_path, obj_path = self.split_ref(values)
setattr(args, self.dest, (file_path, obj_path))


Expand Down
81 changes: 81 additions & 0 deletions projects/fal/src/fal/files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, Optional, Sequence, Tuple, Union

import tomli


@lru_cache
def _load_toml(path: Union[Path, str]) -> Dict[str, Any]:
with open(path, "rb") as f:
return tomli.load(f)


@lru_cache
def _cached_resolve(path: Path) -> Path:
return path.resolve()


@lru_cache
def find_project_root(srcs: Optional[Sequence[str]]) -> Tuple[Path, str]:
"""Return a directory containing .git, or pyproject.toml.

That directory will be a common parent of all files and directories
passed in `srcs`.

If no directory in the tree contains a marker that would specify it's the
project root, the root of the file system is returned.

Returns a two-tuple with the first element as the project root path and
the second element as a string describing the method by which the
project root was discovered.
"""
if not srcs:
srcs = [str(_cached_resolve(Path.cwd()))]

path_srcs = [_cached_resolve(Path(Path.cwd(), src)) for src in srcs]

# A list of lists of parents for each 'src'. 'src' is included as a
# "parent" of itself if it is a directory
src_parents = [
list(path.parents) + ([path] if path.is_dir() else []) for path in path_srcs
]

common_base = max(
set.intersection(*(set(parents) for parents in src_parents)),
key=lambda path: path.parts,
)

for directory in (common_base, *common_base.parents):
if (directory / ".git").exists():
return directory, ".git directory"

if (directory / "pyproject.toml").is_file():
pyproject_toml = _load_toml(directory / "pyproject.toml")
if "fal" in pyproject_toml.get("tool", {}):
return directory, "pyproject.toml"

return directory, "file system root"


def find_pyproject_toml(
path_search_start: Optional[Tuple[str, ...]] = None,
) -> Optional[str]:
"""Find the absolute filepath to a pyproject.toml if it exists"""
path_project_root, _ = find_project_root(path_search_start)
path_pyproject_toml = path_project_root / "pyproject.toml"

if path_pyproject_toml.is_file():
return str(path_pyproject_toml)


def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should eventually make this a typed dict or a data class so we have stronger typing and that way caller functions have guarantees about what fields they can expect to access

"""Parse a pyproject toml file, pulling out relevant parts for fal.

If parsing fails, will raise a tomli.TOMLDecodeError.
"""
pyproject_toml = _load_toml(path_config)
config: Dict[str, Any] = pyproject_toml.get("tool", {}).get("fal", {})
config = {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}

return config
Loading
Loading