target device. """
# Create and setup the output stream
- output = logging.getLogger('bmap-create-output')
- output.setLevel(logging.INFO)
if args.output:
- where = logging.FileHandler(args.output)
+ try:
+ output = open(args.output, "w+")
+ except IOError as err:
+ log.error("cannot open the output file '%s': %s" \
+ % (args.output, err))
+ raise SystemExit(1)
else:
- where = logging.StreamHandler(sys.stdout)
- output.addHandler(where)
+ output = sys.stdout
try:
creator = BmapCreate.BmapCreate(args.image, output)
<!-- The block map which consists of elements which may either be a
range of blocks or a single block. The 'sha1' attribute (if present)
is the SHA1 checksum of this blocks range. -->
- <BlockMap>"""
+ <BlockMap>
+"""
class Error(Exception):
""" A class for exceptions generated by the 'BmapCreate' module. We
create an instance of 'BmapCreate' and provide:
* full path to the image to create bmap for
- * a logger object to output the generated bmap to
+ * file-like object to write the output to
Then you should invoke the 'generate()' method of this class. It will use
the FIEMAP ioctl to generate the bmap, and fall-back to the FIBMAP ioctl if
def __init__(self, image_path, output):
""" Initialize a class instance:
* image_path - full path to the image file to generate bmap for
- * output - a logger object to write the generated bmap to """
+ * output - file-like object to write the generated bmap to """
self._image_path = image_path
self._output = output
self.bmap_image_size_human, self.bmap_image_size,
self.bmap_block_size, self.bmap_blocks_cnt)
- self._output.info(xml)
+ self._output.write(xml)
def _is_mapped_fibmap(self, block):
""" A helper function which returns 'True' if block number 'block' of
% (self.bmap_mapped_size_human, self.bmap_mapped_percent)
xml += " <MappedBlocksCount> %u </MappedBlocksCount>\n" \
% self.bmap_mapped_cnt
- xml += "</bmap>"
+ xml += "</bmap>\n"
- self._output.info(xml)
+ self._output.write(xml)
def _calculate_sha1(self, first, last):
""" A helper function which calculates SHA1 checksum for the range of
sha1 = ""
if first != last:
- self._output.info(" <Range%s> %s-%s </Range>" \
- % (sha1, first, last))
+ self._output.write(" <Range%s> %s-%s </Range>\n" \
+ % (sha1, first, last))
else:
- self._output.info(" <Range%s> %s </Range>" \
- % (sha1, first))
+ self._output.write(" <Range%s> %s </Range>\n" \
+ % (sha1, first))
self.bmap_mapped_size = self.bmap_mapped_cnt * self.bmap_block_size
self.bmap_mapped_size_human = human_size(self.bmap_mapped_size)
self.bmap_mapped_percent = self.bmap_mapped_cnt * 100.0
self.bmap_mapped_percent /= self.bmap_blocks_cnt
+
self._bmap_file_end()
+ self._output.flush()
+
def __del__(self):
""" The class destructor which closes the opened files. """