Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import os
  2. import sys
  3. import string
  4.  
  5. c_exts = ['c']
  6. line_prefix = ["#define", "struct", "class", "typedef"]
  7. beg_log_line = '__android_log_print(4,"[MOT] |BEG|","File: %s, Line: %s, Function: %s", __FILE__, __LINE__, __FUNCTION__);'
  8. end_log_line = '__android_log_print(4,"[MOT] |END|","File: %s, Line: %s, Function: %s", __FILE__, __LINE__, __FUNCTION__);'
  9. tab = " "
  10. def AddToFile(file):
  11. fd = open(file, 'r')
  12. lines = fd.readlines()
  13. fd.close()
  14.  
  15. end_block_end = False
  16. func_end = False
  17. newlines = []
  18. newlines.append(lines[0])
  19. for i in xrange(1,len(lines)):
  20. #add log at functions start
  21. if lines[i][0] == '{':
  22. bool = True
  23. #ignore from thouse prefixes
  24. for prefix in line_prefix:
  25. if lines[i-1].startswith(prefix):
  26. bool = False
  27. #verify that we in function header.
  28. if lines[i-1].find("(") == -1 or lines[i-1].find("=") != -1:
  29. bool = False
  30. #add code
  31. if bool:
  32. func_end = True
  33. newlines.append(lines[i])
  34. newlines.append(tab + beg_log_line + "\n")
  35. continue
  36.  
  37. #add log at the end of the function
  38. if func_end and lines[i][0] == '}':
  39. func_end = False
  40. newlines.append(tab + end_log_line + "\n")
  41. newlines.append(lines[i])
  42. continue
  43.  
  44. #add log before returns
  45. if lines[i].lstrip().startswith("return "):
  46. end_block_end = True
  47. spaces_len = len(lines[i]) - len(lines[i].lstrip())
  48. spaces_prefix = lines[i][:spaces_len]
  49. newlines.append(spaces_prefix + "{" + "\n")
  50. newlines.append(spaces_prefix + tab + end_log_line + "\n")
  51. newlines.append(tab + lines[i])
  52. if lines[i].find(";") != -1:
  53. end_block_end = False
  54. newlines.append(spaces_prefix + "}" + "\n")
  55. continue
  56. if end_block_end and lines[i].find(";") != -1:
  57. end_block_end = False
  58. newlines.append(tab + lines[i])
  59. newlines.append(spaces_prefix + "}" + "\n")
  60. continue
  61. if end_block_end:
  62. newlines.append(tab + lines[i])
  63. continue
  64. newlines.append(lines[i])
  65.  
  66. fd = open(file, 'w')
  67. fd.writelines(newlines)
  68. fd.close()
  69.  
  70. def AddFunc(params, dirname, childs):
  71. for child in childs:
  72. if os.path.isfile(dirname + os.sep + child):
  73. if string.split(child,'.')[-1:][0].lower() in c_exts:
  74. AddToFile(dirname + os.sep + child)
  75.  
  76. def main(args):
  77. if len(args) != 2:
  78. print "Use with directory path"
  79. exit(1)
  80. os.path.walk(args[1],AddFunc,None)
  81. if __name__=='__main__':
  82.  
  83. main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement