Revision b1395967 image_creator/util.py

b/image_creator/util.py
34 34
import sys
35 35
import pbs
36 36
import hashlib
37
from clint.textui import colored, progress as uiprogress
37
from clint.textui import colored
38
from progress.bar import Bar
38 39

  
39 40

  
40 41
class FatalError(Exception):
......
85 86
            sys.stdout.flush()
86 87

  
87 88

  
88
def progress(message=''):
89
def progress(message='', bar_type="default"):
89 90

  
90
    PROGRESS_LENGTH = 32
91
    MESSAGE_LENGTH = 32
91
    MESSAGE_LENGTH = 30
92
    
93
    suffix={'default':'%(index)d/%(max)d',
94
        'percent':'%(percent)d%%',
95
        'b':'%(index)d/%(max)d B',
96
        'kb':'%(index)d/%(max)d KB',
97
        'mb':'%(index)d/%(max)d MB'}
92 98

  
93
    def progress_generator(n=100):
94
        position = 0
95
        msg = message.ljust(MESSAGE_LENGTH)
96
        for i in uiprogress.bar(range(n), msg, PROGRESS_LENGTH, silent):
97
            if i < position:
98
                continue
99
            position = yield
100
        yield  # suppress the StopIteration exception
101
    return progress_generator
99
    return Bar(message=message.ljust(MESSAGE_LENGTH), fill='#', \
100
                                                    suffix=suffix[bar_type])
102 101

  
102
def md5(filename, size):
103 103

  
104
def md5(filename, size, progress=None):
105

  
106
    BLOCKSIZE = 2 ^ 22  # 4MB
104
    BLOCKSIZE = 2 ** 22  # 4MB
107 105

  
106
    progressbar = progress("Calculating md5sum:", 'mb')
107
    progressbar.max = (size // (2 ** 20))
108 108
    md5 = hashlib.md5()
109 109
    with open(filename, "r") as src:
110 110
        left = size
......
113 113
            data = src.read(length)
114 114
            md5.update(data)
115 115
            left -= length
116
            progressbar.goto((size - left) // (2 ** 20))
117
    
118
    checksum = md5.hexdigest()
119
    output("\rCalculating md5sum...\033[K", False)
120
    success(checksum)
116 121

  
117
    return md5.hexdigest()
122
    return checksum
118 123

  
119 124
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :

Also available in: Unified diff