Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import string
- c_exts = ['c']
- line_prefix = ["#define", "struct", "class", "typedef"]
- beg_log_line = '__android_log_print(4,"[MOT] |BEG|","File: %s, Line: %s, Function: %s", __FILE__, __LINE__, __FUNCTION__);'
- end_log_line = '__android_log_print(4,"[MOT] |END|","File: %s, Line: %s, Function: %s", __FILE__, __LINE__, __FUNCTION__);'
- tab = " "
- def AddToFile(file):
- fd = open(file, 'r')
- lines = fd.readlines()
- fd.close()
- end_block_end = False
- func_end = False
- newlines = []
- newlines.append(lines[0])
- for i in xrange(1,len(lines)):
- #add log at functions start
- if lines[i][0] == '{':
- bool = True
- #ignore from thouse prefixes
- for prefix in line_prefix:
- if lines[i-1].startswith(prefix):
- bool = False
- #verify that we in function header.
- if lines[i-1].find("(") == -1 or lines[i-1].find("=") != -1:
- bool = False
- #add code
- if bool:
- func_end = True
- newlines.append(lines[i])
- newlines.append(tab + beg_log_line + "\n")
- continue
- #add log at the end of the function
- if func_end and lines[i][0] == '}':
- func_end = False
- newlines.append(tab + end_log_line + "\n")
- newlines.append(lines[i])
- continue
- #add log before returns
- if lines[i].lstrip().startswith("return "):
- end_block_end = True
- spaces_len = len(lines[i]) - len(lines[i].lstrip())
- spaces_prefix = lines[i][:spaces_len]
- newlines.append(spaces_prefix + "{" + "\n")
- newlines.append(spaces_prefix + tab + end_log_line + "\n")
- newlines.append(tab + lines[i])
- if lines[i].find(";") != -1:
- end_block_end = False
- newlines.append(spaces_prefix + "}" + "\n")
- continue
- if end_block_end and lines[i].find(";") != -1:
- end_block_end = False
- newlines.append(tab + lines[i])
- newlines.append(spaces_prefix + "}" + "\n")
- continue
- if end_block_end:
- newlines.append(tab + lines[i])
- continue
- newlines.append(lines[i])
- fd = open(file, 'w')
- fd.writelines(newlines)
- fd.close()
- def AddFunc(params, dirname, childs):
- for child in childs:
- if os.path.isfile(dirname + os.sep + child):
- if string.split(child,'.')[-1:][0].lower() in c_exts:
- AddToFile(dirname + os.sep + child)
- def main(args):
- if len(args) != 2:
- print "Use with directory path"
- exit(1)
- os.path.walk(args[1],AddFunc,None)
- if __name__=='__main__':
- main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement