From c605b75894383d08fd0b81e929799425f361e3f2 Mon Sep 17 00:00:00 2001 From: Lauren Yu <6631887+laurenyu@users.noreply.github.com> Date: Mon, 3 Aug 2020 15:01:11 -0700 Subject: [PATCH 1/2] change: ignore code cells with shell commands in v2 migration tool --- src/sagemaker/cli/compatibility/v2/files.py | 20 ++++++++++++++++++- .../compatibility/v2/test_file_updaters.py | 19 ++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/sagemaker/cli/compatibility/v2/files.py b/src/sagemaker/cli/compatibility/v2/files.py index 35b436f1af..45c58de1b4 100644 --- a/src/sagemaker/cli/compatibility/v2/files.py +++ b/src/sagemaker/cli/compatibility/v2/files.py @@ -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. diff --git a/tests/unit/sagemaker/cli/compatibility/v2/test_file_updaters.py b/tests/unit/sagemaker/cli/compatibility/v2/test_file_updaters.py index c30ffe29b6..5eb559cc9f 100644 --- a/tests/unit/sagemaker/cli/compatibility/v2/test_file_updaters.py +++ b/tests/unit/sagemaker/cli/compatibility/v2/test_file_updaters.py @@ -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" From 34c98f7d2cb76618432de71bb274d65aecc2f6f4 Mon Sep 17 00:00:00 2001 From: Lauren Yu <6631887+laurenyu@users.noreply.github.com> Date: Mon, 3 Aug 2020 15:04:32 -0700 Subject: [PATCH 2/2] update doc --- doc/v2.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/v2.rst b/doc/v2.rst index 73f6315156..cecf8b8672 100644 --- a/doc/v2.rst +++ b/doc/v2.rst @@ -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 ---------------