From d4f2a37e4b57183ab0796535569f0536bb1186f8 Mon Sep 17 00:00:00 2001 From: Joshua Lockerman Date: Mon, 15 Oct 2018 17:18:19 -0400 Subject: [PATCH] Add automated license check to travis --- .travis.yml | 3 ++- CMakeLists.txt | 7 +++++ scripts/c_license_header.h | 6 +++++ scripts/check_file_license_apache.sh | 38 ++++++++++++++++++++++++++++ scripts/check_license_apache.sh | 24 ++++++++++++++++++ scripts/sql_license.sql | 5 ++++ scripts/test_license.sql | 5 ++++ 7 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 scripts/c_license_header.h create mode 100755 scripts/check_file_license_apache.sh create mode 100755 scripts/check_license_apache.sh create mode 100644 scripts/sql_license.sql create mode 100644 scripts/test_license.sql diff --git a/.travis.yml b/.travis.yml index 66b96a15a..1c24fca3c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -98,7 +98,7 @@ jobs: # This tests the formatting of a PR. - if: (type = pull_request) OR (type = cron) OR NOT (branch = master) stage: test - name: "pgindent" + name: "pgindent and license check" env: - PG_VERSION=10.2 PG_GIT_TAG=REL_10_2 before_install: @@ -112,6 +112,7 @@ jobs: - docker exec -it pgbuild /bin/bash -c 'cd /build/debug && make pgindent' - docker exec -it pgbuild /bin/bash -c 'diff -r -q /build/src /tmp/timescale_src' - docker exec -it pgbuild /bin/bash -c 'diff -r -q /build/test/src /tmp/timescale_test_src' + - docker exec -it pgbuild /bin/bash -c 'cd /build/debug && make licensecheck' - if: type = cron stage: test diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ce42d75d..56b634d39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,3 +300,10 @@ endif (UNIX) add_subdirectory(test) add_subdirectory(sql) add_subdirectory(src) + +add_custom_target(licensecheck + SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_license_apache.sh -c ${CMAKE_CURRENT_SOURCE_DIR}/src + COMMAND SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_license_apache.sh -s ${CMAKE_CURRENT_SOURCE_DIR}/sql + COMMAND SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_license_apache.sh -c ${CMAKE_CURRENT_SOURCE_DIR}/test + COMMAND SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/check_license_apache.sh -t ${CMAKE_CURRENT_SOURCE_DIR}/test + USES_TERMINAL) diff --git a/scripts/c_license_header.h b/scripts/c_license_header.h new file mode 100644 index 000000000..d87e4ac78 --- /dev/null +++ b/scripts/c_license_header.h @@ -0,0 +1,6 @@ +/* + * Copyright (c) 2016-2018 Timescale, Inc. All Rights Reserved. + * + * This file is licensed under the Apache License, + * see LICENSE-APACHE at the top level directory. + */ diff --git a/scripts/check_file_license_apache.sh b/scripts/check_file_license_apache.sh new file mode 100755 index 000000000..0336d6cf8 --- /dev/null +++ b/scripts/check_file_license_apache.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +FLAG=${1} +FILE=${2} +SCRIPTPATH="$( cd "$(dirname "${0}")" ; pwd -P )" +TIMESCALE_LOCATION=`dirname ${SCRIPTPATH}` + +LICENSE_FILE= +LICENSE_STRING= +FIRST_COMMENT= + +if [[ "${FLAG}" == '-c' ]]; then + LICENSE_FILE=${SCRIPTPATH}/c_license_header.h + LICENSE_STRING=`awk 'BEGIN {ORS="\0"}{if($1 == "*/") {print; exit;}} {print}' ${LICENSE_FILE}` + FIRST_COMMENT=`awk 'BEGIN {ORS="\0"}{if($1 == "*/") {print; exit;}} {print}' ${FILE}` +elif [[ "${FLAG}" == '-s' ]]; then + LICENSE_FILE=${SCRIPTPATH}/sql_license.sql + LICENSE_STRING=`awk 'BEGIN {ORS="\0"}{if($1 == "") {print; exit;}} {print}' ${SCRIPTPATH}/sql_license.sql` + FIRST_COMMENT=`awk 'BEGIN {ORS="\0"}{if($1 == "") {print; exit;}} {print}' ${FILE}` +elif [[ "${FLAG}" == '-t' ]]; then + LICENSE_FILE=${SCRIPTPATH}/test_license.sql + LICENSE_STRING=`awk 'BEGIN {ORS="\0"}{if($1 == "") {print; exit;}} {print}' ${SCRIPTPATH}/test_license.sql` + FIRST_COMMENT=`awk 'BEGIN {ORS="\0"}{if($1 == "") {print; exit;}} {print}' ${FILE}` +else + echo "Unkown flag" ${1} + exit 1; +fi + +if [[ "${FIRST_COMMENT}" != "${LICENSE_STRING}" ]]; then + echo ${FILE#"$TIMESCALE_LOCATION/"} "lacks a license header. Add"; + echo + cat ${LICENSE_FILE} + echo + echo "to the top of the file"; + exit 1; +fi + +exit 0; diff --git a/scripts/check_license_apache.sh b/scripts/check_license_apache.sh new file mode 100755 index 000000000..2c2f5f7f8 --- /dev/null +++ b/scripts/check_license_apache.sh @@ -0,0 +1,24 @@ +#! /bin/bash + +SUFFIX0= +SUFFIX1= + +if [[ ${1} == '-c' ]]; then +SUFFIX0='*.c' +SUFFIX1='*.h' +else +SUFFIX0='*.sql' +SUFFIX1='*.sql' +fi + +SCRIPT_DIR=$(dirname ${0}) +SRC_DIR=$(dirname ${SCRIPT_DIR}) + +# we skip license checks for: +# - the update script fragments, because the generated update scripts will +# contain the license at top, and we don't want to repeat it in the middle +# - test/sql/dump which contains auto-generated code +# - src/chunk_adatptive since it's still in BETA + +find $2 -type f \( -name "${SUFFIX0}" -or -name "${SUFFIX1}" \) -and -not -path "${SRC_DIR}/sql/updates/*.sql" -and -not -path "${SRC_DIR}/test/sql/dump/*.sql" -and -not -path "${SRC_DIR}/src/chunk_adaptive.*" -print0 | xargs -0 -n1 $(dirname ${0})/check_file_license_apache.sh ${1} + diff --git a/scripts/sql_license.sql b/scripts/sql_license.sql new file mode 100644 index 000000000..884bad73f --- /dev/null +++ b/scripts/sql_license.sql @@ -0,0 +1,5 @@ +-- Copyright (c) 2016-2018 Timescale, Inc. All Rights Reserved. +-- +-- This file is licensed under the Apache License, see LICENSE-APACHE +-- at the top level directory of the timescaledb distribution. + diff --git a/scripts/test_license.sql b/scripts/test_license.sql new file mode 100644 index 000000000..f90eb1bcc --- /dev/null +++ b/scripts/test_license.sql @@ -0,0 +1,5 @@ +-- Copyright (c) 2016-2018 Timescale, Inc. All Rights Reserved. +-- +-- This file is licensed under the Apache License, +-- see LICENSE-APACHE at the top level directory. +