rmboard.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #! /usr/bin/python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright 2019 Google LLC
  4. #
  5. """
  6. Script to remove boards
  7. Usage:
  8. rmboard.py <board_name>...
  9. A single commit is created for each board removed.
  10. Some boards may depend on files provided by another and this will cause
  11. problems, generally the removal of files which should not be removed.
  12. This script works by:
  13. - Looking through the MAINTAINERS files which mention a board to find out
  14. what files the board uses
  15. - Looking through the Kconfig files which mention a board to find one that
  16. needs to have material removed
  17. Search for ## to update the commit message manually.
  18. """
  19. import glob
  20. import os
  21. import re
  22. import sys
  23. from patman import command
  24. def rm_kconfig_include(path):
  25. """Remove a path from Kconfig files
  26. This function finds the given path in a 'source' statement in a Kconfig
  27. file and removes that line from the file. This is needed because the path
  28. is going to be removed, so any reference to it will cause a problem with
  29. Kconfig parsing.
  30. The changes are made locally and then added to the git staging area.
  31. Args:
  32. path: Path to search for and remove
  33. """
  34. cmd = ['git', 'grep', path]
  35. stdout = command.RunPipe([cmd], capture=True, raise_on_error=False).stdout
  36. if not stdout:
  37. return
  38. fname = stdout.split(':')[0]
  39. print("Fixing up '%s' to remove reference to '%s'" % (fname, path))
  40. cmd = ['sed', '-i', '\|%s|d' % path, fname]
  41. stdout = command.RunPipe([cmd], capture=True).stdout
  42. cmd = ['git', 'add', fname]
  43. stdout = command.RunPipe([cmd], capture=True).stdout
  44. def rm_board(board):
  45. """Create a commit which removes a single board
  46. This looks up the MAINTAINERS file to file files that need to be removed,
  47. then removes pieces from the Kconfig files that mention the board.
  48. Args:
  49. board: Board name to remove
  50. """
  51. # Find all MAINTAINERS and Kconfig files which mention the board
  52. cmd = ['git', 'grep', '-l', board]
  53. stdout = command.RunPipe([cmd], capture=True).stdout
  54. maintain = []
  55. kconfig = []
  56. for line in stdout.splitlines():
  57. line = line.strip()
  58. if 'MAINTAINERS' in line:
  59. if line not in maintain:
  60. maintain.append(line)
  61. elif 'Kconfig' in line:
  62. kconfig.append(line)
  63. paths = []
  64. cc = []
  65. # Look through the MAINTAINERS file to find things to remove
  66. for fname in maintain:
  67. with open(fname) as fd:
  68. for line in fd:
  69. line = line.strip()
  70. fields = re.split('[ \t]', line, 1)
  71. if len(fields) == 2:
  72. if fields[0] == 'M:':
  73. cc.append(fields[1])
  74. elif fields[0] == 'F:':
  75. paths.append(fields[1].strip())
  76. # Expand any wildcards in the MAINTAINERS file
  77. real = []
  78. for path in paths:
  79. if path[-1] == '/':
  80. path = path[:-1]
  81. if '*' in path:
  82. globbed = glob.glob(path)
  83. print("Expanded '%s' to '%s'" % (path, globbed))
  84. real += globbed
  85. else:
  86. real.append(path)
  87. # Search for Kconfig files in the resulting list. Remove any 'source' lines
  88. # which reference Kconfig files we want to remove
  89. for path in real:
  90. cmd = ['find', path]
  91. stdout = (command.RunPipe([cmd], capture=True, raise_on_error=False).
  92. stdout)
  93. for fname in stdout.splitlines():
  94. if fname.endswith('Kconfig'):
  95. rm_kconfig_include(fname)
  96. # Remove unwanted files
  97. cmd = ['git', 'rm', '-r'] + real
  98. stdout = command.RunPipe([cmd], capture=True).stdout
  99. ## Change the messages as needed
  100. msg = '''arm: Remove %s board
  101. This board has not been converted to CONFIG_DM_MMC by the deadline.
  102. Remove it.
  103. ''' % board
  104. for name in cc:
  105. msg += 'Patch-cc: %s\n' % name
  106. # Create the commit
  107. cmd = ['git', 'commit', '-s', '-m', msg]
  108. stdout = command.RunPipe([cmd], capture=True).stdout
  109. # Check if the board is mentioned anywhere else. The user will need to deal
  110. # with this
  111. cmd = ['git', 'grep', '-il', board]
  112. print(command.RunPipe([cmd], capture=True, raise_on_error=False).stdout)
  113. print(' '.join(cmd))
  114. for board in sys.argv[1:]:
  115. rm_board(board)