third_party.pigweed.src/pw_env_setup/get_pw_env_setup.sh
Rob Mohr ce87bc01d8 pw_env_setup: allow setting environment directory
Allow setting of environment installation directory. Everything created
from environment setup gets installed into this directory.

The first time bootstrap is run after this commit is applied will take
longer, and eventually the old paths should be removed from .gitignore
(and deleted).

Bug: 216
Change-Id: I92ebe8a2414998cb5e4ae9ef3a92d6b50ffa8fab
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/13600
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
2020-07-16 17:10:34 +00:00

79 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
PW_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
fi
PREFIX="$PW_ENVIRONMENT_ROOT/bootstrap"
mkdir -p "$PREFIX"
# Update the mtimes on the most recent pw_env_setup executables.
for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=5 --format=format:%H); do
if [ -f "$PREFIX/$HASH" ]; then
touch "$PREFIX/$HASH" &> /dev/null
fi
done
# Delete any files with an (apparent) age greater than 5 days. This will never
# include the 5 most recent pw_env_setup executables, but if there's been no
# bootstrap call in less than 5 days this could delete all version of
# pw_env_setup. This is acceptable because it's very unlikely there have been
# no commits in a 5 day period, and if there really have been no commits this
# will just re-download that executable in a few lines.
find "$PREFIX" -mtime +5 -exec echo rm {} \;
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
if [ "$OS" = "darwin" ]; then
OS=mac
fi
ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
if [ "$ARCH" = "x86_64" ]; then
ARCH="amd64"
fi
for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=10 --format=format:%H); do
URL="https://storage.googleapis.com/pigweed-envsetup/$OS-$ARCH"
URL="$URL/$HASH/pw_env_setup"
FILEPATH="$PREFIX/$HASH"
# First try curl.
if [ ! -f "$FILEPATH" ]; then
curl -o "$FILEPATH" "$URL" &> /dev/null
fi
# If curl fails try wget.
if [ ! -f "$FILEPATH" ]; then
wget -O "$FILEPATH" "$URL" &> /dev/null
fi
# If either curl or wget succeeded mark the file executable, print it, and
# exit. If the file appears to be a text file then it doesn't exist in GCS
# and we'll try the next one.
TEXT=$(file --mime "$FILEPATH" | grep text)
if [ -n "$TEXT" ]; then
rm "$FILEPATH"
continue
fi
if [ -f "$FILEPATH" ]; then
chmod a+x "$FILEPATH"
echo "$FILEPATH"
exit 0
fi
done
exit -1