mklog 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #!/usr/bin/env python3
  2. # Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. #
  4. # This file is part of GCC.
  5. #
  6. # GCC is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # GCC is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GCC; see the file COPYING. If not, write to
  18. # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  19. # Boston, MA 02110-1301, USA.
  20. # This script parses a .diff file generated with 'diff -up' or 'diff -cp'
  21. # and adds a skeleton ChangeLog file to the file. It does not try to be
  22. # too smart when parsing function names, but it produces a reasonable
  23. # approximation.
  24. #
  25. # This is a straightforward adaptation of original Perl script.
  26. #
  27. # Author: Yury Gribov <tetra2005@gmail.com>
  28. import argparse
  29. import sys
  30. import re
  31. import os.path
  32. import os
  33. import tempfile
  34. import time
  35. import shutil
  36. from subprocess import Popen, PIPE
  37. me = os.path.basename(sys.argv[0])
  38. pr_regex = re.compile('\+(\/(\/|\*)|[Cc*!])\s+(PR [a-z+-]+\/[0-9]+)')
  39. def error(msg):
  40. sys.stderr.write("%s: error: %s\n" % (me, msg))
  41. sys.exit(1)
  42. def warn(msg):
  43. sys.stderr.write("%s: warning: %s\n" % (me, msg))
  44. class RegexCache(object):
  45. """Simple trick to Perl-like combined match-and-bind."""
  46. def __init__(self):
  47. self.last_match = None
  48. def match(self, p, s):
  49. self.last_match = re.match(p, s) if isinstance(p, str) else p.match(s)
  50. return self.last_match
  51. def search(self, p, s):
  52. self.last_match = re.search(p, s) if isinstance(p, str) else p.search(s)
  53. return self.last_match
  54. def group(self, n):
  55. return self.last_match.group(n)
  56. cache = RegexCache()
  57. def run(cmd, die_on_error):
  58. """Simple wrapper for Popen."""
  59. proc = Popen(cmd.split(' '), stderr = PIPE, stdout = PIPE)
  60. (out, err) = proc.communicate()
  61. if die_on_error and proc.returncode != 0:
  62. error("`%s` failed:\n" % (cmd, proc.stderr))
  63. return proc.returncode, out.decode(), err
  64. def read_user_info():
  65. dot_mklog_format_msg = """\
  66. The .mklog format is:
  67. NAME = ...
  68. EMAIL = ...
  69. """
  70. # First try to read .mklog config
  71. mklog_conf = os.path.expanduser('~/.mklog')
  72. if os.path.exists(mklog_conf):
  73. attrs = {}
  74. f = open(mklog_conf)
  75. for s in f:
  76. if cache.match(r'^\s*([a-zA-Z0-9_]+)\s*=\s*(.*?)\s*$', s):
  77. attrs[cache.group(1)] = cache.group(2)
  78. f.close()
  79. if 'NAME' not in attrs:
  80. error("'NAME' not present in .mklog")
  81. if 'EMAIL' not in attrs:
  82. error("'EMAIL' not present in .mklog")
  83. return attrs['NAME'], attrs['EMAIL']
  84. # Otherwise go with git
  85. rc1, name, _ = run('git config user.name', False)
  86. name = name.rstrip()
  87. rc2, email, _ = run('git config user.email', False)
  88. email = email.rstrip()
  89. if rc1 != 0 or rc2 != 0:
  90. error("""\
  91. Could not read git user.name and user.email settings.
  92. Please add missing git settings, or create a %s.
  93. """ % mklog_conf)
  94. return name, email
  95. def get_parent_changelog (s):
  96. """See which ChangeLog this file change should go to."""
  97. if s.find('\\') == -1 and s.find('/') == -1:
  98. return "ChangeLog", s
  99. gcc_root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  100. d = s
  101. while d:
  102. clname = d + "/ChangeLog"
  103. if os.path.exists(gcc_root + '/' + clname) or os.path.exists(clname):
  104. relname = s[len(d)+1:]
  105. return clname, relname
  106. d, _ = os.path.split(d)
  107. return "Unknown ChangeLog", s
  108. class FileDiff:
  109. """Class to represent changes in a single file."""
  110. def __init__(self, filename):
  111. self.filename = filename
  112. self.hunks = []
  113. self.clname, self.relname = get_parent_changelog(filename);
  114. def dump(self):
  115. print("Diff for %s:\n ChangeLog = %s\n rel name = %s\n" % (self.filename, self.clname, self.relname))
  116. for i, h in enumerate(self.hunks):
  117. print("Next hunk %d:" % i)
  118. h.dump()
  119. class Hunk:
  120. """Class to represent a single hunk of changes."""
  121. def __init__(self, hdr):
  122. self.hdr = hdr
  123. self.lines = []
  124. self.ctx_diff = is_ctx_hunk_start(hdr)
  125. def dump(self):
  126. print('%s' % self.hdr)
  127. print('%s' % '\n'.join(self.lines))
  128. def is_file_addition(self):
  129. """Does hunk describe addition of file?"""
  130. if self.ctx_diff:
  131. for line in self.lines:
  132. if re.match(r'^\*\*\* 0 \*\*\*\*', line):
  133. return True
  134. else:
  135. return re.match(r'^@@ -0,0 \+1.* @@', self.hdr)
  136. def is_file_removal(self):
  137. """Does hunk describe removal of file?"""
  138. if self.ctx_diff:
  139. for line in self.lines:
  140. if re.match(r'^--- 0 ----', line):
  141. return True
  142. else:
  143. return re.match(r'^@@ -1.* \+0,0 @@', self.hdr)
  144. def is_file_diff_start(s):
  145. # Don't be fooled by context diff line markers:
  146. # *** 385,391 ****
  147. return ((s.startswith('*** ') and not s.endswith('***'))
  148. or (s.startswith('--- ') and not s.endswith('---')))
  149. def is_ctx_hunk_start(s):
  150. return re.match(r'^\*\*\*\*\*\**', s)
  151. def is_uni_hunk_start(s):
  152. return re.match(r'^@@ .* @@', s)
  153. def is_hunk_start(s):
  154. return is_ctx_hunk_start(s) or is_uni_hunk_start(s)
  155. def remove_suffixes(s):
  156. if s.startswith('a/') or s.startswith('b/'):
  157. s = s[2:]
  158. if s.endswith('.jj'):
  159. s = s[:-3]
  160. return s
  161. def find_changed_funs(hunk):
  162. """Find all functions touched by hunk. We don't try too hard
  163. to find good matches. This should return a superset
  164. of the actual set of functions in the .diff file.
  165. """
  166. fns = []
  167. fn = None
  168. if (cache.match(r'^\*\*\*\*\*\** ([a-zA-Z0-9_].*)', hunk.hdr)
  169. or cache.match(r'^@@ .* @@ ([a-zA-Z0-9_].*)', hunk.hdr)):
  170. fn = cache.group(1)
  171. for i, line in enumerate(hunk.lines):
  172. # Context diffs have extra whitespace after first char;
  173. # remove it to make matching easier.
  174. if hunk.ctx_diff:
  175. line = re.sub(r'^([-+! ]) ', r'\1', line)
  176. # Remember most recent identifier in hunk
  177. # that might be a function name.
  178. if cache.match(r'^[-+! ]([a-zA-Z0-9_#].*)', line):
  179. fn = cache.group(1)
  180. change = line and re.match(r'^[-+!][^-]', line)
  181. # Top-level comment cannot belong to function
  182. if re.match(r'^[-+! ]\/\*', line):
  183. fn = None
  184. if change and fn:
  185. if cache.match(r'^((class|struct|union|enum)\s+[a-zA-Z0-9_]+)', fn):
  186. # Struct declaration
  187. fn = cache.group(1)
  188. elif cache.search(r'#\s*define\s+([a-zA-Z0-9_]+)', fn):
  189. # Macro definition
  190. fn = cache.group(1)
  191. elif cache.match('^DEF[A-Z0-9_]+\s*\(([a-zA-Z0-9_]+)', fn):
  192. # Supermacro
  193. fn = cache.group(1)
  194. elif cache.search(r'([a-zA-Z_][^()\s]*)\s*\([^*]', fn):
  195. # Discard template and function parameters.
  196. fn = cache.group(1)
  197. fn = re.sub(r'<[^<>]*>', '', fn)
  198. fn = fn.rstrip()
  199. else:
  200. fn = None
  201. if fn and fn not in fns: # Avoid dups
  202. fns.append(fn)
  203. fn = None
  204. return fns
  205. def parse_patch(contents):
  206. """Parse patch contents to a sequence of FileDiffs."""
  207. diffs = []
  208. lines = contents.split('\n')
  209. i = 0
  210. while i < len(lines):
  211. line = lines[i]
  212. # Diff headers look like
  213. # --- a/gcc/tree.c
  214. # +++ b/gcc/tree.c
  215. # or
  216. # *** gcc/cfgexpand.c 2013-12-25 20:07:24.800350058 +0400
  217. # --- gcc/cfgexpand.c 2013-12-25 20:06:30.612350178 +0400
  218. if is_file_diff_start(line):
  219. left = re.split(r'\s+', line)[1]
  220. else:
  221. i += 1
  222. continue
  223. left = remove_suffixes(left);
  224. i += 1
  225. line = lines[i]
  226. if not cache.match(r'^[+-][+-][+-] +(\S+)', line):
  227. error("expected filename in line %d" % i)
  228. right = remove_suffixes(cache.group(1));
  229. # Extract real file name from left and right names.
  230. filename = None
  231. if left == right:
  232. filename = left
  233. elif left == '/dev/null':
  234. filename = right;
  235. elif right == '/dev/null':
  236. filename = left;
  237. else:
  238. comps = []
  239. while left and right:
  240. left, l = os.path.split(left)
  241. right, r = os.path.split(right)
  242. if l != r:
  243. break
  244. comps.append(l)
  245. if not comps:
  246. error("failed to extract common name for %s and %s" % (left, right))
  247. comps.reverse()
  248. filename = '/'.join(comps)
  249. d = FileDiff(filename)
  250. diffs.append(d)
  251. # Collect hunks for current file.
  252. hunk = None
  253. i += 1
  254. while i < len(lines):
  255. line = lines[i]
  256. # Create new hunk when we see hunk header
  257. if is_hunk_start(line):
  258. if hunk is not None:
  259. d.hunks.append(hunk)
  260. hunk = Hunk(line)
  261. i += 1
  262. continue
  263. # Stop when we reach next diff
  264. if (is_file_diff_start(line)
  265. or line.startswith('diff ')
  266. or line.startswith('Index: ')):
  267. i -= 1
  268. break
  269. if hunk is not None:
  270. hunk.lines.append(line)
  271. i += 1
  272. d.hunks.append(hunk)
  273. return diffs
  274. def get_pr_from_testcase(line):
  275. r = pr_regex.search(line)
  276. if r != None:
  277. return r.group(3)
  278. else:
  279. return None
  280. def main():
  281. name, email = read_user_info()
  282. help_message = """\
  283. Generate ChangeLog template for PATCH.
  284. PATCH must be generated using diff(1)'s -up or -cp options
  285. (or their equivalent in Subversion/git).
  286. """
  287. inline_message = """\
  288. Prepends ChangeLog to PATCH.
  289. If PATCH is not stdin, modifies PATCH in-place,
  290. otherwise writes to stdout.'
  291. """
  292. parser = argparse.ArgumentParser(description = help_message)
  293. parser.add_argument('-v', '--verbose', action = 'store_true', help = 'Verbose messages')
  294. parser.add_argument('-i', '--inline', action = 'store_true', help = inline_message)
  295. parser.add_argument('input', nargs = '?', help = 'Patch file (or missing, read standard input)')
  296. args = parser.parse_args()
  297. if args.input == '-':
  298. args.input = None
  299. input = open(args.input) if args.input else sys.stdin
  300. contents = input.read()
  301. diffs = parse_patch(contents)
  302. if args.verbose:
  303. print("Parse results:")
  304. for d in diffs:
  305. d.dump()
  306. # Generate template ChangeLog.
  307. logs = {}
  308. prs = []
  309. for d in diffs:
  310. log_name = d.clname
  311. logs.setdefault(log_name, '')
  312. logs[log_name] += '\t* %s' % d.relname
  313. change_msg = ''
  314. # Check if file was removed or added.
  315. # Two patterns for context and unified diff.
  316. if len(d.hunks) == 1:
  317. hunk0 = d.hunks[0]
  318. if hunk0.is_file_addition():
  319. if re.search(r'testsuite.*(?<!\.exp)$', d.filename):
  320. change_msg = ': New test.\n'
  321. pr = get_pr_from_testcase(hunk0.lines[0])
  322. if pr and pr not in prs:
  323. prs.append(pr)
  324. else:
  325. change_msg = ": New file.\n"
  326. elif hunk0.is_file_removal():
  327. change_msg = ": Remove.\n"
  328. _, ext = os.path.splitext(d.filename)
  329. if (not change_msg and ext in ['.c', '.cpp', '.C', '.cc', '.h', '.inc', '.def']
  330. and not 'testsuite' in d.filename):
  331. fns = []
  332. for hunk in d.hunks:
  333. for fn in find_changed_funs(hunk):
  334. if fn not in fns:
  335. fns.append(fn)
  336. for fn in fns:
  337. if change_msg:
  338. change_msg += "\t(%s):\n" % fn
  339. else:
  340. change_msg = " (%s):\n" % fn
  341. logs[log_name] += change_msg if change_msg else ":\n"
  342. if args.inline and args.input:
  343. # Get a temp filename, rather than an open filehandle, because we use
  344. # the open to truncate.
  345. fd, tmp = tempfile.mkstemp("tmp.XXXXXXXX")
  346. os.close(fd)
  347. # Copy permissions to temp file
  348. # (old Pythons do not support shutil.copymode)
  349. shutil.copymode(args.input, tmp)
  350. # Open the temp file, clearing contents.
  351. out = open(tmp, 'w')
  352. else:
  353. tmp = None
  354. out = sys.stdout
  355. # Print log
  356. date = time.strftime('%Y-%m-%d')
  357. bugmsg = ''
  358. if len(prs):
  359. bugmsg = '\n'.join(['\t' + pr for pr in prs]) + '\n'
  360. for log_name, msg in sorted(logs.items()):
  361. out.write("""\
  362. %s:
  363. %s %s <%s>
  364. %s%s\n""" % (log_name, date, name, email, bugmsg, msg))
  365. if args.inline:
  366. # Append patch body
  367. out.write(contents)
  368. if args.input:
  369. # Write new contents atomically
  370. out.close()
  371. shutil.move(tmp, args.input)
  372. if __name__ == '__main__':
  373. main()