Files
bizhang_-obav/Tools/px4params/srcscanner.py

49 lines
1.7 KiB
Python
Raw Normal View History

2013-10-18 03:47:15 +04:00
import os
import re
2014-02-16 01:47:11 +01:00
import codecs
import sys
2013-10-18 03:47:15 +04:00
class SourceScanner(object):
2013-10-18 03:47:15 +04:00
"""
Traverses directory tree, reads all source files, and passes their contents
to the Parser.
"""
def ScanDir(self, srcdirs, parser):
2013-10-18 03:47:15 +04:00
"""
Scans provided path and passes all found contents to the parser using
parser.Parse method.
"""
2015-08-28 11:06:12 +02:00
extensions1 = tuple([".h"])
extensions2 = tuple([".cpp", ".c"])
for srcdir in srcdirs:
for dirname, dirnames, filenames in os.walk(srcdir):
for filename in filenames:
if filename.endswith(extensions1):
path = os.path.join(dirname, filename)
if not self.ScanFile(path, parser):
return False
for filename in filenames:
if filename.endswith(extensions2):
path = os.path.join(dirname, filename)
if not self.ScanFile(path, parser):
return False
2015-07-22 09:01:31 -07:00
return True
2013-10-18 03:47:15 +04:00
def ScanFile(self, path, parser):
"""
Scans provided file and passes its contents to the parser using
parser.Parse method.
"""
prefix = "^.*" + os.path.sep + "src" + os.path.sep
2016-05-05 14:19:37 -04:00
scope = re.sub(prefix.replace("\\", "/"), "", os.path.dirname(os.path.relpath(path)).replace("\\", "/"))
2014-02-16 01:47:11 +01:00
with codecs.open(path, 'r', 'utf-8') as f:
2014-12-23 14:34:53 +01:00
try:
contents = f.read()
except:
contents = ''
print('Failed reading file: %s, skipping content.' % path)
pass
return parser.Parse(scope, contents)