Add intelligent vendor pruning (via "go list")

Signed-off-by: Andrew "Tianon" Page <admwiggin@gmail.com>
This commit is contained in:
Tianon Gravi 2015-06-05 14:47:27 -07:00
parent f61d595b55
commit 9e4ee3dea5
2 changed files with 120 additions and 46 deletions

117
hack/.vendor-helpers.sh Executable file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env bash
# Downloads dependencies into vendor/ directory
mkdir -p vendor
rm -rf .gopath
mkdir -p .gopath/src/github.com/docker
ln -sf ../../../.. .gopath/src/github.com/docker/docker
export GOPATH="${PWD}/.gopath:${PWD}/vendor"
clone() {
local vcs="$1"
local pkg="$2"
local rev="$3"
local url="https://$pkg"
local target="vendor/src/$pkg"
echo -n "$pkg @ $rev: "
if [ -d "$target" ]; then
echo -n 'rm old, '
rm -rf "$target"
fi
echo -n 'clone, '
case "$vcs" in
git)
git clone --quiet --no-checkout "$url" "$target"
( cd "$target" && git reset --quiet --hard "$rev" )
;;
hg)
hg clone --quiet --updaterev "$rev" "$url" "$target"
;;
esac
echo -n 'rm VCS, '
( cd "$target" && rm -rf .{git,hg} )
echo -n 'rm vendor, '
( cd "$target" && rm -rf vendor Godeps/_workspace )
echo done
}
# get an ENV from the Dockerfile with support for multiline values
_dockerfile_env() {
local e="$1"
awk '
$1 == "ENV" && $2 == "'"$e"'" {
sub(/^ENV +([^ ]+) +/, "");
inEnv = 1;
}
inEnv {
if (sub(/\\$/, "")) {
printf "%s", $0;
next;
}
print;
exit;
}
' Dockerfile
}
clean() {
local packages=(
github.com/docker/docker/docker # package main
github.com/docker/docker/dockerinit # package main
github.com/docker/docker/integration-cli # external tests
)
local dockerPlatforms=( linux/amd64 $(_dockerfile_env DOCKER_CROSSPLATFORMS) )
local dockerBuildTags="$(_dockerfile_env DOCKER_BUILDTAGS)"
local buildTagCombos=(
''
'experimental'
"$dockerBuildTags"
"daemon $dockerBuildTags"
"experimental $dockerBuildTags"
"experimental daemon $dockerBuildTags"
)
echo
echo -n 'collecting import graph, '
local IFS=$'\n'
local imports=( $(
for platform in "${dockerPlatforms[@]}"; do
export GOOS="${platform%/*}";
export GOARCH="${platform##*/}";
for buildTags in "${buildTagCombos[@]}"; do
go list -e -tags "$buildTags" -f '{{join .Deps "\n"}}' "${packages[@]}"
done
done | grep -vE '^github.com/docker/docker' | sort -u
) )
imports=( $(go list -e -f '{{if not .Standard}}{{.ImportPath}}{{end}}' "${imports[@]}") )
unset IFS
echo -n 'pruning unused packages, '
findArgs=()
for import in "${imports[@]}"; do
[ "${#findArgs[@]}" -eq 0 ] || findArgs+=( -or )
findArgs+=( -path "vendor/src/$import" )
done
local IFS=$'\n'
local prune=( $(find vendor -depth -type d -not '(' "${findArgs[@]}" ')') )
unset IFS
for dir in "${prune[@]}"; do
find "$dir" -maxdepth 1 -not -type d -exec rm -f '{}' +
rmdir "$dir" 2>/dev/null || true
done
echo -n 'pruning unused files, '
find vendor -type f -name '*_test.go' -exec rm '{}' +
echo done
}

View File

@ -2,45 +2,7 @@
set -e
cd "$(dirname "$BASH_SOURCE")/.."
# Downloads dependencies into vendor/ directory
mkdir -p vendor
cd vendor
clone() {
vcs=$1
pkg=$2
rev=$3
pkg_url=https://$pkg
target_dir=src/$pkg
echo -n "$pkg @ $rev: "
if [ -d $target_dir ]; then
echo -n 'rm old, '
rm -fr $target_dir
fi
echo -n 'clone, '
case $vcs in
git)
git clone --quiet --no-checkout $pkg_url $target_dir
( cd $target_dir && git reset --quiet --hard $rev )
;;
hg)
hg clone --quiet --updaterev $rev $pkg_url $target_dir
;;
esac
echo -n 'rm VCS, '
( cd $target_dir && rm -rf .{git,hg} )
echo -n 'rm vendor, '
( cd $target_dir && rm -rf vendor Godeps/_workspace )
echo done
}
source 'hack/.vendor-helpers.sh'
# the following lines are in sorted order, FYI
clone git github.com/Sirupsen/logrus v0.8.2 # logrus is a common dependency among multiple deps
@ -61,13 +23,6 @@ clone git github.com/vishvananda/netlink 8eb64238879fed52fd51c5b30ad20b928fb4c36
# get distribution packages
clone git github.com/docker/distribution b9eeb328080d367dbde850ec6e94f1e4ac2b5efe
mv src/github.com/docker/distribution/digest tmp-digest
mv src/github.com/docker/distribution/registry/api tmp-api
rm -rf src/github.com/docker/distribution
mkdir -p src/github.com/docker/distribution
mv tmp-digest src/github.com/docker/distribution/digest
mkdir -p src/github.com/docker/distribution/registry
mv tmp-api src/github.com/docker/distribution/registry/api
clone git github.com/docker/libcontainer v2.1.1
# libcontainer deps (see src/github.com/docker/libcontainer/update-vendor.sh)
@ -76,3 +31,5 @@ clone git github.com/godbus/dbus v2
clone git github.com/syndtr/gocapability 66ef2aa7a23ba682594e2b6f74cf40c0692b49fb
clone git github.com/golang/protobuf 655cdfa588ea
clone git github.com/Graylog2/go-gelf 6c62a85f1d47a67f2a5144c0e745b325889a8120
clean