Skip to content

Commit

Permalink
refactor/ci: ci and pre-commit use same script to check for license tag
Browse files Browse the repository at this point in the history
  • Loading branch information
goerlibe committed Sep 11, 2023
1 parent 34f05da commit 60f5ad1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 125 deletions.
25 changes: 1 addition & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,7 @@ jobs:
uses: actions/checkout@v3

- name: "Check all files for DiscoPoP License tag"
run: |
ERROR=""
for FILE in $(find . -type f -not -path "**/.git/**" -not -path "**/test/**" \
-not -path "**/docs/**" \
-not -path "**/LICENSE" -not -path "**/VERSION" \
-not -path "**/_version.py" -not -path "**/__init__.py" -not -path "**/py.typed" \
-not -path "**.png" -not -path "**.svg" -not -path "**.ico" \
)
do
FILE_ERROR=""
head -n 20 ${FILE} | grep -q "DiscoPoP software" || FILE_ERROR="yes"
[ -z "$FILE_ERROR" ] || ERROR="yes"
[ -z "$FILE_ERROR" ] || echo "Missing License tag at: ${FILE}"
[ -z "$FILE_ERROR" ] || continue
head -n 20 ${FILE} | grep -q "Technische Universitaet Darmstadt, Germany" || FILE_ERROR="yes"
[ -z "$FILE_ERROR" ] || ERROR="yes"
[ -z "$FILE_ERROR" ] || echo "Missing License tag at: ${FILE}"
[ -z "$FILE_ERROR" ] || continue
head -n 20 ${FILE} | grep -q "3-Clause BSD License" || FILE_ERROR="yes"
[ -z "$FILE_ERROR" ] || ERROR="yes"
[ -z "$FILE_ERROR" ] || echo "Missing License tag at: ${FILE}"
done
# Report error (1), if license tags are missing
[ -z "$ERROR" ] || exit 1
run: ./scripts/dev/check-license.sh $(find . -type f)

- name: Setup Python
uses: actions/setup-python@v4
Expand Down
4 changes: 1 addition & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ repos:
hooks:
- id: licensetag
name: Check all files for DiscoPoP License tag
entry: scripts/dev/check_license.sh
entry: scripts/dev/check-license.sh
language: script
# exclude: we could exclude files here, but we do it in the script instead

# TODO the script should also be used by the CI pipeline
59 changes: 59 additions & 0 deletions scripts/dev/check-license.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# This file is part of the DiscoPoP software (http://www.discopop.tu-darmstadt.de)
#
# Copyright (c) 2020, Technische Universitaet Darmstadt, Germany
#
# This software may be modified and distributed under the terms of
# the 3-Clause BSD License. See the LICENSE file in the package base
# directory for details.


# this script is given a list of filenames as arguments
# it checks if the files contain the license tag
# if not, it prints an error message and exits with exit code 1
# if all files contain the license tag, it exits with exit code 0
# some files are ignored, see below

ERROR=""
for file in "$@"; do
# no license tag is required in the following locations:

# binary files
[[ "$file" = *".png" ]] && continue
[[ "$file" = *".svg" ]] && continue
[[ "$file" = *".ico" ]] && continue

# python:
[[ "$file" = *"/_version.py" ]] && continue
[[ "$file" = *"/__init__.py" ]] && continue # we might want to improve this: only ignore __init__.py files if they are empty
[[ "$file" = *"/py.typed" ]] && continue
# not needed, as they are ignored using .gitignore
#[[ "$file" = *"/__pycache__/"* ]] && continue
#[[ "$file" = *"/.mypy_cache/"* ]] && continue
#[[ "$file" = *"/venv/"* ]] && continue
#[[ "$file" = *"/env/"* ]] && continue
#[[ "$file" = *".egg-info/"* ]] && continue

# other
[[ "$file" = *".git/"* ]] && continue
[[ "$file" = *"test/"* ]] && continue
[[ "$file" = *"docs/"* ]] && continue
[[ "$file" = *"build/"* ]] && continue
[[ "$file" = *"LICENSE" ]] && continue
[[ "$file" = *"VERSION" ]] && continue

# check for the license tag in the first 20 lines of the file
FILE_ERROR=""
head -n 20 ${file} | grep -q "DiscoPoP software" || FILE_ERROR="yes"
head -n 20 ${file} | grep -q "Technische Universitaet Darmstadt, Germany" || FILE_ERROR="yes"
head -n 20 ${file} | grep -q "3-Clause BSD License" || FILE_ERROR="yes"
[ -z "$FILE_ERROR" ] || ERROR="yes"
[ -z "$FILE_ERROR" ] || echo "Missing License tag at: ${file}"
done

# Report error (1), if license tags are missing
[ -z "$ERROR" ] || exit 1

# Report success (0), if license tags are present
exit 0
98 changes: 0 additions & 98 deletions scripts/dev/check_license.sh

This file was deleted.

0 comments on commit 60f5ad1

Please sign in to comment.