152
|
1 #!/usr/bin/env python3
|
|
2 #
|
|
3 # This file is part of GCC.
|
|
4 #
|
|
5 # GCC is free software; you can redistribute it and/or modify it under
|
|
6 # the terms of the GNU General Public License as published by the Free
|
|
7 # Software Foundation; either version 3, or (at your option) any later
|
|
8 # version.
|
|
9 #
|
|
10 # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 # for more details.
|
|
14 #
|
|
15 # You should have received a copy of the GNU General Public License
|
|
16 # along with GCC; see the file COPYING3. If not see
|
|
17 # <http://www.gnu.org/licenses/>. */
|
|
18
|
|
19 import os
|
|
20 import sys
|
|
21 from itertools import takewhile
|
|
22
|
|
23 from dateutil.parser import parse
|
|
24
|
|
25 from git_commit import GitCommit
|
|
26
|
|
27 from unidiff import PatchSet
|
|
28
|
|
29 DATE_PREFIX = 'Date: '
|
|
30 FROM_PREFIX = 'From: '
|
|
31
|
|
32
|
|
33 class GitEmail(GitCommit):
|
|
34 def __init__(self, filename, strict=False):
|
|
35 self.filename = filename
|
|
36 diff = PatchSet.from_filename(filename)
|
|
37 date = None
|
|
38 author = None
|
|
39
|
|
40 lines = open(self.filename).read().splitlines()
|
|
41 lines = list(takewhile(lambda line: line != '---', lines))
|
|
42 for line in lines:
|
|
43 if line.startswith(DATE_PREFIX):
|
|
44 date = parse(line[len(DATE_PREFIX):])
|
|
45 elif line.startswith(FROM_PREFIX):
|
|
46 author = GitCommit.format_git_author(line[len(FROM_PREFIX):])
|
|
47 header = list(takewhile(lambda line: line != '', lines))
|
|
48 body = lines[len(header) + 1:]
|
|
49
|
|
50 modified_files = []
|
|
51 for f in diff:
|
|
52 if f.is_added_file:
|
|
53 t = 'A'
|
|
54 elif f.is_removed_file:
|
|
55 t = 'D'
|
|
56 else:
|
|
57 t = 'M'
|
|
58 modified_files.append((f.path, t))
|
|
59 super().__init__(None, date, author, body, modified_files,
|
|
60 strict=strict)
|
|
61
|
|
62
|
|
63 if __name__ == '__main__':
|
|
64 if len(sys.argv) == 1:
|
|
65 allfiles = []
|
|
66 for root, _dirs, files in os.walk('patches'):
|
|
67 for f in files:
|
|
68 full = os.path.join(root, f)
|
|
69 allfiles.append(full)
|
|
70
|
|
71 success = 0
|
|
72 for full in sorted(allfiles):
|
|
73 email = GitEmail(full, False)
|
|
74 print(email.filename)
|
|
75 if email.success:
|
|
76 success += 1
|
|
77 print(' OK')
|
|
78 else:
|
|
79 for error in email.errors:
|
|
80 print(' ERR: %s' % error)
|
|
81
|
|
82 print()
|
|
83 print('Successfully parsed: %d/%d' % (success, len(allfiles)))
|
|
84 else:
|
|
85 email = GitEmail(sys.argv[1], False)
|
|
86 if email.success:
|
|
87 print('OK')
|
|
88 email.print_output()
|
|
89 else:
|
|
90 if not email.lines:
|
|
91 print('Error: patch contains no parsed lines', file=sys.stderr)
|
|
92 email.print_errors()
|