pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / build-support / fetchbzr / nix-prefetch-bzr
blob184f2485ce57b886423314ba658fe05bc7a7ff48
1 #! /bin/sh -e
3 url=$1
4 rev=$2
5 expHash=$3
7 hashType=$NIX_HASH_ALGO
8 if test -z "$hashType"; then
9 hashType=sha256
11 if test -z "$hashFormat"; then
12 hashFormat=--sri
15 if test -z "$url"; then
16 echo "syntax: nix-prefetch-bzr URL [REVISION [EXPECTED-HASH]]" >&2
17 exit 1
20 revarg="-r $rev"
21 test -n "$rev" || revarg=""
23 repoName=$(echo $url | sed '
24 s,.*/\([^/]\+\)/trunk/*$,\1,;t
25 s,.*/\([^/]\+\)/branches/\([^/]\+\)/*$,\1-\2,;t
26 s,.*/\([^/]\+\)/tags/\([^/]\+\)/*$,\1-\2,;t
27 s,.*/\([^/]\+\)/*$,\1,;t
29 dstFile="bzr-export"
31 # If the hash was given, a file with that hash may already be in the
32 # store.
33 if test -n "$expHash"; then
34 finalPath=$(nix-store --print-fixed-path --recursive "$hashType" "$expHash" $dstFile)
35 if ! nix-store --check-validity "$finalPath" 2> /dev/null; then
36 finalPath=
38 hash=$expHash
42 # If we don't know the hash or a path with that hash doesn't exist,
43 # download the file and add it to the store.
44 if test -z "$finalPath"; then
45 tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/bzr-checkout-tmp-XXXXXXXX")"
46 trap "rm -rf \"$tmpPath\"" EXIT
48 tmpFile="$tmpPath/$dstFile"
50 # Perform the checkout.
51 bzr -Ossl.cert_reqs=none export $revarg --format=dir "$tmpFile" "$url"
53 echo "bzr revision is $(bzr revno $revarg "$url")"
55 # Compute the hash.
56 hash=$(nix-hash --type $hashType $hashFormat $tmpFile)
57 if ! test -n "$QUIET"; then echo "hash is $hash" >&2; fi
59 # Add the downloaded file to the Nix store.
60 finalPath=$(nix-store --add-fixed --recursive "$hashType" $tmpFile)
62 if test -n "$expHash" -a "$expHash" != "$hash"; then
63 echo "hash mismatch for URL \`$url'"
64 exit 1
68 if ! test -n "$QUIET"; then echo "path is $finalPath" >&2; fi
70 echo $hash
72 if test -n "$PRINT_PATH"; then
73 echo $finalPath