@@ -177,6 +177,20 @@ def progress():
177
177
sys .stdout .flush ()
178
178
sys .stdout .write ('\b ' )
179
179
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 " )
180
194
181
195
# Process execution
182
196
class ProcessException (Exception ):
@@ -198,11 +212,11 @@ def popen(command, stdin=None, **kwargs):
198
212
if proc .wait () != 0 :
199
213
raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), getcwd ())
200
214
201
- def pquery (command , stdin = None , ** kwargs ):
215
+ def pquery (command , output_callback = None , stdin = None , ** kwargs ):
202
216
if very_verbose :
203
217
info ('Query "' + ' ' .join (command )+ '" in ' + getcwd ())
204
218
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 )
206
220
except OSError as e :
207
221
if e [0 ] == errno .ENOENT :
208
222
error (
@@ -211,7 +225,21 @@ def pquery(command, stdin=None, **kwargs):
211
225
else :
212
226
raise e
213
227
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 )
215
243
216
244
if very_verbose :
217
245
log (str (stdout ).strip ()+ "\n " )
@@ -413,7 +441,19 @@ def cleanup():
413
441
return True
414
442
415
443
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 )
417
457
418
458
def add (dest ):
419
459
info ("Adding reference \" %s\" " % dest )
@@ -634,7 +674,16 @@ def cleanup():
634
674
pquery ([git_cmd , 'branch' , '-D' , branch ])
635
675
636
676
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 )
638
687
639
688
def add (dest ):
640
689
info ("Adding reference " + dest )
0 commit comments