rmboard.py 4.4 KB

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