Skip to content

Commit b739fbd

Browse files
soburikartben
authored andcommitted
scripts: west_commands: sdk: Display a download progress bar.
Improved the display using tqdm to show the download progress. Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 5bc6446 commit b739fbd

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

scripts/requirements-base.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pylink-square
2323
pyserial
2424
requests
2525
semver
26+
tqdm
2627

2728
# for ram/rom reports
2829
anytree

scripts/west_commands/sdk.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import argparse
66
import hashlib
7+
import io
78
import os
89
import patoolib
910
import platform
@@ -14,6 +15,7 @@
1415
import subprocess
1516
import tempfile
1617
import textwrap
18+
import tqdm
1719
import zcmake
1820
from pathlib import Path
1921

@@ -313,10 +315,41 @@ def download_and_extract(self, base_dir, dir_name, target_release, req_headers):
313315
total_length = int(resp.headers["Content-Length"])
314316
count = 0
315317

318+
class WestLogAdapter(io.StringIO):
319+
buf = []
320+
ctx = None
321+
322+
def __init__(self, ctx):
323+
super(__class__, self).__init__()
324+
self.ctx = ctx
325+
326+
def write(self, buf):
327+
try:
328+
self.ctx.inf(buf, colorize=False, end='')
329+
except TypeError:
330+
# West v1.1.0 and earlier versions do not support the end parameter.
331+
self.ctx.inf(buf, colorize=False)
332+
333+
def flush(self):
334+
pass
335+
336+
tbar = tqdm.tqdm(
337+
total=total_length,
338+
file=WestLogAdapter(self),
339+
desc=Path(file.name).name,
340+
unit_scale=True,
341+
ncols=shutil.get_terminal_size().columns,
342+
unit="",
343+
bar_format="{desc}: {percentage:3.0f}%|{bar}| {n_fmt} {rate_fmt} [{elapsed}] ",
344+
)
345+
316346
for chunk in resp.iter_content(chunk_size=8192):
347+
tbar.update(len(chunk))
317348
file.write(chunk)
318349
count = count + len(chunk)
319-
self.inf(f"\r {count}/{total_length}", end="")
350+
351+
tbar.close()
352+
320353
self.inf()
321354
self.inf(f"Downloaded: {file.name}")
322355
file.close()

0 commit comments

Comments
 (0)