fill_scrapyard.py 4.9 KB

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