|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 AND MIT |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# See https://github.com/nexB/scancode-toolkit for support or download. |
| 8 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 9 | +# |
| 10 | + |
| 11 | +# A minimal shell wrapper to the CLI entry point fo ScanCode |
| 12 | + |
| 13 | +set -e |
| 14 | +#set -x |
| 15 | + |
| 16 | +COMMAND_TO_RUN=scancode-reindex-licenses |
| 17 | + |
| 18 | +################################################################################### |
| 19 | +################################################################################### |
| 20 | +# from https://raw.githubusercontent.com/mkropat/sh-realpath/58c03982cfd8accbcf0c4426a4adf0f120a8b2bb/realpath.sh |
| 21 | +# realpath emulation for portability on *nix |
| 22 | +# this allow running scancode from arbitrary locations and from symlinks |
| 23 | +# |
| 24 | +# Copyright (c) 2014 Michael Kropat |
| 25 | +# |
| 26 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 27 | +# of this software and associated documentation files (the "Software"), to deal |
| 28 | +# in the Software without restriction, including without limitation the rights |
| 29 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 30 | +# copies of the Software, and to permit persons to whom the Software is |
| 31 | +# furnished to do so, subject to the following conditions: |
| 32 | +# |
| 33 | +# The above copyright notice and this permission notice shall be included in |
| 34 | +# all copies or substantial portions of the Software. |
| 35 | +# |
| 36 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 37 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 38 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 39 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 40 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 41 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 42 | +# THE SOFTWARE. |
| 43 | + |
| 44 | + |
| 45 | +realpath() { |
| 46 | + canonicalize_path "$(resolve_symlinks "$1")" |
| 47 | +} |
| 48 | + |
| 49 | +resolve_symlinks() { |
| 50 | + _resolve_symlinks "$1" |
| 51 | +} |
| 52 | + |
| 53 | +_resolve_symlinks() { |
| 54 | + _assert_no_path_cycles "$@" || return |
| 55 | + |
| 56 | + local dir_context path |
| 57 | + path=$(readlink -- "$1") |
| 58 | + if [ $? -eq 0 ]; then |
| 59 | + dir_context=$(dirname -- "$1") |
| 60 | + _resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@" |
| 61 | + else |
| 62 | + printf '%s\n' "$1" |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +_prepend_dir_context_if_necessary() { |
| 67 | + if [ "$1" = . ]; then |
| 68 | + printf '%s\n' "$2" |
| 69 | + else |
| 70 | + _prepend_path_if_relative "$1" "$2" |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +_prepend_path_if_relative() { |
| 75 | + case "$2" in |
| 76 | + /* ) printf '%s\n' "$2" ;; |
| 77 | + * ) printf '%s\n' "$1/$2" ;; |
| 78 | + esac |
| 79 | +} |
| 80 | + |
| 81 | +_assert_no_path_cycles() { |
| 82 | + local target path |
| 83 | + |
| 84 | + target=$1 |
| 85 | + shift |
| 86 | + |
| 87 | + for path in "$@"; do |
| 88 | + if [ "$path" = "$target" ]; then |
| 89 | + return 1 |
| 90 | + fi |
| 91 | + done |
| 92 | +} |
| 93 | + |
| 94 | +canonicalize_path() { |
| 95 | + if [ -d "$1" ]; then |
| 96 | + _canonicalize_dir_path "$1" |
| 97 | + else |
| 98 | + _canonicalize_file_path "$1" |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | +_canonicalize_dir_path() { |
| 103 | + (cd "$1" 2>/dev/null && pwd -P) |
| 104 | +} |
| 105 | + |
| 106 | +_canonicalize_file_path() { |
| 107 | + local dir file |
| 108 | + dir=$(dirname -- "$1") |
| 109 | + file=$(basename -- "$1") |
| 110 | + (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file") |
| 111 | +} |
| 112 | + |
| 113 | +################################################################################### |
| 114 | +################################################################################### |
| 115 | + |
| 116 | +# Now run scancode proper |
| 117 | + |
| 118 | +################################ |
| 119 | +# Setup current directory where this script lives |
| 120 | +SCANCODE_BIN="$( realpath "${BASH_SOURCE[0]}" )" |
| 121 | +SCANCODE_ROOT_DIR="$( cd "$( dirname "${SCANCODE_BIN}" )" && pwd )" |
| 122 | + |
| 123 | +# force relaunching under X86-64 architecture on macOS M1/ARM |
| 124 | +if [[ $OSTYPE == 'darwin'* && $(uname -m) == 'arm64' && $(sysctl -in sysctl.proc_translated) == 0 ]]; then |
| 125 | + arch -x86_64 /bin/bash -c "$SCANCODE_ROOT_DIR/$COMMAND_TO_RUN $*" |
| 126 | + exit $? |
| 127 | +fi |
| 128 | + |
| 129 | + |
| 130 | +SCANCODE_CONFIGURED_PYTHON="$SCANCODE_ROOT_DIR/venv/bin/python" |
| 131 | +if [ ! -f "$SCANCODE_CONFIGURED_PYTHON" ]; then |
| 132 | + set -e |
| 133 | + echo "* Configuring ScanCode for first use..." |
| 134 | + CFG_QUIET=-qq "$SCANCODE_ROOT_DIR/configure" |
| 135 | + set +e |
| 136 | +fi |
| 137 | + |
| 138 | +if [ -f "$SCANCODE_CONFIGURED_PYTHON" ]; then |
| 139 | + "$SCANCODE_ROOT_DIR/venv/bin/$COMMAND_TO_RUN" "$@" |
| 140 | +fi |
0 commit comments