From: Artem Bityutskiy Date: Tue, 20 Nov 2012 11:19:33 +0000 (+0200) Subject: BmapCreate.py: stop using logger for the bmap output X-Git-Tag: v1.0~91 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9298d7392d023522426f7ec84874015e411605a3;p=tools%2Fbmap-tools.git BmapCreate.py: stop using logger for the bmap output It is an overkill to use a logger object to output the bmap, and it is also rather difficult for the users. Instead, make 'BmapCreate' accept a file-like object for the output, not a logger object. Change-Id: I7cf83dc8e0c43f762449e7fcf200b1f8b66359c5 Signed-off-by: Artem Bityutskiy --- diff --git a/bmaptool b/bmaptool index 040d136..6c6cf8d 100755 --- a/bmaptool +++ b/bmaptool @@ -141,13 +141,15 @@ def create_command(args, log): 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) diff --git a/bmaptools/BmapCreate.py b/bmaptools/BmapCreate.py index 44caf57..a8151eb 100644 --- a/bmaptools/BmapCreate.py +++ b/bmaptools/BmapCreate.py @@ -74,7 +74,8 @@ _BMAP_START_TEMPLATE = \ - """ + +""" class Error(Exception): """ A class for exceptions generated by the 'BmapCreate' module. We @@ -95,7 +96,7 @@ class BmapCreate: 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 @@ -104,7 +105,7 @@ class BmapCreate: 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 @@ -178,7 +179,7 @@ class BmapCreate: 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 @@ -272,9 +273,9 @@ class BmapCreate: % (self.bmap_mapped_size_human, self.bmap_mapped_percent) xml += " %u \n" \ % self.bmap_mapped_cnt - xml += "" + xml += "\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 @@ -329,18 +330,21 @@ class BmapCreate: sha1 = "" if first != last: - self._output.info(" %s-%s " \ - % (sha1, first, last)) + self._output.write(" %s-%s \n" \ + % (sha1, first, last)) else: - self._output.info(" %s " \ - % (sha1, first)) + self._output.write(" %s \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. """