+ def dump(self, outfile):
+ """Dumps the content of device into a file.
+
+ This method will only dump the actual payload, found by reading the
+ partition table. Empty space in the end of the device will be ignored.
+ """
+ blocksize = 2 ** 22 # 4MB
+ size = self.size()
+ progress_size = (size + 2 ** 20 - 1) // 2 ** 20 # in MB
+ progressbar = progress_generator("Dumping image file: ", progress_size)
+
+ source = open(self.device, "r")
+ try:
+ dest = open(outfile, "w")
+ try:
+ left = size
+ offset = 0
+ progressbar.next()
+ while left > 0:
+ length = min(left, blocksize)
+ sent = sendfile(dest.fileno(), source.fileno(), offset,
+ length)
+ offset += sent
+ left -= sent
+ for i in range((length + 2 ** 20 - 1) // 2 ** 20):
+ progressbar.next()
+ finally:
+ dest.close()
+ finally:
+ source.close()
+
+ success('Image file %s was successfully created' % outfile)
+