rmboard.py 4.4 KB

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