Skip to content

Commit 095bd1e

Browse files
committed
fix: apply black formatting and fix CodeQL url.startswith alert
Replace url.startswith() assertions with urllib.parse.urlparse() checks to satisfy CodeQL py/incomplete-url-substring-sanitization rule. Reformat both shell.py and test_shell.py with black.
1 parent c62f890 commit 095bd1e

2 files changed

Lines changed: 256 additions & 129 deletions

File tree

centml/cli/shell.py

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def _resolve_pod(cclient, deployment_id, pod_name=None):
5252
"""
5353
status = cclient.get_status_v3(deployment_id)
5454
running_pods = []
55-
for revision in (status.revision_pod_details_list or []):
56-
for pod in (revision.pod_details_list or []):
55+
for revision in status.revision_pod_details_list or []:
56+
for pod in revision.pod_details_list or []:
5757
if pod.status == PodStatus.RUNNING and pod.name:
5858
running_pods.append(pod.name)
5959

@@ -122,12 +122,16 @@ def _on_stdin_ready():
122122
except asyncio.TimeoutError:
123123
continue
124124
rows, cols = shutil.get_terminal_size()
125-
await ws.send(json.dumps({
126-
"operation": "stdin",
127-
"data": data.decode("utf-8", errors="replace"),
128-
"rows": rows,
129-
"cols": cols,
130-
}))
125+
await ws.send(
126+
json.dumps(
127+
{
128+
"operation": "stdin",
129+
"data": data.decode("utf-8", errors="replace"),
130+
"rows": rows,
131+
"cols": cols,
132+
}
133+
)
134+
)
131135
finally:
132136
loop.remove_reader(stdin_fd)
133137

@@ -157,21 +161,31 @@ async def _interactive_session(ws_url, token):
157161

158162
headers = {"Authorization": f"Bearer {token}"}
159163
async with websockets.connect(ws_url, additional_headers=headers) as ws:
160-
await ws.send(json.dumps({
161-
"operation": "resize",
162-
"rows": rows,
163-
"cols": cols,
164-
}))
164+
await ws.send(
165+
json.dumps(
166+
{
167+
"operation": "resize",
168+
"rows": rows,
169+
"cols": cols,
170+
}
171+
)
172+
)
165173

166174
loop = asyncio.get_running_loop()
167175

168176
def _on_resize():
169177
r, c = shutil.get_terminal_size()
170-
asyncio.ensure_future(ws.send(json.dumps({
171-
"operation": "resize",
172-
"rows": r,
173-
"cols": c,
174-
})))
178+
asyncio.ensure_future(
179+
ws.send(
180+
json.dumps(
181+
{
182+
"operation": "resize",
183+
"rows": r,
184+
"cols": c,
185+
}
186+
)
187+
)
188+
)
175189

176190
loop.add_signal_handler(signal.SIGWINCH, _on_resize)
177191

@@ -211,11 +225,15 @@ async def _exec_session(ws_url, token, command):
211225
headers = {"Authorization": f"Bearer {token}"}
212226

213227
async with websockets.connect(ws_url, additional_headers=headers) as ws:
214-
await ws.send(json.dumps({
215-
"operation": "resize",
216-
"rows": rows,
217-
"cols": cols,
218-
}))
228+
await ws.send(
229+
json.dumps(
230+
{
231+
"operation": "resize",
232+
"rows": rows,
233+
"cols": cols,
234+
}
235+
)
236+
)
219237

220238
# Suppress echo/bracketed-paste, emit begin marker, run command,
221239
# emit end marker with exit code, then exit.
@@ -230,12 +248,16 @@ async def _exec_session(ws_url, token, command):
230248
f" exit $__ec\n"
231249
)
232250

233-
await ws.send(json.dumps({
234-
"operation": "stdin",
235-
"data": wrapped,
236-
"rows": rows,
237-
"cols": cols,
238-
}))
251+
await ws.send(
252+
json.dumps(
253+
{
254+
"operation": "stdin",
255+
"data": wrapped,
256+
"rows": rows,
257+
"cols": cols,
258+
}
259+
)
260+
)
239261

240262
exit_code = 0
241263
buffer = ""
@@ -275,9 +297,16 @@ async def _exec_session(ws_url, token, command):
275297

276298
@click.command(help="Open an interactive shell to a deployment pod")
277299
@click.argument("deployment_id", type=int)
278-
@click.option("--pod", default=None, help="Specific pod name (auto-selects first running pod)")
279-
@click.option("--shell", "shell_type", default=None,
280-
type=click.Choice(["bash", "sh", "zsh"]), help="Shell type")
300+
@click.option(
301+
"--pod", default=None, help="Specific pod name (auto-selects first running pod)"
302+
)
303+
@click.option(
304+
"--shell",
305+
"shell_type",
306+
default=None,
307+
type=click.Choice(["bash", "sh", "zsh"]),
308+
help="Shell type",
309+
)
281310
@handle_exception
282311
def shell(deployment_id, pod, shell_type):
283312
if not sys.stdin.isatty():
@@ -286,25 +315,36 @@ def shell(deployment_id, pod, shell_type):
286315
with get_centml_client() as cclient:
287316
pod_name = _resolve_pod(cclient, deployment_id, pod)
288317

289-
ws_url = _build_ws_url(settings.CENTML_PLATFORM_API_URL, deployment_id, pod_name, shell_type)
318+
ws_url = _build_ws_url(
319+
settings.CENTML_PLATFORM_API_URL, deployment_id, pod_name, shell_type
320+
)
290321
token = auth.get_centml_token()
291322
exit_code = asyncio.run(_interactive_session(ws_url, token))
292323
sys.exit(exit_code)
293324

294325

295-
@click.command(help="Execute a command in a deployment pod",
296-
context_settings=dict(ignore_unknown_options=True))
326+
@click.command(
327+
help="Execute a command in a deployment pod",
328+
context_settings=dict(ignore_unknown_options=True),
329+
)
297330
@click.argument("deployment_id", type=int)
298331
@click.argument("command", nargs=-1, required=True, type=click.UNPROCESSED)
299332
@click.option("--pod", default=None, help="Specific pod name")
300-
@click.option("--shell", "shell_type", default=None,
301-
type=click.Choice(["bash", "sh", "zsh"]), help="Shell type")
333+
@click.option(
334+
"--shell",
335+
"shell_type",
336+
default=None,
337+
type=click.Choice(["bash", "sh", "zsh"]),
338+
help="Shell type",
339+
)
302340
@handle_exception
303341
def exec_cmd(deployment_id, command, pod, shell_type):
304342
with get_centml_client() as cclient:
305343
pod_name = _resolve_pod(cclient, deployment_id, pod)
306344

307-
ws_url = _build_ws_url(settings.CENTML_PLATFORM_API_URL, deployment_id, pod_name, shell_type)
345+
ws_url = _build_ws_url(
346+
settings.CENTML_PLATFORM_API_URL, deployment_id, pod_name, shell_type
347+
)
308348
token = auth.get_centml_token()
309349
cmd_str = " ".join(command)
310350
exit_code = asyncio.run(_exec_session(ws_url, token, cmd_str))

0 commit comments

Comments
 (0)