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