Compare commits

..
10 Commits
Author SHA1 Message Date
wyj 96e0ccfbdd fix: clean the message file
Fixes #3
2026-02-06 16:44:42 -05:00
wyj 4b895a0591 fix: clean the home and temdir 2026-02-06 16:28:33 -05:00
wyj 94379cb720 feat: set terminal title 2026-02-06 15:46:51 -05:00
wyj 7587d72578 update: fix PATH 2026-01-19 17:31:33 -05:00
wyj cfe99f928a update: change filedir 2026-01-19 17:30:10 -05:00
wyj 2fc05ba7ea update: safe leaf_update_package_datebase 2026-01-17 16:18:42 -05:00
wyj 30fed4b734 update: add leaf query 2026-01-17 15:17:42 -05:00
wyj caf66028e6 update: add leaf_info to print 2026-01-14 00:27:18 -05:00
wyj d5188a3385 update: update readme 2026-01-11 05:10:21 -05:00
wyj 618ad9222a feat: allow sha512sums ans sha256sums 2026-01-11 05:08:00 -05:00
2 changed files with 128 additions and 20 deletions
+3
View File
@@ -65,6 +65,9 @@
- license 数组, 软件许可证
- sources 数组, 构建过程使用到的所有源码包和补丁,可以是 `https://...` 也可以是文件相对路径
- urls 数组, `sources` 各项对应的地址, 当前仅限使用 `wget` 获取或复制文件
- 以下三个数组有一个即可
- sha512sums 数组, `sources` 各项对应的 sha512 checksum
- sha256sums 数组, `sources` 各项对应的 sha256 checksum
- md5sums 数组, `sources` 各项对应的 md5 checksum
- options 数组, 构建选项, 详见第4小节
- srcdir 字符串, 构建路径(在leaf主程序中定义)
+125 -20
View File
@@ -2,14 +2,15 @@
set -e
shopt -s inherit_errexit 2>/dev/null || true
RED_COLOR="\e[1;31m"
PURPLE_COLOR="\e[1;35m"
GREEN_COLOR="\e[1;32m"
CLEAR_COLOR="\e[0m"
RED_COLOR=$"\e[1;31m"
PURPLE_COLOR=$"\e[1;35m"
GREEN_COLOR=$'\e[1;32m'
CLEAR_COLOR=$'\e[0m'
_DEF_PARALLEL_JOBS=4
_ENV_PARALLEL_JOBS="${PARALLEL_JOBS}"
source /etc/leaf.conf
export PATH="/usr/bin:/usr/sbin"
if [ -n "${_ENV_PARALLEL_JOBS}" ]; then
export PARALLEL_JOBS="${_ENV_PARALLEL_JOBS}"
elif [ -z "${PARALLEL_JOBS}" ]; then
@@ -23,6 +24,7 @@ export TESTSUITEFLAGS="-j${PARALLEL_JOBS}"
export LANG=C
export LC_CTYPE=C.UTF-8
export leaf_flags="CFLAGS CXXFLAGS FCFLAGS FFLAGS RUSTFLAGS"
export LEAF_MESSAGE_DIR="${TEMP_DIR:-/tmp}/leaf-messages"
for flag in ${leaf_flags}; do
export $flag
done
@@ -33,7 +35,12 @@ BUILD_OPTIONS=([strip]="0" [libtool]="0" [zipman]="0")
# global message file list
declare -a LEAF_MESSAGE_FILES=()
leaf_set_title() {
printf '\e]2;%s\a' "$*"
}
main() {
leaf_set_title "leaf"
leaf_check_directories
if [ $# -le 1 ]; then
case $1 in
@@ -102,6 +109,11 @@ main() {
leaf_unpack_package "${_item}"
done
;;
query)
shift
[[ $# -eq 1 ]] || leaf_error "usage: leaf query <atom-or-name>"
leaf_query "$1"
;;
*)
leaf_print_usage
;;
@@ -114,6 +126,10 @@ main() {
# Help functions that give PKGBUILD to use
###########################################################
leaf_info() {
echo -e "${GREEN_COLOR} * $1${CLEAR_COLOR}"
}
leaf_clear_flags() {
for flag in $leaf_flags; do
unset $flag
@@ -242,6 +258,7 @@ leaf_invoke_hooks() {
leaf_merge_package() {
pushd $1 > /dev/null 2>&1
leaf_set_title "leaf: ${PKG_PREFIX}/${PKG_NAME} (Merging)"
local _trace_dir="${TRACE_DIR}/${PKG_PREFIX}/${PKG_NAME}"
local _item _item_conflict _mode _owner _group
local _time="$(date +%Y%m%d%H%M%S)"
@@ -289,6 +306,8 @@ leaf_merge_package() {
done
leaf_invoke_hooks install
popd > /dev/null 2>&1
rm -rf "${LEAFHOME}"
rm -rf "${LEAFTMPDIR}"
}
leaf_parse_options() {
@@ -337,7 +356,7 @@ EOF
leaf_message_init() {
# one log per package invocation
local d="${TEMP_DIR}/leaf-messages/${PKG_PREFIX}/${PKG_NAME}"
local d="${LEAF_MESSAGE_DIR}/${PKG_PREFIX}/${PKG_NAME}"
install -dm755 -- "$d"
LEAF_MESSAGE_FILE="${d}/messages.$$.$(date +%Y%m%d%H%M%S).log"
: > "${LEAF_MESSAGE_FILE}"
@@ -374,9 +393,10 @@ leaf_print_message() {
echo -e "\n${PURPLE_COLOR}* Message from ${prefix}/${name}${CLEAR_COLOR}\n"
cat -- "$f"
rm -f -- "$f"
done
rm -rf -- "${LEAF_MESSAGE_DIR}"/*
LEAF_MESSAGE_FILES=()
unset LEAF_MESSAGE_FILE
}
@@ -539,18 +559,25 @@ leaf_conffile_is_unmodified() {
}
leaf_update_package_database() {
local _item="$(echo "$2" | awk 'BEGIN{FS="/";OFS="\\/"}{print $1,$2}')"
case $1 in
local action=$1 atom=$2 db="${INSTALLED_PACKAGES}"
local tmp="${db}.tmp.$$"
case "$action" in
add)
sed -i "/${_item}/d" "${INSTALLED_PACKAGES}"
echo "$2" >> ${INSTALLED_PACKAGES}
{ [ -f "$db" ] && grep -Fvx -- "$atom" "$db" || :; printf '%s\n' "$atom"; } >"$tmp" &&
mv -f -- "$tmp" "$db"
;;
delete)
sed -i "/${_item}/d" "${INSTALLED_PACKAGES}"
grep -Fvx -- "$atom" "$db" >"$tmp" &&
mv -f -- "$tmp" "$db"
;;
*)
leaf_error "unknown operation leaf_update_package_database ${action}."
;;
esac
}
leaf_update_environment() {
ldconfig
[ ! -r /etc/profile ] || source /etc/profile
@@ -572,6 +599,7 @@ leaf_run_phase() {
return 0
fi
leaf_set_title "leaf: ${PKG_PREFIX}/${PKG_NAME} (${phase})"
echo -e "${GREEN_COLOR}>>> ${PKG_PREFIX}/${PKG_NAME}: ${phase}${CLEAR_COLOR}"
set +e
@@ -600,12 +628,42 @@ leaf_phase_try() {
leaf_prepare_package() {
local url sourcefile index _md5sum local_src
local url sourcefile index _sum local_src
local checksum_algo checksum_arr_name
local decl
leaf_set_title "leaf: ${PKG_PREFIX}/${PKG_NAME} (Fetching)"
distdir="${DIST_DIR}"
filedir="${PKGBUILD_DIR}/${PKG_PREFIX}/${PKG_NAME}"
filedir="${PKGBUILD_DIR}/${PKG_PREFIX}/${pkgname}"
mkdir -pv -- "${distdir}"
# choose checksum type (sha512 -> sha256 -> md5)
if declare -p sha512sums &>/dev/null; then
decl="$(declare -p sha512sums 2>/dev/null)"
[[ "${decl}" == "declare -a "* || "${decl}" == "declare -a"* ]] \
|| leaf_error "sha512sums is defined but is not an array"
checksum_algo="sha512"
checksum_arr_name="sha512sums"
elif declare -p sha256sums &>/dev/null; then
decl="$(declare -p sha256sums 2>/dev/null)"
[[ "${decl}" == "declare -a "* || "${decl}" == "declare -a"* ]] \
|| leaf_error "sha256sums is defined but is not an array"
checksum_algo="sha256"
checksum_arr_name="sha256sums"
elif declare -p md5sums &>/dev/null; then
decl="$(declare -p md5sums 2>/dev/null)"
[[ "${decl}" == "declare -a "* || "${decl}" == "declare -a"* ]] \
|| leaf_error "md5sums is defined but is not an array"
checksum_algo="md5"
checksum_arr_name="md5sums"
else
leaf_error "no checksum list defined: need one of sha512sums/sha256sums/md5sums"
fi
local -n checksums_ref="${checksum_arr_name}"
# enter ${distdir}
pushd "${distdir}" >/dev/null || leaf_error "cannot enter distdir: ${distdir}"
for index in "${!sources[@]}"; do
@@ -618,12 +676,17 @@ leaf_prepare_package() {
if [ -f "${sourcefile}" ]; then
echo "${sourcefile} exists, checking..."
_md5sum="$(md5sum -- "${sourcefile}" | awk '{print $1}')"
if [ "${_md5sum}" = "${md5sums[$index]}" ]; then
echo "md5sum match, skip ${sourcefile}."
case "${checksum_algo}" in
sha512) _sum="$(sha512sum -- "${sourcefile}" | awk '{print $1}')" ;;
sha256) _sum="$(sha256sum -- "${sourcefile}" | awk '{print $1}')" ;;
md5) _sum="$(md5sum -- "${sourcefile}" | awk '{print $1}')" ;;
*) leaf_error "internal error: unknown checksum algo: ${checksum_algo}" ;;
esac
if [ "${_sum}" = "${checksums_ref[$index]}" ]; then
echo "${checksum_algo}sum match, skip ${sourcefile}."
continue
fi
echo "md5sum does not match, refetching..."
echo "${checksum_algo}sum does not match, refetching..."
rm -f -- "${sourcefile}"
fi
@@ -640,9 +703,14 @@ leaf_prepare_package() {
;;
esac
_md5sum="$(md5sum -- "${sourcefile}" | awk '{print $1}')"
if [ "${_md5sum}" != "${md5sums[$index]}" ]; then
leaf_error "md5sums of ${sourcefile}(${_md5sum}) does not match with ${md5sums[$index]}!"
case "${checksum_algo}" in
sha512) _sum="$(sha512sum -- "${sourcefile}" | awk '{print $1}')" ;;
sha256) _sum="$(sha256sum -- "${sourcefile}" | awk '{print $1}')" ;;
md5) _sum="$(md5sum -- "${sourcefile}" | awk '{print $1}')" ;;
*) leaf_error "internal error: unknown checksum algo: ${checksum_algo}" ;;
esac
if [ "${_sum}" != "${checksums_ref[$index]}" ]; then
leaf_error "${checksum_algo}sum of ${sourcefile}(${_sum}) does not match with ${checksums_ref[$index]}!"
fi
done
@@ -820,6 +888,9 @@ leaf_upgrade_package() {
new_trace_dir="${TRACE_DIR}/${new_prefix}/${new_name}"
[ -d "${new_trace_dir}" ] || leaf_error "upgrade: new trace dir missing: ${new_trace_dir}"
rm -rf "${LEAFHOME}"
rm -rf "${LEAFTMPDIR}"
# =========================================================
# 3) REMOVE OLD, but SKIP paths owned by NEW
# =========================================================
@@ -960,6 +1031,40 @@ leaf_show_package() {
echo -e "License: ${license[@]}\n"
}
leaf_query() {
local q="$1"
local trace_root="${TRACE_DIR:-/var/lib/leaf/trace}"
if ! leaf_find_remove_pkgbuild "$q"; then
leaf_error "${q} cannot found in the leaf installed database."
fi
local pkgbuild_path="${trace_root}/${PKG_PREFIX}/${PKG_NAME}/PKGBUILD"
[[ -f "$pkgbuild_path" ]] || leaf_error "query: missing PKGBUILD: $pkgbuild_path"
[[ -r "$pkgbuild_path" ]] || leaf_error "query: PKGBUILD not readable: $pkgbuild_path"
(
set -euo pipefail
unset pkgname pkgver
# shellcheck disable=SC1090
source "$pkgbuild_path"
local full="${PKG_PREFIX}/${PKG_NAME}"
if [[ -t 1 && -z "${NO_COLOR-}" ]]; then
local BOLD=$'\e[1m'
printf '%s%s%s %s %s%s%s\n' \
"${BOLD}" "${full}" "${CLEAR_COLOR}" \
"${pkgname}" \
"${GREEN_COLOR}" "${pkgver}" "${CLEAR_COLOR}"
else
printf '%s %s %s\n' "${full}" "${pkgname}" "${pkgver}"
fi
)
}
leaf_pack_package() {
leaf_build_package
pushd "${srcdir}" > /dev/null 2>&1