Skip to content
Permalink
78a4f08cab
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
231 lines (189 sloc) 5.81 KB
#!/bin/bash
#
# bee-download - download a repository and convert it into a tarball
#
# Copyright (C) 2009-2016
# Tobias Dreyer <dreyer@molgen.mpg.de>
# Marius Tolzmann <m@rius.berlin>
# and other bee developers
#
# This file is part of bee.
#
# bee is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -e
if [ -z "${BEE_VERSION}" ] ; then
echo >&2 "BEE-ERROR: please call $0 from bee .."
exit 1
fi
VERSION=${BEE_VERSION}
: ${BEE_BINDIR:=@BINDIR@}
function usage() {
cat <<-EOF
bee-download v${VERSION} 2009-2016
by Tobias Dreyer <dreyer@molgen.mpg.de>
Usage:
bee download [options] <repository-url>
Options:
-h, --help display this help
-n, --pkgname specify a package name
-v, --pkgversion specify a package version
-t, --type specify type of download (http, https or git)
-c, --commit use a certain commit as HEAD revision
EOF
}
function guess_type() {
repository_url=${1}
[ "${repository_url##*.}" = "git" ] && echo "git" && return
[ -d "${repository_url}/.git" ] && echo "git" && return
echo ${repository_url%%:*}
}
function download() {
repository_url=${1}
repository_type=${2}
: ${repository_type:=$(guess_type ${repository_url})}
case "${repository_type}" in
http|https)
download_http ${repository_url}
;;
*) # default to git download
download_git ${repository_url}
;;
esac
}
function download_http() {
repository_url=${1}
file=${repository_url##*/}
destination=${BEE_DOWNLOADDIR}/${file}
if [ ! -s "${destination}" ] ; then
rm -vf ${destination}
fi
trap "rm -f ${destination}" EXIT
wget \
--output-document=${destination} \
--no-clobber \
${repository_url}
echo ${destination}
trap - EXIT
}
function download_git() {
repository_url=${1}
reponame=${repository_url##*/}
reponame=${reponame%%.git}
tmp_path=${BEE_TMP_TMPDIR}/${BASHPID}
mkdir -p ${tmp_path}
trap "rm -fr ${tmp_path}" EXIT
git clone -n ${repository_url} ${tmp_path}/${reponame} >/dev/null 2>&1
cd ${tmp_path}/${reponame}
if [ -n "${OPT_COMMIT}" ] ; then
git checkout ${OPT_COMMIT} >/dev/null 2>&1
fi
gd=$(git describe --tags --long --always)
commit=${gd##*-g}
gd=${gd%-*}
ahead=${gd##*-}
gd=${gd%-*}
tag=${gd}
unset PKGNAME
eval $(@BINDIR@/beeversion ${tag} 2>/dev/null)
if [ -z "${PKGNAME}" ] ; then
PKGNAME=${reponame}
# how tag2version conversion works:
# 1) remove traling 'v' in potential version numbers
# ^[vV]([0-9][-_.].*)$ -> $1
# 2) remove trailing 'v' after trailing package name (.*)
# ^.*[-_][vV]([0-9][-_.].*)$ -> $1
# 3) remove trailing package name (globally, recursive)
# (where '-v[0-9]' won't indicate version start)
# ^[A-Za-z]+[0-0]*[-_] -> ''
# 4) handle special case
# ^[vV]([0-9]+)$ -> $1
# 5) cleanup result: convert all '-' to '_'
# 6) cleanup result: _([0-9]) -> .$1
# 7) cleanup result: lowercase [A-Z]+
PKGFULLVERSION=$( sed \
-e 's,^[vV]\([0-9]\+[-_\.]\),\1,' \
-e 's,.*[-_][vV]\([0-9]\+[-_\.]\),\1,' \
-e 's,^\(\([A-Za-z]\+[0-9]*\)\+[-_]\)\+,,' \
-e 's,^[vV]\([0-9]\+\)$,\1,' \
-e 's,-,_,g' \
-e 's,_\([0-9]\),.\1,g' \
-e 's,\([A-Z]\+\),\L\1,g' <<< "${tag}" )
fi
if [ "$ahead" != "$gd" ] ; then
pkgextraextraversion="_p${ahead}"
else
PKGFULLVERSION=0
commitdate=$(git log --format=format:"%ct" -1)
if [ ! -z "${commitdate}" ] ; then
pkgextraextraversion="_t${commitdate}"
fi
fi
pkgextraextraversion="${pkgextraextraversion}_${commit}"
filename=${OPT_PKGNAME:-${PKGNAME}}-${OPT_VERSION:-${PKGFULLVERSION}}${pkgextraextraversion}
git archive --format=tar --prefix=${filename}/ ${commit} | zstd >${filename}.tar.zstd
if [ ! -d "${BEE_DOWNLOADDIR}" ] ; then
mkdir -p ${BEE_DOWNLOADDIR}
fi
mv ${filename}.tar.zstd ${BEE_DOWNLOADDIR}
trap - EXIT
rm -fr ${tmp_path}
echo ${BEE_DOWNLOADDIR}/${filename}.tar.zstd
}
options=$(${BEE_BINDIR}/beegetopt --name bee-download \
--option type/t= \
--option help/h \
--option pkgname/n= \
--option pkgversion/v= \
--option commit/c= \
-- "$@")
if [ $? -ne 0 ] ; then
usage
exit 1
fi
eval set -- "${options}"
while true ; do
case "$1" in
--help)
usage
exit 0
;;
--pkgname)
OPT_PKGNAME="${2}"
shift 2
;;
--pkgversion)
OPT_VERSION="${2}"
shift 2
;;
--commit)
OPT_COMMIT="${2}"
shift 2
;;
--type)
OPT_TYPE="${2}"
shift 2
;;
--)
repository_url=${2}
shift
if [ -z "${repository_url}" ] ; then
usage
exit 1
fi
download ${repository_url} ${OPT_TYPE}
exit 0
;;
esac
done