Skip to content

Commit d4978b4

Browse files
yurijmikhalevichethanwharris
authored andcommitted
fix(cloud): detect and ignore venv (#16056)
Co-authored-by: Ethan Harris <[email protected]> (cherry picked from commit 3b323c8)
1 parent 806d035 commit d4978b4

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/lightning_app/runners/cloud.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ def dispatch(
230230
else:
231231
ignore_functions = None
232232

233+
# Create a default dotignore if it doesn't exist
234+
if not (root / DOT_IGNORE_FILENAME).is_file():
235+
with open(root / DOT_IGNORE_FILENAME, "w") as f:
236+
f.write("venv/\n")
237+
if (root / "bin" / "activate").is_file() or (root / "pyvenv.cfg").is_file():
238+
# the user is developing inside venv
239+
f.write("bin/\ninclude/\nlib/\npyvenv.cfg\n")
240+
233241
repo = LocalSourceCodeDir(path=root, ignore_functions=ignore_functions)
234242
self._check_uploaded_folder(root, repo)
235243
requirements_file = root / "requirements.txt"
@@ -556,15 +564,10 @@ def _check_uploaded_folder(root: Path, repo: LocalSourceCodeDir) -> None:
556564
f"Your application folder '{root.absolute()}' is more than {CLOUD_UPLOAD_WARNING} MB. "
557565
f"The total size is {round(app_folder_size_in_mb, 2)} MB. {len(files)} files were uploaded.\n"
558566
+ largest_paths_msg
559-
+ "Perhaps you should try running the app in an empty directory."
567+
+ "Perhaps you should try running the app in an empty directory.\n"
568+
+ "You can ignore some files or folders by adding them to `.lightningignore`.\n"
569+
+ " You can also set the `self.lightningingore` attribute in a Flow or Work."
560570
)
561-
if not (root / DOT_IGNORE_FILENAME).is_file():
562-
warning_msg += (
563-
"\nIn order to ignore some files or folder, create a `.lightningignore` file and add the paths to"
564-
" ignore. You can also set the `lightningingore` attribute in a Flow or Work."
565-
)
566-
else:
567-
warning_msg += "\nYou can ignore some files or folders by adding them to `.lightningignore`."
568571

569572
logger.warn(warning_msg)
570573

tests/tests_app/runners/test_cloud.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ def test_check_uploaded_folder(monkeypatch, tmpdir, caplog):
13341334
assert "The total size is 15.0 MB" in caplog.text
13351335
assert "3 files were uploaded" in caplog.text
13361336
assert "files:\n6.0 MB: c.jpg\n5.0 MB: b.txt\n4.0 MB: a.png\nPerhaps" in caplog.text # tests the order
1337-
assert "create a `.lightningignore` file" in caplog.text
1337+
assert "adding them to `.lightningignore`." in caplog.text
13381338
assert "lightningingore` attribute in a Flow or Work" in caplog.text
13391339

13401340

@@ -1571,6 +1571,56 @@ def run(self):
15711571
flow.run()
15721572

15731573

1574+
def test_default_lightningignore(monkeypatch, caplog, tmpdir):
1575+
mock_client = mock.MagicMock()
1576+
mock_client.projects_service_list_memberships.return_value = V1ListMembershipsResponse(
1577+
memberships=[V1Membership(name="test-project", project_id="test-project-id")]
1578+
)
1579+
mock_client.lightningapp_instance_service_list_lightningapp_instances.return_value = (
1580+
V1ListLightningappInstancesResponse(lightningapps=[])
1581+
)
1582+
mock_client.lightningapp_v2_service_create_lightningapp_release.return_value = V1LightningappRelease(
1583+
cluster_id="test"
1584+
)
1585+
cloud_backend = mock.MagicMock(client=mock_client)
1586+
monkeypatch.setattr(backends, "CloudBackend", mock.MagicMock(return_value=cloud_backend))
1587+
1588+
class MyWork(LightningWork):
1589+
def run(self):
1590+
pass
1591+
1592+
app = LightningApp(MyWork())
1593+
1594+
path = Path(tmpdir)
1595+
cloud_runtime = cloud.CloudRuntime(app=app, entrypoint_file=path / "entrypoint.py")
1596+
monkeypatch.setattr(LocalSourceCodeDir, "upload", mock.MagicMock())
1597+
1598+
# write some files
1599+
write_file_of_size(path / "a.txt", 5 * 1000 * 1000)
1600+
write_file_of_size(path / "venv" / "foo.txt", 4 * 1000 * 1000)
1601+
1602+
assert not (path / ".lightningignore").exists()
1603+
1604+
with mock.patch(
1605+
"lightning_app.runners.cloud._parse_lightningignore", wraps=_parse_lightningignore
1606+
) as parse_mock, mock.patch(
1607+
"lightning_app.source_code.local._copytree", wraps=_copytree
1608+
) as copy_mock, caplog.at_level(
1609+
logging.WARN
1610+
):
1611+
cloud_runtime.dispatch()
1612+
1613+
parse_mock.assert_called_once_with(())
1614+
assert copy_mock.mock_calls[0].kwargs["ignore_functions"][0].args[1] == set()
1615+
1616+
assert (path / ".lightningignore").exists()
1617+
1618+
assert f"Your application folder '{path.absolute()}' is more than 2 MB" in caplog.text
1619+
assert "The total size is 5.0 MB" in caplog.text
1620+
assert "2 files were uploaded" # a.txt and .lightningignore
1621+
assert "files:\n5.0 MB: a.txt\nPerhaps" in caplog.text # only this file appears
1622+
1623+
15741624
@pytest.mark.parametrize(
15751625
"lightning_app_instance, lightning_cloud_url, expected_url",
15761626
[

0 commit comments

Comments
 (0)