github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/scripts/utils.sh (about)

     1  #!/bin/bash
     2  
     3  function list_all_go_dirs {
     4    go list -f '{{.Dir}}' "${AISTORE_PATH}/..."
     5  }
     6  
     7  function check_gomod {
     8    # For now there is no `-check` flag that would tell us if there is something
     9    # that needs to be updated with `go.mod` or `go.sum`. Therefore, we can just
    10    # run tidy and check if anything has changed in the files.
    11    # See: https://github.com/golang/go/issues/27005.
    12    if ! command -v git &>/dev/null; then
    13      return
    14    fi
    15  
    16    go mod tidy &>/dev/null
    17    if ! git diff --exit-code -- go.mod go.sum &>/dev/null; then
    18      printf "\nproject requires to run 'go mod tidy'\n"
    19      exit 1
    20    fi
    21  }
    22  
    23  function check_files_headers {
    24    for f in $(find ${AISTORE_PATH} -type f -name "*.go" -not -name "*gen.go"  ! -regex $EXTERNAL_SRC_REGEX); do
    25      # Allow files to start with '//go:build ...', '// Package ...', or '//nolint:...'\
    26      out=$(head -n 1 $f | grep -P "\/\/(go:build(.*)|\sPackage(.*)|nolint:(.*))")
    27      if [[ $? -ne 0 ]]; then
    28        echo "$f: first line should be package a description. Instead got:"
    29        head -n 1 $f
    30        exit 1
    31      fi
    32  
    33      # Expect '// no-copyright' or standard copyright preamble.
    34      out=$(head -n 10 $f | grep -P "((.*)\/\/\sno-copyright(.*)|(.*)Copyright(.*)NVIDIA(.*)All rights reserved(.*))")
    35      if [[ $? -ne 0 ]]; then
    36        echo "$f: copyright statement not found in first 10 lines. Use no-copyright to skip"
    37        exit 1
    38      fi
    39    done
    40  }
    41  
    42  function check_imports {
    43    # Check if `import` block contains more than one empty line.
    44    for f in $(find ${AISTORE_PATH} -type f -name "*.go" ! -regex $EXTERNAL_SRC_REGEX); do
    45      # https://regexr.com/55u6r
    46      out=$(head -n 50 $f | grep -Pz 'import \((.|\n)*(\n\n)+(\t(\w|\.)?\s?(.*)"(.*)"\n)*\n+(\t(\w|\.)?\s?"(.*)"\n)*\)')
    47      if [[ $? -eq 0 ]]; then
    48        echo "$f: 'import' block contains (at least) 2 empty lines (expected: 1)"
    49        exit 1
    50      fi
    51    done
    52  }
    53  
    54  function check_deps {
    55    # Check if `aisloader` package imports `tutils`.
    56    for f in $(find ${AISTORE_PATH}/bench/tools/aisloader -type f -name "*.go" ! -regex $EXTERNAL_SRC_REGEX); do
    57      out=$(cat $f | grep '"github.com/NVIDIA/aistore/tutils"')
    58      if [[ $? -eq 0 ]]; then
    59        echo "$f: imports 'tutils' package which is forbidden"
    60        exit 1
    61      fi
    62    done
    63  }
    64  
    65  function check_python_formatting {
    66    i=0
    67    # Check any python code not in aistore/python
    68    for f in $(find . -type f -name "*.py" ! -regex ".*__init__.py" ! -regex $EXTERNAL_SRC_REGEX ! -path '*/docs/examples/*' ! -path '*/python/*'); do
    69      pylint --rcfile=$PYLINT_STYLE $f --msg-template="{path} ({C}):{line:3d},{column:2d}: {msg} ({msg_id}:{symbol})" 2>/dev/null
    70      if [[ $? -gt 0 ]]; then i=$((i+1)); fi
    71    done
    72  
    73    # Check the python code in aistore/python using the make targets there
    74    (cd 'python'; make lint && make lint-tests)
    75    if [[ $? -gt 0 ]]; then i=$((i+1)); fi
    76  
    77    if [[ $i -ne 0 ]]; then
    78      printf "\npylint failed, fix before continuing\n"
    79      exit 1
    80    fi
    81  
    82    i=0
    83    black . --check --diff --quiet --extend-exclude examples
    84    if [[ $? -ne 0 ]]; then
    85      printf "\nIncorrect python formatting. Run make fmt-fix to fix it.\n\n" >&2
    86      exit 1
    87    fi
    88  }
    89  
    90  function python_black_fix {
    91    black . --quiet --force-exclude examples
    92  }
    93  
    94  function perror {
    95    err_count=$(echo "$2" | sort -n | uniq | wc -l)
    96    if [[ -n $2 ]]; then
    97      echo "${2}" >&2
    98      echo "$1: ${err_count} failed" >&2
    99      exit 1
   100    fi
   101    exit 0
   102  }