47
47
print ("Docker is not running. Please start it and try again." )
48
48
exit (1 )
49
49
50
+ # make a fresh build.log for this build
51
+ with open ("build.log" , "w" ) as f :
52
+ f .write ("" )
53
+
50
54
# Per the [docs](https://pypi.org/project/python-dotenv/), load `.env` into
51
55
# environment variables.
52
56
load_dotenv ()
99
103
)
100
104
101
105
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" ,
104
109
)
105
110
exit (1 )
106
111
156
161
157
162
# Generate a table for the Live object
158
163
# 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 :
160
165
table = Table (title = "Build Docker Images" )
161
166
table .add_column ("Service" , justify = "right" , style = "cyan" , no_wrap = True )
162
167
table .add_column ("Built" , style = "magenta" )
@@ -178,40 +183,22 @@ def generate_table(status):
178
183
status [service ] = "building"
179
184
lt .update (generate_table (status ))
180
185
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 ,
207
189
)
208
- if ret == 0 :
190
+ f .write (ret .stdout )
191
+ f .write (ret .stderr )
192
+ if ret .returncode == 0 :
209
193
status [service ] = "built"
210
194
lt .update (generate_table (status ))
211
195
else :
212
196
status [service ] = "failed"
213
197
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
+ )
215
202
exit (1 )
216
203
217
204
# read the version from pyproject.toml
@@ -246,4 +233,4 @@ def generate_table(status):
246
233
print (f"{ image } failed to push" )
247
234
exit (1 )
248
235
249
- print ("Docker images pushed successfully" )
236
+ console . print ("Docker images pushed successfully" , style = "green " )
0 commit comments