Skip to content

change: ignore code cells with shell commands in v2 migration tool #1801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2020
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
6 changes: 6 additions & 0 deletions doc/v2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ You can apply it to a set of files using a loop:
Limitations
===========

Jupyter Notebook Cells with Shell Commands
------------------------------------------

If your Jupyter notebook has a code cell with lines that start with either ``%%`` or ``!``, the tool ignores that cell.
The other cells in the notebook are still updated.

Aliased Imports
---------------

Expand Down
20 changes: 19 additions & 1 deletion src/sagemaker/cli/compatibility/v2/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,30 @@ def update(self):
"""
nb_json = self._read_input_file()
for cell in nb_json["cells"]:
if cell["cell_type"] == "code":
if cell["cell_type"] == "code" and not self._contains_shell_cmds(cell):
updated_source = self._update_code_from_cell(cell)
cell["source"] = updated_source

self._write_output_file(nb_json)

def _contains_shell_cmds(self, cell):
"""Checks if the cell's source uses either ``%%`` or ``!`` to execute shell commands.

Args:
cell (dict): A dictionary representation of a code cell from
a Jupyter notebook. For more info, see
https://ipython.org/ipython-doc/dev/notebook/nbformat.html#code-cells.

Returns:
bool: If the first line starts with ``%%`` or any line starts with ``!``.
"""
source = cell["source"]

if source[0].startswith("%%"):
return True

return any(line.startswith("!") for line in source)

def _update_code_from_cell(self, cell):
"""Updates the code from a code cell so that it is
compatible with version 2.0 and later of the SageMaker Python SDK.
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/sagemaker/cli/compatibility/v2/test_file_updaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ def test_update(ast_transformer, pasta_parse, pasta_dump, json_dump):
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"!echo ignore this"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%%%%bash\\n",
"echo ignore this too"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# code to be modified\\n",
"%s"
Expand Down