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