Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions davos/implementations/ipython_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"""


import textwrap
import re
import sys
import textwrap
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from subprocess import CalledProcessError

from IPython.core.error import UsageError
Expand All @@ -22,34 +24,55 @@ def _check_conda_avail_helper():
Check whether the `conda` executable is available.

`IPython` implementation of the helper function for
`davos.core.core.check_conda`. Tries to access the `conda`
executable by running `conda list IPython` using the `%conda`
IPython line magic. If successful, returns the (suppressed) stdout
generated by the command. Otherwise, returns `None`.
`davos.core.core.check_conda`. Runs a command shell (`conda list
IPython`) whose stdout contains the path to the current conda
environment, which can be parsed by the main `check_conda` function.
Uses the `%conda` IPython line magic, if available (usually if
`IPython>=7.3`). Otherwise, looks for `conda` history file and runs
some logic to determine whether -- if the `conda` exe is available,
whether `davos` is running from the base environment or not, and if
not, passes the `--prefix` to the command. If successful, returns
the (suppressed) stdout generated by the command. Otherwise, returns
`None`.

Returns
-------
str or None
If the `%conda` line magic is available and `conda list IPython`
runs successfully, the captured stdout. Otherwise, `None`.
If the command runs successfully, the captured stdout.
Otherwise, `None`.

See Also
--------
davos.core.core.check_conda : core function that calls this helper.
"""
try:
if 'conda' in set(config._ipython_shell.magics_manager.magics['line']):
# if the %conda line magic is available (IPython>=7.3), use that
# directly
try:
with redirect_stdout(StringIO()) as conda_list_output:
config._ipython_shell.run_line_magic('conda', 'list IPython')
except ValueError:
# kernel is not running within a conda environment
return None

else:
conda_history_path = Path(sys.prefix, "conda-meta", "history")
# if conda history file doesn't exist at this location, davos
# isn't running in a conda environment
if not conda_history_path.is_file():
return None

cmd = "conda list IPython"
# location we'd expect to find conda executable if davos is
# running in the 'base' environment (and no prefix is needed)
base_exe_loc = Path(sys.executable).parent.joinpath('conda')

if not base_exe_loc.is_file():
cmd += f" --prefix {sys.prefix}"

with redirect_stdout(StringIO()) as conda_list_output:
# this specific line magic seems to be the only reliable way
# to *actually* get the kernel environment -- shell (!)
# commands and all conda magic (%) commands other than
# `conda list` run in the base conda environment. Listed
# package is arbitrary, but listing single package is much
# faster than listing all, so using IPython because it's
# guaranteed to be installed
config._ipython_shell.run_line_magic('conda', 'list IPython')
except (UsageError, ValueError):
# %conda line magic is not available
return None
_run_shell_cmd(cmd)

return conda_list_output.getvalue()


Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ def terminate_active_sessions(self) -> None:
"""terminate all sessions except for the current one"""
# click "Manage Sessions" button
self.driver.find_element_by_id('ok').click()
time.sleep(3)
# get "Active sessions" popup box under first #shadow-root
active_sessions_dialog_box = self.driver.find_element_by_tag_name(
'colab-sessions-dialog'
Expand Down
4 changes: 3 additions & 1 deletion tests/test__environment_and_init.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@
"source": [
"def test_tqdm_installed():\n",
" \"\"\"used as an example package for some tests\"\"\"\n",
" assert is_installed('tqdm==4.41.1')"
" assert is_installed('tqdm')\n",
" import tqdm\n",
" assert tqdm.__version__ != '==4.45.0'"
]
},
{
Expand Down
7 changes: 3 additions & 4 deletions tests/test_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,10 @@
},
"outputs": [],
"source": [
"@mark.ipython_pre7\n",
"@mark.colab\n",
"def test_check_conda_ipython_pre7():\n",
" \"\"\"\n",
" conda is not available on Google Colaboratory and not yet supported \n",
" for IPython<7.0.0\n",
" conda is not available on Google Colaboratory\n",
" \"\"\"\n",
" davos.core.core.check_conda()\n",
" assert davos.config.conda_avail is False\n",
Expand Down Expand Up @@ -2676,7 +2675,7 @@
" the current interpreter session\n",
" \"\"\"\n",
" import tqdm\n",
" assert tqdm.__version__ == '4.41.1'\n",
" assert tqdm.__version__ != '4.45.0'\n",
" smuggle tqdm # pip: tqdm==4.45.0\n",
" assert is_installed('tqdm==4.45.0')\n",
" assert tqdm is sys.modules['tqdm']\n",
Expand Down
1 change: 0 additions & 1 deletion tests/test_implementations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@
"outputs": [],
"source": [
"@mark.jupyter\n",
"@mark.skipif(not (IPython.version_info[0] >= 7 and IPython.version_info[1] >= 3), reason=\"%conda magic not implemented\")\n",
"def test_conda_env_fset_raises_noenv():\n",
" \"\"\"\n",
" check that trying to set the `conda_env` config field to an \n",
Expand Down
38 changes: 16 additions & 22 deletions tests/test_ipython_common.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T22:43:33.461590Z",
"start_time": "2021-08-07T22:43:33.457191Z"
"end_time": "2021-08-11T03:58:39.244632Z",
"start_time": "2021-08-11T03:58:39.239990Z"
}
},
"outputs": [],
Expand All @@ -23,8 +23,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T22:43:33.694388Z",
"start_time": "2021-08-07T22:43:33.579655Z"
"end_time": "2021-08-11T03:58:39.363193Z",
"start_time": "2021-08-11T03:58:39.246862Z"
}
},
"outputs": [],
Expand All @@ -49,8 +49,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T22:43:34.155492Z",
"start_time": "2021-08-07T22:43:34.112177Z"
"end_time": "2021-08-11T03:58:39.407764Z",
"start_time": "2021-08-11T03:58:39.364819Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -78,8 +78,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T21:03:35.066508Z",
"start_time": "2021-08-07T21:03:35.062853Z"
"end_time": "2021-08-11T03:58:39.414142Z",
"start_time": "2021-08-11T03:58:39.409863Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -108,22 +108,16 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T22:45:32.547027Z",
"start_time": "2021-08-07T22:45:32.539526Z"
"end_time": "2021-08-11T03:58:39.422479Z",
"start_time": "2021-08-11T03:58:39.416239Z"
}
},
"outputs": [],
"source": [
"@mark.jupyter\n",
"@mark.skipif(not (IPython.version_info[0] >= 7 and IPython.version_info[1] >= 3), reason=\"%conda magic not implemented\")\n",
"def test_check_conda_avail_helper():\n",
" \"\"\"\n",
" test helper function for getting conda-related config fields\n",
" \n",
" NOTE: currently skipping this for IPython<7.3 because the %conda \n",
" magic wasn't implemented before then, and the way \n",
" _check_conda_avail_helper is written causes it to improperly return \n",
" None. Will remove the skip once fixed.\n",
" \"\"\"\n",
" expected_env_path = \"/usr/share/miniconda/envs/kernel-env\"\n",
" # only part of output that matters is line with environment path\n",
Expand All @@ -142,8 +136,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T21:03:35.078797Z",
"start_time": "2021-08-07T21:03:35.075174Z"
"end_time": "2021-08-11T03:58:39.429792Z",
"start_time": "2021-08-11T03:58:39.424739Z"
}
},
"outputs": [],
Expand All @@ -166,8 +160,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T21:03:35.084814Z",
"start_time": "2021-08-07T21:03:35.080457Z"
"end_time": "2021-08-11T03:58:39.438946Z",
"start_time": "2021-08-11T03:58:39.431759Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -197,8 +191,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-08-07T21:03:35.324254Z",
"start_time": "2021-08-07T21:03:35.087883Z"
"end_time": "2021-08-11T03:58:39.690163Z",
"start_time": "2021-08-11T03:58:39.441202Z"
}
},
"outputs": [],
Expand Down
Loading