Skip to content

Commit f4de6f7

Browse files
authored
Merge pull request #572 from xcp-ng/srt/xo-cli
the PR is instroducing some xo_cli changes: - use TOOLS variable in data.py to get the path of xo-cli command-line : it permits to use custom path. the default is to use xo_cli as currently - add logging.debug() when xo-cli is invoked in order to trace it (by using local_cmd) - simplify xo_cli function and unify simple_output and use_json arguments
2 parents 3638b63 + 8d974f9 commit f4de6f7

2 files changed

Lines changed: 20 additions & 37 deletions

File tree

data.py-dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ OTHER_GUEST_TOOLS = {
116116
# Tools
117117
TOOLS: dict[str, str] = {
118118
# "iso-remaster": "/home/user/src/xcpng/xcp/scripts/iso-remaster/iso-remaster.sh",
119+
# "xo-cli": "xo-cli",
119120
}
120121

121122
# Values can be either full URLs or only partial URLs that will be automatically appended to DEF_VM_URL

lib/xo.py

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
11
import json
2-
import subprocess
32

3+
from data import TOOLS
4+
from lib.commands import local_cmd
45
from lib.typing import JSONType
56

6-
from typing import Dict, Literal, overload
7+
from typing import Literal, overload
78

8-
# TODO: either:
9-
# * replace simple_output and use_json by a single output type
10-
# * make sure that simple_output=False and use_json=True are not being used together
11-
12-
@overload
13-
def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[True] = True,
14-
use_json: Literal[False] = False) -> str:
15-
...
16-
@overload
17-
def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[True] = True,
18-
use_json: Literal[True]) -> JSONType:
19-
...
209
@overload
21-
def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: Literal[False],
22-
use_json: bool = False) -> subprocess.CompletedProcess[bytes]:
10+
def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: Literal[False] = False) -> str:
2311
...
2412
@overload
25-
def xo_cli(action: str, args: Dict[str, str] = {}, *, check: bool = True, simple_output: bool = True,
26-
use_json: bool = False) -> subprocess.CompletedProcess[bytes] | JSONType | str:
13+
def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: Literal[True]) -> JSONType:
2714
...
28-
def xo_cli(
29-
action: str, args: dict[str, str] = {}, check: bool = True, simple_output: bool = True, use_json: bool = False
30-
) -> subprocess.CompletedProcess[bytes] | JSONType | str:
31-
run_array = ['xo-cli', action]
15+
16+
def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: bool = False) -> JSONType | str:
17+
cmd = [TOOLS.get('xo-cli', 'xo-cli'), action]
3218
if use_json:
33-
run_array += ['--json']
34-
run_array += ["%s=%s" % (key, value) for key, value in args.items()]
35-
res = subprocess.run(
36-
run_array,
37-
stdout=subprocess.PIPE,
38-
stderr=subprocess.STDOUT,
39-
check=check
40-
)
41-
if simple_output:
42-
output = res.stdout.decode().strip()
43-
if use_json:
44-
return json.loads(output)
45-
return output
46-
return res
19+
cmd += ['--json']
20+
cmd += ["%s=%s" % (key, value) for key, value in args.items()]
21+
22+
res = local_cmd(cmd, check=check)
23+
24+
if use_json:
25+
return json.loads(res.stdout)
26+
27+
return res.stdout
4728

4829
def xo_object_exists(uuid: str) -> bool:
49-
lst = json.loads(xo_cli('--list-objects', {'uuid': uuid}))
30+
lst = xo_cli('list-objects', {'uuid': uuid}, use_json=True)
31+
assert isinstance(lst, list)
5032
return len(lst) > 0

0 commit comments

Comments
 (0)