progressbar.py 984 B

1234567891011121314151617181920212223242526272829303132
  1. import time
  2. def show_progress(command, step, total, since=None):
  3. progress = step * 100 // total
  4. filled = progress // 2
  5. blank = 50 - filled
  6. template = '\r%(step)s (%(progress)s%%) [%(progressbar)s]%(estimation)s'
  7. variables = {
  8. "step": str(step).rjust(len(str(total))),
  9. "progress": str(progress).rjust(3),
  10. "progressbar": "".join(['=' * filled, ' ' * blank]),
  11. "estimation": get_estimation_str(since, progress, step, total),
  12. }
  13. command.stdout.write(template % variables, ending='')
  14. command.stdout.flush()
  15. def get_estimation_str(since, progress, step, total):
  16. if not since:
  17. return ""
  18. progress_float = float(step) * 100.0 / float(total)
  19. if progress_float == 0:
  20. return ' --:--:-- est.'
  21. step_time = (time.time() - since) / progress_float
  22. estimated_time = (100 - progress) * step_time
  23. clock = time.strftime('%H:%M:%S', time.gmtime(estimated_time))
  24. return ' %s est.' % clock