Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit bebee15

Browse files
committed
Capture output on vars, but give freedom on commands defined on boost targets
1 parent 30a5379 commit bebee15

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

boostbuild/cmd/unkown.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@
77
from typing import List
88

99

10-
def win_exec(command: List[str]) -> dict:
10+
def win_exec(command: List[str], capture_output=False) -> dict:
1111
"""Execute given command.
1212
1313
This command is executed using powershell.
1414
1515
params:
1616
- command: list containing command that needs to be executed.
17+
- capture_output: capture output of executed command on stdout and stderr.
1718
1819
returns:
1920
- dict containing output of command on output key or error on error key.
2021
"""
2122
result = subprocess.run(
22-
["powershell", " ".join(command)], check=False, text=True, capture_output=True
23+
["powershell", " ".join(command)],
24+
check=False,
25+
text=True,
26+
capture_output=capture_output,
2327
)
24-
return {
25-
"error": result.stderr.rstrip().lstrip(),
26-
"output": result.stdout.rstrip().lstrip(),
27-
}
28+
output = {"code": str(result.returncode)}
29+
if capture_output:
30+
output["output"] = result.stdout.rstrip().lstrip()
31+
output["error"] = result.stderr.rstrip().lstrip()
32+
return output
2833

2934

30-
def posix_exec(command: List[str]) -> dict:
35+
def posix_exec(command: List[str], capture_output=False) -> dict:
3136
"""Execute given command.
3237
3338
This command is executed using bash.
3439
3540
params:
3641
- command: list containing command that needs to be executed.
42+
- capture_output: capture output of executed command on stdout and stderr.
3743
3844
returns:
3945
- dict containing output of command on output key or error on error key.
@@ -42,9 +48,10 @@ def posix_exec(command: List[str]) -> dict:
4248
["/bin/bash", "-c", " ".join(command)],
4349
check=False,
4450
text=True,
45-
capture_output=True,
51+
capture_output=capture_output,
4652
)
47-
return {
48-
"error": result.stderr.rstrip().lstrip(),
49-
"output": result.stdout.rstrip().lstrip(),
50-
}
53+
output = {"code": str(result.returncode)}
54+
if capture_output:
55+
output["output"] = result.stdout.rstrip().lstrip()
56+
output["error"] = result.stderr.rstrip().lstrip()
57+
return output

boostbuild/main.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def init_parser() -> argparse.ArgumentParser:
3434
return parser
3535

3636

37-
def call_command(cmd: str, args: List[str]) -> dict:
37+
def call_command(cmd: str, args: List[str], capture_output=False) -> dict:
3838
"""Execute given command.
3939
4040
Given command is dyanmically imported from cmd module.
@@ -44,6 +44,7 @@ def call_command(cmd: str, args: List[str]) -> dict:
4444
params:
4545
- cmd: command that needs to be executed.
4646
- args: command arguments.
47+
- capture_output: capture output of executed command on stdout and stderr.
4748
4849
returns:
4950
- dict containing executed command output on output key or error on error key.
@@ -59,13 +60,13 @@ def call_command(cmd: str, args: List[str]) -> dict:
5960

6061
# validate if command has implement a generic execution
6162
if hasattr(command, "generic_exec"):
62-
return command.generic_exec(args)
63+
return command.generic_exec(args, capture_output)
6364

6465
# command has different behaviour for windows/posix
6566
os_to_function = {"nt": "win_exec", "posix": "posix_exec"}
6667
try:
6768
call = os_to_function[os.name]
68-
return getattr(command, call)(args)
69+
return getattr(command, call)(args, capture_output)
6970
except KeyError:
7071
return {"error": "unsuported OS"}
7172

@@ -92,7 +93,7 @@ def get_storage(boost_data: dict, variables: List[str]) -> dict:
9293
cmd, *args = (
9394
boost_data["vars"][clean_var].replace("exec ", "").split(" ")
9495
)
95-
cmd_output = call_command(cmd, args)
96+
cmd_output = call_command(cmd, args, True)
9697
if "error" in cmd_output and cmd_output["error"]:
9798
return cmd_output
9899
value = cmd_output["output"]
@@ -148,12 +149,7 @@ def main() -> int:
148149

149150
print(Fore.GREEN + f"-> [{i + 1}/{total_commands}] - {clean_cmd}")
150151
cmd, *args = cmd.split(" ")
151-
output = call_command(cmd, args)
152-
153-
if "error" in output and output["error"]:
154-
print(Fore.RED + output["error"])
155-
if "output" in output and output["output"]:
156-
print(Fore.WHITE + output["output"])
152+
call_command(cmd, args)
157153
return 0
158154

159155

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "boostbuild"
3-
version = "0.1.11"
3+
version = "0.1.12"
44
description = "Boost is a simple build system that aims to create an interface for shell command substitution across different operative systems."
55
authors = ["David Lopez <[email protected]>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)