lol
fork

Configure Feed

Select the types of activity you want to include in your feed.

at release-16.03-start 171 lines 4.0 kB view raw
1#! /bin/sh -e 2 3usage(){ 4 echo >&2 "syntax: nix-prefetch-zip [OPTIONS] [URL [EXPECTED-HASH]] 5 6Options: 7 --url url The url of the archive to fetch. 8 --name name The name to use for the store path (defaults to \`basename \$url\`). 9 --ext ext The file extension (.zip, .tar.gz, ...) to be REMOVED from name 10 --hash hash The hash of unpacked archive. 11 --hash-type type Use the specified cryptographic hash algorithm, which can be one of md5, sha1, and sha256. 12 --leave-root Keep the root directory of the archive. 13 --help Show this help text. 14" 15 exit 1 16} 17 18 19name="" 20ext="" 21argi=0 22argfun="" 23for arg; do 24 if test -z "$argfun"; then 25 case $arg in 26 --url) argfun=set_url;; 27 --name) argfun=set_name;; 28 --ext) argfun=set_ext;; 29 --hash) argfun=set_expHash;; 30 --hash-type) argfun=set_hashType;; 31 --leave-root) leaveRoot=true;; 32 --help) usage;; 33 *) argi=$(($argi + 1)) 34 case $argi in 35 1) url=$arg;; 36 2) rev=$arg;; 37 3) expHash=$arg;; 38 *) echo "Unexpected argument: $arg" >&2 39 usage 40 ;; 41 esac 42 ;; 43 esac 44 else 45 case $argfun in 46 set_*) 47 var=$(echo $argfun | sed 's,^set_,,') 48 eval "$var=\$arg" 49 ;; 50 esac 51 argfun="" 52 fi 53done 54 55if [ -z "$url" ]; then 56 echo "Error: No --url flag given" >&2 57 usage 58fi 59 60if [ -z "$name" ]; then 61 name=$(basename "$url") 62fi 63 64if test -z "$hashType"; then 65 hashType=sha256 66fi 67 68hashFormat="--base32" 69 70tmp=$(mktemp -d 2>/dev/null || mktemp -d -t "$$") 71trap "rm -rf '$tmp'" EXIT 72 73dirname=$(basename -s "$ext" "$name") 74 75unpackDirTmp=$tmp/unpacked-tmp/$dirname 76mkdir -p $unpackDirTmp 77 78unpackDir=$tmp/unpacked/$dirname 79mkdir -p $unpackDir 80 81downloadedFile=$tmp/$(basename "$url") 82 83unpackFile() { 84 local curSrc="$1" 85 86 case "$curSrc" in 87 *.tar.xz | *.tar.lzma) 88 # Don't rely on tar knowing about .xz. 89 xz -d < $curSrc | tar xf - 90 ;; 91 *.tar | *.tar.* | *.tgz | *.tbz2) 92 # GNU tar can automatically select the decompression method 93 # (info "(tar) gzip"). 94 tar xf $curSrc 95 ;; 96 *.zip) 97 unzip -qq $curSrc 98 ;; 99 *) 100 echo "source archive $curSrc has unknown type" >&2 101 exit 1 102 ;; 103 esac 104} 105 106# If the hash was given, a file with that hash may already be in the 107# store. 108if test -n "$expHash"; then 109 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" "$name") 110 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then 111 finalPath= 112 fi 113 hash=$expHash 114fi 115 116# If we don't know the hash or a path with that hash doesn't exist, 117# download the file and add it to the store. 118if test -z "$finalPath"; then 119 curl="curl \ 120 --location --max-redirs 20 \ 121 --disable-epsv \ 122 --insecure" 123 124 if ! $curl --fail "$url" --output "$downloadedFile"; then 125 echo "error: could not download $url" >&2 126 exit 1 127 fi 128 129 if [ -z "$leaveRoot" ]; then 130 shopt -s dotglob 131 132 cd "$unpackDirTmp" 133 unpackFile "$downloadedFile" 134 135 if [ $(ls "$unpackDirTmp" | wc -l) != 1 ]; then 136 echo "error: zip file must contain a single file or directory." 137 exit 1 138 fi 139 140 fn=$(cd "$unpackDirTmp" && echo *) 141 142 if [ -f "$unpackDirTmp/$fn" ]; then 143 mv "$unpackDirTmp/$fn" "$unpackDir" 144 else 145 mv "$unpackDirTmp/$fn/"* "$unpackDir/" 146 fi 147 else 148 cd $unpackDir 149 unpackFile "$downloadedFile" 150 fi 151 152 # Compute the hash. 153 hash=$(nix-hash --type $hashType $hashFormat $unpackDir) 154 if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi 155 156 # Add the downloaded file to the Nix store. 157 finalPath=$(nix-store --add-fixed --recursive "$hashType" $unpackDir) 158 159 if test -n "$expHash" -a "$expHash" != "$hash"; then 160 echo "hash mismatch for URL \`$url'" 161 exit 1 162 fi 163fi 164 165if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi 166 167echo $hash 168 169if test -n "$PRINT_PATH"; then 170 echo $finalPath 171fi