22from dockerfile_generator import render
33import os
44import json
5+ from tqdm import tqdm
56
67from typing import Union , Any , Optional
78
@@ -23,9 +24,13 @@ def push_image(tag: str) -> None:
2324 """
2425 client = docker .from_env ()
2526 print (f"Pushing image: { tag } " )
26- progress = ""
27- for line in client .images .push (tag , stream = True ):
28- print ("\r " + push_image_formatted (line ), end = "" , flush = True )
27+ with tqdm (total = 100 , ascii = False ) as progress_bar :
28+ last_percent = 0.0
29+ for line in client .images .push (tag , stream = True ):
30+ percent = get_completion_percentage (line )
31+ if percent :
32+ progress_bar .update (percent - last_percent )
33+ last_percent = percent
2934
3035
3136def retag_image (
@@ -64,28 +69,27 @@ def retag_image(
6469 client .images .push (new_repo_url , new_tag )
6570
6671
67- def push_image_formatted (line : Any ) -> str :
72+ def get_completion_percentage (line : Any ) -> float :
6873 try :
6974 line = json .loads (line .strip ().decode ("utf-8" ))
7075 except ValueError :
71- return ""
76+ return 0
7277
7378 to_skip = ("Preparing" , "Waiting" , "Layer already exists" )
7479 if "status" in line :
7580 if line ["status" ] in to_skip :
76- return ""
81+ return 0
7782 if line ["status" ] == "Pushing" :
7883 try :
79- current = int (line ["progressDetail" ]["current" ])
80- total = int (line ["progressDetail" ]["total" ])
84+ current = float (line ["progressDetail" ]["current" ])
85+ total = float (line ["progressDetail" ]["total" ])
8186 except KeyError :
82- return ""
83- progress = current / total
84- if progress > 1.0 :
85- progress == 1.0
86- return "Complete: {:.1%}\n " .format (progress )
87-
88- return ""
87+ return 0
88+ result = (current / total ) * 100
89+ if result > 100.0 :
90+ return 100.0
91+ return result
92+ return 0
8993
9094
9195def build_and_push_image (repo_url : str , tag : str , path : str , image_type : str ) -> None :
0 commit comments