I'm trying to write a Python script which should basically act as a text parsing command line tool. The intent of the script is to search for the function name which is provided as the input argument to the script and also the functions which call the function (my input argument) and give the script names containing the functions along with the line numbers as the output.
I have figured out how to get where the input function name is called in the scripts but don't know how to get the name of the functions which call the actual function that I'm searching for. Can anyone let me know how to proceed?
import os, fnmatch
searchStr = "PressButton"
directory = 'C:\TFSMap\FalconPlatformTests\GLib'
# in which file to search.
pattern = "*_Main.php"
a = {}
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
for dir in dirs:
find_files(directory+'/'+dir, pattern)
for filename in find_files(directory,pattern):
i = 0
with open(filename) as Sid:
values = ''
for line in Sid:
i = i +1
if searchStr in line:
if filename in a:
values = a[filename]
a[filename] = values +','+ i.__str__()
else:
values = i.__str__()
a[filename] = i.__str__()
if values != "":
print (filename+' ===== '+values)