[App] Change app root / config path to be the `app.py` parent directory (#15654)
* Change app root / config path to be the `app.py` parent directory * Update CHANGELOG.md * mypy * Fix * Mypy
This commit is contained in:
parent
757413c8e0
commit
b3281eb9ab
|
@ -37,6 +37,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
|
|||
- The `params` argument in `TracerPythonScript.run` no longer prepends `--` automatically to parameters ([#15518](https://github.com/Lightning-AI/lightning/pull/15518))
|
||||
|
||||
|
||||
- Changed the root directory of the app (which gets uploaded) to be the folder containing the app file, rather than any parent folder containing a `.lightning` file ([#15654](https://github.com/Lightning-AI/lightning/pull/15654))
|
||||
|
||||
|
||||
### Deprecated
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ from lightning_app.utilities.app_helpers import Logger
|
|||
from lightning_app.utilities.cloud import _get_project
|
||||
from lightning_app.utilities.dependency_caching import get_hash
|
||||
from lightning_app.utilities.load_app import _prettifiy_exception, load_app_from_file
|
||||
from lightning_app.utilities.packaging.app_config import AppConfig, find_config_file
|
||||
from lightning_app.utilities.packaging.app_config import _get_config_file, AppConfig
|
||||
from lightning_app.utilities.packaging.lightning_utils import _prepare_lightning_wheels_and_requirements
|
||||
from lightning_app.utilities.secrets import _names_to_ids
|
||||
|
||||
|
@ -95,9 +95,9 @@ class CloudRuntime(Runtime):
|
|||
|
||||
# TODO: verify lightning version
|
||||
# _verify_lightning_version()
|
||||
config_file = find_config_file(self.entrypoint_file)
|
||||
app_config = AppConfig.load_from_file(config_file) if config_file else AppConfig()
|
||||
root = config_file.parent if config_file else Path(self.entrypoint_file).absolute().parent
|
||||
config_file = _get_config_file(self.entrypoint_file)
|
||||
app_config = AppConfig.load_from_file(config_file) if config_file.exists() else AppConfig()
|
||||
root = Path(self.entrypoint_file).absolute().parent
|
||||
cleanup_handle = _prepare_lightning_wheels_and_requirements(root)
|
||||
self.app._update_index_file()
|
||||
repo = LocalSourceCodeDir(path=root)
|
||||
|
|
|
@ -28,7 +28,7 @@ class AppConfig:
|
|||
|
||||
def save_to_dir(self, directory: Union[str, pathlib.Path]) -> None:
|
||||
"""Save the configuration to a file '.lightning' to the given folder in YAML format."""
|
||||
self.save_to_file(pathlib.Path(directory, _APP_CONFIG_FILENAME))
|
||||
self.save_to_file(_get_config_file(directory))
|
||||
|
||||
@classmethod
|
||||
def load_from_file(cls, path: Union[str, pathlib.Path]) -> "AppConfig":
|
||||
|
@ -47,22 +47,14 @@ class AppConfig:
|
|||
return cls.load_from_file(pathlib.Path(directory, _APP_CONFIG_FILENAME))
|
||||
|
||||
|
||||
def find_config_file(source_path: pathlib.Path = pathlib.Path.cwd()) -> Optional[pathlib.Path]:
|
||||
"""Search for the Lightning app config file '.lightning' at the given source path.
|
||||
|
||||
Relative to the given path, it will search for the '.lightning' config file by going up the directory structure
|
||||
until found. Returns ``None`` if no config file is found in any of the parent directories.
|
||||
def _get_config_file(source_path: Union[str, pathlib.Path]) -> pathlib.Path:
|
||||
"""Get the Lightning app config file '.lightning' at the given source path.
|
||||
|
||||
Args:
|
||||
source_path: A path to a folder or a file. The search for the config file will start relative to this path.
|
||||
source_path: A path to a folder or a file.
|
||||
"""
|
||||
source_path = pathlib.Path(source_path).absolute()
|
||||
if source_path.is_file():
|
||||
source_path = source_path.parent
|
||||
|
||||
candidate = pathlib.Path(source_path / _APP_CONFIG_FILENAME)
|
||||
if candidate.is_file():
|
||||
return candidate
|
||||
|
||||
if source_path.parents:
|
||||
return find_config_file(source_path.parent)
|
||||
return pathlib.Path(source_path / _APP_CONFIG_FILENAME)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import pathlib
|
||||
|
||||
from lightning_app.utilities.packaging.app_config import AppConfig, find_config_file
|
||||
from lightning_app.utilities.packaging.app_config import _get_config_file, AppConfig
|
||||
|
||||
|
||||
def _make_empty_config_file(folder):
|
||||
|
@ -10,24 +10,12 @@ def _make_empty_config_file(folder):
|
|||
return file
|
||||
|
||||
|
||||
def test_find_config_file(tmpdir, monkeypatch):
|
||||
monkeypatch.chdir(pathlib.Path("/"))
|
||||
assert find_config_file() is None
|
||||
|
||||
monkeypatch.chdir(pathlib.Path.home())
|
||||
assert find_config_file() is None
|
||||
|
||||
def test_get_config_file(tmpdir):
|
||||
_ = _make_empty_config_file(tmpdir)
|
||||
config_file1 = _make_empty_config_file(tmpdir / "a" / "b")
|
||||
config_file1 = _make_empty_config_file(tmpdir)
|
||||
|
||||
assert find_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning")
|
||||
assert find_config_file(config_file1) == pathlib.Path(tmpdir, "a", "b", ".lightning")
|
||||
assert find_config_file(pathlib.Path(tmpdir, "a")) == pathlib.Path(tmpdir, ".lightning")
|
||||
|
||||
# the config must be a file, a folder of the same name gets ignored
|
||||
fake_config_folder = pathlib.Path(tmpdir, "fake", ".lightning")
|
||||
fake_config_folder.mkdir(parents=True)
|
||||
assert find_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning")
|
||||
assert _get_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning")
|
||||
assert _get_config_file(config_file1) == pathlib.Path(tmpdir, ".lightning")
|
||||
|
||||
|
||||
def test_app_config_save_load(tmpdir):
|
||||
|
|
Loading…
Reference in New Issue