| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import os |
| 4 |
import re |
| 5 |
import sys |
| 6 |
|
| 7 |
verbose = '--verbose' in sys.argv |
| 8 |
|
| 9 |
default_copyright = 'The Qt Company Ltd.' |
| 10 |
default_licenses = ( |
| 11 |
'BSD-3-clause', |
| 12 |
'LGPL-3 or GPL-2', |
| 13 |
'GPL-3 with Qt-1.0 exception', |
| 14 |
'GFDL-NIV-1.3', |
| 15 |
) |
| 16 |
header_re = re.compile(r"\$QT_BEGIN_LICENSE:([\w\-]+)\$") |
| 17 |
copyright_re = re.compile(r"Copyright \(C\) (\d\d.+)") |
| 18 |
|
| 19 |
author_map = { |
| 20 |
re.compile('^BlackBerry|^Research [Ii]n Motion'): 'BlackBerry Limited (formerly Research In Motion)', |
| 21 |
re.compile('^BogDan Vatra'): 'BogDan Vatra <[email protected]>', |
| 22 |
re.compile('^Canonical'): 'Canonical, Ltd.', |
| 23 |
re.compile('^David Faure'): 'David Faure <[email protected]>', |
| 24 |
re.compile('^Giuseppe D\'Angelo'): 'Giuseppe D\'Angelo <[email protected]>', |
| 25 |
re.compile('^Governikus GmbH & Co. KG'): 'Governikus GmbH & Co. KG.', |
| 26 |
re.compile('^Green Hills Software'): 'Green Hills Software', |
| 27 |
re.compile('^Intel Corporation'): 'Intel Corporation', |
| 28 |
re.compile('^Ivan Komissarov'): 'Ivan Komissarov <[email protected]>', |
| 29 |
re.compile('KDAB'): 'Klarälvdalens Datakonsult AB, a KDAB Group company', |
| 30 |
re.compile('^Konstantin Ritt'): 'Konstantin Ritt <[email protected]>', |
| 31 |
re.compile('^Lorn Potter'): 'Lorn Potter', |
| 32 |
re.compile(r'Martsum .*tmartsum\[at\]gmail.com'): 'Thorbjørn Lund Martsum <[email protected]>', |
| 33 |
re.compile('^Olivier Goffart'): 'Olivier Goffart <[email protected]>', |
| 34 |
re.compile('^Richard J. Moore'): 'Richard J. Moore <[email protected]>', |
| 35 |
re.compile('^Robin Burchell'): 'Robin Burchell <[email protected]>', |
| 36 |
re.compile('^Samuel Gaist'): 'Samuel Gaist <[email protected]>', |
| 37 |
re.compile('^Stephen Kelly'): 'Stephen Kelly <[email protected]>', |
| 38 |
re.compile('^The Qt Company'): 'The Qt Company Ltd.', |
| 39 |
} |
| 40 |
|
| 41 |
licenses_map = { |
| 42 |
'BSD': 'BSD-3-clause', |
| 43 |
'FDL': 'GFDL-NIV-1.3', |
| 44 |
'GPL': 'GPL-3', |
| 45 |
'GPL-EXCEPT': 'GPL-3 with Qt-1.0 exception', |
| 46 |
'LGPL': 'LGPL-3 or GPL-2', |
| 47 |
'LGPL21': 'LGPL-2.1-or-3 with Qt-1.1 exception', |
| 48 |
'LGPL3': 'LGPL-3 or GPL-2+', |
| 49 |
'LGPL3-COMM': 'LGPL-3', |
| 50 |
'MIT': 'Expat', |
| 51 |
} |
| 52 |
|
| 53 |
exclude_prefixes = ( |
| 54 |
'header', |
| 55 |
'.git', |
| 56 |
) |
| 57 |
|
| 58 |
start_header = '## BEGIN AUTO GENERATED BLOCK' |
| 59 |
end_header = '## END AUTO GENERATED BLOCK' |
| 60 |
|
| 61 |
|
| 62 |
class CopyrightInfo(): |
| 63 |
def __init__(self): |
| 64 |
self.min_years = {} |
| 65 |
self.max_years = {} |
| 66 |
self.files = [] |
| 67 |
|
| 68 |
def add_file(self, authors, file): |
| 69 |
for min_year, max_year, author in authors: |
| 70 |
if author in self.min_years: |
| 71 |
self.min_years[author] = min(self.min_years[author], min_year) |
| 72 |
else: |
| 73 |
self.min_years[author] = min_year |
| 74 |
if author in self.max_years: |
| 75 |
self.max_years[author] = max(self.max_years[author], max_year) |
| 76 |
else: |
| 77 |
self.max_years[author] = max_year |
| 78 |
self.files.append(file) |
| 79 |
|
| 80 |
def get_strings(self, authors): |
| 81 |
for author in authors: |
| 82 |
min_year = self.min_years[author] |
| 83 |
max_year = self.max_years[author] |
| 84 |
if min_year == max_year: |
| 85 |
yield '%d %s' % (min_year, author) |
| 86 |
else: |
| 87 |
yield '%d-%d %s' % (min_year, max_year, author) |
| 88 |
|
| 89 |
|
| 90 |
def canonicalize_author_name(author): |
| 91 |
for regex, replacement in author_map.items(): |
| 92 |
if regex.search(author): |
| 93 |
return replacement |
| 94 |
return author |
| 95 |
|
| 96 |
|
| 97 |
def parse_file(filename): |
| 98 |
license = None |
| 99 |
authors = [] |
| 100 |
with open(filename) as file: |
| 101 |
try: |
| 102 |
data = file.readlines(500) |
| 103 |
except UnicodeDecodeError: |
| 104 |
data = [] |
| 105 |
authors = None |
| 106 |
for line in data: |
| 107 |
match = copyright_re.search(line) |
| 108 |
if match: |
| 109 |
copyright = match.group(1) |
| 110 |
max_year = min_year = int(copyright[:4]) |
| 111 |
if copyright[4] == '-': |
| 112 |
max_year = int(copyright[5:9]) |
| 113 |
author = copyright[10:] |
| 114 |
elif copyright[4:7] == ' - ': |
| 115 |
max_year = int(copyright[7:11]) |
| 116 |
author = copyright[12:] |
| 117 |
else: |
| 118 |
author = copyright[5:] |
| 119 |
author = canonicalize_author_name(author) |
| 120 |
authors.append((min_year, max_year, author)) |
| 121 |
match = header_re.search(line) |
| 122 |
if match: |
| 123 |
license = licenses_map[match.group(1)] |
| 124 |
if license and not authors: |
| 125 |
print(f'{filename} ({license}): No authors!', file=sys.stderr) |
| 126 |
elif verbose: |
| 127 |
if authors is None: |
| 128 |
print(f'{filename} (binary)') |
| 129 |
elif license is None: |
| 130 |
print(f'{filename} (unknown)') |
| 131 |
else: |
| 132 |
print(f'{filename} ({license})') |
| 133 |
return license, authors |
| 134 |
|
| 135 |
|
| 136 |
def get_source_files(root_directory): |
| 137 |
for dirpath, dirnames, filenames in os.walk(root_directory): |
| 138 |
for filename in filenames: |
| 139 |
full_path = os.path.join(dirpath, filename) |
| 140 |
if full_path.startswith('./'): |
| 141 |
full_path = full_path[2:] |
| 142 |
if any(full_path.startswith(prefix) for prefix in exclude_prefixes): |
| 143 |
continue |
| 144 |
yield full_path |
| 145 |
|
| 146 |
|
| 147 |
def format_list(title, strings): |
| 148 |
return title + ('\n' + ' ' * len(title)).join(strings) |
| 149 |
|
| 150 |
|
| 151 |
def main(root_directory): |
| 152 |
with open('debian/copyright') as copyright_file: |
| 153 |
current_copyright = copyright_file.read() |
| 154 |
start_pos = current_copyright.find(start_header) + len(start_header) + 1 |
| 155 |
start_data = current_copyright[:start_pos] |
| 156 |
end_pos = current_copyright.find(end_header) - 1 |
| 157 |
end_data = current_copyright[end_pos:] |
| 158 |
|
| 159 |
data = read_input(root_directory) |
| 160 |
|
| 161 |
with open('debian/copyright', 'w') as output_file: |
| 162 |
output_file.write(start_data) |
| 163 |
write_output(data, output_file) |
| 164 |
output_file.write(end_data) |
| 165 |
|
| 166 |
|
| 167 |
def read_input(root_directory): |
| 168 |
data = {} |
| 169 |
|
| 170 |
for filename in get_source_files(root_directory): |
| 171 |
license, authors = parse_file(filename) |
| 172 |
if license is None: |
| 173 |
continue |
| 174 |
if license not in data: |
| 175 |
data[license] = {} |
| 176 |
license_dict = data[license] |
| 177 |
authors_tuple = tuple(sorted({author[2] for author in authors})) |
| 178 |
if authors_tuple not in license_dict: |
| 179 |
license_dict[authors_tuple] = CopyrightInfo() |
| 180 |
license_dict[authors_tuple].add_file(authors, filename) |
| 181 |
return data |
| 182 |
|
| 183 |
|
| 184 |
def write_output(data, output_file): |
| 185 |
for license in sorted(data.keys()): |
| 186 |
output_file.write('\n## ' + license + '\n') |
| 187 |
license_dict = data[license] |
| 188 |
for authors in sorted(license_dict.keys()): |
| 189 |
if authors == (default_copyright,) and license in default_licenses: |
| 190 |
continue |
| 191 |
copyright_info = license_dict[authors] |
| 192 |
output_file.write('\n') |
| 193 |
output_file.write(format_list('Files: ', sorted(copyright_info.files)) + '\n') |
| 194 |
output_file.write(format_list('Copyright: ', copyright_info.get_strings(authors)) + '\n') |
| 195 |
output_file.write('License: ' + license + '\n') |
| 196 |
|
| 197 |
|
| 198 |
if __name__ == '__main__': |
| 199 |
main('.') |