github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/scripts/check_license.sh (about)

     1  #!/bin/bash
     2  #
     3  # Checks that source files (.go and .proto) have the Apache License header.
     4  # Automatically skips generated files.
     5  set -eu
     6  
     7  check_license() {
     8    local path="$1"
     9  
    10    if [[ "$path" =~ "usbarmory/bootloader" || "$path" =~ "omniwitness/usbarmory" ]]; then
    11      # This code forked from the USB Armory repo, and has
    12      # a LICENCE file in the directory.
    13      return 0
    14    fi
    15  
    16    if head -1 "$path" | grep -iq 'generated by'; then
    17      return 0
    18    fi
    19  
    20    # Look for "Apache License" on the file header
    21    if ! head -10 "$path" | grep -q 'Apache License'; then
    22      # Format: $path:$line:$message
    23      echo "$path:10:license header not found"
    24      return 1
    25    fi
    26  }
    27  
    28  main() {
    29    if [[ $# -lt 1 ]]; then
    30      echo "Usage: $0 <path>"
    31      exit 1
    32    fi
    33  
    34    # Check USB Armory license is intact
    35    if ! grep -q "F-Secure" binary_transparency/firmware/devices/usbarmory/bootloader/LICENSE; then
    36      echo "LICENSE for forked USB armory missing"
    37      exit 1
    38    fi
    39  
    40    local code=0
    41    while [[ $# -gt 0 ]]; do
    42      local path="$1"
    43      if [[ -d "$path" ]]; then
    44        for f in "$path"/*.{go,proto}; do
    45          if [[ ! -f "$f" ]]; then
    46            continue  # Empty glob
    47          fi
    48          check_license "$f" || code=1
    49        done
    50      else
    51        check_license "$path" || code=1
    52      fi
    53      shift
    54    done
    55    exit $code
    56  }
    57  
    58  main "$@"