Skip to content

Commit

Permalink
Configure version checker settings by plugins (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov authored Nov 23, 2021
1 parent 1d08478 commit 71bf051
Show file tree
Hide file tree
Showing 19 changed files with 595 additions and 364 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.D/2405.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Configure version checker settings by plugins.
3 changes: 3 additions & 0 deletions neuro-cli/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ install_requires =
rich>=10.0.1
packaging>=20.0
jedi>=0.16
importlib-metadata>=3.6; python_version<"3.10"

[options.entry_points]
console_scripts =
neuro = neuro_cli.main:main
docker-credential-neuro = neuro_cli.docker_credential_helper:main
neuro_api =
neuro-cli=neuro_cli.plugin:setup

[options.packages.find]
where=src
Expand Down
38 changes: 38 additions & 0 deletions neuro-cli/src/neuro_cli/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from neuro_sdk import ConfigScope, PluginManager

NEURO_CLI_UPGRADE = """\
You are using Neuro Platform Client {old_ver}, however {new_ver} is available.
You should consider upgrading via the following command:
python -m pip install --upgrade neuro-cli
"""


def get_neuro_cli_txt(old: str, new: str) -> str:
return NEURO_CLI_UPGRADE.format(old_ver=old, new_ver=new)


CERTIFI_UPGRADE = """\
Your root certificates are out of date.
You are using certifi {old_ver}, however {new_ver} is available.
Please consider upgrading certifi package, e.g.:
python -m pip install --upgrade certifi
or
conda update certifi
"""


def get_certifi_txt(old: str, new: str) -> str:
return CERTIFI_UPGRADE.format(old_ver=old, new_ver=new)


def setup(manager: PluginManager) -> None:
# Setup config options
manager.config.define_str("job", "ps-format")
manager.config.define_str("job", "top-format")
manager.config.define_str("job", "life-span")
manager.config.define_str("job", "cluster-name", scope=ConfigScope.LOCAL)
manager.config.define_str_list("storage", "cp-exclude")
manager.config.define_str_list("storage", "cp-exclude-from-files")

manager.version_checker.register("neuro-cli", get_neuro_cli_txt)
manager.version_checker.register("certifi", get_certifi_txt, delay=14 * 3600 * 24)
4 changes: 2 additions & 2 deletions neuro-cli/src/neuro_cli/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from rich.text import Text as RichText

from neuro_sdk import Client, ConfigError, Factory, gen_trace_id
from neuro_sdk.config import _ConfigData, load_user_config
from neuro_sdk.config import _ConfigData

from .asyncio_utils import Runner

Expand Down Expand Up @@ -190,7 +190,7 @@ async def get_user_config(self) -> Mapping[str, Any]:
try:
client = await self.init_client()
except ConfigError:
return load_user_config(self.config_path.expanduser())
return await self.factory.load_user_config()
else:
return await client.config.get_user_config()

Expand Down
15 changes: 11 additions & 4 deletions neuro-cli/src/neuro_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from .parse_utils import parse_timedelta
from .root import Root
from .stats import upload_gmp_stats
from .version_utils import run_version_checker

log = logging.getLogger(__name__)

Expand All @@ -58,9 +57,17 @@ async def _run_async_function(
if init_client:
await root.init_client()

pypi_task: "asyncio.Task[None]" = loop.create_task(
run_version_checker(root.client, root.disable_pypi_version_check)
)
if not root.disable_pypi_version_check:
msgs = await root.client.version_checker.get_outdated()
for msg in msgs.values():
root.err_console.print(msg, style="yellow")

pypi_task: "asyncio.Task[None]" = loop.create_task(
root.client.version_checker.update()
)
else:
pypi_task = loop.create_task(asyncio.sleep(0)) # do nothing

stats_task: "asyncio.Task[None]" = loop.create_task(
upload_gmp_stats(
root.client, root.command_path, root.command_params, root.skip_gmp_stats
Expand Down
207 changes: 0 additions & 207 deletions neuro-cli/src/neuro_cli/version_utils.py

This file was deleted.

15 changes: 12 additions & 3 deletions neuro-cli/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from jose import jwt
from yarl import URL

from neuro_sdk import Client, Cluster, Preset, __version__
from neuro_sdk import Client, Cluster, PluginManager, Preset, __version__
from neuro_sdk.config import _AuthConfig, _AuthToken, _ConfigData, _save
from neuro_sdk.tracing import _make_trace_config

Expand Down Expand Up @@ -114,7 +114,8 @@ def go(
registry_url: str = "https://registry-dev.neu.ro",
trace_id: str = "bd7a977555f6b982",
clusters: Optional[Dict[str, Cluster]] = None,
token_url: Optional[URL] = None
token_url: Optional[URL] = None,
plugin_manager: Optional[PluginManager] = None,
) -> Client:
url = URL(url_str)
if clusters is None:
Expand Down Expand Up @@ -155,6 +156,8 @@ def go(
real_auth_config = replace(auth_config, token_url=token_url)
else:
real_auth_config = auth_config
if plugin_manager is None:
plugin_manager = PluginManager()
config = _ConfigData(
auth_config=real_auth_config,
auth_token=_AuthToken.create_non_expiring(token),
Expand All @@ -167,6 +170,12 @@ def go(
config_dir = tmp_path / ".neuro"
_save(config, config_dir)
session = aiohttp.ClientSession(trace_configs=[_make_trace_config()])
return Client._create(session, config_dir, trace_id)
return Client._create(
session,
config_dir,
trace_id,
None,
plugin_manager=plugin_manager,
)

return go
14 changes: 12 additions & 2 deletions neuro-cli/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
JobRestartPolicy,
JobStatus,
JobStatusHistory,
PluginManager,
RemoteImage,
Resources,
SecretFile,
Expand Down Expand Up @@ -232,8 +233,13 @@ async def test_calc_top_columns_section_doesnt_exist(
async def test_calc_ps_columns_user_spec(
monkeypatch: Any, tmp_path: Path, make_client: _MakeClient
) -> None:
plugin_manager = PluginManager()
plugin_manager.config.define_str("job", "ps-format")

async with make_client("https://example.com") as client:
async with make_client(
"https://example.com",
plugin_manager=plugin_manager,
) as client:
monkeypatch.chdir(tmp_path)
local_conf = tmp_path / ".neuro.toml"
# empty config
Expand All @@ -247,8 +253,12 @@ async def test_calc_ps_columns_user_spec(
async def test_calc_top_columns_user_spec(
monkeypatch: Any, tmp_path: Path, make_client: _MakeClient
) -> None:
plugin_manager = PluginManager()
plugin_manager.config.define_str("job", "top-format")

async with make_client("https://example.com") as client:
async with make_client(
"https://example.com", plugin_manager=plugin_manager
) as client:
monkeypatch.chdir(tmp_path)
local_conf = tmp_path / ".neuro.toml"
# empty config
Expand Down
Loading

0 comments on commit 71bf051

Please sign in to comment.