-
Notifications
You must be signed in to change notification settings - Fork 24
Add pre-commit hook to verify required apache headers #91
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
|
|
||
| repos: | ||
| - repo: local | ||
| hooks: | ||
| - id: apache-license-header-check | ||
| name: apache license header check | ||
| entry: python3 scripts/apache_header_check.py | ||
| language: system | ||
| types: [python] | ||
| pass_filenames: true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright 2018 Comcast Cable Communications Management, LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import sys | ||
| import tokenize | ||
|
|
||
| license_to_check = "http://www.apache.org/licenses/LICENSE-2.0" | ||
|
|
||
| missing_license = False | ||
|
|
||
| #checks if the license_to_check is in a comment in the files | ||
| def is_string_in_comment(file_path, target_string): | ||
| with tokenize.open(file_path) as file: | ||
| # Loop through every token in the Python file | ||
| for token in tokenize.generate_tokens(file.readline): | ||
| # Check if the current token is a comment | ||
| if token.type == tokenize.COMMENT: | ||
| # Check if apache link string is inside that comment | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks pretty good, but it currently allows for the Apache link string to be in a comment anywhere in the file. For example, a Python file with no header but a trailing comment such as |
||
| if target_string in token.string: | ||
| return True | ||
| return False | ||
|
|
||
| #Iterate through the .py files passed by the .pre-commit-config.yaml file | ||
| for filepath in sys.argv[1:]: | ||
| with open(filepath, "r", encoding="utf-8") as f: | ||
|
|
||
| #if license is not in header print it out in the pre commit error | ||
| if not is_string_in_comment(filepath, license_to_check): | ||
| print(f"Error: Missing Apache License header in {filepath}") | ||
| missing_license = True | ||
|
|
||
| if missing_license: | ||
| sys.exit(1) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nitpicky, but can we remove the blank lines at the beginning and end of this file?