Skip to content

Commit

Permalink
[App] Change app root / config path to be the app.py parent directo…
Browse files Browse the repository at this point in the history
…ry (#15654)

* Change app root / config path to be the `app.py` parent directory

* Update CHANGELOG.md

* mypy

* Fix

* Mypy
  • Loading branch information
ethanwharris committed Nov 11, 2022
1 parent 757413c commit b3281eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/lightning_app/CHANGELOG.md
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/lightning_app/runners/cloud.py
Expand Up @@ -63,7 +63,7 @@
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

Expand Down Expand Up @@ -95,9 +95,9 @@ def dispatch(

# 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)
Expand Down
18 changes: 5 additions & 13 deletions src/lightning_app/utilities/packaging/app_config.py
Expand Up @@ -28,7 +28,7 @@ def save_to_file(self, path: Union[str, pathlib.Path]) -> None:

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":
Expand All @@ -47,22 +47,14 @@ def load_from_dir(cls, directory: Union[str, pathlib.Path]) -> "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)
22 changes: 5 additions & 17 deletions tests/tests_app/utilities/packaging/test_app_config.py
@@ -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):
Expand All @@ -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")

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")
config_file1 = _make_empty_config_file(tmpdir)

# 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):
Expand Down

0 comments on commit b3281eb

Please sign in to comment.