fill_scrapyard.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python2
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
  5. #
  6. """
  7. Fill the "Commit" and "Removed" fields of doc/README.scrapyard
  8. The file doc/README.scrapyard is used to keep track of removed boards.
  9. When we remove support for boards, we are supposed to add entries to
  10. doc/README.scrapyard leaving "Commit" and "Removed" fields blank.
  11. The "Commit" field is the commit hash in which the board was removed
  12. and the "Removed" is the date at which the board was removed. Those
  13. two are known only after the board removal patch was applied, thus they
  14. need to be filled in later.
  15. This effectively means that the person who removes other boards is
  16. supposed to fill in the blank fields before adding new entries to
  17. doc/README.scrapyard.
  18. That is a really tedious task that should be automated.
  19. This script fills the blank fields of doc/README.scrapyard for you!
  20. Usage:
  21. The "Commit" and "Removed" fields must be "-". The other fields should
  22. have already been filled in by a former commit.
  23. Run
  24. scripts/fill_scrapyard.py
  25. """
  26. import os
  27. import subprocess
  28. import sys
  29. import tempfile
  30. DOC='doc/README.scrapyard'
  31. def get_last_modify_commit(file, line_num):
  32. """Get the commit that last modified the given line.
  33. This function runs "git blame" against the given line of the given
  34. file and returns the commit hash that last modified it.
  35. Arguments:
  36. file: the file to be git-blame'd.
  37. line_num: the line number to be git-blame'd. This line number
  38. starts from 1, not 0.
  39. Returns:
  40. Commit hash that last modified the line. The number of digits is
  41. long enough to form a unique commit.
  42. """
  43. result = subprocess.check_output(['git', 'blame', '-L',
  44. '%d,%d' % (line_num, line_num), file])
  45. commit = result.split()[0]
  46. if commit[0] == '^':
  47. sys.exit('%s: line %d: ' % (file, line_num) +
  48. 'this line was modified before the beginning of git history')
  49. if commit == '0' * len(commit):
  50. sys.exit('%s: line %d: locally modified\n' % (file, line_num) +
  51. 'Please run this script in a clean repository.')
  52. return commit
  53. def get_committer_date(commit):
  54. """Get the committer date of the given commit.
  55. This function returns the date when the given commit was applied.
  56. Arguments:
  57. commit: commit-ish object.
  58. Returns:
  59. The committer date of the given commit in the form YY-MM-DD.
  60. """
  61. committer_date = subprocess.check_output(['git', 'show', '-s',
  62. '--format=%ci', commit])
  63. return committer_date.split()[0]
  64. def move_to_topdir():
  65. """Change directory to the top of the git repository.
  66. Or, exit with an error message if called out of a git repository.
  67. """
  68. try:
  69. toplevel = subprocess.check_output(['git', 'rev-parse',
  70. '--show-toplevel'])
  71. except subprocess.CalledProcessError:
  72. sys.exit('Please run in a git repository.')
  73. # strip '\n'
  74. toplevel = toplevel.rstrip()
  75. # Change the current working directory to the toplevel of the respository
  76. # for our easier life.
  77. os.chdir(toplevel)
  78. class TmpFile:
  79. """Useful class to handle a temporary file.
  80. tempfile.mkstemp() is often used to create a unique temporary file,
  81. but what is inconvenient is that the caller is responsible for
  82. deleting the file when done with it.
  83. Even when the caller errors out on the way, the temporary file must
  84. be deleted somehow. The idea here is that we delete the file in
  85. the destructor of this class because the destructor is always
  86. invoked when the instance of the class is freed.
  87. """
  88. def __init__(self):
  89. """Constructor - create a temporary file"""
  90. fd, self.filename = tempfile.mkstemp()
  91. self.file = os.fdopen(fd, 'w')
  92. def __del__(self):
  93. """Destructor - delete the temporary file"""
  94. try:
  95. os.remove(self.filename)
  96. except:
  97. pass
  98. def main():
  99. move_to_topdir()
  100. line_num = 1
  101. tmpfile = TmpFile()
  102. for line in open(DOC):
  103. tmp = line.split(None, 5)
  104. modified = False
  105. if len(tmp) >= 5:
  106. # fill "Commit" field
  107. if tmp[3] == '-':
  108. tmp[3] = get_last_modify_commit(DOC, line_num)
  109. modified = True
  110. # fill "Removed" field
  111. if tmp[4] == '-':
  112. tmp[4] = get_committer_date(tmp[3])
  113. if modified:
  114. line = tmp[0].ljust(17)
  115. line += tmp[1].ljust(12)
  116. line += tmp[2].ljust(15)
  117. line += tmp[3].ljust(12)
  118. line += tmp[4].ljust(12)
  119. if len(tmp) >= 6:
  120. line += tmp[5]
  121. line = line.rstrip() + '\n'
  122. tmpfile.file.write(line)
  123. line_num += 1
  124. os.rename(tmpfile.filename, DOC)
  125. if __name__ == '__main__':
  126. main()