11#!/usr/bin/env python3
22
3- # check_license_headers - check that all source files have licensing information
3+ # check_headers.py - check that all source files have licensing information
44#
55# Copyright (C) 2023 Arthur de Jong
66#
2222"""This script checks that all source files have licensing information."""
2323
2424import glob
25+ import os
2526import re
2627import sys
2728import textwrap
2829
2930
31+ # Regext to match the first line
32+ identification_re = re .compile (
33+ r'^(#!/usr/bin/env python3?\s+|/\*\s+)?'
34+ r'(# coding: utf-8\s+)?'
35+ r'(\s?#\s+)?'
36+ r'(?P<filename>[^ :]+) - ' , re .MULTILINE )
37+
3038# Regex to match standard license blurb
3139license_re = re .compile (textwrap .dedent (r'''
3240 [# ]*This library is free software; you can redistribute it and/or
4654 ''' ).strip (), re .MULTILINE )
4755
4856
49- def file_has_correct_license (filename ):
50- """Check that the file contains a valid license header ."""
57+ def get_file_header (filename ):
58+ """Read the file header from the file ."""
5159 with open (filename , 'rt' ) as f :
5260 # Only read the first 2048 bytes to avoid loading too much and the
5361 # license information should be in the first part anyway.
54- contents = f .read (2048 )
55- return bool (license_re .search (contents ))
62+ return f .read (2048 )
5663
5764
5865if __name__ == '__main__' :
@@ -66,8 +73,17 @@ def file_has_correct_license(filename):
6673 glob .glob ('online_check/check.js' , recursive = True )
6774 )
6875
69- incorrect_files = [f for f in files_to_check if not file_has_correct_license (f )]
70- if incorrect_files :
71- print ('Files with incorrect license information:' )
72- print ('\n ' .join (incorrect_files ))
76+ # Look for files with incorrect first lines or license
77+ fail = False
78+ for filename in sorted (files_to_check ):
79+ contents = get_file_header (filename )
80+ m = identification_re .match (contents )
81+ if not bool (m ) or m .group ('filename' ) not in (filename , os .path .basename (filename )):
82+ print ('%s: Incorrect file identification' % filename )
83+ fail = True
84+ if not bool (license_re .search (contents )):
85+ print ('%s: Incorrect license text' % filename )
86+ fail = True
87+
88+ if fail :
7389 sys .exit (1 )
0 commit comments