Skip to content

Commit 733ec55

Browse files
ethanwharrisBorda
authored andcommitted
[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 (cherry picked from commit b3281eb)
1 parent 27afcd3 commit 733ec55

File tree

4 files changed

+17
-34
lines changed

4 files changed

+17
-34
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1313

1414
### Changed
1515

16+
- 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))
17+
18+
1619
-
1720

1821
### Fixed

src/lightning_app/runners/cloud.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
from lightning_app.utilities.cloud import _get_project
6464
from lightning_app.utilities.dependency_caching import get_hash
6565
from lightning_app.utilities.load_app import _prettifiy_exception, load_app_from_file
66-
from lightning_app.utilities.packaging.app_config import AppConfig, find_config_file
66+
from lightning_app.utilities.packaging.app_config import _get_config_file, AppConfig
6767
from lightning_app.utilities.packaging.lightning_utils import _prepare_lightning_wheels_and_requirements
6868
from lightning_app.utilities.secrets import _names_to_ids
6969

@@ -95,9 +95,9 @@ def dispatch(
9595

9696
# TODO: verify lightning version
9797
# _verify_lightning_version()
98-
config_file = find_config_file(self.entrypoint_file)
99-
app_config = AppConfig.load_from_file(config_file) if config_file else AppConfig()
100-
root = config_file.parent if config_file else Path(self.entrypoint_file).absolute().parent
98+
config_file = _get_config_file(self.entrypoint_file)
99+
app_config = AppConfig.load_from_file(config_file) if config_file.exists() else AppConfig()
100+
root = Path(self.entrypoint_file).absolute().parent
101101
cleanup_handle = _prepare_lightning_wheels_and_requirements(root)
102102
self.app._update_index_file()
103103
repo = LocalSourceCodeDir(path=root)

src/lightning_app/utilities/packaging/app_config.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def save_to_file(self, path: Union[str, pathlib.Path]) -> None:
2828

2929
def save_to_dir(self, directory: Union[str, pathlib.Path]) -> None:
3030
"""Save the configuration to a file '.lightning' to the given folder in YAML format."""
31-
self.save_to_file(pathlib.Path(directory, _APP_CONFIG_FILENAME))
31+
self.save_to_file(_get_config_file(directory))
3232

3333
@classmethod
3434
def load_from_file(cls, path: Union[str, pathlib.Path]) -> "AppConfig":
@@ -47,22 +47,14 @@ def load_from_dir(cls, directory: Union[str, pathlib.Path]) -> "AppConfig":
4747
return cls.load_from_file(pathlib.Path(directory, _APP_CONFIG_FILENAME))
4848

4949

50-
def find_config_file(source_path: pathlib.Path = pathlib.Path.cwd()) -> Optional[pathlib.Path]:
51-
"""Search for the Lightning app config file '.lightning' at the given source path.
52-
53-
Relative to the given path, it will search for the '.lightning' config file by going up the directory structure
54-
until found. Returns ``None`` if no config file is found in any of the parent directories.
50+
def _get_config_file(source_path: Union[str, pathlib.Path]) -> pathlib.Path:
51+
"""Get the Lightning app config file '.lightning' at the given source path.
5552
5653
Args:
57-
source_path: A path to a folder or a file. The search for the config file will start relative to this path.
54+
source_path: A path to a folder or a file.
5855
"""
5956
source_path = pathlib.Path(source_path).absolute()
6057
if source_path.is_file():
6158
source_path = source_path.parent
6259

63-
candidate = pathlib.Path(source_path / _APP_CONFIG_FILENAME)
64-
if candidate.is_file():
65-
return candidate
66-
67-
if source_path.parents:
68-
return find_config_file(source_path.parent)
60+
return pathlib.Path(source_path / _APP_CONFIG_FILENAME)

tests/tests_app/utilities/packaging/test_app_config.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pathlib
22

3-
from lightning_app.utilities.packaging.app_config import AppConfig, find_config_file
3+
from lightning_app.utilities.packaging.app_config import _get_config_file, AppConfig
44

55

66
def _make_empty_config_file(folder):
@@ -10,24 +10,12 @@ def _make_empty_config_file(folder):
1010
return file
1111

1212

13-
def test_find_config_file(tmpdir, monkeypatch):
14-
monkeypatch.chdir(pathlib.Path("/"))
15-
assert find_config_file() is None
16-
17-
monkeypatch.chdir(pathlib.Path.home())
18-
assert find_config_file() is None
19-
13+
def test_get_config_file(tmpdir):
2014
_ = _make_empty_config_file(tmpdir)
21-
config_file1 = _make_empty_config_file(tmpdir / "a" / "b")
22-
23-
assert find_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning")
24-
assert find_config_file(config_file1) == pathlib.Path(tmpdir, "a", "b", ".lightning")
25-
assert find_config_file(pathlib.Path(tmpdir, "a")) == pathlib.Path(tmpdir, ".lightning")
15+
config_file1 = _make_empty_config_file(tmpdir)
2616

27-
# the config must be a file, a folder of the same name gets ignored
28-
fake_config_folder = pathlib.Path(tmpdir, "fake", ".lightning")
29-
fake_config_folder.mkdir(parents=True)
30-
assert find_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning")
17+
assert _get_config_file(tmpdir) == pathlib.Path(tmpdir, ".lightning")
18+
assert _get_config_file(config_file1) == pathlib.Path(tmpdir, ".lightning")
3119

3220

3321
def test_app_config_save_load(tmpdir):

0 commit comments

Comments
 (0)