-
Notifications
You must be signed in to change notification settings - Fork 65
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
feat: Add license utility to chectl to validate and add license header to codebase #1278
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# | ||
# Copyright (c) 2019-2021 Red Hat, Inc. | ||
# This program and the accompanying materials are made | ||
# available under the terms of the Eclipse Public License 2.0 | ||
# which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
# | ||
# SPDX-License-Identifier: EPL-2.0 | ||
# | ||
# Contributors: | ||
# Red Hat, Inc. - initial API and implementation | ||
|
||
export ROOT_DIR=$(dirname $(dirname $(readlink -f "$0"))); | ||
|
||
# Validate a Eclipse Che license header | ||
function validateChectlLicenseHeader() { | ||
python "${ROOT_DIR}"/.ci/validate-license.py $(find "${ROOT_DIR}" -type d \( -path "${ROOT_DIR}"/node_modules -o -path "${ROOT_DIR}"/templates \) -prune -false -o -name '*.sh' -o -name '*.ts' -o -name '*.yml' -o -name '*.yaml' \ | ||
| grep -v installers/cert-manager.yml) | ||
} | ||
|
||
# Add a license to a file without license | ||
function addLicensetoChectlCode() { | ||
if ! command -v addlicense &> /dev/null | ||
then | ||
echo "Command addlicense not found locally. Please install it from https://github.com/google/addlicense." | ||
exit 1 | ||
fi | ||
|
||
addlicense -v -f "${ROOT_DIR}"/license_header.txt $(find . -type d \( -path "${ROOT_DIR}"/node_modules -o -path "${ROOT_DIR}"/templates \) -prune -false -o \( -name '*.sh' -o -name '*.ts' -o -name '*.yml' -o -name '*.yaml' \)) | ||
} | ||
|
||
# catch first arguments with $1 | ||
case "$1" in | ||
-c|--check-license) | ||
echo -e "[INFO] Launching Eclipse Che license header check." | ||
validateChectlLicenseHeader | ||
;; | ||
-a|--add-license) | ||
echo -e "[INFO] Start adding Eclipse Che license headers to code." | ||
addLicensetoChectlCode | ||
;; | ||
*) | ||
# else | ||
echo "Usage: | ||
-c|--check-license: Check Eclipse license in codebase | ||
-a|--add-license: Add a license to codebase. The file should not have any license if you execute this command. | ||
" | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from __future__ import ( | ||
absolute_import, print_function, division, unicode_literals | ||
) | ||
|
||
import logging | ||
import re | ||
import sys | ||
from datetime import datetime | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
logger = logging.getLogger(__name__) | ||
|
||
CURRENT_YEAR = datetime.today().year | ||
|
||
COPYRIGHT_RE=r'Copyright \(c\) 2019-'+str(CURRENT_YEAR) + r' Red Hat, Inc.' | ||
PATTERN1=r'This program and the accompanying materials are made' | ||
PATTERN2=r'available under the terms of the Eclipse Public License 2.0' | ||
PATTERN3=r'which is available at https://www.eclipse.org/legal/epl-2.0/' | ||
PATTERN4=r'SPDX-License-Identifier: EPL-2.0' | ||
PATTERN5=r'Contributors:' | ||
PATTERN6=r'Red Hat, Inc. - initial API and implementation' | ||
ARRAY_OF_PATTERNS=[COPYRIGHT_RE, PATTERN6, PATTERN2, PATTERN3, PATTERN4, PATTERN5, PATTERN6] | ||
|
||
def update_go_license(name, force=False): | ||
with open(name) as f: | ||
orig_lines = list(f) | ||
lines = list(orig_lines) | ||
|
||
for pattern in ARRAY_OF_PATTERNS: | ||
try: | ||
validated = license_lines_check(lines,pattern) | ||
if validated is None: | ||
raise ValueError('Exception: Found an invalid license, file_name=%s, pattern=%s, success=%s' % (name, pattern, False)) | ||
except ValueError as err: | ||
print(err.args) | ||
sys.exit(1) | ||
print('Successfully validated license header', 'file_name=%s, success=%s' % (name, True)) | ||
|
||
def license_lines_check(lines, pattern): | ||
for i, line in enumerate(lines[:20]): | ||
found = False | ||
|
||
m = re.compile(pattern, re.I).search(line) | ||
if not m: | ||
continue | ||
found=True | ||
|
||
return found | ||
|
||
def main(): | ||
if len(sys.argv) == 1: | ||
print('USAGE: %s FILE ...' % sys.argv[0]) | ||
sys.exit(1) | ||
|
||
for name in sys.argv[1:]: | ||
if name.endswith('.ts') or name.endswith('.sh') or name.endswith('.yaml') or name.endswith('.yml'): | ||
try: | ||
update_go_license(name) | ||
except Exception as error: | ||
logger.error('Failed to process file %s', name) | ||
logger.exception(error) | ||
raise error | ||
else: | ||
raise NotImplementedError('Unsupported file type: %s' % name) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe for shell script we have to save #!/bin/bash ? I guess they will be broken without this directive...
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.
My mistake