Print Linux Device Tree and optionally search for strings within that tree. Requires the top level dts file as input. #linux #python
#!/usr/bin/python
from __future__ import print_function
import os, sys, getopt
searchstring = ""
def usage():
print (os.path.basename(sys.argv[0]) + ' [-s search_string] -i <inputfile>')
exit()
def printLine(input, depth):
print(depth * '\t', end="")
print(input)
def readFile(dir, filename, depth):
# Print file name
printLine (filename, depth)
# Slurp file
with open(dir + "/" + filename) as f:
lines = f.read().splitlines()
# Print matching lines
map(lambda x: printLine(str(lines.index(x)) + ": " + x, depth), filter(lambda x: searchstring in
x, lines))
# Go through included files
for include_file in [x.split()[1].replace('"', '') for x in lines if
"/include/" in x]:
readFile(dir, include_file, depth+1)
def main(argv):
inputfile = ""
global searchstring
try:
opts, args = getopt.getopt(argv, "hi:s:")
except getopt.GetoptError:
usage()
for opt, arg in opts:
print (arg)
if opt == '-h':
usage()
elif (opt == "-i"):
inputfile = arg
elif (opt == "-s"):
searchstring = arg
if inputfile == "":
usage()
readFile(os.path.dirname(inputfile), os.path.basename(inputfile), 0)
if __name__ == "__main__":
main(sys.argv[1:])