Revision d603d80d image_creator/main.py

b/image_creator/main.py
38 38
from image_creator import FatalError
39 39
from image_creator.disk import Disk
40 40
from image_creator.util import get_command, error, progress_generator, success
41
from clint.textui import puts, indent
42
from sendfile import sendfile
41
from clint.textui import puts
43 42

  
44 43
import sys
45 44
import os
......
108 107
    return options
109 108

  
110 109

  
111
def extract_image(device, outfile, size):
112
    blocksize = 4194304  # 4MB
113
    progress_size = (size + 1048575) // 1048576  # in MB
114
    progressbar = progress_generator("Dumping image file: ",
115
                                                    progress_size)
116
    source = open(device, "r")
117
    try:
118
        dest = open(outfile, "w")
119
        try:
120
            left = size
121
            offset = 0
122
            progressbar.next()
123
            while left > 0:
124
                length = min(left, blocksize)
125
                sent = sendfile(dest.fileno(), source.fileno(), offset, length)
126
                offset += sent
127
                left -= sent
128
                for i in range(4):
129
                    progressbar.next()
130
        finally:
131
            dest.close()
132
    finally:
133
        source.close()
134

  
135
    success('Image file %s was successfully created' % outfile)
136

  
137

  
138 110
def image_creator():
139 111
    puts('snf-image-creator %s\n' % version)
140 112
    options = parse_options(sys.argv[1:])
......
180 152
            finally:
181 153
                f.close()
182 154

  
183
            extract_image(dev.device, options.outfile, size)
155
            dev.dump(options.outfile)
184 156
    finally:
185 157
        puts('cleaning up...')
186 158
        disk.cleanup()

Also available in: Unified diff