Revision 8e3065a0 image_creator/util.py
b/image_creator/util.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
import sys |
35 | 35 |
import pbs |
36 |
import hashlib |
|
36 | 37 |
from clint.textui import colored, progress as uiprogress |
37 | 38 |
|
38 | 39 |
|
... | ... | |
84 | 85 |
sys.stdout.flush() |
85 | 86 |
|
86 | 87 |
|
87 |
def progress(label='', n=100):
|
|
88 |
def progress(message=''):
|
|
88 | 89 |
|
89 | 90 |
PROGRESS_LENGTH = 32 |
90 | 91 |
MESSAGE_LENGTH = 32 |
91 | 92 |
|
92 |
def progress_generator(label, n):
|
|
93 |
def progress_generator(n=100):
|
|
93 | 94 |
position = 0 |
94 |
for i in uiprogress.bar(range(n), label.ljust(MESSAGE_LENGTH), \
|
|
95 |
PROGRESS_LENGTH, silent):
|
|
95 |
msg = message.ljust(MESSAGE_LENGTH)
|
|
96 |
for i in uiprogress.bar(range(n), msg, PROGRESS_LENGTH, silent):
|
|
96 | 97 |
if i < position: |
97 | 98 |
continue |
98 | 99 |
position = yield |
99 | 100 |
yield # suppress the StopIteration exception |
100 |
return progress_generator(label, n) |
|
101 |
return progress_generator |
|
102 |
|
|
103 |
def md5(filename, size, progress = None): |
|
104 |
|
|
105 |
BLOCKSIZE = 2^22 # 4MB |
|
106 |
|
|
107 |
md5 = hashlib.md5() |
|
108 |
with open(filename, "r") as src: |
|
109 |
left = size |
|
110 |
while left > 0: |
|
111 |
length = min(left, BLOCKSIZE) |
|
112 |
data = src.read(length) |
|
113 |
md5.update(data) |
|
114 |
left -= length |
|
115 |
|
|
116 |
return md5.hexdigest() |
|
101 | 117 |
|
102 | 118 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
Also available in: Unified diff