From 0c6f03c65a9592d5a7e78d254aefdc3267395955 Mon Sep 17 00:00:00 2001 From: Mikhail Sveshnikov Date: Wed, 8 Feb 2023 16:20:45 +0300 Subject: [PATCH] Add no-deploy option for Flyio app creation (#601) --- mlem/contrib/docker/context.py | 21 ++++++++++++++++++++- mlem/contrib/flyio/meta.py | 2 ++ mlem/utils/module.py | 4 ++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/mlem/contrib/docker/context.py b/mlem/contrib/docker/context.py index 0ae2828a..0058ad30 100644 --- a/mlem/contrib/docker/context.py +++ b/mlem/contrib/docker/context.py @@ -1,4 +1,5 @@ import glob +import json import logging import os import posixpath @@ -7,11 +8,13 @@ import tempfile from abc import abstractmethod from contextlib import contextmanager +from json import JSONDecodeError from pathlib import Path from typing import Any, Callable, ClassVar, Dict, List, Optional, Union from fsspec import AbstractFileSystem from fsspec.implementations.local import LocalFileSystem +from importlib_metadata import Distribution, PackageNotFoundError from pydantic import BaseModel from yaml import safe_dump @@ -141,6 +144,9 @@ def get_install_command(self, args: "DockerBuildArgs"): class GitSource(MlemSource): type = "git" + def __init__(self, rev: str = ""): + self.rev = rev + def build(self, path): pass @@ -148,6 +154,8 @@ def get_install_command(self, args: "DockerBuildArgs"): rev = "" if LOCAL_DOCKER_CONFIG.git_rev is not None: rev = f"@{LOCAL_DOCKER_CONFIG.git_rev}" + if self.rev: + rev = f"@{self.rev}" return ( f"RUN {args.package_install_cmd} git {args.package_clean_cmd}\n" f"RUN pip install git+https://github.com/iterative/mlem{rev}#egg=mlem" @@ -175,7 +183,18 @@ def get_mlem_source() -> MlemSource: raise ValueError(f"unknown mlem source '{source}'") # if source is not specified if "dev" in mlem.__version__: - return WhlSource() + mlem_src_path = os.path.dirname(os.path.dirname(mlem.__file__)) + if os.path.exists(os.path.join(mlem_src_path, "setup.py")): # dev env + return WhlSource() + # installed from git branch (probably) + rev = "" + try: + rev = json.loads( + Distribution.from_name("mlem").read_text("direct_url.json") + )["vcs_info"]["commit_id"] + except (PackageNotFoundError, JSONDecodeError, TypeError, KeyError): + pass + return GitSource(rev=rev) return PipSource() diff --git a/mlem/contrib/flyio/meta.py b/mlem/contrib/flyio/meta.py index 0dcb7b3d..5a8b1a01 100644 --- a/mlem/contrib/flyio/meta.py +++ b/mlem/contrib/flyio/meta.py @@ -101,6 +101,7 @@ def _create_app(self, state: FlyioAppState): "region": self.region or self.get_env().region or project_config("", section=FlyioConfig).region, + "no-deploy": True, } if self.app_name: args["name"] = self.app_name @@ -144,6 +145,7 @@ def _build_in_dir(self, model: MlemModel, state: FlyioAppState): state.fly_toml = read_fly_toml(tempdir) state.update_model(model) self.update_state(state) + ui.echo(f"Model deployed to https://{state.hostname}") def deploy(self, model: MlemModel): check_flyctl_exec() diff --git a/mlem/utils/module.py b/mlem/utils/module.py index 9e6b6597..0ed621a6 100644 --- a/mlem/utils/module.py +++ b/mlem/utils/module.py @@ -283,14 +283,14 @@ def get_package_name(mod: Union[ModuleType, str]) -> str: packages = packages_distributions() fix_suggestion = "If that's incorrect, fix metadata manually, either in `.mlem` file or in your MLEM Python object." if mod not in packages or len(packages[mod]) == 0: - logger.warning( + logger.debug( "Fail to determine package name for module '%s', using module name instead. %s", mod, fix_suggestion, ) return mod if len(set(packages[mod])) > 1: - logger.warning( + logger.debug( "Found multiple packages for '%s' module: %s. Using first one. %s", mod, packages[mod],