Skip to content

Commit c605b75

Browse files
committed
change: ignore code cells with shell commands in v2 migration tool
1 parent c4bb695 commit c605b75

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/sagemaker/cli/compatibility/v2/files.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,30 @@ def update(self):
121121
"""
122122
nb_json = self._read_input_file()
123123
for cell in nb_json["cells"]:
124-
if cell["cell_type"] == "code":
124+
if cell["cell_type"] == "code" and not self._contains_shell_cmds(cell):
125125
updated_source = self._update_code_from_cell(cell)
126126
cell["source"] = updated_source
127127

128128
self._write_output_file(nb_json)
129129

130+
def _contains_shell_cmds(self, cell):
131+
"""Checks if the cell's source uses either ``%%`` or ``!`` to execute shell commands.
132+
133+
Args:
134+
cell (dict): A dictionary representation of a code cell from
135+
a Jupyter notebook. For more info, see
136+
https://ipython.org/ipython-doc/dev/notebook/nbformat.html#code-cells.
137+
138+
Returns:
139+
bool: If the first line starts with ``%%`` or any line starts with ``!``.
140+
"""
141+
source = cell["source"]
142+
143+
if source[0].startswith("%%"):
144+
return True
145+
146+
return any(line.startswith("!") for line in source)
147+
130148
def _update_code_from_cell(self, cell):
131149
"""Updates the code from a code cell so that it is
132150
compatible with version 2.0 and later of the SageMaker Python SDK.

tests/unit/sagemaker/cli/compatibility/v2/test_file_updaters.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ def test_update(ast_transformer, pasta_parse, pasta_dump, json_dump):
9595
"execution_count": 1,
9696
"metadata": {},
9797
"outputs": [],
98+
"source": [
99+
"!echo ignore this"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 2,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"%%%%bash\\n",
109+
"echo ignore this too"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 3,
115+
"metadata": {},
116+
"outputs": [],
98117
"source": [
99118
"# code to be modified\\n",
100119
"%s"

0 commit comments

Comments
 (0)