Skip to content

Commit 432d150

Browse files
committed
Improve: better build progress
1 parent da74324 commit 432d150

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

build.py

+19-32
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
print("Docker is not running. Please start it and try again.")
4848
exit(1)
4949

50+
# make a fresh build.log for this build
51+
with open("build.log", "w") as f:
52+
f.write("")
53+
5054
# Per the [docs](https://pypi.org/project/python-dotenv/), load `.env` into
5155
# environment variables.
5256
load_dotenv()
@@ -99,8 +103,9 @@
99103
)
100104

101105
if finish:
102-
print(
103-
"Your environment is not set up correctly. Please define the environment variables listed above."
106+
console.print(
107+
"Your environment is not set up correctly. Please define the environment variables listed above.",
108+
style="bold red",
104109
)
105110
exit(1)
106111

@@ -156,7 +161,7 @@
156161

157162
# Generate a table for the Live object
158163
# see https://rich.readthedocs.io/en/stable/live.html?highlight=update#basic-usage
159-
def generate_table(status):
164+
def generate_table(status: dict) -> Table:
160165
table = Table(title="Build Docker Images")
161166
table.add_column("Service", justify="right", style="cyan", no_wrap=True)
162167
table.add_column("Built", style="magenta")
@@ -178,40 +183,22 @@ def generate_table(status):
178183
status[service] = "building"
179184
lt.update(generate_table(status))
180185
with open("build.log", "ab") as f:
181-
ret = stream_command(
182-
"docker",
183-
"compose",
184-
"build",
185-
service,
186-
"--progress",
187-
"plain",
188-
# For stdout, stream only high points of the build to stdout; save
189-
# *everything* to the log file.
190-
stdout_streamer=subprocess_streamer(
191-
sys.stdout.buffer,
192-
f,
193-
filter=lambda line: (
194-
# Only show lines to stdout like `#18 naming to
195-
# docker.io/library/rs-jobe 0.2s done`; just report the
196-
# container name for brevity.
197-
b"Finished "
198-
+ line[line.rindex(b"/") + 1 :].replace(b" done", b"").strip()
199-
+ b"\n"
200-
if b"naming to docker.io" in line and b"done" in line
201-
else b"",
202-
# Save everything to the file.
203-
line,
204-
),
205-
),
206-
stderr_streamer=subprocess_streamer(sys.stderr.buffer, f),
186+
ret = subprocess.run(
187+
["docker", "compose", "build", service, "--progress", "plain"],
188+
capture_output=True,
207189
)
208-
if ret == 0:
190+
f.write(ret.stdout)
191+
f.write(ret.stderr)
192+
if ret.returncode == 0:
209193
status[service] = "built"
210194
lt.update(generate_table(status))
211195
else:
212196
status[service] = "failed"
213197
lt.update(generate_table(status))
214-
print(f"There was an error building {service} see build.log for details")
198+
console.print(
199+
f"There was an error building {service} see build.log for details",
200+
style="bold red",
201+
)
215202
exit(1)
216203

217204
# read the version from pyproject.toml
@@ -246,4 +233,4 @@ def generate_table(status):
246233
print(f"{image} failed to push")
247234
exit(1)
248235

249-
print("Docker images pushed successfully")
236+
console.print("Docker images pushed successfully", style="green")

components/rsptx/cl_utils/core.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,20 @@ def __init__(
111111
self,
112112
# The path to change to upon entering the context manager.
113113
path: str | Path,
114+
verbose: bool = False,
114115
):
115116
self.path = path
117+
self.verbose = verbose
116118

117119
def __enter__(self) -> None:
118-
flush_print("pushd {}".format(self.path))
120+
if self.verbose:
121+
flush_print("pushd {}".format(self.path))
119122
self.cwd = os.getcwd()
120123
os.chdir(str(self.path))
121124

122125
def __exit__(self, type_, value, traceback) -> Literal[False]:
123-
flush_print("popd - returning to {}.".format(self.cwd))
126+
if self.verbose:
127+
flush_print("popd - returning to {}.".format(self.cwd))
124128
os.chdir(str(self.cwd))
125129
return False
126130

0 commit comments

Comments
 (0)