forked from Percona-QA/percona-qa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download_launchpad_single_file.sh
executable file
·100 lines (87 loc) · 2.39 KB
/
download_launchpad_single_file.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
# Download the latest version of a single file from a launchpad project.
# Created by David Bennett, Percona LLC - 2015-06-02
# NOTE: if you which to embed this code in another script or Jenkins build
# job, you can just cut and paste the code at the bottom after --EMBEDDABLE--
# Make sure we have a URL
if [ -z "$2" ]; then
echo "Download the latest version of a single file from a launchpad project."
echo "Usage: $0 [lp:project] [path/file.ext] {destination path}"
exit 1
fi
# parameters
LP_PROJECT=${1#lp:}
SRC_FILE=$2
DEST_PATH=$3
# determine package manager for requirements function
pkgmgr=
hash apt-get >/dev/null 2>&1
if [ $? -eq 0 ]; then
pkgmgr='apt-get'
else
hash yum >/dev/null 2>&1
if [ $? -eq 0 ]; then
pkgmgr='yum'
fi
fi
# function to check for script dependency
reqmissing=0
requires()
{
hash "$1" >/dev/null 2>&1
if [ $? -eq 1 ]; then
reqmissing=$((reqmissing+1))
echo "This script requires the $1 command."
if [ -n "${pkgmgr}" ]; then
echo "Use the ${pkgmgr} command to install it:"
printf "\tsudo %s install %s" ${pkgmgr} "$1"
fi
echo ""
fi
}
# Make sure we have 'wget'
requires "wget"
# exit if something is missing
if [ $reqmissing -gt 0 ]; then
exit 1
fi
# --EMBEDDABLE--
# this is the actual download, if you know the target
# system has wget, you can just set LP_PROJECT and SRC_FILE
# (and optionally DEST_PATH} here and cut & paste this block
# into your script
#LP_PROJECT=percona-qa
#SRC_FILE=download_launchpad_single_file.sh
#DEST_PATH=.
# set destination path
_DEST_PATH="${DEST_PATH:-.}"
_DEST_PATH="${_DEST_PATH%/}/"
# get the project path prefix
PROJ_PATH=$(
wget -q -O - "http://code.launchpad.net/${LP_PROJECT}/" \
| grep -E "href.*lp:${LP_PROJECT}" \
| head -n1 \
| cut -d'"' -f2
)
# validate project path
# shellcheck disable=SC2086
if [ "${PROJ_PATH}" == "" -o ${PIPESTATUS[0]} -gt 0 ]; then
echo "ERROR: Project ${1} not found."
exit 1
fi
# get file url
REL_URL=$(
wget -q -O - "http://bazaar.launchpad.net${PROJ_PATH}/view/head:/${SRC_FILE}" \
| grep 'download file' \
| head -n1 \
| cut -d'"' -f2
)
# validate file url
# shellcheck disable=SC2086
if [ "${REL_URL}" == "" -o ${PIPESTATUS[0]} -gt 0 ]; then
echo "ERROR: File ${2} in project ${1} not found."
exit 1
fi
# download
ABS_URL="http://bazaar.launchpad.net${REL_URL}"
wget -q -O - "$ABS_URL" > "${_DEST_PATH}${SRC_FILE}"