check_deprecated.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8 compliant>
  19. import os
  20. from os.path import splitext
  21. DEPRECATE_DAYS = 120
  22. SKIP_DIRS = ("extern",
  23. "tests", # not this dir
  24. )
  25. def is_c_header(filename):
  26. ext = splitext(filename)[1]
  27. return (ext in {".h", ".hpp", ".hxx", ".hh"})
  28. def is_c(filename):
  29. ext = splitext(filename)[1]
  30. return (ext in {".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl"})
  31. def is_c_any(filename):
  32. return is_c(filename) or is_c_header(filename)
  33. def is_py(filename):
  34. ext = splitext(filename)[1]
  35. return (ext == ".py")
  36. def is_source_any(filename):
  37. return is_c_any(filename) or is_py(filename)
  38. def source_list(path, filename_check=None):
  39. for dirpath, dirnames, filenames in os.walk(path):
  40. # skip '.git'
  41. dirnames[:] = [d for d in dirnames if not d.startswith(".")]
  42. for filename in filenames:
  43. if filename_check is None or filename_check(filename):
  44. yield os.path.join(dirpath, filename)
  45. def deprecations():
  46. """
  47. Searches out source code for lines like
  48. /* *DEPRECATED* 2011/7/17 bgl.Buffer.list info text */
  49. Or...
  50. # *DEPRECATED* 2010/12/22 some.py.func more info */
  51. """
  52. import datetime
  53. SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.normpath(os.path.join(os.path.dirname(__file__), ".."))))
  54. SKIP_DIRS_ABS = [os.path.join(SOURCE_DIR, p) for p in SKIP_DIRS]
  55. deprecations_ls = []
  56. scan_tot = 0
  57. print("scanning in %r for '*DEPRECATED* YYYY/MM/DD info'" % SOURCE_DIR)
  58. for fn in source_list(SOURCE_DIR, is_source_any):
  59. # print(fn)
  60. skip = False
  61. for p in SKIP_DIRS_ABS:
  62. if fn.startswith(p):
  63. skip = True
  64. break
  65. if skip:
  66. continue
  67. file = open(fn, 'r', encoding="utf8")
  68. for i, l in enumerate(file):
  69. # logic for deprecation warnings
  70. if '*DEPRECATED*' in l:
  71. try:
  72. l = l.strip()
  73. data = l.split('*DEPRECATED*', 1)[-1].strip().strip()
  74. data = [w.strip() for w in data.split('/', 2)]
  75. data[-1], info = data[-1].split(' ', 1)
  76. info = info.split("*/", 1)[0]
  77. if len(data) != 3:
  78. print(" poorly formatting line:\n"
  79. " %r:%d\n"
  80. " %s" %
  81. (fn, i + 1, l)
  82. )
  83. else:
  84. data = datetime.datetime(*tuple([int(w) for w in data]))
  85. deprecations_ls.append((data, (fn, i + 1), info))
  86. except:
  87. print("Error file - %r:%d" % (fn, i + 1))
  88. import traceback
  89. traceback.print_exc()
  90. scan_tot += 1
  91. print(" scanned %d files" % scan_tot)
  92. return deprecations_ls
  93. def main():
  94. import datetime
  95. now = datetime.datetime.now()
  96. deps = deprecations()
  97. print("\nAll deprecations...")
  98. for data, fileinfo, info in deps:
  99. days_old = (now - data).days
  100. if days_old > DEPRECATE_DAYS:
  101. info = "*** REMOVE! *** " + info
  102. print(" %r, days-old(%.2d), %s:%d - %s" % (data, days_old, fileinfo[0], fileinfo[1], info))
  103. if deps:
  104. print("\ndone!")
  105. else:
  106. print("\nnone found!")
  107. if __name__ == '__main__':
  108. main()