Skip to content

Commit

Permalink
App: change cache location (Lightning-AI#17491)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: thomas <thomas@thomass-MacBook-Pro.local>
Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
  • Loading branch information
4 people committed May 4, 2023
1 parent 04a86f9 commit 7df627b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extend-select = [
]
ignore = [
"E731", # Do not assign a lambda expression, use a def
"S108",
]
# Exclude a variety of commonly ignored directories.
exclude = [
Expand Down
2 changes: 1 addition & 1 deletion src/lightning/app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Changed

-
- Changed `LocalSourceCodeDir` cache_location to not use home in some certain cases ([#17491](https://github.com/Lightning-AI/lightning/pull/17491))


### Deprecated
Expand Down
5 changes: 4 additions & 1 deletion src/lightning/app/runners/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,12 @@ def _resolve_cluster_id(
self, cluster_id: Optional[str], project_id: str, existing_cloudspaces: List[V1CloudSpace]
) -> Optional[str]:
"""If cloudspaces exist and cluster is None, mimic cluster selection logic to choose a default."""
# 1. Use the environement variables
if cluster_id is None:
cluster_id = os.getenv("CLUSTER_ID", None)
cluster_id = os.getenv("LIGHTNING_CLUSTER_ID", None)

# 2. Use the project bindings
# TODO: Use the user prefered cluster.
if cluster_id is None and len(existing_cloudspaces) > 0:
# Determine the cluster ID
cluster_id = _get_default_cluster(self.backend.client, project_id)
Expand Down
8 changes: 6 additions & 2 deletions src/lightning/app/source_code/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
class LocalSourceCodeDir:
"""Represents the source code directory and provide the utilities to manage it."""

cache_location: Path = Path.home() / ".lightning" / "cache" / "repositories"

def __init__(self, path: Path, ignore_functions: Optional[List[_IGNORE_FUNCTION]] = None) -> None:
if "LIGHTNING_VSCODE_WORKSPACE" in os.environ:
# Don't use home to store the tar ball. This won't play nice with symlinks
self.cache_location: Path = Path("/tmp", ".lightning", "cache", "repositories")
else:
self.cache_location: Path = Path.home() / ".lightning" / "cache" / "repositories"

self.path = path
self.ignore_functions = ignore_functions

Expand Down
1 change: 1 addition & 0 deletions tests/integrations_app/public/test_boring_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def check_hello_there(*_, **__):

assert result.exit_code == 0
assert result.exception is None
# TODO: Resolve
# lines = result.output.splitlines()
# assert any("Received from root.dict.dst_w" in line for line in lines)
print("Succeeded App!")
26 changes: 21 additions & 5 deletions tests/tests_app/source_code/test_local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
import sys
import tarfile
import uuid
from pathlib import Path
from unittest import mock

import pytest

from lightning.app.source_code import LocalSourceCodeDir

Expand Down Expand Up @@ -28,17 +33,29 @@ def test_repository_checksum(tmp_path):
assert checksum_a != checksum_c


@pytest.mark.skipif(sys.platform == "win32", reason="this runs only on linux")
@mock.patch.dict(os.environ, {"LIGHTNING_VSCODE_WORKSPACE": "something"})
def test_local_cache_path_tmp(tmp_path):
"""LocalRepository.cache_location is under tmp."""
repository = LocalSourceCodeDir(path=Path(tmp_path))
assert str(repository.cache_location).startswith("/tmp")


def test_local_cache_path_home(tmp_path):
"""LocalRepository.cache_location is under home."""
repository = LocalSourceCodeDir(path=Path(tmp_path))
assert str(repository.cache_location).startswith(str(Path.home()))


def test_repository_package(tmp_path, monkeypatch):
"""LocalRepository.package() ceates package from local dir."""
cache_path = Path(tmp_path)
source_path = cache_path / "nested"
source_path.mkdir(parents=True, exist_ok=True)
(source_path / "test.txt").write_text("test")

# set cache location to temp dir
monkeypatch.setattr(LocalSourceCodeDir, "cache_location", cache_path)

repository = LocalSourceCodeDir(path=source_path)
repository.cache_location = cache_path
repository.package()

# test that package is created
Expand Down Expand Up @@ -276,8 +293,6 @@ def test_repository_lightningignore_unpackage(tmp_path, monkeypatch):
lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

cache_path = tmp_path / "cache"
monkeypatch.setattr(LocalSourceCodeDir, "cache_location", cache_path)

source_path = tmp_path / "source"
source_path.mkdir()

Expand Down Expand Up @@ -345,6 +360,7 @@ def test_repository_lightningignore_unpackage(tmp_path, monkeypatch):

# create repo object
repository = LocalSourceCodeDir(path=source_path)
repository.cache_location = cache_path
repository.package()

unpackage_path = tmp_path / "unpackage"
Expand Down

0 comments on commit 7df627b

Please sign in to comment.