scripts/check_license_header.py

Sat, 09 Aug 2025 18:12:31 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sat, 09 Aug 2025 18:12:31 +0800
branch
gir-dependency
changeset 43305
4ede49515766
parent 43088
cbd733874bd4
permissions
-rwxr-xr-x

Add builtin library dependency for introspection

Without specifying, gir defaults to the system pidgin/purple libraries by default,
which fails the build when new symbols were added and gir failed to link for them.

42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 #!/usr/bin/env python3
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 # Purple - Internet Messaging Library
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 # Copyright (C) Pidgin Developers <devel@pidgin.im>
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 #
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 # Purple is the legal property of its developers, whose names are too numerous
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 # to list here. Please refer to the COPYRIGHT file distributed with this
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 # source distribution.
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 #
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 # This library is free software; you can redistribute it and/or modify it
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 # under the terms of the GNU General Public License as published by the Free
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 # Software Foundation; either version 2 of the License, or (at your option)
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 # any later version.
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 #
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 # This library is distributed in the hope that it will be useful, but WITHOUT
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 # more details.
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 #
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20 # You should have received a copy of the GNU General Public License along with
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 # this library; if not, see <https://www.gnu.org/licenses/>.
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
22
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
23 import difflib
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24 import os
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25 import sys
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
26
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28 if len(sys.argv) < 4:
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29 print('usage: directory template-file file...')
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30 sys.exit(1)
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32 path = sys.argv[1]
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34 header = ""
43088
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
35 with open(os.path.join(path, sys.argv[2]), 'r', encoding='utf-8') as f:
42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 header = f.read().splitlines(keepends=True)
43088
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
37 if header[-1] != '\n': # Always separate header from body with a blank line.
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
38 header.append('\n')
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
39 header_lines = len(header)
42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
40
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
41 error = False
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
42 debug = False
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
43
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
44 for filename in sys.argv[3:]:
43086
de9a870ce36e Standardize license headers in Windows files
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 42594
diff changeset
45 if filename.endswith('.o'):
de9a870ce36e Standardize license headers in Windows files
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 42594
diff changeset
46 # On Windows, we sometimes have object files in sources, which we don't
de9a870ce36e Standardize license headers in Windows files
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 42594
diff changeset
47 # want to try to read.
de9a870ce36e Standardize license headers in Windows files
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 42594
diff changeset
48 continue
42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
49 file_data = None
43088
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
50 with open(os.path.join(path, filename), 'r', encoding='utf-8') as f:
42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
51 file_data = f.read().splitlines(keepends=True)
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
52
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
53 # Look for a starting commenting ignoring newlines, if we find code, abort.
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
54 comment_start = False
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
55 comment_end = 0
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
56 for i, line in enumerate(file_data):
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
57 if line.startswith('/*'):
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58 comment_start = True
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 elif comment_start and line.endswith('*/\n'):
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60 comment_end = i
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 continue
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 if line.strip() == '':
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64 continue
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66 if comment_start and comment_end != 0:
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 header_end = i
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68 break
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70 if comment_start:
43088
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
71 new_data = header + file_data[header_end:]
42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72 else:
43088
cbd733874bd4 Fix license header check
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 43086
diff changeset
73 new_data = header + file_data
42594
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 if debug:
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 sys.stdout.writelines(new_data[:30])
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77 print(f'comment_start: {comment_start}')
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 print(f'comment_end: {comment_end}')
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79 print(f'header_end: {header_end}')
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 sys.stdout.writelines(file_data[:25])
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 diff = [*difflib.unified_diff(file_data, new_data,
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83 fromfile=f'a/{filename}',
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84 tofile=f'b/{filename}')]
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 if diff:
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86 sys.stdout.writelines(diff)
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 error = True
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89 if debug:
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90 sys.exit(1)
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 if error:
eddde70cedd8 Create a test for verifying license headers and correct the ones that were wrong
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 sys.exit(1)

mercurial