From 4308699484e91053b471f64fdf29149087d8cd75 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 30 Jun 2022 17:28:12 -0500 Subject: Update to version 1.0 --- COPYING | 24 ++++++ LICENSE | 19 ----- Makefile | 19 +++-- licenser | 181 --------------------------------------------- licensor | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 287 insertions(+), 207 deletions(-) create mode 100644 COPYING delete mode 100644 LICENSE delete mode 100755 licenser create mode 100755 licensor diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..0a06d2f --- /dev/null +++ b/COPYING @@ -0,0 +1,24 @@ + +Copyright (C) 2022 Case Duckworth + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 44c8493..0000000 --- a/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright 2020 Case Duckworth - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/Makefile b/Makefile index 6f7723b..cca469c 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,20 @@ # licenser -PREFIX ?= /usr +PREFIX = /usr/local -.PHONY: def install uninstall +.PHONY: help install uninstall -def: - @echo "run 'make install' to install" - @echo "run 'make uninstall' to uninstall" +help: + @echo "licensor : drop urself a license" + @echo "(C) 2021--2022 Case Duckworth " + @echo + @echo "TARGETS:" + @echo " install Install licensor to $(DESTDIR)$(PREFIX)/bin/licensor." + @echo " uninstall Uninstall licensor-related files." -install: licenser +install: licensor install -D $< $(DESTDIR)$(PREFIX)/bin uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/licenser + rm -f $(DESTDIR)$(PREFIX)/bin/licensor + rm -rf "${XDG_CACHE_HOME:-$HOME/.cache}/licensor" diff --git a/licenser b/licenser deleted file mode 100755 index 2337470..0000000 --- a/licenser +++ /dev/null @@ -1,181 +0,0 @@ -#!/bin/sh -# licenser: drop a little ol license in there -# Author: Case Duckworth -# License: MIT -# Version: 0.2 - -# Constants -## where to download licenses from -PRGN="${0##*/}" -: "${LICENSER_SOURCE:=https://git.sr.ht/~acdw/licenser-licenses/blob/master}" -LICENSER_CACHE="${XDG_DATA_HOME:-$HOME/.local/share}/licenser" -LICENSER_LICENSE="${LICENSER_LICENSE:-MIT}" -EXEC="${EXEC:-true}" - -usage() { - cat <"$__OUT" - - log "$__LICENSE written to $__OUT." -} - -# download a license, or get it from the cache -# if there's not one, return error -get_license() { - license="$1" - cached="$LICENSER_CACHE/$license/$__LANG" - source="$LICENSER_SOURCE/$license/$__LANG" - - mkdir -p "$LICENSER_CACHE/$license" - log "Checking cache for $license..." - if [ ! -f "$cached" ]; then - log "Not found. Downloading from $source..." - if ! curl -fLo "$cached" "$source"; then - log "License $license unavailable in language: $__LANG" - return 1 - fi - fi - put "$cached" -} - -# download a list of the licenses, or get it from the cache -list_licenses() { - cached="$LICENSER_CACHE/manifest" - source="$LICENSER_SOURCE/manifest" - - [ "x$1" = "x-r" ] && rm "$cached" - - mkdir -p "$LICENSER_CACHE" - log "Checking cache for manifest..." - if [ ! -f "$cached" ]; then - log "Not found. Downloading from $source..." - if ! curl -fLo "$cached" "$LICENSER_SOURCE/manifest"; then - log "Unable to get the manifest" - return 1 - fi - fi - cat "$cached" -} - -# template function for the copyright year -year() { - case "$__YEAR" in - '') # figure out the year - date +%Y ;; - *) # one was provided - put "$__YEAR" ;; - esac -} - -# template function for author/owner -author() { - if [ -z "$__AUTHOR" ]; then - __AUTHOR="$(git config --get user.name)" # try from git - [ -z "$__AUTHOR" ] && __AUTHOR="$( - getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}' - )" # or, try from getent - [ -z "$__AUTHOR" ] && __AUTHOR="$USER" # give up, get $USER - fi - put "$__AUTHOR" -} - -email() { - if [ -z "$__EMAIL" ]; then - __EMAIL="$(git config --get user.email)" # try from git - fi - [ -n "$__EMAIL" ] && put "<$__EMAIL>" -} - -# helpers -put() { printf '%s\n' "$*"; } -log() { $_quiet || put "$PRGN: $*" >&2; } - -# run -$EXEC && licenser "$@" diff --git a/licensor b/licensor new file mode 100755 index 0000000..47da938 --- /dev/null +++ b/licensor @@ -0,0 +1,251 @@ +#!/bin/sh + +# Constants +PRGN="${0##*/}" +LICENSOR_CACHE="${XDG_CACHE_HOME:-$HOME/.cache/licensor" +# Configuration +: "${LICENSE_REPO_VERSION:=3.17}" +: "${LICENSE_REPO_URL:=https://github.com/spdx/license-list-data/archive/refs/tags/v$LICENSE_REPO_VERSION.tar.gz}" +: "${LICENSE_REPO_PATH:=license-list-data-$LICENSE_REPO_VERSION/template}" +: "${LICENSOR_LICENSE:=MIT}" +: "${LICENSOR_OUTPUT_FILE:=COPYING}" + +usage() { + cat <"$__output" + [ "$__output" != /dev/stdout ] && log "$__license license written to $__output." +} + +get_licenses() { + # Get licenses from cache, or download them + if ! [ -d "$LICENSOR_CACHE" ] || [ "x$1" = "x-f" ]; then + log "Downlading licenses from $LICENSE_REPO_URL..." + mkdir -p "$LICENSOR_CACHE" + tmpfile="/tmp/licenses.tar.gz" + if ! [ -f "$tmpfile" ]; then + curl -o "$tmpfile" -L "$LICENSE_REPO_URL" >/dev/null 2>&1 || + return 1 + fi + log "Extracting licenses to $LICENSOR_CACHE..." + if tar -C "$LICENSOR_CACHE" \ + -xvf "$tmpfile" --strip-components=2 \ + "$LICENSE_REPO_PATH" >/dev/null 2>&1; then + rm "$tmpfile" + else + return 1 + fi + fi +} + +get_license() { + list_licenses | grep -iE '^'"$1"'$' || { + log "Can't find license \"$1\"." + exit 1 + } +} + +list_licenses() { + get_licenses "$1" || exit 1 + + find "$LICENSOR_CACHE" -iname '*.template.txt' | + sort | + xargs basename -s .template.txt +} + +search_licenses() { + list_licenses | grep -iE "$1" + exit $? +} + +guess_author() { + author="$(git config --get user.name)" + [ -z "$author" ] && + author="$(getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}')" + [ -z "$author" ] && + author="$USER" + put "$author" +} +guess_email() { + email="$(git config --get user.email)" + put "$email" +} + +put() { printf '%s\n' "$*"; } +log() { $_quiet || put "$PRGN: $*" >&2; } + +license_convert() { + copyright="$1" # "Copyright (C) 2022 Case Duckworth " + show_optional="${2:-0}" # 0 + fold_output="${3:-1}" # 1 + fold_width="${4:-70}" # 70 + awk ' +BEGIN { + foldOutput = '"$fold_output"' + foldWidth = '"$fold_width"' + showOptional = '"$show_optional"' + copyright = "'"$copyright"'" +} +{ buf = buf "\n" $0 } +END { + split(buf, b, "") + c = 1 + out = "" + optstr = "" + opt = 0 + while (c <= length(b)) { + if (b[c] == "<" && b[c + 1] == "<") { + tok = "" + c += 2 + closed = 0 + while (! closed) { + tok = tok b[c++] + if (b[c] == ">" && b[c + 1] == ">") { + closed++ + c += 2 + } + } + if (tok ~ /^beginOptional/) { + opt = 1 + } else if (tok == "endOptional") { + if (showOptional) { + out = out optstr + } + opt = 0 + optstr = "" + } else if (tok ~ /^var/) { + split(tok, ta, ";") + name = "" + original = "" + for (a in ta) { + if (ta[a] ~ "^name") { + match(ta[a], /=".*/) + name = substr(ta[a], RSTART + 2, RLENGTH - 3) + } else if (ta[a] ~ "^original") { + match(ta[a], /=".*/) + original = substr(ta[a], RSTART + 2, RLENGTH - 3) + } else if (ta[a] ~ "^match") { + # currently not used + } + } + if (name == "copyright") { + out = out copyright + } else { + out = out original + } + } + continue + } + if (opt) { + optstr = optstr b[c++] + } else { + out = out b[c++] + } + } + # "Fold" the output + if (foldOutput) { + split(out, oa, "\n") + out = "" + for (l in oa) { + split(oa[l], la, FS) + lc = 0 + for (w in la) { + lc += length(la[w]) + 1 + if (lc >= foldWidth) { + out = out "\n" la[w] " " + lc = length(la[w]) + 1 + } else { + out = out la[w] " " + } + } + out = out "\n" + } + } + print out +} +' +} + +main "$@" -- cgit 1.4.1-21-gabe81