Skip to content

Commit 3eb3a4a

Browse files
committed
Add progress bar for both Git and Mercurial when using non-verbose mode
1 parent 9aff07b commit 3eb3a4a

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

mbed/mbed.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ def progress():
177177
sys.stdout.flush()
178178
sys.stdout.write('\b')
179179

180+
def show_progress(title, percent, max_width=80):
181+
percent = round(float(percent), 2)
182+
show_percent = '%.2f' % percent
183+
line = str(title) + ' |'
184+
bwidth = max_width - len(str(title)) - len(show_percent) - 6
185+
for i in range(0, bwidth):
186+
line += '#' if i <= (percent / 100 * bwidth) else '-'
187+
line += '| ' + show_percent + '%'
188+
sys.stdout.write(line+'\r')
189+
sys.stdout.flush()
190+
sys.stdout.write('\b')
191+
192+
def hide_progress():
193+
sys.stdout.write("\033[K\r\b")
180194

181195
# Process execution
182196
class ProcessException(Exception):
@@ -198,11 +212,11 @@ def popen(command, stdin=None, **kwargs):
198212
if proc.wait() != 0:
199213
raise ProcessException(proc.returncode, command[0], ' '.join(command), getcwd())
200214

201-
def pquery(command, stdin=None, **kwargs):
215+
def pquery(command, output_callback=None, stdin=None, **kwargs):
202216
if very_verbose:
203217
info('Query "'+' '.join(command)+'" in '+getcwd())
204218
try:
205-
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
219+
proc = subprocess.Popen(command, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
206220
except OSError as e:
207221
if e[0] == errno.ENOENT:
208222
error(
@@ -211,7 +225,21 @@ def pquery(command, stdin=None, **kwargs):
211225
else:
212226
raise e
213227

214-
stdout, _ = proc.communicate(stdin)
228+
if output_callback:
229+
line = ""
230+
while 1:
231+
s = str(proc.stderr.read(1))
232+
line += s
233+
if s == '\r' or s == '\n':
234+
output_callback(line, s)
235+
line = ""
236+
237+
if proc.returncode is None:
238+
code = proc.poll()
239+
else:
240+
break
241+
242+
stdout, stderr = proc.communicate(stdin)
215243

216244
if very_verbose:
217245
log(str(stdout).strip()+"\n")
@@ -413,7 +441,19 @@ def cleanup():
413441
return True
414442

415443
def clone(url, name=None, depth=None, protocol=None):
416-
popen([hg_cmd, 'clone', formaturl(url, protocol), name] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
444+
if verbose or very_verbose:
445+
popen([hg_cmd, 'clone', formaturl(url, protocol), name] + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
446+
else:
447+
pquery([hg_cmd, 'clone', '--config', 'progress.assume-tty=true', formaturl(url, protocol), name], output_callback=Hg.clone_progress)
448+
hide_progress()
449+
450+
def clone_progress(line, sep):
451+
m = re.match(r'(\w+).+?\s+(\d+)/(\d+)\s+.*?', line)
452+
if m:
453+
if m.group(1) == "manifests":
454+
show_progress('Downloading', (float(m.group(2)) / float(m.group(3))) * 20, 80)
455+
if m.group(1) == "files":
456+
show_progress('Downloading', (float(m.group(2)) / float(m.group(3))) * 100, 80)
417457

418458
def add(dest):
419459
info("Adding reference \"%s\"" % dest)
@@ -634,7 +674,16 @@ def cleanup():
634674
pquery([git_cmd, 'branch', '-D', branch])
635675

636676
def clone(url, name=None, depth=None, protocol=None):
637-
popen([git_cmd, 'clone', formaturl(url, protocol), name] + (['--depth', depth] if depth else []) + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
677+
if verbose or very_verbose:
678+
popen([git_cmd, 'clone', formaturl(url, protocol), name] + (['--depth', depth] if depth else []) + (['-v'] if very_verbose else ([] if verbose else ['-q'])))
679+
else:
680+
pquery([git_cmd, 'clone', '--progress', formaturl(url, protocol), name] + (['--depth', depth] if depth else []), output_callback=Git.clone_progress)
681+
hide_progress()
682+
683+
def clone_progress(line, sep):
684+
m = re.match(r'Receiving objects\:\s*(\d+)% \((\d+)/(\d+)\)', line)
685+
if m:
686+
show_progress('Downloading', (float(m.group(2)) / float(m.group(3))) * 100, 80)
638687

639688
def add(dest):
640689
info("Adding reference "+dest)

0 commit comments

Comments
 (0)