pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / build-support / fetchurl / builder.sh
bloba82728ef1025c11f4282cdf4a74b32e2ae040974
1 if [ -e "$NIX_ATTRS_SH_FILE" ]; then . "$NIX_ATTRS_SH_FILE"; elif [ -f .attrs.sh ]; then . .attrs.sh; fi
2 source $stdenv/setup
4 source $mirrorsFile
6 curlVersion=$(curl -V | head -1 | cut -d' ' -f2)
8 # Curl flags to handle redirects, not use EPSV, handle cookies for
9 # servers to need them during redirects, and work on SSL without a
10 # certificate (this isn't a security problem because we check the
11 # cryptographic hash of the output anyway).
12 curl=(
13 curl
14 --location
15 --max-redirs 20
16 --retry 3
17 --disable-epsv
18 --cookie-jar cookies
19 --user-agent "curl/$curlVersion Nixpkgs/$nixpkgsVersion"
22 if ! [ -f "$SSL_CERT_FILE" ]; then
23 curl+=(--insecure)
26 eval "curl+=($curlOptsList)"
28 curl+=(
29 $curlOpts
30 $NIX_CURL_FLAGS
33 downloadedFile="$out"
34 if [ -n "$downloadToTemp" ]; then downloadedFile="$TMPDIR/file"; fi
37 tryDownload() {
38 local url="$1"
39 echo
40 echo "trying $url"
41 local curlexit=18;
43 success=
45 # if we get error code 18, resume partial download
46 while [ $curlexit -eq 18 ]; do
47 # keep this inside an if statement, since on failure it doesn't abort the script
48 if "${curl[@]}" -C - --fail "$url" --output "$downloadedFile"; then
49 success=1
50 break
51 else
52 curlexit=$?;
54 done
58 finish() {
59 local skipPostFetch="$1"
61 set +o noglob
63 if [[ $executable == "1" ]]; then
64 chmod +x $downloadedFile
67 if [ -z "$skipPostFetch" ]; then
68 runHook postFetch
71 exit 0
75 tryHashedMirrors() {
76 if test -n "$NIX_HASHED_MIRRORS"; then
77 hashedMirrors="$NIX_HASHED_MIRRORS"
80 for mirror in $hashedMirrors; do
81 url="$mirror/$outputHashAlgo/$outputHash"
82 if "${curl[@]}" --retry 0 --connect-timeout "${NIX_CONNECT_TIMEOUT:-15}" \
83 --fail --silent --show-error --head "$url" \
84 --write-out "%{http_code}" --output /dev/null > code 2> log; then
85 tryDownload "$url"
87 # We skip postFetch here, because hashed-mirrors are
88 # already content addressed. So if $outputHash is in the
89 # hashed-mirror, changes from ‘postFetch’ would already be
90 # made. So, running postFetch will end up applying the
91 # change /again/, which we don’t want.
92 if test -n "$success"; then finish skipPostFetch; fi
93 else
94 # Be quiet about 404 errors, which we interpret as the file
95 # not being present on this particular mirror.
96 if test "$(cat code)" != 404; then
97 echo "error checking the existence of $url:"
98 cat log
101 done
105 # URL list may contain ?. No glob expansion for that, please
106 set -o noglob
108 urls2=
109 for url in $urls; do
110 if test "${url:0:9}" != "mirror://"; then
111 urls2="$urls2 $url"
112 else
113 url2="${url:9}"; echo "${url2/\// }" > split; read site fileName < split
114 #varName="mirror_$site"
115 varName="$site" # !!! danger of name clash, fix this
116 if test -z "${!varName}"; then
117 echo "warning: unknown mirror:// site \`$site'"
118 else
119 mirrors=${!varName}
121 # Allow command-line override by setting NIX_MIRRORS_$site.
122 varName="NIX_MIRRORS_$site"
123 if test -n "${!varName}"; then mirrors="${!varName}"; fi
125 for url3 in $mirrors; do
126 urls2="$urls2 $url3$fileName";
127 done
130 done
131 urls="$urls2"
133 # Restore globbing settings
134 set +o noglob
136 if test -n "$showURLs"; then
137 echo "$urls" > $out
138 exit 0
141 if test -n "$preferHashedMirrors"; then
142 tryHashedMirrors
145 # URL list may contain ?. No glob expansion for that, please
146 set -o noglob
148 success=
149 for url in $urls; do
150 if [ -z "$postFetch" ]; then
151 case "$url" in
152 https://github.com/*/archive/*)
153 echo "warning: archives from GitHub revisions should use fetchFromGitHub"
155 https://gitlab.com/*/-/archive/*)
156 echo "warning: archives from GitLab revisions should use fetchFromGitLab"
158 esac
160 tryDownload "$url"
161 if test -n "$success"; then finish; fi
162 done
164 # Restore globbing settings
165 set +o noglob
167 if test -z "$preferHashedMirrors"; then
168 tryHashedMirrors
172 echo "error: cannot download $name from any mirror"
173 exit 1