-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update * update * update * update * update * update --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: thomas <[email protected]> (cherry picked from commit 4c2fc3b)
- Loading branch information
Showing
15 changed files
with
111 additions
and
26 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from contextlib import contextmanager | ||
from subprocess import Popen | ||
from typing import Any | ||
|
||
from lightning.data.constants import _IS_IN_STUDIO | ||
|
||
|
||
@contextmanager | ||
def optimize_dns_context(enable: bool) -> Any: | ||
optimize_dns(enable) | ||
try: | ||
yield | ||
optimize_dns(False) # always disable the optimize DNS | ||
except Exception as e: | ||
optimize_dns(False) # always disable the optimize DNS | ||
raise e | ||
|
||
def optimize_dns(enable: bool) -> None: | ||
if not _IS_IN_STUDIO: | ||
return | ||
|
||
with open("/etc/resolv.conf") as f: | ||
lines = f.readlines() | ||
|
||
if ( | ||
(enable and any("127.0.0.53" in line for line in lines)) | ||
or (not enable and any("127.0.0.1" in line for line in lines)) | ||
): # noqa E501 | ||
Popen(f"sudo /home/zeus/miniconda3/envs/cloudspace/bin/python -c 'from lightning.data.processing.dns import _optimize_dns; _optimize_dns({enable})'", shell=True).wait() # noqa E501 | ||
|
||
def _optimize_dns(enable: bool) -> None: | ||
with open("/etc/resolv.conf") as f: | ||
lines = f.readlines() | ||
|
||
write_lines = [] | ||
for line in lines: | ||
if "nameserver 127" in line: | ||
if enable: | ||
write_lines.append('nameserver 127.0.0.1\n') | ||
else: | ||
write_lines.append('nameserver 127.0.0.53\n') | ||
else: | ||
write_lines.append(line) | ||
|
||
with open("/etc/resolv.conf", "w") as f: | ||
for line in write_lines: | ||
f.write(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from unittest.mock import MagicMock | ||
|
||
from lightning.data.processing import dns as dns_module | ||
from lightning.data.processing.dns import optimize_dns_context | ||
|
||
|
||
def test_optimize_dns_context(monkeypatch): | ||
popen_mock = MagicMock() | ||
|
||
monkeypatch.setattr(dns_module, "_IS_IN_STUDIO", True) | ||
monkeypatch.setattr(dns_module, "Popen", popen_mock) | ||
|
||
class FakeFile: | ||
|
||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, *args, **kwargs): | ||
return self | ||
|
||
def readlines(self): | ||
return ["127.0.0.53"] | ||
|
||
monkeypatch.setitem(__builtins__, "open", MagicMock(return_value=FakeFile())) | ||
|
||
with optimize_dns_context(True): | ||
pass | ||
|
||
cmd = popen_mock._mock_call_args_list[0].args[0] | ||
assert cmd == "sudo /home/zeus/miniconda3/envs/cloudspace/bin/python -c 'from lightning.data.processing.dns import _optimize_dns; _optimize_dns(True)'" # noqa: E501 |