Commit b79dac6df58d55ec949821d4d6b2bb4fb98ae289

Thomas de Grivel 2023-02-14T01:15:22

shpkg_ci

diff --git a/bin/shpkg b/bin/shpkg
index 598168a..da76848 100755
--- a/bin/shpkg
+++ b/bin/shpkg
@@ -129,18 +129,4 @@ main () {
     fi
 }
 
-# Logging
-
-redirect_err () {
-    "$@" 2>&1
-}
-
-log () {
-    if ! [ -d "${SHPKG_LOG_DIR}" ]; then
-        verbose 1 mkdir -p "${SHPKG_LOG_DIR}"
-    fi
-    shpkg_start_logging
-    redirect_err "$@" | tee -a "${SHPKG_LOG}"
-}
-
-log main "$@"
+shpkg_log shpkg shpkg_lock shpkg main "$@"
diff --git a/bin/shpkg_ci b/bin/shpkg_ci
index 2f250a6..e78db0c 100755
--- a/bin/shpkg_ci
+++ b/bin/shpkg_ci
@@ -1,42 +1,40 @@
 #!/bin/sh
 
+. ~/shpkg/etc/shpkg/profile
+. ~/shpkg/share/shpkg/shpkg.subr
+
 logs2html () {
     for F in ~/"www/log/${SHPKG_TARGET}/${SHPKG_REPO}".*.log; do
         if ! [ -f "$F.html" ]; then
-            ansi2html "$F"
+            verbose 1 ansi2html "$F"
         fi
     done
 }
 
-if [ "x${CC}" = "x" ]; then
-    if which cc >/dev/null; then
-        CC=cc
-    elif which gcc >/dev/null; then
-        CC=gcc
-    elif which egcc >/dev/null; then
-        CC=egcc
-    else
-        err 1 "C compiler not found."
+main () {
+    if ! [ -d ~/"www/log/${SHPKG_TARGET}" ]; then
+        verbose 1 mkdir -p ~/"www/log/${SHPKG_TARGET}"
     fi
-fi
-SHPKG_TARGET="$(LC_ALL=C.UTF-8 ${CC} -v 2>&1 | grep '^Target: ' | head -n 1 | cut -c 9-)"
-
-if ! [ -d ~/"www/log/${SHPKG_TARGET}" ]; then
-    mkdir -p ~/"www/log/${SHPKG_TARGET}"
-fi
+    for SHPKG_REPO; do
+        SHPKG_REPO_DIR="$(shpkg_dir "${SHPKG_REPO}")"
+        if [ "x${SHPKG_REPO_DIR}" = "x" ]; then
+            err 1 "shpkg_ci: unknown repo: $SHPKG_REPO"
+        fi
+        shpkg_tag_log "${SHPKG_REPO}.ci.log"
+        if ! [ -d ~/"shpkg_${SHPKG_REPO}" ]; then
+            verbose 2 shpkg_bootstrap ~/"shpkg_${SHPKG_REPO}"
+        fi
+        ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
+          verbose 2 shpkg upgrade "${SHPKG_REPO}"; )
+        verbose 1 cp ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}/"
+        verbose 2 logs2html
+        verbose 2 shpkg_bootstrap -f ~/"shpkg_${SHPKG_REPO}"
+        ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
+          verbose 2 shpkg install "${SHPKG_REPO}"; )
+        verbose 1 cp ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}"
+        verbose 1 cp ~/"shpkg/var/log/shpkg/tags/${SHPKG_REPO}.ci.log" ~/"www/log/${SHPKG_TARGET}"
+        verbose 2 logs2html
+    done
+}
 
-for SHPKG_REPO; do
-    export SHPKG_DIR=~/"shpkg_${SHPKG_REPO}"
-    if ! [ -d "${SHPKG_DIR}" ]; then
-        shpkg_bootstrap
-    fi
-    ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
-      shpkg upgrade "${SHPKG_REPO}"
-      cp ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}/"
-      logs2html
-      shpkg_bootstrap -f ~/"shpkg_${SHPKG_REPO}"; )
-    ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
-      shpkg install "${SHPKG_REPO}"
-      cp ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}"
-      logs2html; )
-done
+shpkg_log shpkg_ci shpkg_lock shpkg_ci main "$@"
diff --git a/share/shpkg/shpkg.subr b/share/shpkg/shpkg.subr
index 14883c0..de15040 100644
--- a/share/shpkg/shpkg.subr
+++ b/share/shpkg/shpkg.subr
@@ -62,9 +62,20 @@ err () {
 
 # Logging
 
+shpkg_log () {
+    SHPKG_NAME="$1"
+    shift
+    shpkg_start_logging "${SHPKG_NAME}"
+    shpkg_redirect_err "$@" | tee -a "${SHPKG_LOG}"
+}
+
+shpkg_redirect_err () {
+    "$@" 2>&1
+}
+
 shpkg_start_logging () {
     SHPKG_LOG_ALL_DIR="${SHPKG_LOG_DIR}/all"
-    SHPKG_LOG="${SHPKG_LOG_ALL_DIR}/shpkg_${SHPKG_DATE}_$$.log"
+    SHPKG_LOG="${SHPKG_LOG_ALL_DIR}/$1_${SHPKG_DATE}_$$.log"
     SHPKG_LOG_RELATIVE="../all/$(basename "${SHPKG_LOG}")"
     SHPKG_LOG_TAGS_DIR="${SHPKG_LOG_DIR}/tags"
     SHPKG_LOG_TMP_DIR="${SHPKG_LOG_DIR}/tmp"
@@ -97,6 +108,28 @@ shpkg_tag_log () {
           verbose 1 mv "${SHPKG_LOG_TAG}.tmp" "${SHPKG_LOG_TAG}"; )
 }
 
+# Locking
+
+shpkg_lock () {
+    SHPKG_LOCK_DIR="${SHPKG_DIR}/var/lib/shpkg/locks"
+    SHPKG_LOCK_NAME="$1"
+    shift
+    SHPKG_LOCK="${SHPKG_LOCK_DIR}/${SHPKG_LOCK_NAME}"
+    verbose -n 2 "Locking ${SHPKG_LOCK}"
+    if ! [ -d "${SHPKG_LOCK_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_LOCK_DIR}"
+    fi
+    ( verbose 1 cd "${SHPKG_LOCK_DIR}" && 
+          while ! mkdir "${SHPKG_LOCK_NAME}" 2>/dev/null; do
+                sleep 1
+          done
+      cd "${SHPKG_LOCK_NAME}" &&
+          ( "$@"; )
+      cd .. &&
+          verbose -n 2 "Unlocking ${SHPKG_LOCK}" &&
+          rmdir "${SHPKG_LOCK_NAME}"; )
+}
+
 # C compiler
 
 if [ "x${CC}" = "x" ]; then