convert-variable-renames.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. #
  3. # Conversion script to rename variables to versions with improved terminology.
  4. # Also highlights potentially problematic language and removed variables.
  5. #
  6. # Copyright (C) 2021 Richard Purdie
  7. # Copyright (C) 2022 Wind River Systems, Inc.
  8. #
  9. # SPDX-License-Identifier: GPL-2.0-only
  10. #
  11. import re
  12. import os
  13. import sys
  14. import tempfile
  15. import shutil
  16. import mimetypes
  17. if len(sys.argv) < 2:
  18. print("Please specify a directory to run the conversion script against.")
  19. sys.exit(1)
  20. renames = {
  21. "BB_ENV_WHITELIST" : "BB_ENV_PASSTHROUGH",
  22. "BB_ENV_EXTRAWHITE" : "BB_ENV_PASSTHROUGH_ADDITIONS",
  23. "BB_HASHCONFIG_WHITELIST" : "BB_HASHCONFIG_IGNORE_VARS",
  24. "BB_SETSCENE_ENFORCE_WHITELIST" : "BB_SETSCENE_ENFORCE_IGNORE_TASKS",
  25. "BB_HASHBASE_WHITELIST" : "BB_BASEHASH_IGNORE_VARS",
  26. "BB_HASHTASK_WHITELIST" : "BB_TASKHASH_IGNORE_TASKS",
  27. "CVE_CHECK_PN_WHITELIST" : "CVE_CHECK_SKIP_RECIPE",
  28. "CVE_CHECK_WHITELIST" : "CVE_CHECK_IGNORE",
  29. "MULTI_PROVIDER_WHITELIST" : "BB_MULTI_PROVIDER_ALLOWED",
  30. "PNBLACKLIST" : "SKIP_RECIPE",
  31. "SDK_LOCAL_CONF_BLACKLIST" : "ESDK_LOCALCONF_REMOVE",
  32. "SDK_LOCAL_CONF_WHITELIST" : "ESDK_LOCALCONF_ALLOW",
  33. "SDK_INHERIT_BLACKLIST" : "ESDK_CLASS_INHERIT_DISABLE",
  34. "SSTATE_DUPWHITELIST" : "SSTATE_ALLOW_OVERLAP_FILES",
  35. "SYSROOT_DIRS_BLACKLIST" : "SYSROOT_DIRS_IGNORE",
  36. "UNKNOWN_CONFIGURE_WHITELIST" : "UNKNOWN_CONFIGURE_OPT_IGNORE",
  37. "ICECC_USER_CLASS_BL" : "ICECC_CLASS_DISABLE",
  38. "ICECC_SYSTEM_CLASS_BL" : "ICECC_CLASS_DISABLE",
  39. "ICECC_USER_PACKAGE_WL" : "ICECC_RECIPE_ENABLE",
  40. "ICECC_USER_PACKAGE_BL" : "ICECC_RECIPE_DISABLE",
  41. "ICECC_SYSTEM_PACKAGE_BL" : "ICECC_RECIPE_DISABLE",
  42. "LICENSE_FLAGS_WHITELIST" : "LICENSE_FLAGS_ACCEPTED",
  43. }
  44. removed_list = [
  45. "BB_STAMP_WHITELIST",
  46. "BB_STAMP_POLICY",
  47. "INHERIT_BLACKLIST",
  48. "TUNEABI_WHITELIST",
  49. ]
  50. context_check_list = [
  51. "blacklist",
  52. "whitelist",
  53. "abort",
  54. ]
  55. def processfile(fn):
  56. print("processing file '%s'" % fn)
  57. try:
  58. fh, abs_path = tempfile.mkstemp()
  59. modified = False
  60. with os.fdopen(fh, 'w') as new_file:
  61. with open(fn, "r") as old_file:
  62. lineno = 0
  63. for line in old_file:
  64. lineno += 1
  65. if not line or "BB_RENAMED_VARIABLE" in line:
  66. continue
  67. # Do the renames
  68. for old_name, new_name in renames.items():
  69. if old_name in line:
  70. line = line.replace(old_name, new_name)
  71. modified = True
  72. # Find removed names
  73. for removed_name in removed_list:
  74. if removed_name in line:
  75. print("%s needs further work at line %s because %s has been deprecated" % (fn, lineno, removed_name))
  76. for check_word in context_check_list:
  77. if re.search(check_word, line, re.IGNORECASE):
  78. print("%s needs further work at line %s since it contains %s"% (fn, lineno, check_word))
  79. new_file.write(line)
  80. new_file.close()
  81. if modified:
  82. print("*** Modified file '%s'" % (fn))
  83. shutil.copymode(fn, abs_path)
  84. os.remove(fn)
  85. shutil.move(abs_path, fn)
  86. except UnicodeDecodeError:
  87. pass
  88. ourname = os.path.basename(sys.argv[0])
  89. ourversion = "0.1"
  90. if os.path.isfile(sys.argv[1]):
  91. processfile(sys.argv[1])
  92. sys.exit(0)
  93. for targetdir in sys.argv[1:]:
  94. print("processing directory '%s'" % targetdir)
  95. for root, dirs, files in os.walk(targetdir):
  96. for name in files:
  97. if name == ourname:
  98. continue
  99. fn = os.path.join(root, name)
  100. if os.path.islink(fn):
  101. continue
  102. if "ChangeLog" in fn or "/.git/" in fn or fn.endswith(".html") or fn.endswith(".patch") or fn.endswith(".m4") or fn.endswith(".diff") or fn.endswith(".orig"):
  103. continue
  104. processfile(fn)
  105. print("All files processed with version %s" % ourversion)