-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add license utility to chectl to validate and add license heade…
…r to code Signed-off-by: Flavius Lacatusu <[email protected]>
- Loading branch information
Showing
110 changed files
with
837 additions
and
368 deletions.
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, fileName=%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.