scripts/check_license_header.py

changeset 42594
eddde70cedd8
child 43086
de9a870ce36e
equal deleted inserted replaced
42593:c821b5ab8895 42594:eddde70cedd8
1 #!/usr/bin/env python3
2
3 # Purple - Internet Messaging Library
4 # Copyright (C) Pidgin Developers <devel@pidgin.im>
5 #
6 # Purple is the legal property of its developers, whose names are too numerous
7 # to list here. Please refer to the COPYRIGHT file distributed with this
8 # source distribution.
9 #
10 # This library is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by the Free
12 # Software Foundation; either version 2 of the License, or (at your option)
13 # any later version.
14 #
15 # This library is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 # more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # this library; if not, see <https://www.gnu.org/licenses/>.
22
23 import difflib
24 import os
25 import sys
26
27 HEADER = """\
28 /*
29 * Purple - Internet Messaging Library
30 * Copyright (C) Pidgin Developers <devel@pidgin.im>
31 *
32 * Purple is the legal property of its developers, whose names are too numerous
33 * to list here. Please refer to the COPYRIGHT file distributed with this
34 * source distribution.
35 *
36 * This library is free software; you can redistribute it and/or modify it
37 * under the terms of the GNU General Public License as published by the Free
38 * Software Foundation; either version 2 of the License, or (at your option)
39 * any later version.
40 *
41 * This library is distributed in the hope that it will be useful, but WITHOUT
42 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
44 * more details.
45 *
46 * You should have received a copy of the GNU General Public License along with
47 * this library; if not, see <https://www.gnu.org/licenses/>.
48 */
49
50 """.splitlines(keepends=True)
51
52 header_lines = len(HEADER)
53
54 if len(sys.argv) < 4:
55 print('usage: directory template-file file...')
56 sys.exit(1)
57
58 path = sys.argv[1]
59
60 header = ""
61 with open(os.path.join(path, sys.argv[2]), 'r') as f:
62 header = f.read().splitlines(keepends=True)
63
64 error = False
65
66 debug = False
67
68 for filename in sys.argv[3:]:
69 file_data = None
70 with open(os.path.join(path, filename), 'r') as f:
71 file_data = f.read().splitlines(keepends=True)
72
73 # Look for a starting commenting ignoring newlines, if we find code, abort.
74 comment_start = False
75 comment_end = 0
76 for i, line in enumerate(file_data):
77 if line.startswith('/*'):
78 comment_start = True
79 elif comment_start and line.endswith('*/\n'):
80 comment_end = i
81 continue
82
83 if line.strip() == '':
84 continue
85
86 if comment_start and comment_end != 0:
87 header_end = i
88 break
89
90 if comment_start:
91 new_data = HEADER + file_data[header_end:]
92 else:
93 new_data = HEADER + file_data
94
95 if debug:
96 sys.stdout.writelines(new_data[:30])
97 print(f'comment_start: {comment_start}')
98 print(f'comment_end: {comment_end}')
99 print(f'header_end: {header_end}')
100 sys.stdout.writelines(file_data[:25])
101
102 diff = [*difflib.unified_diff(file_data, new_data,
103 fromfile=f'a/{filename}',
104 tofile=f'b/{filename}')]
105 if diff:
106 sys.stdout.writelines(diff)
107 error = True
108
109 if debug:
110 sys.exit(1)
111
112 if error:
113 sys.exit(1)

mercurial