rmboard.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. # Bring in the patman libraries
  24. our_path = os.path.dirname(os.path.realpath(__file__))
  25. from patman import command
  26. def rm_kconfig_include(path):
  27. """Remove a path from Kconfig files
  28. This function finds the given path in a 'source' statement in a Kconfig
  29. file and removes that line from the file. This is needed because the path
  30. is going to be removed, so any reference to it will cause a problem with
  31. Kconfig parsing.
  32. The changes are made locally and then added to the git staging area.
  33. Args:
  34. path: Path to search for and remove
  35. """
  36. cmd = ['git', 'grep', path]
  37. stdout = command.RunPipe([cmd], capture=True, raise_on_error=False).stdout
  38. if not stdout:
  39. return
  40. fname = stdout.split(':')[0]
  41. print("Fixing up '%s' to remove reference to '%s'" % (fname, path))
  42. cmd = ['sed', '-i', '\|%s|d' % path, fname]
  43. stdout = command.RunPipe([cmd], capture=True).stdout
  44. cmd = ['git', 'add', fname]
  45. stdout = command.RunPipe([cmd], capture=True).stdout
  46. def rm_board(board):
  47. """Create a commit which removes a single board
  48. This looks up the MAINTAINERS file to file files that need to be removed,
  49. then removes pieces from the Kconfig files that mention the board.
  50. Args:
  51. board: Board name to remove
  52. """
  53. # Find all MAINTAINERS and Kconfig files which mention the board
  54. cmd = ['git', 'grep', '-l', board]
  55. stdout = command.RunPipe([cmd], capture=True).stdout
  56. maintain = []
  57. kconfig = []
  58. for line in stdout.splitlines():
  59. line = line.strip()
  60. if 'MAINTAINERS' in line:
  61. if line not in maintain:
  62. maintain.append(line)
  63. elif 'Kconfig' in line:
  64. kconfig.append(line)
  65. paths = []
  66. cc = []
  67. # Look through the MAINTAINERS file to find things to remove
  68. for fname in maintain:
  69. with open(fname) as fd:
  70. for line in fd:
  71. line = line.strip()
  72. fields = re.split('[ \t]', line, 1)
  73. if len(fields) == 2:
  74. if fields[0] == 'M:':
  75. cc.append(fields[1])
  76. elif fields[0] == 'F:':
  77. paths.append(fields[1].strip())
  78. # Expand any wildcards in the MAINTAINERS file
  79. real = []
  80. for path in paths:
  81. if path[-1] == '/':
  82. path = path[:-1]
  83. if '*' in path:
  84. globbed = glob.glob(path)
  85. print("Expanded '%s' to '%s'" % (path, globbed))
  86. real += globbed
  87. else:
  88. real.append(path)
  89. # Search for Kconfig files in the resulting list. Remove any 'source' lines
  90. # which reference Kconfig files we want to remove
  91. for path in real:
  92. cmd = ['find', path]
  93. stdout = (command.RunPipe([cmd], capture=True, raise_on_error=False).
  94. stdout)
  95. for fname in stdout.splitlines():
  96. if fname.endswith('Kconfig'):
  97. rm_kconfig_include(fname)
  98. # Remove unwanted files
  99. cmd = ['git', 'rm', '-r'] + real
  100. stdout = command.RunPipe([cmd], capture=True).stdout
  101. ## Change the messages as needed
  102. msg = '''arm: Remove %s board
  103. This board has not been converted to CONFIG_DM_MMC by the deadline.
  104. Remove it.
  105. ''' % board
  106. for name in cc:
  107. msg += 'Patch-cc: %s\n' % name
  108. # Create the commit
  109. cmd = ['git', 'commit', '-s', '-m', msg]
  110. stdout = command.RunPipe([cmd], capture=True).stdout
  111. # Check if the board is mentioned anywhere else. The user will need to deal
  112. # with this
  113. cmd = ['git', 'grep', '-il', board]
  114. print(command.RunPipe([cmd], capture=True, raise_on_error=False).stdout)
  115. print(' '.join(cmd))
  116. for board in sys.argv[1:]:
  117. rm_board(board)