2 # Copyright 2020 The Pigweed Authors
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 # use this file except in compliance with the License. You may obtain a copy of
8 # https://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
16 if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
17 PW_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
19 PREFIX="$PW_ENVIRONMENT_ROOT/bootstrap"
24 # Update the mtimes on the most recent pw_env_setup executables.
25 for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=5 --format=format:%H); do
26 if [ -f "$PREFIX/$HASH" ]; then
27 touch "$PREFIX/$HASH" &> /dev/null
31 # Delete any files with an (apparent) age greater than 5 days. This will never
32 # include the 5 most recent pw_env_setup executables, but if there's been no
33 # bootstrap call in less than 5 days this could delete all versions of
34 # pw_env_setup. This is acceptable because it's very unlikely there have been
35 # no commits in a 5 day period, and if there really have been no commits this
36 # will just re-download that executable in a few lines.
37 find "$PREFIX" -mtime +5 -exec rm {} \;
39 OS=$(uname -s | tr '[:upper:]' '[:lower:]')
40 if [ "$OS" = "darwin" ]; then
44 ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
45 if [ "$ARCH" = "x86_64" ]; then
49 for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=10 --format=format:%H); do
50 URL="https://storage.googleapis.com/pigweed-envsetup/$OS-$ARCH"
51 URL="$URL/$HASH/pw_env_setup"
52 FILEPATH="$PREFIX/$HASH"
55 if [ ! -f "$FILEPATH" ]; then
56 curl -o "$FILEPATH" "$URL" &> /dev/null
59 # If curl fails try wget.
60 if [ ! -f "$FILEPATH" ]; then
61 wget -O "$FILEPATH" "$URL" &> /dev/null
64 # If either curl or wget succeeded mark the file executable, print it, and
65 # exit. If the file appears to be a text file then it doesn't exist in GCS
66 # and we'll try the next one.
67 TEXT=$(file --mime "$FILEPATH" | grep text)
68 if [ -n "$TEXT" ]; then
73 if [ -f "$FILEPATH" ]; then