diff --git a/scripts/bootstrapit_update.sh b/scripts/bootstrapit_update.sh index 69e13b8..619ea63 100644 --- a/scripts/bootstrapit_update.sh +++ b/scripts/bootstrapit_update.sh @@ -3,15 +3,15 @@ set -Ee -o pipefail; shopt -s extglob nocasematch; -if [[ -f ${PROJECT_DIR}/.git/config ]]; then - OLD_PWD=${PWD} - cd ${PROJECT_DIR}; - if [[ $( git diff -s --exit-code || echo $? ) -gt 0 ]]; then +if [[ -f "$PROJECT_DIR/.git/config" ]]; then + OLD_PWD="$PWD" + cd "$PROJECT_DIR"; + if [[ $( git diff -s --exit-code || echo "$?" ) -gt 0 ]]; then echo "ABORTING!: Your repo has local changes which are not comitted." echo "To avoid overwriting these changes, please commit your changes." exit 1; fi - cd $OLD_PWD; + cd "$OLD_PWD"; fi BOOTSTRAPIT_GIT_URL="https://gitlab.com/mbarkhau/bootstrapit.git/" @@ -20,26 +20,27 @@ BOOTSTRAPIT_GIT_PATH=/tmp/bootstrapit; echo "Updating from $BOOTSTRAPIT_GIT_URL"; -if [[ ! -e $BOOTSTRAPIT_GIT_PATH ]]; then - git clone ${BOOTSTRAPIT_GIT_URL} ${BOOTSTRAPIT_GIT_PATH}; +if [[ ! -e "$BOOTSTRAPIT_GIT_PATH" ]]; then + git clone "$BOOTSTRAPIT_GIT_URL" "$BOOTSTRAPIT_GIT_PATH"; else - OLD_PWD=${PWD}; - cd ${BOOTSTRAPIT_GIT_PATH}; + OLD_PWD="$PWD"; + cd "$BOOTSTRAPIT_GIT_PATH"; git pull --quiet; - cd $OLD_PWD; + cd "$OLD_PWD"; fi md5sum=$(which md5sum || which md5) -old_md5=$( cat $PROJECT_DIR/scripts/bootstrapit_update.sh | $md5sum ); -new_md5=$( cat $BOOTSTRAPIT_GIT_PATH/scripts/bootstrapit_update.sh | $md5sum ); +old_md5=$( cat "$PROJECT_DIR/scripts/bootstrapit_update.sh" | $md5sum ); +new_md5=$( cat "$BOOTSTRAPIT_GIT_PATH/scripts/bootstrapit_update.sh" | $md5sum ); -if [[ $old_md5 != $new_md5 ]]; then +if [[ "$old_md5" != "$new_md5" ]]; then # Copy the updated file, run it and exit the current execution. cp "${BOOTSTRAPIT_GIT_PATH}/scripts/bootstrapit_update.sh" \ "${PROJECT_DIR}/scripts/"; git add "${PROJECT_DIR}/scripts/bootstrapit_update.sh"; git commit --no-verify -m "auto update of scripts/bootstrapit_update.sh" + # shellcheck source=scripts/bootstrapit_update.sh source "${PROJECT_DIR}/scripts/bootstrapit_update.sh"; exit 0; fi @@ -58,7 +59,7 @@ declare -a required_config_param_names=( "LICENSE_ID" ) -for name in "${required_config_param_names}"; do +for name in "${required_config_param_names[@]}"; do if [[ -z ${!name} ]]; then echo "Missing parameter $name in $1"; exit 1; @@ -74,7 +75,7 @@ if [[ -z $DEFAULT_PYTHON_VERSION ]]; then fi if [[ -z $SPDX_LICENSE_ID ]]; then - if [[ $LICENSE_ID =~ "none" ]]; then + if [[ $LICENSE_ID =~ none ]]; then SPDX_LICENSE_ID="Proprietary"; else SPDX_LICENSE_ID=$LICENSE_ID; @@ -90,27 +91,27 @@ LICENSE_TXT_FILE="/tmp/bootstrapit_$LICENSE_ID.txt" LICENSE_XML_FILE="/tmp/bootstrapit_$LICENSE_ID.xml" -if ! [[ $LICENSE_ID =~ "none" ]]; then - if ! [[ -f $LICENSE_TXT_FILE ]]; then +if ! [[ $LICENSE_ID =~ none ]]; then + if ! [[ -f "$LICENSE_TXT_FILE" ]]; then echo "Downloading license text from $LICENSE_TXT_URL" - curl -L --silent $LICENSE_TXT_URL > $LICENSE_TXT_FILE.tmp; - mv $LICENSE_TXT_FILE.tmp $LICENSE_TXT_FILE; + curl -L --silent "$LICENSE_TXT_URL" > "$LICENSE_TXT_FILE.tmp"; + mv "$LICENSE_TXT_FILE.tmp" "$LICENSE_TXT_FILE"; fi - if ! [[ -f $LICENSE_XML_FILE ]]; then + if ! [[ -f "$LICENSE_XML_FILE" ]]; then echo "Downloading license info from $LICENSE_XML_URL" - curl -L --silent $LICENSE_XML_URL > $LICENSE_XML_FILE.tmp; - mv $LICENSE_XML_FILE.tmp $LICENSE_XML_FILE; + curl -L --silent "$LICENSE_XML_URL" > "$LICENSE_XML_FILE.tmp"; + mv "$LICENSE_XML_FILE.tmp" "$LICENSE_XML_FILE"; fi fi if [[ -z $LICENSE_NAME ]]; then - if [[ $LICENSE_ID =~ "none" ]]; then + if [[ $LICENSE_ID =~ none ]]; then LICENSE_NAME="All Rights Reserved"; else LICENSE_NAME=$( awk '{ if ($0 ~ /[^>]\s*$/ ) { printf "%s", $0 } else {printf "%s\n", $0 } }' \ - $LICENSE_XML_FILE \ + "$LICENSE_XML_FILE" \ | grep " $PROJECT_DIR/LICENSE; +if [[ "$LICENSE_ID" =~ "none" ]]; then + echo "$COPYRIGHT_STRING" > "$PROJECT_DIR/LICENSE"; else - cat $LICENSE_TXT_FILE \ - | sed "s/Copyright (c) [[:space:]]*/Copyright (c) $YEAR $AUTHOR_NAME ($AUTHOR_CONTACT)/g" \ - | sed "s/Copyright (c) [[:space:]]*/Copyright (c) $YEAR $AUTHOR_NAME ($AUTHOR_CONTACT)/g" \ - > $PROJECT_DIR/LICENSE; + cat "$LICENSE_TXT_FILE" \ + | sed "s/Copyright (c) [[:space:]]*/Copyright (c) $YEAR $AUTHOR_NAME ($AUTHOR_CONTACT)/g" \ + | sed "s/Copyright (c) [[:space:]]*/Copyright (c) $YEAR $AUTHOR_NAME ($AUTHOR_CONTACT)/g" \ + > "$PROJECT_DIR/LICENSE"; fi function format_template() { - cat $1 \ + cat "$1" \ | sed "s;\${GIT_REPO_URL};${GIT_REPO_URL};g" \ | sed "s;\${GIT_REPO_PATH};${GIT_REPO_PATH};g" \ | sed "s;\${GIT_REPO_NAMESPACE};${GIT_REPO_NAMESPACE};g" \ @@ -225,24 +235,50 @@ function format_template() | sed "s;\${DESCRIPTION};${DESCRIPTION};g" \ | sed "s;\${KEYWORDS};${KEYWORDS};g" \ | sed "s;\${SPDX_LICENSE_ID};${SPDX_LICENSE_ID};g" \ + | sed "s;\${SETUP_PY_LICENSE};${SETUP_PY_LICENSE};g" \ | sed "s;\${LICENSE_CLASSIFIER};${LICENSE_CLASSIFIER};g" \ | sed "s;\${COPYRIGHT_STRING};${COPYRIGHT_STRING};g" \ | sed "s;\${YEAR};${YEAR};g" \ | sed "s;\${MONTH};${MONTH};g" \ - > $1.tmp; - mv $1.tmp $1; + > "$1.tmp"; + mv "$1.tmp" "$1"; } +if [[ -z "${IGNORE_IF_EXISTS[*]}" ]]; then + declare -a IGNORE_IF_EXISTS=( + "CHANGELOG.md" + "README.md" + "setup.py" + "makefile.config.make" + "requirements/pypi.txt" + "requirements/development.txt" + "requirements/conda.txt" + "requirements/vendor.txt" + "src/${MODULE_NAME}/__init__.py" + "src/${MODULE_NAME}/__main__.py" + ) +fi + function copy_template() { if [[ -z ${2} ]]; then - dest_path=${PROJECT_DIR}/$1; + dest_subpath=$1; else - dest_path=${PROJECT_DIR}/$2; - fi; - cat ${BOOTSTRAPIT_GIT_PATH}/$1.template > ${dest_path}; + dest_subpath=$2; + fi - format_template ${dest_path}; + dest_path=${PROJECT_DIR}/$dest_subpath; + if [[ -f "$dest_subpath" ]]; then + for ignore_item in "${IGNORE_IF_EXISTS[@]}"; do + if [[ "$dest_subpath" == "$ignore_item" ]]; then + return 0; + fi + done + fi + + cat "${BOOTSTRAPIT_GIT_PATH}/$1.template" > "$dest_path"; + + format_template "$dest_path"; } mkdir -p "${PROJECT_DIR}/test/"; @@ -288,32 +324,32 @@ chmod +x "${PROJECT_DIR}/scripts/update_conda_env_deps.sh"; chmod +x "${PROJECT_DIR}/scripts/setup_conda_envs.sh"; chmod +x "${PROJECT_DIR}/scripts/pre-push-hook.sh"; -head -n 7 ${PROJECT_DIR}/license.header \ +head -n 7 "${PROJECT_DIR}/license.header" \ | tail -n +3 \ | sed -re 's/(^ |^$)/#/g' \ - > .py_license.header; + > /tmp/.py_license.header; -src_files=${PROJECT_DIR}/src/**/*.py +src_files="${PROJECT_DIR}/src/*/*.py" for src_file in $src_files; do - if grep -q -E '^# SPDX-License-Identifier' $src_file; then + if grep -q -E '^# SPDX-License-Identifier' "$src_file"; then continue; fi offset=0 - if grep -z -q -E '^#![/a-z ]+?python' $src_file; then + if grep -z -q -E '^#![/a-z ]+?python' "$src_file"; then let offset+=1; fi - if grep -q -E '^# .+?coding: [a-zA-Z0-9_\-]+' $src_file; then + if grep -q -E '^# .+?coding: [a-zA-Z0-9_\-]+' "$src_file"; then let offset+=1; fi - rm -f ${src_file}.with_header; + rm -f "${src_file}.with_header"; if [[ $offset -gt 0 ]]; then - head -n $offset ${src_file} > ${src_file}.with_header; + head -n $offset "${src_file}" > "${src_file}.with_header"; fi let offset+=1; - cat .py_license.header >> ${src_file}.with_header; - tail -n +$offset ${src_file} >> ${src_file}.with_header; - mv ${src_file}.with_header $src_file; + cat /tmp/.py_license.header >> "${src_file}.with_header"; + tail -n +$offset "${src_file}" >> "${src_file}.with_header"; + mv "${src_file}.with_header" "$src_file"; done -rm .py_license.header +rm /tmp/.py_license.header