Files
LibreELEC.tv/tools/mkpkg/update_common_functions
Portisch bec6d17054 update binary addons scripts: implement GITHUB_API_TOKEN alternative
When environment variable GITHUB_API_TOKEN like in ${HOME}/.${DISTRO,,}/options
is assigned the script will fetch all data direct from Github instead
clone every single package.
2021-10-04 11:31:18 +02:00

215 lines
5.2 KiB
Plaintext

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2018-present Team LibreELEC (https://libreelec.tv)
msg_color() {
echo $(
cd "${ROOT}"
PROJECT="${PROJECT:-Generic}" ARCH="${ARCH:-x86_64}" . config/options ""
echo $(print_color "$1" "$2")
)
}
msg_warn() {
msg_color CLR_WARNING "$1"
}
msg_error() {
msg_color CLR_ERROR "$1"
}
msg_info() {
echo "$1"
}
git_clone() {
# git_clone https://repo.url target_dir [branch]
echo "[mkpkg] Checking out $1 ..."
if [ ! -d "$2" ]; then
git clone "$1" "$2"
else
cd "$2"
git fetch
cd ..
fi
if [ -n "$3" ]; then
cd "$2"
git checkout -q origin/"$3";
cd ..
fi
}
get_pkg_var() {
local pkg_name="$1" pkg_var="$2"
cd "${ROOT}"
PROJECT="${PROJECT:-Generic}" ARCH="${ARCH:-x86_64}" source config/options ${pkg_name} &>/dev/null
echo "${!pkg_var}"
}
resolve_hash() {
if [ -d "$1" ] ; then
cd "$1"
git rev-parse $2 2>/dev/null
fi
}
resolve_hash_in_branch() {
if [ -d "$1" ] ; then
cd "$1"
git rev-parse origin/$2 2>/dev/null
fi
}
resolve_hash_on_gh() {
curl -s -L -H "Authorization: token ${GITHUB_API_TOKEN}" \
-H "Accept: application/vnd.github.VERSION.sha" \
"${1/*github.com/https:\/\/api.github.com\/repos}/commits/$2"
}
resolve_tag_in_branch() {
local tag
if [ -d "$1" ] ; then
cd "$1"
if [ $# -eq 3 ] ; then
tag=$(git describe --abbrev=0 --tags --match "$3" origin/$2 2>/dev/null)
if [ -n "$tag" ] ; then
echo "$tag"
return
fi
fi
git describe --abbrev=0 --tags origin/$2 2>/dev/null
fi
}
resolve_tag_on_gh() {
local tag
tag=$(curl -s -L -H "Authorization: token ${GITHUB_API_TOKEN}" \
"${1/*github.com/https:\/\/api.github.com\/repos}/releases" | \
jq -r '[.[] | select(.target_commitish == "'$2'")][0].tag_name | select (.!=null)')
if [ -z "$tag" ] ; then
tag=$(curl -s -L -H "Authorization: token ${GITHUB_API_TOKEN}" \
"${1/*github.com/https:\/\/api.github.com\/repos}/tags" | \
jq -r '[.[] | select(.name | contains("'$2'"))][0].name | select (.!=null)')
fi
echo "$tag"
}
check_package_excluded() {
local package="$1" local packages_to_exclude="$2" pkg
[ -z "${package}" -o -z "${packages_to_exclude}" ] && return 1
if [[ ${packages_to_exclude} =~ (^|[[:space:]])${package}($|[[:space:]]) ]]; then
msg_info "SKIPPING excluded package ${package}"
return 0
fi
return 1
}
set_pkg_version() {
local package_mk="$1/package.mk" pkg_version="$2"
sed -e "s|PKG_VERSION=.*|PKG_VERSION=\"${pkg_version}\"|g" -i "${package_mk}"
}
download_pkg_file() {
local pkg_name="$1"
local pkg_url=$(get_pkg_var "${pkg_name}" PKG_URL)
wget -q -O "${TMP_PKG_FILE}" "${pkg_url}"
}
extract_pkg_file() {
mkdir -p "${TMP_PKG_DIR}"
tar xf "${TMP_PKG_FILE}" --strip-components=1 -C "${TMP_PKG_DIR}"
}
cleanup_pkg_tmp() {
rm -rf "${TMP_PKG_FILE}" "${TMP_PKG_DIR}"
}
set_pkg_sha256() {
local package_mk="$1/package.mk"
local new_sha256=$(sha256sum < "${TMP_PKG_FILE}" | awk '{print $1}')
sed -e "s|PKG_SHA256=.*|PKG_SHA256=\"${new_sha256}\"|g" -i "${package_mk}"
}
bump_pkg_rev() {
local package_mk="$1/package.mk" pkg_name="$2"
local pkg_rev=$(get_pkg_var "${pkg_name}" PKG_REV)
local new_pkg_rev=$((${pkg_rev}+1))
sed -e "s|PKG_REV=.*|PKG_REV=\"${new_pkg_rev}\"|" -i "${package_mk}"
msg_info "BUMPED ${pkg_name} PKG_REV from ${pkg_rev} to ${new_pkg_rev}"
}
reset_pkg_rev() {
local package_mk="$1/package.mk" pkg_name="$2"
local pkg_rev=$(get_pkg_var "${pkg_name}" PKG_REV)
local new_pkg_rev="1"
sed -e "s|PKG_REV=.*|PKG_REV=\"${new_pkg_rev}\"|" -i "${package_mk}"
msg_info "RESET ${pkg_name} PKG_REV from ${pkg_rev} to ${new_pkg_rev}"
}
update_pkg() {
local pkg_path="$1" pkg_name="$2" pkg_version="$3"
local old_version pkg_url new_sha256 pkg_rev
old_version=$(get_pkg_var "${pkg_name}" PKG_VERSION)
if [ "${old_version}" != "${pkg_version}" ]; then
[ -n "${pkg_version}" ] && set_pkg_version "${pkg_path}" "${pkg_version}"
download_pkg_file "${pkg_name}"
set_pkg_sha256 "${pkg_path}"
msg_info "UPDATED ${pkg_name} from ${old_version} to ${pkg_version}"
return 0
else
return 1
fi
}
# Get url in git:// notation for a package.mk, assuming it is a github.com url
# Return 1 if not a github domain
geturl() {
local addon="$1"
local domain owner repo PKG_URL
PKG_URL="$(get_pkg_var ${addon} PKG_URL)"
domain="$(echo "${PKG_URL}" | cut -d/ -f3)"
[ "${domain}" = "github.com" ] || return 1
owner="$(echo "${PKG_URL}" | cut -d/ -f4)"
repo="$(echo "${PKG_URL}" | cut -d/ -f5)"
echo "git://${domain}/${owner}/${repo}.git"
return 0
}
# For the specified addon, verify that the package url
# matches the url retrieved from Kodi
validate_pkg_url() {
local addon="$1" url1="$2"
local domain owner repo url2
domain="$(echo "${url1}" | cut -d/ -f3)"
owner="$(echo "${url1}" | cut -d/ -f4)"
repo="$(echo "${url1}" | cut -d/ -f5)"
url1="git://${domain}/${owner}/${repo}.git"
url2="$(geturl "${addon}")"
[ "${url1}" = "${url2}" ] && return 0 || return 1
}
# Get user Github token if available
get_gh_token() {
if [ -z "${GITHUB_API_TOKEN}" ]; then
GITHUB_API_TOKEN=$(get_pkg_var "" GITHUB_API_TOKEN)
fi
}