Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copyright checker #100

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/global.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: global
on:
pull_request:
paths:
- '.github/workflows/global.yml'
- '.pre-commit-config.yaml'
- 'scripts/check_copyright_notice.py'
- '**/*.go'
- '!**/docs/**/*'
- '!**/*.md'

jobs:
copyright:
runs-on: ubuntu-latest
steps:
- name: Checkout devtools
uses: actions/checkout@v4

- name: Check copyright notice
run: |
pip install \
pre-commit \
python-magic==0.4.18 \
comment-parser>=1.2.3
pre-commit run --all-files
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: local
hooks:
- id: check-copyright-notice
name: check-copyright-notice
description: Check copyright notice
entry: python3 scripts/check_copyright_notice.py
language: system
types_or: [go]
86 changes: 86 additions & 0 deletions scripts/check_copyright_notice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -------------------------------------------------------
# Copyright (c) 2023 Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
# -------------------------------------------------------

"""
Checks the presence of copyright notice in the files
"""

from typing import Optional, Sequence
import argparse
import os
import sys
import re
import magic
from comment_parser import comment_parser

COPYRIGHT_TEXT = "Copyright (c) <ValidYear>"
LICENSE_TEXT = "SPDX-License-Identifier: Apache-2.0"

def check_file(filename: str, copyright_reg_exp: re.Pattern) -> int:
"""
Checks a file for the presence of a comment in the form of a copyright
and license notice.
@param filename: The name of the file to check.
@param copyright_reg_exp A regular expression giving the format of the
copyright notice (exclusing language-specific comment chars).
@return 0 If the copyright & license notice are found, otherwise 1.
"""
if os.path.getsize(filename) == 0:
return 0

mime_type = magic.from_file(filename, mime=True)
if mime_type == "text/plain":
mime_type = "text/x-c++"

copyrightfound=False
licensefound=False
comments = ""
for comment in comment_parser.extract_comments(filename,
mime=mime_type):
comments += comment.text() + '\n'

if copyright_reg_exp.search(comments):
copyrightfound=True
if comments.find(LICENSE_TEXT) != -1:
licensefound=True

if copyrightfound and licensefound:
return 0

errstr = ""
if not copyrightfound:
errstr = "\n\t # Missing or invalid copyright text. Please follow format: " + COPYRIGHT_TEXT
if not licensefound:
errstr += "\n\t # Missing or invalid license text. Please write : " + LICENSE_TEXT

print(f"# Copyright check error(s) in : {filename} {errstr}")
return 1

def main(argv: Optional[Sequence[str]] = None) -> int:
"""
Entry point that checks for copyright notices being present in all the
files supplied on the command-line.
@param argv: The names of the files to check.
@return Non-zero if one or more of the passed files was missing a copyright
notice.
"""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)

print("Checking copyright headers...")
ret = 0
copyright_reg_exp=re.compile(r"(Copyright\s\(c\)\s(19|20)[0-9][0-9][^0-9])")
for filename in args.filenames:
ret |= check_file(filename, copyright_reg_exp)

if ret != 0:
print(">> error: Files are missing a valid copyright header")

return ret

if __name__ == '__main__':
sys.exit(main())