3 # Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
5 # SPDX-License-Identifier: GPL-2.0+
9 Fill the "Commit" and "Removed" fields of doc/README.scrapyard
11 The file doc/README.scrapyard is used to keep track of removed boards.
13 When we remove support for boards, we are supposed to add entries to
14 doc/README.scrapyard leaving "Commit" and "Removed" fields blank.
16 The "Commit" field is the commit hash in which the board was removed
17 and the "Removed" is the date at which the board was removed. Those
18 two are known only after the board removal patch was applied, thus they
19 need to be filled in later.
21 This effectively means that the person who removes other boards is
22 supposed to fill in the blank fields before adding new entries to
25 That is a really tedious task that should be automated.
26 This script fills the blank fields of doc/README.scrapyard for you!
30 The "Commit" and "Removed" fields must be "-". The other fields should
31 have already been filled in by a former commit.
34 scripts/fill_scrapyard.py
42 DOC='doc/README.scrapyard'
44 def get_last_modify_commit(file, line_num):
45 """Get the commit that last modified the given line.
47 This function runs "git blame" against the given line of the given
48 file and returns the commit hash that last modified it.
51 file: the file to be git-blame'd.
52 line_num: the line number to be git-blame'd. This line number
56 Commit hash that last modified the line. The number of digits is
57 long enough to form a unique commit.
59 result = subprocess.check_output(['git', 'blame', '-L',
60 '%d,%d' % (line_num, line_num), file])
61 commit = result.split()[0]
64 sys.exit('%s: line %d: ' % (file, line_num) +
65 'this line was modified before the beginning of git history')
67 if commit == '0' * len(commit):
68 sys.exit('%s: line %d: locally modified\n' % (file, line_num) +
69 'Please run this script in a clean repository.')
73 def get_committer_date(commit):
74 """Get the committer date of the given commit.
76 This function returns the date when the given commit was applied.
79 commit: commit-ish object.
82 The committer date of the given commit in the form YY-MM-DD.
84 committer_date = subprocess.check_output(['git', 'show', '-s',
85 '--format=%ci', commit])
86 return committer_date.split()[0]
89 """Change directory to the top of the git repository.
91 Or, exit with an error message if called out of a git repository.
94 toplevel = subprocess.check_output(['git', 'rev-parse',
96 except subprocess.CalledProcessError:
97 sys.exit('Please run in a git repository.')
100 toplevel = toplevel.rstrip()
102 # Change the current working directory to the toplevel of the respository
103 # for our easier life.
108 """Useful class to handle a temporary file.
110 tempfile.mkstemp() is often used to create a unique temporary file,
111 but what is inconvenient is that the caller is responsible for
112 deleting the file when done with it.
114 Even when the caller errors out on the way, the temporary file must
115 be deleted somehow. The idea here is that we delete the file in
116 the destructor of this class because the destructor is always
117 invoked when the instance of the class is freed.
121 """Constructor - create a temporary file"""
122 fd, self.filename = tempfile.mkstemp()
123 self.file = os.fdopen(fd, 'w')
126 """Destructor - delete the temporary file"""
128 os.remove(self.filename)
138 for line in open(DOC):
139 tmp = line.split(None, 5)
143 # fill "Commit" field
145 tmp[3] = get_last_modify_commit(DOC, line_num)
147 # fill "Removed" field
149 tmp[4] = get_committer_date(tmp[3])
151 line = tmp[0].ljust(17)
152 line += tmp[1].ljust(12)
153 line += tmp[2].ljust(15)
154 line += tmp[3].ljust(12)
155 line += tmp[4].ljust(12)
158 line = line.rstrip() + '\n'
160 tmpfile.file.write(line)
163 os.rename(tmpfile.filename, DOC)
165 if __name__ == '__main__':