MCPcopy Index your code
hub / github.com/dbcli/mycli / execute_system_command

Function execute_system_command

mycli/packages/special/iocommands.py:447–506  ·  view source on GitHub ↗

Execute a system shell command.

(arg: str, **_)

Source from the content-addressed store, hash-verified

445 "Execute a system shell command (raw mode with -r).",
446)
447def execute_system_command(arg: str, **_) -> list[SQLResult]:
448 """Execute a system shell command."""
449 usage = "Syntax: system [-r] [command].\n-r denotes \"raw\" mode, in which output is passed through without formatting."
450
451 IMPLICIT_RAW_MODE_COMMANDS = {
452 'clear',
453 'vim',
454 'vi',
455 'bash',
456 'zsh',
457 }
458
459 if not arg.strip():
460 return [SQLResult(status=usage)]
461
462 try:
463 command = shlex.split(arg.strip(), posix=not WIN)
464 except ValueError as e:
465 return [SQLResult(status=f"Cannot parse system command: {e}")]
466
467 raw = False
468 if command[0] == '-r':
469 command.pop(0)
470 raw = True
471 elif command[0].lower() in IMPLICIT_RAW_MODE_COMMANDS:
472 raw = True
473
474 if not command:
475 return [SQLResult(status=usage)]
476
477 if command[0].lower() == 'cd':
478 ok, error_message = handle_cd_command(command)
479 if not ok:
480 return [SQLResult(status=error_message)]
481 return [SQLResult()]
482
483 try:
484 if raw:
485 completed_process = subprocess.run(command, check=False)
486 if completed_process.returncode:
487 return [SQLResult(status=f'Command exited with return code {completed_process.returncode}')]
488 else:
489 return [SQLResult()]
490 else:
491 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
492 try:
493 output, error = process.communicate(timeout=60)
494 except subprocess.TimeoutExpired:
495 process.kill()
496 output, error = process.communicate()
497 response = output if not error else error
498 encoding = locale.getpreferredencoding(False)
499 response_str = response.decode(encoding)
500 if process.returncode:
501 status = f'Command exited with return code {process.returncode}'
502 else:
503 status = None
504 return [SQLResult(preamble=response_str, status=status)]

Callers

nothing calls this directly

Calls 6

SQLResultClass · 0.90
handle_cd_commandFunction · 0.90
splitMethod · 0.80
communicateMethod · 0.80
killMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected