Created
January 25, 2011 01:34
-
-
Save samuel/794357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import division | |
import os | |
import sys | |
import time | |
def seconds_to_hms(sec): | |
return "%d:%.2d:%.2d" % (sec // (60*60), sec // 60 % 60, sec % 60) | |
def main(filename): | |
if not filename or filename == "-": | |
file_size = None | |
fp = sys.stdin | |
else: | |
file_size = os.stat(filename).st_size | |
fp = open(filename, "rb") | |
block_size = 100*1024 | |
count = 0 | |
last_count = 0 | |
last_ts = time.time() | |
start_ts = time.time() | |
while True: | |
block = fp.read(block_size) | |
if not block: | |
break | |
count += len(block) | |
sys.stdout.write(block) | |
now = time.time() | |
if now - last_ts > 5: | |
mb = count / (1024 * 1024) | |
rate = (count - last_count) / (now - last_ts) | |
mb_rate = rate / (1024 * 1024) | |
last_ts = now | |
last_count = count | |
eta = (file_size - count) / rate | |
elapsed = now - start_ts | |
if file_size: | |
done_perc = 100 * count / file_size | |
sys.stderr.write("%s %.3f MB of %.3f MB total = %.2f %% (%.3f MB/s) (ETA %s)\n" % (seconds_to_hms(elapsed), mb, file_size/(1024*1024), done_perc, mb_rate, seconds_to_hms(eta))) | |
else: | |
sys.stderr.write("%s %.3f MB (%.3f MB/s)\n" % (seconds_to_hms(elapsed), mb, mb_rate)) | |
if fp != sys.stdin: | |
fp.close() | |
if __name__ == "__main__": | |
main(sys.argv[1] if len(sys.argv[1]) > 1 else None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment