Commit 29b57b2c5d9fd1fd7b643b5f844ef50c854d313a

Thomas de Grivel 2023-02-14T17:56:21

wip shpkg_ci

diff --git a/bin/ansi2html b/bin/ansi2html
index d07678e..b780d04 100755
--- a/bin/ansi2html
+++ b/bin/ansi2html
@@ -1,11 +1,21 @@
 #!/bin/sh
-for F; do
+set -e
+
+ansi2html_sed () {
     sed -e 's|&|&|g' \
         -e 's|<|&lt;|g' \
         -e 's|>|&gt;|g' \
         -e "s|$(printf '\33\\[0;34m')|<span style=\"color: blue;\">|g" \
         -e "s|$(printf '\33\\[0;35m')|<span style=\"color: purple;\">|g" \
         -e "s|$(printf '\33\\[0m')|</span>|g" \
-        -e 's|$|<br/>|' \
-        < "$F" > "$F.html"
+        -e 's|$|<br/>|'
+}
+
+if [ $# -gt 0 ]; then
+    for F; do
+        ansi2html_sed < "$F" > "$F.html.tmp"
+        mv "$F.html.tmp" "$F.html"
+    done
+else
+    ansi2html_sed
 done
diff --git a/bin/shpkg_ci b/bin/shpkg_ci
index aaa5ca7..19fad65 100755
--- a/bin/shpkg_ci
+++ b/bin/shpkg_ci
@@ -2,66 +2,141 @@
 
 set -e
 
+# shpkg
+
 . ~/shpkg/etc/shpkg/profile
 
 . ~/shpkg/share/shpkg/shpkg.subr
 
-shpkg_lock shpkg_ci.lock
+verbose 2 shpkg_config
+
+verbose 2 shpkg_lock shpkg_ci.lock
+
+# Configuration
+
+: ${SHPKG_CI_DIR:=~/shpkg_ci}
+SHPKG_CI_LOG_DIR="${SHPKG_CI_DIR}/log/${SHPKG_OS_}/${SHPKG_TARGET}"
+SHPKG_CI_STATUS_DIR="${SHPKG_CI_DIR}/status/${SHPKG_OS_}/${SHPKG_TARGET}"
 
-logs2html () (
-    cd ~/"www/log/${SHPKG_OS}/txt"
-    for F in "${SHPKG_REPO}".*.log; do
-        if ! [ -f "../html/$F.html" ] || [ "$F" -nt "../html/$F.html" ]; then
-            verbose 1 ansi2html "$F"
+log2html () {
+    {
+        echo <<EOF
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8"/>
+    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+    <title>
+EOF
+        echo <<EOF
+</title>
+    <link rel="stylesheet" href="/_assets/app.css">
+    <script defer type="text/javascript" src="/_assets/app.js"></script>
+    <link rel="icon" type="image/png" sizes="128x128" href="/_images/ci.128.png">
+    <link rel="icon" type="image/png" sizes="64x64" href="/_images/ci.64.png">
+    <link rel="icon" type="image/png" sizes="32x32" href="/_images/ci.32.png">
+    <link rel="icon" type="image/png" sizes="16x16" href="/_images/ci.16.png">
+  </head>
+  <body>
+    <h1>
+EOF
+        echo "$1" | ansi2html
+        echo <<EOF
+</h1>
+EOF
+        ansi2html < "$1"
+        echo <<EOF
+    <div class="footer">
+      <a href="https://git.kmx.io/kmx.io/shpkg">shpkg_ci</a>
+    </div>
+  </body>
+</html>
+EOF
+    } > "$1.html"
+}
+
+logs2html () {
+    for F in "${SHPKG_CI_LOG_DIR}"/"${SHPKG_REPO}".*.log; do
+        if ! [ -f "$F.html" ] || [ "$F" -nt "$F.html" ]; then
+            verbose 2 log2html "$F"
         fi
     done
-    verbose -k 1 mv ~/www/log/"${SHPKG_OS}"/txt/*.html ~/www/log/"${SHPKG_OS}"/html/
-)
+}
+
+# state machine :
+#  any -> running
+#  running -> ko, ok
+# additionaly status will be changed to ko if aborted while running
+shpkg_ci_status () {
+    if [ "x$2" = "xrunning" ]; then
+        echo "$2" > "${SHPKG_CI_STATUS_DIR}/$1"
+        trap "shpkg_ci_status_trap $1" EXIT
+    elif [ "x$(< "${SHPKG_CI_STATUS_DIR}/$1")" = "xrunning" ]; then
+        echo "$2" > "${SHPKG_CI_STATUS_DIR}/$1"
+    fi
+}
+
+shpkg_ci_status_trap () {
+    if [ "x$(< "${SHPKG_CI_STATUS_DIR}/$1")" = "xrunning" ]; then
+        echo ko > "${SHPKG_CI_STATUS_DIR}/$1"
+    fi
+}
 
 main () {
-    verbose 2 shpkg_config
-    SHPKG_CI_STATUS=0
-    if ! [ -d ~/"www/log/${SHPKG_OS}/txt" ]; then
-        verbose 1 mkdir -p ~/"www/log/${SHPKG_OS}/txt"
+    if ! [ -d "${SHPKG_CI_LOG_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_CI_LOG_DIR}"
     fi
-    if ! [ -d ~/"www/log/${SHPKG_OS}/html" ]; then
-        verbose 1 mkdir -p ~/"www/log/${SHPKG_OS}/html"
+    if ! [ -d "${SHPKG_CI_STATUS_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_CI_STATUS_DIR}"
     fi
+    verbose 2 shpkg_ci_status "shpkg_ci.status" running
     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"
+        verbose 2 shpkg_tag_log "${SHPKG_REPO}.ci.log"
+        verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" running
         # first bootstrap
         if ! [ -d ~/"shpkg_${SHPKG_REPO}" ]; then
             ( verbose 2 shpkg_bootstrap ~/"shpkg_${SHPKG_REPO}"; )
         else
             # check upgrade
             ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
-              if ! verbose 2 shpkg upgrade "${SHPKG_REPO}"; then
-                  SHPKG_CI_STATUS=10
+              verbose 2 shpkg_ci_status "${SHPKG_REPO}.upgrade.status" running
+              if verbose 2 shpkg upgrade "${SHPKG_REPO}"; then
+                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.upgrade.status" ok
+              else
+                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.upgrade.status" ko
+                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" ko
               fi; )
-            if verbose 1 cp -p ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_OS}/txt"; then
+            if verbose 1 cp -p ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log "${SHPKG_CI_LOG_DIR}"; then
                 verbose 2 logs2html
             fi
         fi
         ( verbose 2 shpkg_bootstrap -f ~/"shpkg_${SHPKG_REPO}"; )
         ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
-          if ! verbose 2 shpkg install "${SHPKG_REPO}"; then
-              SHPKG_CI_STATUS=10
+          verbose 2 shpkg_ci_status "${SHPKG_REPO}.install.status" running
+          if verbose 2 shpkg install "${SHPKG_REPO}"; then
+              verbose 2 shpkg_ci_status "${SHPKG_REPO}.install.status" ok
+          else
+              verbose 2 shpkg_ci_status "${SHPKG_REPO}.install.status" ko
+              verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" ko
           fi; )
         SHPKG_CI_COPY=No
-        if verbose 1 cp -p ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_OS}/txt"; then
+        if verbose 1 cp -p ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log "${SHPKG_CI_LOG_DIR}"; then
             SHPKG_CI_COPY=Yes
         fi
-        if verbose 1 cp -p ~/"shpkg/var/log/shpkg/tags/${SHPKG_REPO}.ci.log" ~/"www/log/${SHPKG_OS}/txt"; then
+        if verbose 1 cp -p ~/"shpkg/var/log/shpkg/tags/${SHPKG_REPO}.ci.log" "${SHPKG_CI_LOG_DIR}"; then
             SHPKG_CI_COPY=Yes
         fi
         if [ "x${SHPKG_CI_COPY}" = "xYes" ]; then
             verbose 2 logs2html
         fi
+        verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" ok
     done
+    verbose 2 shpkg_ci_status "shpkg_ci.status" ok
 }
 
 shpkg_log shpkg_ci main "$@"
diff --git a/share/shpkg/shpkg.subr b/share/shpkg/shpkg.subr
index 2676730..eee390e 100644
--- a/share/shpkg/shpkg.subr
+++ b/share/shpkg/shpkg.subr
@@ -127,7 +127,7 @@ shpkg_lock () {
     if ! [ -d "${SHPKG_LOCK_DIR}" ]; then
         verbose 1 mkdir -p "${SHPKG_LOCK_DIR}"
     fi
-    while ! { set -C; 2>/dev/null >"${SHPKG_LOCK}"; }; do
+    while ! ( set -C; 2>/dev/null >"${SHPKG_LOCK}"; ); do
         sleep 1
     done
     trap "rm -f '${SHPKG_LOCK}'" EXIT
@@ -161,8 +161,11 @@ shpkg_config () {
     SHPKG_TAG_DIR="${SHPKG_TARGET_DIR}/tag"
     # OS detection
     SHPKG_OS="$(uname)"
+    SHPKG_OS_="${SHPKG_OS}"
     if [ "x${SHPKG_OS}" = "xLinux" ]; then
-        SHPKG_OS="${SHPKG_OS}/$(uname -a | grep -io -e Debian -e gentoo -e Ubuntu)"
+        SHPKG_DISTRIB="$(uname -a | grep -io -e Debian -e gentoo -e Ubuntu | sed -e 's|gentoo|Gentoo|')"
+        SHPKG_OS="${SHPKG_OS}/${SHPKG_DISTRIB}"
+        SHPKG_OS_="${SHPKG_OS}_${SHPKG_DISTRIB}"
     fi
     SHPKG_OS_DIR="${SHPKG_SHARE_DIR}/${SHPKG_OS}"
     # Number of CPU cores