blob: 594fd89b8d789bb543dd1ef7c7fba5b584a24ee7 [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
115 cmd = ['git', 'rm', '-r'] + real
Simon Glass51f55182025-02-03 09:26:45 -0700116 stdout = command.output(*cmd, capture=True)
Simon Glass1bc06862019-05-18 10:07:17 -0600117
118 ## Change the messages as needed
119 msg = '''arm: Remove %s board
120
121This board has not been converted to CONFIG_DM_MMC by the deadline.
122Remove it.
123
124''' % board
125 for name in cc:
126 msg += 'Patch-cc: %s\n' % name
127
128 # Create the commit
Simon Glass51f55182025-02-03 09:26:45 -0700129 stdout = command.output('git', 'commit', '-s', '-m', msg)
Simon Glass1bc06862019-05-18 10:07:17 -0600130
131 # Check if the board is mentioned anywhere else. The user will need to deal
132 # with this
Simon Glass51f55182025-02-03 09:26:45 -0700133 print(command.output('git', 'grep', '-il', board, 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)