How to pass an escape slash to subprocess.run

Question:

Trying to run command this from a Python script:

gh api "/search/code?q=somecode" --jq ".items[] | { "file": .path, "repo": .repository["full_name"] } "

by way of:

output = subprocess.run( [ 'gh', f'api "/search/code?q={CODE}"', '--jq ".items[] | {{ "file": .path, "repo": .repository[{}] }} "'.format('\"full_name\"') ])

to no avail.

I’ve also tried:

.format(r'"full_name"')

which, afaik, is supposed to work, but I get

Command '['gh', 'api "/search/code?q=somecode"', '--jq ".items[] | { "file": .path, "repo": .repository[\"full_name\"] } "']' returned non-zero exit status 1.

Using .format(repr('"full_name"')) yields:

Command '['gh', 'api "/search/code?q=staging-procore-tech-docs+org:procore"', '--jq ".items[] | { "file": .path, "repo": .repository['"full_name"'] } "']' returned non-zero exit status 1.

Thanks in advance! I’ve spent at least 3 hours reading docs, SO and asking friends

Asked By: shiruken

||

Answers:

I have likewise attempted this on my Mac OS X (M1 chip) and ran into a similar issue on my end. I had originally installed the GitHub CLI (gh) via homebrew (brew).

I have few points I would suggest fixing:

  1. Use shlex.split to (unequivocally) split multi-word arguments into a list of arguments
  2. Pass the full path to the command, which you can get by running which gh in a terminal
  3. Use an rf'' template- or f- string, where the r is for raw string so backslashes are automatically escaped

Putting all this together:

from __future__ import annotations

import shlex
import subprocess

CODE = 'somecode'

# full path to `gh` command
GH_BINARY = '/opt/homebrew/bin/gh'

cmd: list[str] = shlex.split(rf'{GH_BINARY} api "/search/code?q={CODE}" --jq ".items[] | {{ "file": .path, "repo": .repository["full_name"]}} "')
result = subprocess.run(cmd, capture_output=True)

print('Return Code:', result.returncode)
print('Output:', result.stdout.decode())

Output (redacted):

Return Code: 0
Output: {"file":"tuixiangzi-control/Debug/tuixiangzi-control.Build.CppClean.log","repo":"ChaoFeng-alone/tuixiangzi"}
{"file":"tuixiangzi-easyx/Debug/tuixiangzi-easyx.Build.CppClean.log","repo":"ChaoFeng-alone/tuixiangzi"}
...
Answered By: rv.kvetch