Skip to content

Commit e9803d3

Browse files
committed
util, osinstall: Add a progress display
Per-file, but good enough for now Signed-off-by: Hector Martin <[email protected]>
1 parent 6e04afd commit e9803d3

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/osinstall.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ def install(self, boot_obj_path):
104104
if image:
105105
p_plain(f" Extracting {image} into {info.name} partition...")
106106
logging.info(f"Extract: {image}")
107+
zinfo = self.pkg.getinfo(image)
107108
with self.pkg.open(image) as sfd, \
108109
open(f"/dev/r{info.name}", "r+b") as dfd:
109-
shutil.copyfileobj(sfd, dfd, 1024 * 1024)
110+
self.fdcopy(sfd, dfd, zinfo.file_size)
110111
self.flush_progress()
111112
source = part.get("source", None)
112113
if source:

src/util.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,46 @@ def input_prompt(*args):
118118
class PackageInstaller:
119119
def __init__(self):
120120
self.verbose = "-v" in sys.argv
121+
self.printed_progress = False
121122

122123
def flush_progress(self):
123-
if self.ucache:
124-
self.ucache.flush_progress()
124+
if self.ucache and self.ucache.flush_progress():
125+
self.printed_progress = False
126+
return
127+
if self.printed_progress:
128+
sys.stdout.write("\n")
129+
self.printed_progress = False
125130

126131
def extract(self, src, dest):
127132
logging.info(f" {src} -> {dest}/")
128133
self.pkg.extract(src, dest)
129134

135+
def fdcopy(self, sfd, dfd, size=None):
136+
BLOCK = 128 * 1024 * 1024
137+
copied = 0
138+
while True:
139+
if size is not None:
140+
prog = copied / size * 100
141+
sys.stdout.write(f"\033[3G{prog:6.2f}% ")
142+
sys.stdout.flush()
143+
self.printed_progress = True
144+
d = sfd.read(BLOCK)
145+
if not d:
146+
break
147+
dfd.write(d)
148+
copied += len(d)
149+
150+
if size is not None:
151+
sys.stdout.write("\033[3G100.00% ")
152+
sys.stdout.flush()
153+
130154
def extract_file(self, src, dest, optional=True):
131155
try:
156+
info = self.pkg.getinfo(src)
132157
with self.pkg.open(src) as sfd, \
133158
open(dest, "wb") as dfd:
134159
logging.info(f" {src} -> {dest}")
135-
shutil.copyfileobj(sfd, dfd)
160+
self.fdcopy(sfd, dfd, info.file_size)
136161
except KeyError:
137162
if not optional:
138163
raise

0 commit comments

Comments
 (0)