Commit ccd6cd44be4fb7fa1d61242412acad7884564c41

Authored by Christian Herdtweck
Committed by Philippe Lagadec
1 parent eafe4d92

xls_parser: introduce minimal logging

Showing 1 changed file with 13 additions and 14 deletions
oletools/xls_parser.py
... ... @@ -28,8 +28,6 @@ Read storages, (sub-)streams, records from xls file
28 28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30  
31   -from __future__ import print_function
32   -
33 31 #------------------------------------------------------------------------------
34 32 # CHANGELOG:
35 33 # 2017-11-02 v0.01 CH: - first version
... ... @@ -55,6 +53,7 @@ import sys
55 53 import os.path
56 54 from struct import unpack
57 55 from io import SEEK_CUR
  56 +import logging
58 57  
59 58 # little hack to allow absolute imports even if oletools is not installed.
60 59 # Copied from olevba.py
... ... @@ -108,7 +107,7 @@ class XlsFile(olefile.OleFileIO):
108 107  
109 108 def get_streams(self):
110 109 """ find all streams, including orphans """
111   - print('Finding streams in ole file')
  110 + logging.debug('Finding streams in ole file')
112 111  
113 112 for sid, direntry in enumerate(self.direntries):
114 113 is_orphan = direntry is None
... ... @@ -116,12 +115,10 @@ class XlsFile(olefile.OleFileIO):
116 115 # this direntry is not part of the tree --> unused or orphan
117 116 direntry = self._load_direntry(sid)
118 117 is_stream = direntry.entry_type == olefile.STGTY_STREAM
119   - print('direntry {:2d} {}: {}'
120   - .format(sid, '[orphan]' if is_orphan else direntry.name,
121   - 'is stream of size {}'.format(direntry.size)
122   - if is_stream else
123   - 'no stream ({})'
124   - .format(ENTRY_TYPE2STR[direntry.entry_type])))
  118 + logging.debug('direntry {:2d} {}: {}'.format(
  119 + sid, '[orphan]' if is_orphan else direntry.name,
  120 + 'is stream of size {}'.format(direntry.size) if is_stream else
  121 + 'no stream ({})'.format(ENTRY_TYPE2STR[direntry.entry_type])))
125 122 if is_stream:
126 123 if direntry.name == 'Workbook':
127 124 clz = WorkbookStream
... ... @@ -154,7 +151,7 @@ class WorkbookStream(XlsStream):
154 151 def iter_records(self, fill_data=False):
155 152 """ iterate over records in streams"""
156 153 if self.stream.tell() != 0:
157   - print('have to jump to start')
  154 + logging.debug('have to jump to start')
158 155 self.stream.seek(0)
159 156  
160 157 while True:
... ... @@ -386,21 +383,23 @@ def read_unicode(data, start_idx, n_chars):
386 383  
387 384 def test(*filenames):
388 385 """ parse all given file names and print rough structure """
  386 + logging.basicConfig(level=logging.DEBUG)
389 387 if not filenames:
390   - print('need file name[s]')
  388 + logging.info('need file name[s]')
391 389 return 2
392 390 for filename in filenames:
  391 + logging.info('checking file {0}'.format(filename))
393 392 if not olefile.isOleFile(filename):
394 393 continue
395 394 xls = XlsFile(filename)
396 395  
397 396 for stream in xls.get_streams():
398   - print(stream)
  397 + logging.info(stream)
399 398 if isinstance(stream, WorkbookStream):
400 399 for record in stream.iter_records():
401   - print(' {0}'.format(record))
  400 + logging.info(' {0}'.format(record))
402 401 return 0
403 402  
404 403  
405 404 if __name__ == '__main__':
406   - sys.exit(test(sys.argv[1:]))
  405 + sys.exit(test(*sys.argv[1:]))
... ...