Commit 14d4ec28 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add --min-size option to copyright.py

parent 0142bb78
Loading
Loading
Loading
Loading
+27 −4
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
Check if the given files contain a copyright notice mentioning the current year
"""

import argparse
import os
import re
import sys
import time
@@ -29,11 +31,13 @@ from typing import Iterator
from typing import List


def check_file(path: str, year: str) -> bool:
def check_file(path: Path, year: str, min_size: int) -> bool:
	"""
	Check for an up-to-date copyright notice in the first few lines of the given file
	"""
	with open(path) as file:
	with path.open() as file:
		if os.fstat(file.fileno()).st_size < min_size:
			return True
		lines = file.read(512)
	return bool(re.search(f'\\b(?:copyright)\\b.*\\b{year}\\b', lines, re.I))

@@ -89,11 +93,30 @@ def split_paths(paths: bytes) -> Iterator[Path]:
	return (Path(p.decode()) for p in paths.split(b'\x00') if p != b'')


def cli_parser() -> argparse.ArgumentParser:
	"""
	Return an argparse parser
	"""
	parser = argparse.ArgumentParser()

	parser.add_argument(
		'--min-size', '-m',
		type=int,
		default=-1,
		help="Minimum file size to check, anything smaller will be ignored",
	)

	parser.add_argument('files', nargs='+', type=Path)

	return parser


def main():
	"""
	CLI entrypoint
	"""
	paths = filter_excluded(Path(arg) for arg in sys.argv[1:])
	opts = cli_parser().parse_args()
	paths = filter_excluded(opts.files)

	year = time.strftime('%Y')
	years = {path: year for path in paths if path.is_file()}
@@ -102,7 +125,7 @@ def main():

	missing = []
	for path in paths:
		if path.is_file() and not check_file(path, years[path]):
		if path.is_file() and not check_file(path, years[path], opts.min_size):
			missing.append(path)

	if missing: