Skip to content

Commit b8ee830

Browse files
committed
Extend license check to file header check
This also checks that the file name referenced in the file header is correct.
1 parent ef49f49 commit b8ee830

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
strategy:
6161
fail-fast: false
6262
matrix:
63-
tox_job: [docs, flake8, license]
63+
tox_job: [docs, flake8, headers]
6464
steps:
6565
- uses: actions/checkout@v3
6666
- name: Set up Python
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
#
@@ -22,11 +22,19 @@
2222
"""This script checks that all source files have licensing information."""
2323

2424
import glob
25+
import os
2526
import re
2627
import sys
2728
import 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
3139
license_re = re.compile(textwrap.dedent(r'''
3240
[# ]*This library is free software; you can redistribute it and/or
@@ -46,13 +54,12 @@
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

5865
if __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)

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,35,36,37,38,39,310,311,py,py3},flake8,docs,license
2+
envlist = py{27,35,36,37,38,39,310,311,py,py3},flake8,docs,headers
33
skip_missing_interpreters = true
44

55
[testenv]
@@ -35,7 +35,7 @@ use_develop = true
3535
deps = Sphinx
3636
commands = sphinx-build -N -b html docs {envtmpdir}/sphinx -W
3737

38-
[testenv:license]
38+
[testenv:headers]
3939
skip_install = true
4040
deps =
41-
commands = python scripts/check_license_headers.py
41+
commands = python scripts/check_headers.py

0 commit comments

Comments
 (0)