|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Print |
| 6 | +if [ "$OS" = "Windows_NT" ]; then |
| 7 | + RED='' |
| 8 | + GREEN='' |
| 9 | + YELLOW='' |
| 10 | + NC='' |
| 11 | +else |
| 12 | + RED='\033[1;31m' |
| 13 | + GREEN='\033[0;32m' |
| 14 | + YELLOW='\033[1;33m' |
| 15 | + NC='\033[0m' |
| 16 | +fi |
| 17 | +INFO="${GREEN}[INFO]${NC}" |
| 18 | +NOTE="${YELLOW}[NOTE]${NC}" |
| 19 | +ERROR="${RED}[ERROR]${NC}" |
| 20 | + |
| 21 | +printfln() { |
| 22 | + printf "%b |
| 23 | +" "$@" |
| 24 | +} |
| 25 | + |
| 26 | +# Input |
| 27 | +if [ $# -ge 1 ] && [ -f "$1" ]; then |
| 28 | + input_tar_gz="$1" |
| 29 | + printfln "${INFO} Use ${input_tar_gz} as input packed .tar.gz file" |
| 30 | +else |
| 31 | + printfln "${ERROR} No input .tar.gz file specified" |
| 32 | + printfln "${INFO} Usage: sh $0 [input_tar_gz]" |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +# Check system architecture |
| 37 | +file_arch="${input_tar_gz##*-}" # x64.tar.gz |
| 38 | +file_arch="${file_arch%%.tar.gz}" # x64 |
| 39 | +sys_arch="$(uname -m)" |
| 40 | +case "${sys_arch}" in |
| 41 | + x86_64|amd64) sys_arch="x64" ;; |
| 42 | + aarch64|arm64) sys_arch="aarch64" ;; |
| 43 | +esac |
| 44 | +if [ "${file_arch}" != "${sys_arch}" ]; then |
| 45 | + printfln "${ERROR} Arch mismatch: input_file=${RED}${file_arch}${NC}, system=${RED}${sys_arch}${NC}" |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# Output |
| 50 | +if [ "${TRITON_HOME}" != "" ]; then |
| 51 | + output_dir="${TRITON_HOME}" |
| 52 | + printfln "${INFO} Use ${output_dir} as output directory because TRITON_HOME is set" |
| 53 | +else |
| 54 | + output_dir="${HOME}/.triton" |
| 55 | + printfln "${INFO} Use ${output_dir} as default output directory" |
| 56 | +fi |
| 57 | + |
| 58 | +if [ -d "${output_dir}" ]; then |
| 59 | + last_output_dir=${output_dir}.$(date +%Y%m%d_%H%M%S) |
| 60 | + if [ -d "${last_output_dir}" ]; then |
| 61 | + printfln "${ERROR} Backup directory ${RED}${last_output_dir}${NC} already exists, retrying will resolve it" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + printfln "${NOTE} Output directory ${YELLOW}${output_dir}${NC} already exists, will mv to ${YELLOW}${last_output_dir}${NC}" |
| 65 | +fi |
| 66 | + |
| 67 | +# Check unpack dirs |
| 68 | +printfln "${NOTE} Will unpack following dirs to ${YELLOW}${output_dir}${NC} (will be created):" |
| 69 | +tar tzf "${input_tar_gz}" \ |
| 70 | + | awk -F'/' '{ |
| 71 | + sub(/^\.triton\/?/, ""); if ($0 == "") next |
| 72 | + if ($1 == "nvidia") { |
| 73 | + if (NF >= 2 && $2 != "") { |
| 74 | + print "nvidia/"$2"/" |
| 75 | + } else { |
| 76 | + print "nvidia/" |
| 77 | + } |
| 78 | + } else { |
| 79 | + print $1"/" |
| 80 | + } |
| 81 | + }' \ |
| 82 | + | uniq |
| 83 | +printfln "${NOTE} Press any key to confirm and continue, or Ctrl+C to cancel ..." |
| 84 | +read dummy |
| 85 | + |
| 86 | +# Create output dir |
| 87 | +if [ -d "${output_dir}" ]; then |
| 88 | + set -x |
| 89 | + mv "${output_dir}" "${last_output_dir}" |
| 90 | + { set +x; } 2>/dev/null |
| 91 | +fi |
| 92 | +set -x |
| 93 | +mkdir -p "${output_dir}" |
| 94 | +{ set +x; } 2>/dev/null |
| 95 | +printfln "" |
| 96 | + |
| 97 | +# Unpack |
| 98 | +printfln "${NOTE} Unpacking ${YELLOW}${input_tar_gz}${NC} into ${YELLOW}${output_dir}${NC}" |
| 99 | +set -x |
| 100 | +tar zxf "${input_tar_gz}" -C "${output_dir}" --strip-components=1 |
| 101 | +{ set +x; } 2>/dev/null |
| 102 | + |
| 103 | +printfln "${INFO} Finished successfully." |
0 commit comments