series.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2011 The Chromium OS Authors.
  3. #
  4. import itertools
  5. import os
  6. from patman import get_maintainer
  7. from patman import gitutil
  8. from patman import settings
  9. from patman import terminal
  10. from patman import tools
  11. # Series-xxx tags that we understand
  12. valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
  13. 'cover_cc', 'process_log']
  14. class Series(dict):
  15. """Holds information about a patch series, including all tags.
  16. Vars:
  17. cc: List of aliases/emails to Cc all patches to
  18. commits: List of Commit objects, one for each patch
  19. cover: List of lines in the cover letter
  20. notes: List of lines in the notes
  21. changes: (dict) List of changes for each version, The key is
  22. the integer version number
  23. allow_overwrite: Allow tags to overwrite an existing tag
  24. """
  25. def __init__(self):
  26. self.cc = []
  27. self.to = []
  28. self.cover_cc = []
  29. self.commits = []
  30. self.cover = None
  31. self.notes = []
  32. self.changes = {}
  33. self.allow_overwrite = False
  34. # Written in MakeCcFile()
  35. # key: name of patch file
  36. # value: list of email addresses
  37. self._generated_cc = {}
  38. # These make us more like a dictionary
  39. def __setattr__(self, name, value):
  40. self[name] = value
  41. def __getattr__(self, name):
  42. return self[name]
  43. def AddTag(self, commit, line, name, value):
  44. """Add a new Series-xxx tag along with its value.
  45. Args:
  46. line: Source line containing tag (useful for debug/error messages)
  47. name: Tag name (part after 'Series-')
  48. value: Tag value (part after 'Series-xxx: ')
  49. """
  50. # If we already have it, then add to our list
  51. name = name.replace('-', '_')
  52. if name in self and not self.allow_overwrite:
  53. values = value.split(',')
  54. values = [str.strip() for str in values]
  55. if type(self[name]) != type([]):
  56. raise ValueError("In %s: line '%s': Cannot add another value "
  57. "'%s' to series '%s'" %
  58. (commit.hash, line, values, self[name]))
  59. self[name] += values
  60. # Otherwise just set the value
  61. elif name in valid_series:
  62. if name=="notes":
  63. self[name] = [value]
  64. else:
  65. self[name] = value
  66. else:
  67. raise ValueError("In %s: line '%s': Unknown 'Series-%s': valid "
  68. "options are %s" % (commit.hash, line, name,
  69. ', '.join(valid_series)))
  70. def AddCommit(self, commit):
  71. """Add a commit into our list of commits
  72. We create a list of tags in the commit subject also.
  73. Args:
  74. commit: Commit object to add
  75. """
  76. commit.CheckTags()
  77. self.commits.append(commit)
  78. def ShowActions(self, args, cmd, process_tags):
  79. """Show what actions we will/would perform
  80. Args:
  81. args: List of patch files we created
  82. cmd: The git command we would have run
  83. process_tags: Process tags as if they were aliases
  84. """
  85. to_set = set(gitutil.BuildEmailList(self.to));
  86. cc_set = set(gitutil.BuildEmailList(self.cc));
  87. col = terminal.Color()
  88. print('Dry run, so not doing much. But I would do this:')
  89. print()
  90. print('Send a total of %d patch%s with %scover letter.' % (
  91. len(args), '' if len(args) == 1 else 'es',
  92. self.get('cover') and 'a ' or 'no '))
  93. # TODO: Colour the patches according to whether they passed checks
  94. for upto in range(len(args)):
  95. commit = self.commits[upto]
  96. print(col.Color(col.GREEN, ' %s' % args[upto]))
  97. cc_list = list(self._generated_cc[commit.patch])
  98. for email in sorted(set(cc_list) - to_set - cc_set):
  99. if email == None:
  100. email = col.Color(col.YELLOW, "<alias '%s' not found>"
  101. % tag)
  102. if email:
  103. print(' Cc: ', email)
  104. print
  105. for item in sorted(to_set):
  106. print('To:\t ', item)
  107. for item in sorted(cc_set - to_set):
  108. print('Cc:\t ', item)
  109. print('Version: ', self.get('version'))
  110. print('Prefix:\t ', self.get('prefix'))
  111. if self.cover:
  112. print('Cover: %d lines' % len(self.cover))
  113. cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
  114. all_ccs = itertools.chain(cover_cc, *self._generated_cc.values())
  115. for email in sorted(set(all_ccs) - to_set - cc_set):
  116. print(' Cc: ', email)
  117. if cmd:
  118. print('Git command: %s' % cmd)
  119. def MakeChangeLog(self, commit):
  120. """Create a list of changes for each version.
  121. Return:
  122. The change log as a list of strings, one per line
  123. Changes in v4:
  124. - Jog the dial back closer to the widget
  125. Changes in v3: None
  126. Changes in v2:
  127. - Fix the widget
  128. - Jog the dial
  129. etc.
  130. """
  131. final = []
  132. process_it = self.get('process_log', '').split(',')
  133. process_it = [item.strip() for item in process_it]
  134. need_blank = False
  135. for change in sorted(self.changes, reverse=True):
  136. out = []
  137. for this_commit, text in self.changes[change]:
  138. if commit and this_commit != commit:
  139. continue
  140. if 'uniq' not in process_it or text not in out:
  141. out.append(text)
  142. line = 'Changes in v%d:' % change
  143. have_changes = len(out) > 0
  144. if 'sort' in process_it:
  145. out = sorted(out)
  146. if have_changes:
  147. out.insert(0, line)
  148. else:
  149. out = [line + ' None']
  150. if need_blank:
  151. out.insert(0, '')
  152. final += out
  153. need_blank = have_changes
  154. if self.changes:
  155. final.append('')
  156. return final
  157. def DoChecks(self):
  158. """Check that each version has a change log
  159. Print an error if something is wrong.
  160. """
  161. col = terminal.Color()
  162. if self.get('version'):
  163. changes_copy = dict(self.changes)
  164. for version in range(1, int(self.version) + 1):
  165. if self.changes.get(version):
  166. del changes_copy[version]
  167. else:
  168. if version > 1:
  169. str = 'Change log missing for v%d' % version
  170. print(col.Color(col.RED, str))
  171. for version in changes_copy:
  172. str = 'Change log for unknown version v%d' % version
  173. print(col.Color(col.RED, str))
  174. elif self.changes:
  175. str = 'Change log exists, but no version is set'
  176. print(col.Color(col.RED, str))
  177. def MakeCcFile(self, process_tags, cover_fname, raise_on_error,
  178. add_maintainers, limit):
  179. """Make a cc file for us to use for per-commit Cc automation
  180. Also stores in self._generated_cc to make ShowActions() faster.
  181. Args:
  182. process_tags: Process tags as if they were aliases
  183. cover_fname: If non-None the name of the cover letter.
  184. raise_on_error: True to raise an error when an alias fails to match,
  185. False to just print a message.
  186. add_maintainers: Either:
  187. True/False to call the get_maintainers to CC maintainers
  188. List of maintainers to include (for testing)
  189. limit: Limit the length of the Cc list
  190. Return:
  191. Filename of temp file created
  192. """
  193. col = terminal.Color()
  194. # Look for commit tags (of the form 'xxx:' at the start of the subject)
  195. fname = '/tmp/patman.%d' % os.getpid()
  196. fd = open(fname, 'w', encoding='utf-8')
  197. all_ccs = []
  198. for commit in self.commits:
  199. cc = []
  200. if process_tags:
  201. cc += gitutil.BuildEmailList(commit.tags,
  202. raise_on_error=raise_on_error)
  203. cc += gitutil.BuildEmailList(commit.cc_list,
  204. raise_on_error=raise_on_error)
  205. if type(add_maintainers) == type(cc):
  206. cc += add_maintainers
  207. elif add_maintainers:
  208. cc += get_maintainer.GetMaintainer(commit.patch)
  209. for x in set(cc) & set(settings.bounces):
  210. print(col.Color(col.YELLOW, 'Skipping "%s"' % x))
  211. cc = set(cc) - set(settings.bounces)
  212. cc = [tools.FromUnicode(m) for m in cc]
  213. if limit is not None:
  214. cc = cc[:limit]
  215. all_ccs += cc
  216. print(commit.patch, '\0'.join(sorted(set(cc))), file=fd)
  217. self._generated_cc[commit.patch] = cc
  218. if cover_fname:
  219. cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
  220. cover_cc = [tools.FromUnicode(m) for m in cover_cc]
  221. cover_cc = list(set(cover_cc + all_ccs))
  222. if limit is not None:
  223. cover_cc = cover_cc[:limit]
  224. cc_list = '\0'.join([tools.ToUnicode(x) for x in sorted(cover_cc)])
  225. print(cover_fname, cc_list, file=fd)
  226. fd.close()
  227. return fname
  228. def AddChange(self, version, commit, info):
  229. """Add a new change line to a version.
  230. This will later appear in the change log.
  231. Args:
  232. version: version number to add change list to
  233. info: change line for this version
  234. """
  235. if not self.changes.get(version):
  236. self.changes[version] = []
  237. self.changes[version].append([commit, info])
  238. def GetPatchPrefix(self):
  239. """Get the patch version string
  240. Return:
  241. Patch string, like 'RFC PATCH v5' or just 'PATCH'
  242. """
  243. git_prefix = gitutil.GetDefaultSubjectPrefix()
  244. if git_prefix:
  245. git_prefix = '%s][' % git_prefix
  246. else:
  247. git_prefix = ''
  248. version = ''
  249. if self.get('version'):
  250. version = ' v%s' % self['version']
  251. # Get patch name prefix
  252. prefix = ''
  253. if self.get('prefix'):
  254. prefix = '%s ' % self['prefix']
  255. return '%s%sPATCH%s' % (git_prefix, prefix, version)