From 2188fe944f28a13369460a7b9285e407824acd9d Mon Sep 17 00:00:00 2001 From: Robert Iannucci Date: Fri, 2 Dec 2016 11:15:57 -0800 Subject: [PATCH] Add cipd bootstrap scripts to depot_tools. This takes advantage of powershell on windows for a cleanish duplicate of the posix version. R=dnj@chromium.org, vadimsh@chromium.org BUG=663843 Change-Id: Ib23a044ff912e3239b58848a26143eb6575826d5 Reviewed-on: https://chromium-review.googlesource.com/414228 Commit-Queue: Robbie Iannucci Reviewed-by: Vadim Shtayura --- .gitignore | 4 +++ cipd | 73 +++++++++++++++++++++++++++++++++++++++++++++ cipd.bat | 6 ++++ cipd.ps1 | 57 +++++++++++++++++++++++++++++++++++ cipd_client_version | 1 + 5 files changed, 141 insertions(+) create mode 100755 cipd create mode 100644 cipd.bat create mode 100644 cipd.ps1 create mode 100644 cipd_client_version diff --git a/.gitignore b/.gitignore index b8290067d..f902bbe11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ # Ignore any python bytecode. *.pyc +# ignore cipd client files +/.cipd_client* +/.versions + # Ignore the batch files produced by the Windows bootstrapping. /git-bash /git.bat diff --git a/cipd b/cipd new file mode 100755 index 000000000..5030437b7 --- /dev/null +++ b/cipd @@ -0,0 +1,73 @@ +#!/bin/bash -e + +# Copyright (c) 2016 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +MYPATH=$(dirname "${BASH_SOURCE[0]}") + +: ${CIPD_CLIENT_VER:=`cat $MYPATH/cipd_client_version`} +: ${CIPD_CLIENT_SRV:='https://chrome-infra-packages.appspot.com'} + +UNAME=`uname -s | tr '[:upper:]' '[:lower:]'` +case $UNAME in + linux) + PLAT=linux + ;; + cygwin*|msys*|mingw*) + PLAT=windows + ;; + darwin) + PLAT=mac + ;; + *) + echo "UNKNOWN OS: $UNAME" + exit 1 +esac + +UNAME=`uname -m | tr '[:upper:]' '[:lower:]'` +case $UNAME in + x86_64|amd64) + ARCH=amd64 + ;; + arm*) + ARCH=$UNAME + ;; + *86) + ARCH=386 + ;; + *) + echo "UNKNOWN Machine architecture: $UNAME" + exit 1 +esac + +URL="$CIPD_CLIENT_SRV/client?platform=${PLAT}-${ARCH}&version=$CIPD_CLIENT_VER" +CLIENT="$MYPATH/.cipd_client" + +USER_AGENT="depot_tools/$(git -C $MYPATH rev-parse HEAD 2>/dev/null || echo "???")" + +if [ ! -e "$CLIENT" ]; then + echo "Bootstrapping cipd client for ${PLAT}-${ARCH}..." + echo "From $URL" + if hash curl 2> /dev/null ; then + curl "$URL" -A "$USER_AGENT" -L -o "$CLIENT" + chmod +x "$CLIENT" + else + echo Your platform is missing the \`curl\` command. Please use your package + echo manager to install it before continuing. + echo + echo Alternately, manually download: + echo "$URL" + echo To $CLIENT, and then re-run this command. + exit 1 + fi +fi + +export CIPD_HTTP_USER_AGENT_PREFIX=$USER_AGENT +if ! "$CLIENT" selfupdate -version "$CIPD_CLIENT_VER" ; then + echo -n "selfupdate failed: " 1>&2 + echo "run \`CIPD_HTTP_USER_AGENT_PREFIX=$USER_AGENT/manual $CLIENT selfupdate -version '$CIPD_CLIENT_VER'\` to diagnose" 1>&2 + echo "" 1>&2 +fi + +exec "$CLIENT" "${@}" diff --git a/cipd.bat b/cipd.bat new file mode 100644 index 000000000..6a65238b1 --- /dev/null +++ b/cipd.bat @@ -0,0 +1,6 @@ +@echo off +:: Copyright (c) 2016 The Chromium Authors. All rights reserved. +:: Use of this source code is governed by a BSD-style license that can be +:: found in the LICENSE file. + +powershell -NoProfile -ExecutionPolicy RemoteSigned -File "%~dp0\cipd.ps1" %* diff --git a/cipd.ps1 b/cipd.ps1 new file mode 100644 index 000000000..c4dc09b1b --- /dev/null +++ b/cipd.ps1 @@ -0,0 +1,57 @@ +# Copyright (c) 2016 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +$myPath = Split-Path $MyInvocation.MyCommand.Path -Parent + +function GetEnvVar([string] $key, [scriptblock] $defaultFn) { + if (Test-Path "Env:\$key") { + return Get-ChildItem $Env $key + } + return $defaultFn.Invoke() +} + +$cipdClientVer = GetEnvVar "CIPD_CLIENT_VER" { + Get-Content (Join-Path $myPath -ChildPath 'cipd_client_version') -First 1 +} +$cipdClientSrv = GetEnvVar "CIPD_CLIENT_SRV" { + "https://chrome-infra-packages.appspot.com" +} + +$plat="windows" +if ([environment]::Is64BitOperatingSystem) { + $arch="amd64" +} else { + $arch="386" +} + +$url = "$cipdClientSrv/client?platform=$plat-$arch&version=$cipdClientVer" +$client = Join-Path $myPath -ChildPath ".cipd_client.exe" + +$depot_tools_version = &git -C $myPath rev-parse HEAD 2>&1 +if ($LastExitCode -eq 0) { + $user_agent = "depot_tools/$depot_tools_version" +} else { + $user_agent = "depot_tools/???" +} + +$Env:CIPD_HTTP_USER_AGENT_PREFIX = $user_agent +if (!(Test-Path $client)) { + echo "Bootstrapping cipd client for $plat-$arch..." + echo "From $url" + # TODO(iannucci): It would be really nice if there was a way to get this to + # show progress without also completely destroying the download speed, but + # I can't seem to find a way to do it. Patches welcome :) + $wc = (New-Object System.Net.WebClient) + $wc.Headers.add('User-Agent', $user_agent) + $wc.DownloadFile($url, $client) +} + +$_ = & $client selfupdate -version "$cipdClientVer" +if ($LastExitCode -ne 0) { + Write-Host "selfupdate failed: " -ForegroundColor Red -NoNewline + Write-Host "run ``set CIPD_HTTP_USER_AGENT_PREFIX=$user_agent/manual && $client selfupdate -version $cipdClientVer`` to diagnose`n" -ForegroundColor White +} + +& $client @args +exit $LastExitCode diff --git a/cipd_client_version b/cipd_client_version new file mode 100644 index 000000000..5d97e086b --- /dev/null +++ b/cipd_client_version @@ -0,0 +1 @@ +git_revision:05844bd9d1200cba8449b936b76e25eb90eabe25