Skip to content

Commit a68786c

Browse files
log subprocess output
1 parent 9415d37 commit a68786c

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

app/get_knowledge_base.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
2-
import shutil
32
import subprocess
43
import re
54
import json
5+
import logging
66
from git import Repo
77

88
kb_file_path = './data/knowledge_base.json'
@@ -32,21 +32,43 @@ def setup_repositories():
3232
for repo_name, repo_url in repos.items():
3333
clone_repository(repo_url, os.path.join(tmp_dir, repo_name))
3434

35+
def run_command(command, label):
36+
"""Helper function to run a command and log its output"""
37+
process = subprocess.Popen(
38+
command,
39+
stdout=subprocess.PIPE,
40+
stderr=subprocess.PIPE,
41+
text=True,
42+
bufsize=1,
43+
universal_newlines=True
44+
)
45+
46+
# log stdout
47+
for line in iter(process.stdout.readline, ''):
48+
if line.strip():
49+
logging.info(f"{label}: {line.rstrip()}")
50+
51+
# log stderr
52+
for line in iter(process.stderr.readline, ''):
53+
if line.strip():
54+
logging.error(f"{label}: {line.rstrip()}")
55+
56+
# Wait for process to complete and check return code
57+
return_code = process.wait()
58+
if return_code != 0:
59+
raise subprocess.CalledProcessError(return_code, command)
60+
3561
def run_prebuild_script():
3662
""" Run the defang-docs repo prebuild script"""
3763

38-
subprocess.run(
64+
run_command(
3965
["npm", "-C", ".tmp/defang-docs", "install"],
40-
check=True,
41-
stdout=subprocess.PIPE,
42-
stderr=subprocess.PIPE
66+
"npm install"
4367
)
4468

45-
subprocess.run(
69+
run_command(
4670
["npm", "-C", ".tmp/defang-docs", "run", "prebuild"],
47-
check=True,
48-
stdout=subprocess.PIPE,
49-
stderr=subprocess.PIPE
71+
"npm run prebuild"
5072
)
5173

5274
def parse_markdown():

0 commit comments

Comments
 (0)